Skip to content

Commit

Permalink
Request write storage permissions only if you add scripts. Saving the…
Browse files Browse the repository at this point in the history
… tile cache does not require additional permissions.
  • Loading branch information
modos189 committed Dec 10, 2018
1 parent 3c7a952 commit cdb7e89
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,6 @@ public void updatePlugins(final boolean force) {
final long lastUpdated = mPrefs.getLong("pref_last_plugin_update", 0);
final long now = System.currentTimeMillis();

final IITC_Mobile iitc = (IITC_Mobile) mActivity;
if (!isWriteStoragePermissionGranted(iitc)) {
Toast.makeText(mActivity, "Permissions are required if you want to use user scripts or cache on SDCard", Toast.LENGTH_LONG).show();
}

// return if no update wanted
if ((now - lastUpdated < mUpdateInterval) && !force) return;
// get the plugin preferences
Expand All @@ -364,19 +359,6 @@ public void setUpdateInterval(final int interval) {
mUpdateInterval = 1000 * 60 * 60 * 24 * interval;
}

private boolean isWriteStoragePermissionGranted(IITC_Mobile iitc) {
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(iitc, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(iitc, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 3);
return true;
} else {
return false;
}
} else { //permission is automatically granted on sdk<23 upon installation
return true;
}
}

private class FileRequest extends WebResourceResponse implements ResponseHandler, Runnable {
private Intent mData;
private final String mFunctionName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.exarhteam.iitc_mobile.prefs;

import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
Expand All @@ -19,6 +23,7 @@
import android.widget.Toast;

import org.exarhteam.iitc_mobile.IITC_FileManager;
import org.exarhteam.iitc_mobile.IITC_Mobile;
import org.exarhteam.iitc_mobile.IITC_NotificationHelper;
import org.exarhteam.iitc_mobile.Log;
import org.exarhteam.iitc_mobile.R;
Expand All @@ -38,6 +43,7 @@
public class PluginPreferenceActivity extends PreferenceActivity {

private final static int COPY_PLUGIN_REQUEST = 1;
private static final int PERMISSION_REQUEST_CODE = 3;

private List<Header> mHeaders;
// we use a tree map to have a map with alphabetical order
Expand Down Expand Up @@ -135,23 +141,50 @@ public boolean onOptionsItemSelected(final MenuItem item) {
onBackPressed();
return true;
case R.id.menu_plugins_add:
// create the chooser Intent
final Intent target = new Intent(Intent.ACTION_GET_CONTENT);
// iitcm only parses *.user.js scripts
target.setType("file/*");
target.addCategory(Intent.CATEGORY_OPENABLE);

try {
startActivityForResult(Intent.createChooser(target, "Choose file"), COPY_PLUGIN_REQUEST);
} catch (final ActivityNotFoundException e) {
Toast.makeText(this, "No activity to select a file found." +
"Please install a file browser of your choice!", Toast.LENGTH_LONG).show();
if (checkWriteStoragePermissionGranted()) {
// create the chooser Intent
final Intent target = new Intent(Intent.ACTION_GET_CONTENT);
// iitcm only parses *.user.js scripts
target.setType("file/*");
target.addCategory(Intent.CATEGORY_OPENABLE);

try {
startActivityForResult(Intent.createChooser(target, "Choose file"), COPY_PLUGIN_REQUEST);
} catch (final ActivityNotFoundException e) {
Toast.makeText(this, "No activity to select a file found." +
"Please install a file browser of your choice!", Toast.LENGTH_LONG).show();
}
}
default:
return super.onOptionsItemSelected(item);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this,"Permission denied. You cannot add plugins.",
Toast.LENGTH_LONG).show();
}
break;
}
}

private boolean checkWriteStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
return false;
}
} else { //permission is automatically granted on sdk<23 upon installation
return true;
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
Expand Down

0 comments on commit cdb7e89

Please sign in to comment.