| 16 | import android.util.TypedValue; |
| 17 | |
| 18 | public class BaseActivity extends Activity |
| 19 | { |
| 20 | protected final int PERMISSION_READ_EXTERNAL_STORAGE = 1; |
| 21 | protected final int PERMISSION_WRITE_EXTERNAL_STORAGE = 2; |
| 22 | |
| 23 | @Override |
| 24 | protected void onCreate(Bundle savedInstanceState) |
| 25 | { |
| 26 | super.onCreate(savedInstanceState); |
| 27 | |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Requests given permission. If the permission has been denied previously, a Dialog will prompt |
| 32 | * the user to grant the permission, otherwise it is requested directly. |
| 33 | */ |
| 34 | protected void requestPermission(final String permission, String message, final int requestCode) |
| 35 | { |
| 36 | if(ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) |
| 37 | { |
| 38 | if(ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) |
| 39 | { |
| 40 | // Show an explanation to the user *asynchronously* -- don't block |
| 41 | // this thread waiting for the user's response! After the user |
| 42 | // sees the explanation, try again to request the permission. |
| 43 | new AlertDialog.Builder(this) |
| 44 | .setTitle(R.string.permission_request) |
| 45 | .setMessage(message) |
| 46 | .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() |
| 47 | { |
| 48 | @Override |
| 49 | public void onClick(DialogInterface dialog, int which) |
| 50 | { |
| 51 | requestPermissions(new String[]{ permission }, requestCode); |
| 52 | } |
| 53 | }) |
| 54 | .setNegativeButton(android.R.string.cancel, null) |
| 55 | .show(); |
| 56 | } |
| 57 | else // No explanation needed, we can request the permission. |
| 58 | ActivityCompat.requestPermissions(this, new String[]{ permission }, requestCode); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * checkSelfPermission returning PERMISSION_GRANTED for revoked permission with targetSdkVersion <= 22 |
| 64 | * http://stackoverflow.com/questions/33407250/checkselfpermission-method-is-not-working-in-targetsdkversion-22 |
| 65 | */ |
| 66 | public boolean checkSelfPermission(Context context, String permission) |
| 67 | { |
| 68 | // For Android < Android Marshmallow(23), self permissions are always granted. |
| 69 | boolean result = true; |
| 70 | |
| 71 | if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) |
| 72 | { |
| 73 | // https://developer.android.com/guide/topics/manifest/uses-sdk-element.html |
| 74 | int targetSdkVersion = Build.VERSION_CODES.BASE; |
| 75 | try |
nothing calls this directly
no outgoing calls
no test coverage detected