From fbdd72273e6924e0c0c23f432785a26827bde74a Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 10 Apr 2019 17:02:32 -0400 Subject: [PATCH] Restructure SuRequestActivity --- .../topjohnwu/magisk/SuRequestActivity.java | 240 ++++++++++-------- 1 file changed, 137 insertions(+), 103 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/SuRequestActivity.java b/app/src/main/java/com/topjohnwu/magisk/SuRequestActivity.java index c0e01f57a..afaade948 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SuRequestActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SuRequestActivity.java @@ -1,5 +1,6 @@ package com.topjohnwu.magisk; +import android.annotation.SuppressLint; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; @@ -25,8 +26,11 @@ import com.topjohnwu.magisk.utils.FingerprintHelper; import com.topjohnwu.magisk.utils.SuConnector; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import butterknife.BindView; +import java9.lang.Iterables; public class SuRequestActivity extends BaseActivity { @BindView(R.id.su_popup) LinearLayout suPopup; @@ -39,10 +43,8 @@ public class SuRequestActivity extends BaseActivity { @BindView(R.id.fingerprint) ImageView fingerprintImg; @BindView(R.id.warning) TextView warning; - private SuConnector connector; + private ActionHandler handler; private Policy policy; - private CountDownTimer timer; - private FingerprintHelper fingerprintHelper; private SharedPreferences timeoutPrefs; @Override @@ -50,22 +52,9 @@ public class SuRequestActivity extends BaseActivity { return R.style.SuRequest_Dark; } - @Override - public void finish() { - if (timer != null) - timer.cancel(); - if (fingerprintHelper != null) - fingerprintHelper.cancel(); - super.finish(); - } - @Override public void onBackPressed() { - if (policy != null) { - handleAction(Policy.DENY); - } else { - finish(); - } + handler.handleAction(Policy.DENY, -1); } @Override @@ -74,28 +63,58 @@ public class SuRequestActivity extends BaseActivity { lockOrientation(); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); - PackageManager pm = getPackageManager(); app.mDB.clearOutdated(); timeoutPrefs = App.deContext.getSharedPreferences("su_timeout", 0); // Get policy Intent intent = getIntent(); - try { - String socketName = intent.getStringExtra("socket"); - connector = new SuConnector(socketName) { + String socketName = intent.getStringExtra("socket"); + if (socketName != null) { + SuConnector connector; + try { + connector = new SuConnector(socketName) { + @Override + protected void onResponse() throws IOException { + out.writeInt(policy.policy); + } + }; + Bundle bundle = connector.readSocketInput(); + int uid = Integer.parseInt(bundle.getString("uid")); + policy = app.mDB.getPolicy(uid); + if (policy == null) { + policy = new Policy(uid, getPackageManager()); + } + } catch (IOException | PackageManager.NameNotFoundException e) { + e.printStackTrace(); + finish(); + return; + } + handler = new ActionHandler() { @Override - protected void onResponse() throws IOException { - out.writeInt(policy.policy); + void handleAction() { + connector.response(); + done(); + } + + @Override + void handleAction(int action) { + int pos = timeout.getSelectedItemPosition(); + timeoutPrefs.edit().putInt(policy.packageName, pos).apply(); + handleAction(action, Config.Value.TIMEOUT_LIST[pos]); + } + + @Override + void handleAction(int action, int time) { + policy.policy = action; + if (time >= 0) { + policy.until = (time == 0) ? 0 + : (System.currentTimeMillis() / 1000 + time * 60); + app.mDB.updatePolicy(policy); + } + handleAction(); } }; - Bundle bundle = connector.readSocketInput(); - int uid = Integer.parseInt(bundle.getString("uid")); - policy = app.mDB.getPolicy(uid); - if (policy == null) { - policy = new Policy(uid, pm); - } - } catch (IOException | PackageManager.NameNotFoundException e) { - e.printStackTrace(); + } else { finish(); return; } @@ -106,27 +125,30 @@ public class SuRequestActivity extends BaseActivity { return; } - switch ((int) Config.get(Config.Key.SU_AUTO_RESPONSE)) { - case Config.Value.SU_AUTO_DENY: - handleAction(Policy.DENY, 0); - return; - case Config.Value.SU_AUTO_ALLOW: - handleAction(Policy.ALLOW, 0); - return; - case Config.Value.SU_PROMPT: - default: - } - // If not interactive, response directly if (policy.policy != Policy.INTERACTIVE) { - handleAction(); + handler.handleAction(); return; } + switch ((int) Config.get(Config.Key.SU_AUTO_RESPONSE)) { + case Config.Value.SU_AUTO_DENY: + handler.handleAction(Policy.DENY, 0); + return; + case Config.Value.SU_AUTO_ALLOW: + handler.handleAction(Policy.ALLOW, 0); + return; + } + + showUI(); + } + + @SuppressLint("ClickableViewAccessibility") + private void showUI() { setContentView(R.layout.activity_request); new SuRequestActivity_ViewBinding(this); - appIcon.setImageDrawable(policy.info.loadIcon(pm)); + appIcon.setImageDrawable(policy.info.loadIcon(getPackageManager())); appNameView.setText(policy.appName); packageNameView.setText(policy.packageName); warning.setCompoundDrawablesRelativeWithIntrinsicBounds( @@ -138,53 +160,39 @@ public class SuRequestActivity extends BaseActivity { timeout.setAdapter(adapter); timeout.setSelection(timeoutPrefs.getInt(policy.packageName, 0)); - timer = new CountDownTimer((int) Config.get(Config.Key.SU_REQUEST_TIMEOUT) * 1000, 1000) { + CountDownTimer timer = new CountDownTimer( + (int) Config.get(Config.Key.SU_REQUEST_TIMEOUT) * 1000, 1000) { @Override - public void onTick(long millisUntilFinished) { - deny_btn.setText(getString(R.string.deny_with_str, "(" + millisUntilFinished / 1000 + ")")); + public void onTick(long remains) { + deny_btn.setText(getString(R.string.deny) + "(" + remains / 1000 + ")"); } @Override public void onFinish() { - deny_btn.setText(getString(R.string.deny_with_str, "(0)")); - handleAction(Policy.DENY); + deny_btn.setText(getString(R.string.deny)); + handler.handleAction(Policy.DENY); } }; + timer.start(); + Runnable cancelTimer = () -> { + timer.cancel(); + deny_btn.setText(getString(R.string.deny)); + }; + handler.addCancel(cancelTimer); boolean useFP = FingerprintHelper.useFingerprint(); - if (useFP) { - try { - fingerprintHelper = new FingerprintHelper() { - @Override - public void onAuthenticationError(int errorCode, CharSequence errString) { - warning.setText(errString); - } - - @Override - public void onAuthenticationHelp(int helpCode, CharSequence helpString) { - warning.setText(helpString); - } - - @Override - public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { - handleAction(Policy.ALLOW); - } - - @Override - public void onAuthenticationFailed() { - warning.setText(R.string.auth_fail); - } - }; - fingerprintHelper.authenticate(); - } catch (Exception e) { - e.printStackTrace(); - useFP = false; - } + if (useFP) try { + FingerprintHelper helper = new SuFingerprint(); + helper.authenticate(); + handler.addCancel(helper::cancel); + } catch (Exception e) { + e.printStackTrace(); + useFP = false; } if (!useFP) { grant_btn.setOnClickListener(v -> { - handleAction(Policy.ALLOW); + handler.handleAction(Policy.ALLOW); timer.cancel(); }); grant_btn.requestFocus(); @@ -194,37 +202,63 @@ public class SuRequestActivity extends BaseActivity { fingerprintImg.setVisibility(useFP ? View.VISIBLE : View.GONE); deny_btn.setOnClickListener(v -> { - handleAction(Policy.DENY); + handler.handleAction(Policy.DENY); timer.cancel(); }); - suPopup.setOnClickListener(v -> cancelTimeout()); - timeout.setOnTouchListener((v, event) -> cancelTimeout()); - timer.start(); + suPopup.setOnClickListener(v -> cancelTimer.run()); + timeout.setOnTouchListener((v, event) -> { + cancelTimer.run(); + return false; + }); } - private boolean cancelTimeout() { - timer.cancel(); - deny_btn.setText(getString(R.string.deny)); - return false; - } + private class SuFingerprint extends FingerprintHelper { - private void handleAction() { - connector.response(); - finish(); - } + SuFingerprint() throws Exception {} - private void handleAction(int action) { - int pos = timeout.getSelectedItemPosition(); - timeoutPrefs.edit().putInt(policy.packageName, pos).apply(); - handleAction(action, Config.Value.TIMEOUT_LIST[pos]); - } - - private void handleAction(int action, int time) { - policy.policy = action; - if (time >= 0) { - policy.until = (time == 0) ? 0 : (System.currentTimeMillis() / 1000 + time * 60); - app.mDB.updatePolicy(policy); + @Override + public void onAuthenticationError(int errorCode, CharSequence errString) { + warning.setText(errString); + } + + @Override + public void onAuthenticationHelp(int helpCode, CharSequence helpString) { + warning.setText(helpString); + } + + @Override + public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { + handler.handleAction(Policy.ALLOW); + } + + @Override + public void onAuthenticationFailed() { + warning.setText(R.string.auth_fail); + } + } + + private class ActionHandler { + private List cancelTasks = new ArrayList<>(); + + void handleAction() { + done(); + } + + void handleAction(int action) { + done(); + } + + void handleAction(int action, int time) { + done(); + } + + void addCancel(Runnable r) { + cancelTasks.add(r); + } + + void done() { + Iterables.forEach(cancelTasks, Runnable::run); + finish(); } - handleAction(); } }