mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-12-28 00:27:39 +00:00
Proper runtime permission implementation
This commit is contained in:
parent
d8154a5815
commit
439c7118f1
@ -1,11 +1,9 @@
|
|||||||
package com.topjohnwu.magisk;
|
package com.topjohnwu.magisk;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.os.Handler;
|
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.design.widget.Snackbar;
|
import android.support.design.widget.Snackbar;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
@ -25,6 +23,7 @@ import com.topjohnwu.magisk.asyncs.ParallelTask;
|
|||||||
import com.topjohnwu.magisk.components.Fragment;
|
import com.topjohnwu.magisk.components.Fragment;
|
||||||
import com.topjohnwu.magisk.components.SnackbarMaker;
|
import com.topjohnwu.magisk.components.SnackbarMaker;
|
||||||
import com.topjohnwu.magisk.utils.Shell;
|
import com.topjohnwu.magisk.utils.Shell;
|
||||||
|
import com.topjohnwu.magisk.utils.Utils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
@ -40,25 +39,18 @@ public class MagiskLogFragment extends Fragment {
|
|||||||
private static final String MAGISK_LOG = "/cache/magisk.log";
|
private static final String MAGISK_LOG = "/cache/magisk.log";
|
||||||
|
|
||||||
private Unbinder unbinder;
|
private Unbinder unbinder;
|
||||||
|
|
||||||
@BindView(R.id.txtLog) TextView txtLog;
|
@BindView(R.id.txtLog) TextView txtLog;
|
||||||
@BindView(R.id.svLog) ScrollView svLog;
|
@BindView(R.id.svLog) ScrollView svLog;
|
||||||
@BindView(R.id.hsvLog) HorizontalScrollView hsvLog;
|
@BindView(R.id.hsvLog) HorizontalScrollView hsvLog;
|
||||||
|
|
||||||
@BindView(R.id.progressBar) ProgressBar progressBar;
|
@BindView(R.id.progressBar) ProgressBar progressBar;
|
||||||
|
|
||||||
private MenuItem mClickedMenuItem;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
setHasOptionsMenu(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
View view = inflater.inflate(R.layout.fragment_magisk_log, container, false);
|
View view = inflater.inflate(R.layout.fragment_magisk_log, container, false);
|
||||||
unbinder = ButterKnife.bind(this, view);
|
unbinder = ButterKnife.bind(this, view);
|
||||||
|
setHasOptionsMenu(true);
|
||||||
|
|
||||||
txtLog.setTextIsSelectable(true);
|
txtLog.setTextIsSelectable(true);
|
||||||
|
|
||||||
@ -92,13 +84,14 @@ public class MagiskLogFragment extends Fragment {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
mClickedMenuItem = item;
|
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
case R.id.menu_refresh:
|
case R.id.menu_refresh:
|
||||||
new LogManager().read();
|
new LogManager().read();
|
||||||
return true;
|
return true;
|
||||||
case R.id.menu_save:
|
case R.id.menu_save:
|
||||||
new LogManager().save();
|
Utils.runWithPermission(getActivity(),
|
||||||
|
Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
||||||
|
() -> new LogManager().save());
|
||||||
return true;
|
return true;
|
||||||
case R.id.menu_clear:
|
case R.id.menu_clear:
|
||||||
new LogManager().clear();
|
new LogManager().clear();
|
||||||
@ -108,25 +101,15 @@ public class MagiskLogFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
||||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
||||||
if (requestCode == 0) {
|
|
||||||
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
||||||
if (mClickedMenuItem != null) {
|
|
||||||
new Handler().postDelayed(() -> onOptionsItemSelected(mClickedMenuItem), 500);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
SnackbarMaker.make(txtLog, R.string.permissionNotGranted, Snackbar.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class LogManager extends ParallelTask<Object, Void, Object> {
|
private class LogManager extends ParallelTask<Object, Void, Object> {
|
||||||
|
|
||||||
private int mode;
|
private int mode;
|
||||||
private File targetFile;
|
private File targetFile;
|
||||||
|
|
||||||
|
LogManager() {
|
||||||
|
super(MagiskLogFragment.this.getActivity());
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressLint("DefaultLocale")
|
@SuppressLint("DefaultLocale")
|
||||||
@Override
|
@Override
|
||||||
protected Object doInBackground(Object... params) {
|
protected Object doInBackground(Object... params) {
|
||||||
@ -138,7 +121,7 @@ public class MagiskLogFragment extends Fragment {
|
|||||||
return logList.toString();
|
return logList.toString();
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
getShell().su_raw("echo > " + MAGISK_LOG);
|
getShell().su_raw("echo -n > " + MAGISK_LOG);
|
||||||
SnackbarMaker.make(txtLog, R.string.logs_cleared, Snackbar.LENGTH_SHORT).show();
|
SnackbarMaker.make(txtLog, R.string.logs_cleared, Snackbar.LENGTH_SHORT).show();
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
@ -150,7 +133,7 @@ public class MagiskLogFragment extends Fragment {
|
|||||||
now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY),
|
now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY),
|
||||||
now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
|
now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
|
||||||
|
|
||||||
targetFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MagiskManager/" + filename);
|
targetFile = new File(Environment.getExternalStorageDirectory() + "/MagiskManager/" + filename);
|
||||||
|
|
||||||
if ((!targetFile.getParentFile().exists() && !targetFile.getParentFile().mkdirs())
|
if ((!targetFile.getParentFile().exists() && !targetFile.getParentFile().mkdirs())
|
||||||
|| (targetFile.exists() && !targetFile.delete())) {
|
|| (targetFile.exists() && !targetFile.delete())) {
|
||||||
@ -187,9 +170,9 @@ public class MagiskLogFragment extends Fragment {
|
|||||||
case 2:
|
case 2:
|
||||||
boolean bool = (boolean) o;
|
boolean bool = (boolean) o;
|
||||||
if (bool) {
|
if (bool) {
|
||||||
MagiskLogFragment.this.getApplication().toast(targetFile.toString(), Toast.LENGTH_LONG);
|
getMagiskManager().toast(targetFile.toString(), Toast.LENGTH_LONG);
|
||||||
} else {
|
} else {
|
||||||
MagiskLogFragment.this.getApplication().toast(R.string.logs_save_failed, Toast.LENGTH_LONG);
|
getMagiskManager().toast(R.string.logs_save_failed, Toast.LENGTH_LONG);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,11 @@
|
|||||||
package com.topjohnwu.magisk;
|
package com.topjohnwu.magisk;
|
||||||
|
|
||||||
import android.Manifest;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.os.Build;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.design.widget.NavigationView;
|
import android.support.design.widget.NavigationView;
|
||||||
import android.support.v4.app.ActivityCompat;
|
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.app.FragmentTransaction;
|
import android.support.v4.app.FragmentTransaction;
|
||||||
import android.support.v4.widget.DrawerLayout;
|
import android.support.v4.widget.DrawerLayout;
|
||||||
@ -52,11 +48,6 @@ public class MainActivity extends Activity
|
|||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
ButterKnife.bind(this);
|
ButterKnife.bind(this);
|
||||||
|
|
||||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|
|
||||||
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
||||||
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
setSupportActionBar(toolbar);
|
setSupportActionBar(toolbar);
|
||||||
|
|
||||||
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
|
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.topjohnwu.magisk;
|
package com.topjohnwu.magisk;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
@ -125,7 +126,9 @@ public class SettingsActivity extends Activity implements Topic.Subscriber {
|
|||||||
});
|
});
|
||||||
|
|
||||||
hideManager.setOnPreferenceClickListener((pref) -> {
|
hideManager.setOnPreferenceClickListener((pref) -> {
|
||||||
new HideManager(getActivity()).exec();
|
Utils.runWithPermission(getActivity(),
|
||||||
|
Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
||||||
|
() -> new HideManager(getActivity()).exec());
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package com.topjohnwu.magisk.components;
|
package com.topjohnwu.magisk.components;
|
||||||
|
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
@ -13,6 +15,8 @@ import com.topjohnwu.magisk.utils.Topic;
|
|||||||
|
|
||||||
public class Activity extends AppCompatActivity {
|
public class Activity extends AppCompatActivity {
|
||||||
|
|
||||||
|
private Runnable permissionGrantCallback;
|
||||||
|
|
||||||
public Activity() {
|
public Activity() {
|
||||||
super();
|
super();
|
||||||
Configuration configuration = new Configuration();
|
Configuration configuration = new Configuration();
|
||||||
@ -36,6 +40,20 @@ public class Activity extends AppCompatActivity {
|
|||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||||
|
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
if (permissionGrantCallback != null) {
|
||||||
|
permissionGrantCallback.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
permissionGrantCallback = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPermissionGrantCallback(Runnable callback) {
|
||||||
|
permissionGrantCallback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MagiskManager getApplicationContext() {
|
public MagiskManager getApplicationContext() {
|
||||||
return (MagiskManager) super.getApplicationContext();
|
return (MagiskManager) super.getApplicationContext();
|
||||||
|
@ -23,6 +23,7 @@ import android.support.v4.app.ActivityCompat;
|
|||||||
import android.support.v4.app.FragmentActivity;
|
import android.support.v4.app.FragmentActivity;
|
||||||
import android.support.v4.app.NotificationCompat;
|
import android.support.v4.app.NotificationCompat;
|
||||||
import android.support.v4.app.TaskStackBuilder;
|
import android.support.v4.app.TaskStackBuilder;
|
||||||
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
@ -80,32 +81,28 @@ public class Utils {
|
|||||||
if (isDownloading)
|
if (isDownloading)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
runWithPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE, () -> {
|
||||||
!= PackageManager.PERMISSION_GRANTED) {
|
File file = new File(Environment.getExternalStorageDirectory() + "/MagiskManager/" + filename);
|
||||||
Toast.makeText(context, R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
File file = new File(Environment.getExternalStorageDirectory() + "/MagiskManager/" + filename);
|
if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs())
|
||||||
|
|| (file.exists() && !file.delete())) {
|
||||||
|
Toast.makeText(context, R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs())
|
Toast.makeText(context, context.getString(R.string.downloading_toast, filename), Toast.LENGTH_LONG).show();
|
||||||
|| (file.exists() && !file.delete())) {
|
isDownloading = true;
|
||||||
Toast.makeText(context, R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Toast.makeText(context, context.getString(R.string.downloading_toast, filename), Toast.LENGTH_LONG).show();
|
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||||
isDownloading = true;
|
|
||||||
|
|
||||||
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
if (link != null) {
|
||||||
|
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link));
|
||||||
if (link != null) {
|
request.setDestinationUri(Uri.fromFile(file));
|
||||||
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link));
|
receiver.setDownloadID(downloadManager.enqueue(request));
|
||||||
request.setDestinationUri(Uri.fromFile(file));
|
}
|
||||||
receiver.setDownloadID(downloadManager.enqueue(request));
|
receiver.setFilename(filename);
|
||||||
}
|
context.getApplicationContext().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
||||||
receiver.setFilename(filename);
|
});
|
||||||
context.getApplicationContext().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getLegalFilename(CharSequence filename) {
|
public static String getLegalFilename(CharSequence filename) {
|
||||||
@ -314,4 +311,17 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void runWithPermission(Context context, String permission, Runnable callback) {
|
||||||
|
if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
// Passed in context should be an activity if not granted, need to show dialog!
|
||||||
|
if (!(context instanceof com.topjohnwu.magisk.components.Activity))
|
||||||
|
return;
|
||||||
|
com.topjohnwu.magisk.components.Activity activity = (com.topjohnwu.magisk.components.Activity) context;
|
||||||
|
activity.setPermissionGrantCallback(callback);
|
||||||
|
ActivityCompat.requestPermissions(activity, new String[] { permission }, 0);
|
||||||
|
} else {
|
||||||
|
callback.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user