Refactor zimHostCallbacks in HotspotService

This commit is contained in:
Adeel Zafar 2019-08-17 19:07:08 +05:00
parent 494179b9a6
commit 47d11384fd
3 changed files with 15 additions and 15 deletions

View File

@ -28,8 +28,7 @@ public class WebServerHelper {
}
public boolean startServerHelper(@NonNull ZimHostCallbacks zimHostCallbacks,
@NonNull ArrayList<String> selectedBooksPath) {
public boolean startServerHelper(@NonNull ArrayList<String> selectedBooksPath) {
// 1. Get port from settings screen
// 2. Ask user to change port in settings if port is in use.
@ -38,21 +37,18 @@ public class WebServerHelper {
String ip = getIpAddress();
ip = ip.replaceAll("\n", "");
if (ip.length() == 0) {
zimHostCallbacks.onServerFailedToStart();
} else if (!isServerStarted && startAndroidWebServer(selectedBooksPath)) {
zimHostCallbacks.onServerStarted("http://" + ip + ":" + port);
return false;
} else if (startAndroidWebServer(selectedBooksPath)) {
return true;
}
return isServerStarted;
}
public boolean stopAndroidWebServer(@NonNull ZimHostCallbacks zimHostCallbacks) {
public void stopAndroidWebServer() {
if (isServerStarted) {
kiwixServer.stop();
isServerStarted = false;
zimHostCallbacks.onServerStopped();
return true;
}
return false;
}
private boolean startAndroidWebServer(ArrayList<String> selectedBooksPath) {
@ -111,6 +107,9 @@ public class WebServerHelper {
}
@NonNull public static String getAddress() {
return "http://" + getIpAddress() + ":" + port;
String address = "http://" + getIpAddress() + ":" + port;
address = address.replaceAll("\n", "");
return address;
}
}

View File

@ -246,7 +246,6 @@ public class ZimHostActivity extends BaseActivity implements
presenter.loadBooks();
if (isServerStarted) {
ip = getAddress();
ip = ip.replaceAll("\n", "");
serverTextView.setText(getString(R.string.server_started_message, ip));
startServerButton.setText(getString(R.string.stop_server_label));
startServerButton.setBackgroundColor(getResources().getColor(R.color.stopServer));
@ -449,7 +448,7 @@ public class ZimHostActivity extends BaseActivity implements
}
@Override public void onServerFailedToStart() {
Toast.makeText(this, R.string.server_failed_message, Toast.LENGTH_LONG).show();
Toast.makeText(this, R.string.server_failed_toast_message, Toast.LENGTH_LONG).show();
}
@Override public void onHotspotTurnedOn(@NonNull WifiConfiguration wifiConfiguration) {

View File

@ -91,15 +91,16 @@ public class HotspotService extends Service {
break;
case ACTION_START_SERVER:
if (!webServerHelper.startServerHelper(zimHostCallbacks,
if (!webServerHelper.startServerHelper(
intent.getStringArrayListExtra(SELECTED_ZIM_PATHS_KEY))) {
zimHostCallbacks.onServerFailedToStart();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true);
stopSelf();
notificationManager.cancel(HOTSPOT_NOTIFICATION_ID);
}
Toast.makeText(this, R.string.server_failed_toast_message, Toast.LENGTH_SHORT).show();
} else {
zimHostCallbacks.onServerStarted(webServerHelper.getAddress());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
startForegroundNotificationHelper();
}
@ -151,7 +152,8 @@ public class HotspotService extends Service {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
hotspotManager.turnOffHotspot();
}
webServerHelper.stopAndroidWebServer(zimHostCallbacks);
webServerHelper.stopAndroidWebServer();
zimHostCallbacks.onServerStopped();
stopForeground(true);
stopSelf();
notificationManager.cancel(HOTSPOT_NOTIFICATION_ID);