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, public boolean startServerHelper(@NonNull ArrayList<String> selectedBooksPath) {
@NonNull ArrayList<String> selectedBooksPath) {
// 1. Get port from settings screen // 1. Get port from settings screen
// 2. Ask user to change port in settings if port is in use. // 2. Ask user to change port in settings if port is in use.
@ -38,21 +37,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) {
zimHostCallbacks.onServerFailedToStart(); return false;
} else if (!isServerStarted && startAndroidWebServer(selectedBooksPath)) { } else if (startAndroidWebServer(selectedBooksPath)) {
zimHostCallbacks.onServerStarted("http://" + ip + ":" + port); return true;
} }
return isServerStarted; return isServerStarted;
} }
public boolean stopAndroidWebServer(@NonNull ZimHostCallbacks zimHostCallbacks) { public void stopAndroidWebServer() {
if (isServerStarted) { if (isServerStarted) {
kiwixServer.stop(); kiwixServer.stop();
isServerStarted = false; isServerStarted = false;
zimHostCallbacks.onServerStopped();
return true;
} }
return false;
} }
private boolean startAndroidWebServer(ArrayList<String> selectedBooksPath) { private boolean startAndroidWebServer(ArrayList<String> selectedBooksPath) {
@ -111,6 +107,9 @@ public class WebServerHelper {
} }
@NonNull public static String getAddress() { @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(); presenter.loadBooks();
if (isServerStarted) { if (isServerStarted) {
ip = getAddress(); ip = getAddress();
ip = ip.replaceAll("\n", "");
serverTextView.setText(getString(R.string.server_started_message, ip)); serverTextView.setText(getString(R.string.server_started_message, ip));
startServerButton.setText(getString(R.string.stop_server_label)); startServerButton.setText(getString(R.string.stop_server_label));
startServerButton.setBackgroundColor(getResources().getColor(R.color.stopServer)); startServerButton.setBackgroundColor(getResources().getColor(R.color.stopServer));
@ -449,7 +448,7 @@ public class ZimHostActivity extends BaseActivity implements
} }
@Override public void onServerFailedToStart() { @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) { @Override public void onHotspotTurnedOn(@NonNull WifiConfiguration wifiConfiguration) {

View File

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