Add ServerStateListener

Show server started info on serverTextView
Change label of Start server button to stop server
This commit is contained in:
Adeel Zafar 2019-08-06 22:58:13 +05:00
parent da0b2f3b72
commit d6d79b2cf7
6 changed files with 59 additions and 27 deletions

View File

@ -0,0 +1,8 @@
package org.kiwix.kiwixmobile.webserver;
public interface ServerStateListener {
void serverStarted(String ip);
void serverStopped();
}

View File

@ -17,8 +17,6 @@ import java.net.SocketException;
import java.util.Enumeration; import java.util.Enumeration;
import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.R;
import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle;
/** /**
* WebServerHelper class is used to set up the suitable environment i.e. getting the * WebServerHelper class is used to set up the suitable environment i.e. getting the
* ip address and port no. before starting the WebServer * ip address and port no. before starting the WebServer
@ -26,13 +24,14 @@ import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle;
*/ */
public class WebServerHelper { public class WebServerHelper {
private Context context; Context context;
private TextView textViewIpAccess; private TextView textViewIpAccess;
private EditText editTextPort; private EditText editTextPort;
public static boolean isStarted; public static boolean isStarted;
private int port; int port;
private static WebServer webServer; private static WebServer webServer;
private CoordinatorLayout coordinatorLayout; private CoordinatorLayout coordinatorLayout;
ServerStateListener listener;
public WebServerHelper(Context context) { public WebServerHelper(Context context) {
this.context = context; this.context = context;
@ -73,7 +72,8 @@ public class WebServerHelper {
public void onClick(DialogInterface dialog, int whichButton) { public void onClick(DialogInterface dialog, int whichButton) {
if (!isStarted && startAndroidWebServer()) { if (!isStarted && startAndroidWebServer()) {
isStarted = true; isStarted = true;
serverStartedDialog(); listener = (ServerStateListener) context;
listener.serverStarted(getIpAddress()+port);
} }
} }
}); });
@ -118,7 +118,7 @@ public class WebServerHelper {
return false; return false;
} }
private int getPortFromEditText() { int getPortFromEditText() {
String valueEditText = editTextPort.getText().toString(); String valueEditText = editTextPort.getText().toString();
int DEFAULT_PORT = 8080; int DEFAULT_PORT = 8080;
return (valueEditText.length() > 0) ? Integer.parseInt(valueEditText) : DEFAULT_PORT; return (valueEditText.length() > 0) ? Integer.parseInt(valueEditText) : DEFAULT_PORT;
@ -129,7 +129,7 @@ public class WebServerHelper {
} }
// get Ip address of the device's wireless access point i.e. wifi hotspot OR wifi network // get Ip address of the device's wireless access point i.e. wifi hotspot OR wifi network
private String getIpAddress() { String getIpAddress() {
Log.v("DANG", "Inside getIpAdress()"); Log.v("DANG", "Inside getIpAdress()");
String ip = ""; String ip = "";
try { try {
@ -166,18 +166,4 @@ public class WebServerHelper {
Log.v("DANG", "Returning : " + "http://" + ip); Log.v("DANG", "Returning : " + "http://" + ip);
return "http://" + ip; return "http://" + ip;
} }
//Once server is started successfully, this dialog is shown.
void serverStartedDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle());
builder.setPositiveButton(android.R.string.ok, (dialog, id) -> {
});
builder.setTitle(context.getString(R.string.server_started_title));
builder.setMessage(
context.getString(R.string.server_started_message) + "\n " + getIpAddress() + ":" + port);
AlertDialog dialog = builder.create();
dialog.show();
}
} }

View File

@ -15,6 +15,7 @@ import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -47,15 +48,19 @@ import static org.kiwix.kiwixmobile.webserver.WebServerHelper.stopAndroidWebServ
import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.checkHotspotState; import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.checkHotspotState;
public class ZimHostActivity extends AppCompatActivity implements public class ZimHostActivity extends AppCompatActivity implements
ZimFileSelectFragment.OnHostActionButtonClickedListener { ZimFileSelectFragment.OnHostActionButtonClickedListener, ServerStateListener {
Button startServerButton; Button startServerButton;
TextView serverTextView;
public static final String ACTION_TURN_ON_AFTER_O = "Turn_on_hotspot_after_oreo"; public static final String ACTION_TURN_ON_AFTER_O = "Turn_on_hotspot_after_oreo";
public static final String ACTION_TURN_OFF_AFTER_O = "Turn_off_hotspot_after_oreo"; public static final String ACTION_TURN_OFF_AFTER_O = "Turn_off_hotspot_after_oreo";
private final String IP_STATE_KEY = "ip_state_key";
private static final int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 102; private static final int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 102;
private Intent serviceIntent; private Intent serviceIntent;
private Task<LocationSettingsResponse> task; private Task<LocationSettingsResponse> task;
boolean flag = false;
String ip;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -65,6 +70,14 @@ public class ZimHostActivity extends AppCompatActivity implements
setUpToolbar(); setUpToolbar();
startServerButton = (Button) findViewById(R.id.startServerButton); startServerButton = (Button) findViewById(R.id.startServerButton);
serverTextView = (TextView) findViewById(R.id.server_textView);
if (savedInstanceState != null) {
serverTextView.setText(
getString(R.string.server_started_message) + "\n" + savedInstanceState.getString(
IP_STATE_KEY));
startServerButton.setText(getString(R.string.stop_server_label));
}
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
@ -327,4 +340,24 @@ public class ZimHostActivity extends AppCompatActivity implements
dialog.show(); dialog.show();
} }
} }
@Override public void serverStarted(String ip) {
this.ip = ip;
serverTextView.setText(getString(R.string.server_started_message) + "\n" + ip);
startServerButton.setText(getString(R.string.stop_server_label));
flag = true;
}
@Override public void serverStopped() {
serverTextView.setText(getString(R.string.server_textview_default_message));
startServerButton.setText(getString(R.string.start_server_label));
flag = false;
}
@Override protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (flag) {
outState.putString(IP_STATE_KEY, ip);
}
}
} }

