From 81dda0cafbf1903fbbc062754d52f8473e8fc2e0 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Wed, 13 Sep 2023 20:24:06 +0300 Subject: [PATCH] actions: handle `nullException` for wifi switch (fixes #2480) (#2482) Co-authored-by: dogi --- app/build.gradle | 4 +- .../ui/sync/DashboardElementActivity.java | 46 ++++++++++--------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index eae19b77fd..f4f55b2fc6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,8 +10,8 @@ android { applicationId "org.ole.planet.myplanet" minSdkVersion 21 targetSdkVersion 34 - versionCode 1029 - versionName "0.10.29" + versionCode 1030 + versionName "0.10.30" ndkVersion '21.3.6528147' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/sync/DashboardElementActivity.java b/app/src/main/java/org/ole/planet/myplanet/ui/sync/DashboardElementActivity.java index 61f702cb5f..0c8f8faead 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/sync/DashboardElementActivity.java +++ b/app/src/main/java/org/ole/planet/myplanet/ui/sync/DashboardElementActivity.java @@ -52,6 +52,7 @@ public abstract class DashboardElementActivity extends AppCompatActivity impleme public UserProfileDbHandler profileDbHandler; boolean doubleBackToExitPressedOnce; private SharedPreferences settings; + private MenuItem goOnline; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -86,6 +87,7 @@ public void onClickTabItems(int position) { @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_dashboard, menu); + goOnline = menu.findItem(R.id.menu_goOnline); return true; } @@ -99,7 +101,7 @@ public void openCallFragment(Fragment newfragment, String tag) { @Override public boolean onPrepareOptionsMenu(Menu menu) { - menu.findItem(R.id.menu_goOnline).setVisible(Constants.showBetaFeature(Constants.KEY_SYNC, this)); + goOnline.setVisible(Constants.showBetaFeature(Constants.KEY_SYNC, this)); return super.onPrepareOptionsMenu(menu); } @@ -108,6 +110,7 @@ public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.menu_goOnline) { wifiStatusSwitch(); + return true; } else if (id == R.id.menu_logout) { logout(); } else if (id == R.id.action_feedback) { @@ -129,26 +132,27 @@ protected void syncNow() { @SuppressLint("RestrictedApi") public void wifiStatusSwitch() { - ActionMenuItemView goOnline = findViewById(R.id.menu_goOnline); - Drawable resIcon = getResources().getDrawable(R.drawable.goonline); - ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); - WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); - NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); - - Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS); - startActivity(intent); - - if (mWifi.isConnected()) { - wifi.setWifiEnabled(false); - resIcon.mutate().setColorFilter(getApplicationContext().getResources().getColor(R.color.green), PorterDuff.Mode.SRC_ATOP); - goOnline.setIcon(resIcon); - Toast.makeText(this, getString(R.string.wifi_is_turned_off_saving_battery_power), Toast.LENGTH_LONG).show(); - } else { - wifi.setWifiEnabled(true); - Toast.makeText(this, getString(R.string.turning_on_wifi_please_wait), Toast.LENGTH_LONG).show(); - (new Handler()).postDelayed(this::connectToWifi, 5000); - resIcon.mutate().setColorFilter(getApplicationContext().getResources().getColor(R.color.accent), PorterDuff.Mode.SRC_ATOP); - goOnline.setIcon(resIcon); + if (goOnline != null) { + Drawable resIcon = getResources().getDrawable(R.drawable.goonline); + ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); + NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); + + Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS); + startActivity(intent); + + if (mWifi.isConnected()) { + wifi.setWifiEnabled(false); + resIcon.mutate().setColorFilter(getApplicationContext().getResources().getColor(R.color.green), PorterDuff.Mode.SRC_ATOP); + goOnline.setIcon(resIcon); + Toast.makeText(this, getString(R.string.wifi_is_turned_off_saving_battery_power), Toast.LENGTH_LONG).show(); + } else { + wifi.setWifiEnabled(true); + Toast.makeText(this, getString(R.string.turning_on_wifi_please_wait), Toast.LENGTH_LONG).show(); + (new Handler()).postDelayed(this::connectToWifi, 5000); + resIcon.mutate().setColorFilter(getApplicationContext().getResources().getColor(R.color.accent), PorterDuff.Mode.SRC_ATOP); + goOnline.setIcon(resIcon); + } } }