Browse Source

fixed #1104 - android

pull/1117/head
hypnosis-i2p 7 years ago
parent
commit
3bb4151074
  1. 4
      android/build.gradle
  2. 77
      android/src/org/purplei2p/i2pd/I2PDPermsAskerActivity.java

4
android/build.gradle

@ -17,10 +17,6 @@ repositories {
} }
} }
dependencies {
compile 'com.android.support:support-v4:25.3.1'
}
android { android {
compileSdkVersion 25 compileSdkVersion 25
buildToolsVersion "25.0.2" buildToolsVersion "25.0.2"

77
android/src/org/purplei2p/i2pd/I2PDPermsAskerActivity.java

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

Loading…
Cancel
Save