From f11bde631389bf20aefa8ca6d51f90eb324c19b6 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Tue, 28 May 2019 16:41:32 +0500 Subject: [PATCH 001/266] Added permissions to work with wifi-hotspot Wifi related permissions. Write system settings permission. --- app/src/main/AndroidManifest.xml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 29b052325..79dd2877c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,12 +1,36 @@ +<<<<<<< HEAD +||||||| merged common ancestors + + + + + + + + + Date: Tue, 28 May 2019 16:50:25 +0500 Subject: [PATCH 002/266] Add wifi hotspot manager class This classes manages all the wifi-hotspot logic. It uses WifiManager and WifiConfiguration classes by Android. Add method to check show write permission settings. Add method setWifiEnabled to setup wifi hotspot Add setters/getters for WifiConfiguration --- .../wifi_hotspot/WifiHotspotManager.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java new file mode 100644 index 000000000..9a69464f0 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -0,0 +1,68 @@ +package org.kiwix.kiwixmobile.wifi_hotspot; + +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiManager; +import android.os.Build; +import android.provider.Settings; +import android.util.Log; +import java.lang.reflect.Method; + +public class WifiHotspotManager { + private final WifiManager wifiManager; + private Context context; + + public WifiHotspotManager(Context context) { + this.context = context; + wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE); + } + + public void showWritePermissionSettings(boolean force) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (force || !Settings.System.canWrite(this.context)) { + Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS); + intent.setData(Uri.parse("package:" + this.context.getPackageName())); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + this.context.startActivity(intent); + } + } + } + + public boolean setWifiEnabled(WifiConfiguration wifiConfig, boolean enabled) { + try { + if (enabled) { + wifiManager.setWifiEnabled(false); + } + + Method method = wifiManager.getClass() + .getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); + return (Boolean) method.invoke(wifiManager, wifiConfig, enabled); + } catch (Exception e) { + Log.e(this.getClass().toString(), "", e); + return false; + } + } + + public WifiConfiguration getWifiApConfiguration() { + try { + Method method = wifiManager.getClass().getMethod("getWifiApConfiguration"); + return (WifiConfiguration) method.invoke(wifiManager); + } catch (Exception e) { + Log.e(this.getClass().toString(), "", e); + return null; + } + } + + public boolean setWifiApConfiguration(WifiConfiguration wifiConfig) { + try { + Method method = + wifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class); + return (Boolean) method.invoke(wifiManager, wifiConfig); + } catch (Exception e) { + Log.e(this.getClass().toString(), "", e); + return false; + } + } +} From 8eff3c5d84f6f640162d6c05c2250977c6cae5a4 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Tue, 28 May 2019 16:56:13 +0500 Subject: [PATCH 003/266] Add WIFI_AP_STATE_ENUMS Declare enums to check the WifiAp state. --- .../kiwix/kiwixmobile/wifi_hotspot/WIFI_AP_STATE_ENUMS.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WIFI_AP_STATE_ENUMS.java diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WIFI_AP_STATE_ENUMS.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WIFI_AP_STATE_ENUMS.java new file mode 100644 index 000000000..8442647ba --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WIFI_AP_STATE_ENUMS.java @@ -0,0 +1,5 @@ +package org.kiwix.kiwixmobile.wifi_hotspot; + +public enum WIFI_AP_STATE_ENUMS { + WIFI_AP_STATE_DISABLING, WIFI_AP_STATE_DISABLED, WIFI_AP_STATE_ENABLING, WIFI_AP_STATE_ENABLED, WIFI_AP_STATE_FAILED +} From bd328b5d1d546af8a6ec06f141831c8546239fd2 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Tue, 28 May 2019 16:59:34 +0500 Subject: [PATCH 004/266] Add method getWifiApState() and isWifiApEnabled() It's used to get current state of wifi access point. --- .../wifi_hotspot/WifiHotspotManager.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index 9a69464f0..3a1b67855 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -45,6 +45,28 @@ public class WifiHotspotManager { } } + public WIFI_AP_STATE_ENUMS getWifiApState() { + try { + Method method = wifiManager.getClass().getMethod("getWifiApState"); + + int tmp = ((Integer) method.invoke(wifiManager)); + + // Fix for Android 4 + if (tmp >= 10) { + tmp = tmp - 10; + } + + return WIFI_AP_STATE_ENUMS.class.getEnumConstants()[tmp]; + } catch (Exception e) { + Log.e(this.getClass().toString(), "", e); + return WIFI_AP_STATE_ENUMS.WIFI_AP_STATE_FAILED; + } + } + + public boolean isWifiApEnabled() { + return getWifiApState() == WIFI_AP_STATE_ENUMS.WIFI_AP_STATE_ENABLED; + } + public WifiConfiguration getWifiApConfiguration() { try { Method method = wifiManager.getClass().getMethod("getWifiApConfiguration"); From d4b0ea1dd3a9604abc7df386444379f0e2487655 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Tue, 28 May 2019 17:55:35 +0500 Subject: [PATCH 005/266] Add menu item to turn on/off Wifi Hotspot Initialize WifiManager object in MainActivity. And work with WifiHotspotManager class. --- .../org/kiwix/kiwixmobile/main/MainActivity.java | 12 ++++++++++++ app/src/main/res/menu/menu_main.xml | 5 +++++ app/src/main/res/values/strings.xml | 1 + 3 files changed, 18 insertions(+) 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 41bb39f86..a39efd3c4 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -110,6 +110,7 @@ import org.kiwix.kiwixmobile.utils.LanguageUtils; import org.kiwix.kiwixmobile.utils.NetworkUtils; import org.kiwix.kiwixmobile.utils.StyleUtils; import org.kiwix.kiwixmobile.utils.files.FileUtils; +import org.kiwix.kiwixmobile.wifi_hotspot.WifiHotspotManager; import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity; import org.kiwix.kiwixmobile.zim_manager.fileselect_view.StorageObserver; import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BookOnDiskDelegate; @@ -163,6 +164,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, public static boolean refresh; public static boolean wifiOnly; public static boolean nightMode; + private WifiHotspotManager wifiHotspotManager; private static Uri KIWIX_LOCAL_MARKET_URI; private static Uri KIWIX_BROWSER_MARKET_URI; private final ArrayList bookmarks = new ArrayList<>(); @@ -383,6 +385,9 @@ public class MainActivity extends BaseActivity implements WebViewCallback, tabRecyclerView.setAdapter(tabsAdapter); new ItemTouchHelper(tabCallback).attachToRecyclerView(tabRecyclerView); drawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); + + wifiHotspotManager = new WifiHotspotManager(this); + wifiHotspotManager.showWritePermissionSettings(true); } //End of onCreate @@ -920,6 +925,13 @@ public class MainActivity extends BaseActivity implements WebViewCallback, intentSupportKiwix.putExtra(EXTRA_EXTERNAL_LINK, true); openExternalUrl(intentSupportKiwix); + case R.id.menu_wifi_hotspot: + if (wifiHotspotManager.isWifiApEnabled()) { + wifiHotspotManager.setWifiEnabled(null, false); + } else { + wifiHotspotManager.setWifiEnabled(null, true); + } + default: break; } diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index cad504d35..b0eb1174d 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -94,4 +94,9 @@ android:title="@string/menu_support_kiwix" app:showAsAction="never"/> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c0fd5a58d..6f5ecedff 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -14,6 +14,7 @@ Read aloud Stop reading aloud Support Kiwix + Wifi Hotspot Save Media An error occurred when trying to save the media! Saved media as %s to Android/media/org.kiwix…/ From dc425c21998fd8db36cbd2ceeda5d6161037cd63 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Tue, 28 May 2019 18:28:19 +0500 Subject: [PATCH 006/266] Fix write settings permission bug Write settings permission popping up on every restart. Using Toast to display Wifi Hotspot Enabled/Disabled --- .../org/kiwix/kiwixmobile/main/MainActivity.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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 a39efd3c4..3860b3aa2 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -160,6 +160,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private static final String NEW_TAB = "NEW_TAB"; private static final String HOME_URL = "file:///android_asset/home.html"; + private final String requiredPermission = "android.permission.WRITE_SETTINGS"; public static boolean isFullscreenOpened; public static boolean refresh; public static boolean wifiOnly; @@ -387,7 +388,10 @@ public class MainActivity extends BaseActivity implements WebViewCallback, drawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); wifiHotspotManager = new WifiHotspotManager(this); - wifiHotspotManager.showWritePermissionSettings(true); + int checkVal = getApplicationContext().checkCallingOrSelfPermission(requiredPermission); + if (checkVal == PackageManager.PERMISSION_DENIED) { + wifiHotspotManager.showWritePermissionSettings(true); + } } //End of onCreate @@ -928,8 +932,12 @@ public class MainActivity extends BaseActivity implements WebViewCallback, case R.id.menu_wifi_hotspot: 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(); } default: @@ -1464,6 +1472,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback, } } updateWidgets(this); + + wifiHotspotManager.showWritePermissionSettings(false); } private void updateBottomToolbarVisibility() { From c8e975f8edfe34ae4e3eef3434d4d648dc592a1e Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Wed, 29 May 2019 19:17:54 +0500 Subject: [PATCH 007/266] Add documentation to the code. --- .../wifi_hotspot/WIFI_AP_STATE_ENUMS.java | 5 +++++ .../wifi_hotspot/WifiHotspotManager.java | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WIFI_AP_STATE_ENUMS.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WIFI_AP_STATE_ENUMS.java index 8442647ba..c09be1e12 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WIFI_AP_STATE_ENUMS.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WIFI_AP_STATE_ENUMS.java @@ -1,5 +1,10 @@ package org.kiwix.kiwixmobile.wifi_hotspot; +/** + * WIFI_AP_STATE_ENUMS class is used to get current state of the wifi access point using enums. + * Created by Adeel Zafar on 28/5/2019. + */ + public enum WIFI_AP_STATE_ENUMS { WIFI_AP_STATE_DISABLING, WIFI_AP_STATE_DISABLED, WIFI_AP_STATE_ENABLING, WIFI_AP_STATE_ENABLED, WIFI_AP_STATE_FAILED } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index 3a1b67855..196521723 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -10,6 +10,12 @@ import android.provider.Settings; import android.util.Log; import java.lang.reflect.Method; +/** + * WifiHotstopManager class makes use of the Android's WifiManager and WifiConfiguration class + * to implement the wifi hotspot feature. + * Created by Adeel Zafar on 28/5/2019. + */ + public class WifiHotspotManager { private final WifiManager wifiManager; private Context context; @@ -19,6 +25,7 @@ public class WifiHotspotManager { wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE); } + //To get write permission settings, we use this method. public void showWritePermissionSettings(boolean force) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (force || !Settings.System.canWrite(this.context)) { @@ -32,7 +39,7 @@ public class WifiHotspotManager { public boolean setWifiEnabled(WifiConfiguration wifiConfig, boolean enabled) { try { - if (enabled) { + if (enabled) { //disables wifi hotspot if it's already enabled wifiManager.setWifiEnabled(false); } @@ -45,6 +52,7 @@ public class WifiHotspotManager { } } + // This method returns the current state of the Wifi access point public WIFI_AP_STATE_ENUMS getWifiApState() { try { Method method = wifiManager.getClass().getMethod("getWifiApState"); @@ -63,10 +71,12 @@ public class WifiHotspotManager { } } + //This method returns if wifi access point is enabled or not public boolean isWifiApEnabled() { return getWifiApState() == WIFI_AP_STATE_ENUMS.WIFI_AP_STATE_ENABLED; } + //This method is to get the wifi ap configuration public WifiConfiguration getWifiApConfiguration() { try { Method method = wifiManager.getClass().getMethod("getWifiApConfiguration"); @@ -77,6 +87,7 @@ public class WifiHotspotManager { } } + //This method is to set the wifi ap configuration public boolean setWifiApConfiguration(WifiConfiguration wifiConfig) { try { Method method = From d04839f6ef0a8bb9be31e995f2f453d89f025fec Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Wed, 29 May 2019 20:52:09 +0500 Subject: [PATCH 008/266] Workaround for Android versions >=Oreo Worked with WifiManager's startLocalOnlyHotspotCallback It is specifically for android versions>=O --- app/src/main/AndroidManifest.xml | 4 ++ .../kiwix/kiwixmobile/main/MainActivity.java | 18 +++++---- .../wifi_hotspot/WifiHotspotManager.java | 38 ++++++++++++++++++- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 79dd2877c..8d7ccba71 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -32,6 +32,10 @@ + + + + = Build.VERSION_CODES.O) { + wifiHotspotManager.turnOnHotspot(); } else { - wifiHotspotManager.setWifiEnabled(null, true); - Toast.makeText(this, "Wifi Hotspot Enabled", Toast.LENGTH_LONG) - .show(); + 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(); + } } default: diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index 196521723..44129f074 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -6,8 +6,10 @@ import android.net.Uri; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.os.Build; +import android.os.Handler; import android.provider.Settings; import android.util.Log; +import androidx.annotation.RequiresApi; import java.lang.reflect.Method; /** @@ -17,8 +19,9 @@ import java.lang.reflect.Method; */ public class WifiHotspotManager { - private final WifiManager wifiManager; + private WifiManager wifiManager; private Context context; + private WifiManager.LocalOnlyHotspotReservation hotspotReservation; public WifiHotspotManager(Context context) { this.context = context; @@ -52,6 +55,39 @@ public class WifiHotspotManager { } } + //Workaround to turn on hotspot for Oreo versions + @RequiresApi(api = Build.VERSION_CODES.O) + public void turnOnHotspot() { + wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() { + + @Override + public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) { + super.onStarted(reservation); + //hotspotReservation = reservation; + } + + @Override + public void onStopped() { + super.onStopped(); + Log.v("DANG", "Local Hotspot Stopped"); + } + + @Override + public void onFailed(int reason) { + super.onFailed(reason); + Log.v("DANG", "Local Hotspot failed to start"); + } + }, new Handler()); + } + + //Workaround to turn off hotspot for Oreo versions + @RequiresApi(api = Build.VERSION_CODES.O) + public void turnOffHotspot() { + if (hotspotReservation != null) { + hotspotReservation.close(); + } + } + // This method returns the current state of the Wifi access point public WIFI_AP_STATE_ENUMS getWifiApState() { try { From 8349f6d380a1a54576b34c08bc5b95f6324413f2 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 31 May 2019 17:18:09 +0500 Subject: [PATCH 009/266] Add location permissions --- .../java/org/kiwix/kiwixmobile/main/MainActivity.java | 10 ++++++++++ .../kiwixmobile/wifi_hotspot/WifiHotspotManager.java | 3 +++ 2 files changed, 13 insertions(+) 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 3c4687df8..09f2ecf25 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -241,6 +241,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private CompatFindActionModeCallback compatCallback; private TabsAdapter tabsAdapter; private int currentWebViewIndex = 0; + private final int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 102; private File file; private ActionMode actionMode = null; private KiwixWebView tempForUndo; @@ -931,7 +932,16 @@ public class MainActivity extends BaseActivity implements WebViewCallback, case R.id.menu_wifi_hotspot: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) + == PackageManager.PERMISSION_GRANTED) { wifiHotspotManager.turnOnHotspot(); + } else { + //Show rationale and request permission. + //No explanation needed; request the permission + ActivityCompat.requestPermissions(this, + new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, + MY_PERMISSIONS_ACCESS_FINE_LOCATION); + } } else { if (wifiHotspotManager.isWifiApEnabled()) { wifiHotspotManager.setWifiEnabled(null, false); diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index 44129f074..b2e211433 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -22,6 +22,7 @@ public class WifiHotspotManager { private WifiManager wifiManager; private Context context; private WifiManager.LocalOnlyHotspotReservation hotspotReservation; + private boolean oreoenabled = false; public WifiHotspotManager(Context context) { this.context = context; @@ -64,6 +65,7 @@ public class WifiHotspotManager { public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) { super.onStarted(reservation); //hotspotReservation = reservation; + oreoenabled = true; } @Override @@ -85,6 +87,7 @@ public class WifiHotspotManager { public void turnOffHotspot() { if (hotspotReservation != null) { hotspotReservation.close(); + oreoenabled = false; } } From 950ca069d5fbd37e4ddce860b1082a93f8fc594a Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sat, 1 Jun 2019 01:54:25 +0500 Subject: [PATCH 010/266] Fixes#1187 Wifi-hotspot not working with devices>=Oreo fixed. Created a location request added onCompletionListener for LocationSettingsResponse Checked LocationSettingsStates in onActivityResult() Added gms location dependency. --- app/build.gradle | 15 +++ app/src/main/AndroidManifest.xml | 2 - .../kiwix/kiwixmobile/main/MainActivity.java | 105 ++++++++++++++++-- .../wifi_hotspot/WifiHotspotManager.java | 50 ++++++--- 4 files changed, 142 insertions(+), 30 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 61206e82d..942cff8d0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -137,6 +137,21 @@ dependencies { testImplementation "org.assertj:assertj-core:3.11.1" testImplementation 'com.jraska.livedata:testing-ktx:1.1.0' testImplementation 'androidx.arch.core:core-testing:2.0.1' + // Leak canary + implementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' + implementation 'com.google.android.gms:play-services-location:16.0.0' + // Only enable leak canary in debug builds + configurations.all { config -> + if (config.name.contains('debug') || config.name.contains("Debug")) { + config.resolutionStrategy.eachDependency { details -> + if (details.requested.group == 'com.squareup.leakcanary' && + details.requested.name == 'leakcanary-android-no-op') { + details.useTarget(group: details.requested.group, name: 'leakcanary-android', + version: details.requested.version) + } + } + } + } } // Set custom app import directory diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8d7ccba71..2ea8e5341 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,8 +4,6 @@ android:installLocation="auto" package="org.kiwix.kiwixmobile"> -<<<<<<< HEAD -||||||| merged common ancestors task; private File file; private ActionMode actionMode = null; private KiwixWebView tempForUndo; @@ -932,16 +944,17 @@ public class MainActivity extends BaseActivity implements WebViewCallback, case R.id.menu_wifi_hotspot: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) - == PackageManager.PERMISSION_GRANTED) { - wifiHotspotManager.turnOnHotspot(); - } else { - //Show rationale and request permission. - //No explanation needed; request the permission - ActivityCompat.requestPermissions(this, - new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, - MY_PERMISSIONS_ACCESS_FINE_LOCATION); - } + //if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) + // == PackageManager.PERMISSION_GRANTED) { + // wifiHotspotManager.turnOnHotspot(); + //} else { + // //Show rationale and request permission. + // //No explanation needed; request the permission + // ActivityCompat.requestPermissions(this, + // new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, + // MY_PERMISSIONS_ACCESS_FINE_LOCATION); + //} + setupLocationServices(); } else { if (wifiHotspotManager.isWifiApEnabled()) { wifiHotspotManager.setWifiEnabled(null, false); @@ -2167,4 +2180,76 @@ public class MainActivity extends BaseActivity implements WebViewCallback, public boolean checkNull(View view) { return view != null; } + + private void setupLocationServices() { + LocationRequest mLocationRequest = new LocationRequest(); + mLocationRequest.setInterval(10); + mLocationRequest.setSmallestDisplacement(10); + mLocationRequest.setFastestInterval(10); + mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); + LocationSettingsRequest.Builder builder = new + LocationSettingsRequest.Builder(); + builder.addLocationRequest(mLocationRequest); + + task = LocationServices.getSettingsClient(this).checkLocationSettings(builder.build()); + + task.addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(Task task) { + try { + LocationSettingsResponse response = task.getResult(ApiException.class); + // All location settings are satisfied. The client can initialize location + // requests here. + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + wifiHotspotManager.turnOnHotspot(); + } + } catch (ApiException exception) { + switch (exception.getStatusCode()) { + case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: + // Location settings are not satisfied. But could be fixed by showing the + // user a dialog. + try { + // Cast to a resolvable exception. + ResolvableApiException resolvable = (ResolvableApiException) exception; + // Show the dialog by calling startResolutionForResult(), + // and check the result in onActivityResult(). + resolvable.startResolutionForResult( + MainActivity.this, + 101); + } catch (IntentSender.SendIntentException e) { + // Ignore the error. + } catch (ClassCastException e) { + // Ignore, should be an impossible error. + } + break; + case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: + // Location settings are not satisfied. However, we have no way to fix the + // settings so we won't show the dialog. + break; + } + } + } + }); + } + + //@Override + //protected void onActivityResult(int requestCode, int resultCode, Intent data) { + // final LocationSettingsStates states = LocationSettingsStates.fromIntent(data); + // switch (requestCode) { + // case 101: + // switch (resultCode) { + // case Activity.RESULT_OK: + // // All required changes were successfully made + // Toast.makeText(MainActivity.this,states.isLocationPresent()+"",Toast.LENGTH_SHORT).show(); + // break; + // case Activity.RESULT_CANCELED: + // // The user was asked to change settings, but chose not to + // Toast.makeText(MainActivity.this,"Canceled",Toast.LENGTH_SHORT).show(); + // break; + // default: + // break; + // } + // break; + // } + //} } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index b2e211433..1f4d1685b 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -9,6 +9,7 @@ import android.os.Build; import android.os.Handler; import android.provider.Settings; import android.util.Log; +import android.widget.Toast; import androidx.annotation.RequiresApi; import java.lang.reflect.Method; @@ -23,6 +24,7 @@ public class WifiHotspotManager { private Context context; private WifiManager.LocalOnlyHotspotReservation hotspotReservation; private boolean oreoenabled = false; + private WifiConfiguration currentConfig; public WifiHotspotManager(Context context) { this.context = context; @@ -59,27 +61,39 @@ public class WifiHotspotManager { //Workaround to turn on hotspot for Oreo versions @RequiresApi(api = Build.VERSION_CODES.O) public void turnOnHotspot() { - wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() { + if (!oreoenabled) { + wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() { - @Override - public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) { - super.onStarted(reservation); - //hotspotReservation = reservation; - oreoenabled = true; - } + @Override + public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) { + super.onStarted(reservation); + //hotspotReservation = reservation; + hotspotReservation = reservation; + currentConfig = reservation.getWifiConfiguration(); + Toast.makeText(context, "THE PASSWORD IS: " + + currentConfig.preSharedKey + + " \n SSID is : " + + currentConfig.SSID, Toast.LENGTH_LONG).show(); + Log.v("DANG", "THE PASSWORD IS: " + + currentConfig.preSharedKey + + " \n SSID is : " + + currentConfig.SSID); + oreoenabled = true; + } - @Override - public void onStopped() { - super.onStopped(); - Log.v("DANG", "Local Hotspot Stopped"); - } + @Override + public void onStopped() { + super.onStopped(); + Log.v("DANG", "Local Hotspot Stopped"); + } - @Override - public void onFailed(int reason) { - super.onFailed(reason); - Log.v("DANG", "Local Hotspot failed to start"); - } - }, new Handler()); + @Override + public void onFailed(int reason) { + super.onFailed(reason); + Log.v("DANG", "Local Hotspot failed to start"); + } + }, new Handler()); + } } //Workaround to turn off hotspot for Oreo versions From 38af60109a20c75b086aadb68723e6dea60477f6 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sat, 1 Jun 2019 02:43:12 +0500 Subject: [PATCH 011/266] Fixes#1186 Allow user to disable data before turning on hotspot Added a method isMobileDataEnabled() that uses ConnectivityManager And checks if data is enabled or not. If data is enabled, then it takes the user to enableDisableMobileData() It shows the user Data Usage Summary settings screen. --- .../kiwix/kiwixmobile/main/MainActivity.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) 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 70046f426..3e5530bf7 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -35,6 +35,7 @@ import android.content.res.Configuration; import android.graphics.Canvas; import android.graphics.Color; import android.media.AudioManager; +import android.net.ConnectivityManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; @@ -97,7 +98,9 @@ import com.google.android.material.navigation.NavigationView; import com.google.android.material.snackbar.Snackbar; import io.reactivex.android.schedulers.AndroidSchedulers; import java.io.File; + import java.text.SimpleDateFormat; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -943,6 +946,10 @@ public class MainActivity extends BaseActivity implements WebViewCallback, openExternalUrl(intentSupportKiwix); case R.id.menu_wifi_hotspot: + if (isMobileDataEnabled(this)) { + Toast.makeText(this, "You need to disable mobile data ", Toast.LENGTH_LONG).show(); + enableDisableMobileData(); + } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) // == PackageManager.PERMISSION_GRANTED) { @@ -1097,6 +1104,28 @@ public class MainActivity extends BaseActivity implements WebViewCallback, return true; } + public static boolean isMobileDataEnabled(Context context) { + boolean enabled = false; + ConnectivityManager cm = + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + try { + Class cmClass = Class.forName(cm.getClass().getName()); + Method method = cmClass.getDeclaredMethod("getMobileDataEnabled"); + method.setAccessible(true); + enabled = (Boolean) method.invoke(cm); + } catch (Exception e) { + Log.e("DANG ", e.toString()); + } + return enabled; + } + + public void enableDisableMobileData() { + Intent intent = new Intent(); + intent.setComponent(new ComponentName("com.android.settings", + "com.android.settings.Settings$DataUsageSummaryActivity")); + startActivity(intent); + } + private void openFullScreen() { toolbarContainer.setVisibility(View.GONE); bottomToolbar.setVisibility(View.GONE); From 7807927562cbae15a3b153e080cfa2466224cc2a Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sat, 1 Jun 2019 17:16:06 +0500 Subject: [PATCH 012/266] Fixed write permission force settings bug Removed the force variable that forced permission. Permission will only appear if it is not allowed. --- .../kiwix/kiwixmobile/main/MainActivity.java | 27 +++++++++---------- .../wifi_hotspot/WifiHotspotManager.java | 5 ++-- 2 files changed, 15 insertions(+), 17 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 3e5530bf7..700af0531 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -174,7 +174,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private static final String NEW_TAB = "NEW_TAB"; private static final String HOME_URL = "file:///android_asset/home.html"; - private final String requiredPermission = "android.permission.WRITE_SETTINGS"; public static boolean isFullscreenOpened; public static boolean refresh; public static boolean wifiOnly; @@ -404,10 +403,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback, drawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); wifiHotspotManager = new WifiHotspotManager(this); - int checkVal = getApplicationContext().checkCallingOrSelfPermission(requiredPermission); - if (checkVal == PackageManager.PERMISSION_DENIED) { - wifiHotspotManager.showWritePermissionSettings(true); - } + wifiHotspotManager.showWritePermissionSettings(); + } //End of onCreate @@ -952,15 +949,15 @@ public class MainActivity extends BaseActivity implements WebViewCallback, } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) - // == PackageManager.PERMISSION_GRANTED) { - // wifiHotspotManager.turnOnHotspot(); - //} else { - // //Show rationale and request permission. - // //No explanation needed; request the permission - // ActivityCompat.requestPermissions(this, - // new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, - // MY_PERMISSIONS_ACCESS_FINE_LOCATION); - //} + // // == PackageManager.PERMISSION_GRANTED) { + // // wifiHotspotManager.turnOnHotspot(); + // //} else { + // // //Show rationale and request permission. + // // //No explanation needed; request the permission + // // ActivityCompat.requestPermissions(this, + // // new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, + // // MY_PERMISSIONS_ACCESS_FINE_LOCATION); + // //} setupLocationServices(); } else { if (wifiHotspotManager.isWifiApEnabled()) { @@ -1529,7 +1526,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, } updateWidgets(this); - wifiHotspotManager.showWritePermissionSettings(false); + wifiHotspotManager.showWritePermissionSettings(); } private void updateBottomToolbarVisibility() { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index 1f4d1685b..cfc257796 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -32,9 +32,10 @@ public class WifiHotspotManager { } //To get write permission settings, we use this method. - public void showWritePermissionSettings(boolean force) { + public void showWritePermissionSettings() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (force || !Settings.System.canWrite(this.context)) { + if (!Settings.System.canWrite(this.context)) { + Log.v("DANG", " " + !Settings.System.canWrite(this.context)); Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS); intent.setData(Uri.parse("package:" + this.context.getPackageName())); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); From 5f5f6709fb05efff3e9aa6c075984e76ff82d64e Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sat, 1 Jun 2019 19:13:47 +0500 Subject: [PATCH 013/266] Integrate turn off hotspot feature with UI for devices>=O Added method checkHotspotState() It checks the state of the hostpot for devices>=Oreo --- .../kiwix/kiwixmobile/main/MainActivity.java | 36 +++++++++++++------ .../wifi_hotspot/WifiHotspotManager.java | 10 ++++++ 2 files changed, 35 insertions(+), 11 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 700af0531..902de8478 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -948,17 +948,23 @@ public class MainActivity extends BaseActivity implements WebViewCallback, enableDisableMobileData(); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - //if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) - // // == PackageManager.PERMISSION_GRANTED) { - // // wifiHotspotManager.turnOnHotspot(); - // //} else { - // // //Show rationale and request permission. - // // //No explanation needed; request the permission - // // ActivityCompat.requestPermissions(this, - // // new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, - // // MY_PERMISSIONS_ACCESS_FINE_LOCATION); - // //} - setupLocationServices(); + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) + == PackageManager.PERMISSION_GRANTED) { + if (wifiHotspotManager.checkHotspotState()) //If hotspot is already enabled, turn it off + { + wifiHotspotManager.turnOffHotspot(); + } else //If hotspot is not already enabled, then turn it on. + { + setupLocationServices(); + } + } else { + //Show rationale and request permission. + //No explanation needed; request the permission + ActivityCompat.requestPermissions(this, + new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, + MY_PERMISSIONS_ACCESS_FINE_LOCATION); + } + } else { if (wifiHotspotManager.isWifiApEnabled()) { wifiHotspotManager.setWifiEnabled(null, false); @@ -2219,6 +2225,10 @@ public class MainActivity extends BaseActivity implements WebViewCallback, task = LocationServices.getSettingsClient(this).checkLocationSettings(builder.build()); + locationSettingsResponseBuilder(); + } + + private void locationSettingsResponseBuilder() { task.addOnCompleteListener(new OnCompleteListener() { @Override public void onComplete(Task task) { @@ -2226,6 +2236,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, LocationSettingsResponse response = task.getResult(ApiException.class); // All location settings are satisfied. The client can initialize location // requests here. + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { wifiHotspotManager.turnOnHotspot(); } @@ -2267,6 +2278,9 @@ public class MainActivity extends BaseActivity implements WebViewCallback, // case Activity.RESULT_OK: // // All required changes were successfully made // Toast.makeText(MainActivity.this,states.isLocationPresent()+"",Toast.LENGTH_SHORT).show(); + // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // wifiHotspotManager.turnOnHotspot(); + // } // break; // case Activity.RESULT_CANCELED: // // The user was asked to change settings, but chose not to diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index cfc257796..ba7a35583 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -102,10 +102,20 @@ public class WifiHotspotManager { public void turnOffHotspot() { if (hotspotReservation != null) { hotspotReservation.close(); + hotspotReservation = null; oreoenabled = false; } } + //This method checks the state of the hostpot for devices>=Oreo + public boolean checkHotspotState() { + if (hotspotReservation != null) { + return true; + } else { + return false; + } + } + // This method returns the current state of the Wifi access point public WIFI_AP_STATE_ENUMS getWifiApState() { try { From 145b177616f309e8bd43cb1fac0fd549930f3ee7 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sat, 1 Jun 2019 19:23:56 +0500 Subject: [PATCH 014/266] Add case in onActivityResult for LocationSettings Checking the result of LocationSettings resolution in onActivityResult --- .../kiwix/kiwixmobile/main/MainActivity.java | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 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 902de8478..e97af2c1b 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -1744,6 +1744,27 @@ public class MainActivity extends BaseActivity implements WebViewCallback, } } return; + + //Checking the result code for LocationSettings resolution + case 101: + final LocationSettingsStates states = LocationSettingsStates.fromIntent(data); + switch (resultCode) { + case Activity.RESULT_OK: + // All required changes were successfully made + Toast.makeText(MainActivity.this, states.isLocationPresent() + "", Toast.LENGTH_SHORT) + .show(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + wifiHotspotManager.turnOnHotspot(); + } + break; + case Activity.RESULT_CANCELED: + // The user was asked to change settings, but chose not to + Toast.makeText(MainActivity.this, "Canceled", Toast.LENGTH_SHORT).show(); + break; + default: + break; + } + break; default: break; } @@ -2268,28 +2289,4 @@ public class MainActivity extends BaseActivity implements WebViewCallback, } }); } - - //@Override - //protected void onActivityResult(int requestCode, int resultCode, Intent data) { - // final LocationSettingsStates states = LocationSettingsStates.fromIntent(data); - // switch (requestCode) { - // case 101: - // switch (resultCode) { - // case Activity.RESULT_OK: - // // All required changes were successfully made - // Toast.makeText(MainActivity.this,states.isLocationPresent()+"",Toast.LENGTH_SHORT).show(); - // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - // wifiHotspotManager.turnOnHotspot(); - // } - // break; - // case Activity.RESULT_CANCELED: - // // The user was asked to change settings, but chose not to - // Toast.makeText(MainActivity.this,"Canceled",Toast.LENGTH_SHORT).show(); - // break; - // default: - // break; - // } - // break; - // } - //} } From acb53fa5aadc6ad9d70bf64a543c9e8078f63a96 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sat, 1 Jun 2019 19:35:54 +0500 Subject: [PATCH 015/266] Add documentation and clean code. Add comments to mobile data methods, menu item wifi_hospot --- .../org/kiwix/kiwixmobile/main/MainActivity.java | 12 +++++++++--- .../kiwixmobile/wifi_hotspot/WifiHotspotManager.java | 3 ++- 2 files changed, 11 insertions(+), 4 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 e97af2c1b..5a48eeaa4 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -943,11 +943,13 @@ 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)) { Toast.makeText(this, "You need to disable mobile data ", Toast.LENGTH_LONG).show(); - enableDisableMobileData(); + disableMobileData(); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + //Check if location permissions are granted if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { if (wifiHotspotManager.checkHotspotState()) //If hotspot is already enabled, turn it off @@ -958,7 +960,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, setupLocationServices(); } } else { - //Show rationale and request permission. + //Show rationale and request location permission. //No explanation needed; request the permission ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, @@ -966,6 +968,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback, } } 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) @@ -1107,6 +1111,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, return true; } + // This method checks if mobile data is enabled in user's device. public static boolean isMobileDataEnabled(Context context) { boolean enabled = false; ConnectivityManager cm = @@ -1122,7 +1127,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback, return enabled; } - public void enableDisableMobileData() { + //This method sends the user to data usage summary settings activity + public void disableMobileData() { Intent intent = new Intent(); intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity")); diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index ba7a35583..afc52ce6a 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -44,6 +44,8 @@ public class WifiHotspotManager { } } + // This method enables/disables the wifi access point + // It is used for API<26 public boolean setWifiEnabled(WifiConfiguration wifiConfig, boolean enabled) { try { if (enabled) { //disables wifi hotspot if it's already enabled @@ -68,7 +70,6 @@ public class WifiHotspotManager { @Override public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) { super.onStarted(reservation); - //hotspotReservation = reservation; hotspotReservation = reservation; currentConfig = reservation.getWifiConfiguration(); Toast.makeText(context, "THE PASSWORD IS: " From 4964da786915f5a7a5055826ee48dcf35392a116 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sat, 1 Jun 2019 20:30:31 +0500 Subject: [PATCH 016/266] Add turn on wifi hotspot on a single click feature Added a method toggleHotspot() that deals with recursion. Now on first app installation, on a single click permission is asked and if granted, wifi hotspot turns on. --- .../kiwix/kiwixmobile/main/MainActivity.java | 50 ++++++++++++------- 1 file changed, 33 insertions(+), 17 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 5a48eeaa4..4d70a7203 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -63,6 +63,7 @@ import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; +import androidx.annotation.RequiresApi; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.AppCompatButton; @@ -949,24 +950,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback, disableMobileData(); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - //Check if location permissions are granted - if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) - == PackageManager.PERMISSION_GRANTED) { - if (wifiHotspotManager.checkHotspotState()) //If hotspot is already enabled, turn it off - { - wifiHotspotManager.turnOffHotspot(); - } else //If hotspot is not already enabled, then turn it on. - { - setupLocationServices(); - } - } else { - //Show rationale and request location permission. - //No explanation needed; request the permission - ActivityCompat.requestPermissions(this, - new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, - MY_PERMISSIONS_ACCESS_FINE_LOCATION); - } + toggleHotspot(); } else { // For API<26 we use this // Checks if wifi access point is already enabled then turns it off, otherwise enables it. @@ -988,6 +973,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, return super.onOptionsItemSelected(item); } +<<<<<<< HEAD /** Dialog to take user confirmation before deleting all notes */ private void showClearAllNotesDialog() { @@ -1102,6 +1088,36 @@ public class MainActivity extends BaseActivity implements WebViewCallback, Toast.makeText(this, stringResource, duration).show(); } + @RequiresApi(api = VERSION_CODES.O) + private void toggleHotspot() { + boolean check = false; + //Check if location permissions are granted + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) + == PackageManager.PERMISSION_GRANTED) { + if (wifiHotspotManager.checkHotspotState()) //If hotspot is already enabled, turn it off + { + wifiHotspotManager.turnOffHotspot(); + } else //If hotspot is not already enabled, then turn it on. + { + setupLocationServices(); + } + } else { + //This var makes sure recursion occurs only once. + if (!check) { + //Show rationale and request location permission. + //No explanation needed; request the permission + ActivityCompat.requestPermissions(this, + new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, + MY_PERMISSIONS_ACCESS_FINE_LOCATION); + + check = true; + + //Go to toggle hotspot to check if permission is granted. + toggleHotspot(); + } + } + } + @SuppressWarnings("SameReturnValue") @OnLongClick(R.id.bottom_toolbar_bookmark) boolean goToBookmarks() { From 8a84f38522f9c7059232257a933b6237d30da7a3 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sun, 2 Jun 2019 21:33:20 +0500 Subject: [PATCH 017/266] Fixes#1188 Add alert dialogs for hotspot details & data warning mobileDataDialog(): tells the user his data is enabled. hotspotDetailsDialog(): tells the user details of local hotspot. --- .../kiwix/kiwixmobile/main/MainActivity.java | 24 +++++++++++++-- .../wifi_hotspot/WifiHotspotManager.java | 30 ++++++++++++++++--- app/src/main/res/values/strings.xml | 8 +++++ 3 files changed, 55 insertions(+), 7 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 4d70a7203..06a7d5936 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -946,8 +946,9 @@ public class MainActivity extends BaseActivity implements WebViewCallback, case R.id.menu_wifi_hotspot: //Check if user's hotspot is enabled if (isMobileDataEnabled(this)) { - Toast.makeText(this, "You need to disable mobile data ", Toast.LENGTH_LONG).show(); - disableMobileData(); + + mobileDataDialog(); + } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -973,7 +974,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, return super.onOptionsItemSelected(item); } -<<<<<<< HEAD /** Dialog to take user confirmation before deleting all notes */ private void showClearAllNotesDialog() { @@ -1087,6 +1087,24 @@ 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(); + } @RequiresApi(api = VERSION_CODES.O) private void toggleHotspot() { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index afc52ce6a..eeb3aa69b 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -11,7 +11,13 @@ import android.provider.Settings; import android.util.Log; import android.widget.Toast; import androidx.annotation.RequiresApi; +import androidx.appcompat.app.AlertDialog; +import com.google.android.material.snackbar.Snackbar; import java.lang.reflect.Method; +import org.kiwix.kiwixmobile.R; +import org.kiwix.kiwixmobile.main.MainActivity; + +import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; /** * WifiHotstopManager class makes use of the Android's WifiManager and WifiConfiguration class @@ -72,14 +78,14 @@ public class WifiHotspotManager { super.onStarted(reservation); hotspotReservation = reservation; currentConfig = reservation.getWifiConfiguration(); - Toast.makeText(context, "THE PASSWORD IS: " - + currentConfig.preSharedKey - + " \n SSID is : " - + currentConfig.SSID, Toast.LENGTH_LONG).show(); + Log.v("DANG", "THE PASSWORD IS: " + currentConfig.preSharedKey + " \n SSID is : " + currentConfig.SSID); + + hotspotDetailsDialog(); + oreoenabled = true; } @@ -163,4 +169,20 @@ public class WifiHotspotManager { return false; } } + + private void hotspotDetailsDialog() { + + AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle()); + + builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { + //Do nothing + }); + builder.setTitle(context.getString(R.string.hotspot_turned_on)); + builder.setMessage( + context.getString(R.string.hotspot_details_message) + "\n" + context.getString( + R.string.hotspot_ssid_label) + " " + currentConfig.SSID + "\n" + context.getString( + R.string.hotspot_pass_label) + " " + currentConfig.preSharedKey); + AlertDialog dialog = builder.create(); + dialog.show(); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6f5ecedff..1555b467c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -22,6 +22,14 @@ Search Select a Content File (*.zim) Open link in new tab? + No content found on your device.\nTake a look at the Help Page to get directions on how to load content into Kiwix.\nIf you did put a ZIM file on your device/external storage, you might retry in a minute or restart your device. + Hotspot turned on + Following are the details of your local hotspot. + SSID : + Pass : + Warning: Mobile data enabled + You\'re about to turn on your wifi hotspot. This feature can work without data usage. + Do you want to disable your data? Error: The selected ZIM file could not be found. Error: The selected file is not a valid ZIM file. Error: Loading article (Url: %1$s) failed. From 7fb1aa8fadfc6d787c79ccdf4ee3f7a67e2d2fcc Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sun, 2 Jun 2019 22:48:09 +0500 Subject: [PATCH 018/266] 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) From e4e333ae21957628c1c5f787f0035bc6dac536ae Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 27 Jun 2019 20:46:42 +0500 Subject: [PATCH 019/266] Remove WRITE_SETTINGS permission for devices>=O --- .../main/java/org/kiwix/kiwixmobile/main/MainActivity.java | 6 +++--- .../kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java | 3 ++- 2 files changed, 5 insertions(+), 4 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 8ec14472e..49ac0cd6c 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -1792,15 +1792,15 @@ public class MainActivity extends BaseActivity implements WebViewCallback, switch (resultCode) { case Activity.RESULT_OK: // All required changes were successfully made - Toast.makeText(MainActivity.this, states.isLocationPresent() + "", Toast.LENGTH_SHORT) - .show(); + Log.v("case 101", states.isLocationPresent() + ""); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { wifiHotspotManager.turnOnHotspot(); } break; case Activity.RESULT_CANCELED: // The user was asked to change settings, but chose not to - Toast.makeText(MainActivity.this, "Canceled", Toast.LENGTH_SHORT).show(); + Log.v("case 101", "Canceled"); break; default: break; diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index eeb3aa69b..aea42c61a 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -39,7 +39,8 @@ public class WifiHotspotManager { //To get write permission settings, we use this method. public void showWritePermissionSettings() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M + && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { if (!Settings.System.canWrite(this.context)) { Log.v("DANG", " " + !Settings.System.canWrite(this.context)); Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS); From dd76a41bf2a8cdb9150f4c5192f7715a27fc0ea1 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Mon, 1 Jul 2019 10:46:41 +0500 Subject: [PATCH 020/266] Hotspot foreground service. Added HotspotService Build foreground notification. Added broadcast receiver for stop button. Changed code in MainActivity for API<26. Added string values Declared service in manifest. --- app/src/main/AndroidManifest.xml | 3 + .../kiwix/kiwixmobile/main/MainActivity.java | 29 +++- .../wifi_hotspot/HotspotService.java | 125 ++++++++++++++++++ app/src/main/res/values/strings.xml | 2 + 4 files changed, 154 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2ea8e5341..f65fd4c1b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -199,6 +199,9 @@ android:resource="@xml/kiwix_widget_provider_info"/> + + + 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 49ac0cd6c..4ed4f73c4 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -125,6 +125,7 @@ import org.kiwix.kiwixmobile.utils.LanguageUtils; import org.kiwix.kiwixmobile.utils.NetworkUtils; import org.kiwix.kiwixmobile.utils.StyleUtils; import org.kiwix.kiwixmobile.utils.files.FileUtils; +import org.kiwix.kiwixmobile.wifi_hotspot.HotspotService; import org.kiwix.kiwixmobile.wifi_hotspot.WifiHotspotManager; import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity; import org.kiwix.kiwixmobile.zim_manager.fileselect_view.StorageObserver; @@ -175,6 +176,10 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private static final String NEW_TAB = "NEW_TAB"; private static final String HOME_URL = "file:///android_asset/home.html"; + public static final String ACTION_TURN_ON_BEFORE_O = "Turn_on_hotspot_before_oreo"; + public static final String ACTION_TURN_OFF_BEFORE_O = "Turn_aff_hotspot_before_oreo"; + public static final String ACTION_TURN_ON_AFTER_O = "Turn_on_hotspot_after_oreo"; + public static final String ACTION_TURN_OFF_AFTER_O = "Turn_off_hotspot_after_oreo"; public static boolean isFullscreenOpened; public static boolean refresh; public static boolean wifiOnly; @@ -184,6 +189,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private static Uri KIWIX_BROWSER_MARKET_URI; private final ArrayList bookmarks = new ArrayList<>(); private final List webViewList = new ArrayList<>(); + private Intent serviceIntent; @BindView(R.id.activity_main_root) ConstraintLayout root; @BindView(R.id.activity_main_toolbar) @@ -406,6 +412,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback, wifiHotspotManager = new WifiHotspotManager(this); wifiHotspotManager.showWritePermissionSettings(); + serviceIntent = new Intent(this, HotspotService.class); + } //End of onCreate @@ -1075,18 +1083,27 @@ public class MainActivity extends BaseActivity implements WebViewCallback, // 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); + startService(ACTION_TURN_OFF_BEFORE_O); + //wifiHotspotManager.setWifiEnabled(null, false); } else { //Check if user's hotspot is enabled if (isMobileDataEnabled(this)) { mobileDataDialog(); } else { - wifiHotspotManager.setWifiEnabled(null, true); + // potentially add data to the intent + //i.putExtra("TURN_ON_HOTSPOT_BEFORE_O", "turnOnHotspotBeforeO"); + startService(ACTION_TURN_ON_BEFORE_O); + //wifiHotspotManager.setWifiEnabled(null, true); } } } + private void startService(String ACTION) { + serviceIntent.setAction(ACTION); + this.startService(serviceIntent); + } + private void mobileDataDialog() { if (Build.VERSION.SDK_INT < VERSION_CODES.O) { AlertDialog.Builder builder = new AlertDialog.Builder(this, dialogStyle()); @@ -2299,9 +2316,11 @@ public class MainActivity extends BaseActivity implements WebViewCallback, // All location settings are satisfied. The client can initialize location // requests here. - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - wifiHotspotManager.turnOnHotspot(); - } + //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + serviceIntent.setAction(ACTION_TURN_ON_AFTER_O); + getApplicationContext().startService(serviceIntent); + //wifiHotspotManager.turnOnHotspot(); + //} } catch (ApiException exception) { switch (exception.getStatusCode()) { case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java new file mode 100644 index 000000000..d4dca29b6 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -0,0 +1,125 @@ +package org.kiwix.kiwixmobile.wifi_hotspot; + +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.app.Service; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.Build; +import android.os.IBinder; +import android.util.Log; +import androidx.annotation.Nullable; +import androidx.core.app.NotificationCompat; +import org.kiwix.kiwixmobile.R; +import org.kiwix.kiwixmobile.main.MainActivity; + +import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_AFTER_O; +import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_BEFORE_O; +import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_AFTER_O; +import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_BEFORE_O; + +public class HotspotService extends Service { + private static final int HOTSPOT_NOTIFICATION_ID = 666; + public static final String ACTION_START = "hotspot_start"; + public static final String ACTION_STOP = "hotspot_stop"; + public static final String ACTION_STATUS = "hotspot_status"; + private WifiHotspotManager wifiHotspotManager; + private BroadcastReceiver stopReceiver; + + @Override public void onCreate() { + super.onCreate(); + wifiHotspotManager = new WifiHotspotManager(this); + stopReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (intent != null && intent.getAction().equals(ACTION_STOP)) { + stopHotspot(); + } + } + }; + registerReceiver(stopReceiver, new IntentFilter(ACTION_STOP)); + startForeground(HOTSPOT_NOTIFICATION_ID, + buildForegroundNotification(getString(R.string.hotspot_start), false)); + } + + @Override public int onStartCommand(Intent intent, int flags, int startId) { + switch (intent.getAction()) { + case ACTION_TURN_ON_BEFORE_O: + if (wifiHotspotManager.setWifiEnabled(null, true)) { + updateNotification(getString(R.string.hotspot_running), true); + } + break; + case ACTION_TURN_ON_AFTER_O: + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + wifiHotspotManager.turnOnHotspot(); + } + break; + case ACTION_TURN_OFF_BEFORE_O: + wifiHotspotManager.setWifiEnabled(null, false); + stopForeground(true); + stopSelf(); + break; + case ACTION_TURN_OFF_AFTER_O: + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + wifiHotspotManager.turnOffHotspot(); + } + stopForeground(true); + stopSelf(); + + default: + break; + } + return START_NOT_STICKY; + } + + @Nullable @Override public IBinder onBind(Intent intent) { + return null; + } + + private Notification buildForegroundNotification(String status, boolean showStopButton) { + Log.v("DANG","Building notification "+status); + NotificationCompat.Builder b = new NotificationCompat.Builder(this); + b.setContentTitle("Kiwix Hotspot").setContentText(status); + Intent targetIntent = new Intent(this, MainActivity.class); + targetIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); + PendingIntent contentIntent = + PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT); + b.setContentIntent(contentIntent) + .setSmallIcon(R.mipmap.kiwix_icon) + .setWhen(System.currentTimeMillis()); + if (showStopButton) { + Intent stopIntent = new Intent(ACTION_STOP); + PendingIntent stopHotspot = + PendingIntent.getBroadcast(this, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT); + b.addAction(R.drawable.ic_close_white_24dp, getString(R.string.tts_stop), stopHotspot); + } + return (b.build()); + } + + private void updateNotification(String status, boolean stopAction) { + NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + nm.notify(HOTSPOT_NOTIFICATION_ID, buildForegroundNotification(status, stopAction)); + } + + private void stopHotspot() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + wifiHotspotManager.turnOffHotspot(); + } else { + Log.v("DANG", "Coming yes"); + wifiHotspotManager.setWifiEnabled(null, false); + } + stopForeground(true); + stopSelf(); + } + + @Override + public void onDestroy() { + if (stopReceiver != null) { + unregisterReceiver(stopReceiver); + } + super.onDestroy(); + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1555b467c..a8465f744 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -23,6 +23,8 @@ Select a Content File (*.zim) Open link in new tab? No content found on your device.\nTake a look at the Help Page to get directions on how to load content into Kiwix.\nIf you did put a ZIM file on your device/external storage, you might retry in a minute or restart your device. + Starting hotspot + Running Hotspot Hotspot turned on Following are the details of your local hotspot. SSID : From c75296b2de757a948de2302a474acd039749f073 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Mon, 1 Jul 2019 18:19:38 +0500 Subject: [PATCH 021/266] Add notification channel for devices>=O Added forground hotspot service for >=O Add constants. --- .../kiwix/kiwixmobile/main/MainActivity.java | 7 ++- .../kiwix/kiwixmobile/utils/Constants.java | 2 + .../wifi_hotspot/HotspotService.java | 43 +++++++++++++++---- .../wifi_hotspot/WifiHotspotManager.java | 11 ++++- app/src/main/res/values/strings.xml | 1 + 5 files changed, 54 insertions(+), 10 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 4ed4f73c4..72c4f14c5 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -1128,11 +1128,16 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private void toggleHotspot() { boolean check = false; //Check if location permissions are granted + Log.v("DANG","Turn off -1"); if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { + Log.v("DANG","Turn off 0"); if (wifiHotspotManager.checkHotspotState()) //If hotspot is already enabled, turn it off { - wifiHotspotManager.turnOffHotspot(); + Log.v("DANG","Turn off 1"); + serviceIntent.setAction(ACTION_TURN_OFF_AFTER_O); + getApplicationContext().startService(serviceIntent); + //wifiHotspotManager.turnOffHotspot(); } else //If hotspot is not already enabled, then turn it on. { setupLocationServices(); diff --git a/app/src/main/java/org/kiwix/kiwixmobile/utils/Constants.java b/app/src/main/java/org/kiwix/kiwixmobile/utils/Constants.java index 5b070dec0..1245b0d8a 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/utils/Constants.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/utils/Constants.java @@ -126,6 +126,8 @@ public final class Constants { public static final String EXTRA_NOTIFICATION_ID = "notificationID"; + public static final String HOTSPOT_SERVICE_CHANNEL_ID = "hotspotService"; + public static final String EXTRA_WEBVIEWS_LIST = "webviewsList"; public static final String EXTRA_SEARCH_TEXT = "searchText"; diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index d4dca29b6..2f0ef1d2a 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -1,6 +1,7 @@ package org.kiwix.kiwixmobile.wifi_hotspot; import android.app.Notification; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; @@ -15,6 +16,7 @@ import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.main.MainActivity; +import org.kiwix.kiwixmobile.utils.Constants; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_AFTER_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_BEFORE_O; @@ -28,6 +30,8 @@ public class HotspotService extends Service { public static final String ACTION_STATUS = "hotspot_status"; private WifiHotspotManager wifiHotspotManager; private BroadcastReceiver stopReceiver; + private NotificationManager notificationManager; + private NotificationCompat.Builder builder; @Override public void onCreate() { super.onCreate(); @@ -41,6 +45,7 @@ public class HotspotService extends Service { } }; registerReceiver(stopReceiver, new IntentFilter(ACTION_STOP)); + notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); startForeground(HOTSPOT_NOTIFICATION_ID, buildForegroundNotification(getString(R.string.hotspot_start), false)); } @@ -54,7 +59,11 @@ public class HotspotService extends Service { break; case ACTION_TURN_ON_AFTER_O: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + Log.v("DANG","Coming after 3"); wifiHotspotManager.turnOnHotspot(); + //if(it gets turned on successfully) then it goes to catch in MainActivity + updateNotification(getString(R.string.hotspot_running), true); + Log.v("DANG","Coming after calling updateNotification 8"); } break; case ACTION_TURN_OFF_BEFORE_O: @@ -63,12 +72,13 @@ public class HotspotService extends Service { stopSelf(); break; case ACTION_TURN_OFF_AFTER_O: + Log.v("DANG","Turn off 3"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { wifiHotspotManager.turnOffHotspot(); } stopForeground(true); stopSelf(); - + break; default: break; } @@ -81,27 +91,30 @@ public class HotspotService extends Service { private Notification buildForegroundNotification(String status, boolean showStopButton) { Log.v("DANG","Building notification "+status); - NotificationCompat.Builder b = new NotificationCompat.Builder(this); - b.setContentTitle("Kiwix Hotspot").setContentText(status); + builder = new NotificationCompat.Builder(this); + builder.setContentTitle("Kiwix Hotspot").setContentText(status); Intent targetIntent = new Intent(this, MainActivity.class); targetIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT); - b.setContentIntent(contentIntent) + builder.setContentIntent(contentIntent) .setSmallIcon(R.mipmap.kiwix_icon) .setWhen(System.currentTimeMillis()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + hotspotNotificationChannel(); + } if (showStopButton) { Intent stopIntent = new Intent(ACTION_STOP); PendingIntent stopHotspot = PendingIntent.getBroadcast(this, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT); - b.addAction(R.drawable.ic_close_white_24dp, getString(R.string.tts_stop), stopHotspot); + builder.addAction(R.drawable.ic_close_white_24dp, getString(R.string.tts_stop), stopHotspot); } - return (b.build()); + return (builder.build()); } private void updateNotification(String status, boolean stopAction) { - NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - nm.notify(HOTSPOT_NOTIFICATION_ID, buildForegroundNotification(status, stopAction)); + notificationManager.notify(HOTSPOT_NOTIFICATION_ID, + buildForegroundNotification(status, stopAction)); } private void stopHotspot() { @@ -122,4 +135,18 @@ public class HotspotService extends Service { } super.onDestroy(); } + + private void hotspotNotificationChannel() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + Log.v("DANG","Building notification channel start : 1"); + NotificationChannel hotspotServiceChannel = new NotificationChannel( + Constants.HOTSPOT_SERVICE_CHANNEL_ID, getString(R.string.hotspot_service_channel_name), + NotificationManager.IMPORTANCE_DEFAULT); + hotspotServiceChannel.setDescription("Sample hotspot description"); + hotspotServiceChannel.setSound(null, null); + builder.setChannelId(Constants.HOTSPOT_SERVICE_CHANNEL_ID); + notificationManager.createNotificationChannel(hotspotServiceChannel); + Log.v("DANG","Building notification channel end : 1.1"); + } + } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index aea42c61a..713b9558b 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -85,7 +85,7 @@ public class WifiHotspotManager { + " \n SSID is : " + currentConfig.SSID); - hotspotDetailsDialog(); + //hotspotDetailsDialog(); oreoenabled = true; } @@ -108,18 +108,23 @@ public class WifiHotspotManager { //Workaround to turn off hotspot for Oreo versions @RequiresApi(api = Build.VERSION_CODES.O) public void turnOffHotspot() { + Log.v("DANG","Turn off 4"); if (hotspotReservation != null) { hotspotReservation.close(); hotspotReservation = null; oreoenabled = false; + Log.v("DANG","Turned off hotspot"); } } //This method checks the state of the hostpot for devices>=Oreo public boolean checkHotspotState() { if (hotspotReservation != null) { + Log.v("DANG","I'm returning true"); return true; } else { + Log.v("DANG","I'm returning false"); + return false; } } @@ -173,6 +178,8 @@ public class WifiHotspotManager { private void hotspotDetailsDialog() { + Log.v("DANG","Coming hotspot details dialog :4"); + AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle()); builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { @@ -185,5 +192,7 @@ public class WifiHotspotManager { R.string.hotspot_pass_label) + " " + currentConfig.preSharedKey); AlertDialog dialog = builder.create(); dialog.show(); + Log.v("DANG","Coming end of hotspot details dialog :5"); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a8465f744..3ff3b8d01 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -23,6 +23,7 @@ Select a Content File (*.zim) Open link in new tab? No content found on your device.\nTake a look at the Help Page to get directions on how to load content into Kiwix.\nIf you did put a ZIM file on your device/external storage, you might retry in a minute or restart your device. + Hotspot Service Channel Starting hotspot Running Hotspot Hotspot turned on From 51907ffd8c725d9b6f9bc7e59dba5836b1bd839b Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Tue, 2 Jul 2019 17:53:11 +0500 Subject: [PATCH 022/266] Add logging to debug --- .../kiwix/kiwixmobile/main/MainActivity.java | 7 ++--- .../wifi_hotspot/WifiHotspotManager.java | 28 ++++++++++--------- 2 files changed, 18 insertions(+), 17 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 72c4f14c5..1d5530e61 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -413,7 +413,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, wifiHotspotManager.showWritePermissionSettings(); serviceIntent = new Intent(this, HotspotService.class); - } //End of onCreate @@ -1128,13 +1127,13 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private void toggleHotspot() { boolean check = false; //Check if location permissions are granted - Log.v("DANG","Turn off -1"); + Log.v("DANG", "Turn off -1"); if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { - Log.v("DANG","Turn off 0"); + Log.v("DANG", "Turn off 0"); if (wifiHotspotManager.checkHotspotState()) //If hotspot is already enabled, turn it off { - Log.v("DANG","Turn off 1"); + Log.v("DANG", "Turn off 1"); serviceIntent.setAction(ACTION_TURN_OFF_AFTER_O); getApplicationContext().startService(serviceIntent); //wifiHotspotManager.turnOffHotspot(); diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index 713b9558b..8472a3cce 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -28,7 +28,7 @@ import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; public class WifiHotspotManager { private WifiManager wifiManager; private Context context; - private WifiManager.LocalOnlyHotspotReservation hotspotReservation; + private WifiManager.LocalOnlyHotspotReservation hotspotReservation = null; private boolean oreoenabled = false; private WifiConfiguration currentConfig; @@ -78,7 +78,7 @@ public class WifiHotspotManager { public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) { super.onStarted(reservation); hotspotReservation = reservation; - currentConfig = reservation.getWifiConfiguration(); + currentConfig = hotspotReservation.getWifiConfiguration(); Log.v("DANG", "THE PASSWORD IS: " + currentConfig.preSharedKey @@ -108,24 +108,27 @@ public class WifiHotspotManager { //Workaround to turn off hotspot for Oreo versions @RequiresApi(api = Build.VERSION_CODES.O) public void turnOffHotspot() { - Log.v("DANG","Turn off 4"); + Log.v("DANG", "Turn off 4"); if (hotspotReservation != null) { hotspotReservation.close(); hotspotReservation = null; oreoenabled = false; - Log.v("DANG","Turned off hotspot"); + Log.v("DANG", "Turned off hotspot"); } } //This method checks the state of the hostpot for devices>=Oreo - public boolean checkHotspotState() { - if (hotspotReservation != null) { - Log.v("DANG","I'm returning true"); - return true; - } else { - Log.v("DANG","I'm returning false"); + @RequiresApi(api = Build.VERSION_CODES.O) + public boolean checkHotspotState() { + //Log.v("DANG",(hotspotReservation.getWifiConfiguration()).SSID); + if (hotspotReservation == null) { + Log.v("DANG", "I'm returning false"); return false; + } else { + Log.v("DANG", "I'm returning true"); + + return true; } } @@ -178,7 +181,7 @@ public class WifiHotspotManager { private void hotspotDetailsDialog() { - Log.v("DANG","Coming hotspot details dialog :4"); + Log.v("DANG", "Coming hotspot details dialog :4"); AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle()); @@ -192,7 +195,6 @@ public class WifiHotspotManager { R.string.hotspot_pass_label) + " " + currentConfig.preSharedKey); AlertDialog dialog = builder.create(); dialog.show(); - Log.v("DANG","Coming end of hotspot details dialog :5"); - + Log.v("DANG", "Coming end of hotspot details dialog :5"); } } From 78bf84e151ac0bc928b07567d5cba1935c09effc Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 4 Jul 2019 14:47:36 +0500 Subject: [PATCH 023/266] Add hotspot preference category. change hotspot ssid and hotspot pass. --- app/src/main/res/values/strings.xml | 7 +++++++ app/src/main/res/xml/preferences.xml | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3ff3b8d01..1a584baef 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -42,6 +42,13 @@ Information Version Zoom controls + Hotspot Settings + Password required to connect with hotspot + Set Password + ******** + Set hotspot password + hotspot + Hotspot SSID Night mode Show articles with inverted colors Back to Top diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 59fb37f46..af75b3be5 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -63,6 +63,29 @@ android:title="@string/pref_wifi_only"/> + + + + + + + + + + + From 7603f8d5e96a3c5958f4a6007638503b5f2877dc Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 5 Jul 2019 18:35:50 +0500 Subject: [PATCH 024/266] Removing null from hotspot reservation --- .../org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index 8472a3cce..cf5e13809 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -28,7 +28,7 @@ import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; public class WifiHotspotManager { private WifiManager wifiManager; private Context context; - private WifiManager.LocalOnlyHotspotReservation hotspotReservation = null; + private WifiManager.LocalOnlyHotspotReservation hotspotReservation; private boolean oreoenabled = false; private WifiConfiguration currentConfig; From 1f5a52484f620faf9c4ca5b6eb910f8a458d97d9 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 5 Jul 2019 18:38:55 +0500 Subject: [PATCH 025/266] Use startService() for Oreo+ --- .../java/org/kiwix/kiwixmobile/main/MainActivity.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 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 1d5530e61..26f3fc54a 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -1134,8 +1134,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, if (wifiHotspotManager.checkHotspotState()) //If hotspot is already enabled, turn it off { Log.v("DANG", "Turn off 1"); - serviceIntent.setAction(ACTION_TURN_OFF_AFTER_O); - getApplicationContext().startService(serviceIntent); + startService(ACTION_TURN_OFF_AFTER_O); //wifiHotspotManager.turnOffHotspot(); } else //If hotspot is not already enabled, then turn it on. { @@ -1814,10 +1813,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, case Activity.RESULT_OK: // All required changes were successfully made Log.v("case 101", states.isLocationPresent() + ""); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - wifiHotspotManager.turnOnHotspot(); - } + startService(ACTION_TURN_ON_AFTER_O); break; case Activity.RESULT_CANCELED: // The user was asked to change settings, but chose not to @@ -2321,8 +2317,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, // requests here. //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - serviceIntent.setAction(ACTION_TURN_ON_AFTER_O); - getApplicationContext().startService(serviceIntent); + startService(ACTION_TURN_ON_AFTER_O); //wifiHotspotManager.turnOnHotspot(); //} } catch (ApiException exception) { From ecf134a63241df0e8c430be7fa7d281cccd1d689 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 5 Jul 2019 20:47:31 +0500 Subject: [PATCH 026/266] Fixed local hotspot not stopping bug on devices >=O Now checking hotspotstate from within hotspot service using the context from main activity. --- .../kiwix/kiwixmobile/main/MainActivity.java | 11 ++++---- .../wifi_hotspot/HotspotService.java | 28 +++++++++++++------ .../wifi_hotspot/WifiHotspotManager.java | 2 +- 3 files changed, 27 insertions(+), 14 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 26f3fc54a..f6b88ffd6 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -170,6 +170,7 @@ import static org.kiwix.kiwixmobile.utils.Constants.TAG_KIWIX; import static org.kiwix.kiwixmobile.utils.LanguageUtils.getResourceString; import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; import static org.kiwix.kiwixmobile.utils.UpdateUtils.reformatProviderUrl; +import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.checkHotspotState; public class MainActivity extends BaseActivity implements WebViewCallback, MainContract.View { @@ -1083,7 +1084,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private void switchHotspot() { if (wifiHotspotManager.isWifiApEnabled()) { startService(ACTION_TURN_OFF_BEFORE_O); - //wifiHotspotManager.setWifiEnabled(null, false); + //hotspotManager.setWifiEnabled(null, false); } else { //Check if user's hotspot is enabled if (isMobileDataEnabled(this)) { @@ -1093,7 +1094,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, // potentially add data to the intent //i.putExtra("TURN_ON_HOTSPOT_BEFORE_O", "turnOnHotspotBeforeO"); startService(ACTION_TURN_ON_BEFORE_O); - //wifiHotspotManager.setWifiEnabled(null, true); + //hotspotManager.setWifiEnabled(null, true); } } } @@ -1131,11 +1132,11 @@ public class MainActivity extends BaseActivity implements WebViewCallback, if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { Log.v("DANG", "Turn off 0"); - if (wifiHotspotManager.checkHotspotState()) //If hotspot is already enabled, turn it off + if (checkHotspotState(this)) //If hotspot is already enabled, turn it off { Log.v("DANG", "Turn off 1"); startService(ACTION_TURN_OFF_AFTER_O); - //wifiHotspotManager.turnOffHotspot(); + //hotspotManager.turnOffHotspot(); } else //If hotspot is not already enabled, then turn it on. { setupLocationServices(); @@ -2318,7 +2319,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startService(ACTION_TURN_ON_AFTER_O); - //wifiHotspotManager.turnOnHotspot(); + //hotspotManager.turnOnHotspot(); //} } catch (ApiException exception) { switch (exception.getStatusCode()) { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index 2f0ef1d2a..903835fad 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -13,6 +13,7 @@ import android.os.Build; import android.os.IBinder; import android.util.Log; import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.main.MainActivity; @@ -28,14 +29,16 @@ public class HotspotService extends Service { public static final String ACTION_START = "hotspot_start"; public static final String ACTION_STOP = "hotspot_stop"; public static final String ACTION_STATUS = "hotspot_status"; - private WifiHotspotManager wifiHotspotManager; + public static WifiHotspotManager hotspotManager; private BroadcastReceiver stopReceiver; private NotificationManager notificationManager; private NotificationCompat.Builder builder; @Override public void onCreate() { super.onCreate(); - wifiHotspotManager = new WifiHotspotManager(this); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + hotspotManager = new WifiHotspotManager(this); + } stopReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -53,28 +56,28 @@ public class HotspotService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { switch (intent.getAction()) { case ACTION_TURN_ON_BEFORE_O: - if (wifiHotspotManager.setWifiEnabled(null, true)) { + if (hotspotManager.setWifiEnabled(null, true)) { updateNotification(getString(R.string.hotspot_running), true); } break; case ACTION_TURN_ON_AFTER_O: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Log.v("DANG","Coming after 3"); - wifiHotspotManager.turnOnHotspot(); + hotspotManager.turnOnHotspot(); //if(it gets turned on successfully) then it goes to catch in MainActivity updateNotification(getString(R.string.hotspot_running), true); Log.v("DANG","Coming after calling updateNotification 8"); } break; case ACTION_TURN_OFF_BEFORE_O: - wifiHotspotManager.setWifiEnabled(null, false); + hotspotManager.setWifiEnabled(null, false); stopForeground(true); stopSelf(); break; case ACTION_TURN_OFF_AFTER_O: Log.v("DANG","Turn off 3"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - wifiHotspotManager.turnOffHotspot(); + hotspotManager.turnOffHotspot(); } stopForeground(true); stopSelf(); @@ -119,10 +122,10 @@ public class HotspotService extends Service { private void stopHotspot() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - wifiHotspotManager.turnOffHotspot(); + hotspotManager.turnOffHotspot(); } else { Log.v("DANG", "Coming yes"); - wifiHotspotManager.setWifiEnabled(null, false); + hotspotManager.setWifiEnabled(null, false); } stopForeground(true); stopSelf(); @@ -149,4 +152,13 @@ public class HotspotService extends Service { Log.v("DANG","Building notification channel end : 1.1"); } } + + @RequiresApi(api = Build.VERSION_CODES.O) + public static boolean checkHotspotState(Context context) { + if (hotspotManager == null) { + Log.v("DANG", "hotspotManager initialized"); + hotspotManager = new WifiHotspotManager(context); + } + return hotspotManager.checkHotspotState(); + } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index cf5e13809..d9a279fcd 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -85,7 +85,7 @@ public class WifiHotspotManager { + " \n SSID is : " + currentConfig.SSID); - //hotspotDetailsDialog(); + hotspotDetailsDialog(); oreoenabled = true; } From 35ed0e079d4770c77313879503214d219421b8a9 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sat, 6 Jul 2019 18:17:42 +0500 Subject: [PATCH 027/266] Add dialog showing hotspot details for versions bookmarks = new ArrayList<>(); @@ -1099,6 +1099,10 @@ public class MainActivity extends BaseActivity implements WebViewCallback, } } + public static void startHotspotDetails() { + wifiHotspotManager.hotspotDetailsDialog(); + } + private void startService(String ACTION) { serviceIntent.setAction(ACTION); this.startService(serviceIntent); diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index 903835fad..fc4a15bd2 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -23,6 +23,7 @@ import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_AFTER_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_BEFORE_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_AFTER_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_BEFORE_O; +import static org.kiwix.kiwixmobile.main.MainActivity.startHotspotDetails; public class HotspotService extends Service { private static final int HOTSPOT_NOTIFICATION_ID = 666; @@ -57,6 +58,8 @@ public class HotspotService extends Service { switch (intent.getAction()) { case ACTION_TURN_ON_BEFORE_O: if (hotspotManager.setWifiEnabled(null, true)) { + Log.v("DANG", "INSIDE"); + startHotspotDetails(); updateNotification(getString(R.string.hotspot_running), true); } break; diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index d9a279fcd..435216519 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -179,7 +179,7 @@ public class WifiHotspotManager { } } - private void hotspotDetailsDialog() { + public void hotspotDetailsDialog() { Log.v("DANG", "Coming hotspot details dialog :4"); @@ -189,10 +189,18 @@ public class WifiHotspotManager { //Do nothing }); builder.setTitle(context.getString(R.string.hotspot_turned_on)); - builder.setMessage( - context.getString(R.string.hotspot_details_message) + "\n" + context.getString( - R.string.hotspot_ssid_label) + " " + currentConfig.SSID + "\n" + context.getString( - R.string.hotspot_pass_label) + " " + currentConfig.preSharedKey); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + builder.setMessage( + context.getString(R.string.hotspot_details_message) + "\n" + context.getString( + R.string.hotspot_ssid_label) + " " + currentConfig.SSID + "\n" + context.getString( + R.string.hotspot_pass_label) + " " + currentConfig.preSharedKey); + } else { + currentConfig = getWifiApConfiguration(); + builder.setMessage( + context.getString(R.string.hotspot_details_message) + "\n" + context.getString( + R.string.hotspot_ssid_label) + " " + currentConfig.SSID + "\n" + context.getString( + R.string.hotspot_pass_label) + " " + currentConfig.preSharedKey); + } AlertDialog dialog = builder.create(); dialog.show(); Log.v("DANG", "Coming end of hotspot details dialog :5"); From 2053e6892621e51f993c8ffc34bbde709687aabe Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sat, 6 Jul 2019 18:44:01 +0500 Subject: [PATCH 028/266] Clean Code Removing unused imports. Removing irrelevant comments and logs --- .../org/kiwix/kiwixmobile/main/MainActivity.java | 11 +---------- .../kiwixmobile/wifi_hotspot/HotspotService.java | 8 -------- .../kiwixmobile/wifi_hotspot/WifiHotspotManager.java | 12 ------------ 3 files changed, 1 insertion(+), 30 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 1feb834de..ff543aa49 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -1084,17 +1084,13 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private void switchHotspot() { if (wifiHotspotManager.isWifiApEnabled()) { startService(ACTION_TURN_OFF_BEFORE_O); - //hotspotManager.setWifiEnabled(null, false); } else { //Check if user's hotspot is enabled if (isMobileDataEnabled(this)) { mobileDataDialog(); } else { - // potentially add data to the intent - //i.putExtra("TURN_ON_HOTSPOT_BEFORE_O", "turnOnHotspotBeforeO"); startService(ACTION_TURN_ON_BEFORE_O); - //hotspotManager.setWifiEnabled(null, true); } } } @@ -1132,15 +1128,11 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private void toggleHotspot() { boolean check = false; //Check if location permissions are granted - Log.v("DANG", "Turn off -1"); if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { - Log.v("DANG", "Turn off 0"); if (checkHotspotState(this)) //If hotspot is already enabled, turn it off { - Log.v("DANG", "Turn off 1"); startService(ACTION_TURN_OFF_AFTER_O); - //hotspotManager.turnOffHotspot(); } else //If hotspot is not already enabled, then turn it on. { setupLocationServices(); @@ -1182,7 +1174,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, method.setAccessible(true); enabled = (Boolean) method.invoke(cm); } catch (Exception e) { - Log.e("DANG ", e.toString()); + Log.e("DANG", e.toString()); } return enabled; } @@ -2323,7 +2315,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startService(ACTION_TURN_ON_AFTER_O); - //hotspotManager.turnOnHotspot(); //} } catch (ApiException exception) { switch (exception.getStatusCode()) { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index fc4a15bd2..0fe850e8a 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -58,18 +58,14 @@ public class HotspotService extends Service { switch (intent.getAction()) { case ACTION_TURN_ON_BEFORE_O: if (hotspotManager.setWifiEnabled(null, true)) { - Log.v("DANG", "INSIDE"); startHotspotDetails(); updateNotification(getString(R.string.hotspot_running), true); } break; case ACTION_TURN_ON_AFTER_O: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - Log.v("DANG","Coming after 3"); hotspotManager.turnOnHotspot(); - //if(it gets turned on successfully) then it goes to catch in MainActivity updateNotification(getString(R.string.hotspot_running), true); - Log.v("DANG","Coming after calling updateNotification 8"); } break; case ACTION_TURN_OFF_BEFORE_O: @@ -78,7 +74,6 @@ public class HotspotService extends Service { stopSelf(); break; case ACTION_TURN_OFF_AFTER_O: - Log.v("DANG","Turn off 3"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { hotspotManager.turnOffHotspot(); } @@ -127,7 +122,6 @@ public class HotspotService extends Service { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { hotspotManager.turnOffHotspot(); } else { - Log.v("DANG", "Coming yes"); hotspotManager.setWifiEnabled(null, false); } stopForeground(true); @@ -144,7 +138,6 @@ public class HotspotService extends Service { private void hotspotNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - Log.v("DANG","Building notification channel start : 1"); NotificationChannel hotspotServiceChannel = new NotificationChannel( Constants.HOTSPOT_SERVICE_CHANNEL_ID, getString(R.string.hotspot_service_channel_name), NotificationManager.IMPORTANCE_DEFAULT); @@ -152,7 +145,6 @@ public class HotspotService extends Service { hotspotServiceChannel.setSound(null, null); builder.setChannelId(Constants.HOTSPOT_SERVICE_CHANNEL_ID); notificationManager.createNotificationChannel(hotspotServiceChannel); - Log.v("DANG","Building notification channel end : 1.1"); } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index 435216519..9aeade174 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -9,13 +9,10 @@ import android.os.Build; import android.os.Handler; import android.provider.Settings; import android.util.Log; -import android.widget.Toast; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AlertDialog; -import com.google.android.material.snackbar.Snackbar; import java.lang.reflect.Method; import org.kiwix.kiwixmobile.R; -import org.kiwix.kiwixmobile.main.MainActivity; import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; @@ -108,7 +105,6 @@ public class WifiHotspotManager { //Workaround to turn off hotspot for Oreo versions @RequiresApi(api = Build.VERSION_CODES.O) public void turnOffHotspot() { - Log.v("DANG", "Turn off 4"); if (hotspotReservation != null) { hotspotReservation.close(); hotspotReservation = null; @@ -118,16 +114,11 @@ public class WifiHotspotManager { } //This method checks the state of the hostpot for devices>=Oreo - @RequiresApi(api = Build.VERSION_CODES.O) public boolean checkHotspotState() { - //Log.v("DANG",(hotspotReservation.getWifiConfiguration()).SSID); if (hotspotReservation == null) { - Log.v("DANG", "I'm returning false"); return false; } else { - Log.v("DANG", "I'm returning true"); - return true; } } @@ -181,8 +172,6 @@ public class WifiHotspotManager { public void hotspotDetailsDialog() { - Log.v("DANG", "Coming hotspot details dialog :4"); - AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle()); builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { @@ -203,6 +192,5 @@ public class WifiHotspotManager { } AlertDialog dialog = builder.create(); dialog.show(); - Log.v("DANG", "Coming end of hotspot details dialog :5"); } } From 5a8cbb2efc7dc3322b0312c7b12ed49702238a94 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sat, 6 Jul 2019 18:47:44 +0500 Subject: [PATCH 029/266] Add credits to HotspotService --- .../org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index 0fe850e8a..b0b068cca 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -25,6 +25,11 @@ import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_AFTER_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_BEFORE_O; import static org.kiwix.kiwixmobile.main.MainActivity.startHotspotDetails; +/** + * HotspotService is used to add a foreground service for the wifi hotspot. + * Created by Adeel Zafar on 07/01/2019. + */ + public class HotspotService extends Service { private static final int HOTSPOT_NOTIFICATION_ID = 666; public static final String ACTION_START = "hotspot_start"; From 16e310793d132356f465d1d169dc9d2767006d43 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sat, 6 Jul 2019 20:34:28 +0500 Subject: [PATCH 030/266] WRITE_Settings permission not given bug Handle if write settings permission is not given. --- .../kiwix/kiwixmobile/main/MainActivity.java | 27 ++++++++++++++----- .../wifi_hotspot/WifiHotspotManager.java | 17 ------------ 2 files changed, 20 insertions(+), 24 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 ff543aa49..fee2603f9 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -411,7 +411,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, drawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); wifiHotspotManager = new WifiHotspotManager(this); - wifiHotspotManager.showWritePermissionSettings(); serviceIntent = new Intent(this, HotspotService.class); } @@ -955,7 +954,9 @@ public class MainActivity extends BaseActivity implements WebViewCallback, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { toggleHotspot(); } else { - switchHotspot(); + if (showWritePermissionSettings()) { //request permission and if already granted switch hotspot. + switchHotspot(); + } } default: @@ -1085,14 +1086,13 @@ public class MainActivity extends BaseActivity implements WebViewCallback, if (wifiHotspotManager.isWifiApEnabled()) { startService(ACTION_TURN_OFF_BEFORE_O); } else { - //Check if user's hotspot is enabled if (isMobileDataEnabled(this)) { mobileDataDialog(); } else { startService(ACTION_TURN_ON_BEFORE_O); } - } + } } public static void startHotspotDetails() { @@ -1154,6 +1154,22 @@ public class MainActivity extends BaseActivity implements WebViewCallback, } } + //To get write permission settings, we use this method. + private boolean showWritePermissionSettings() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M + && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + if (!Settings.System.canWrite(this)) { + Log.v("DANG", " " + !Settings.System.canWrite(this)); + Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS); + intent.setData(Uri.parse("package:" + this.getPackageName())); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + this.startActivity(intent); + return false; + } + } + return true; //Permission already given + } + @SuppressWarnings("SameReturnValue") @OnLongClick(R.id.bottom_toolbar_bookmark) boolean goToBookmarks() { @@ -1589,8 +1605,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, } } updateWidgets(this); - - wifiHotspotManager.showWritePermissionSettings(); } private void updateBottomToolbarVisibility() { @@ -1802,7 +1816,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, } } return; - //Checking the result code for LocationSettings resolution case 101: final LocationSettingsStates states = LocationSettingsStates.fromIntent(data); diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index 9aeade174..bc51f6203 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -1,13 +1,10 @@ package org.kiwix.kiwixmobile.wifi_hotspot; import android.content.Context; -import android.content.Intent; -import android.net.Uri; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.os.Build; import android.os.Handler; -import android.provider.Settings; import android.util.Log; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AlertDialog; @@ -34,20 +31,6 @@ public class WifiHotspotManager { wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE); } - //To get write permission settings, we use this method. - public void showWritePermissionSettings() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M - && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { - if (!Settings.System.canWrite(this.context)) { - Log.v("DANG", " " + !Settings.System.canWrite(this.context)); - Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS); - intent.setData(Uri.parse("package:" + this.context.getPackageName())); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - this.context.startActivity(intent); - } - } - } - // This method enables/disables the wifi access point // It is used for API<26 public boolean setWifiEnabled(WifiConfiguration wifiConfig, boolean enabled) { From 16a6a9f0b6f212892cd5ddc47fc6dcd8d3021c51 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Tue, 9 Jul 2019 20:51:11 +0500 Subject: [PATCH 031/266] Add FOREGROUND_SERVICE permission in manifest Device with versions >= Pie need this permission to start foreground service. --- app/src/main/AndroidManifest.xml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f65fd4c1b..866a151f6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -30,10 +30,13 @@ - + + + + Date: Thu, 18 Jul 2019 04:04:22 +0500 Subject: [PATCH 032/266] Reverting hotspot preference category --- app/src/main/res/values/strings.xml | 7 ------- app/src/main/res/xml/preferences.xml | 25 +------------------------ 2 files changed, 1 insertion(+), 31 deletions(-) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1a584baef..3ff3b8d01 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -42,13 +42,6 @@ Information Version Zoom controls - Hotspot Settings - Password required to connect with hotspot - Set Password - ******** - Set hotspot password - hotspot - Hotspot SSID Night mode Show articles with inverted colors Back to Top diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index af75b3be5..7b3d2ff37 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -62,30 +62,7 @@ android:summary="@string/pref_wifi_only" android:title="@string/pref_wifi_only"/> - - - - - - - - - - - - + From 76b4a3587f9fa2bafdb5418e8b738b58de99986e Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 18 Jul 2019 04:27:14 +0500 Subject: [PATCH 033/266] Resolve lint errors Resolve java warnings --- .../kiwix/kiwixmobile/main/MainActivity.java | 79 +++++++++---------- .../wifi_hotspot/HotspotService.java | 2 - .../wifi_hotspot/WifiHotspotManager.java | 10 +-- app/src/main/res/xml/preferences.xml | 2 +- 4 files changed, 40 insertions(+), 53 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 fee2603f9..ae2c7289e 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -262,7 +262,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private CompatFindActionModeCallback compatCallback; private TabsAdapter tabsAdapter; private int currentWebViewIndex = 0; - private final int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 102; private Task task; private File file; private ActionMode actionMode = null; @@ -1108,12 +1107,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback, 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.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( @@ -1142,6 +1137,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, if (!check) { //Show rationale and request location permission. //No explanation needed; request the permission + int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 102; ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, MY_PERMISSIONS_ACCESS_FINE_LOCATION); @@ -1180,7 +1176,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, } // This method checks if mobile data is enabled in user's device. - public static boolean isMobileDataEnabled(Context context) { + private static boolean isMobileDataEnabled(Context context) { boolean enabled = false; ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); @@ -1196,7 +1192,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, } //This method sends the user to data usage summary settings activity - public void disableMobileData() { + private void disableMobileData() { Intent intent = new Intent(); intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity")); @@ -2318,41 +2314,38 @@ public class MainActivity extends BaseActivity implements WebViewCallback, } private void locationSettingsResponseBuilder() { - task.addOnCompleteListener(new OnCompleteListener() { - @Override - public void onComplete(Task task) { - try { - LocationSettingsResponse response = task.getResult(ApiException.class); - // All location settings are satisfied. The client can initialize location - // requests here. + task.addOnCompleteListener(task -> { + try { + LocationSettingsResponse response = task.getResult(ApiException.class); + // All location settings are satisfied. The client can initialize location + // requests here. - //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - startService(ACTION_TURN_ON_AFTER_O); - //} - } catch (ApiException exception) { - switch (exception.getStatusCode()) { - case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: - // Location settings are not satisfied. But could be fixed by showing the - // user a dialog. - try { - // Cast to a resolvable exception. - ResolvableApiException resolvable = (ResolvableApiException) exception; - // Show the dialog by calling startResolutionForResult(), - // and check the result in onActivityResult(). - resolvable.startResolutionForResult( - MainActivity.this, - 101); - } catch (IntentSender.SendIntentException e) { - // Ignore the error. - } catch (ClassCastException e) { - // Ignore, should be an impossible error. - } - break; - case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: - // Location settings are not satisfied. However, we have no way to fix the - // settings so we won't show the dialog. - break; - } + //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + startService(ACTION_TURN_ON_AFTER_O); + //} + } catch (ApiException exception) { + switch (exception.getStatusCode()) { + case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: + // Location settings are not satisfied. But could be fixed by showing the + // user a dialog. + try { + // Cast to a resolvable exception. + ResolvableApiException resolvable = (ResolvableApiException) exception; + // Show the dialog by calling startResolutionForResult(), + // and check the result in onActivityResult(). + resolvable.startResolutionForResult( + MainActivity.this, + 101); + } catch (IntentSender.SendIntentException e) { + // Ignore the error. + } catch (ClassCastException e) { + // Ignore, should be an impossible error. + } + break; + case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: + // Location settings are not satisfied. However, we have no way to fix the + // settings so we won't show the dialog. + break; } } }); diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index b0b068cca..2a9c98e8a 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -32,9 +32,7 @@ import static org.kiwix.kiwixmobile.main.MainActivity.startHotspotDetails; public class HotspotService extends Service { private static final int HOTSPOT_NOTIFICATION_ID = 666; - public static final String ACTION_START = "hotspot_start"; public static final String ACTION_STOP = "hotspot_stop"; - public static final String ACTION_STATUS = "hotspot_status"; public static WifiHotspotManager hotspotManager; private BroadcastReceiver stopReceiver; private NotificationManager notificationManager; diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index bc51f6203..cbd172fed 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -99,15 +99,11 @@ public class WifiHotspotManager { //This method checks the state of the hostpot for devices>=Oreo @RequiresApi(api = Build.VERSION_CODES.O) public boolean checkHotspotState() { - if (hotspotReservation == null) { - return false; - } else { - return true; - } + return hotspotReservation != null; } // This method returns the current state of the Wifi access point - public WIFI_AP_STATE_ENUMS getWifiApState() { + private WIFI_AP_STATE_ENUMS getWifiApState() { try { Method method = wifiManager.getClass().getMethod("getWifiApState"); @@ -131,7 +127,7 @@ public class WifiHotspotManager { } //This method is to get the wifi ap configuration - public WifiConfiguration getWifiApConfiguration() { + private WifiConfiguration getWifiApConfiguration() { try { Method method = wifiManager.getClass().getMethod("getWifiApConfiguration"); return (WifiConfiguration) method.invoke(wifiManager); diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 7b3d2ff37..59fb37f46 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -62,7 +62,7 @@ android:summary="@string/pref_wifi_only" android:title="@string/pref_wifi_only"/> - + From 956098c3d30f40ab0f1c33b14f7e8833a0b2a080 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 18 Jul 2019 06:01:20 +0500 Subject: [PATCH 034/266] Changed wifi hotspot menu button's visibility modes --- app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java | 2 ++ app/src/main/res/menu/menu_main.xml | 1 + 2 files changed, 3 insertions(+) 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 ae2c7289e..5ea3e2991 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -1898,11 +1898,13 @@ public class MainActivity extends BaseActivity implements WebViewCallback, menu.findItem(R.id.menu_home).setVisible(false); menu.findItem(R.id.menu_random_article).setVisible(false); menu.findItem(R.id.menu_searchintext).setVisible(false); + menu.findItem(R.id.menu_wifi_hotspot).setVisible(false); } else { menu.findItem(R.id.menu_read_aloud).setVisible(true); menu.findItem(R.id.menu_home).setVisible(true); menu.findItem(R.id.menu_random_article).setVisible(true); menu.findItem(R.id.menu_searchintext).setVisible(true); + menu.findItem(R.id.menu_wifi_hotspot).setVisible(true); } } return true; diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index b0eb1174d..0f7c5b1b4 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -97,6 +97,7 @@ From 2f2130a21f86e8b341ef1addef6b0d4d3b9dd67a Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 18 Jul 2019 06:12:42 +0500 Subject: [PATCH 035/266] Add NanoHTTPD dependency --- app/build.gradle | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index 942cff8d0..2901ebb43 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -132,6 +132,9 @@ dependencies { implementation "io.objectbox:objectbox-kotlin:$objectboxVersion" implementation "io.objectbox:objectbox-rxjava:$objectboxVersion" + //NanoHTTPD + implementation 'org.nanohttpd:nanohttpd:2.3.1' + testImplementation "org.junit.jupiter:junit-jupiter:5.4.2" testImplementation "io.mockk:mockk:1.9" testImplementation "org.assertj:assertj-core:3.11.1" From 000da0cc5a5fc26deb54fdb899231c5d6a183a50 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 18 Jul 2019 06:16:30 +0500 Subject: [PATCH 036/266] Add WebServer class Add serve method --- .../kiwixmobile/webserver/WebServer.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java new file mode 100644 index 000000000..fea26ceb9 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java @@ -0,0 +1,23 @@ +package org.kiwix.kiwixmobile.webserver; + +import fi.iki.elonen.NanoHTTPD; +import java.util.Map; + +public class WebServer extends NanoHTTPD { + + public WebServer(int port) { + super(port); + } + + @Override + public Response serve(IHTTPSession session) { + String msg = "

