Skip to content

Commit

Permalink
actions: handle nullException for wifi switch (fixes #2480) (#2482)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Sep 13, 2023
1 parent f5e0c19 commit 81dda0c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}

Expand All @@ -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) {
Expand All @@ -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);
}
}
}

Expand Down

0 comments on commit 81dda0c

Please sign in to comment.