mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-22 12:03:09 -04:00
Refactor ZimHostCallbacks
This commit is contained in:
parent
75dc88828d
commit
3ee5e7cde2
@ -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);
|
||||
|
||||
}
|
@ -28,7 +28,7 @@ public class WebServerHelper {
|
||||
|
||||
}
|
||||
|
||||
public boolean startServerHelper(@NonNull ServerStateListener stateListener,
|
||||
public boolean startServerHelper(@NonNull ZimHostCallbacks zimHostCallbacks,
|
||||
@NonNull ArrayList<String> selectedBooksPath) {
|
||||
|
||||
// 1. Get port from settings screen
|
||||
@ -38,18 +38,18 @@ public class WebServerHelper {
|
||||
String ip = getIpAddress();
|
||||
ip = ip.replaceAll("\n", "");
|
||||
if (ip.length() == 0) {
|
||||
stateListener.serverFailed();
|
||||
zimHostCallbacks.onServerFailedToStart();
|
||||
} else if (!isServerStarted && startAndroidWebServer(selectedBooksPath)) {
|
||||
stateListener.serverStarted("http://" + ip + ":" + port);
|
||||
zimHostCallbacks.onServerStarted("http://" + ip + ":" + port);
|
||||
}
|
||||
return isServerStarted;
|
||||
}
|
||||
|
||||
public boolean stopAndroidWebServer(@NonNull ServerStateListener stateListener) {
|
||||
public boolean stopAndroidWebServer(@NonNull ZimHostCallbacks zimHostCallbacks) {
|
||||
if (isServerStarted) {
|
||||
kiwixServer.stop();
|
||||
isServerStarted = false;
|
||||
stateListener.serverStopped();
|
||||
zimHostCallbacks.onServerStopped();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -58,7 +58,7 @@ import static org.kiwix.kiwixmobile.webserver.WebServerHelper.getAddress;
|
||||
import static org.kiwix.kiwixmobile.webserver.WebServerHelper.isServerStarted;
|
||||
|
||||
public class ZimHostActivity extends BaseActivity implements
|
||||
ServerStateListener, ZimHostContract.View {
|
||||
ZimHostCallbacks, ZimHostContract.View {
|
||||
|
||||
@BindView(R.id.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;
|
||||
serverTextView.setText(getString(R.string.server_started_message, this.ip));
|
||||
startServerButton.setText(getString(R.string.stop_server_label));
|
||||
@ -441,18 +441,18 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
isServerStarted = true;
|
||||
}
|
||||
|
||||
@Override public void serverStopped() {
|
||||
@Override public void onServerStopped() {
|
||||
serverTextView.setText(getString(R.string.server_textview_default_message));
|
||||
startServerButton.setText(getString(R.string.start_server_label));
|
||||
startServerButton.setBackgroundColor(getResources().getColor(R.color.greenTick));
|
||||
isServerStarted = false;
|
||||
}
|
||||
|
||||
@Override public void serverFailed() {
|
||||
@Override public void onServerFailedToStart() {
|
||||
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
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this, dialogStyle());
|
||||
@ -493,7 +493,7 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
@Override public void hotspotFailed() {
|
||||
@Override public void onHotspotFailedToStart() {
|
||||
//Show a dialog to turn off default hotspot
|
||||
|
||||
alertDialogShower.show(KiwixDialog.HotspotFailed.INSTANCE,
|
||||
@ -505,8 +505,8 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
});
|
||||
}
|
||||
|
||||
@Override public void hotspotState(@Nullable Boolean state) {
|
||||
if (state) //if hotspot is already enabled, turn it off.
|
||||
@Override public void onHotspotStateReceived(@Nullable Boolean isHotspotEnabled) {
|
||||
if (isHotspotEnabled) //if hotspot is already enabled, turn it off.
|
||||
{
|
||||
startService(ACTION_TURN_OFF_AFTER_O);
|
||||
} else //If hotspot is not already enabled, then turn it on.
|
||||
|
@ -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);
|
||||
|
||||
}
|
@ -19,7 +19,7 @@ import androidx.annotation.Nullable;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import org.kiwix.kiwixmobile.R;
|
||||
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.ZimHostActivity;
|
||||
|
||||
@ -43,7 +43,7 @@ public class HotspotService extends Service {
|
||||
private BroadcastReceiver stopReceiver;
|
||||
private NotificationManager notificationManager;
|
||||
private NotificationCompat.Builder builder;
|
||||
private ServerStateListener serverStateListener;
|
||||
private ZimHostCallbacks zimHostCallbacks;
|
||||
private final IBinder serviceBinder = new HotspotBinder();
|
||||
private WebServerHelper webServerHelper;
|
||||
|
||||
@ -74,13 +74,13 @@ public class HotspotService extends Service {
|
||||
|
||||
case ACTION_IS_HOTSPOT_ENABLED:
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
serverStateListener.hotspotState(hotspotManager.checkHotspotState());
|
||||
zimHostCallbacks.onHotspotStateReceived(hotspotManager.checkHotspotState());
|
||||
}
|
||||
break;
|
||||
|
||||
case ACTION_TURN_ON_AFTER_O:
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
hotspotManager.turnOnHotspot(serverStateListener);
|
||||
hotspotManager.turnOnHotspot(zimHostCallbacks);
|
||||
startForeground(HOTSPOT_NOTIFICATION_ID,
|
||||
buildForegroundNotification(getString(R.string.hotspot_running)));
|
||||
}
|
||||
@ -93,7 +93,7 @@ public class HotspotService extends Service {
|
||||
break;
|
||||
|
||||
case ACTION_START_SERVER:
|
||||
if (!webServerHelper.startServerHelper(serverStateListener,
|
||||
if (!webServerHelper.startServerHelper(zimHostCallbacks,
|
||||
intent.getStringArrayListExtra(SELECTED_ZIM_PATHS_KEY))) {
|
||||
Toast.makeText(this, R.string.server_failed_toast_message, Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
@ -149,7 +149,7 @@ public class HotspotService extends Service {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
hotspotManager.turnOffHotspot();
|
||||
}
|
||||
webServerHelper.stopAndroidWebServer(serverStateListener);
|
||||
webServerHelper.stopAndroidWebServer(zimHostCallbacks);
|
||||
stopForeground(true);
|
||||
stopSelf();
|
||||
notificationManager.cancel(HOTSPOT_NOTIFICATION_ID);
|
||||
@ -182,7 +182,7 @@ public class HotspotService extends Service {
|
||||
}
|
||||
}
|
||||
|
||||
public void registerCallBack(@Nullable ServerStateListener myCallback) {
|
||||
serverStateListener = myCallback;
|
||||
public void registerCallBack(@Nullable ZimHostCallbacks myCallback) {
|
||||
zimHostCallbacks = myCallback;
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import android.os.Handler;
|
||||
import android.util.Log;
|
||||
import androidx.annotation.NonNull;
|
||||
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
|
||||
@ -31,7 +31,7 @@ public class WifiHotspotManager {
|
||||
|
||||
//Workaround to turn on hotspot for Oreo versions
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
public void turnOnHotspot(@NonNull ServerStateListener serverStateListener) {
|
||||
public void turnOnHotspot(@NonNull ZimHostCallbacks zimHostCallbacks) {
|
||||
wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
|
||||
|
||||
@Override
|
||||
@ -45,7 +45,7 @@ public class WifiHotspotManager {
|
||||
+ " \n SSID is : "
|
||||
+ currentConfig.SSID);
|
||||
|
||||
serverStateListener.hotspotTurnedOn(currentConfig);
|
||||
zimHostCallbacks.onHotspotTurnedOn(currentConfig);
|
||||
|
||||
isHotspotEnabled = true;
|
||||
|
||||
@ -56,14 +56,14 @@ public class WifiHotspotManager {
|
||||
public void onStopped() {
|
||||
super.onStopped();
|
||||
Log.v(TAG, "Local Hotspot Stopped");
|
||||
serverStateListener.serverStopped();
|
||||
zimHostCallbacks.onServerStopped();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailed(int reason) {
|
||||
super.onFailed(reason);
|
||||
Log.v(TAG, "Local Hotspot failed to start");
|
||||
serverStateListener.hotspotFailed();
|
||||
zimHostCallbacks.onHotspotFailedToStart();
|
||||
isHotspotEnabled = false;
|
||||
Log.v(TAG, "Is hotspot enabled? " + isHotspotEnabled);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user