Organize dialog code

This commit is contained in:
topjohnwu 2018-07-28 22:52:40 +08:00
parent 27851bdefa
commit a747fdd27d
15 changed files with 403 additions and 334 deletions

View File

@ -3,6 +3,7 @@ package com.topjohnwu.magisk;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
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.annotation.StringRes; import android.support.annotation.StringRes;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
@ -20,12 +21,16 @@ import android.widget.TextView;
import com.topjohnwu.magisk.asyncs.CheckSafetyNet; import com.topjohnwu.magisk.asyncs.CheckSafetyNet;
import com.topjohnwu.magisk.asyncs.CheckUpdates; import com.topjohnwu.magisk.asyncs.CheckUpdates;
import com.topjohnwu.magisk.components.AlertDialogBuilder; import com.topjohnwu.magisk.components.Activity;
import com.topjohnwu.magisk.components.CustomAlertDialog;
import com.topjohnwu.magisk.components.EnvFixDialog;
import com.topjohnwu.magisk.components.ExpandableView; import com.topjohnwu.magisk.components.ExpandableView;
import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.components.Fragment;
import com.topjohnwu.magisk.components.MagiskInstallDialog;
import com.topjohnwu.magisk.components.ManagerInstallDialog;
import com.topjohnwu.magisk.components.UninstallDialog;
import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.ISafetyNetHelper; import com.topjohnwu.magisk.utils.ISafetyNetHelper;
import com.topjohnwu.magisk.utils.ShowUI;
import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Topic;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.Shell;
@ -84,11 +89,11 @@ public class MagiskFragment extends Fragment
safetyNetProgress.setVisibility(View.VISIBLE); safetyNetProgress.setVisibility(View.VISIBLE);
safetyNetRefreshIcon.setVisibility(View.GONE); safetyNetRefreshIcon.setVisibility(View.GONE);
safetyNetStatusText.setText(R.string.checking_safetyNet_status); safetyNetStatusText.setText(R.string.checking_safetyNet_status);
new CheckSafetyNet(getActivity()).exec(); new CheckSafetyNet(requireActivity()).exec();
collapse(); collapse();
}; };
if (!TextUtils.equals(mm.getPackageName(), Const.ORIG_PKG_NAME)) { if (!TextUtils.equals(mm.getPackageName(), Const.ORIG_PKG_NAME)) {
new AlertDialogBuilder(getActivity()) new CustomAlertDialog(requireActivity())
.setTitle(R.string.cannot_check_sn_title) .setTitle(R.string.cannot_check_sn_title)
.setMessage(R.string.cannot_check_sn_notice) .setMessage(R.string.cannot_check_sn_notice)
.setCancelable(true) .setCancelable(true)
@ -96,7 +101,7 @@ public class MagiskFragment extends Fragment
.show(); .show();
} else if (!CheckSafetyNet.dexPath.exists()) { } else if (!CheckSafetyNet.dexPath.exists()) {
// Show dialog // Show dialog
new AlertDialogBuilder(getActivity()) new CustomAlertDialog(requireActivity())
.setTitle(R.string.proprietary_title) .setTitle(R.string.proprietary_title)
.setMessage(R.string.proprietary_notice) .setMessage(R.string.proprietary_notice)
.setCancelable(true) .setCancelable(true)
@ -115,25 +120,26 @@ public class MagiskFragment extends Fragment
// Show Manager update first // Show Manager update first
if (mm.remoteManagerVersionCode > BuildConfig.VERSION_CODE) { if (mm.remoteManagerVersionCode > BuildConfig.VERSION_CODE) {
ShowUI.managerInstallDialog(getActivity()); new ManagerInstallDialog((Activity) requireActivity()).show();
return; return;
} }
((NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE)).cancelAll(); ((NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE)).cancelAll();
ShowUI.magiskInstallDialog(getActivity()); new MagiskInstallDialog((Activity) getActivity()).show();
} }
@OnClick(R.id.uninstall_button) @OnClick(R.id.uninstall_button)
void uninstall() { void uninstall() {
ShowUI.uninstallDialog(getActivity()); new UninstallDialog(requireActivity()).show();
} }
@Nullable @Nullable
@Override @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_magisk, container, false); View v = inflater.inflate(R.layout.fragment_magisk, container, false);
unbinder = ButterKnife.bind(this, v); unbinder = ButterKnife.bind(this, v);
getActivity().setTitle(R.string.magisk); requireActivity().setTitle(R.string.magisk);
mm = getApplication(); mm = getApplication();
@ -204,7 +210,7 @@ public class MagiskFragment extends Fragment
} }
private void updateUI() { private void updateUI() {
((MainActivity) getActivity()).checkHideSection(); ((MainActivity) requireActivity()).checkHideSection();
boolean hasNetwork = Utils.checkNetworkStatus(); boolean hasNetwork = Utils.checkNetworkStatus();
boolean hasRoot = Shell.rootAccess(); boolean hasRoot = Shell.rootAccess();
@ -266,7 +272,7 @@ public class MagiskFragment extends Fragment
install(); install();
} else if (mm.remoteMagiskVersionCode >= Const.MAGISK_VER.FIX_ENV && } else if (mm.remoteMagiskVersionCode >= Const.MAGISK_VER.FIX_ENV &&
!ShellUtils.fastCmdResult("env_check")) { !ShellUtils.fastCmdResult("env_check")) {
ShowUI.envFixDialog(getActivity()); new EnvFixDialog(requireActivity()).show();
} }
} }
} }

