More cleanups

This commit is contained in:
topjohnwu 2019-04-10 13:54:33 -04:00
parent 96127f8bd1
commit 0165602515
4 changed files with 20 additions and 23 deletions

View File

@ -10,7 +10,6 @@ import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -29,8 +28,6 @@ import com.topjohnwu.magisk.utils.LocaleManager;
public abstract class BaseActivity extends AppCompatActivity implements Event.AutoListener {
private static Runnable grantCallback;
static int[] EMPTY_INT_ARRAY = new int[0];
private SparseArrayCompat<ActivityResultListener> resultListeners = new SparseArrayCompat<>();
@ -91,14 +88,14 @@ public abstract class BaseActivity extends AppCompatActivity implements Event.Au
}
public void runWithExternalRW(Runnable callback) {
runWithPermission(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, callback);
runWithPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, callback);
}
public void runWithPermission(String[] permissions, Runnable callback) {
runWithPermission(this, permissions, callback);
public void runWithPermissions(String[] permissions, Runnable callback) {
runWithPermissions(this, permissions, callback);
}
public static void runWithPermission(Context context, String[] permissions, Runnable callback) {
public static void runWithPermissions(Context context, String[] permissions, Runnable callback) {
boolean granted = true;
for (String perm : permissions) {
if (ContextCompat.checkSelfPermission(context, perm) != PackageManager.PERMISSION_GRANTED)
@ -110,14 +107,20 @@ public abstract class BaseActivity extends AppCompatActivity implements Event.Au
} else {
// Passed in context should be an activity if not granted, need to show dialog!
if (context instanceof BaseActivity) {
grantCallback = callback;
ActivityCompat.requestPermissions((BaseActivity) context, permissions, 0);
BaseActivity activity = (BaseActivity) context;
int code = callback.hashCode() & 0xFFFF;
activity.resultListeners.put(code, ((i, d) -> callback.run()));
ActivityCompat.requestPermissions(activity, permissions, code);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
onActivityResultListener(requestCode, resultCode, data);
}
private void onActivityResultListener(int requestCode, int resultCode, Intent data) {
ActivityResultListener listener = resultListeners.get(requestCode);
if (listener != null) {
resultListeners.remove(requestCode);
@ -137,14 +140,10 @@ public abstract class BaseActivity extends AppCompatActivity implements Event.Au
if (result != PackageManager.PERMISSION_GRANTED)
grant = false;
}
if (grant) {
if (grantCallback != null) {
grantCallback.run();
}
} else {
Toast.makeText(this, R.string.no_rw_storage, Toast.LENGTH_LONG).show();
}
grantCallback = null;
if (grant)
onActivityResultListener(requestCode, 0, null);
else
resultListeners.remove(requestCode);
}
public interface ActivityResultListener {

View File

@ -44,8 +44,8 @@ public abstract class BaseFragment extends Fragment implements Event.AutoListene
((BaseActivity) requireActivity()).startActivityForResult(intent, requestCode, listener);
}
public void runWithPermission(String[] permissions, Runnable callback) {
((BaseActivity) requireActivity()).runWithPermission(permissions,callback);
protected void runWithExternalRW(Runnable callback) {
((BaseActivity) requireActivity()).runWithExternalRW(callback);
}
@Override

View File

@ -1,6 +1,5 @@
package com.topjohnwu.magisk.fragments;
import android.Manifest;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
@ -67,7 +66,7 @@ public class MagiskLogFragment extends BaseFragment {
readLogs();
return true;
case R.id.menu_save:
runWithPermission(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, this::saveLogs);
runWithExternalRW(this::saveLogs);
return true;
case R.id.menu_clear:
clearLogs();

View File

@ -1,6 +1,5 @@
package com.topjohnwu.magisk.fragments;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
@ -44,7 +43,7 @@ public class ModulesFragment extends BaseFragment {
@OnClick(R.id.fab)
void selectFile() {
runWithPermission(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> {
runWithExternalRW(() -> {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/zip");
startActivityForResult(intent, Const.ID.FETCH_ZIP);