2016-08-23 09:39:18 +00:00
|
|
|
package com.topjohnwu.magisk;
|
|
|
|
|
2016-08-26 10:45:35 +00:00
|
|
|
import android.Manifest;
|
2016-08-27 11:02:41 +00:00
|
|
|
import android.app.DownloadManager;
|
|
|
|
import android.content.BroadcastReceiver;
|
2016-08-26 10:45:35 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
2016-08-27 11:02:41 +00:00
|
|
|
import android.content.IntentFilter;
|
2016-08-26 10:45:35 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2016-08-27 11:02:41 +00:00
|
|
|
import android.database.Cursor;
|
2016-08-26 10:45:35 +00:00
|
|
|
import android.net.Uri;
|
2016-08-25 10:08:07 +00:00
|
|
|
import android.os.AsyncTask;
|
2016-08-23 09:39:18 +00:00
|
|
|
import android.os.Bundle;
|
2016-08-26 10:45:35 +00:00
|
|
|
import android.os.Environment;
|
|
|
|
import android.support.annotation.NonNull;
|
2016-08-23 09:39:18 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2016-08-26 10:45:35 +00:00
|
|
|
import android.support.v4.app.ActivityCompat;
|
2016-08-23 09:39:18 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
2016-08-27 11:02:41 +00:00
|
|
|
import android.support.v4.content.FileProvider;
|
2016-08-26 10:45:35 +00:00
|
|
|
import android.support.v7.app.AlertDialog;
|
|
|
|
import android.text.Html;
|
|
|
|
import android.text.TextUtils;
|
2016-08-23 09:39:18 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
2016-08-25 10:08:07 +00:00
|
|
|
import android.widget.ProgressBar;
|
2016-08-23 09:39:18 +00:00
|
|
|
import android.widget.TextView;
|
2016-08-26 10:45:35 +00:00
|
|
|
import android.widget.Toast;
|
2016-08-23 09:39:18 +00:00
|
|
|
|
2016-08-27 11:02:41 +00:00
|
|
|
import com.topjohnwu.magisk.utils.DownloadReceiver;
|
2016-08-24 21:58:15 +00:00
|
|
|
import com.topjohnwu.magisk.utils.Shell;
|
2016-08-23 09:39:18 +00:00
|
|
|
|
2016-08-26 10:45:35 +00:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
2016-08-23 09:39:18 +00:00
|
|
|
import java.io.File;
|
2016-08-26 10:45:35 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
2016-08-27 11:02:41 +00:00
|
|
|
import java.lang.reflect.Method;
|
2016-08-26 10:45:35 +00:00
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
2016-08-25 10:08:07 +00:00
|
|
|
import java.util.List;
|
2016-08-23 09:39:18 +00:00
|
|
|
|
|
|
|
import butterknife.BindColor;
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
|
|
|
public class MagiskFragment extends Fragment {
|
|
|
|
|
2016-08-27 11:02:41 +00:00
|
|
|
private static final String JSON_UPDATE_CHECK = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/updates/magisk_update.json";
|
2016-08-25 10:08:07 +00:00
|
|
|
|
2016-08-26 10:45:35 +00:00
|
|
|
@BindView(R.id.progressBarVersion) ProgressBar progressBar;
|
2016-08-23 09:39:18 +00:00
|
|
|
|
2016-08-26 10:45:35 +00:00
|
|
|
@BindView(R.id.magiskStatusView) View magiskStatusView;
|
2016-08-23 09:39:18 +00:00
|
|
|
@BindView(R.id.magisk_status_container) View magiskStatusContainer;
|
|
|
|
@BindView(R.id.magisk_status_icon) ImageView magiskStatusIcon;
|
|
|
|
@BindView(R.id.magisk_version) TextView magiskVersion;
|
|
|
|
|
2016-08-26 10:45:35 +00: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 09:39:18 +00:00
|
|
|
|
2016-08-26 10:45:35 +00: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 09:39:18 +00:00
|
|
|
|
|
|
|
@BindColor(R.color.green500) int green500;
|
2016-08-25 12:59:07 +00:00
|
|
|
@BindColor(R.color.accent) int accent;
|
2016-08-26 10:45:35 +00:00
|
|
|
@BindColor(R.color.blue500) int blue500;
|
|
|
|
@BindColor(R.color.grey500) int grey500;
|
2016-08-23 09:39:18 +00:00
|
|
|
|
|
|
|
int statusOK = R.drawable.ic_check_circle;
|
|
|
|
int statusUnknown = R.drawable.ic_help;
|
|
|
|
|
2016-08-26 10:45:35 +00:00
|
|
|
private String mLastLink;
|
|
|
|
private boolean mLastIsApp;
|
|
|
|
private List<String> version;
|
2016-08-27 11:02:41 +00:00
|
|
|
private long apkID, zipID;
|
2016-08-26 10:45:35 +00:00
|
|
|
|
2016-08-23 09:39:18 +00:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
2016-08-26 10:45:35 +00:00
|
|
|
View v = inflater.inflate(R.layout.magisk_fragment, container, false);
|
|
|
|
ButterKnife.bind(this, v);
|
2016-08-23 09:39:18 +00:00
|
|
|
|
2016-08-25 10:08:07 +00:00
|
|
|
new updateUI().execute();
|
2016-08-26 10:45:35 +00:00
|
|
|
new CheckUpdates().execute();
|
2016-08-23 09:39:18 +00:00
|
|
|
|
2016-08-26 10:45:35 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
|
|
|
|
if (requestCode == 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
2016-08-27 11:02:41 +00:00
|
|
|
downloadFile();
|
2016-08-26 10:45:35 +00:00
|
|
|
} else {
|
|
|
|
Toast.makeText(getContext(), R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setupCardView(final boolean app, final String versionCode, final String link, String changelog) {
|
|
|
|
View clickView;
|
|
|
|
if (app) {
|
|
|
|
clickView = appUpdateView;
|
|
|
|
appCheckUpdatesContainer.setBackgroundColor(blue500);
|
|
|
|
appCheckUpdatesIcon.setImageResource(R.drawable.ic_file_download);
|
|
|
|
appCheckUpdatesStatus.setText(R.string.app_update_available);
|
|
|
|
} else {
|
|
|
|
clickView = magiskUpdateView;
|
|
|
|
magiskCheckUpdatesContainer.setBackgroundColor(blue500);
|
|
|
|
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_file_download);
|
|
|
|
magiskCheckUpdatesStatus.setText(R.string.magisk_update_available);
|
|
|
|
}
|
2016-08-23 09:39:18 +00:00
|
|
|
|
2016-08-26 10:45:35 +00:00
|
|
|
String text = app ? getString(R.string.app_name) : getString(R.string.magisk);
|
|
|
|
final String msg = getString(R.string.update_available_message, text, versionCode, changelog);
|
|
|
|
|
2016-08-27 11:02:41 +00:00
|
|
|
clickView.setOnClickListener(view -> new AlertDialog.Builder(getContext())
|
|
|
|
.setTitle(R.string.update_available)
|
|
|
|
.setMessage(Html.fromHtml(msg))
|
|
|
|
.setCancelable(false)
|
|
|
|
.setPositiveButton(R.string.update, (dialogInterface, i) -> {
|
|
|
|
mLastLink = link;
|
|
|
|
mLastIsApp = app;
|
|
|
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
downloadFile();
|
|
|
|
} else {
|
|
|
|
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
.setNegativeButton(R.string.no_thanks, null)
|
|
|
|
.show());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void downloadFile() {
|
|
|
|
|
|
|
|
File downloadFile, dir = new File(Environment.getExternalStorageDirectory() + "/MagiskManager");
|
|
|
|
DownloadReceiver receiver;
|
|
|
|
|
|
|
|
if (mLastIsApp) {
|
|
|
|
downloadFile = new File(dir + "/MagiskManager.apk");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
downloadFile = new File(dir + "/Magisk.zip");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dir.exists()) dir.mkdir();
|
|
|
|
|
|
|
|
DownloadManager downloadManager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
|
|
|
|
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mLastLink));
|
|
|
|
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
|
|
|
|
.setDestinationUri(Uri.fromFile(downloadFile));
|
|
|
|
|
|
|
|
if (downloadFile.exists()) downloadFile.delete();
|
|
|
|
|
|
|
|
long downloadID = downloadManager.enqueue(request);
|
|
|
|
|
|
|
|
if (mLastIsApp) {
|
|
|
|
receiver = new DownloadReceiver(downloadID) {
|
|
|
|
@Override
|
|
|
|
public void task(File file) {
|
|
|
|
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
|
|
|
|
install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
|
install.setData(FileProvider.getUriForFile(context, "com.topjohnwu.magisk.provider", file));
|
|
|
|
context.startActivity(install);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
receiver = new DownloadReceiver(downloadID) {
|
|
|
|
@Override
|
|
|
|
public void task(final File file) {
|
|
|
|
new AlertDialog.Builder(context)
|
|
|
|
.setTitle("Reboot Recovery")
|
|
|
|
.setMessage("Do you want to flash in recovery now?")
|
|
|
|
.setCancelable(false)
|
|
|
|
.setPositiveButton("Yes, flash now", (dialogInterface, i) -> Toast.makeText(context, file.getPath(), Toast.LENGTH_LONG).show())
|
|
|
|
.setNegativeButton(R.string.no_thanks, null)
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
getActivity().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
2016-08-23 09:39:18 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 10:08:07 +00:00
|
|
|
private class updateUI extends AsyncTask<Void, Void, Void> {
|
2016-08-23 09:39:18 +00:00
|
|
|
|
2016-08-25 10:08:07 +00:00
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... voids) {
|
|
|
|
// Make sure static block invoked
|
|
|
|
Shell.rootAccess();
|
|
|
|
return null;
|
2016-08-23 09:39:18 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 10:08:07 +00:00
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void v) {
|
|
|
|
super.onPostExecute(v);
|
2016-08-23 09:39:18 +00:00
|
|
|
|
2016-08-26 10:45:35 +00:00
|
|
|
version = Shell.sh("getprop magisk.version");
|
2016-08-23 09:39:18 +00:00
|
|
|
|
2016-08-27 11:02:41 +00:00
|
|
|
if (version.get(0).replaceAll("\\s", "").isEmpty()) {
|
2016-08-25 10:08:07 +00:00
|
|
|
magiskStatusContainer.setBackgroundColor(grey500);
|
|
|
|
magiskStatusIcon.setImageResource(statusUnknown);
|
2016-08-23 09:39:18 +00:00
|
|
|
|
2016-08-25 10:08:07 +00:00
|
|
|
magiskVersion.setTextColor(grey500);
|
|
|
|
magiskVersion.setText(R.string.magisk_version_error);
|
2016-08-23 09:39:18 +00:00
|
|
|
} else {
|
2016-08-25 10:08:07 +00:00
|
|
|
magiskStatusContainer.setBackgroundColor(green500);
|
|
|
|
magiskStatusIcon.setImageResource(statusOK);
|
2016-08-23 09:39:18 +00:00
|
|
|
|
2016-08-25 10:08:07 +00:00
|
|
|
magiskVersion.setTextColor(green500);
|
|
|
|
magiskVersion.setText(getString(R.string.magisk_version, version.get(0)));
|
2016-08-23 09:39:18 +00:00
|
|
|
}
|
|
|
|
|
2016-08-27 11:02:41 +00:00
|
|
|
progressBar.setVisibility(View.GONE);
|
2016-08-26 10:45:35 +00:00
|
|
|
magiskStatusView.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class CheckUpdates extends AsyncTask<Void, Void, String> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected String doInBackground(Void... voids) {
|
|
|
|
try {
|
|
|
|
HttpURLConnection c = (HttpURLConnection) new URL(JSON_UPDATE_CHECK).openConnection();
|
|
|
|
c.setRequestMethod("GET");
|
|
|
|
c.setInstanceFollowRedirects(false);
|
|
|
|
c.setDoOutput(false);
|
|
|
|
c.connect();
|
|
|
|
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
String line;
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
sb.append(line);
|
|
|
|
}
|
|
|
|
br.close();
|
|
|
|
return sb.toString();
|
|
|
|
} catch (IOException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(String result) {
|
|
|
|
super.onPostExecute(result);
|
|
|
|
|
|
|
|
appCheckUpdatesProgress.setVisibility(View.GONE);
|
|
|
|
magiskCheckUpdatesProgress.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
if (result == null) {
|
|
|
|
appCheckUpdatesContainer.setBackgroundColor(accent);
|
|
|
|
magiskCheckUpdatesContainer.setBackgroundColor(accent);
|
|
|
|
|
|
|
|
appCheckUpdatesIcon.setImageResource(R.drawable.ic_warning);
|
|
|
|
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_warning);
|
|
|
|
|
|
|
|
appCheckUpdatesStatus.setText(R.string.cannot_check_updates);
|
|
|
|
magiskCheckUpdatesStatus.setText(R.string.cannot_check_updates);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
JSONObject json = new JSONObject(result);
|
|
|
|
|
|
|
|
JSONObject app = json.getJSONObject("app");
|
|
|
|
JSONObject magisk = json.getJSONObject("magisk");
|
|
|
|
|
|
|
|
String appVersionCode = app.getString("versionCode");
|
|
|
|
String appLink = app.getString("link");
|
|
|
|
String appChangelog = app.getString("changelog");
|
2016-08-23 09:39:18 +00:00
|
|
|
|
2016-08-26 10:45:35 +00:00
|
|
|
String magiskVersionCode = magisk.getString("versionCode");
|
|
|
|
String magiskLink = magisk.getString("link");
|
|
|
|
String magiskChangelog = magisk.getString("changelog");
|
2016-08-23 09:39:18 +00:00
|
|
|
|
2016-08-26 10:45:35 +00:00
|
|
|
if (Integer.parseInt(appVersionCode) > BuildConfig.VERSION_CODE) {
|
|
|
|
setupCardView(true, appVersionCode, appLink, appChangelog);
|
|
|
|
} else {
|
|
|
|
appCheckUpdatesContainer.setBackgroundColor(green500);
|
|
|
|
appCheckUpdatesIcon.setImageResource(R.drawable.ic_check_circle);
|
|
|
|
appCheckUpdatesStatus.setText(getString(R.string.up_to_date, getString(R.string.app_name)));
|
|
|
|
}
|
|
|
|
|
2016-08-27 11:02:41 +00:00
|
|
|
String v = version.get(0).replaceAll("\\s", "");
|
2016-08-26 10:45:35 +00:00
|
|
|
|
|
|
|
int versionInt = TextUtils.isEmpty(v) ? 0 : Integer.parseInt(v);
|
|
|
|
|
|
|
|
if (Integer.parseInt(magiskVersionCode) > versionInt) {
|
|
|
|
setupCardView(false, magiskVersionCode, magiskLink, magiskChangelog);
|
|
|
|
} else {
|
|
|
|
magiskCheckUpdatesContainer.setBackgroundColor(green500);
|
|
|
|
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_check_circle);
|
|
|
|
magiskCheckUpdatesStatus.setText(getString(R.string.up_to_date, getString(R.string.magisk)));
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (JSONException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-23 09:39:18 +00:00
|
|
|
}
|