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

View File

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

View File

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

View File

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