Hello server

\n"; + Map parms = session.getParms(); + if (parms.get("username") == null) { + msg += "
\n

Your name:

\n" + "
\n"; + } else { + msg += "

Hello, " + parms.get("username") + "!

"; + } + return newFixedLengthResponse( msg + "\n" ); + } +} From ee7f5c1ff4c85ca01d8e140a381e48a9952db30e Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 19 Jul 2019 17:52:25 +0500 Subject: [PATCH 037/266] Start server from MainActivity Getting ip address Adding a custom dialog Adding a network change listener broadcast receiver Stop android server Unregister receiver in onDestroy --- .../kiwix/kiwixmobile/main/MainActivity.java | 192 +++++++++++++++++- app/src/main/res/values/strings.xml | 1 + 2 files changed, 184 insertions(+), 9 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 5ea3e2991..1ae5eaa6c 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -24,10 +24,12 @@ import android.annotation.SuppressLint; import android.app.Activity; import android.appwidget.AppWidgetManager; import android.content.ActivityNotFoundException; +import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.IntentFilter; import android.content.IntentSender; import android.content.SharedPreferences; import android.content.pm.PackageManager; @@ -42,6 +44,8 @@ import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; import android.provider.Settings; +import android.text.InputFilter; +import android.text.InputType; import android.text.SpannableString; import android.text.style.ForegroundColorSpan; import android.util.AttributeSet; @@ -57,9 +61,11 @@ import android.view.WindowManager; import android.webkit.WebSettings; import android.webkit.WebView; import android.widget.Button; +import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ImageButton; import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; @@ -71,6 +77,7 @@ import androidx.appcompat.widget.Toolbar; import androidx.cardview.widget.CardView; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.Group; +import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.core.view.GravityCompat; @@ -91,7 +98,6 @@ import com.google.android.gms.location.LocationSettingsRequest; import com.google.android.gms.location.LocationSettingsResponse; import com.google.android.gms.location.LocationSettingsStates; import com.google.android.gms.location.LocationSettingsStatusCodes; -import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.floatingactionbutton.FloatingActionButton; @@ -100,10 +106,14 @@ import com.google.android.material.snackbar.Snackbar; import io.reactivex.android.schedulers.AndroidSchedulers; import java.io.File; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; import java.text.SimpleDateFormat; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Date; +import java.util.Enumeration; import java.util.List; import java.util.Locale; import javax.inject.Inject; @@ -125,6 +135,7 @@ import org.kiwix.kiwixmobile.utils.LanguageUtils; import org.kiwix.kiwixmobile.utils.NetworkUtils; import org.kiwix.kiwixmobile.utils.StyleUtils; import org.kiwix.kiwixmobile.utils.files.FileUtils; +import org.kiwix.kiwixmobile.webserver.WebServer; import org.kiwix.kiwixmobile.wifi_hotspot.HotspotService; import org.kiwix.kiwixmobile.wifi_hotspot.WifiHotspotManager; import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity; @@ -191,6 +202,14 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private final ArrayList bookmarks = new ArrayList<>(); private final List webViewList = new ArrayList<>(); private Intent serviceIntent; + private WebServer webServer; + private static final int DEFAULT_PORT = 8080; + private BroadcastReceiver broadcastReceiverNetworkState; + private static boolean isStarted = false; + private CoordinatorLayout coordinatorLayout; + private EditText editTextPort; + private TextView textViewIpAccess; + private int port; @BindView(R.id.activity_main_root) ConstraintLayout root; @BindView(R.id.activity_main_toolbar) @@ -229,7 +248,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, View tabSwitcherRoot; @BindView(R.id.tab_switcher_close_all_tabs) FloatingActionButton closeAllTabsButton; - @Inject MainContract.Presenter presenter; @Inject @@ -412,6 +430,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback, wifiHotspotManager = new WifiHotspotManager(this); serviceIntent = new Intent(this, HotspotService.class); + + //initBroadcastReceiverNetworkStateChanged(); } //End of onCreate @@ -769,6 +789,12 @@ public class MainActivity extends BaseActivity implements WebViewCallback, // TODO create a base Activity class that class this. FileUtils.deleteCachedFiles(this); tts.shutdown(); + + stopAndroidWebServer(); + isStarted = false; + if (broadcastReceiverNetworkState != null) { + unregisterReceiver(broadcastReceiverNetworkState); + } } private void updateTableOfContents() { @@ -950,13 +976,14 @@ public class MainActivity extends BaseActivity implements WebViewCallback, openExternalUrl(intentSupportKiwix); case R.id.menu_wifi_hotspot: - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - toggleHotspot(); - } else { - if (showWritePermissionSettings()) { //request permission and if already granted switch hotspot. - switchHotspot(); - } - } + startServerDialog(); + //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // toggleHotspot(); + //} else { + // if (showWritePermissionSettings()) { //request permission and if already granted switch hotspot. + // switchHotspot(); + // } + //} default: break; @@ -965,6 +992,136 @@ public class MainActivity extends BaseActivity implements WebViewCallback, return super.onOptionsItemSelected(item); } + private boolean startAndroidWebServer() { + if (!isStarted) { + port = getPortFromEditText(); + try { + if (port == 0) { + throw new Exception(); + } + + webServer = new WebServer(port); + webServer.start(); + //dialog.dismiss(); + return true; + } catch (Exception e) { + e.printStackTrace(); + Snackbar.make(coordinatorLayout, + "The PORT " + port + " doesn't work, please change it between 1000 and 9999.", + Snackbar.LENGTH_LONG).show(); + } + } + return false; + } + + private int getPortFromEditText() { + String valueEditText = editTextPort.getText().toString(); + return (valueEditText.length() > 0) ? Integer.parseInt(valueEditText) : DEFAULT_PORT; + } + + private void setIpAccess() { + textViewIpAccess.setText(getIpAddress()); + } + + // get Ip address of the device's wireless access point i.e. wifi hotspot OR wifi network + private String getIpAddress() { + Log.v("DANG", "Inside getIpAdress()"); + String ip = ""; + try { + Enumeration enumNetworkInterfaces = NetworkInterface + .getNetworkInterfaces(); + while (enumNetworkInterfaces.hasMoreElements()) { + NetworkInterface networkInterface = enumNetworkInterfaces + .nextElement(); + Enumeration enumInetAddress = networkInterface + .getInetAddresses(); + while (enumInetAddress.hasMoreElements()) { + InetAddress inetAddress = enumInetAddress.nextElement(); + + if (inetAddress.isSiteLocalAddress()) { + ip += inetAddress.getHostAddress() + "\n"; + } + } + } + } catch (SocketException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + ip += "Something Wrong! " + e.toString() + "\n"; + } + Log.v("DANG", "Returning : " + "http://" + ip); + return "http://" + ip; + } + + private void initBroadcastReceiverNetworkStateChanged() { + final IntentFilter filters = new IntentFilter(); + filters.addAction("android.net.wifi.WIFI_STATE_CHANGED"); + filters.addAction("android.net.wifi.STATE_CHANGE"); + broadcastReceiverNetworkState = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + setIpAccess(); + } + }; + super.registerReceiver(broadcastReceiverNetworkState, filters); + } + + private boolean stopAndroidWebServer() { + if (isStarted && webServer != null) { + webServer.stop(); + return true; + } + return false; + } + + private void startServerDialog() { + AlertDialog.Builder alert = new AlertDialog.Builder(this); + alert.setTitle("Start the server"); + alert.setMessage("Happy sharing"); + + LinearLayout layout = new LinearLayout(this); + layout.setOrientation(LinearLayout.HORIZONTAL); + + textViewIpAccess = new TextView(this); + textViewIpAccess.setText("http://000.000.000.000"); + textViewIpAccess.setTextSize(20); + layout.addView(textViewIpAccess); + + TextView colonTextView = new TextView(this); + colonTextView.setTextSize(20); + colonTextView.setText(":"); + layout.addView(colonTextView); + + editTextPort = new EditText(this); + editTextPort.setInputType(InputType.TYPE_CLASS_NUMBER); + editTextPort.setHint(R.string.port_hint); + editTextPort.setText(R.string.port_hint); + editTextPort.setFilters(new InputFilter[] { new InputFilter.LengthFilter(4) }); + editTextPort.setTextSize(20); + layout.addView(editTextPort); + + alert.setView(layout); + + alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + if (!isStarted && startAndroidWebServer()) { + isStarted = true; + editTextPort.setEnabled(false); + } else if (stopAndroidWebServer()) { + isStarted = false; + editTextPort.setEnabled(true); + } + } + }); + + alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + // Canceled. + } + }); + alert.show(); + + setIpAccess(); + } /** Dialog to take user confirmation before deleting all notes */ private void showClearAllNotesDialog() { @@ -1456,6 +1613,23 @@ public class MainActivity extends BaseActivity implements WebViewCallback, } else { finish(); } + + //Code for webserver + if (isStarted) { + new android.app.AlertDialog.Builder(this) + .setTitle("WARNING") + .setMessage("You've already a server running") + .setPositiveButton(getResources().getString(android.R.string.ok), + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + finish(); + } + }) + .setNegativeButton(getResources().getString(android.R.string.cancel), null) + .show(); + } else { + finish(); + } return true; case KeyEvent.KEYCODE_MENU: openOptionsMenu(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3ff3b8d01..ed5aac8e2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -30,6 +30,7 @@ Following are the details of your local hotspot. SSID : Pass : + 8080 Warning: Mobile data enabled You\'re about to turn on your wifi hotspot. This feature can work without data usage. Do you want to disable your data? From 45d876b656ff058dc67644b0fafba10814ff075e Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sat, 20 Jul 2019 02:50:43 +0500 Subject: [PATCH 038/266] Add WebServerHelper class Shift server related methods from MainActivity to here. Call methods of this class from HotspotService dealing with server. --- app/src/main/AndroidManifest.xml | 3 + .../kiwix/kiwixmobile/main/MainActivity.java | 168 ++---------------- .../webserver/WebServerHelper.java | 155 ++++++++++++++++ .../wifi_hotspot/HotspotService.java | 13 +- .../wifi_hotspot/WifiHotspotManager.java | 2 +- 5 files changed, 189 insertions(+), 152 deletions(-) create mode 100644 app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 866a151f6..b52ea2ea3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -61,6 +61,9 @@ android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>
+ + bookmarks = new ArrayList<>(); private final List webViewList = new ArrayList<>(); private Intent serviceIntent; - private WebServer webServer; - private static final int DEFAULT_PORT = 8080; private BroadcastReceiver broadcastReceiverNetworkState; - private static boolean isStarted = false; - private CoordinatorLayout coordinatorLayout; - private EditText editTextPort; - private TextView textViewIpAccess; - private int port; + public static WebServerHelper webServerHelper; @BindView(R.id.activity_main_root) ConstraintLayout root; @BindView(R.id.activity_main_toolbar) @@ -789,12 +775,11 @@ public class MainActivity extends BaseActivity implements WebViewCallback, // TODO create a base Activity class that class this. FileUtils.deleteCachedFiles(this); tts.shutdown(); - - stopAndroidWebServer(); + webServerHelper.stopAndroidWebServer(); isStarted = false; - if (broadcastReceiverNetworkState != null) { - unregisterReceiver(broadcastReceiverNetworkState); - } + //if (broadcastReceiverNetworkState != null) { + // unregisterReceiver(broadcastReceiverNetworkState); + //} } private void updateTableOfContents() { @@ -976,14 +961,13 @@ public class MainActivity extends BaseActivity implements WebViewCallback, openExternalUrl(intentSupportKiwix); case R.id.menu_wifi_hotspot: - startServerDialog(); - //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - // toggleHotspot(); - //} else { - // if (showWritePermissionSettings()) { //request permission and if already granted switch hotspot. - // switchHotspot(); - // } - //} + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + toggleHotspot(); + } else { + if (showWritePermissionSettings()) { //request permission and if already granted switch hotspot. + switchHotspot(); + } + } default: break; @@ -992,65 +976,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, return super.onOptionsItemSelected(item); } - private boolean startAndroidWebServer() { - if (!isStarted) { - port = getPortFromEditText(); - try { - if (port == 0) { - throw new Exception(); - } - - webServer = new WebServer(port); - webServer.start(); - //dialog.dismiss(); - return true; - } catch (Exception e) { - e.printStackTrace(); - Snackbar.make(coordinatorLayout, - "The PORT " + port + " doesn't work, please change it between 1000 and 9999.", - Snackbar.LENGTH_LONG).show(); - } - } - return false; - } - - private int getPortFromEditText() { - String valueEditText = editTextPort.getText().toString(); - return (valueEditText.length() > 0) ? Integer.parseInt(valueEditText) : DEFAULT_PORT; - } - - private void setIpAccess() { - textViewIpAccess.setText(getIpAddress()); - } - - // get Ip address of the device's wireless access point i.e. wifi hotspot OR wifi network - private String getIpAddress() { - Log.v("DANG", "Inside getIpAdress()"); - String ip = ""; - try { - Enumeration enumNetworkInterfaces = NetworkInterface - .getNetworkInterfaces(); - while (enumNetworkInterfaces.hasMoreElements()) { - NetworkInterface networkInterface = enumNetworkInterfaces - .nextElement(); - Enumeration enumInetAddress = networkInterface - .getInetAddresses(); - while (enumInetAddress.hasMoreElements()) { - InetAddress inetAddress = enumInetAddress.nextElement(); - - if (inetAddress.isSiteLocalAddress()) { - ip += inetAddress.getHostAddress() + "\n"; - } - } - } - } catch (SocketException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - ip += "Something Wrong! " + e.toString() + "\n"; - } - Log.v("DANG", "Returning : " + "http://" + ip); - return "http://" + ip; - } private void initBroadcastReceiverNetworkStateChanged() { final IntentFilter filters = new IntentFilter(); @@ -1059,69 +984,12 @@ public class MainActivity extends BaseActivity implements WebViewCallback, broadcastReceiverNetworkState = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - setIpAccess(); + //setIpAccess(); } }; super.registerReceiver(broadcastReceiverNetworkState, filters); } - private boolean stopAndroidWebServer() { - if (isStarted && webServer != null) { - webServer.stop(); - return true; - } - return false; - } - - private void startServerDialog() { - AlertDialog.Builder alert = new AlertDialog.Builder(this); - alert.setTitle("Start the server"); - alert.setMessage("Happy sharing"); - - LinearLayout layout = new LinearLayout(this); - layout.setOrientation(LinearLayout.HORIZONTAL); - - textViewIpAccess = new TextView(this); - textViewIpAccess.setText("http://000.000.000.000"); - textViewIpAccess.setTextSize(20); - layout.addView(textViewIpAccess); - - TextView colonTextView = new TextView(this); - colonTextView.setTextSize(20); - colonTextView.setText(":"); - layout.addView(colonTextView); - - editTextPort = new EditText(this); - editTextPort.setInputType(InputType.TYPE_CLASS_NUMBER); - editTextPort.setHint(R.string.port_hint); - editTextPort.setText(R.string.port_hint); - editTextPort.setFilters(new InputFilter[] { new InputFilter.LengthFilter(4) }); - editTextPort.setTextSize(20); - layout.addView(editTextPort); - - alert.setView(layout); - - alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - if (!isStarted && startAndroidWebServer()) { - isStarted = true; - editTextPort.setEnabled(false); - } else if (stopAndroidWebServer()) { - isStarted = false; - editTextPort.setEnabled(true); - } - } - }); - - alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - // Canceled. - } - }); - alert.show(); - - setIpAccess(); - } /** Dialog to take user confirmation before deleting all notes */ private void showClearAllNotesDialog() { @@ -1243,9 +1111,9 @@ public class MainActivity extends BaseActivity implements WebViewCallback, startService(ACTION_TURN_OFF_BEFORE_O); } else { if (isMobileDataEnabled(this)) { - mobileDataDialog(); } else { + webServerHelper = new WebServerHelper(this); startService(ACTION_TURN_ON_BEFORE_O); } } @@ -2497,7 +2365,9 @@ public class MainActivity extends BaseActivity implements WebViewCallback, // requests here. //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + //webServerHelper = new WebServerHelper(this); startService(ACTION_TURN_ON_AFTER_O); + //} } catch (ApiException exception) { switch (exception.getStatusCode()) { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java new file mode 100644 index 000000000..08ff51451 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -0,0 +1,155 @@ +package org.kiwix.kiwixmobile.webserver; + +import android.app.Activity; +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; +import android.text.InputFilter; +import android.text.InputType; +import android.util.Log; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.TextView; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import com.google.android.material.snackbar.Snackbar; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Enumeration; +import org.kiwix.kiwixmobile.R; + +public class WebServerHelper extends Activity { + private static Context context; + private static TextView textViewIpAccess; + private static EditText editTextPort; + public static boolean isStarted; + private static int port; + private static WebServer webServer; + private static final int DEFAULT_PORT = 8080; + private static CoordinatorLayout coordinatorLayout; + +public WebServerHelper(Context context) +{ + this.context = context; +} + public static void startServerDialog() { + AlertDialog.Builder alert = new AlertDialog.Builder(context); + alert.setTitle("Start the server"); + alert.setMessage("Happy sharing"); + + LinearLayout layout = new LinearLayout(context); + layout.setOrientation(LinearLayout.HORIZONTAL); + + textViewIpAccess = new TextView(context); + textViewIpAccess.setText("http://000.000.000.000"); + textViewIpAccess.setTextSize(20); + layout.addView(textViewIpAccess); + + TextView colonTextView = new TextView(context); + colonTextView.setTextSize(20); + colonTextView.setText(":"); + layout.addView(colonTextView); + + editTextPort = new EditText(context); + editTextPort.setInputType(InputType.TYPE_CLASS_NUMBER); + editTextPort.setHint(R.string.port_hint); + editTextPort.setText(R.string.port_hint); + editTextPort.setFilters(new InputFilter[] { new InputFilter.LengthFilter(4) }); + editTextPort.setTextSize(20); + layout.addView(editTextPort); + + alert.setView(layout); + + alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + if (!isStarted && startAndroidWebServer()) { + isStarted = true; + editTextPort.setEnabled(false); + } else if (stopAndroidWebServer()) { + isStarted = false; + editTextPort.setEnabled(true); + } + } + }); + + alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + // Canceled. + } + }); + stopAndroidWebServer(); + isStarted = false; + alert.show(); + + setIpAccess(); + } + + public static boolean stopAndroidWebServer() { + if (isStarted && webServer != null) { + webServer.stop(); + return true; + } + return false; + } + + private static boolean startAndroidWebServer() { + if (!isStarted) { + port = getPortFromEditText(); + try { + if (port == 0) { + throw new Exception(); + } + webServer = new WebServer(port); + webServer.start(); + return true; + } catch (Exception e) { + e.printStackTrace(); + Snackbar.make(coordinatorLayout, + "The PORT " + port + " doesn't work, please change it between 1000 and 9999.", + Snackbar.LENGTH_LONG).show(); + } + } + return false; + } + + private static int getPortFromEditText() { + String valueEditText = editTextPort.getText().toString(); + return (valueEditText.length() > 0) ? Integer.parseInt(valueEditText) : DEFAULT_PORT; + } + + private static void setIpAccess() { + textViewIpAccess.setText(getIpAddress()); + } + + // get Ip address of the device's wireless access point i.e. wifi hotspot OR wifi network + private static String getIpAddress() { + Log.v("DANG", "Inside getIpAdress()"); + String ip = ""; + try { + Enumeration enumNetworkInterfaces = NetworkInterface + .getNetworkInterfaces(); + while (enumNetworkInterfaces.hasMoreElements()) { + NetworkInterface networkInterface = enumNetworkInterfaces + .nextElement(); + Enumeration enumInetAddress = networkInterface + .getInetAddresses(); + while (enumInetAddress.hasMoreElements()) { + InetAddress inetAddress = enumInetAddress.nextElement(); + + if (inetAddress.isSiteLocalAddress()) { + ip += inetAddress.getHostAddress() + "\n"; + } + } + } + } catch (SocketException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + ip += "Something Wrong! " + e.toString() + "\n"; + } + Log.v("DANG", "Returning : " + "http://" + ip); + return "http://" + ip; + } + +} diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index 2a9c98e8a..6cf6b4f73 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -10,6 +10,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Build; +import android.os.Handler; import android.os.IBinder; import android.util.Log; import androidx.annotation.Nullable; @@ -23,7 +24,7 @@ import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_AFTER_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_BEFORE_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_AFTER_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_BEFORE_O; -import static org.kiwix.kiwixmobile.main.MainActivity.startHotspotDetails; +import static org.kiwix.kiwixmobile.webserver.WebServerHelper.startServerDialog; /** * HotspotService is used to add a foreground service for the wifi hotspot. @@ -61,7 +62,15 @@ public class HotspotService extends Service { switch (intent.getAction()) { case ACTION_TURN_ON_BEFORE_O: if (hotspotManager.setWifiEnabled(null, true)) { - startHotspotDetails(); + final Handler handler = new Handler(); + handler.postDelayed(new Runnable() { + @Override + public void run() { + startServerDialog(); + } + }, 6000); + + //startHotspotDetails(); updateNotification(getString(R.string.hotspot_running), true); } break; diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index cbd172fed..8ffcf6ac6 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -65,7 +65,7 @@ public class WifiHotspotManager { + " \n SSID is : " + currentConfig.SSID); - hotspotDetailsDialog(); + //hotspotDetailsDialog(); oreoenabled = true; } From 5d15bed4d28f0ebea7687246ed0e73f2ac2dc678 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sat, 20 Jul 2019 02:59:43 +0500 Subject: [PATCH 039/266] Clean and Reformat code in WebServerHelper and HotspotService --- .../kiwix/kiwixmobile/webserver/WebServerHelper.java | 11 ++++------- .../kiwixmobile/wifi_hotspot/HotspotService.java | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index 08ff51451..e3a0febbc 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -3,14 +3,12 @@ package org.kiwix.kiwixmobile.webserver; import android.app.Activity; import android.content.Context; import android.content.DialogInterface; -import android.os.Bundle; import android.text.InputFilter; import android.text.InputType; import android.util.Log; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; -import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.coordinatorlayout.widget.CoordinatorLayout; import com.google.android.material.snackbar.Snackbar; @@ -30,10 +28,10 @@ public class WebServerHelper extends Activity { private static final int DEFAULT_PORT = 8080; private static CoordinatorLayout coordinatorLayout; -public WebServerHelper(Context context) -{ - this.context = context; -} + public WebServerHelper(Context context) { + this.context = context; + } + public static void startServerDialog() { AlertDialog.Builder alert = new AlertDialog.Builder(context); alert.setTitle("Start the server"); @@ -151,5 +149,4 @@ public WebServerHelper(Context context) Log.v("DANG", "Returning : " + "http://" + ip); return "http://" + ip; } - } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index 6cf6b4f73..00aa424c0 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -103,7 +103,7 @@ public class HotspotService extends Service { } private Notification buildForegroundNotification(String status, boolean showStopButton) { - Log.v("DANG","Building notification "+status); + Log.v("DANG", "Building notification " + status); builder = new NotificationCompat.Builder(this); builder.setContentTitle("Kiwix Hotspot").setContentText(status); Intent targetIntent = new Intent(this, MainActivity.class); From f7d063b7e0d2cd362928a0312789bb68bd6c9a4f Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sat, 20 Jul 2019 03:15:17 +0500 Subject: [PATCH 040/266] Remove network change broadcast receiver Because no intent filter available/working for wifi hotspot ap. --- .../kiwix/kiwixmobile/main/MainActivity.java | 17 ----------------- 1 file changed, 17 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 e04730fb8..9eaf0a539 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -29,7 +29,6 @@ import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import android.content.IntentFilter; import android.content.IntentSender; import android.content.SharedPreferences; import android.content.pm.PackageManager; @@ -416,8 +415,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, wifiHotspotManager = new WifiHotspotManager(this); serviceIntent = new Intent(this, HotspotService.class); - - //initBroadcastReceiverNetworkStateChanged(); } //End of onCreate @@ -976,20 +973,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, return super.onOptionsItemSelected(item); } - - private void initBroadcastReceiverNetworkStateChanged() { - final IntentFilter filters = new IntentFilter(); - filters.addAction("android.net.wifi.WIFI_STATE_CHANGED"); - filters.addAction("android.net.wifi.STATE_CHANGE"); - broadcastReceiverNetworkState = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - //setIpAccess(); - } - }; - super.registerReceiver(broadcastReceiverNetworkState, filters); - } - /** Dialog to take user confirmation before deleting all notes */ private void showClearAllNotesDialog() { From ba4230f17c12699cb4b7d82c99fc358aef76ed0a Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sun, 21 Jul 2019 03:37:15 +0500 Subject: [PATCH 041/266] Start webserver for devices >=Oreo --- .../org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index 8ffcf6ac6..dc025afe4 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -12,6 +12,7 @@ import java.lang.reflect.Method; import org.kiwix.kiwixmobile.R; import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; +import static org.kiwix.kiwixmobile.webserver.WebServerHelper.startServerDialog; /** * WifiHotstopManager class makes use of the Android's WifiManager and WifiConfiguration class @@ -65,6 +66,8 @@ public class WifiHotspotManager { + " \n SSID is : " + currentConfig.SSID); + startServerDialog(); + //hotspotDetailsDialog(); oreoenabled = true; From ffed7c2e64fd8ea2dbb0681b6ab7835dad5b6ec6 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sun, 21 Jul 2019 03:42:14 +0500 Subject: [PATCH 042/266] Stop webserver when hotspot is stopped --- .../kiwixmobile/wifi_hotspot/HotspotService.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index 00aa424c0..f791257ff 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -25,6 +25,7 @@ import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_BEFORE_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_AFTER_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_BEFORE_O; import static org.kiwix.kiwixmobile.webserver.WebServerHelper.startServerDialog; +import static org.kiwix.kiwixmobile.webserver.WebServerHelper.stopAndroidWebServer; /** * HotspotService is used to add a foreground service for the wifi hotspot. @@ -81,9 +82,11 @@ public class HotspotService extends Service { } break; case ACTION_TURN_OFF_BEFORE_O: - hotspotManager.setWifiEnabled(null, false); - stopForeground(true); - stopSelf(); + if (hotspotManager.setWifiEnabled(null, false)) { + stopForeground(true); + stopSelf(); + stopAndroidWebServer(); + } break; case ACTION_TURN_OFF_AFTER_O: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -91,6 +94,7 @@ public class HotspotService extends Service { } stopForeground(true); stopSelf(); + stopAndroidWebServer(); break; default: break; @@ -138,6 +142,7 @@ public class HotspotService extends Service { } stopForeground(true); stopSelf(); + stopAndroidWebServer(); } @Override From 5d08156b1ee0d3097045be38f416dc45d5e9b4c3 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sun, 21 Jul 2019 04:13:31 +0500 Subject: [PATCH 043/266] Refactor code in WebServerHelper Remove unnecessary code. --- .../org/kiwix/kiwixmobile/webserver/WebServerHelper.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index e3a0febbc..c2de74044 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -65,9 +65,6 @@ public class WebServerHelper extends Activity { if (!isStarted && startAndroidWebServer()) { isStarted = true; editTextPort.setEnabled(false); - } else if (stopAndroidWebServer()) { - isStarted = false; - editTextPort.setEnabled(true); } } }); @@ -77,8 +74,7 @@ public class WebServerHelper extends Activity { // Canceled. } }); - stopAndroidWebServer(); - isStarted = false; + alert.show(); setIpAccess(); @@ -87,6 +83,7 @@ public class WebServerHelper extends Activity { public static boolean stopAndroidWebServer() { if (isStarted && webServer != null) { webServer.stop(); + isStarted = false; return true; } return false; From e784200649ccaff269e7c18db8fbae2c0ee37a8a Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sun, 21 Jul 2019 05:21:06 +0500 Subject: [PATCH 044/266] Start service from mobile data dialog response --- app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 9eaf0a539..30685057e 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -1116,7 +1116,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback, 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.setNegativeButton(android.R.string.no, + (dialog, id) -> startService(ACTION_TURN_ON_BEFORE_O)); builder.setTitle(this.getString(R.string.mobile_data_enabled)); builder.setMessage( this.getString(R.string.mobile_data_message) + "\n" + this.getString( From 2e0da70bf4a56cba45ea693bff88e91db4a7629f Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 26 Jul 2019 17:19:29 +0500 Subject: [PATCH 045/266] Resolve hotspot details dialog bug --- .../kiwix/kiwixmobile/main/MainActivity.java | 2 +- .../kiwixmobile/webserver/WebServerHelper.java | 5 ++--- .../wifi_hotspot/HotspotService.java | 17 +++++++++-------- .../wifi_hotspot/WifiHotspotManager.java | 6 ++---- 4 files changed, 14 insertions(+), 16 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 30685057e..07a919777 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -1096,7 +1096,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, if (isMobileDataEnabled(this)) { mobileDataDialog(); } else { - webServerHelper = new WebServerHelper(this); + webServerHelper = new WebServerHelper(getApplicationContext()); startService(ACTION_TURN_ON_BEFORE_O); } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index c2de74044..34fce49b1 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -1,6 +1,5 @@ package org.kiwix.kiwixmobile.webserver; -import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.text.InputFilter; @@ -18,7 +17,7 @@ import java.net.SocketException; import java.util.Enumeration; import org.kiwix.kiwixmobile.R; -public class WebServerHelper extends Activity { +public class WebServerHelper { private static Context context; private static TextView textViewIpAccess; private static EditText editTextPort; @@ -32,7 +31,7 @@ public class WebServerHelper extends Activity { this.context = context; } - public static void startServerDialog() { + public static void startServerDialog(Context context) { AlertDialog.Builder alert = new AlertDialog.Builder(context); alert.setTitle("Start the server"); alert.setMessage("Happy sharing"); diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index f791257ff..02ba9c83c 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -24,6 +24,7 @@ import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_AFTER_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_BEFORE_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_AFTER_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_BEFORE_O; +import static org.kiwix.kiwixmobile.main.MainActivity.startHotspotDetails; import static org.kiwix.kiwixmobile.webserver.WebServerHelper.startServerDialog; import static org.kiwix.kiwixmobile.webserver.WebServerHelper.stopAndroidWebServer; @@ -63,15 +64,15 @@ public class HotspotService extends Service { switch (intent.getAction()) { case ACTION_TURN_ON_BEFORE_O: if (hotspotManager.setWifiEnabled(null, true)) { - final Handler handler = new Handler(); - handler.postDelayed(new Runnable() { - @Override - public void run() { - startServerDialog(); - } - }, 6000); + //final Handler handler = new Handler(); + //handler.postDelayed(new Runnable() { + // @Override + // public void run() { + // startServerDialog(); + // } + //}, 6000); - //startHotspotDetails(); + startHotspotDetails(); updateNotification(getString(R.string.hotspot_running), true); } break; diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index dc025afe4..4f2c87fd9 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -66,9 +66,7 @@ public class WifiHotspotManager { + " \n SSID is : " + currentConfig.SSID); - startServerDialog(); - - //hotspotDetailsDialog(); + hotspotDetailsDialog(); oreoenabled = true; } @@ -157,7 +155,7 @@ public class WifiHotspotManager { AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle()); builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { - //Do nothing + startServerDialog(context); }); builder.setTitle(context.getString(R.string.hotspot_turned_on)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { From eefeaed069e62e4848a350d9a31d0b03563afb25 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 26 Jul 2019 17:45:56 +0500 Subject: [PATCH 046/266] Add delay before start server dialog for devices < O --- .../wifi_hotspot/HotspotService.java | 8 -------- .../wifi_hotspot/WifiHotspotManager.java | 19 ++++++++++++++++--- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index 02ba9c83c..d20fc3b89 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -64,14 +64,6 @@ public class HotspotService extends Service { switch (intent.getAction()) { case ACTION_TURN_ON_BEFORE_O: if (hotspotManager.setWifiEnabled(null, true)) { - //final Handler handler = new Handler(); - //handler.postDelayed(new Runnable() { - // @Override - // public void run() { - // startServerDialog(); - // } - //}, 6000); - startHotspotDetails(); updateNotification(getString(R.string.hotspot_running), true); } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index 4f2c87fd9..6bc504e37 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -154,9 +154,22 @@ public class WifiHotspotManager { AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle()); - builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { - startServerDialog(context); - }); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { + startServerDialog(context); + }); + } else { + builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { + final Handler handler = new Handler(); + handler.postDelayed(new Runnable() { + @Override + public void run() { + startServerDialog(context); + } + }, 6000); + }); + } + builder.setTitle(context.getString(R.string.hotspot_turned_on)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder.setMessage( From 48d9978f6adf27b1fdbde5fd90467213bf6b419c Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 26 Jul 2019 18:16:09 +0500 Subject: [PATCH 047/266] Resolve lint warnings in WebServerHelper.java --- .../kiwix/kiwixmobile/main/MainActivity.java | 8 ++----- .../webserver/WebServerHelper.java | 24 +++++++++---------- .../wifi_hotspot/HotspotService.java | 2 -- .../wifi_hotspot/WifiHotspotManager.java | 7 +++--- app/src/main/res/values/strings.xml | 1 + 5 files changed, 19 insertions(+), 23 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 07a919777..e241f822a 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -125,7 +125,6 @@ import org.kiwix.kiwixmobile.utils.LanguageUtils; import org.kiwix.kiwixmobile.utils.NetworkUtils; import org.kiwix.kiwixmobile.utils.StyleUtils; import org.kiwix.kiwixmobile.utils.files.FileUtils; -import org.kiwix.kiwixmobile.webserver.WebServerHelper; import org.kiwix.kiwixmobile.wifi_hotspot.HotspotService; import org.kiwix.kiwixmobile.wifi_hotspot.WifiHotspotManager; import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity; @@ -172,6 +171,7 @@ import static org.kiwix.kiwixmobile.utils.LanguageUtils.getResourceString; import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; import static org.kiwix.kiwixmobile.utils.UpdateUtils.reformatProviderUrl; import static org.kiwix.kiwixmobile.webserver.WebServerHelper.isStarted; +import static org.kiwix.kiwixmobile.webserver.WebServerHelper.stopAndroidWebServer; import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.checkHotspotState; public class MainActivity extends BaseActivity implements WebViewCallback, @@ -194,7 +194,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private final List webViewList = new ArrayList<>(); private Intent serviceIntent; private BroadcastReceiver broadcastReceiverNetworkState; - public static WebServerHelper webServerHelper; @BindView(R.id.activity_main_root) ConstraintLayout root; @BindView(R.id.activity_main_toolbar) @@ -772,7 +771,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, // TODO create a base Activity class that class this. FileUtils.deleteCachedFiles(this); tts.shutdown(); - webServerHelper.stopAndroidWebServer(); + stopAndroidWebServer(); isStarted = false; //if (broadcastReceiverNetworkState != null) { // unregisterReceiver(broadcastReceiverNetworkState); @@ -1096,7 +1095,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, if (isMobileDataEnabled(this)) { mobileDataDialog(); } else { - webServerHelper = new WebServerHelper(getApplicationContext()); startService(ACTION_TURN_ON_BEFORE_O); } } @@ -2348,8 +2346,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, // All location settings are satisfied. The client can initialize location // requests here. - //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - //webServerHelper = new WebServerHelper(this); startService(ACTION_TURN_ON_AFTER_O); //} diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index 34fce49b1..cd044d1f3 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -18,20 +18,20 @@ import java.util.Enumeration; import org.kiwix.kiwixmobile.R; public class WebServerHelper { - private static Context context; - private static TextView textViewIpAccess; - private static EditText editTextPort; + private Context context; + private TextView textViewIpAccess; + private EditText editTextPort; public static boolean isStarted; - private static int port; + private int port; private static WebServer webServer; - private static final int DEFAULT_PORT = 8080; - private static CoordinatorLayout coordinatorLayout; + private final int DEFAULT_PORT = 8080; + private CoordinatorLayout coordinatorLayout; public WebServerHelper(Context context) { this.context = context; } - public static void startServerDialog(Context context) { + public void startServerDialog() { AlertDialog.Builder alert = new AlertDialog.Builder(context); alert.setTitle("Start the server"); alert.setMessage("Happy sharing"); @@ -40,7 +40,7 @@ public class WebServerHelper { layout.setOrientation(LinearLayout.HORIZONTAL); textViewIpAccess = new TextView(context); - textViewIpAccess.setText("http://000.000.000.000"); + textViewIpAccess.setText(context.getString(R.string.sample_ip_address)); textViewIpAccess.setTextSize(20); layout.addView(textViewIpAccess); @@ -88,7 +88,7 @@ public class WebServerHelper { return false; } - private static boolean startAndroidWebServer() { + private boolean startAndroidWebServer() { if (!isStarted) { port = getPortFromEditText(); try { @@ -108,17 +108,17 @@ public class WebServerHelper { return false; } - private static int getPortFromEditText() { + private int getPortFromEditText() { String valueEditText = editTextPort.getText().toString(); return (valueEditText.length() > 0) ? Integer.parseInt(valueEditText) : DEFAULT_PORT; } - private static void setIpAccess() { + private void setIpAccess() { textViewIpAccess.setText(getIpAddress()); } // get Ip address of the device's wireless access point i.e. wifi hotspot OR wifi network - private static String getIpAddress() { + private String getIpAddress() { Log.v("DANG", "Inside getIpAdress()"); String ip = ""; try { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index d20fc3b89..8d0990c29 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -10,7 +10,6 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Build; -import android.os.Handler; import android.os.IBinder; import android.util.Log; import androidx.annotation.Nullable; @@ -25,7 +24,6 @@ import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_BEFORE_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_AFTER_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_BEFORE_O; import static org.kiwix.kiwixmobile.main.MainActivity.startHotspotDetails; -import static org.kiwix.kiwixmobile.webserver.WebServerHelper.startServerDialog; import static org.kiwix.kiwixmobile.webserver.WebServerHelper.stopAndroidWebServer; /** diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index 6bc504e37..dc17b8dc3 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -10,9 +10,9 @@ import androidx.annotation.RequiresApi; import androidx.appcompat.app.AlertDialog; import java.lang.reflect.Method; import org.kiwix.kiwixmobile.R; +import org.kiwix.kiwixmobile.webserver.WebServerHelper; import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; -import static org.kiwix.kiwixmobile.webserver.WebServerHelper.startServerDialog; /** * WifiHotstopManager class makes use of the Android's WifiManager and WifiConfiguration class @@ -153,10 +153,11 @@ public class WifiHotspotManager { public void hotspotDetailsDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle()); + WebServerHelper webServerHelper = new WebServerHelper(context); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { - startServerDialog(context); + webServerHelper.startServerDialog(); }); } else { builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { @@ -164,7 +165,7 @@ public class WifiHotspotManager { handler.postDelayed(new Runnable() { @Override public void run() { - startServerDialog(context); + webServerHelper.startServerDialog(); } }, 6000); }); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ed5aac8e2..38945bcc8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -34,6 +34,7 @@ Warning: Mobile data enabled You\'re about to turn on your wifi hotspot. This feature can work without data usage. Do you want to disable your data? + http://000.000.000.000 Error: The selected ZIM file could not be found. Error: The selected file is not a valid ZIM file. Error: Loading article (Url: %1$s) failed. From e15f398f9faa14f7b3f01c6019e76d6a8d37cb88 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 26 Jul 2019 18:44:38 +0500 Subject: [PATCH 048/266] Server started successfully dialog --- .../webserver/WebServerHelper.java | 20 ++++++++++++++++++- app/src/main/res/values/strings.xml | 2 ++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index cd044d1f3..9dae6ce33 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -2,6 +2,8 @@ package org.kiwix.kiwixmobile.webserver; import android.content.Context; import android.content.DialogInterface; +import android.os.Build; +import android.os.Handler; import android.text.InputFilter; import android.text.InputType; import android.util.Log; @@ -17,6 +19,8 @@ import java.net.SocketException; import java.util.Enumeration; import org.kiwix.kiwixmobile.R; +import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; + public class WebServerHelper { private Context context; private TextView textViewIpAccess; @@ -63,7 +67,7 @@ public class WebServerHelper { public void onClick(DialogInterface dialog, int whichButton) { if (!isStarted && startAndroidWebServer()) { isStarted = true; - editTextPort.setEnabled(false); + serverStartedDialog(); } } }); @@ -145,4 +149,18 @@ public class WebServerHelper { Log.v("DANG", "Returning : " + "http://" + ip); return "http://" + ip; } + + public void serverStartedDialog() { + + AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle()); + WebServerHelper webServerHelper = new WebServerHelper(context); + builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { + + }); + builder.setTitle(context.getString(R.string.server_started_title)); + builder.setMessage( + context.getString(R.string.server_started_message) + "\n " + getIpAddress() + ":" + port); + AlertDialog dialog = builder.create(); + dialog.show(); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 38945bcc8..dd7894bf8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -35,6 +35,8 @@ You\'re about to turn on your wifi hotspot. This feature can work without data usage. Do you want to disable your data? http://000.000.000.000 + Server started + Enter this ip address into your browser to access the server Error: The selected ZIM file could not be found. Error: The selected file is not a valid ZIM file. Error: Loading article (Url: %1$s) failed. From e71f3b4341e4995969a948dae751e400741ad9d3 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 26 Jul 2019 19:20:40 +0500 Subject: [PATCH 049/266] Hotspot details dialog !cancelable --- .../org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index dc17b8dc3..d745b3ff7 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -184,6 +184,7 @@ public class WifiHotspotManager { R.string.hotspot_ssid_label) + " " + currentConfig.SSID + "\n" + context.getString( R.string.hotspot_pass_label) + " " + currentConfig.preSharedKey); } + builder.setCancelable(false); AlertDialog dialog = builder.create(); dialog.show(); } From 45208f64a712fa647b82c5ef981e7a5acf235828 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sun, 28 Jul 2019 15:19:21 +0500 Subject: [PATCH 050/266] Fix Hotspot Ip address For Android pie --- .../kiwixmobile/webserver/WebServerHelper.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index 9dae6ce33..16d5c7ba3 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -141,12 +141,21 @@ public class WebServerHelper { } } } + if (ip.length() > 14) { + for (int i = 15, j = 12; i < 18; i++, j++) { + if ((ip.charAt(i) == '.')) { + ip = ip.substring(0, j + 1); //from first char to 12 + break; + } + } + } } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); ip += "Something Wrong! " + e.toString() + "\n"; - } - Log.v("DANG", "Returning : " + "http://" + ip); + }; + + Log.v("DANG", "Returning : " + "http://" + ip) return "http://" + ip; } From bff90479a57603250bcf0d618a54e27378ef360f Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sun, 28 Jul 2019 16:36:38 +0500 Subject: [PATCH 051/266] Hosting HTML files using the HTTP server --- .../kiwixmobile/webserver/WebServer.java | 31 +++++++++++++------ .../webserver/WebServerHelper.java | 2 +- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java index fea26ceb9..85653b27b 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java @@ -1,23 +1,36 @@ package org.kiwix.kiwixmobile.webserver; +import android.util.Log; import fi.iki.elonen.NanoHTTPD; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; import java.util.Map; public class WebServer extends NanoHTTPD { + private String selectedFilePath; - public WebServer(int port) { + public WebServer(int port,String selectedFilePath) { super(port); + this.selectedFilePath = selectedFilePath; } @Override public Response serve(IHTTPSession session) { - String msg = "