View File

@ -18,6 +18,7 @@ import androidx.core.app.NotificationCompat;
import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.R;
import org.kiwix.kiwixmobile.main.MainActivity; import org.kiwix.kiwixmobile.main.MainActivity;
import org.kiwix.kiwixmobile.utils.Constants; import org.kiwix.kiwixmobile.utils.Constants;
import org.kiwix.kiwixmobile.webserver.ServerStateListener;
import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_TURN_OFF_AFTER_O; import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_TURN_OFF_AFTER_O;
import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_TURN_ON_AFTER_O; import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_TURN_ON_AFTER_O;
@ -35,6 +36,7 @@ public class HotspotService extends Service {
private BroadcastReceiver stopReceiver; private BroadcastReceiver stopReceiver;
private NotificationManager notificationManager; private NotificationManager notificationManager;
private NotificationCompat.Builder builder; private NotificationCompat.Builder builder;
ServerStateListener serverStateListener;
@Override public void onCreate() { @Override public void onCreate() {
super.onCreate(); super.onCreate();
@ -67,11 +69,8 @@ public class HotspotService extends Service {
case ACTION_TURN_OFF_AFTER_O: case ACTION_TURN_OFF_AFTER_O:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
hotspotManager.turnOffHotspot(); stopHotspot();
} }
stopForeground(true);
stopSelf();
stopAndroidWebServer();
break; break;
default: default:
break; break;
@ -117,6 +116,7 @@ public class HotspotService extends Service {
stopForeground(true); stopForeground(true);
stopSelf(); stopSelf();
stopAndroidWebServer(); stopAndroidWebServer();
serverStateListener.serverStopped();
} }
@Override @Override

View File

@ -9,6 +9,7 @@ import android.util.Log;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.R;
import org.kiwix.kiwixmobile.webserver.ServerStateListener;
import org.kiwix.kiwixmobile.webserver.WebServerHelper; import org.kiwix.kiwixmobile.webserver.WebServerHelper;
import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle;
@ -21,10 +22,11 @@ import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle;
public class WifiHotspotManager { public class WifiHotspotManager {
private WifiManager wifiManager; private WifiManager wifiManager;
private Context context; Context context;
WifiManager.LocalOnlyHotspotReservation hotspotReservation; WifiManager.LocalOnlyHotspotReservation hotspotReservation;
boolean oreoenabled = false; boolean oreoenabled = false;
WifiConfiguration currentConfig; WifiConfiguration currentConfig;
ServerStateListener serverStateListener;
public WifiHotspotManager(Context context) { public WifiHotspotManager(Context context) {
this.context = context; this.context = context;
@ -57,6 +59,8 @@ public class WifiHotspotManager {
public void onStopped() { public void onStopped() {
super.onStopped(); super.onStopped();
Log.v("DANG", "Local Hotspot Stopped"); Log.v("DANG", "Local Hotspot Stopped");
serverStateListener = (ServerStateListener) context;
serverStateListener.serverStopped();
} }
@Override @Override

View File

@ -44,6 +44,7 @@
<string name="hotspot_dialog_positive_button">Okay</string> <string name="hotspot_dialog_positive_button">Okay</string>
<string name="sample_ip_address">http://000.000.000.000</string> <string name="sample_ip_address">http://000.000.000.000</string>
<string name="start_server_label">Start server</string> <string name="start_server_label">Start server</string>
<string name="stop_server_label">Stop server</string>
<string name="server_started_title">Server started</string> <string name="server_started_title">Server started</string>
<string name="server_started_message">Enter this ip address into your browser to access the server</string> <string name="server_started_message">Enter this ip address into your browser to access the server</string>
<string name="error_filenotfound">Error: The selected ZIM file could not be found.</string> <string name="error_filenotfound">Error: The selected ZIM file could not be found.</string>