From 93c71e79f151c2142265ffc759a7119b2ccab285 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Tue, 6 Aug 2019 23:15:30 +0500 Subject: [PATCH] Remove start server dialog Start server without asking for port from user dialog --- .../webserver/WebServerHelper.java | 74 ++++--------------- .../webserver/ZimHostActivity.java | 2 +- .../wifi_hotspot/HotspotService.java | 4 +- .../wifi_hotspot/WifiHotspotManager.java | 2 +- 4 files changed, 17 insertions(+), 65 deletions(-) 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 6f84b6d12..afab212e6 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -37,58 +37,20 @@ public class WebServerHelper { this.context = context; } - //Dialog to start the server where user is shown the hotspot ip address can edit the port no. - public void startServerDialog() { - AlertDialog.Builder alert = new AlertDialog.Builder(context); - alert.setTitle(R.string.start_server_dialog_title); - alert.setMessage(R.string.start_server_dialog_message); - - LinearLayout layout = new LinearLayout(context); - layout.setOrientation(LinearLayout.HORIZONTAL); - - coordinatorLayout = new CoordinatorLayout(context); - - textViewIpAccess = new TextView(context); - textViewIpAccess.setText(context.getString(R.string.sample_ip_address)); - textViewIpAccess.setTextSize(20); - layout.addView(textViewIpAccess); - - TextView colonTextView = new TextView(context); - colonTextView.setTextSize(20); - colonTextView.setText(":"); - layout.addView(colonTextView); - - editTextPort = new EditText(context); - editTextPort.setInputType(InputType.TYPE_CLASS_NUMBER); - editTextPort.setHint(R.string.port_hint); - editTextPort.setText(R.string.port_hint); - editTextPort.setFilters(new InputFilter[] { new InputFilter.LengthFilter(4) }); - editTextPort.setTextSize(20); - layout.addView(editTextPort); - - alert.setView(layout); - - alert.setPositiveButton("START SERVER", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - if (!isStarted && startAndroidWebServer()) { - isStarted = true; - listener = (ServerStateListener) context; - listener.serverStarted(getIpAddress()+port); - } - } - }); - - alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - // Canceled. - } - }); - - alert.show(); - - setIpAccess(); + public void startServerHelper() { + //TO DO: + //1. Get port from settings screen + //2. Ask user to change port in settings if port is in use. + //OR + //Always use 8080 and when its not available then iterate this number. + if (!isStarted && startAndroidWebServer()) { + isStarted = true; + listener = (ServerStateListener) context; + listener.serverStarted(getIpAddress() + port); + } } + public static boolean stopAndroidWebServer() { if (isStarted && webServer != null) { webServer.stop(); @@ -100,7 +62,7 @@ public class WebServerHelper { boolean startAndroidWebServer() { if (!isStarted) { - port = getPortFromEditText(); + port = 8080; try { if (port == 0) { throw new Exception(); @@ -118,16 +80,6 @@ public class WebServerHelper { return false; } - int getPortFromEditText() { - String valueEditText = editTextPort.getText().toString(); - int DEFAULT_PORT = 8080; - return (valueEditText.length() > 0) ? Integer.parseInt(valueEditText) : DEFAULT_PORT; - } - - private void setIpAccess() { - textViewIpAccess.setText(getIpAddress()); - } - // get Ip address of the device's wireless access point i.e. wifi hotspot OR wifi network String getIpAddress() { Log.v("DANG", "Inside getIpAdress()"); 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 e796a2433..e62fb9051 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java @@ -304,7 +304,7 @@ public class ZimHostActivity extends AppCompatActivity implements handler.postDelayed(new Runnable() { @Override public void run() { - webServerHelper.startServerDialog(); + webServerHelper.startServerHelper(); } }, 5000); }); 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 c4d12c054..682c63a01 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 @@ -94,7 +94,7 @@ public class HotspotService extends Service { .setSmallIcon(R.mipmap.kiwix_icon) .setWhen(System.currentTimeMillis()); - hotspotNotificationChannel(); + hotspotNotificationChannel(); if (showStopButton) { Intent stopIntent = new Intent(ACTION_STOP); @@ -112,7 +112,7 @@ public class HotspotService extends Service { @RequiresApi(Build.VERSION_CODES.O) void stopHotspot() { - hotspotManager.turnOffHotspot(); + hotspotManager.turnOffHotspot(); stopForeground(true); stopSelf(); stopAndroidWebServer(); 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 1fbb7f1e6..4a315611c 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 @@ -94,7 +94,7 @@ public class WifiHotspotManager { AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle()); WebServerHelper webServerHelper = new WebServerHelper(context); builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { - webServerHelper.startServerDialog(); + webServerHelper.startServerHelper(); }); builder.setTitle(context.getString(R.string.hotspot_turned_on));