Remove start server dialog

Start server without asking for port from user dialog
This commit is contained in:
Adeel Zafar 2019-08-06 23:15:30 +05:00
parent d6d79b2cf7
commit 93c71e79f1
4 changed files with 17 additions and 65 deletions

View File

@ -37,58 +37,20 @@ public class WebServerHelper {
this.context = context; this.context = context;
} }
//Dialog to start the server where user is shown the hotspot ip address can edit the port no. public void startServerHelper() {
public void startServerDialog() { //TO DO:
AlertDialog.Builder alert = new AlertDialog.Builder(context); //1. Get port from settings screen
alert.setTitle(R.string.start_server_dialog_title); //2. Ask user to change port in settings if port is in use.
alert.setMessage(R.string.start_server_dialog_message); //OR
//Always use 8080 and when its not available then iterate this number.
LinearLayout layout = new LinearLayout(context); if (!isStarted && startAndroidWebServer()) {
layout.setOrientation(LinearLayout.HORIZONTAL); isStarted = true;
listener = (ServerStateListener) context;
coordinatorLayout = new CoordinatorLayout(context); listener.serverStarted(getIpAddress() + port);
}
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 static boolean stopAndroidWebServer() { public static boolean stopAndroidWebServer() {
if (isStarted && webServer != null) { if (isStarted && webServer != null) {
webServer.stop(); webServer.stop();
@ -100,7 +62,7 @@ public class WebServerHelper {
boolean startAndroidWebServer() { boolean startAndroidWebServer() {
if (!isStarted) { if (!isStarted) {
port = getPortFromEditText(); port = 8080;
try { try {
if (port == 0) { if (port == 0) {
throw new Exception(); throw new Exception();
@ -118,16 +80,6 @@ public class WebServerHelper {
return false; 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 // get Ip address of the device's wireless access point i.e. wifi hotspot OR wifi network
String getIpAddress() { String getIpAddress() {
Log.v("DANG", "Inside getIpAdress()"); Log.v("DANG", "Inside getIpAdress()");

View File

@ -304,7 +304,7 @@ public class ZimHostActivity extends AppCompatActivity implements
handler.postDelayed(new Runnable() { handler.postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
webServerHelper.startServerDialog(); webServerHelper.startServerHelper();
} }
}, 5000); }, 5000);
}); });

View File

@ -94,7 +94,7 @@ public class HotspotService extends Service {
.setSmallIcon(R.mipmap.kiwix_icon) .setSmallIcon(R.mipmap.kiwix_icon)
.setWhen(System.currentTimeMillis()); .setWhen(System.currentTimeMillis());
hotspotNotificationChannel(); hotspotNotificationChannel();
if (showStopButton) { if (showStopButton) {
Intent stopIntent = new Intent(ACTION_STOP); Intent stopIntent = new Intent(ACTION_STOP);
@ -112,7 +112,7 @@ public class HotspotService extends Service {
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
void stopHotspot() { void stopHotspot() {
hotspotManager.turnOffHotspot(); hotspotManager.turnOffHotspot();
stopForeground(true); stopForeground(true);
stopSelf(); stopSelf();
stopAndroidWebServer(); stopAndroidWebServer();

View File

@ -94,7 +94,7 @@ public class WifiHotspotManager {
AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle()); AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle());
WebServerHelper webServerHelper = new WebServerHelper(context); WebServerHelper webServerHelper = new WebServerHelper(context);
builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { builder.setPositiveButton(android.R.string.ok, (dialog, id) -> {
webServerHelper.startServerDialog(); webServerHelper.startServerHelper();
}); });
builder.setTitle(context.getString(R.string.hotspot_turned_on)); builder.setTitle(context.getString(R.string.hotspot_turned_on));