Merge pull request #41 from lbryio/public-downloads

Added required permission request for WRITE_EXTERNAL_STORAGE on Android Marshmallow and higher (6+)
This commit is contained in:
akinwale 2018-03-27 04:44:25 +01:00 committed by GitHub
commit 2949484836
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,10 +4,16 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.app.Activity; import android.app.Activity;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.content.Intent; import android.content.Intent;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.content.SharedPreferences;
import android.Manifest;
import android.net.Uri; import android.net.Uri;
import android.provider.Settings; import android.provider.Settings;
import android.widget.Toast;
import com.brentvatne.react.ReactVideoPackage; import com.brentvatne.react.ReactVideoPackage;
import com.facebook.react.common.LifecycleState; import com.facebook.react.common.LifecycleState;
@ -19,11 +25,17 @@ import com.facebook.react.shell.MainReactPackage;
import io.lbry.lbrynet.reactpackages.LbryReactPackage; import io.lbry.lbrynet.reactpackages.LbryReactPackage;
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler { public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
private static final int OVERLAY_PERMISSION_REQ_CODE = 101; private static final int OVERLAY_PERMISSION_REQ_CODE = 101;
private static final int STORAGE_PERMISSION_REQ_CODE = 201;
private ReactRootView mReactRootView; private ReactRootView mReactRootView;
private ReactInstanceManager mReactInstanceManager; 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 * Flag which indicates whether or not the service is running. Will be updated in the
* onResume method. * onResume method.
@ -32,11 +44,17 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) { // Request external storage permission on Android version >= 6
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Uri.parse("package:" + getPackageName())); // Should we show an explanation?
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE); 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);
}
} }
} }
@ -75,6 +93,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 @Override
public void invokeDefaultOnBackPressed() { public void invokeDefaultOnBackPressed() {
super.onBackPressed(); super.onBackPressed();
@ -105,6 +147,16 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
@Override @Override
protected void onDestroy() { 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);
}
}
super.onDestroy(); super.onDestroy();
if (mReactInstanceManager != null) { if (mReactInstanceManager != null) {
@ -119,9 +171,9 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand
} else { } else {
super.onBackPressed(); super.onBackPressed();
} }
} }
private boolean isServiceRunning(Class<?> serviceClass) { private boolean isServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo serviceInfo : manager.getRunningServices(Integer.MAX_VALUE)) { for (ActivityManager.RunningServiceInfo serviceInfo : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(serviceInfo.service.getClassName())) { if (serviceClass.getName().equals(serviceInfo.service.getClassName())) {