|
|
|
@ -5,27 +5,32 @@ import android.app.Activity;
@@ -5,27 +5,32 @@ import android.app.Activity;
|
|
|
|
|
import android.content.Intent; |
|
|
|
|
import android.content.pm.PackageManager; |
|
|
|
|
import android.os.Bundle; |
|
|
|
|
import android.support.v4.app.ActivityCompat; |
|
|
|
|
import android.view.View; |
|
|
|
|
import android.widget.Button; |
|
|
|
|
import android.widget.TextView; |
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Method; |
|
|
|
|
|
|
|
|
|
//dangerous perms, per https://developer.android.com/guide/topics/permissions/normal-permissions.html :
|
|
|
|
|
//android.permission.WRITE_EXTERNAL_STORAGE
|
|
|
|
|
public class I2PDPermsAskerActivity extends Activity { |
|
|
|
|
|
|
|
|
|
private static final int PERMISSION_WRITE_EXTERNAL_STORAGE = 0; |
|
|
|
|
|
|
|
|
|
private View mLayout; |
|
|
|
|
private Button button_request_write_ext_storage_perms; |
|
|
|
|
private TextView textview_retry; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void onCreate(Bundle savedInstanceState) { |
|
|
|
|
super.onCreate(savedInstanceState); |
|
|
|
|
//if less than Android 6, no runtime perms req system present
|
|
|
|
|
if (android.os.Build.VERSION.SDK_INT < 23) { |
|
|
|
|
startMainActivity(); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setContentView(R.layout.activity_perms_asker); |
|
|
|
|
mLayout = findViewById(R.id.main_layout); |
|
|
|
|
button_request_write_ext_storage_perms = (Button) findViewById(R.id.button_request_write_ext_storage_perms); |
|
|
|
|
textview_retry = (TextView) findViewById(R.id.textview_retry); |
|
|
|
|
|
|
|
|
@ -43,14 +48,37 @@ public class I2PDPermsAskerActivity extends Activity {
@@ -43,14 +48,37 @@ public class I2PDPermsAskerActivity extends Activity {
|
|
|
|
|
textview_retry.setVisibility(TextView.GONE); |
|
|
|
|
button_request_write_ext_storage_perms.setVisibility(Button.GONE); |
|
|
|
|
|
|
|
|
|
// Here, thisActivity is the current activity
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, |
|
|
|
|
Manifest.permission.WRITE_EXTERNAL_STORAGE) |
|
|
|
|
!= PackageManager.PERMISSION_GRANTED) { |
|
|
|
|
Method methodCheckPermission; |
|
|
|
|
Method method_shouldShowRequestPermissionRationale; |
|
|
|
|
Method method_requestPermissions; |
|
|
|
|
try { |
|
|
|
|
methodCheckPermission = getClass().getMethod("checkSelfPermission", String.class); |
|
|
|
|
method_shouldShowRequestPermissionRationale = |
|
|
|
|
getClass().getMethod("shouldShowRequestPermissionRationale", String.class); |
|
|
|
|
method_requestPermissions = |
|
|
|
|
getClass().getMethod("requestPermissions", String[].class, int.class); |
|
|
|
|
} catch (NoSuchMethodException e) { |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} |
|
|
|
|
Integer resultObj; |
|
|
|
|
try { |
|
|
|
|
resultObj = (Integer) methodCheckPermission.invoke( |
|
|
|
|
this, Manifest.permission.WRITE_EXTERNAL_STORAGE); |
|
|
|
|
} catch (Throwable e) { |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (resultObj != PackageManager.PERMISSION_GRANTED) { |
|
|
|
|
|
|
|
|
|
// Should we show an explanation?
|
|
|
|
|
if (ActivityCompat.shouldShowRequestPermissionRationale(this, |
|
|
|
|
Manifest.permission.WRITE_EXTERNAL_STORAGE)) { |
|
|
|
|
Boolean aBoolean; |
|
|
|
|
try { |
|
|
|
|
aBoolean = (Boolean) method_shouldShowRequestPermissionRationale.invoke(this, |
|
|
|
|
Manifest.permission.WRITE_EXTERNAL_STORAGE); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} |
|
|
|
|
if (aBoolean) { |
|
|
|
|
|
|
|
|
|
// Show an explanation to the user *asynchronously* -- don't block
|
|
|
|
|
// this thread waiting for the user's response! After the user
|
|
|
|
@ -62,13 +90,13 @@ public class I2PDPermsAskerActivity extends Activity {
@@ -62,13 +90,13 @@ public class I2PDPermsAskerActivity extends Activity {
|
|
|
|
|
|
|
|
|
|
// No explanation needed, we can request the permission.
|
|
|
|
|
|
|
|
|
|
ActivityCompat.requestPermissions(this, |
|
|
|
|
try { |
|
|
|
|
method_requestPermissions.invoke(this, |
|
|
|
|
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, |
|
|
|
|
PERMISSION_WRITE_EXTERNAL_STORAGE); |
|
|
|
|
|
|
|
|
|
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
|
|
|
|
|
// app-defined int constant. The callback method gets the
|
|
|
|
|
// result of the request.
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else startMainActivity(); |
|
|
|
|
} |
|
|
|
@ -121,9 +149,20 @@ public class I2PDPermsAskerActivity extends Activity {
@@ -121,9 +149,20 @@ public class I2PDPermsAskerActivity extends Activity {
|
|
|
|
|
// Make sure the request was successful
|
|
|
|
|
if (resultCode == RESULT_OK) { |
|
|
|
|
// Request the permission
|
|
|
|
|
ActivityCompat.requestPermissions(I2PDPermsAskerActivity.this, |
|
|
|
|
Method method_requestPermissions; |
|
|
|
|
try { |
|
|
|
|
method_requestPermissions = |
|
|
|
|
getClass().getMethod("requestPermissions", String[].class, int.class); |
|
|
|
|
} catch (NoSuchMethodException e) { |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
method_requestPermissions.invoke(this, |
|
|
|
|
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, |
|
|
|
|
PERMISSION_WRITE_EXTERNAL_STORAGE); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
finish(); //close the app
|
|
|
|
|
} |
|
|
|
|