Refactor ZimHostCallbacks

This commit is contained in:
Adeel Zafar 2019-08-16 20:57:52 +05:00
parent 75dc88828d
commit 3ee5e7cde2
6 changed files with 45 additions and 45 deletions

View File

@ -1,19 +0,0 @@
package org.kiwix.kiwixmobile.webserver;
import android.net.wifi.WifiConfiguration;
public interface ServerStateListener {
void serverStarted(String ip);
void serverStopped();
void serverFailed();
void hotspotTurnedOn(WifiConfiguration wifiConfiguration);
void hotspotFailed();
void hotspotState(Boolean state);
}

View File

@ -28,7 +28,7 @@ public class WebServerHelper {
} }
public boolean startServerHelper(@NonNull ServerStateListener stateListener, public boolean startServerHelper(@NonNull ZimHostCallbacks zimHostCallbacks,
@NonNull ArrayList<String> selectedBooksPath) { @NonNull ArrayList<String> selectedBooksPath) {
// 1. Get port from settings screen // 1. Get port from settings screen
@ -38,18 +38,18 @@ public class WebServerHelper {
String ip = getIpAddress(); String ip = getIpAddress();
ip = ip.replaceAll("\n", ""); ip = ip.replaceAll("\n", "");
if (ip.length() == 0) { if (ip.length() == 0) {
stateListener.serverFailed(); zimHostCallbacks.onServerFailedToStart();
} else if (!isServerStarted && startAndroidWebServer(selectedBooksPath)) { } else if (!isServerStarted && startAndroidWebServer(selectedBooksPath)) {
stateListener.serverStarted("http://" + ip + ":" + port); zimHostCallbacks.onServerStarted("http://" + ip + ":" + port);
} }
return isServerStarted; return isServerStarted;
} }
public boolean stopAndroidWebServer(@NonNull ServerStateListener stateListener) { public boolean stopAndroidWebServer(@NonNull ZimHostCallbacks zimHostCallbacks) {
if (isServerStarted) { if (isServerStarted) {
kiwixServer.stop(); kiwixServer.stop();
isServerStarted = false; isServerStarted = false;
stateListener.serverStopped(); zimHostCallbacks.onServerStopped();
return true; return true;
} }
return false; return false;

View File

@ -58,7 +58,7 @@ import static org.kiwix.kiwixmobile.webserver.WebServerHelper.getAddress;
import static org.kiwix.kiwixmobile.webserver.WebServerHelper.isServerStarted; import static org.kiwix.kiwixmobile.webserver.WebServerHelper.isServerStarted;
public class ZimHostActivity extends BaseActivity implements public class ZimHostActivity extends BaseActivity implements
ServerStateListener, ZimHostContract.View { ZimHostCallbacks, ZimHostContract.View {
@BindView(R.id.startServerButton) @BindView(R.id.startServerButton)
Button startServerButton; Button startServerButton;
@ -433,7 +433,7 @@ public class ZimHostActivity extends BaseActivity implements
} }
} }
@Override public void serverStarted(@NonNull String ip) { @Override public void onServerStarted(@NonNull String ip) {
this.ip = ip; this.ip = ip;
serverTextView.setText(getString(R.string.server_started_message, this.ip)); serverTextView.setText(getString(R.string.server_started_message, this.ip));
startServerButton.setText(getString(R.string.stop_server_label)); startServerButton.setText(getString(R.string.stop_server_label));
@ -441,18 +441,18 @@ public class ZimHostActivity extends BaseActivity implements
isServerStarted = true; isServerStarted = true;
} }
@Override public void serverStopped() { @Override public void onServerStopped() {
serverTextView.setText(getString(R.string.server_textview_default_message)); serverTextView.setText(getString(R.string.server_textview_default_message));
startServerButton.setText(getString(R.string.start_server_label)); startServerButton.setText(getString(R.string.start_server_label));
startServerButton.setBackgroundColor(getResources().getColor(R.color.greenTick)); startServerButton.setBackgroundColor(getResources().getColor(R.color.greenTick));
isServerStarted = false; isServerStarted = false;
} }
@Override public void serverFailed() { @Override public void onServerFailedToStart() {
Toast.makeText(this, R.string.server_failed_message, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.server_failed_message, Toast.LENGTH_LONG).show();
} }
@Override public void hotspotTurnedOn(@NonNull WifiConfiguration wifiConfiguration) { @Override public void onHotspotTurnedOn(@NonNull WifiConfiguration wifiConfiguration) {
//Show an alert dialog for hotspot details //Show an alert dialog for hotspot details
AlertDialog.Builder builder = new AlertDialog.Builder(this, dialogStyle()); AlertDialog.Builder builder = new AlertDialog.Builder(this, dialogStyle());
@ -493,7 +493,7 @@ public class ZimHostActivity extends BaseActivity implements
startActivity(intent); startActivity(intent);
} }
@Override public void hotspotFailed() { @Override public void onHotspotFailedToStart() {
//Show a dialog to turn off default hotspot //Show a dialog to turn off default hotspot
alertDialogShower.show(KiwixDialog.HotspotFailed.INSTANCE, alertDialogShower.show(KiwixDialog.HotspotFailed.INSTANCE,
@ -505,8 +505,8 @@ public class ZimHostActivity extends BaseActivity implements
}); });
} }
@Override public void hotspotState(@Nullable Boolean state) { @Override public void onHotspotStateReceived(@Nullable Boolean isHotspotEnabled) {
if (state) //if hotspot is already enabled, turn it off. if (isHotspotEnabled) //if hotspot is already enabled, turn it off.
{ {
startService(ACTION_TURN_OFF_AFTER_O); startService(ACTION_TURN_OFF_AFTER_O);
} else //If hotspot is not already enabled, then turn it on. } else //If hotspot is not already enabled, then turn it on.

View File

@ -0,0 +1,19 @@
package org.kiwix.kiwixmobile.webserver;
import android.net.wifi.WifiConfiguration;
public interface ZimHostCallbacks {
void onServerStarted(String ip);
void onServerStopped();
void onServerFailedToStart();
void onHotspotTurnedOn(WifiConfiguration wifiConfiguration);
void onHotspotFailedToStart();
void onHotspotStateReceived(Boolean state);
}

View File

@ -19,7 +19,7 @@ import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.R;
import org.kiwix.kiwixmobile.utils.Constants; import org.kiwix.kiwixmobile.utils.Constants;
import org.kiwix.kiwixmobile.webserver.ServerStateListener; import org.kiwix.kiwixmobile.webserver.ZimHostCallbacks;
import org.kiwix.kiwixmobile.webserver.WebServerHelper; import org.kiwix.kiwixmobile.webserver.WebServerHelper;
import org.kiwix.kiwixmobile.webserver.ZimHostActivity; import org.kiwix.kiwixmobile.webserver.ZimHostActivity;
@ -43,7 +43,7 @@ public class HotspotService extends Service {
private BroadcastReceiver stopReceiver; private BroadcastReceiver stopReceiver;
private NotificationManager notificationManager; private NotificationManager notificationManager;
private NotificationCompat.Builder builder; private NotificationCompat.Builder builder;
private ServerStateListener serverStateListener; private ZimHostCallbacks zimHostCallbacks;
private final IBinder serviceBinder = new HotspotBinder(); private final IBinder serviceBinder = new HotspotBinder();
private WebServerHelper webServerHelper; private WebServerHelper webServerHelper;
@ -74,13 +74,13 @@ public class HotspotService extends Service {
case ACTION_IS_HOTSPOT_ENABLED: case ACTION_IS_HOTSPOT_ENABLED:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
serverStateListener.hotspotState(hotspotManager.checkHotspotState()); zimHostCallbacks.onHotspotStateReceived(hotspotManager.checkHotspotState());
} }
break; break;
case ACTION_TURN_ON_AFTER_O: case ACTION_TURN_ON_AFTER_O:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
hotspotManager.turnOnHotspot(serverStateListener); hotspotManager.turnOnHotspot(zimHostCallbacks);
startForeground(HOTSPOT_NOTIFICATION_ID, startForeground(HOTSPOT_NOTIFICATION_ID,
buildForegroundNotification(getString(R.string.hotspot_running))); buildForegroundNotification(getString(R.string.hotspot_running)));
} }
@ -93,7 +93,7 @@ public class HotspotService extends Service {
break; break;
case ACTION_START_SERVER: case ACTION_START_SERVER:
if (!webServerHelper.startServerHelper(serverStateListener, if (!webServerHelper.startServerHelper(zimHostCallbacks,
intent.getStringArrayListExtra(SELECTED_ZIM_PATHS_KEY))) { intent.getStringArrayListExtra(SELECTED_ZIM_PATHS_KEY))) {
Toast.makeText(this, R.string.server_failed_toast_message, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.server_failed_toast_message, Toast.LENGTH_LONG).show();
} else { } else {
@ -149,7 +149,7 @@ public class HotspotService extends Service {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
hotspotManager.turnOffHotspot(); hotspotManager.turnOffHotspot();
} }
webServerHelper.stopAndroidWebServer(serverStateListener); webServerHelper.stopAndroidWebServer(zimHostCallbacks);
stopForeground(true); stopForeground(true);
stopSelf(); stopSelf();
notificationManager.cancel(HOTSPOT_NOTIFICATION_ID); notificationManager.cancel(HOTSPOT_NOTIFICATION_ID);
@ -182,7 +182,7 @@ public class HotspotService extends Service {
} }
} }
public void registerCallBack(@Nullable ServerStateListener myCallback) { public void registerCallBack(@Nullable ZimHostCallbacks myCallback) {
serverStateListener = myCallback; zimHostCallbacks = myCallback;
} }
} }

