|
| 1 | +package org.volkszaehler.volkszaehlerapp; |
| 2 | + |
| 3 | +import android.Manifest; |
| 4 | +import android.content.Intent; |
| 5 | +import android.content.pm.PackageManager; |
| 6 | +import android.os.Build; |
| 7 | +import android.app.Activity; |
| 8 | +import android.support.v4.app.ActivityCompat; |
| 9 | +import android.view.Menu; |
| 10 | +import android.view.MenuItem; |
| 11 | +import android.widget.Toast; |
| 12 | + |
| 13 | +public class CustomMenuActivity extends Activity { |
| 14 | + private Menu menu; |
| 15 | + private int itemId = 0; |
| 16 | + |
| 17 | + |
| 18 | + private boolean isStoragePermissionGranted() { |
| 19 | + if (Build.VERSION.SDK_INT >= 23) { |
| 20 | + if(R.id.backup_settings == itemId) { |
| 21 | + if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { |
| 22 | + //Log.d("Tools Permission check ","Permission is granted"); |
| 23 | + return true; |
| 24 | + } else { |
| 25 | + //Log.d("Tools Permission check ","Permission is revoked"); |
| 26 | + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); |
| 27 | + return false; |
| 28 | + } |
| 29 | + } else if (R.id.restore_settings == itemId) { |
| 30 | + if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { |
| 31 | + //Log.d("Tools Permission check ","Permission is granted"); |
| 32 | + return true; |
| 33 | + } else { |
| 34 | + //Log.d("Tools Permission check ","Permission is revoked"); |
| 35 | + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); |
| 36 | + return false; |
| 37 | + } |
| 38 | + } |
| 39 | + return false; |
| 40 | + } |
| 41 | + else { //permission is automatically granted on sdk<23 upon installation |
| 42 | + //Log.d("Tools Permission check ","Permission is granted automatically"); |
| 43 | + return true; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { |
| 49 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 50 | + if(grantResults[0]== PackageManager.PERMISSION_GRANTED){ |
| 51 | + //Log.d("Tools Permission check ","Permission: "+permissions[0]+ " was "+grantResults[0]); |
| 52 | + //resume tasks needing this permission |
| 53 | + onOptionsItemSelected(menu.findItem(itemId)); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 59 | + this.menu = menu; |
| 60 | + // Inflate the menu; this adds items to the action bar if it is present. |
| 61 | + getMenuInflater().inflate(R.menu.main, menu); |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 67 | + itemId = item.getItemId(); |
| 68 | + switch (itemId) { |
| 69 | + case R.id.action_settings: |
| 70 | + startActivityForResult(new Intent(this, Preferences.class), 1); |
| 71 | + return (true); |
| 72 | + case R.id.backup_settings: |
| 73 | + if(isStoragePermissionGranted()) { |
| 74 | + boolean saved = Tools.saveFile(getApplicationContext()); |
| 75 | + if (saved) { |
| 76 | + Toast.makeText(this, R.string.saved, Toast.LENGTH_SHORT).show(); |
| 77 | + } else { |
| 78 | + Toast.makeText(this, R.string.notsaved, Toast.LENGTH_SHORT).show(); |
| 79 | + } |
| 80 | + } |
| 81 | + return (true); |
| 82 | + case R.id.restore_settings: |
| 83 | + if(isStoragePermissionGranted()) { |
| 84 | + boolean restored = Tools.loadFile(getApplicationContext()); |
| 85 | + if (restored) { |
| 86 | + Toast.makeText(this, R.string.restored, Toast.LENGTH_SHORT).show(); |
| 87 | + } else { |
| 88 | + Toast.makeText(this, R.string.notrestored, Toast.LENGTH_SHORT).show(); |
| 89 | + } |
| 90 | + } |
| 91 | + return (true); |
| 92 | + case R.id.about: |
| 93 | + return Tools.showAboutDialog(this); |
| 94 | + |
| 95 | + default: |
| 96 | + break; |
| 97 | + } |
| 98 | + return super.onOptionsItemSelected(item); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments