From c78209604c8cc5c18d876bf32f11f1e60e7ad398 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 25 Dec 2016 22:36:51 +0800 Subject: [PATCH] Update Install Fragment UI --- .../com/topjohnwu/magisk/InstallFragment.java | 81 ++++---- .../com/topjohnwu/magisk/MainActivity.java | 37 +++- .../com/topjohnwu/magisk/SplashActivity.java | 10 +- .../com/topjohnwu/magisk/StatusFragment.java | 22 ++- .../com/topjohnwu/magisk/utils/Async.java | 17 ++ .../com/topjohnwu/magisk/utils/Utils.java | 4 - app/src/main/res/layout/install_fragment.xml | 179 ++++++++++-------- app/src/main/res/menu/drawer.xml | 3 +- app/src/main/res/values-de/strings.xml | 5 +- app/src/main/res/values-es/strings.xml | 5 +- app/src/main/res/values-it/strings.xml | 5 +- app/src/main/res/values-nl/strings.xml | 5 +- app/src/main/res/values-pt/strings.xml | 5 +- app/src/main/res/values/strings.xml | 10 +- 14 files changed, 217 insertions(+), 171 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/InstallFragment.java b/app/src/main/java/com/topjohnwu/magisk/InstallFragment.java index 0ed0429a6..179d9b1ce 100644 --- a/app/src/main/java/com/topjohnwu/magisk/InstallFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/InstallFragment.java @@ -1,10 +1,8 @@ package com.topjohnwu.magisk; import android.app.Fragment; -import android.net.Uri; import android.os.Bundle; import android.support.annotation.Nullable; -import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -13,22 +11,21 @@ import android.widget.Button; import android.widget.Spinner; import android.widget.TextView; -import com.topjohnwu.magisk.receivers.DownloadReceiver; -import com.topjohnwu.magisk.utils.Async; -import com.topjohnwu.magisk.utils.Shell; -import com.topjohnwu.magisk.utils.Utils; -import com.topjohnwu.magisk.utils.ZipUtils; +import com.topjohnwu.magisk.utils.CallbackHandler; -import java.io.File; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; -public class InstallFragment extends Fragment { +public class InstallFragment extends Fragment implements CallbackHandler.EventListener { - private List blockList; + public static final CallbackHandler.Event blockDetectionDone = new CallbackHandler.Event(); + public static List blockList; + public static String bootBlock = null; + + @BindView(R.id.install_title) TextView installTitle; @BindView(R.id.block_spinner) Spinner spinner; @BindView(R.id.detect_bootimage) Button detectButton; @@ -37,56 +34,46 @@ public class InstallFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.install_fragment, container, false); ButterKnife.bind(this, v); - detectButton.setOnClickListener(v1 -> detectBootImage()); - - getBlockList(); - + detectButton.setOnClickListener(v1 -> toAutoDetect()); + installTitle.setText(getString(R.string.install_magisk_title, StatusFragment.remoteMagiskVersion)); + if (blockDetectionDone.isTriggered) { + updateUI(); + } return v; } - private void getBlockList() { - new Async.RootTask() { - @Override - protected Void doInBackground(Void... params) { - blockList = Utils.getBlockList(); - return null; - } - @Override - protected void onPostExecute(Void aVoid) { - ArrayAdapter adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, blockList); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - spinner.setAdapter(adapter); - detectBootImage(); - } - }.exec(); + @Override + public void onTrigger(CallbackHandler.Event event) { + updateUI(); } - private void detectBootImage() { - new Async.RootTask() { - String boot; - @Override - protected Void doInBackground(Void... params) { - boot = Utils.detectBootImage(); - return null; - } - @Override - protected void onPostExecute(Void aVoid) { - if (boot != null) { - int idx = blockList.indexOf(boot); - if (idx != -1) { - spinner.setSelection(idx); - } - } - } - }.exec(); + private void updateUI() { + blockList.add(0, getString(R.string.auto_detect, bootBlock)); + ArrayAdapter adapter = new ArrayAdapter<>(getActivity(), + android.R.layout.simple_spinner_item, blockList); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spinner.setAdapter(adapter); + toAutoDetect(); + } + + private void toAutoDetect() { + if (bootBlock != null) { + spinner.setSelection(0); + } } @Override public void onResume() { super.onResume(); getActivity().setTitle(R.string.install); + CallbackHandler.register(blockDetectionDone, this); } + @Override + public void onDestroy() { + super.onDestroy(); + CallbackHandler.unRegister(blockDetectionDone, this); + } } // private AlertDialog.OnClickListener flashMagisk = (dialogInterface, i) -> Utils.dlAndReceive( diff --git a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java index 218c64766..34d9d2254 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java @@ -4,10 +4,12 @@ import android.Manifest; import android.app.Fragment; import android.app.FragmentTransaction; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.os.Handler; +import android.preference.Preference; import android.preference.PreferenceManager; import android.support.annotation.IdRes; import android.support.annotation.NonNull; @@ -22,16 +24,19 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; +import com.topjohnwu.magisk.utils.CallbackHandler; import com.topjohnwu.magisk.utils.Logger; import butterknife.BindView; import butterknife.ButterKnife; -public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { +public class MainActivity extends AppCompatActivity + implements NavigationView.OnNavigationItemSelectedListener, CallbackHandler.EventListener { private static final String SELECTED_ITEM_ID = "SELECTED_ITEM_ID"; private final Handler mDrawerHandler = new Handler(); + private SharedPreferences prefs; @BindView(R.id.toolbar) Toolbar toolbar; @BindView(R.id.drawer_layout) DrawerLayout drawer; @@ -43,7 +48,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On @Override protected void onCreate(final Bundle savedInstanceState) { - String theme = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("theme", ""); + prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + + String theme = prefs.getString("theme", ""); Logger.dev("MainActivity: Theme is " + theme); if (theme.equals("Dark")) { setTheme(R.style.AppTheme_dh); @@ -85,14 +92,25 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On } navigationView.setNavigationItemSelectedListener(this); + + if (StatusFragment.updateCheckDone.isTriggered) { + onTrigger(StatusFragment.updateCheckDone); + } } @Override protected void onResume() { super.onResume(); + CallbackHandler.register(StatusFragment.updateCheckDone, this); checkHideSection(); } + @Override + protected void onDestroy() { + super.onDestroy(); + CallbackHandler.unRegister(StatusFragment.updateCheckDone, this); + } + @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); @@ -117,17 +135,26 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On return true; } + @Override + public void onTrigger(CallbackHandler.Event event) { + Menu menu = navigationView.getMenu(); + if (StatusFragment.remoteMagiskVersion < 0) { + menu.findItem(R.id.install).setVisible(false); + } else { + menu.findItem(R.id.install).setVisible(true); + } + } + private void checkHideSection() { Menu menu = navigationView.getMenu(); - if (StatusFragment.magiskVersion == -1) { + if (StatusFragment.magiskVersion < 0) { menu.findItem(R.id.magiskhide).setVisible(false); menu.findItem(R.id.modules).setVisible(false); menu.findItem(R.id.downloads).setVisible(false); } else { menu.findItem(R.id.modules).setVisible(true); menu.findItem(R.id.downloads).setVisible(true); - menu.findItem(R.id.magiskhide).setVisible( - PreferenceManager.getDefaultSharedPreferences(this).getBoolean("magiskhide", false)); + menu.findItem(R.id.magiskhide).setVisible(prefs.getBoolean("magiskhide", false)); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java index 649585600..468d9c18f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java @@ -31,6 +31,8 @@ public class SplashActivity extends AppCompatActivity { .putBoolean("hosts", Utils.itemExist(false, "/magisk/.core/hosts")) .apply(); + // Start all async tasks + new Async.GetBootBlocks().exec(); new Async.CheckUpdates().exec(); Async.checkSafetyNet(getApplicationContext()); new Async.LoadModules() { @@ -38,12 +40,12 @@ public class SplashActivity extends AppCompatActivity { protected void onPostExecute(Void v) { super.onPostExecute(v); new Async.LoadRepos(getApplicationContext()).exec(); - // Start main activity - Intent intent = new Intent(getApplicationContext(), MainActivity.class); - startActivity(intent); - finish(); } }.exec(); + // Start main activity + Intent intent = new Intent(getApplicationContext(), MainActivity.class); + startActivity(intent); + finish(); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java b/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java index b87f3c8e8..a238aa5f7 100644 --- a/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java @@ -62,6 +62,10 @@ public class StatusFragment extends Fragment implements CallbackHandler.EventLis private AlertDialog.Builder builder; + static { + checkMagiskInfo(); + } + @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -147,13 +151,10 @@ public class StatusFragment extends Fragment implements CallbackHandler.EventLis CallbackHandler.unRegister(safetyNetDone, this); } - private void updateUI() { - int image, color; - + private static void checkMagiskInfo() { List ret = Shell.sh("getprop magisk.version"); if (ret.get(0).length() == 0) { magiskVersion = -1; - magiskVersionText.setText(R.string.magisk_version_error); } else { try { magiskVersionString = ret.get(0); @@ -162,6 +163,17 @@ public class StatusFragment extends Fragment implements CallbackHandler.EventLis // Custom version don't need to receive updates magiskVersion = Double.POSITIVE_INFINITY; } + } + } + + private void updateUI() { + int image, color; + + checkMagiskInfo(); + + if (magiskVersion < 0) { + magiskVersionText.setText(R.string.magisk_version_error); + } else { magiskVersionText.setText(getString(R.string.magisk_version, magiskVersionString)); } @@ -199,7 +211,7 @@ public class StatusFragment extends Fragment implements CallbackHandler.EventLis } else if (remoteMagiskVersion > magiskVersion) { color = colorInfo; image = R.drawable.ic_update; - magiskUpdateText.setText(getString(R.string.magisk_update_available, String.valueOf(remoteMagiskVersion))); + magiskUpdateText.setText(getString(R.string.magisk_update_available, remoteMagiskVersion)); } else { color = colorOK; image = R.drawable.ic_check_circle; diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Async.java b/app/src/main/java/com/topjohnwu/magisk/utils/Async.java index 805892a6a..2784fe83e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Async.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Async.java @@ -12,6 +12,7 @@ import android.support.v7.app.AlertDialog; import android.util.Log; import android.widget.Toast; +import com.topjohnwu.magisk.InstallFragment; import com.topjohnwu.magisk.ModulesFragment; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.ReposFragment; @@ -293,4 +294,20 @@ public class Async { } } + + public static class GetBootBlocks extends RootTask { + @Override + protected Void doInBackground(Void... params) { + InstallFragment.blockList = Shell.su("ls /dev/block | grep mmc"); + if (InstallFragment.bootBlock == null) { + InstallFragment.bootBlock = Utils.detectBootImage(); + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + InstallFragment.blockDetectionDone.trigger(); + } + } } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java index 7c527e0a2..7915f0d76 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -138,10 +138,6 @@ public class Utils { .replace("#", "").replace("@", "").replace("*", ""); } - public static List getBlockList() { - return Shell.su("ls /dev/block | grep mmc"); - } - public static String detectBootImage() { String[] commands = { "for PARTITION in kern-a KERN-A android_boot ANDROID_BOOT kernel KERNEL boot BOOT lnx LNX; do", diff --git a/app/src/main/res/layout/install_fragment.xml b/app/src/main/res/layout/install_fragment.xml index 2b6b72ff7..4b91fe2f0 100644 --- a/app/src/main/res/layout/install_fragment.xml +++ b/app/src/main/res/layout/install_fragment.xml @@ -18,114 +18,125 @@ app:cardUseCompatPadding="true"> + android:textStyle="bold" /> - + android:maxWidth="400dp" + android:minWidth="400dp"> - - - + + android:layout_height="match_parent" + android:layout_marginTop="15dp" + android:layout_marginBottom="15dp"> - - -