Hello server

\n"; - Map parms = session.getParms(); - if (parms.get("username") == null) { - msg += "
\n

Your name:

\n" + "
\n"; - } else { - msg += "

Hello, " + parms.get("username") + "!

"; + String answer = ""; + try { + FileReader index = new FileReader(selectedFilePath); + BufferedReader reader = new BufferedReader(index); + String line = ""; + while ((line = reader.readLine()) != null) { + answer += line; + } + reader.close(); + + } catch(IOException ioe) { + Log.w("Httpd", ioe.toString()); } - return newFixedLengthResponse( msg + "\n" ); + + + return newFixedLengthResponse(answer); } -} diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index 16d5c7ba3..81a6a34e5 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -155,7 +155,7 @@ public class WebServerHelper { ip += "Something Wrong! " + e.toString() + "\n"; }; - Log.v("DANG", "Returning : " + "http://" + ip) + Log.v("DANG", "Returning : " + "http://" + ip); return "http://" + ip; } From ade89401107087e163b9197a6c33d104b8a78ef7 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sun, 28 Jul 2019 16:48:44 +0500 Subject: [PATCH 052/266] Read main page of ZIM File and write on storage --- .../kiwix/kiwixmobile/main/MainActivity.java | 40 ++++++++++++++++--- .../kiwixmobile/webserver/WebServer.java | 8 ++-- 2 files changed, 37 insertions(+), 11 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 e241f822a..f35d257a2 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -100,6 +100,9 @@ import com.google.android.material.snackbar.Snackbar; import io.reactivex.android.schedulers.AndroidSchedulers; import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; import java.text.SimpleDateFormat; import java.lang.reflect.Method; import java.util.ArrayList; @@ -183,6 +186,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback, public static final String ACTION_TURN_OFF_BEFORE_O = "Turn_aff_hotspot_before_oreo"; public static final String ACTION_TURN_ON_AFTER_O = "Turn_on_hotspot_after_oreo"; public static final String ACTION_TURN_OFF_AFTER_O = "Turn_off_hotspot_after_oreo"; + public static final String MAIN_PAGE_STORAGE_PATH = + "/storage/emulated/0/Download/outputDang.html"; public static boolean isFullscreenOpened; public static boolean refresh; public static boolean wifiOnly; @@ -957,14 +962,15 @@ public class MainActivity extends BaseActivity implements WebViewCallback, openExternalUrl(intentSupportKiwix); case R.id.menu_wifi_hotspot: - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - toggleHotspot(); - } else { - if (showWritePermissionSettings()) { //request permission and if already granted switch hotspot. - switchHotspot(); + if (readHtmlPage()) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + toggleHotspot(); + } else { + if (showWritePermissionSettings()) { //request permission and if already granted switch hotspot. + switchHotspot(); + } } } - default: break; } @@ -972,6 +978,28 @@ public class MainActivity extends BaseActivity implements WebViewCallback, return super.onOptionsItemSelected(item); } + private boolean readHtmlPage() { + String url = + Uri.parse(ZimContentProvider.CONTENT_URI + ZimContentProvider.getMainPage()).toString(); + Uri uri = Uri.parse(url); + try { + InputStream in = getContentResolver().openInputStream(uri); + OutputStream out = + new FileOutputStream(new File(MAIN_PAGE_STORAGE_PATH)); + byte[] buf = new byte[1024]; + int len; + while ((len = in.read(buf)) > 0) { + out.write(buf, 0, len); + } + out.close(); + in.close(); + return true; + } catch (Exception e) { + Log.v("DANG", e.toString()); + } + return false; + } + /** Dialog to take user confirmation before deleting all notes */ private void showClearAllNotesDialog() { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java index 85653b27b..7c8c1defd 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java @@ -5,12 +5,11 @@ import fi.iki.elonen.NanoHTTPD; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; -import java.util.Map; public class WebServer extends NanoHTTPD { private String selectedFilePath; - public WebServer(int port,String selectedFilePath) { + public WebServer(int port, String selectedFilePath) { super(port); this.selectedFilePath = selectedFilePath; } @@ -26,11 +25,10 @@ public class WebServer extends NanoHTTPD { answer += line; } reader.close(); - - } catch(IOException ioe) { + } catch (IOException ioe) { Log.w("Httpd", ioe.toString()); } - return newFixedLengthResponse(answer); } +} From 31f0dbe3899179224424af959561d6ac13c4b62d Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sun, 28 Jul 2019 16:50:42 +0500 Subject: [PATCH 053/266] Send main page path to the server --- .../java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index 81a6a34e5..9ad210e7b 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -19,6 +19,7 @@ import java.net.SocketException; import java.util.Enumeration; import org.kiwix.kiwixmobile.R; +import static org.kiwix.kiwixmobile.main.MainActivity.MAIN_PAGE_STORAGE_PATH; import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; public class WebServerHelper { @@ -99,7 +100,7 @@ public class WebServerHelper { if (port == 0) { throw new Exception(); } - webServer = new WebServer(port); + webServer = new WebServer(port, MAIN_PAGE_STORAGE_PATH); webServer.start(); return true; } catch (Exception e) { From e4a309c32436747cd14ad13be7f37fb4079e71d1 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 1 Aug 2019 17:23:47 +0500 Subject: [PATCH 054/266] Add documentation in WebServerHelper.java --- .../org/kiwix/kiwixmobile/webserver/WebServerHelper.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index 9ad210e7b..deb6de681 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -36,6 +36,7 @@ public class WebServerHelper { this.context = context; } + //Dialog to start the server where user is shown the hotspot ip address can edit the port no. public void startServerDialog() { AlertDialog.Builder alert = new AlertDialog.Builder(context); alert.setTitle("Start the server"); @@ -142,10 +143,11 @@ public class WebServerHelper { } } } + //To remove extra characters from IP for Android Pie if (ip.length() > 14) { for (int i = 15, j = 12; i < 18; i++, j++) { if ((ip.charAt(i) == '.')) { - ip = ip.substring(0, j + 1); //from first char to 12 + ip = ip.substring(0, j + 1); break; } } @@ -160,6 +162,7 @@ public class WebServerHelper { return "http://" + ip; } + //Once server is started successfully, this dialog is shown. public void serverStartedDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle()); From eef8ae2cd875e2eb9546c41681d278e368a81071 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 1 Aug 2019 17:25:06 +0500 Subject: [PATCH 055/266] Remove unused imports in WebServerHelper.java --- .../java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index deb6de681..1144d395a 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -2,8 +2,6 @@ package org.kiwix.kiwixmobile.webserver; import android.content.Context; import android.content.DialogInterface; -import android.os.Build; -import android.os.Handler; import android.text.InputFilter; import android.text.InputType; import android.util.Log; From 37b96c267099cd6934e6f813308cdc06d557fdaf Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 1 Aug 2019 17:27:59 +0500 Subject: [PATCH 056/266] Clean code in MainActivity Remove unnnecessary comments --- app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java | 3 --- 1 file changed, 3 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 f35d257a2..acbd3ec04 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -778,9 +778,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, tts.shutdown(); stopAndroidWebServer(); isStarted = false; - //if (broadcastReceiverNetworkState != null) { - // unregisterReceiver(broadcastReceiverNetworkState); - //} } private void updateTableOfContents() { From c3a77d3c91bafc84ea673f2e2715e44c80022f4d Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 1 Aug 2019 17:35:41 +0500 Subject: [PATCH 057/266] Use onRequestPermissionResult for ACCESS_FINE_LOCATION --- .../kiwix/kiwixmobile/main/MainActivity.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 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 acbd3ec04..064602c0a 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -188,6 +188,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, public static final String ACTION_TURN_OFF_AFTER_O = "Turn_off_hotspot_after_oreo"; public static final String MAIN_PAGE_STORAGE_PATH = "/storage/emulated/0/Download/outputDang.html"; + private static final int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 102; public static boolean isFullscreenOpened; public static boolean refresh; public static boolean wifiOnly; @@ -1153,7 +1154,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, @RequiresApi(api = VERSION_CODES.O) private void toggleHotspot() { - boolean check = false; //Check if location permissions are granted if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { @@ -1165,20 +1165,10 @@ public class MainActivity extends BaseActivity implements WebViewCallback, setupLocationServices(); } } else { - //This var makes sure recursion occurs only once. - if (!check) { - //Show rationale and request location permission. - //No explanation needed; request the permission - int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 102; + //Ask location permission if not granted ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, MY_PERMISSIONS_ACCESS_FINE_LOCATION); - - check = true; - - //Go to toggle hotspot to check if permission is granted. - toggleHotspot(); - } } } @@ -1377,6 +1367,15 @@ public class MainActivity extends BaseActivity implements WebViewCallback, break; } + case MY_PERMISSIONS_ACCESS_FINE_LOCATION: { + if (grantResults.length > 0 + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + toggleHotspot(); + } + } + } + case REQUEST_READ_STORAGE_PERMISSION: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { From 336d4b3d99d628a52a878d98c4fb6e8159a67c8f Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 1 Aug 2019 17:42:12 +0500 Subject: [PATCH 058/266] Add credits in WebServerHelper.java --- .../org/kiwix/kiwixmobile/webserver/WebServerHelper.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index 1144d395a..3f3098b88 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -20,6 +20,12 @@ import org.kiwix.kiwixmobile.R; import static org.kiwix.kiwixmobile.main.MainActivity.MAIN_PAGE_STORAGE_PATH; import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; +/** + * WebServerHelper class is used to set up the suitable environment i.e. getting the + * ip address and port no. before starting the WebServer + * Created by Adeel Zafar on 18/07/2019. + */ + public class WebServerHelper { private Context context; private TextView textViewIpAccess; From 995b20ce14eae37a85920cdd04d9937596b85122 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 1 Aug 2019 23:21:59 +0500 Subject: [PATCH 059/266] Remove hotspot code for API<26 from MainActivity.java Removing the code for turning on hotspot programmatically for API<26. --- .../kiwix/kiwixmobile/main/MainActivity.java | 41 +------------------ 1 file changed, 1 insertion(+), 40 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 064602c0a..78feeae11 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -182,8 +182,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private static final String NEW_TAB = "NEW_TAB"; private static final String HOME_URL = "file:///android_asset/home.html"; - public static final String ACTION_TURN_ON_BEFORE_O = "Turn_on_hotspot_before_oreo"; - public static final String ACTION_TURN_OFF_BEFORE_O = "Turn_aff_hotspot_before_oreo"; public static final String ACTION_TURN_ON_AFTER_O = "Turn_on_hotspot_after_oreo"; public static final String ACTION_TURN_OFF_AFTER_O = "Turn_off_hotspot_after_oreo"; public static final String MAIN_PAGE_STORAGE_PATH = @@ -193,7 +191,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, public static boolean refresh; public static boolean wifiOnly; public static boolean nightMode; - private static WifiHotspotManager wifiHotspotManager; private static Uri KIWIX_LOCAL_MARKET_URI; private static Uri KIWIX_BROWSER_MARKET_URI; private final ArrayList bookmarks = new ArrayList<>(); @@ -964,9 +961,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { toggleHotspot(); } else { - if (showWritePermissionSettings()) { //request permission and if already granted switch hotspot. - switchHotspot(); - } + //TO DO: show Dialog() + within that add check mobile Data check later. } } default: @@ -1112,24 +1107,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, Toast.makeText(this, stringResource, duration).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()) { - startService(ACTION_TURN_OFF_BEFORE_O); - } else { - if (isMobileDataEnabled(this)) { - mobileDataDialog(); - } else { - startService(ACTION_TURN_ON_BEFORE_O); - } - } - } - - public static void startHotspotDetails() { - wifiHotspotManager.hotspotDetailsDialog(); - } - private void startService(String ACTION) { serviceIntent.setAction(ACTION); this.startService(serviceIntent); @@ -1172,22 +1149,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, } } - //To get write permission settings, we use this method. - private boolean showWritePermissionSettings() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M - && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { - if (!Settings.System.canWrite(this)) { - Log.v("DANG", " " + !Settings.System.canWrite(this)); - Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS); - intent.setData(Uri.parse("package:" + this.getPackageName())); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - this.startActivity(intent); - return false; - } - } - return true; //Permission already given - } - @SuppressWarnings("SameReturnValue") @OnLongClick(R.id.bottom_toolbar_bookmark) boolean goToBookmarks() { From 543175d7a601bcbd37d5611270e207d1bb5f102f Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 1 Aug 2019 23:28:53 +0500 Subject: [PATCH 060/266] Remove hotspot code for API<26 from WifiHotspotManager.java Remove the code for turning on hotspot programmatically for API<26. --- .../wifi_hotspot/WifiHotspotManager.java | 87 +------------------ 1 file changed, 1 insertion(+), 86 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index d745b3ff7..80c2e9b63 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -8,7 +8,6 @@ import android.os.Handler; import android.util.Log; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AlertDialog; -import java.lang.reflect.Method; import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.webserver.WebServerHelper; @@ -32,23 +31,6 @@ public class WifiHotspotManager { wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE); } - // This method enables/disables the wifi access point - // It is used for API<26 - public boolean setWifiEnabled(WifiConfiguration wifiConfig, boolean enabled) { - try { - if (enabled) { //disables wifi hotspot if it's already enabled - wifiManager.setWifiEnabled(false); - } - - Method method = wifiManager.getClass() - .getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); - return (Boolean) method.invoke(wifiManager, wifiConfig, enabled); - } catch (Exception e) { - Log.e(this.getClass().toString(), "", e); - return false; - } - } - //Workaround to turn on hotspot for Oreo versions @RequiresApi(api = Build.VERSION_CODES.O) public void turnOnHotspot() { @@ -103,87 +85,20 @@ public class WifiHotspotManager { return hotspotReservation != null; } - // This method returns the current state of the Wifi access point - private WIFI_AP_STATE_ENUMS getWifiApState() { - try { - Method method = wifiManager.getClass().getMethod("getWifiApState"); - - int tmp = ((Integer) method.invoke(wifiManager)); - - // Fix for Android 4 - if (tmp >= 10) { - tmp = tmp - 10; - } - - return WIFI_AP_STATE_ENUMS.class.getEnumConstants()[tmp]; - } catch (Exception e) { - Log.e(this.getClass().toString(), "", e); - return WIFI_AP_STATE_ENUMS.WIFI_AP_STATE_FAILED; - } - } - - //This method returns if wifi access point is enabled or not - public boolean isWifiApEnabled() { - return getWifiApState() == WIFI_AP_STATE_ENUMS.WIFI_AP_STATE_ENABLED; - } - - //This method is to get the wifi ap configuration - private WifiConfiguration getWifiApConfiguration() { - try { - Method method = wifiManager.getClass().getMethod("getWifiApConfiguration"); - return (WifiConfiguration) method.invoke(wifiManager); - } catch (Exception e) { - Log.e(this.getClass().toString(), "", e); - return null; - } - } - - //This method is to set the wifi ap configuration - public boolean setWifiApConfiguration(WifiConfiguration wifiConfig) { - try { - Method method = - wifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class); - return (Boolean) method.invoke(wifiManager, wifiConfig); - } catch (Exception e) { - Log.e(this.getClass().toString(), "", e); - return false; - } - } - public void hotspotDetailsDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle()); WebServerHelper webServerHelper = new WebServerHelper(context); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { webServerHelper.startServerDialog(); }); - } else { - builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { - final Handler handler = new Handler(); - handler.postDelayed(new Runnable() { - @Override - public void run() { - webServerHelper.startServerDialog(); - } - }, 6000); - }); - } builder.setTitle(context.getString(R.string.hotspot_turned_on)); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder.setMessage( context.getString(R.string.hotspot_details_message) + "\n" + context.getString( R.string.hotspot_ssid_label) + " " + currentConfig.SSID + "\n" + context.getString( R.string.hotspot_pass_label) + " " + currentConfig.preSharedKey); - } else { - currentConfig = getWifiApConfiguration(); - builder.setMessage( - context.getString(R.string.hotspot_details_message) + "\n" + context.getString( - R.string.hotspot_ssid_label) + " " + currentConfig.SSID + "\n" + context.getString( - R.string.hotspot_pass_label) + " " + currentConfig.preSharedKey); - } + builder.setCancelable(false); AlertDialog dialog = builder.create(); dialog.show(); From c1d6d9fd96136385c76d9374b4a5e9c66ffe57c9 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 1 Aug 2019 23:35:09 +0500 Subject: [PATCH 061/266] Refactor hotspot code for API<26 Remove hotspot code for API<26 from HotspotService.java Remove hotspot code for API<26 from WebServerHelper.java Remove the code for turning on hotspot programmatically for API<26. --- .../webserver/WebServerHelper.java | 1 - .../wifi_hotspot/HotspotService.java | 31 ++++--------------- 2 files changed, 6 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index 3f3098b88..ae604363d 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -170,7 +170,6 @@ public class WebServerHelper { public void serverStartedDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle()); - WebServerHelper webServerHelper = new WebServerHelper(context); builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { }); diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index 8d0990c29..ff1c0a72a 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -20,10 +20,7 @@ import org.kiwix.kiwixmobile.main.MainActivity; import org.kiwix.kiwixmobile.utils.Constants; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_AFTER_O; -import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_BEFORE_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_AFTER_O; -import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_BEFORE_O; -import static org.kiwix.kiwixmobile.main.MainActivity.startHotspotDetails; import static org.kiwix.kiwixmobile.webserver.WebServerHelper.stopAndroidWebServer; /** @@ -41,9 +38,7 @@ public class HotspotService extends Service { @Override public void onCreate() { super.onCreate(); - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { - hotspotManager = new WifiHotspotManager(this); - } + stopReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -60,25 +55,14 @@ public class HotspotService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { switch (intent.getAction()) { - case ACTION_TURN_ON_BEFORE_O: - if (hotspotManager.setWifiEnabled(null, true)) { - startHotspotDetails(); - updateNotification(getString(R.string.hotspot_running), true); - } - break; + case ACTION_TURN_ON_AFTER_O: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { hotspotManager.turnOnHotspot(); updateNotification(getString(R.string.hotspot_running), true); } break; - case ACTION_TURN_OFF_BEFORE_O: - if (hotspotManager.setWifiEnabled(null, false)) { - stopForeground(true); - stopSelf(); - stopAndroidWebServer(); - } - break; + case ACTION_TURN_OFF_AFTER_O: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { hotspotManager.turnOffHotspot(); @@ -108,9 +92,9 @@ public class HotspotService extends Service { builder.setContentIntent(contentIntent) .setSmallIcon(R.mipmap.kiwix_icon) .setWhen(System.currentTimeMillis()); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + hotspotNotificationChannel(); - } + if (showStopButton) { Intent stopIntent = new Intent(ACTION_STOP); PendingIntent stopHotspot = @@ -125,12 +109,9 @@ public class HotspotService extends Service { buildForegroundNotification(status, stopAction)); } + @RequiresApi(Build.VERSION_CODES.O) private void stopHotspot() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { hotspotManager.turnOffHotspot(); - } else { - hotspotManager.setWifiEnabled(null, false); - } stopForeground(true); stopSelf(); stopAndroidWebServer(); From 9b891b0b219f268cc4e4c57b49abf57225c56dfd Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 1 Aug 2019 23:37:25 +0500 Subject: [PATCH 062/266] Remove WIFI_AP_STATE_ENUMS Remove the code for turning on hotspot programmatically for API<26. --- .../kiwixmobile/wifi_hotspot/WIFI_AP_STATE_ENUMS.java | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WIFI_AP_STATE_ENUMS.java diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WIFI_AP_STATE_ENUMS.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WIFI_AP_STATE_ENUMS.java deleted file mode 100644 index c09be1e12..000000000 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WIFI_AP_STATE_ENUMS.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.kiwix.kiwixmobile.wifi_hotspot; - -/** - * WIFI_AP_STATE_ENUMS class is used to get current state of the wifi access point using enums. - * Created by Adeel Zafar on 28/5/2019. - */ - -public enum WIFI_AP_STATE_ENUMS { - WIFI_AP_STATE_DISABLING, WIFI_AP_STATE_DISABLED, WIFI_AP_STATE_ENABLING, WIFI_AP_STATE_ENABLED, WIFI_AP_STATE_FAILED -} From bf46fe28b1734964bfd6b29bce86fa243b69798c Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Thu, 1 Aug 2019 23:39:13 +0500 Subject: [PATCH 063/266] Refactor code in MainActivity.java Remove unncessary initialization of WifiHotspotManager --- .../main/java/org/kiwix/kiwixmobile/main/MainActivity.java | 7 ++----- 1 file changed, 2 insertions(+), 5 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 78feeae11..aaf10bd3a 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -129,7 +129,6 @@ import org.kiwix.kiwixmobile.utils.NetworkUtils; import org.kiwix.kiwixmobile.utils.StyleUtils; import org.kiwix.kiwixmobile.utils.files.FileUtils; import org.kiwix.kiwixmobile.wifi_hotspot.HotspotService; -import org.kiwix.kiwixmobile.wifi_hotspot.WifiHotspotManager; import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity; import org.kiwix.kiwixmobile.zim_manager.fileselect_view.StorageObserver; import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BookOnDiskDelegate; @@ -414,8 +413,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, new ItemTouchHelper(tabCallback).attachToRecyclerView(tabRecyclerView); drawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); - wifiHotspotManager = new WifiHotspotManager(this); - serviceIntent = new Intent(this, HotspotService.class); } @@ -1117,8 +1114,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback, 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) -> startService(ACTION_TURN_ON_BEFORE_O)); + builder.setNegativeButton((android.R.string.no), (dialog, id) -> { + }); builder.setTitle(this.getString(R.string.mobile_data_enabled)); builder.setMessage( this.getString(R.string.mobile_data_message) + "\n" + this.getString( From be95e23c055b7bc18d050790edd763d5dd1fae85 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 01:21:07 +0500 Subject: [PATCH 064/266] Dialog to turn on hotspot manually for API<26 --- .../kiwix/kiwixmobile/main/MainActivity.java | 20 +++++++++++++++++++ app/src/main/res/values/strings.xml | 4 ++++ 2 files changed, 24 insertions(+) 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 aaf10bd3a..82333f75d 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -959,6 +959,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, toggleHotspot(); } else { //TO DO: show Dialog() + within that add check mobile Data check later. + startHotspotDialog(); } } default: @@ -1109,6 +1110,25 @@ public class MainActivity extends BaseActivity implements WebViewCallback, this.startService(serviceIntent); } + //Advice user to turn on hotspot manually for API<26 + private void startHotspotDialog() { + AlertDialog.Builder builder = new AlertDialog.Builder(this, dialogStyle()); + + builder.setPositiveButton(getString(R.string.hotspot_dialog_positive_button), (dialog, id) -> { + }); + + builder.setNeutralButton(getString(R.string.hotspot_dialog_neutral_button), (dialog, id) -> { + //TO DO: START SERVER WITHIN THE SERVICE. + }); + + builder.setTitle(getString(R.string.hotspot_dialog_title)); + builder.setMessage( + getString(R.string.hotspot_dialog_message) + ); + AlertDialog dialog = builder.create(); + dialog.show(); + } + private void mobileDataDialog() { if (Build.VERSION.SDK_INT < VERSION_CODES.O) { AlertDialog.Builder builder = new AlertDialog.Builder(this, dialogStyle()); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dd7894bf8..1a3fe09be 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -34,6 +34,10 @@ Warning: Mobile data enabled You\'re about to turn on your wifi hotspot. This feature can work without data usage. Do you want to disable your data? + Turn on your hotspot + In order for this feature to work you need to turn on your hotspot first. + YES, I’VE TURNED IT ON + Okay http://000.000.000.000 Server started Enter this ip address into your browser to access the server From e41589af166d96764844b33fa9e6d7ebf2f24732 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 02:15:01 +0500 Subject: [PATCH 065/266] Start webserver after turning on hotspot manually --- app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java | 3 +++ 1 file changed, 3 insertions(+) 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 82333f75d..f9a3b8590 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -128,6 +128,7 @@ import org.kiwix.kiwixmobile.utils.LanguageUtils; import org.kiwix.kiwixmobile.utils.NetworkUtils; import org.kiwix.kiwixmobile.utils.StyleUtils; import org.kiwix.kiwixmobile.utils.files.FileUtils; +import org.kiwix.kiwixmobile.webserver.WebServerHelper; import org.kiwix.kiwixmobile.wifi_hotspot.HotspotService; import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity; import org.kiwix.kiwixmobile.zim_manager.fileselect_view.StorageObserver; @@ -1119,6 +1120,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback, builder.setNeutralButton(getString(R.string.hotspot_dialog_neutral_button), (dialog, id) -> { //TO DO: START SERVER WITHIN THE SERVICE. + WebServerHelper webServerHelper = new WebServerHelper(this); + webServerHelper.startServerDialog(); }); builder.setTitle(getString(R.string.hotspot_dialog_title)); From 9b04bea2b1afc62a1379e8835ee0725180eb2e4d Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 02:18:03 +0500 Subject: [PATCH 066/266] Check if mobile data is enabled for API<26 --- .../main/java/org/kiwix/kiwixmobile/main/MainActivity.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 f9a3b8590..d03957fa8 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -960,7 +960,11 @@ public class MainActivity extends BaseActivity implements WebViewCallback, toggleHotspot(); } else { //TO DO: show Dialog() + within that add check mobile Data check later. - startHotspotDialog(); + if (isMobileDataEnabled(this)) { + mobileDataDialog(); + } else { + startHotspotDialog(); + } } } default: @@ -1138,6 +1142,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, builder.setPositiveButton(this.getString(R.string.yes), (dialog, id) -> disableMobileData()); builder.setNegativeButton((android.R.string.no), (dialog, id) -> { + startHotspotDialog(); }); builder.setTitle(this.getString(R.string.mobile_data_enabled)); builder.setMessage( From 5f5a312c74b23efca16e989ed333cdcb841f01d3 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 02:19:46 +0500 Subject: [PATCH 067/266] Make mobile data dialog !cancelable --- app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java | 1 + 1 file changed, 1 insertion(+) 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 d03957fa8..1239d8c1b 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -1149,6 +1149,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, this.getString(R.string.mobile_data_message) + "\n" + this.getString( R.string.mobile_data_message_confirmation) ); + builder.setCancelable(false); AlertDialog dialog = builder.create(); dialog.show(); } From 2aa0af31486c760eed66fbfdb3ec7cae7810abdc Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 02:23:38 +0500 Subject: [PATCH 068/266] Delete readHtml() from MainActivity.java --- .../kiwix/kiwixmobile/main/MainActivity.java | 24 ------------------- 1 file changed, 24 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 1239d8c1b..fd49203d4 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -955,7 +955,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, openExternalUrl(intentSupportKiwix); case R.id.menu_wifi_hotspot: - if (readHtmlPage()) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { toggleHotspot(); } else { @@ -966,7 +965,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, startHotspotDialog(); } } - } default: break; } @@ -974,28 +972,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, return super.onOptionsItemSelected(item); } - private boolean readHtmlPage() { - String url = - Uri.parse(ZimContentProvider.CONTENT_URI + ZimContentProvider.getMainPage()).toString(); - Uri uri = Uri.parse(url); - try { - InputStream in = getContentResolver().openInputStream(uri); - OutputStream out = - new FileOutputStream(new File(MAIN_PAGE_STORAGE_PATH)); - byte[] buf = new byte[1024]; - int len; - while ((len = in.read(buf)) > 0) { - out.write(buf, 0, len); - } - out.close(); - in.close(); - return true; - } catch (Exception e) { - Log.v("DANG", e.toString()); - } - return false; - } - /** Dialog to take user confirmation before deleting all notes */ private void showClearAllNotesDialog() { From b459886e9dc38fe72237733ed52ba079717ae99c Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 02:28:03 +0500 Subject: [PATCH 069/266] Reverting main page of ZIM webserver Replace it with a simple hello world webserver. --- .../kiwix/kiwixmobile/main/MainActivity.java | 2 -- .../kiwixmobile/webserver/WebServer.java | 30 +++++++------------ .../webserver/WebServerHelper.java | 3 +- 3 files changed, 12 insertions(+), 23 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 fd49203d4..a14a549f9 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -184,8 +184,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private static final String HOME_URL = "file:///android_asset/home.html"; public static final String ACTION_TURN_ON_AFTER_O = "Turn_on_hotspot_after_oreo"; public static final String ACTION_TURN_OFF_AFTER_O = "Turn_off_hotspot_after_oreo"; - public static final String MAIN_PAGE_STORAGE_PATH = - "/storage/emulated/0/Download/outputDang.html"; private static final int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 102; public static boolean isFullscreenOpened; public static boolean refresh; diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java index 7c8c1defd..ca018cfe7 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java @@ -2,33 +2,25 @@ package org.kiwix.kiwixmobile.webserver; import android.util.Log; import fi.iki.elonen.NanoHTTPD; -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; +import java.util.Map; public class WebServer extends NanoHTTPD { - private String selectedFilePath; - public WebServer(int port, String selectedFilePath) { + public WebServer(int port) { super(port); - this.selectedFilePath = selectedFilePath; } @Override public Response serve(IHTTPSession session) { - String answer = ""; - try { - FileReader index = new FileReader(selectedFilePath); - BufferedReader reader = new BufferedReader(index); - String line = ""; - while ((line = reader.readLine()) != null) { - answer += line; - } - reader.close(); - } catch (IOException ioe) { - Log.w("Httpd", ioe.toString()); + String msg = "

