From e6f10176c685ed236a4db01bc8d13178a23ae9a5 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 23 May 2017 16:51:23 +0800 Subject: [PATCH] Network check --- .../com/topjohnwu/magisk/MagiskFragment.java | 60 ++++++++++++------- .../com/topjohnwu/magisk/MainActivity.java | 21 ++++--- .../com/topjohnwu/magisk/SplashActivity.java | 52 ++++++++-------- .../com/topjohnwu/magisk/utils/Utils.java | 16 +++++ app/src/main/res/layout/fragment_magisk.xml | 4 +- build.gradle | 2 +- 6 files changed, 96 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java index c0bf91d7a..b364161ed 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java @@ -58,16 +58,17 @@ public class MagiskFragment extends Fragment implements CallbackEvent.Listener { + updateUI(); + magiskUpdateText.setText(R.string.checking_for_updates); - magiskCheckUpdatesProgress.setVisibility(View.VISIBLE); + magiskUpdateProgress.setVisibility(View.VISIBLE); magiskUpdateIcon.setVisibility(View.GONE); safetyNetStatusText.setText(R.string.safetyNet_check_text); magiskManager.safetyNetDone.isTriggered = false; + magiskManager.updateCheckDone.isTriggered = false; + magiskManager.remoteMagiskVersionString = null; + magiskManager.remoteMagiskVersionCode = -1; collapse(); noDialog = false; - updateVersionUI(); - new CheckUpdates(getActivity()).exec(); + // Trigger state check + if (Utils.checkNetworkStatus(magiskManager)) { + new CheckUpdates(getActivity()).exec(); + } else { + mSwipeRefreshLayout.setRefreshing(false); + } }); if (magiskManager.magiskVersionCode < 0 && Shell.rootAccess() && !noDialog) { @@ -251,14 +261,7 @@ public class MagiskFragment extends Fragment implements CallbackEvent.Listener= 130 ? UPTODATE : 0; + magiskUpdateCard.setVisibility(Utils.checkBits(status, NETWORK) ? View.VISIBLE : View.GONE); + safetyNetCard.setVisibility(Utils.checkBits(status, NETWORK) ? View.VISIBLE : View.GONE); + bootImageCard.setVisibility(Utils.checkBits(status, NETWORK, ROOT) ? View.VISIBLE : View.GONE); + installOptionCard.setVisibility(Utils.checkBits(status, NETWORK, ROOT) ? View.VISIBLE : View.GONE); + installButton.setVisibility(Utils.checkBits(status, NETWORK) ? View.VISIBLE : View.GONE); + uninstallButton.setVisibility(Utils.checkBits(status, UPTODATE, ROOT) ? View.VISIBLE : View.GONE); + updateVersionUI(); + } + private void updateVersionUI() { int image, color; @@ -356,20 +382,14 @@ public class MagiskFragment extends Fragment implements CallbackEvent.Listener= 130 ? View.VISIBLE : View.GONE); installText.setText(R.string.download_install); List items = new ArrayList<>(); diff --git a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java index 48f8d7b07..06afeb1e2 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java @@ -22,6 +22,7 @@ import android.view.View; import com.topjohnwu.magisk.components.Activity; import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.Shell; +import com.topjohnwu.magisk.utils.Utils; import butterknife.BindView; import butterknife.ButterKnife; @@ -81,7 +82,6 @@ public class MainActivity extends Activity navigationView.setNavigationItemSelectedListener(this); getApplicationContext().reloadMainActivity.register(this); - getApplicationContext().updateCheckDone.register(this); } @@ -106,16 +106,18 @@ public class MainActivity extends Activity @Override protected void onDestroy() { getApplicationContext().reloadMainActivity.unRegister(this); - getApplicationContext().updateCheckDone.unRegister(this); super.onDestroy(); } @Override public void onBackPressed() { - if (drawer.isDrawerOpen(navigationView)) + if (drawer.isDrawerOpen(navigationView)) { drawer.closeDrawer(navigationView); - else + } else if (mDrawerItem != R.id.magisk) { + navigate(R.id.magisk); + } else { finish(); + } } @Override @@ -128,21 +130,17 @@ public class MainActivity extends Activity @Override public void onTrigger(CallbackEvent event) { - if (event == getApplicationContext().reloadMainActivity) { - recreate(); - } else if (event == getApplicationContext().updateCheckDone) { - checkHideSection(); - } + recreate(); } - private void checkHideSection() { + public void checkHideSection() { Menu menu = navigationView.getMenu(); menu.findItem(R.id.magiskhide).setVisible( Shell.rootAccess() && getApplicationContext().magiskVersionCode >= 130 && prefs.getBoolean("magiskhide", false)); menu.findItem(R.id.modules).setVisible( Shell.rootAccess() && getApplicationContext().magiskVersionCode >= 0); - menu.findItem(R.id.downloads).setVisible( + menu.findItem(R.id.downloads).setVisible(Utils.checkNetworkStatus(this) && Shell.rootAccess() && getApplicationContext().magiskVersionCode >= 0); menu.findItem(R.id.log).setVisible(Shell.rootAccess()); menu.findItem(R.id.superuser).setVisible( @@ -154,6 +152,7 @@ public class MainActivity extends Activity if (item != null) { switch (item) { case "magisk": + case "install": itemId = R.id.magisk; break; case "superuser": diff --git a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java index fca15e9cc..ea7cfa695 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java @@ -6,14 +6,15 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.text.TextUtils; -import com.topjohnwu.magisk.asyncs.CheckUpdates; import com.topjohnwu.magisk.asyncs.GetBootBlocks; import com.topjohnwu.magisk.asyncs.LoadApps; import com.topjohnwu.magisk.asyncs.LoadModules; import com.topjohnwu.magisk.asyncs.LoadRepos; import com.topjohnwu.magisk.components.Activity; import com.topjohnwu.magisk.services.UpdateCheckService; +import com.topjohnwu.magisk.utils.Utils; public class SplashActivity extends Activity{ @@ -29,40 +30,39 @@ public class SplashActivity extends Activity{ // Init the info and configs and root shell magiskManager.init(); - // Initialize the update check service, notify every 3 hours - if (!"install".equals(getIntent().getStringExtra(MagiskManager.INTENT_SECTION))) { - ComponentName service = new ComponentName(magiskManager, UpdateCheckService.class); - JobInfo jobInfo = new JobInfo.Builder(UPDATE_SERVICE_ID, service) - .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) - .setPersisted(true) - .setPeriodic(3 * 60 * 60 * 1000) - .build(); - JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); - scheduler.schedule(jobInfo); - } - // Now fire all async tasks new GetBootBlocks(this).exec(); new LoadModules(this) { @Override protected void onPostExecute(Void v) { super.onPostExecute(v); - new LoadRepos(activity).exec(); + if (Utils.checkNetworkStatus(activity)) { + new LoadRepos(activity).exec(); + } } }.exec(); new LoadApps(this).exec(); - new CheckUpdates(this, false){ - @Override - protected void onPostExecute(Void v) { - super.onPostExecute(v); - String section = getIntent().getStringExtra(MagiskManager.INTENT_SECTION); - Intent intent = new Intent(magiskManager, MainActivity.class); - if (section != null) { - intent.putExtra(MagiskManager.INTENT_SECTION, section); - } - startActivity(intent); - finish(); + + if (Utils.checkNetworkStatus(this)) { + // Initialize the update check service, notify every 12 hours + if (!TextUtils.equals("install", getIntent().getStringExtra(MagiskManager.INTENT_SECTION))) { + ComponentName service = new ComponentName(magiskManager, UpdateCheckService.class); + JobInfo jobInfo = new JobInfo.Builder(UPDATE_SERVICE_ID, service) + .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) + .setPersisted(true) + .setPeriodic(12 * 60 * 60 * 1000) + .build(); + JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); + scheduler.schedule(jobInfo); } - }.exec(); + } + + Intent intent = new Intent(magiskManager, MainActivity.class); + String section = getIntent().getStringExtra(MagiskManager.INTENT_SECTION); + if (section != null) { + intent.putExtra(MagiskManager.INTENT_SECTION, section); + } + startActivity(intent); + finish(); } } 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 ee97b7beb..a3df1e937 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -8,6 +8,8 @@ import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.database.Cursor; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.net.Uri; import android.os.Environment; import android.provider.OpenableColumns; @@ -186,4 +188,18 @@ public class Utils { Snackbar.LENGTH_LONG) .setAction(R.string.ok, (v)->{}).show(); } + + public static boolean checkNetworkStatus(Context context) { + ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo networkInfo = manager.getActiveNetworkInfo(); + return networkInfo != null && networkInfo.isConnected(); + } + + public static boolean checkBits(int bits, int... masks) { + for (int mask : masks) { + if ((bits & mask) == 0) + return false; + } + return true; + } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_magisk.xml b/app/src/main/res/layout/fragment_magisk.xml index 3e963632d..9592aa948 100644 --- a/app/src/main/res/layout/fragment_magisk.xml +++ b/app/src/main/res/layout/fragment_magisk.xml @@ -21,6 +21,7 @@ android:orientation="vertical">