Added required permission request for WRITE_EXTERNAL_STORAGE on Android Marshmallow and higher (6+) #41

Merged
akinwale merged 1 commit from public-downloads into master 2018-03-27 05:44:26 +02:00

View file

@ -4,10 +4,16 @@ import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.content.Intent;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.SharedPreferences;
import android.Manifest;
import android.net.Uri;
import android.provider.Settings;
import android.widget.Toast;
import com.brentvatne.react.ReactVideoPackage;
import com.facebook.react.common.LifecycleState;
@ -19,11 +25,17 @@ import com.facebook.react.shell.MainReactPackage;
import io.lbry.lbrynet.reactpackages.LbryReactPackage;
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
private static final int OVERLAY_PERMISSION_REQ_CODE = 101;
private static final int STORAGE_PERMISSION_REQ_CODE = 201;
private ReactRootView mReactRootView;
private ReactInstanceManager mReactInstanceManager;
public static final String SHARED_PREFERENCES_NAME = "LBRY";
/**
* Flag which indicates whether or not the service is running. Will be updated in the
* onResume method.
@ -32,11 +44,17 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
@Override
protected void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Request external storage permission on Android version >= 6
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(this,
"LBRY requires access to your device storage to be able to download files and media.", Toast.LENGTH_SHORT).show();
} else {
ActivityCompat.requestPermissions(this,
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, STORAGE_PERMISSION_REQ_CODE);
}
}
}
@ -74,6 +92,30 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case STORAGE_PERMISSION_REQ_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
}
} else {
// Permission not granted. Show a message and terminate the application
Toast.makeText(this,
"LBRY requires access to your device storage to be able to download files and media." +
" Please enable the storage permission and restart the app.", Toast.LENGTH_LONG).show();
if (serviceRunning) {
ServiceHelper.stop(this, LbrynetService.class);
}
finish();
}
break;
}
}
@Override
public void invokeDefaultOnBackPressed() {
@ -105,8 +147,18 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
@Override
protected void onDestroy() {
// check service running setting and end it here
SharedPreferences sp = getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
boolean shouldKeepDaemonRunning = sp.getBoolean("keepDaemonRunning", true);
if (!shouldKeepDaemonRunning) {
serviceRunning = isServiceRunning(LbrynetService.class);
if (serviceRunning) {
ServiceHelper.stop(this, LbrynetService.class);
IGassmann commented 2018-03-26 23:43:14 +02:00 (Migrated from github.com)
Review

Should this method do something if the requestCode doesn't match any declared case? If that's the case, a default case should be added to the switch statement.

Should this method do something if the `requestCode` doesn't match any declared case? If that's the case, a default case should be added to the switch statement.
akinwale commented 2018-03-27 00:01:13 +02:00 (Migrated from github.com)
Review

We only care about the request code for the storage permission, so there is no need to handle any other request code.

We only care about the request code for the storage permission, so there is no need to handle any other request code.
}
}
super.onDestroy();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostDestroy(this);
}
@ -119,9 +171,9 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
} else {
super.onBackPressed();
}
}
}
private boolean isServiceRunning(Class<?> serviceClass) {
private boolean isServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo serviceInfo : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(serviceInfo.service.getClassName())) {