From 45d876b656ff058dc67644b0fafba10814ff075e Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Sat, 20 Jul 2019 02:50:43 +0500 Subject: [PATCH] Add WebServerHelper class Shift server related methods from MainActivity to here. Call methods of this class from HotspotService dealing with server. --- app/src/main/AndroidManifest.xml | 3 + .../kiwix/kiwixmobile/main/MainActivity.java | 168 ++---------------- .../webserver/WebServerHelper.java | 155 ++++++++++++++++ .../wifi_hotspot/HotspotService.java | 13 +- .../wifi_hotspot/WifiHotspotManager.java | 2 +- 5 files changed, 189 insertions(+), 152 deletions(-) create mode 100644 app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 866a151f6..b52ea2ea3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -61,6 +61,9 @@ android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/> + + bookmarks = new ArrayList<>(); private final List webViewList = new ArrayList<>(); private Intent serviceIntent; - private WebServer webServer; - private static final int DEFAULT_PORT = 8080; private BroadcastReceiver broadcastReceiverNetworkState; - private static boolean isStarted = false; - private CoordinatorLayout coordinatorLayout; - private EditText editTextPort; - private TextView textViewIpAccess; - private int port; + public static WebServerHelper webServerHelper; @BindView(R.id.activity_main_root) ConstraintLayout root; @BindView(R.id.activity_main_toolbar) @@ -789,12 +775,11 @@ public class MainActivity extends BaseActivity implements WebViewCallback, // TODO create a base Activity class that class this. FileUtils.deleteCachedFiles(this); tts.shutdown(); - - stopAndroidWebServer(); + webServerHelper.stopAndroidWebServer(); isStarted = false; - if (broadcastReceiverNetworkState != null) { - unregisterReceiver(broadcastReceiverNetworkState); - } + //if (broadcastReceiverNetworkState != null) { + // unregisterReceiver(broadcastReceiverNetworkState); + //} } private void updateTableOfContents() { @@ -976,14 +961,13 @@ public class MainActivity extends BaseActivity implements WebViewCallback, openExternalUrl(intentSupportKiwix); case R.id.menu_wifi_hotspot: - startServerDialog(); - //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - // toggleHotspot(); - //} else { - // if (showWritePermissionSettings()) { //request permission and if already granted switch hotspot. - // switchHotspot(); - // } - //} + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + toggleHotspot(); + } else { + if (showWritePermissionSettings()) { //request permission and if already granted switch hotspot. + switchHotspot(); + } + } default: break; @@ -992,65 +976,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback, return super.onOptionsItemSelected(item); } - private boolean startAndroidWebServer() { - if (!isStarted) { - port = getPortFromEditText(); - try { - if (port == 0) { - throw new Exception(); - } - - webServer = new WebServer(port); - webServer.start(); - //dialog.dismiss(); - return true; - } catch (Exception e) { - e.printStackTrace(); - Snackbar.make(coordinatorLayout, - "The PORT " + port + " doesn't work, please change it between 1000 and 9999.", - Snackbar.LENGTH_LONG).show(); - } - } - return false; - } - - private int getPortFromEditText() { - String valueEditText = editTextPort.getText().toString(); - 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 - private String getIpAddress() { - Log.v("DANG", "Inside getIpAdress()"); - String ip = ""; - try { - Enumeration enumNetworkInterfaces = NetworkInterface - .getNetworkInterfaces(); - while (enumNetworkInterfaces.hasMoreElements()) { - NetworkInterface networkInterface = enumNetworkInterfaces - .nextElement(); - Enumeration enumInetAddress = networkInterface - .getInetAddresses(); - while (enumInetAddress.hasMoreElements()) { - InetAddress inetAddress = enumInetAddress.nextElement(); - - if (inetAddress.isSiteLocalAddress()) { - ip += inetAddress.getHostAddress() + "\n"; - } - } - } - } catch (SocketException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - ip += "Something Wrong! " + e.toString() + "\n"; - } - Log.v("DANG", "Returning : " + "http://" + ip); - return "http://" + ip; - } private void initBroadcastReceiverNetworkStateChanged() { final IntentFilter filters = new IntentFilter(); @@ -1059,69 +984,12 @@ public class MainActivity extends BaseActivity implements WebViewCallback, broadcastReceiverNetworkState = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - setIpAccess(); + //setIpAccess(); } }; super.registerReceiver(broadcastReceiverNetworkState, filters); } - private boolean stopAndroidWebServer() { - if (isStarted && webServer != null) { - webServer.stop(); - return true; - } - return false; - } - - private void startServerDialog() { - AlertDialog.Builder alert = new AlertDialog.Builder(this); - alert.setTitle("Start the server"); - alert.setMessage("Happy sharing"); - - LinearLayout layout = new LinearLayout(this); - layout.setOrientation(LinearLayout.HORIZONTAL); - - textViewIpAccess = new TextView(this); - textViewIpAccess.setText("http://000.000.000.000"); - textViewIpAccess.setTextSize(20); - layout.addView(textViewIpAccess); - - TextView colonTextView = new TextView(this); - colonTextView.setTextSize(20); - colonTextView.setText(":"); - layout.addView(colonTextView); - - editTextPort = new EditText(this); - 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("Ok", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - if (!isStarted && startAndroidWebServer()) { - isStarted = true; - editTextPort.setEnabled(false); - } else if (stopAndroidWebServer()) { - isStarted = false; - editTextPort.setEnabled(true); - } - } - }); - - alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - // Canceled. - } - }); - alert.show(); - - setIpAccess(); - } /** Dialog to take user confirmation before deleting all notes */ private void showClearAllNotesDialog() { @@ -1243,9 +1111,9 @@ public class MainActivity extends BaseActivity implements WebViewCallback, startService(ACTION_TURN_OFF_BEFORE_O); } else { if (isMobileDataEnabled(this)) { - mobileDataDialog(); } else { + webServerHelper = new WebServerHelper(this); startService(ACTION_TURN_ON_BEFORE_O); } } @@ -2497,7 +2365,9 @@ public class MainActivity extends BaseActivity implements WebViewCallback, // requests here. //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + //webServerHelper = new WebServerHelper(this); startService(ACTION_TURN_ON_AFTER_O); + //} } catch (ApiException exception) { switch (exception.getStatusCode()) { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java new file mode 100644 index 000000000..08ff51451 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -0,0 +1,155 @@ +package org.kiwix.kiwixmobile.webserver; + +import android.app.Activity; +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; +import android.text.InputFilter; +import android.text.InputType; +import android.util.Log; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.TextView; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import com.google.android.material.snackbar.Snackbar; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Enumeration; +import org.kiwix.kiwixmobile.R; + +public class WebServerHelper extends Activity { + private static Context context; + private static TextView textViewIpAccess; + private static EditText editTextPort; + public static boolean isStarted; + private static int port; + private static WebServer webServer; + private static final int DEFAULT_PORT = 8080; + private static CoordinatorLayout coordinatorLayout; + +public WebServerHelper(Context context) +{ + this.context = context; +} + public static void startServerDialog() { + AlertDialog.Builder alert = new AlertDialog.Builder(context); + alert.setTitle("Start the server"); + alert.setMessage("Happy sharing"); + + LinearLayout layout = new LinearLayout(context); + layout.setOrientation(LinearLayout.HORIZONTAL); + + textViewIpAccess = new TextView(context); + textViewIpAccess.setText("http://000.000.000.000"); + 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("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + if (!isStarted && startAndroidWebServer()) { + isStarted = true; + editTextPort.setEnabled(false); + } else if (stopAndroidWebServer()) { + isStarted = false; + editTextPort.setEnabled(true); + } + } + }); + + alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + // Canceled. + } + }); + stopAndroidWebServer(); + isStarted = false; + alert.show(); + + setIpAccess(); + } + + public static boolean stopAndroidWebServer() { + if (isStarted && webServer != null) { + webServer.stop(); + return true; + } + return false; + } + + private static boolean startAndroidWebServer() { + if (!isStarted) { + port = getPortFromEditText(); + try { + if (port == 0) { + throw new Exception(); + } + webServer = new WebServer(port); + webServer.start(); + return true; + } catch (Exception e) { + e.printStackTrace(); + Snackbar.make(coordinatorLayout, + "The PORT " + port + " doesn't work, please change it between 1000 and 9999.", + Snackbar.LENGTH_LONG).show(); + } + } + return false; + } + + private static int getPortFromEditText() { + String valueEditText = editTextPort.getText().toString(); + return (valueEditText.length() > 0) ? Integer.parseInt(valueEditText) : DEFAULT_PORT; + } + + private static void setIpAccess() { + textViewIpAccess.setText(getIpAddress()); + } + + // get Ip address of the device's wireless access point i.e. wifi hotspot OR wifi network + private static String getIpAddress() { + Log.v("DANG", "Inside getIpAdress()"); + String ip = ""; + try { + Enumeration enumNetworkInterfaces = NetworkInterface + .getNetworkInterfaces(); + while (enumNetworkInterfaces.hasMoreElements()) { + NetworkInterface networkInterface = enumNetworkInterfaces + .nextElement(); + Enumeration enumInetAddress = networkInterface + .getInetAddresses(); + while (enumInetAddress.hasMoreElements()) { + InetAddress inetAddress = enumInetAddress.nextElement(); + + if (inetAddress.isSiteLocalAddress()) { + ip += inetAddress.getHostAddress() + "\n"; + } + } + } + } catch (SocketException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + ip += "Something Wrong! " + e.toString() + "\n"; + } + Log.v("DANG", "Returning : " + "http://" + ip); + return "http://" + ip; + } + +} 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 2a9c98e8a..6cf6b4f73 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 @@ -10,6 +10,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Build; +import android.os.Handler; import android.os.IBinder; import android.util.Log; import androidx.annotation.Nullable; @@ -23,7 +24,7 @@ import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_AFTER_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_BEFORE_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_AFTER_O; import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_BEFORE_O; -import static org.kiwix.kiwixmobile.main.MainActivity.startHotspotDetails; +import static org.kiwix.kiwixmobile.webserver.WebServerHelper.startServerDialog; /** * HotspotService is used to add a foreground service for the wifi hotspot. @@ -61,7 +62,15 @@ public class HotspotService extends Service { switch (intent.getAction()) { case ACTION_TURN_ON_BEFORE_O: if (hotspotManager.setWifiEnabled(null, true)) { - startHotspotDetails(); + final Handler handler = new Handler(); + handler.postDelayed(new Runnable() { + @Override + public void run() { + startServerDialog(); + } + }, 6000); + + //startHotspotDetails(); updateNotification(getString(R.string.hotspot_running), true); } break; 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 cbd172fed..8ffcf6ac6 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 @@ -65,7 +65,7 @@ public class WifiHotspotManager { + " \n SSID is : " + currentConfig.SSID); - hotspotDetailsDialog(); + //hotspotDetailsDialog(); oreoenabled = true; }