View File

@ -12,7 +12,7 @@ import android.widget.Switch;
import android.widget.TextView; import android.widget.TextView;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.components.AlertDialogBuilder; import com.topjohnwu.magisk.components.CustomAlertDialog;
import com.topjohnwu.magisk.components.ExpandableView; import com.topjohnwu.magisk.components.ExpandableView;
import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.magisk.container.Policy; import com.topjohnwu.magisk.container.Policy;
@ -93,7 +93,7 @@ public class PolicyAdapter extends RecyclerView.Adapter<PolicyAdapter.ViewHolder
dbHelper.updatePolicy(policy); dbHelper.updatePolicy(policy);
} }
}); });
holder.delete.setOnClickListener(v -> new AlertDialogBuilder((Activity) v.getContext()) holder.delete.setOnClickListener(v -> new CustomAlertDialog((Activity) v.getContext())
.setTitle(R.string.su_revoke_title) .setTitle(R.string.su_revoke_title)
.setMessage(v.getContext().getString(R.string.su_revoke_msg, policy.appName)) .setMessage(v.getContext().getString(R.string.su_revoke_msg, policy.appName))
.setPositiveButton(R.string.yes, (dialog, which) -> { .setPositiveButton(R.string.yes, (dialog, which) -> {

View File

@ -16,7 +16,7 @@ import android.widget.TextView;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.asyncs.MarkDownWindow; import com.topjohnwu.magisk.asyncs.MarkDownWindow;
import com.topjohnwu.magisk.asyncs.ProcessRepoZip; import com.topjohnwu.magisk.asyncs.ProcessRepoZip;
import com.topjohnwu.magisk.components.AlertDialogBuilder; import com.topjohnwu.magisk.components.CustomAlertDialog;
import com.topjohnwu.magisk.container.Module; import com.topjohnwu.magisk.container.Module;
import com.topjohnwu.magisk.container.Repo; import com.topjohnwu.magisk.container.Repo;
import com.topjohnwu.magisk.database.RepoDatabaseHelper; import com.topjohnwu.magisk.database.RepoDatabaseHelper;
@ -102,7 +102,7 @@ public class ReposAdapter extends SectionedAdapter<ReposAdapter.SectionHolder, R
holder.downloadImage.setOnClickListener(v -> { holder.downloadImage.setOnClickListener(v -> {
String filename = repo.getName() + "-" + repo.getVersion() + ".zip"; String filename = repo.getName() + "-" + repo.getVersion() + ".zip";
new AlertDialogBuilder((Activity) context) new CustomAlertDialog((Activity) context)
.setTitle(context.getString(R.string.repo_install_title, repo.getName())) .setTitle(context.getString(R.string.repo_install_title, repo.getName()))
.setMessage(context.getString(R.string.repo_install_msg, filename)) .setMessage(context.getString(R.string.repo_install_msg, filename))
.setCancelable(true) .setCancelable(true)

View File

@ -3,7 +3,7 @@ package com.topjohnwu.magisk.asyncs;
import com.topjohnwu.magisk.BuildConfig; import com.topjohnwu.magisk.BuildConfig;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.ShowUI; import com.topjohnwu.magisk.utils.NotificationMgr;
import com.topjohnwu.magisk.utils.WebService; import com.topjohnwu.magisk.utils.WebService;
import org.json.JSONException; import org.json.JSONException;
@ -59,9 +59,9 @@ public class CheckUpdates extends ParallelTask<Void, Void, Void> {
MagiskManager mm = MagiskManager.get(); MagiskManager mm = MagiskManager.get();
if (showNotification) { if (showNotification) {
if (BuildConfig.VERSION_CODE < mm.remoteManagerVersionCode) { if (BuildConfig.VERSION_CODE < mm.remoteManagerVersionCode) {
ShowUI.managerUpdateNotification(); NotificationMgr.managerUpdate();
} else if (mm.magiskVersionCode < mm.remoteMagiskVersionCode) { } else if (mm.magiskVersionCode < mm.remoteMagiskVersionCode) {
ShowUI.magiskUpdateNotification(); NotificationMgr.magiskUpdate();
} }
} }
mm.updateCheckDone.publish(); mm.updateCheckDone.publish();

View File

@ -18,7 +18,7 @@ import com.topjohnwu.magisk.R;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
public class AlertDialogBuilder extends AlertDialog.Builder { public class CustomAlertDialog extends AlertDialog.Builder {
@BindView(R.id.button_panel) LinearLayout buttons; @BindView(R.id.button_panel) LinearLayout buttons;
@BindView(R.id.message_panel) LinearLayout messagePanel; @BindView(R.id.message_panel) LinearLayout messagePanel;
@ -34,17 +34,7 @@ public class AlertDialogBuilder extends AlertDialog.Builder {
private AlertDialog dialog; private AlertDialog dialog;
public AlertDialogBuilder(@NonNull Activity context) { {
super(context);
setup();
}
public AlertDialogBuilder(@NonNull Activity context, @StyleRes int themeResId) {
super(context, themeResId);
setup();
}
private void setup() {
View v = LayoutInflater.from(getContext()).inflate(R.layout.alert_dialog, null); View v = LayoutInflater.from(getContext()).inflate(R.layout.alert_dialog, null);
ButterKnife.bind(this, v); ButterKnife.bind(this, v);
super.setView(v); super.setView(v);
@ -55,9 +45,12 @@ public class AlertDialogBuilder extends AlertDialog.Builder {
messagePanel.setVisibility(View.GONE); messagePanel.setVisibility(View.GONE);
} }
@Override public CustomAlertDialog(@NonNull Activity context) {
public AlertDialog.Builder setTitle(int titleId) { super(context);
return super.setTitle(titleId); }
public CustomAlertDialog(@NonNull Activity context, @StyleRes int themeResId) {
super(context, themeResId);
} }
@Override @Override

View File

@ -0,0 +1,33 @@
package com.topjohnwu.magisk.components;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.asyncs.InstallMagisk;
import com.topjohnwu.magisk.receivers.DownloadReceiver;
import com.topjohnwu.magisk.utils.Utils;
public class EnvFixDialog extends CustomAlertDialog {
public EnvFixDialog(@NonNull Activity activity) {
super(activity);
MagiskManager mm = Utils.getMagiskManager(activity);
String filename = Utils.fmt("Magisk-v%s(%d).zip",
mm.remoteMagiskVersionString, mm.remoteMagiskVersionCode);
setTitle(R.string.env_fix_title);
setMessage(R.string.env_fix_msg);
setCancelable(true);
setPositiveButton(R.string.yes, (d, i) -> Utils.dlAndReceive(activity,
new DownloadReceiver() {
@Override
public void onDownloadDone(Context context, Uri uri) {
new InstallMagisk(activity, uri).exec();
}
}, mm.magiskLink, filename));
setNegativeButton(R.string.no_thanks, null);
}
}

View File

@ -1,5 +1,6 @@
package com.topjohnwu.magisk.components; package com.topjohnwu.magisk.components;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.annotation.StyleRes; import android.support.annotation.StyleRes;
@ -12,6 +13,8 @@ import com.topjohnwu.magisk.utils.Topic;
public abstract class FlavorActivity extends AppCompatActivity { public abstract class FlavorActivity extends AppCompatActivity {
private ActivityResultListener activityResultListener;
@StyleRes @StyleRes
public int getDarkTheme() { public int getDarkTheme() {
return -1; return -1;
@ -27,7 +30,6 @@ public abstract class FlavorActivity extends AppCompatActivity {
if (this instanceof Topic.Subscriber) { if (this instanceof Topic.Subscriber) {
((Topic.Subscriber) this).subscribeTopics(); ((Topic.Subscriber) this).subscribeTopics();
} }
if (getMagiskManager().isDarkTheme && getDarkTheme() != -1) { if (getMagiskManager().isDarkTheme && getDarkTheme() != -1) {
setTheme(getDarkTheme()); setTheme(getDarkTheme());
} }
@ -54,4 +56,20 @@ public abstract class FlavorActivity extends AppCompatActivity {
setFinishOnTouchOutside(true); setFinishOnTouchOutside(true);
} }
} }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (activityResultListener != null)
activityResultListener.onActivityResult(requestCode, resultCode, data);
activityResultListener = null;
}
public void startActivityForResult(Intent intent, int requestCode, ActivityResultListener listener) {
activityResultListener = listener;
super.startActivityForResult(intent, requestCode);
}
public interface ActivityResultListener {
void onActivityResult(int requestCode, int resultCode, Intent data);
}
} }

View File

@ -0,0 +1,101 @@
package com.topjohnwu.magisk.components;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;
import com.topjohnwu.magisk.FlashActivity;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.receivers.DownloadReceiver;
import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.Utils;
import java.util.List;
class InstallMethodDialog extends AlertDialog.Builder {
InstallMethodDialog(Activity activity, List<String> options, String filename) {
super(activity);
MagiskManager mm = Utils.getMagiskManager(activity);
setTitle(R.string.select_method);
setItems(options.toArray(new String [0]), (dialog, idx) -> {
DownloadReceiver receiver = null;
switch (idx) {
case 1:
if (mm.remoteMagiskVersionCode < 1400) {
SnackbarMaker.make(activity, R.string.no_boot_file_patch_support,
Snackbar.LENGTH_LONG).show();
return;
}
MagiskManager.toast(R.string.boot_file_patch_msg, Toast.LENGTH_LONG);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
activity.startActivityForResult(intent, Const.ID.SELECT_BOOT,
(requestCode, resultCode, data) -> {
if (requestCode == Const.ID.SELECT_BOOT &&
resultCode == Activity.RESULT_OK && data != null) {
Utils.dlAndReceive(activity, new SelectBoot(data),
mm.magiskLink, filename);
}
});
return;
case 0:
receiver = new DownloadReceiver() {
@Override
public void onDownloadDone(Context context, Uri uri) {
SnackbarMaker.showUri(activity, uri);
}
};
break;
case 2:
receiver = new DownloadReceiver() {
@Override
public void onDownloadDone(Context context, Uri uri) {
Intent intent = new Intent(context, FlashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setData(uri).putExtra(Const.Key.FLASH_ACTION,
Const.Value.FLASH_MAGISK);
context.startActivity(intent);
}
};
break;
case 3:
receiver = new DownloadReceiver() {
@Override
public void onDownloadDone(Context context, Uri uri) {
Intent intent = new Intent(context, FlashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setData(uri).putExtra(Const.Key.FLASH_ACTION,
Const.Value.FLASH_SECOND_SLOT);
context.startActivity(intent);
}
};
default:
}
Utils.dlAndReceive(activity, receiver, mm.magiskLink, filename);
});
}
private class SelectBoot extends DownloadReceiver {
private Intent data;
public SelectBoot(Intent data) {
this.data = data;
}
@Override
public void onDownloadDone(Context context, Uri uri) {
Intent intent = new Intent(context, FlashActivity.class);
intent.setData(uri)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(Const.Key.FLASH_SET_BOOT, data.getData())
.putExtra(Const.Key.FLASH_ACTION, Const.Value.PATCH_BOOT);
context.startActivity(intent);
}
}
}

View File

@ -0,0 +1,53 @@
package com.topjohnwu.magisk.components;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.asyncs.MarkDownWindow;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils;
import java.util.ArrayList;
import java.util.List;
public class MagiskInstallDialog extends CustomAlertDialog {
public MagiskInstallDialog(Activity activity) {
super(activity);
MagiskManager mm = Utils.getMagiskManager(activity);
String filename = Utils.fmt("Magisk-v%s(%d).zip",
mm.remoteMagiskVersionString, mm.remoteMagiskVersionCode);
setTitle(mm.getString(R.string.repo_install_title, mm.getString(R.string.magisk)));
setMessage(mm.getString(R.string.repo_install_msg, filename));
setCancelable(true);
setPositiveButton(R.string.install, (d, i) -> {
List<String> options = new ArrayList<>();
options.add(mm.getString(R.string.download_zip_only));
options.add(mm.getString(R.string.patch_boot_file));
if (Shell.rootAccess()) {
options.add(mm.getString(R.string.direct_install));
}
String s = ShellUtils.fastCmd("grep_prop ro.build.ab_update");
if (!s.isEmpty() && Boolean.parseBoolean(s)) {
options.add(mm.getString(R.string.install_second_slot));
}
new InstallMethodDialog(activity, options, filename).show();
});
setNegativeButton(R.string.no_thanks, null);
if (!TextUtils.isEmpty(mm.magiskNoteLink)) {
setNeutralButton(R.string.release_notes, (d, i) -> {
if (mm.magiskNoteLink.contains("forum.xda-developers")) {
// Open forum links in browser
Intent openLink = new Intent(Intent.ACTION_VIEW, Uri.parse(mm.magiskNoteLink));
openLink.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mm.startActivity(openLink);
} else {
new MarkDownWindow(activity, null, mm.magiskNoteLink).exec();
}
});
}
}
}

View File

@ -0,0 +1,38 @@
package com.topjohnwu.magisk.components;
import android.Manifest;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.asyncs.MarkDownWindow;
import com.topjohnwu.magisk.receivers.ManagerUpdate;
import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.Utils;
public class ManagerInstallDialog extends CustomAlertDialog {
public ManagerInstallDialog(@NonNull Activity activity) {
super(activity);
MagiskManager mm = Utils.getMagiskManager(activity);
String filename = Utils.fmt("MagiskManager-v%s(%d).apk",
mm.remoteManagerVersionString, mm.remoteManagerVersionCode);
setTitle(mm.getString(R.string.repo_install_title, mm.getString(R.string.app_name)));
setMessage(mm.getString(R.string.repo_install_msg, filename));
setCancelable(true);
setPositiveButton(R.string.install, (d, i) -> activity.runWithPermission(
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> {
Intent intent = new Intent(mm, ManagerUpdate.class);
intent.putExtra(Const.Key.INTENT_SET_LINK, mm.managerLink);
intent.putExtra(Const.Key.INTENT_SET_FILENAME, filename);
mm.sendBroadcast(intent);
}))
.setNegativeButton(R.string.no_thanks, null);
if (!TextUtils.isEmpty(mm.managerNoteLink)) {
setNeutralButton(R.string.app_changelog, (d, i) ->
new MarkDownWindow(activity, null, mm.managerNoteLink).exec());
}
}
}

View File

@ -0,0 +1,40 @@
package com.topjohnwu.magisk.components;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.topjohnwu.magisk.FlashActivity;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.asyncs.RestoreImages;
import com.topjohnwu.magisk.receivers.DownloadReceiver;
import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.Utils;
public class UninstallDialog extends CustomAlertDialog {
public UninstallDialog(@NonNull Activity activity) {
super(activity);
MagiskManager mm = Utils.getMagiskManager(activity);
setTitle(R.string.uninstall_magisk_title);
setMessage(R.string.uninstall_magisk_msg);
setNeutralButton(R.string.restore_img, (d, i) -> new RestoreImages(activity).exec());
if (!TextUtils.isEmpty(mm.uninstallerLink)) {
setPositiveButton(R.string.complete_uninstall, (d, i) ->
Utils.dlAndReceive(activity, new DownloadReceiver() {
@Override
public void onDownloadDone(Context context, Uri uri) {
Intent intent = new Intent(context, FlashActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setData(uri)
.putExtra(Const.Key.FLASH_ACTION, Const.Value.UNINSTALL);
context.startActivity(intent);
}
}, mm.uninstallerLink, "magisk-uninstaller.zip"));
}
}
}

View File

@ -6,7 +6,7 @@ import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService; import android.support.v4.app.JobIntentService;
import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.ShowUI; import com.topjohnwu.magisk.utils.NotificationMgr;
import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils; import com.topjohnwu.superuser.ShellUtils;
@ -28,6 +28,6 @@ public class OnBootService extends JobIntentService {
Shell shell = Shell.newInstance(); Shell shell = Shell.newInstance();
if (shell.getStatus() >= Shell.ROOT_SHELL && if (shell.getStatus() >= Shell.ROOT_SHELL &&
Boolean.parseBoolean(ShellUtils.fastCmd(shell, "mm_patch_dtbo"))) Boolean.parseBoolean(ShellUtils.fastCmd(shell, "mm_patch_dtbo")))
ShowUI.dtboPatchedNotification(); NotificationMgr.dtboPatched();
} }
} }

View File

@ -0,0 +1,84 @@
package com.topjohnwu.magisk.utils;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.SplashActivity;
import com.topjohnwu.magisk.receivers.ManagerUpdate;
import com.topjohnwu.magisk.receivers.RebootReceiver;
public class NotificationMgr {
public static void magiskUpdate() {
MagiskManager mm = MagiskManager.get();
Intent intent = new Intent(mm, SplashActivity.class);
intent.putExtra(Const.Key.OPEN_SECTION, "magisk");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mm);
stackBuilder.addParentStack(SplashActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(Const.ID.MAGISK_UPDATE_NOTIFICATION_ID,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mm, Const.ID.NOTIFICATION_CHANNEL);
builder.setSmallIcon(R.drawable.ic_magisk_outline)
.setContentTitle(mm.getString(R.string.magisk_update_title))
.setContentText(mm.getString(R.string.magisk_update_available, mm.remoteMagiskVersionString))
.setVibrate(new long[]{0, 100, 100, 100})
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Const.ID.MAGISK_UPDATE_NOTIFICATION_ID, builder.build());
}
public static void managerUpdate() {
MagiskManager mm = MagiskManager.get();
String filename = Utils.fmt("MagiskManager-v%s(%d).apk",
mm.remoteManagerVersionString, mm.remoteManagerVersionCode);
Intent intent = new Intent(mm, ManagerUpdate.class);
intent.putExtra(Const.Key.INTENT_SET_LINK, mm.managerLink);
intent.putExtra(Const.Key.INTENT_SET_FILENAME, filename);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mm,
Const.ID.APK_UPDATE_NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mm, Const.ID.NOTIFICATION_CHANNEL);
builder.setSmallIcon(R.drawable.ic_magisk_outline)
.setContentTitle(mm.getString(R.string.manager_update_title))
.setContentText(mm.getString(R.string.manager_download_install))
.setVibrate(new long[]{0, 100, 100, 100})
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Const.ID.APK_UPDATE_NOTIFICATION_ID, builder.build());
}
public static void dtboPatched() {
MagiskManager mm = MagiskManager.get();
Intent intent = new Intent(mm, RebootReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mm,
Const.ID.DTBO_NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mm, Const.ID.NOTIFICATION_CHANNEL);
builder.setSmallIcon(R.drawable.ic_magisk_outline)
.setContentTitle(mm.getString(R.string.dtbo_patched_title))
.setContentText(mm.getString(R.string.dtbo_patched_reboot))
.setVibrate(new long[]{0, 100, 100, 100})
.addAction(R.drawable.ic_refresh, mm.getString(R.string.reboot), pendingIntent);
NotificationManager notificationManager =
(NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Const.ID.DTBO_NOTIFICATION_ID, builder.build());
}
}

View File

@ -1,278 +0,0 @@
package com.topjohnwu.magisk.utils;
import android.Manifest;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.widget.Toast;
import com.topjohnwu.magisk.FlashActivity;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.SplashActivity;
import com.topjohnwu.magisk.asyncs.InstallMagisk;
import com.topjohnwu.magisk.asyncs.MarkDownWindow;
import com.topjohnwu.magisk.asyncs.RestoreImages;
import com.topjohnwu.magisk.components.AlertDialogBuilder;
import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.magisk.receivers.DownloadReceiver;
import com.topjohnwu.magisk.receivers.ManagerUpdate;
import com.topjohnwu.magisk.receivers.RebootReceiver;
import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils;
import java.util.ArrayList;
import java.util.List;
public class ShowUI {
public static void magiskUpdateNotification() {
MagiskManager mm = MagiskManager.get();
Intent intent = new Intent(mm, SplashActivity.class);
intent.putExtra(Const.Key.OPEN_SECTION, "magisk");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mm);
stackBuilder.addParentStack(SplashActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(Const.ID.MAGISK_UPDATE_NOTIFICATION_ID,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mm, Const.ID.NOTIFICATION_CHANNEL);
builder.setSmallIcon(R.drawable.ic_magisk_outline)
.setContentTitle(mm.getString(R.string.magisk_update_title))
.setContentText(mm.getString(R.string.magisk_update_available, mm.remoteMagiskVersionString))
.setVibrate(new long[]{0, 100, 100, 100})
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Const.ID.MAGISK_UPDATE_NOTIFICATION_ID, builder.build());
}
public static void managerUpdateNotification() {
MagiskManager mm = MagiskManager.get();
String filename = Utils.fmt("MagiskManager-v%s(%d).apk",
mm.remoteManagerVersionString, mm.remoteManagerVersionCode);
Intent intent = new Intent(mm, ManagerUpdate.class);
intent.putExtra(Const.Key.INTENT_SET_LINK, mm.managerLink);
intent.putExtra(Const.Key.INTENT_SET_FILENAME, filename);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mm,
Const.ID.APK_UPDATE_NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mm, Const.ID.NOTIFICATION_CHANNEL);
builder.setSmallIcon(R.drawable.ic_magisk_outline)
.setContentTitle(mm.getString(R.string.manager_update_title))
.setContentText(mm.getString(R.string.manager_download_install))
.setVibrate(new long[]{0, 100, 100, 100})
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Const.ID.APK_UPDATE_NOTIFICATION_ID, builder.build());
}
public static void dtboPatchedNotification() {
MagiskManager mm = MagiskManager.get();
Intent intent = new Intent(mm, RebootReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mm,
Const.ID.DTBO_NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mm, Const.ID.NOTIFICATION_CHANNEL);
builder.setSmallIcon(R.drawable.ic_magisk_outline)
.setContentTitle(mm.getString(R.string.dtbo_patched_title))
.setContentText(mm.getString(R.string.dtbo_patched_reboot))
.setVibrate(new long[]{0, 100, 100, 100})
.addAction(R.drawable.ic_refresh, mm.getString(R.string.reboot), pendingIntent);
NotificationManager notificationManager =
(NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Const.ID.DTBO_NOTIFICATION_ID, builder.build());
}
public static void envFixDialog(Activity activity) {
MagiskManager mm = Utils.getMagiskManager(activity);
String filename = Utils.fmt("Magisk-v%s(%d).zip",
mm.remoteMagiskVersionString, mm.remoteMagiskVersionCode);
new AlertDialogBuilder(activity)
.setTitle(R.string.env_fix_title)
.setMessage(R.string.env_fix_msg)
.setCancelable(true)
.setPositiveButton(R.string.yes, (d, i) -> {
Utils.dlAndReceive(activity, new DownloadReceiver() {
@Override
public void onDownloadDone(Context context, Uri uri) {
new InstallMagisk(activity, uri).exec();
}
}, mm.magiskLink, filename);
})
.setNegativeButton(R.string.no_thanks, null)
.show();
}
public static void magiskInstallDialog(Activity activity) {
MagiskManager mm = Utils.getMagiskManager(activity);
String filename = Utils.fmt("Magisk-v%s(%d).zip",
mm.remoteMagiskVersionString, mm.remoteMagiskVersionCode);
AlertDialog.Builder b = new AlertDialogBuilder(activity)
.setTitle(mm.getString(R.string.repo_install_title, mm.getString(R.string.magisk)))
.setMessage(mm.getString(R.string.repo_install_msg, filename))
.setCancelable(true)
.setPositiveButton(R.string.install, (d, i) -> {
List<String> options = new ArrayList<>();
options.add(mm.getString(R.string.download_zip_only));
options.add(mm.getString(R.string.patch_boot_file));
if (Shell.rootAccess()) {
options.add(mm.getString(R.string.direct_install));
}
String s = ShellUtils.fastCmd("grep_prop ro.build.ab_update");
if (s != null && Boolean.parseBoolean(s)) {
options.add(mm.getString(R.string.install_second_slot));
}
new AlertDialog.Builder(activity)
.setTitle(R.string.select_method)
.setItems(
options.toArray(new String [0]),
(dialog, idx) -> {
DownloadReceiver receiver = null;
switch (idx) {
case 1:
if (mm.remoteMagiskVersionCode < 1400) {
MagiskManager.toast(R.string.no_boot_file_patch_support, Toast.LENGTH_LONG);
return;
}
MagiskManager.toast(R.string.boot_file_patch_msg, Toast.LENGTH_LONG);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
((com.topjohnwu.magisk.components.Activity) activity)
.startActivityForResult(intent, Const.ID.SELECT_BOOT,
(requestCode, resultCode, data) -> {
if (requestCode == Const.ID.SELECT_BOOT
&& resultCode == Activity.RESULT_OK && data != null) {
Utils.dlAndReceive(
activity,
new DownloadReceiver() {
@Override
public void onDownloadDone(Context context, Uri uri) {
Intent intent = new Intent(mm, FlashActivity.class);
intent.setData(uri)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(Const.Key.FLASH_SET_BOOT, data.getData())
.putExtra(Const.Key.FLASH_ACTION, Const.Value.PATCH_BOOT);
mm.startActivity(intent);
}
},
mm.magiskLink,
filename
);
}
});
return;
case 0:
receiver = new DownloadReceiver() {
@Override
public void onDownloadDone(Context context, Uri uri) {
SnackbarMaker.showUri(activity, uri);
}
};
break;
case 2:
receiver = new DownloadReceiver() {
@Override
public void onDownloadDone(Context context, Uri uri) {
Intent intent = new Intent(mm, FlashActivity.class);
intent.setData(uri).putExtra(Const.Key.FLASH_ACTION,
Const.Value.FLASH_MAGISK);
activity.startActivity(intent);
}
};
break;
case 3:
receiver = new DownloadReceiver() {
@Override
public void onDownloadDone(Context context, Uri uri) {
Intent intent = new Intent(mm, FlashActivity.class);
intent.setData(uri).putExtra(Const.Key.FLASH_ACTION,
Const.Value.FLASH_SECOND_SLOT);
activity.startActivity(intent);
}
};
default:
}
Utils.dlAndReceive(activity, receiver, mm.magiskLink, filename);
}
).show();
})
.setNegativeButton(R.string.no_thanks, null);
if (!TextUtils.isEmpty(mm.magiskNoteLink)) {
b.setNeutralButton(R.string.release_notes, (d, i) -> {
if (mm.magiskNoteLink.contains("forum.xda-developers")) {
// Open forum links in browser
Intent openLink = new Intent(Intent.ACTION_VIEW, Uri.parse(mm.magiskNoteLink));
openLink.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mm.startActivity(openLink);
} else {
new MarkDownWindow(activity, null, mm.magiskNoteLink).exec();
}
});
}
b.show();
}
public static void managerInstallDialog(Activity activity) {
MagiskManager mm = Utils.getMagiskManager(activity);
String filename = Utils.fmt("MagiskManager-v%s(%d).apk",
mm.remoteManagerVersionString, mm.remoteManagerVersionCode);
AlertDialog.Builder b = new AlertDialogBuilder(activity)
.setTitle(mm.getString(R.string.repo_install_title, mm.getString(R.string.app_name)))
.setMessage(mm.getString(R.string.repo_install_msg, filename))
.setCancelable(true)
.setPositiveButton(R.string.install, (d, i) -> {
com.topjohnwu.magisk.components.Activity.runWithPermission(activity,
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> {
Intent intent = new Intent(mm, ManagerUpdate.class);
intent.putExtra(Const.Key.INTENT_SET_LINK, mm.managerLink);
intent.putExtra(Const.Key.INTENT_SET_FILENAME, filename);
mm.sendBroadcast(intent);
});
})
.setNegativeButton(R.string.no_thanks, null);
if (!TextUtils.isEmpty(mm.managerNoteLink)) {
b.setNeutralButton(R.string.app_changelog, (d, i) ->
new MarkDownWindow(activity, null, mm.managerNoteLink).exec());
}
b.show();
}
public static void uninstallDialog(Activity activity) {
MagiskManager mm = Utils.getMagiskManager(activity);
AlertDialog.Builder b = new AlertDialogBuilder(activity)
.setTitle(R.string.uninstall_magisk_title)
.setMessage(R.string.uninstall_magisk_msg)
.setNeutralButton(R.string.restore_img, (d, i) -> new RestoreImages(activity).exec());
if (!TextUtils.isEmpty(mm.uninstallerLink)) {
b.setPositiveButton(R.string.complete_uninstall, (d, i) ->
Utils.dlAndReceive(activity, new DownloadReceiver() {
@Override
public void onDownloadDone(Context context, Uri uri) {
Intent intent = new Intent(context, FlashActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setData(uri)
.putExtra(Const.Key.FLASH_ACTION, Const.Value.UNINSTALL);
context.startActivity(intent);
}
}, mm.uninstallerLink, "magisk-uninstaller.zip"));
}
b.show();
}
}

View File

@ -17,8 +17,6 @@ public abstract class Activity extends FlavorActivity {
protected static Runnable permissionGrantCallback; protected static Runnable permissionGrantCallback;
private ActivityResultListener activityResultListener;
public Activity() { public Activity() {
super(); super();
Configuration configuration = new Configuration(); Configuration configuration = new Configuration();
@ -74,21 +72,4 @@ public abstract class Activity extends FlavorActivity {
} }
permissionGrantCallback = null; permissionGrantCallback = null;
} }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (activityResultListener != null)
activityResultListener.onActivityResult(requestCode, resultCode, data);
activityResultListener = null;
}
public void startActivityForResult(Intent intent, int requestCode, ActivityResultListener listener) {
activityResultListener = listener;
super.startActivityForResult(intent, requestCode);
}
public interface ActivityResultListener {
void onActivityResult(int requestCode, int resultCode, Intent data);
}
} }