Hello server

\n"; + Map parms = session.getParms(); + if (parms.get("username") == null) { + msg += + "
\n

Your name:

\n" + + "
\n"; + } else { + msg += "

Hello, " + parms.get("username") + "!

"; } - - return newFixedLengthResponse(answer); + return newFixedLengthResponse(msg + "\n"); } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index ae604363d..d3351e7d3 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -17,7 +17,6 @@ import java.net.SocketException; import java.util.Enumeration; import org.kiwix.kiwixmobile.R; -import static org.kiwix.kiwixmobile.main.MainActivity.MAIN_PAGE_STORAGE_PATH; import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; /** @@ -105,7 +104,7 @@ public class WebServerHelper { if (port == 0) { throw new Exception(); } - webServer = new WebServer(port, MAIN_PAGE_STORAGE_PATH); + webServer = new WebServer(port); webServer.start(); return true; } catch (Exception e) { From e45f36d29e50353942694b74b718befb03d4b74a Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 02:55:27 +0500 Subject: [PATCH 070/266] Add handler before startServerDialog --- .../java/org/kiwix/kiwixmobile/main/MainActivity.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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 a14a549f9..74bf970e4 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -1099,7 +1099,15 @@ public class MainActivity extends BaseActivity implements WebViewCallback, builder.setNeutralButton(getString(R.string.hotspot_dialog_neutral_button), (dialog, id) -> { //TO DO: START SERVER WITHIN THE SERVICE. WebServerHelper webServerHelper = new WebServerHelper(this); - webServerHelper.startServerDialog(); + //Adding a handler because sometimes hotspot can take time to turn on. + //TO DO: Add a progress dialog instead of handler + final Handler handler = new Handler(); + handler.postDelayed(new Runnable() { + @Override + public void run() { + webServerHelper.startServerDialog(); + } + }, 5000); }); builder.setTitle(getString(R.string.hotspot_dialog_title)); From 2bdff5aba6cb34557ce65c3308fcd4ed13730437 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 03:04:32 +0500 Subject: [PATCH 071/266] Resolve lint warnings in HotspotService.java --- .../wifi_hotspot/HotspotService.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index ff1c0a72a..180809451 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -39,14 +39,16 @@ public class HotspotService extends Service { @Override public void onCreate() { super.onCreate(); - stopReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - if (intent != null && intent.getAction().equals(ACTION_STOP)) { - stopHotspot(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + stopReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (intent != null && intent.getAction().equals(ACTION_STOP)) { + stopHotspot(); + } } - } - }; + }; + } registerReceiver(stopReceiver, new IntentFilter(ACTION_STOP)); notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); startForeground(HOTSPOT_NOTIFICATION_ID, @@ -110,7 +112,7 @@ public class HotspotService extends Service { } @RequiresApi(Build.VERSION_CODES.O) - private void stopHotspot() { + void stopHotspot() { hotspotManager.turnOffHotspot(); stopForeground(true); stopSelf(); From 7b8fe6e98d81dd7981808f39a43607cae2d52770 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 03:17:46 +0500 Subject: [PATCH 072/266] Resolve lint warnings in WifiHotspotManager.java --- .../kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index 80c2e9b63..b0d835f55 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -22,9 +22,9 @@ import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; public class WifiHotspotManager { private WifiManager wifiManager; private Context context; - private WifiManager.LocalOnlyHotspotReservation hotspotReservation; - private boolean oreoenabled = false; - private WifiConfiguration currentConfig; + WifiManager.LocalOnlyHotspotReservation hotspotReservation; + boolean oreoenabled = false; + WifiConfiguration currentConfig; public WifiHotspotManager(Context context) { this.context = context; From da53308ce5ef23f4c0a1461de3eb9284bc0b6aa5 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 03:18:25 +0500 Subject: [PATCH 073/266] Resolve lint warning in WebServerHelper.java --- .../java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index d3351e7d3..a9a957acc 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -97,7 +97,7 @@ public class WebServerHelper { return false; } - private boolean startAndroidWebServer() { + boolean startAndroidWebServer() { if (!isStarted) { port = getPortFromEditText(); try { From da78f12c5b30a87cbf3b9a3753afd43bfdfb8f94 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 03:19:21 +0500 Subject: [PATCH 074/266] Refactor code in WebServer.java Remove unused import --- app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java index ca018cfe7..65b3a896d 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServer.java @@ -1,6 +1,5 @@ package org.kiwix.kiwixmobile.webserver; -import android.util.Log; import fi.iki.elonen.NanoHTTPD; import java.util.Map; From 3cf2cf7f3180333e02d46b4dac560cb8d4d314c7 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 03:20:43 +0500 Subject: [PATCH 075/266] Refactor code in MainActivity.java Remove unused imports --- .../main/java/org/kiwix/kiwixmobile/main/MainActivity.java | 5 ----- 1 file changed, 5 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 74bf970e4..73b129543 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -24,7 +24,6 @@ import android.annotation.SuppressLint; import android.app.Activity; import android.appwidget.AppWidgetManager; import android.content.ActivityNotFoundException; -import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; @@ -100,9 +99,6 @@ import com.google.android.material.snackbar.Snackbar; import io.reactivex.android.schedulers.AndroidSchedulers; import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.io.OutputStream; import java.text.SimpleDateFormat; import java.lang.reflect.Method; import java.util.ArrayList; @@ -194,7 +190,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, private final ArrayList bookmarks = new ArrayList<>(); private final List webViewList = new ArrayList<>(); private Intent serviceIntent; - private BroadcastReceiver broadcastReceiverNetworkState; @BindView(R.id.activity_main_root) ConstraintLayout root; @BindView(R.id.activity_main_toolbar) From f1438b17bd149eedfd5365510c6638da5e082438 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 03:26:02 +0500 Subject: [PATCH 076/266] Resolve java warnings in WebServerHelper.java --- .../org/kiwix/kiwixmobile/webserver/WebServerHelper.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index a9a957acc..3815db178 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -32,7 +32,6 @@ public class WebServerHelper { public static boolean isStarted; private int port; private static WebServer webServer; - private final int DEFAULT_PORT = 8080; private CoordinatorLayout coordinatorLayout; public WebServerHelper(Context context) { @@ -119,6 +118,7 @@ public class WebServerHelper { private int getPortFromEditText() { String valueEditText = editTextPort.getText().toString(); + int DEFAULT_PORT = 8080; return (valueEditText.length() > 0) ? Integer.parseInt(valueEditText) : DEFAULT_PORT; } @@ -159,14 +159,14 @@ public class WebServerHelper { // TODO Auto-generated catch block e.printStackTrace(); ip += "Something Wrong! " + e.toString() + "\n"; - }; + } Log.v("DANG", "Returning : " + "http://" + ip); return "http://" + ip; } //Once server is started successfully, this dialog is shown. - public void serverStartedDialog() { + void serverStartedDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle()); builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { From b3e17867c39fce2568945e595a748d749569a680 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 16:16:54 +0500 Subject: [PATCH 077/266] Initialize coordinatorlayout --- .../java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index 3815db178..30527f19f 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -47,6 +47,8 @@ public class WebServerHelper { LinearLayout layout = new LinearLayout(context); layout.setOrientation(LinearLayout.HORIZONTAL); + coordinatorLayout = new CoordinatorLayout(context); + textViewIpAccess = new TextView(context); textViewIpAccess.setText(context.getString(R.string.sample_ip_address)); textViewIpAccess.setTextSize(20); From 83cd114f8f61093e6f4e0494019a6ade14812f59 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 16:21:30 +0500 Subject: [PATCH 078/266] Refactor start server dialog --- .../org/kiwix/kiwixmobile/webserver/WebServerHelper.java | 8 ++++---- app/src/main/res/values/strings.xml | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index 30527f19f..f77675edd 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -41,8 +41,8 @@ public class WebServerHelper { //Dialog to start the server where user is shown the hotspot ip address can edit the port no. public void startServerDialog() { AlertDialog.Builder alert = new AlertDialog.Builder(context); - alert.setTitle("Start the server"); - alert.setMessage("Happy sharing"); + alert.setTitle(R.string.start_server_dialog_title); + alert.setMessage(R.string.start_server_dialog_message); LinearLayout layout = new LinearLayout(context); layout.setOrientation(LinearLayout.HORIZONTAL); @@ -69,7 +69,7 @@ public class WebServerHelper { alert.setView(layout); - alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { + alert.setPositiveButton("START SERVER", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { if (!isStarted && startAndroidWebServer()) { isStarted = true; @@ -78,7 +78,7 @@ public class WebServerHelper { } }); - alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Canceled. } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1a3fe09be..8b4af21d6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -31,6 +31,8 @@ SSID : Pass : 8080 + Start server + You can now start the server using this ip address and port no. Warning: Mobile data enabled You\'re about to turn on your wifi hotspot. This feature can work without data usage. Do you want to disable your data? From 485f156a77e1584b68190dd4d1e389ecb3a44627 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 18:44:06 +0500 Subject: [PATCH 079/266] Remove WRITE_SETTINGS_PERMISSION Removing the code for turning on hotspot programmatically for API<26. --- app/src/main/AndroidManifest.xml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b52ea2ea3..fbf52fa16 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -18,10 +18,6 @@ android:resizeable="true" android:smallScreens="true"/> - - From ff6107a1cf5cc608ef8d87c9c7e086971cb6e13e Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 2 Aug 2019 18:47:13 +0500 Subject: [PATCH 080/266] Remove WebServerHelper from Manifest --- app/src/main/AndroidManifest.xml | 4 +--- .../java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index fbf52fa16..dd6c27bc4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -56,10 +56,8 @@ -
- + Date: Fri, 2 Aug 2019 18:57:28 +0500 Subject: [PATCH 081/266] Remove COARSE_LOCATION_PERMISSION Its not required for startLocalOnlyHotspot --- app/src/main/AndroidManifest.xml | 1 - .../java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index dd6c27bc4..1701de5d0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -27,7 +27,6 @@ - diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index e1c4b505b..f77675edd 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -37,7 +37,7 @@ public class WebServerHelper { public WebServerHelper(Context context) { this.context = context; } - + //Dialog to start the server where user is shown the hotspot ip address can edit the port no. public void startServerDialog() { AlertDialog.Builder alert = new AlertDialog.Builder(context); From 5ae358c3d08b2a12e2de6483ae21f0b909afb72d Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Mon, 5 Aug 2019 22:24:29 +0500 Subject: [PATCH 082/266] Add StartServer activity Create activity_start_server layout Add textview and zim_list Add menu button Host Books in MainActivity Add values in strings.xml --- app/build.gradle | 9 +-- app/src/main/AndroidManifest.xml | 22 +++----- .../kiwix/kiwixmobile/main/MainActivity.java | 8 +++ .../kiwixmobile/webserver/StartServer.java | 34 ++++++++++++ .../main/res/layout/activity_start_server.xml | 55 +++++++++++++++++++ app/src/main/res/menu/menu_main.xml | 5 ++ app/src/main/res/values/strings.xml | 2 + 7 files changed, 118 insertions(+), 17 deletions(-) create mode 100644 app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java create mode 100644 app/src/main/res/layout/activity_start_server.xml diff --git a/app/build.gradle b/app/build.gradle index 2901ebb43..791d750cc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -69,6 +69,8 @@ dependencies { implementation "androidx.collection:collection-ktx:1.1.0" implementation "androidx.test.espresso:espresso-idling-resource:$espressoVersion" + implementation 'androidx.appcompat:appcompat:1.0.2' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' androidTestImplementation("androidx.test.espresso:espresso-core:$espressoVersion") androidTestImplementation "androidx.test.espresso:espresso-web:$espressoVersion" androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion" @@ -126,7 +128,6 @@ dependencies { // Leak canary debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-alpha-2' - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion" implementation "android.arch.lifecycle:extensions:1.1.1" implementation "io.objectbox:objectbox-kotlin:$objectboxVersion" @@ -253,8 +254,8 @@ android { 'DuplicateStrings', 'LogConditional' warning 'UnknownNullness', - 'SelectableText', - 'IconDensities' + 'SelectableText', + 'IconDensities' baseline file("lint-baseline.xml") } @@ -470,7 +471,7 @@ play { resolutionStrategy = "fail" } -ktlint{ +ktlint { android = true } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1701de5d0..e86b6c786 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,7 +10,6 @@ android:normalScreens="true" android:resizeable="true" android:smallScreens="true"/> - - - - - - - - + + + - + + - - + + @@ -215,7 +211,6 @@ android:resource="@xml/provider_paths"/> - @@ -231,4 +226,5 @@
-
+ + \ No newline at end of file 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 73b129543..068e89972 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/MainActivity.java @@ -124,6 +124,7 @@ import org.kiwix.kiwixmobile.utils.LanguageUtils; import org.kiwix.kiwixmobile.utils.NetworkUtils; import org.kiwix.kiwixmobile.utils.StyleUtils; import org.kiwix.kiwixmobile.utils.files.FileUtils; +import org.kiwix.kiwixmobile.webserver.StartServer; import org.kiwix.kiwixmobile.webserver.WebServerHelper; import org.kiwix.kiwixmobile.wifi_hotspot.HotspotService; import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity; @@ -946,6 +947,12 @@ public class MainActivity extends BaseActivity implements WebViewCallback, Intent intentSupportKiwix = new Intent(Intent.ACTION_VIEW, uriSupportKiwix); intentSupportKiwix.putExtra(EXTRA_EXTERNAL_LINK, true); openExternalUrl(intentSupportKiwix); + break; + + case R.id.menu_host_books: + Intent intent = new Intent(MainActivity.this, StartServer.class); + startActivity(intent); + break; case R.id.menu_wifi_hotspot: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -1911,6 +1918,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback, menu.findItem(R.id.menu_random_article).setVisible(false); menu.findItem(R.id.menu_searchintext).setVisible(false); menu.findItem(R.id.menu_wifi_hotspot).setVisible(false); + menu.findItem(R.id.menu_host_books).setVisible(true); } else { menu.findItem(R.id.menu_read_aloud).setVisible(true); menu.findItem(R.id.menu_home).setVisible(true); diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java new file mode 100644 index 000000000..5e2d35763 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java @@ -0,0 +1,34 @@ +package org.kiwix.kiwixmobile.webserver; + +import androidx.appcompat.app.AppCompatActivity; +import android.os.Bundle; +import androidx.appcompat.widget.Toolbar; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentTransaction; +import org.kiwix.kiwixmobile.R; +import org.kiwix.kiwixmobile.zim_manager.fileselect_view.ZimFileSelectFragment; + +public class StartServer extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_start_server); + FragmentManager fragmentManager = getSupportFragmentManager(); + FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); + ZimFileSelectFragment fragment = new ZimFileSelectFragment(); + fragmentTransaction.add(R.id.frameLayoutServer, fragment); + fragmentTransaction.commit(); + setUpToolbar(); + } + + private void setUpToolbar() { + Toolbar toolbar = findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + getSupportActionBar().setTitle(getString(R.string.menu_host_books)); + getSupportActionBar().setHomeButtonEnabled(true); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + toolbar.setNavigationOnClickListener(v -> onBackPressed()); + } +} diff --git a/app/src/main/res/layout/activity_start_server.xml b/app/src/main/res/layout/activity_start_server.xml new file mode 100644 index 000000000..77ec7b0b1 --- /dev/null +++ b/app/src/main/res/layout/activity_start_server.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index 0f7c5b1b4..794c46261 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -79,6 +79,11 @@ android:visible="false" app:showAsAction="never"/> + + Stop reading aloud Support Kiwix Wifi Hotspot + Host Books Save Media An error occurred when trying to save the media! Saved media as %s to Android/media/org.kiwix…/ @@ -31,6 +32,7 @@ SSID : Pass : 8080 + Select the files you wish to host on the server Start server You can now start the server using this ip address and port no. Warning: Mobile data enabled From 0265800170e1d5ae4a4a5ffd141015d7c5380564 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Mon, 5 Aug 2019 22:48:35 +0500 Subject: [PATCH 083/266] Minor refactoring to StartServer layout/activity_start_server --- .../java/org/kiwix/kiwixmobile/webserver/StartServer.java | 2 +- app/src/main/res/layout/activity_start_server.xml | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java index 5e2d35763..430965fc1 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java @@ -14,12 +14,12 @@ public class StartServer extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_start_server); + setUpToolbar(); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); ZimFileSelectFragment fragment = new ZimFileSelectFragment(); fragmentTransaction.add(R.id.frameLayoutServer, fragment); fragmentTransaction.commit(); - setUpToolbar(); } private void setUpToolbar() { diff --git a/app/src/main/res/layout/activity_start_server.xml b/app/src/main/res/layout/activity_start_server.xml index 77ec7b0b1..eacc77a22 100644 --- a/app/src/main/res/layout/activity_start_server.xml +++ b/app/src/main/res/layout/activity_start_server.xml @@ -5,6 +5,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" + android:fitsSystemWindows="true" android:id="@+id/constraintLayoutId" tools:context=".webserver.StartServer" > @@ -38,7 +39,7 @@ android:layout_marginTop="16dp" android:textAlignment="center" android:text="@string/server_textview_default_message" - app:layout_constraintTop_toBottomOf="@+id/appBarLayout2" + app:layout_constraintTop_toBottomOf="@+id/toolbar" /> + /> From 0eb8d3d3f935d85d0087d2f7e5abc0fa98926a56 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Mon, 5 Aug 2019 23:00:45 +0500 Subject: [PATCH 084/266] Add host books menu Add start server item Inflate menu in StartServer activity --- .../org/kiwix/kiwixmobile/webserver/StartServer.java | 6 ++++++ app/src/main/res/menu/menu_host_books.xml | 9 +++++++++ app/src/main/res/values/strings.xml | 1 + 3 files changed, 16 insertions(+) create mode 100644 app/src/main/res/menu/menu_host_books.xml diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java index 430965fc1..4f58e541a 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java @@ -1,5 +1,6 @@ package org.kiwix.kiwixmobile.webserver; +import android.view.Menu; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import androidx.appcompat.widget.Toolbar; @@ -31,4 +32,9 @@ public class StartServer extends AppCompatActivity { toolbar.setNavigationOnClickListener(v -> onBackPressed()); } + + @Override public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.menu_host_books, menu); + return super.onCreateOptionsMenu(menu); + } } diff --git a/app/src/main/res/menu/menu_host_books.xml b/app/src/main/res/menu/menu_host_books.xml new file mode 100644 index 000000000..fb388f902 --- /dev/null +++ b/app/src/main/res/menu/menu_host_books.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9a3ec779b..ce3b317d4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,6 +16,7 @@ Support Kiwix Wifi Hotspot Host Books + Start server Save Media An error occurred when trying to save the media! Saved media as %s to Android/media/org.kiwix…/ From 96c57fb691bd56b91d55c0f40e4223e9651f4048 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Mon, 5 Aug 2019 23:25:02 +0500 Subject: [PATCH 085/266] Revert "Add host books menu" This reverts commit 0eb8d3d3f935d85d0087d2f7e5abc0fa98926a56. --- .../org/kiwix/kiwixmobile/webserver/StartServer.java | 6 ------ app/src/main/res/menu/menu_host_books.xml | 9 --------- app/src/main/res/values/strings.xml | 1 - 3 files changed, 16 deletions(-) delete mode 100644 app/src/main/res/menu/menu_host_books.xml diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java index 4f58e541a..430965fc1 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java @@ -1,6 +1,5 @@ package org.kiwix.kiwixmobile.webserver; -import android.view.Menu; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import androidx.appcompat.widget.Toolbar; @@ -32,9 +31,4 @@ public class StartServer extends AppCompatActivity { toolbar.setNavigationOnClickListener(v -> onBackPressed()); } - - @Override public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.menu_host_books, menu); - return super.onCreateOptionsMenu(menu); - } } diff --git a/app/src/main/res/menu/menu_host_books.xml b/app/src/main/res/menu/menu_host_books.xml deleted file mode 100644 index fb388f902..000000000 --- a/app/src/main/res/menu/menu_host_books.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ce3b317d4..9a3ec779b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,7 +16,6 @@ Support Kiwix Wifi Hotspot Host Books - Start server Save Media An error occurred when trying to save the media! Saved media as %s to Android/media/org.kiwix…/ From 020073461f8eaac7e98cda26f4442d2477ea2b46 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Mon, 5 Aug 2019 23:32:34 +0500 Subject: [PATCH 086/266] Add cloud vector asset Start server action button --- .../main/res/drawable/ic_baseline_cloud_upload_24px.xml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 app/src/main/res/drawable/ic_baseline_cloud_upload_24px.xml diff --git a/app/src/main/res/drawable/ic_baseline_cloud_upload_24px.xml b/app/src/main/res/drawable/ic_baseline_cloud_upload_24px.xml new file mode 100644 index 000000000..f230bc20f --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_cloud_upload_24px.xml @@ -0,0 +1,9 @@ + + + From fbbb6ba4acc7e74be131527f76792e3c900ad9d9 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Tue, 6 Aug 2019 00:06:20 +0500 Subject: [PATCH 087/266] Add host books action button New Item in menu_zim_files_contextual --- .../java/org/kiwix/kiwixmobile/webserver/StartServer.java | 1 + app/src/main/res/menu/menu_zim_files_contextual.xml | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java index 430965fc1..e3d193ecd 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java @@ -1,5 +1,6 @@ package org.kiwix.kiwixmobile.webserver; +import android.view.Menu; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import androidx.appcompat.widget.Toolbar; diff --git a/app/src/main/res/menu/menu_zim_files_contextual.xml b/app/src/main/res/menu/menu_zim_files_contextual.xml index 580500d88..888f46a94 100644 --- a/app/src/main/res/menu/menu_zim_files_contextual.xml +++ b/app/src/main/res/menu/menu_zim_files_contextual.xml @@ -18,6 +18,14 @@ app:iconifiedByDefault="true" app:showAsAction="always"/> + + From 0a2c145298c4b4ce9810d01ce6252dc0a3532a8b Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Tue, 6 Aug 2019 06:17:18 +0500 Subject: [PATCH 088/266] Add OnHostActionButtonClickedListener Implement its method in StartServer.java Add this interface in ZimFileSelectFragment.kt Add an inner class in ZimFileSelectFragment.kt Call the inner class from in StartMultiSelection.kt --- .../kiwixmobile/webserver/StartServer.java | 12 ++++++++++-- .../fileselect_view/ZimFileSelectFragment.kt | 19 +++++++++++++++++++ .../effects/StartMultiSelection.kt | 5 ++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java index e3d193ecd..3690d6c33 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/StartServer.java @@ -1,6 +1,9 @@ package org.kiwix.kiwixmobile.webserver; +import android.content.Context; +import android.util.Log; import android.view.Menu; +import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import androidx.appcompat.widget.Toolbar; @@ -9,8 +12,8 @@ import androidx.fragment.app.FragmentTransaction; import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.zim_manager.fileselect_view.ZimFileSelectFragment; -public class StartServer extends AppCompatActivity { - +public class StartServer extends AppCompatActivity implements + ZimFileSelectFragment.OnHostActionButtonClickedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -32,4 +35,9 @@ public class StartServer extends AppCompatActivity { toolbar.setNavigationOnClickListener(v -> onBackPressed()); } + + @Override public void onHostActionButtonClicked() { + Log.v("DANG", "Action button clicked"); + Toast.makeText(this, "Host action button has been clicked", Toast.LENGTH_LONG).show(); + } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/fileselect_view/ZimFileSelectFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/fileselect_view/ZimFileSelectFragment.kt index ac98fa804..fecaa94e3 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/fileselect_view/ZimFileSelectFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/fileselect_view/ZimFileSelectFragment.kt @@ -22,6 +22,7 @@ package org.kiwix.kiwixmobile.zim_manager.fileselect_view import android.Manifest import android.content.pm.PackageManager import android.os.Bundle +import android.util.Log import android.view.ActionMode import android.view.LayoutInflater import android.view.View @@ -158,4 +159,22 @@ class ZimFileSelectFragment : BaseFragment() { private fun requestFileSystemCheck() { zimManageViewModel.requestFileSystemCheck.onNext(Unit) } + + interface OnHostActionButtonClickedListener { + fun onHostActionButtonClicked() + } + + inner class Test() { + private var listener: OnHostActionButtonClickedListener? = null + + init { + listener = context as? OnHostActionButtonClickedListener + listener?.onHostActionButtonClicked() + if (listener == null) + Log.v("DANG", "Listener is null") + if (context == null) + Log.v("DANG", "Context is null") + } + } } + diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/fileselect_view/effects/StartMultiSelection.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/fileselect_view/effects/StartMultiSelection.kt index 8f31c75e4..06315352d 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/fileselect_view/effects/StartMultiSelection.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/fileselect_view/effects/StartMultiSelection.kt @@ -8,6 +8,8 @@ import org.kiwix.kiwixmobile.extensions.startActionMode import org.kiwix.kiwixmobile.zim_manager.ZimManageViewModel.FileSelectActions import org.kiwix.kiwixmobile.zim_manager.ZimManageViewModel.FileSelectActions.RequestDeleteMultiSelection import org.kiwix.kiwixmobile.zim_manager.ZimManageViewModel.FileSelectActions.RequestShareMultiSelection +import org.kiwix.kiwixmobile.zim_manager.fileselect_view.ZimFileSelectFragment +import org.kiwix.kiwixmobile.zim_manager.fileselect_view.ZimFileSelectFragment.Test import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem data class StartMultiSelection( @@ -19,7 +21,8 @@ data class StartMultiSelection( R.menu.menu_zim_files_contextual, mapOf( R.id.zim_file_delete_item to { fileSelectActions.offer(RequestDeleteMultiSelection) }, - R.id.zim_file_share_item to { fileSelectActions.offer(RequestShareMultiSelection) } + R.id.zim_file_share_item to { fileSelectActions.offer(RequestShareMultiSelection) }, + R.id.zim_file_host_item to { ZimFileSelectFragment().Test() } ) ) { fileSelectActions.offer(FileSelectActions.MultiModeFinished) } } From 4c8799957433d845edaaa1c1f763bf2263a598a4 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Tue, 6 Aug 2019 17:00:23 +0500 Subject: [PATCH 089/266] Add start server button --- .../main/res/layout/activity_start_server.xml | 79 +++++++++++-------- app/src/main/res/values/strings.xml | 1 + 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/app/src/main/res/layout/activity_start_server.xml b/app/src/main/res/layout/activity_start_server.xml index eacc77a22..f50868779 100644 --- a/app/src/main/res/layout/activity_start_server.xml +++ b/app/src/main/res/layout/activity_start_server.xml @@ -3,53 +3,62 @@ xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" + android:id="@+id/constraintLayoutId" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" - android:id="@+id/constraintLayoutId" tools:context=".webserver.StartServer" > - - - - - - - + - + + + + +