From 6c75fefdb7afc070179ecc4639a50cc603e3723f Mon Sep 17 00:00:00 2001 From: Adeel Date: Fri, 30 Aug 2019 16:02:05 +0500 Subject: [PATCH] Add IpAddressCallbacks Refactor Flowable using Interval Shift Flowable code to WebServerHelper --- .../kiwixmobile/di/modules/ServiceModule.kt | 11 +++- .../kiwix/kiwixmobile/utils/ServerUtils.java | 8 ++- .../webserver/WebServerHelper.java | 31 +++++++++- .../webserver/ZimHostActivity.java | 58 ++++++------------- .../webserver/ZimHostCallbacks.java | 4 ++ .../wifi_hotspot/HotspotService.java | 17 +++++- .../wifi_hotspot/IpAddressCallbacks.java | 8 +++ .../wifi_hotspot/WifiHotspotManager.java | 6 +- 8 files changed, 93 insertions(+), 50 deletions(-) create mode 100644 app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/IpAddressCallbacks.java diff --git a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt index 51ee3a2c8..5bb4125b7 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt @@ -13,6 +13,7 @@ import org.kiwix.kiwixmobile.di.ServiceScope import org.kiwix.kiwixmobile.webserver.WebServerHelper import org.kiwix.kiwixmobile.wifi_hotspot.HotspotNotificationManager import org.kiwix.kiwixmobile.wifi_hotspot.HotspotStateListener +import org.kiwix.kiwixmobile.wifi_hotspot.IpAddressCallbacks import org.kiwix.kiwixmobile.wifi_hotspot.WifiHotspotManager @Module @@ -22,8 +23,9 @@ class ServiceModule { @ServiceScope fun providesWebServerHelper( jniKiwixLibrary: JNIKiwixLibrary, - kiwixServer: JNIKiwixServer - ): WebServerHelper = WebServerHelper(jniKiwixLibrary, kiwixServer) + kiwixServer: JNIKiwixServer, + ipAddressCallbacks: IpAddressCallbacks + ): WebServerHelper = WebServerHelper(jniKiwixLibrary, kiwixServer, ipAddressCallbacks) @Provides @ServiceScope @@ -38,6 +40,11 @@ class ServiceModule { fun providesHotspotStateListener(service: Service): HotspotStateListener = service as HotspotStateListener + @Provides + @ServiceScope + fun providesIpAddressCallbacks(service: Service): IpAddressCallbacks = + service as IpAddressCallbacks + @Provides @ServiceScope fun providesJNIKiwixLibrary(): JNIKiwixLibrary = JNIKiwixLibrary() diff --git a/app/src/main/java/org/kiwix/kiwixmobile/utils/ServerUtils.java b/app/src/main/java/org/kiwix/kiwixmobile/utils/ServerUtils.java index 3ffb668a9..d9afaab5e 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/utils/ServerUtils.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/utils/ServerUtils.java @@ -1,6 +1,7 @@ package org.kiwix.kiwixmobile.utils; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; @@ -9,9 +10,10 @@ import java.util.Enumeration; public class ServerUtils { public static int port; public static boolean isServerStarted; + public static final String INVALID_IP = "-1"; // get Ip address of the device's wireless access point i.e. wifi hotspot OR wifi network - public static String getIpAddress() { + @Nullable public static String getIpAddress() { String ip = ""; try { Enumeration enumNetworkInterfaces = NetworkInterface @@ -51,10 +53,10 @@ public class ServerUtils { return address; } - public static String getIp() { + @Nullable public static String getIp() { String ip = getIpAddress(); ip = ip.replaceAll("\n", ""); - if (ip.length() == 0) throw new IllegalStateException(); + if (ip.length() == 0) return INVALID_IP; return ip; } } 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 f14634193..7d20e0e46 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -2,12 +2,18 @@ package org.kiwix.kiwixmobile.webserver; import android.util.Log; import androidx.annotation.NonNull; +import io.reactivex.Flowable; +import io.reactivex.android.schedulers.AndroidSchedulers; import java.util.ArrayList; +import java.util.concurrent.TimeUnit; import javax.inject.Inject; import org.kiwix.kiwixlib.JNIKiwixException; import org.kiwix.kiwixlib.JNIKiwixLibrary; import org.kiwix.kiwixlib.JNIKiwixServer; import org.kiwix.kiwixmobile.utils.ServerUtils; +import org.kiwix.kiwixmobile.wifi_hotspot.IpAddressCallbacks; + +import static org.kiwix.kiwixmobile.utils.ServerUtils.INVALID_IP; /** * WebServerHelper class is used to set up the suitable environment i.e. getting the @@ -19,11 +25,13 @@ public class WebServerHelper { private static final String TAG = "WebServerHelper"; private JNIKiwixLibrary kiwixLibrary; private JNIKiwixServer kiwixServer; + private IpAddressCallbacks ipAddressCallbacks; @Inject public WebServerHelper(@NonNull JNIKiwixLibrary kiwixLibrary, - @NonNull JNIKiwixServer kiwixServer) { + @NonNull JNIKiwixServer kiwixServer, @NonNull IpAddressCallbacks ipAddressCallbacks) { this.kiwixLibrary = kiwixLibrary; this.kiwixServer = kiwixServer; + this.ipAddressCallbacks = ipAddressCallbacks; } public boolean startServerHelper(@NonNull ArrayList selectedBooksPath) { @@ -63,4 +71,25 @@ public class WebServerHelper { } return ServerUtils.isServerStarted; } + + //Keeps checking if hotspot has been turned using the ip address with an interval of 1 sec + //If no ip is found after 15 seconds, dismisses the progress dialog + public void pollForValidIpAddress() { + Flowable.interval(1, TimeUnit.SECONDS) + .map(__ -> ServerUtils.getIp()) + .filter(s -> s != INVALID_IP) + .timeout(15, TimeUnit.SECONDS) + .take(1) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + s -> { + ipAddressCallbacks.onIpAddressValid(); + Log.d(TAG, "onSuccess: " + s); + }, //success stuff + e -> { + Log.d(TAG, "Unable to turn on server", e); + ipAddressCallbacks.onIpAddressInvalid(); + } //failure stuff + ); + } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java index e027ce5f4..7f5a8f1c6 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java @@ -22,14 +22,9 @@ import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.recyclerview.widget.RecyclerView; import butterknife.BindView; -import io.reactivex.Flowable; -import io.reactivex.SingleObserver; -import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.disposables.Disposable; import java.io.File; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.TimeUnit; import javax.inject.Inject; import kotlin.Unit; import kotlin.jvm.functions.Function0; @@ -44,6 +39,7 @@ import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BookOnDiskDeleg import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskAdapter; import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem; +import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.ACTION_CHECK_IP_ADDRESS; import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.ACTION_LOCATION_ACCESS_GRANTED; import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.ACTION_START_SERVER; import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.ACTION_STOP_SERVER; @@ -70,15 +66,15 @@ public class ZimHostActivity extends BaseActivity implements private static final String TAG = "ZimHostActivity"; private static final int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 102; - public static final String SELECTED_ZIM_PATHS_KEY = "selected_zim_paths"; private static final String IP_STATE_KEY = "ip_state_key"; - private ProgressDialog progressDialog; + public static final String SELECTED_ZIM_PATHS_KEY = "selected_zim_paths"; private BooksOnDiskAdapter booksAdapter; private BookOnDiskDelegate.BookDelegate bookDelegate; private HotspotService hotspotService; private String ip; private ServiceConnection serviceConnection; + private ProgressDialog progressDialog; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -280,43 +276,15 @@ public class ZimHostActivity extends BaseActivity implements null, () -> { progressDialog = - ProgressDialog.show(ZimHostActivity.this, + ProgressDialog.show(this, getString(R.string.progress_dialog_starting_server), "", true); - pollForValidIpAddress(); + startService(createHotspotIntent(ACTION_CHECK_IP_ADDRESS)); return Unit.INSTANCE; } ); } - //Keeps checking if hotspot has been turned using the ip address with an interval of 1 sec - //If no ip is found after 15 seconds, dismisses the progress dialog - private void pollForValidIpAddress() { - Flowable.fromCallable(ServerUtils::getIp) - .retryWhen(error -> error.delay(1, TimeUnit.SECONDS)) - .timeout(15, TimeUnit.SECONDS) - .firstOrError() - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(new SingleObserver() { - @Override public void onSubscribe(Disposable d) { - } - - @Override public void onSuccess(String s) { - progressDialog.dismiss(); - startService(createHotspotIntent(ACTION_START_SERVER).putStringArrayListExtra( - SELECTED_ZIM_PATHS_KEY, getSelectedBooksPath())); - Log.d(TAG, "onSuccess: " + s); - } - - @Override public void onError(Throwable e) { - progressDialog.dismiss(); - Toast.makeText(ZimHostActivity.this, R.string.server_failed_message, Toast.LENGTH_SHORT) - .show(); - Log.d(TAG, "Unable to turn on server", e); - } - }); - } - private Intent createHotspotIntent(String action) { return new Intent(this, HotspotService.class).setAction(action); } @@ -338,10 +306,10 @@ public class ZimHostActivity extends BaseActivity implements alertDialogShower.show(new KiwixDialog.ShowHotspotDetails(wifiConfiguration), (Function0) () -> { progressDialog = - ProgressDialog.show(ZimHostActivity.this, + ProgressDialog.show(this, getString(R.string.progress_dialog_starting_server), "", true); - pollForValidIpAddress(); + startService(createHotspotIntent(ACTION_CHECK_IP_ADDRESS)); return Unit.INSTANCE; }); } @@ -383,4 +351,16 @@ public class ZimHostActivity extends BaseActivity implements @Override public void onLocationSet() { startService(createHotspotIntent(ACTION_LOCATION_ACCESS_GRANTED)); } + + @Override public void provideBooksAndStartServer() { + startService(createHotspotIntent(ACTION_START_SERVER).putStringArrayListExtra( + SELECTED_ZIM_PATHS_KEY, getSelectedBooksPath())); + } + + @Override public void dismissProgressDialog() { + progressDialog.dismiss(); + Toast.makeText(this, R.string.server_failed_message, + Toast.LENGTH_SHORT) + .show(); + } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostCallbacks.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostCallbacks.java index 259136106..86fafdbae 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostCallbacks.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostCallbacks.java @@ -16,4 +16,8 @@ public interface ZimHostCallbacks { void onHotspotFailedToStart(); void requestLocationAccess(); + + void provideBooksAndStartServer(); + + void dismissProgressDialog(); } 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 545f4b709..9f304c344 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 @@ -27,12 +27,13 @@ import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotNotificationManager.HOTS * Created by Adeel Zafar on 07/01/2019. */ -public class HotspotService extends Service implements HotspotStateListener { +public class HotspotService extends Service implements HotspotStateListener, IpAddressCallbacks { public static final String ACTION_TOGGLE_HOTSPOT = "toggle_hotspot"; public static final String ACTION_LOCATION_ACCESS_GRANTED = "location_access_granted"; public static final String ACTION_START_SERVER = "start_server"; public static final String ACTION_STOP_SERVER = "stop_server"; + public static final String ACTION_CHECK_IP_ADDRESS = "check_ip_address"; public static final String ACTION_STOP = "hotspot_stop"; private static final String TAG = "HotspotService"; @@ -111,6 +112,11 @@ public class HotspotService extends Service implements HotspotStateListener { case ACTION_STOP_SERVER: stopHotspotAndDismissNotification(); break; + + case ACTION_CHECK_IP_ADDRESS: + webServerHelper.pollForValidIpAddress(); + break; + default: break; } @@ -169,6 +175,15 @@ public class HotspotService extends Service implements HotspotStateListener { hotspotNotificationManager.dismissNotification(); } + @Override public void onIpAddressValid() { + zimHostCallbacks.dismissProgressDialog(); + zimHostCallbacks.provideBooksAndStartServer(); + } + + @Override public void onIpAddressInvalid() { + zimHostCallbacks.dismissProgressDialog(); + } + public class HotspotBinder extends Binder { @NonNull public HotspotService getService() { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/IpAddressCallbacks.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/IpAddressCallbacks.java new file mode 100644 index 000000000..e3da8a063 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/IpAddressCallbacks.java @@ -0,0 +1,8 @@ +package org.kiwix.kiwixmobile.wifi_hotspot; + +public interface IpAddressCallbacks { + + void onIpAddressValid(); + + void onIpAddressInvalid(); +} \ No newline at end of file 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 8e6e0d237..335a4711f 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 @@ -17,15 +17,13 @@ import javax.inject.Inject; public class WifiHotspotManager { private static final String TAG = "WifiHotspotManager"; + private WifiManager wifiManager; private WifiManager.LocalOnlyHotspotReservation hotspotReservation; private HotspotStateListener hotspotStateListener; - @Inject - WifiManager wifiManager; - @Inject public WifiHotspotManager(@NonNull WifiManager wifiManager, - HotspotStateListener hotspotStateListener) { + @NonNull HotspotStateListener hotspotStateListener) { this.wifiManager = wifiManager; this.hotspotStateListener = hotspotStateListener; }