Magisk/app/src/full/java/com/topjohnwu/magisk/FlashActivity.java

166 lines
5.5 KiB
Java
Raw Normal View History

2017-07-18 00:59:22 +08:00
package com.topjohnwu.magisk;
import android.Manifest;
2017-08-31 03:07:33 +08:00
import android.content.Intent;
2017-07-18 00:59:22 +08:00
import android.net.Uri;
import android.os.Bundle;
2017-10-16 14:08:14 +08:00
import android.text.TextUtils;
2017-07-18 00:59:22 +08:00
import android.view.View;
2017-08-31 03:07:33 +08:00
import android.widget.Button;
2017-07-18 00:59:22 +08:00
import android.widget.LinearLayout;
2017-10-16 14:08:14 +08:00
import android.widget.ScrollView;
2017-07-18 00:59:22 +08:00
import android.widget.TextView;
import android.widget.Toast;
2017-07-18 00:59:22 +08:00
import com.topjohnwu.magisk.asyncs.FlashZip;
2017-09-03 00:10:14 +08:00
import com.topjohnwu.magisk.asyncs.InstallMagisk;
2018-08-01 17:57:11 +08:00
import com.topjohnwu.magisk.components.BaseActivity;
2018-07-30 20:37:00 +08:00
import com.topjohnwu.magisk.utils.Download;
2018-06-27 05:58:56 +08:00
import com.topjohnwu.magisk.utils.RootUtils;
2018-07-31 17:41:54 +08:00
import com.topjohnwu.magisk.utils.Utils;
2018-01-25 18:43:30 +08:00
import com.topjohnwu.superuser.CallbackList;
2018-01-21 06:07:24 +08:00
import com.topjohnwu.superuser.Shell;
2017-08-31 03:07:33 +08:00
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
2018-09-10 02:27:45 -04:00
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import butterknife.BindView;
import butterknife.OnClick;
2017-07-18 00:59:22 +08:00
2018-08-01 17:57:11 +08:00
public class FlashActivity extends BaseActivity {
2017-07-18 00:59:22 +08:00
@BindView(R.id.toolbar) Toolbar toolbar;
@BindView(R.id.txtLog) TextView flashLogs;
@BindView(R.id.button_panel) public LinearLayout buttonPanel;
@BindView(R.id.reboot) public Button reboot;
@BindView(R.id.scrollView) ScrollView sv;
2017-07-18 00:59:22 +08:00
private List<String> logs;
@OnClick(R.id.reboot)
void reboot() {
2018-07-27 04:48:32 +08:00
Shell.su("/system/bin/reboot").submit();
2017-07-18 00:59:22 +08:00
}
@OnClick(R.id.save_logs)
void saveLogs() {
runWithPermission(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, () -> {
Calendar now = Calendar.getInstance();
String filename = String.format(Locale.US,
"magisk_install_log_%04d%02d%02d_%02d%02d%02d.log",
now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1,
now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
File logFile = new File(Download.EXTERNAL_PATH, filename);
try (FileWriter writer = new FileWriter(logFile)) {
for (String s : logs) {
writer.write(s);
writer.write('\n');
}
} catch (IOException e) {
e.printStackTrace();
return;
}
Utils.toast(logFile.getPath(), Toast.LENGTH_LONG);
});
}
2017-12-03 15:15:00 +08:00
@Override
public int getDarkTheme() {
2018-06-10 14:51:37 +08:00
return R.style.AppTheme_StatusBar_Dark;
2017-12-03 15:15:00 +08:00
}
2017-07-18 00:59:22 +08:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash);
new FlashActivity_ViewBinding(this);
2017-07-18 00:59:22 +08:00
setSupportActionBar(toolbar);
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setTitle(R.string.flashing);
}
setFloating();
setFinishOnTouchOutside(false);
2017-08-31 03:07:33 +08:00
if (!Shell.rootAccess())
reboot.setVisibility(View.GONE);
2017-07-18 00:59:22 +08:00
logs = new ArrayList<>();
2018-01-27 08:23:02 +08:00
CallbackList<String> console = new CallbackList<String>(new ArrayList<>()) {
2018-08-03 22:48:44 +08:00
private void updateUI() {
flashLogs.setText(TextUtils.join("\n", this));
sv.postDelayed(() -> sv.fullScroll(ScrollView.FOCUS_DOWN), 10);
}
@Override
2018-01-25 18:43:30 +08:00
public void onAddElement(String s) {
2018-01-21 06:07:24 +08:00
logs.add(s);
2018-08-03 22:48:44 +08:00
updateUI();
}
@Override
public String set(int i, String s) {
String ret = super.set(i, s);
Data.mainHandler.post(this::updateUI);
return ret;
}
};
2017-07-18 00:59:22 +08:00
// We must receive a Uri of the target zip
2017-08-31 03:07:33 +08:00
Intent intent = getIntent();
Uri uri = intent.getData();
switch (intent.getStringExtra(Const.Key.FLASH_ACTION)) {
2017-11-06 04:41:23 +08:00
case Const.Value.FLASH_ZIP:
new FlashZip(this, uri, console, logs).exec();
2017-08-31 03:07:33 +08:00
break;
2018-06-27 05:58:56 +08:00
case Const.Value.UNINSTALL:
new UninstallMagisk(this, uri, console, logs).exec();
break;
2017-11-06 04:41:23 +08:00
case Const.Value.FLASH_MAGISK:
2018-07-29 22:58:22 +08:00
new InstallMagisk(this, console, logs, InstallMagisk.DIRECT_MODE).exec();
2018-06-26 00:29:01 +08:00
break;
case Const.Value.FLASH_INACTIVE_SLOT:
2018-07-29 22:58:22 +08:00
new InstallMagisk(this, console, logs, InstallMagisk.SECOND_SLOT_MODE).exec();
2018-06-26 00:29:01 +08:00
break;
case Const.Value.PATCH_BOOT:
runWithPermission(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
() -> new InstallMagisk(this, console, logs,
intent.getParcelableExtra(Const.Key.FLASH_SET_BOOT)).exec());
2017-09-03 00:10:14 +08:00
break;
2017-08-31 03:07:33 +08:00
}
2017-07-18 00:59:22 +08:00
}
@Override
public void onBackPressed() {
// Prevent user accidentally press back button
}
2018-06-27 05:58:56 +08:00
private static class UninstallMagisk extends FlashZip {
2018-08-01 17:57:11 +08:00
private UninstallMagisk(BaseActivity context, Uri uri, List<String> console, List<String> logs) {
2018-06-27 05:58:56 +08:00
super(context, uri, console, logs);
}
@Override
protected void onPostExecute(Integer result) {
if (result == 1) {
Data.mainHandler.postDelayed(() ->
2018-06-27 05:58:56 +08:00
RootUtils.uninstallPkg(getActivity().getPackageName()), 3000);
} else {
super.onPostExecute(result);
}
}
}
2017-07-18 00:59:22 +08:00
}