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

183 lines
8.7 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.res.TypedArray;
import android.graphics.Color;
2016-08-25 18:08:07 +08:00
import android.os.AsyncTask;
2016-08-23 11:39:18 +02:00
import android.os.Bundle;
import android.support.annotation.Nullable;
2016-09-21 16:55:20 -05:00
2016-08-27 19:02:41 +08:00
import android.support.v4.content.FileProvider;
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-21 01:08:05 +08:00
import com.topjohnwu.magisk.utils.Async;
2016-08-28 03:52:03 +08:00
import com.topjohnwu.magisk.utils.Utils;
2016-08-23 11:39:18 +02:00
import java.io.File;
import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MagiskFragment extends Fragment {
2016-08-26 12:45:35 +02:00
@BindView(R.id.progressBarVersion) ProgressBar progressBar;
2016-08-23 11:39:18 +02:00
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;
@BindView(R.id.magisk_version) TextView magiskVersion;
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
2016-08-26 12:45:35 +02:00
@BindColor(R.color.grey500) int grey500;
2016-08-23 11:39:18 +02:00
private int colorOK, colorWarn, colorNeutral;
2016-08-23 11:39:18 +02:00
int statusOK = R.drawable.ic_check_circle;
int statusUnknown = R.drawable.ic_help;
@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);
int[] attrs0 = {R.attr.ColorOK};
int[] attrs1 = {R.attr.ColorWarn};
int[] attrs2 = {R.attr.ColorNeutral};
TypedArray ta0 = getActivity().obtainStyledAttributes(attrs0);
TypedArray ta1 = getActivity().obtainStyledAttributes(attrs1);
TypedArray ta2 = getActivity().obtainStyledAttributes(attrs2);
colorOK = ta0.getColor(0, Color.GRAY);
colorWarn = ta1.getColor(0, Color.GRAY);
colorNeutral = ta2.getColor(0, Color.GRAY);
ta0.recycle();
ta1.recycle();
ta2.recycle();
2016-08-25 18:08:07 +08:00
new updateUI().execute();
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();
getActivity().setTitle("Magisk");
}
2016-08-25 18:08:07 +08:00
private class updateUI extends AsyncTask<Void, Void, Void> {
2016-08-23 11:39:18 +02:00
2016-08-25 18:08:07 +08:00
@Override
protected Void doInBackground(Void... voids) {
return null;
2016-08-23 11:39:18 +02:00
}
2016-08-25 18:08:07 +08:00
@Override
protected void onPostExecute(Void v) {
super.onPostExecute(v);
2016-08-23 11:39:18 +02:00
2016-08-29 06:35:07 +08:00
if (Utils.magiskVersion == -1) {
2016-08-25 18:08:07 +08:00
magiskStatusContainer.setBackgroundColor(grey500);
magiskStatusIcon.setImageResource(statusUnknown);
2016-08-23 11:39:18 +02:00
2016-08-25 18:08:07 +08:00
magiskVersion.setTextColor(grey500);
magiskVersion.setText(R.string.magisk_version_error);
2016-08-23 11:39:18 +02:00
} else {
magiskStatusContainer.setBackgroundColor(colorOK);
2016-08-25 18:08:07 +08:00
magiskStatusIcon.setImageResource(statusOK);
2016-08-23 11:39:18 +02:00
magiskVersion.setTextColor(colorOK);
2016-08-29 06:35:07 +08:00
magiskVersion.setText(getString(R.string.magisk_version, String.valueOf(Utils.magiskVersion)));
2016-08-26 12:45:35 +02:00
}
2016-08-29 06:35:07 +08:00
if (Utils.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);
appCheckUpdatesStatus.setText(R.string.cannot_check_updates);
magiskCheckUpdatesStatus.setText(R.string.cannot_check_updates);
2016-08-29 06:35:07 +08:00
} else {
if (Utils.remoteMagiskVersion > Utils.magiskVersion) {
magiskCheckUpdatesContainer.setBackgroundColor(colorNeutral);
2016-08-28 03:52:03 +08:00
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_file_download);
2016-08-29 06:35:07 +08:00
magiskCheckUpdatesStatus.setText(getString(R.string.magisk_update_available, String.valueOf(Utils.remoteMagiskVersion)));
magiskUpdateView.setOnClickListener(view -> new AlertDialog.Builder(getActivity())
.setTitle(getString(R.string.update_title, getString(R.string.magisk)))
2016-09-18 02:32:08 +08:00
.setMessage(getString(R.string.update_msg, getString(R.string.magisk), String.valueOf(Utils.remoteMagiskVersion), Utils.magiskChangelog))
2016-08-29 06:35:07 +08:00
.setCancelable(true)
.setPositiveButton(R.string.download_install, (dialogInterface, i) -> {
Utils.downloadAndReceive(
getActivity(),
new Utils.DownloadReceiver(getString(R.string.magisk)) {
@Override
public void task(File file) {
2016-09-21 01:08:05 +08:00
new Async.FlashZIP(mContext, mName, file.getPath()).execute();
2016-08-29 06:35:07 +08:00
}
},
Utils.magiskLink, "latest_magisk.zip");
})
.setNegativeButton(R.string.no_thanks, null)
.show());
2016-08-28 03:52:03 +08:00
} else {
magiskCheckUpdatesContainer.setBackgroundColor(colorOK);
2016-08-28 03:52:03 +08:00
magiskCheckUpdatesIcon.setImageResource(R.drawable.ic_check_circle);
magiskCheckUpdatesStatus.setText(getString(R.string.up_to_date, getString(R.string.magisk)));
}
2016-08-29 06:35:07 +08:00
if (Utils.remoteAppVersion > BuildConfig.VERSION_CODE) {
appCheckUpdatesContainer.setBackgroundColor(colorNeutral);
2016-08-28 03:52:03 +08:00
appCheckUpdatesIcon.setImageResource(R.drawable.ic_file_download);
2016-08-29 06:35:07 +08:00
appCheckUpdatesStatus.setText(getString(R.string.app_update_available, String.valueOf(Utils.remoteAppVersion)));
appUpdateView.setOnClickListener(view -> new AlertDialog.Builder(getActivity())
.setTitle(getString(R.string.update_title, getString(R.string.app_name)))
2016-09-18 02:32:08 +08:00
.setMessage(getString(R.string.update_msg, getString(R.string.app_name), String.valueOf(Utils.remoteAppVersion), Utils.appChangelog))
2016-08-29 06:35:07 +08:00
.setCancelable(true)
.setPositiveButton(R.string.download_install, (dialogInterface, i) -> {
Utils.downloadAndReceive(getActivity(),
new Utils.DownloadReceiver() {
@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(mContext, "com.topjohnwu.magisk.provider", file));
mContext.startActivity(install);
}
},
Utils.appLink, "latest_manager.apk");
})
.setNegativeButton(R.string.no_thanks, null)
.show()
2016-08-28 03:52:03 +08:00
);
2016-08-26 12:45:35 +02:00
} else {
appCheckUpdatesContainer.setBackgroundColor(colorOK);
2016-08-26 12:45:35 +02:00
appCheckUpdatesIcon.setImageResource(R.drawable.ic_check_circle);
appCheckUpdatesStatus.setText(getString(R.string.up_to_date, getString(R.string.app_name)));
}
}
2016-08-29 06:35:07 +08:00
progressBar.setVisibility(View.GONE);
appCheckUpdatesProgress.setVisibility(View.GONE);
magiskCheckUpdatesProgress.setVisibility(View.GONE);
2016-08-26 12:45:35 +02:00
}
}
2016-08-23 11:39:18 +02:00
}