Magisk/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java

298 lines
13 KiB
Java
Raw Normal View History

2016-08-23 11:39:18 +02:00
package com.topjohnwu.magisk;
2016-09-21 16:55:20 -05:00
import android.app.Fragment;
2016-08-26 12:45:35 +02:00
import android.content.Intent;
import android.content.SharedPreferences;
2016-09-27 15:51:38 +08:00
import android.net.Uri;
2016-10-19 06:25:50 +08:00
import android.os.Build;
2016-08-23 11:39:18 +02:00
import android.os.Bundle;
2016-09-25 10:11:57 -05:00
import android.preference.PreferenceManager;
2016-08-23 11:39:18 +02:00
import android.support.annotation.Nullable;
2016-08-27 19:02:41 +08:00
import android.support.v4.content.FileProvider;
import android.support.v4.widget.SwipeRefreshLayout;
2016-08-26 12:45:35 +02:00
import android.support.v7.app.AlertDialog;
2016-08-23 11:39:18 +02:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
2016-08-25 18:08:07 +08:00
import android.widget.ProgressBar;
2016-08-23 11:39:18 +02:00
import android.widget.TextView;
2016-09-26 10:45:34 +08:00
import com.topjohnwu.magisk.receivers.DownloadReceiver;
2016-09-21 01:08:05 +08:00
import com.topjohnwu.magisk.utils.Async;
import com.topjohnwu.magisk.utils.Logger;
2016-10-01 05:21:24 +08:00
import com.topjohnwu.magisk.utils.Shell;
2016-08-28 03:52:03 +08:00
import com.topjohnwu.magisk.utils.Utils;
2016-11-29 13:24:48 +08:00
import com.topjohnwu.magisk.utils.ZipUtils;
2016-08-23 11:39:18 +02:00
import java.io.File;
2016-10-01 05:21:24 +08:00
import java.util.List;
2016-08-23 11:39:18 +02:00
import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MagiskFragment extends Fragment {
2016-09-29 01:42:25 +08:00
public static int remoteAppVersionCode = -1;
public static double magiskVersion, remoteMagiskVersion = -1;
public static String magiskVersionString, magiskLink, magiskChangelog, appLink, appChangelog, remoteAppVersion;
2016-09-29 01:42:25 +08:00
@BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
2016-08-26 12:45:35 +02:00
@BindView(R.id.magiskStatusView) View magiskStatusView;
2016-08-23 11:39:18 +02:00
@BindView(R.id.magisk_status_container) View magiskStatusContainer;
@BindView(R.id.magisk_status_icon) ImageView magiskStatusIcon;
2016-09-29 01:42:25 +08:00
@BindView(R.id.magisk_version) TextView magiskVersionText;
2016-08-23 11:39:18 +02:00
2016-08-26 12:45:35 +02:00
@BindView(R.id.app_updateView) View appUpdateView;
@BindView(R.id.app_check_updates_container) View appCheckUpdatesContainer;
@BindView(R.id.app_check_updates_icon) ImageView appCheckUpdatesIcon;
@BindView(R.id.app_check_updates_status) TextView appCheckUpdatesStatus;
@BindView(R.id.app_check_updates_progress) ProgressBar appCheckUpdatesProgress;
2016-08-23 11:39:18 +02:00
2016-08-26 12:45:35 +02:00
@BindView(R.id.magisk_updateView) View magiskUpdateView;
@BindView(R.id.magisk_check_updates_container) View magiskCheckUpdatesContainer;
@BindView(R.id.magisk_check_updates_icon) ImageView magiskCheckUpdatesIcon;
@BindView(R.id.magisk_check_updates_status) TextView magiskCheckUpdatesStatus;
@BindView(R.id.magisk_check_updates_progress) ProgressBar magiskCheckUpdatesProgress;
2016-08-23 11:39:18 +02:00
@BindColor(R.color.green500) int colorOK;
@BindColor(R.color.yellow500) int colorWarn;
@BindColor(R.color.grey500) int colorNeutral;
@BindColor(R.color.blue500) int colorInfo;
2016-09-29 01:42:25 +08:00
@BindColor(android.R.color.transparent) int trans;
2016-08-23 11:39:18 +02:00
int statusOK = R.drawable.ic_check_circle;
int statusUnknown = R.drawable.ic_help;
2016-09-25 10:11:57 -05:00
private AlertDialog.Builder builder;
2016-08-23 11:39:18 +02:00
private SharedPreferences prefs;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
private AlertDialog.OnClickListener flashMagisk = (dialogInterface, i) -> Utils.dlAndReceive(
2016-10-03 00:05:53 +08:00
getActivity(),
new DownloadReceiver() {
2016-10-03 00:05:53 +08:00
@Override
public void task(Uri uri) {
new Async.FlashZIP(mContext, uri, mFilename) {
2016-11-29 13:24:48 +08:00
@Override
protected boolean unzipAndCheck() {
publishProgress(mContext.getString(R.string.zip_install_unzip_zip_msg));
// We might not have busybox yet, unzip with Java
// We will have complete busybox after Magisk installation
ZipUtils.unzip(mCachedFile, new File(mCachedFile.getParent(), "magisk"));
Shell.su(
"mkdir -p " + Async.TMP_FOLDER_PATH + "/magisk",
"cp -af " + mCachedFile.getParent() + "/magisk/. " + Async.TMP_FOLDER_PATH + "/magisk"
);
return super.unzipAndCheck();
}
2016-10-03 00:05:53 +08:00
@Override
protected void done() {
Shell.su("setprop magisk.version " + String.valueOf(remoteMagiskVersion));
super.done();
}
2016-11-07 21:06:18 +08:00
}.exec();
2016-10-03 00:05:53 +08:00
}
},
magiskLink,
"Magisk-v" + String.valueOf(remoteMagiskVersion) + ".zip");
2016-10-03 00:05:53 +08:00
private AlertDialog.OnClickListener installMagiskApk = (dialogInterface, i) -> Utils.dlAndReceive(
2016-10-03 00:05:53 +08:00
getActivity(),
new DownloadReceiver() {
@Override
public void task(Uri uri) {
2016-10-19 06:25:50 +08:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri content = FileProvider.getUriForFile(getActivity(), "com.topjohnwu.magisk.provider", new File(uri.getPath()));
install.setData(content);
mContext.startActivity(install);
} else {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(uri, "application/vnd.android.package-archive");
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(install);
}
2016-10-03 00:05:53 +08:00
}
},
appLink,
"MagiskManager-v" + remoteAppVersion + ".apk");
2016-11-29 13:24:48 +08:00
static {
updateMagiskVersion();
}
2016-08-23 11:39:18 +02:00
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2016-08-26 12:45:35 +02:00
View v = inflater.inflate(R.layout.magisk_fragment, container, false);
ButterKnife.bind(this, v);
2016-09-26 11:46:13 +08:00
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
mSwipeRefreshLayout.setOnRefreshListener(() -> {
prefs.edit().putBoolean("update_check_done", false).apply();
2016-09-29 01:42:25 +08:00
appCheckUpdatesContainer.setBackgroundColor(trans);
magiskCheckUpdatesContainer.setBackgroundColor(trans);
appCheckUpdatesIcon.setImageResource(0);
magiskCheckUpdatesIcon.setImageResource(0);
appCheckUpdatesStatus.setText(null);
magiskCheckUpdatesStatus.setText(null);
appCheckUpdatesProgress.setVisibility(View.VISIBLE);
magiskCheckUpdatesProgress.setVisibility(View.VISIBLE);
2016-11-07 21:06:18 +08:00
new Async.CheckUpdates(prefs).exec();
});
if (prefs.getBoolean("update_check_done", false)) {
updateUI();
}
listener = (pref, s) -> {
if (s.equals("update_check_done")) {
if (pref.getBoolean(s, false)) {
Logger.dev("MagiskFragment: UI refresh triggered");
updateMagiskVersion();
updateUI();
}
}
};
2016-08-23 11:39:18 +02:00
2016-08-26 12:45:35 +02:00
return v;
}
2016-09-23 16:22:11 -05:00
@Override
public void onResume() {
super.onResume();
prefs.registerOnSharedPreferenceChangeListener(listener);
2016-09-23 16:22:11 -05:00
}
@Override
public void onDestroy() {
super.onDestroy();
prefs.unregisterOnSharedPreferenceChangeListener(listener);
}
2016-08-23 11:39:18 +02:00
2016-11-29 13:24:48 +08:00
private static void updateMagiskVersion() {
List<String> ret = Shell.sh("getprop magisk.version");
if (ret.get(0).length() == 0) {
magiskVersion = -1;
} else try {
magiskVersionString = ret.get(0);
magiskVersion = Double.parseDouble(ret.get(0));
} catch (NumberFormatException e) {
// Custom version don't need to receive updates
magiskVersion = Double.POSITIVE_INFINITY;
}
2016-11-29 13:24:48 +08:00
}
private void updateUI() {
String theme = prefs.getString("theme", "");
if (theme.equals("Dark")) {
builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialog_dh);
} else {
builder = new AlertDialog.Builder(getActivity());
}
if (magiskVersion == -1) {
magiskStatusContainer.setBackgroundColor(colorNeutral);
magiskStatusIcon.setImageResource(statusUnknown);
magiskVersionText.setTextColor(colorNeutral);
magiskVersionText.setText(R.string.magisk_version_error);
} else {
magiskStatusContainer.setBackgroundColor(colorOK);
magiskStatusIcon.setImageResource(statusOK);
magiskVersionText.setText(getString(R.string.magisk_version, magiskVersionString));
magiskVersionText.setTextColor(colorOK);
}
2016-08-23 11:39:18 +02:00
2016-09-29 01:42:25 +08:00
if (remoteMagiskVersion == -1) {
appCheckUpdatesContainer.setBackgroundColor(colorWarn);
magiskCheckUpdatesContainer.setBackgroundColor(colorWarn);
2016-08-26 12:45:35 +02:00
appCheckUpdatesIcon.setImageResource(R.drawable.ic_warning);
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_warning);
2016-08-26 12:45:35 +02:00
appCheckUpdatesStatus.setText(R.string.cannot_check_updates);
appCheckUpdatesStatus.setTextColor(colorWarn);
magiskCheckUpdatesStatus.setText(R.string.cannot_check_updates);
magiskCheckUpdatesStatus.setTextColor(colorWarn);
} else {
2016-09-29 01:42:25 +08:00
if (remoteMagiskVersion > magiskVersion) {
magiskCheckUpdatesContainer.setBackgroundColor(colorInfo);
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_file_download);
2016-09-29 01:42:25 +08:00
magiskCheckUpdatesStatus.setText(getString(R.string.magisk_update_available, String.valueOf(remoteMagiskVersion)));
magiskCheckUpdatesStatus.setTextColor(colorInfo);
magiskUpdateView.setOnClickListener(view -> builder
.setTitle(getString(R.string.update_title, getString(R.string.magisk)))
2016-09-29 01:42:25 +08:00
.setMessage(getString(R.string.update_msg, getString(R.string.magisk), String.valueOf(remoteMagiskVersion), magiskChangelog))
.setCancelable(true)
2016-10-03 00:05:53 +08:00
.setPositiveButton(R.string.download_install, flashMagisk)
.setNegativeButton(R.string.no_thanks, null)
.show());
2016-09-29 23:24:31 +08:00
2016-08-29 06:35:07 +08:00
} else {
magiskCheckUpdatesContainer.setBackgroundColor(colorOK);
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_check_circle);
magiskCheckUpdatesStatus.setText(getString(R.string.up_to_date, getString(R.string.magisk)));
magiskCheckUpdatesStatus.setTextColor(colorOK);
2016-09-29 23:24:31 +08:00
magiskUpdateView.setOnClickListener(view -> builder
.setTitle(getString(R.string.repo_install_title, getString(R.string.magisk)))
.setMessage(getString(R.string.repo_install_msg, "Magisk-v" + String.valueOf(remoteMagiskVersion)))
.setCancelable(true)
2016-10-03 00:05:53 +08:00
.setPositiveButton(R.string.download_install, flashMagisk)
2016-09-29 23:24:31 +08:00
.setNegativeButton(R.string.no_thanks, null)
.show());
}
2016-08-28 03:52:03 +08:00
2016-10-01 05:21:24 +08:00
if (remoteAppVersionCode > BuildConfig.VERSION_CODE) {
appCheckUpdatesContainer.setBackgroundColor(colorInfo);
appCheckUpdatesIcon.setImageResource(R.drawable.ic_file_download);
2016-09-29 01:42:25 +08:00
appCheckUpdatesStatus.setText(getString(R.string.app_update_available, remoteAppVersion));
appCheckUpdatesStatus.setTextColor(colorInfo);
appUpdateView.setOnClickListener(view -> builder
.setTitle(getString(R.string.update_title, getString(R.string.app_name)))
2016-09-29 01:42:25 +08:00
.setMessage(getString(R.string.update_msg, getString(R.string.app_name), remoteAppVersion, appChangelog))
.setCancelable(true)
2016-10-03 00:05:53 +08:00
.setPositiveButton(R.string.download_install, installMagiskApk)
.setNegativeButton(R.string.no_thanks, null)
.show()
);
} else {
appCheckUpdatesContainer.setBackgroundColor(colorOK);
appCheckUpdatesIcon.setImageResource(R.drawable.ic_check_circle);
appCheckUpdatesStatus.setText(getString(R.string.up_to_date, getString(R.string.app_name)));
appCheckUpdatesStatus.setTextColor(colorOK);
2016-09-25 01:16:08 -05:00
2016-08-26 12:45:35 +02:00
}
}
2016-08-29 06:35:07 +08:00
appCheckUpdatesProgress.setVisibility(View.GONE);
magiskCheckUpdatesProgress.setVisibility(View.GONE);
2016-09-29 01:42:25 +08:00
mSwipeRefreshLayout.setRefreshing(false);
2016-09-29 01:42:25 +08:00
if (magiskVersion == -1) {
builder
.setTitle(R.string.no_magisk_title)
.setMessage(R.string.no_magisk_msg)
.setCancelable(true)
2016-10-03 00:05:53 +08:00
.setPositiveButton(R.string.download_install, flashMagisk)
.setNegativeButton(R.string.no_thanks, null)
.show();
2016-08-26 12:45:35 +02:00
}
}
2016-08-23 11:39:18 +02:00
}