From 7fb1aa8fadfc6d787c79ccdf4ee3f7a67e2d2fcc Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sun, 2 Jun 2019 22:48:09 +0500 Subject: [PATCH] Fixes#1190 and #1189 --- .../kiwix/kiwixmobile/main/MainActivity.java | 69 ++++++++++--------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java index 06a7d5936..8ec14472e 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -944,27 +944,10 @@ public class MainActivity extends BaseActivity implements WebViewCallback, openExternalUrl(intentSupportKiwix); case R.id.menu_wifi_hotspot: - //Check if user's hotspot is enabled - if (isMobileDataEnabled(this)) { - - mobileDataDialog(); - - } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - toggleHotspot(); } else { - // For API<26 we use this - // Checks if wifi access point is already enabled then turns it off, otherwise enables it. - if (wifiHotspotManager.isWifiApEnabled()) { - wifiHotspotManager.setWifiEnabled(null, false); - Toast.makeText(this, "Wifi Hotspot Disabled", Toast.LENGTH_LONG) - .show(); - } else { - wifiHotspotManager.setWifiEnabled(null, true); - Toast.makeText(this, "Wifi Hotspot Enabled", Toast.LENGTH_LONG) - .show(); - } + switchHotspot(); } default: @@ -1087,23 +1070,41 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private void showToast(int stringResource, int duration) { Toast.makeText(this, stringResource, duration).show(); } - - private void mobileDataDialog() { - AlertDialog.Builder builder = new AlertDialog.Builder(this, dialogStyle()); - builder.setPositiveButton(this.getString(R.string.yes), (dialog, id) -> { - disableMobileData(); - }); - builder.setNegativeButton(android.R.string.no, (dialog, id) -> { - //Do nothing - }); - builder.setTitle(this.getString(R.string.mobile_data_enabled)); - builder.setMessage( - this.getString(R.string.mobile_data_message) + "\n" + this.getString( - R.string.mobile_data_message_confirmation) - ); - AlertDialog dialog = builder.create(); - dialog.show(); + // For API<26 we use this + // Checks if wifi access point is already enabled then turns it off, otherwise enables it. + private void switchHotspot() { + if (wifiHotspotManager.isWifiApEnabled()) { + wifiHotspotManager.setWifiEnabled(null, false); + } else { + //Check if user's hotspot is enabled + if (isMobileDataEnabled(this)) { + + mobileDataDialog(); + } else { + wifiHotspotManager.setWifiEnabled(null, true); + } + } + } + + private void mobileDataDialog() { + if (Build.VERSION.SDK_INT < VERSION_CODES.O) { + AlertDialog.Builder builder = new AlertDialog.Builder(this, dialogStyle()); + + builder.setPositiveButton(this.getString(R.string.yes), (dialog, id) -> { + disableMobileData(); + }); + builder.setNegativeButton(android.R.string.no, (dialog, id) -> { + wifiHotspotManager.setWifiEnabled(null, true); + }); + builder.setTitle(this.getString(R.string.mobile_data_enabled)); + builder.setMessage( + this.getString(R.string.mobile_data_message) + "\n" + this.getString( + R.string.mobile_data_message_confirmation) + ); + AlertDialog dialog = builder.create(); + dialog.show(); + } } @RequiresApi(api = VERSION_CODES.O)