mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-22 12:03:09 -04:00
Add ServerStateListener
Show server started info on serverTextView Change label of Start server button to stop server
This commit is contained in:
parent
da0b2f3b72
commit
d6d79b2cf7
@ -0,0 +1,8 @@
|
||||
package org.kiwix.kiwixmobile.webserver;
|
||||
|
||||
public interface ServerStateListener {
|
||||
|
||||
void serverStarted(String ip);
|
||||
|
||||
void serverStopped();
|
||||
}
|
@ -17,8 +17,6 @@ import java.net.SocketException;
|
||||
import java.util.Enumeration;
|
||||
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
|
||||
* ip address and port no. before starting the WebServer
|
||||
@ -26,13 +24,14 @@ import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle;
|
||||
*/
|
||||
|
||||
public class WebServerHelper {
|
||||
private Context context;
|
||||
Context context;
|
||||
private TextView textViewIpAccess;
|
||||
private EditText editTextPort;
|
||||
public static boolean isStarted;
|
||||
private int port;
|
||||
int port;
|
||||
private static WebServer webServer;
|
||||
private CoordinatorLayout coordinatorLayout;
|
||||
ServerStateListener listener;
|
||||
|
||||
public WebServerHelper(Context context) {
|
||||
this.context = context;
|
||||
@ -73,7 +72,8 @@ public class WebServerHelper {
|
||||
public void onClick(DialogInterface dialog, int whichButton) {
|
||||
if (!isStarted && startAndroidWebServer()) {
|
||||
isStarted = true;
|
||||
serverStartedDialog();
|
||||
listener = (ServerStateListener) context;
|
||||
listener.serverStarted(getIpAddress()+port);
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -118,7 +118,7 @@ public class WebServerHelper {
|
||||
return false;
|
||||
}
|
||||
|
||||
private int getPortFromEditText() {
|
||||
int getPortFromEditText() {
|
||||
String valueEditText = editTextPort.getText().toString();
|
||||
int DEFAULT_PORT = 8080;
|
||||
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
|
||||
private String getIpAddress() {
|
||||
String getIpAddress() {
|
||||
Log.v("DANG", "Inside getIpAdress()");
|
||||
String ip = "";
|
||||
try {
|
||||
@ -166,18 +166,4 @@ public class WebServerHelper {
|
||||
Log.v("DANG", "Returning : " + "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();
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ import android.util.Log;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import androidx.annotation.NonNull;
|
||||
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;
|
||||
|
||||
public class ZimHostActivity extends AppCompatActivity implements
|
||||
ZimFileSelectFragment.OnHostActionButtonClickedListener {
|
||||
ZimFileSelectFragment.OnHostActionButtonClickedListener, ServerStateListener {
|
||||
|
||||
Button startServerButton;
|
||||
TextView serverTextView;
|
||||
|
||||
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";
|
||||
private final String IP_STATE_KEY = "ip_state_key";
|
||||
private static final int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 102;
|
||||
private Intent serviceIntent;
|
||||
private Task<LocationSettingsResponse> task;
|
||||
boolean flag = false;
|
||||
String ip;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -65,6 +70,14 @@ public class ZimHostActivity extends AppCompatActivity implements
|
||||
setUpToolbar();
|
||||
|
||||
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();
|
||||
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
|
||||
@ -327,4 +340,24 @@ public class ZimHostActivity extends AppCompatActivity implements
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ import androidx.core.app.NotificationCompat;
|
||||
import org.kiwix.kiwixmobile.R;
|
||||
import org.kiwix.kiwixmobile.main.MainActivity;
|
||||
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_ON_AFTER_O;
|
||||
@ -35,6 +36,7 @@ public class HotspotService extends Service {
|
||||
private BroadcastReceiver stopReceiver;
|
||||
private NotificationManager notificationManager;
|
||||
private NotificationCompat.Builder builder;
|
||||
ServerStateListener serverStateListener;
|
||||
|
||||
@Override public void onCreate() {
|
||||
super.onCreate();
|
||||
@ -67,11 +69,8 @@ public class HotspotService extends Service {
|
||||
|
||||
case ACTION_TURN_OFF_AFTER_O:
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
hotspotManager.turnOffHotspot();
|
||||
stopHotspot();
|
||||
}
|
||||
stopForeground(true);
|
||||
stopSelf();
|
||||
stopAndroidWebServer();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -117,6 +116,7 @@ public class HotspotService extends Service {
|
||||
stopForeground(true);
|
||||
stopSelf();
|
||||
stopAndroidWebServer();
|
||||
serverStateListener.serverStopped();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -9,6 +9,7 @@ import android.util.Log;
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import org.kiwix.kiwixmobile.R;
|
||||
import org.kiwix.kiwixmobile.webserver.ServerStateListener;
|
||||
import org.kiwix.kiwixmobile.webserver.WebServerHelper;
|
||||
|
||||
import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle;
|
||||
@ -21,10 +22,11 @@ import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle;
|
||||
|
||||
public class WifiHotspotManager {
|
||||
private WifiManager wifiManager;
|
||||
private Context context;
|
||||
Context context;
|
||||
WifiManager.LocalOnlyHotspotReservation hotspotReservation;
|
||||
boolean oreoenabled = false;
|
||||
WifiConfiguration currentConfig;
|
||||
ServerStateListener serverStateListener;
|
||||
|
||||
public WifiHotspotManager(Context context) {
|
||||
this.context = context;
|
||||
@ -57,6 +59,8 @@ public class WifiHotspotManager {
|
||||
public void onStopped() {
|
||||
super.onStopped();
|
||||
Log.v("DANG", "Local Hotspot Stopped");
|
||||
serverStateListener = (ServerStateListener) context;
|
||||
serverStateListener.serverStopped();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -44,6 +44,7 @@
|
||||
<string name="hotspot_dialog_positive_button">Okay</string>
|
||||
<string name="sample_ip_address">http://000.000.000.000</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_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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user