View File

@ -8,7 +8,7 @@ import android.os.Handler;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import org.kiwix.kiwixmobile.webserver.ServerStateListener; import org.kiwix.kiwixmobile.webserver.ZimHostCallbacks;
/** /**
* WifiHotstopManager class makes use of the Android's WifiManager and WifiConfiguration class * WifiHotstopManager class makes use of the Android's WifiManager and WifiConfiguration class
@ -31,7 +31,7 @@ public class WifiHotspotManager {
//Workaround to turn on hotspot for Oreo versions //Workaround to turn on hotspot for Oreo versions
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
public void turnOnHotspot(@NonNull ServerStateListener serverStateListener) { public void turnOnHotspot(@NonNull ZimHostCallbacks zimHostCallbacks) {
wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() { wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
@Override @Override
@ -45,7 +45,7 @@ public class WifiHotspotManager {
+ " \n SSID is : " + " \n SSID is : "
+ currentConfig.SSID); + currentConfig.SSID);
serverStateListener.hotspotTurnedOn(currentConfig); zimHostCallbacks.onHotspotTurnedOn(currentConfig);
isHotspotEnabled = true; isHotspotEnabled = true;
@ -56,14 +56,14 @@ public class WifiHotspotManager {
public void onStopped() { public void onStopped() {
super.onStopped(); super.onStopped();
Log.v(TAG, "Local Hotspot Stopped"); Log.v(TAG, "Local Hotspot Stopped");
serverStateListener.serverStopped(); zimHostCallbacks.onServerStopped();
} }
@Override @Override
public void onFailed(int reason) { public void onFailed(int reason) {
super.onFailed(reason); super.onFailed(reason);
Log.v(TAG, "Local Hotspot failed to start"); Log.v(TAG, "Local Hotspot failed to start");
serverStateListener.hotspotFailed(); zimHostCallbacks.onHotspotFailedToStart();
isHotspotEnabled = false; isHotspotEnabled = false;
Log.v(TAG, "Is hotspot enabled? " + isHotspotEnabled); Log.v(TAG, "Is hotspot enabled? " + isHotspotEnabled);
} }