mirror of
				https://github.com/PurpleI2P/i2pd.git
				synced 2025-11-04 08:30:46 +00:00 
			
		
		
		
	fixed #1104 - android
This commit is contained in:
		
							parent
							
								
									1de4c2e8c6
								
							
						
					
					
						commit
						3bb4151074
					
				
					 2 changed files with 58 additions and 23 deletions
				
			
		| 
						 | 
				
			
			@ -17,10 +17,6 @@ repositories {
 | 
			
		|||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
dependencies {
 | 
			
		||||
    compile 'com.android.support:support-v4:25.3.1'
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
android {
 | 
			
		||||
    compileSdkVersion 25
 | 
			
		||||
    buildToolsVersion "25.0.2"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 {
 | 
			
		|||
        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 {
 | 
			
		|||
 | 
			
		||||
                // No explanation needed, we can request the permission.
 | 
			
		||||
 | 
			
		||||
                ActivityCompat.requestPermissions(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.
 | 
			
		||||
                try {
 | 
			
		||||
                    method_requestPermissions.invoke(this,
 | 
			
		||||
                            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
 | 
			
		||||
                            PERMISSION_WRITE_EXTERNAL_STORAGE);
 | 
			
		||||
                } catch (Exception e) {
 | 
			
		||||
                    throw new RuntimeException(e);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        } else startMainActivity();
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -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,
 | 
			
		||||
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
 | 
			
		||||
                        PERMISSION_WRITE_EXTERNAL_STORAGE);
 | 
			
		||||
                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
 | 
			
		||||
            }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue