Add check for null ip address

Show toast
This commit is contained in:
Adeel Zafar 2019-08-11 20:08:07 +05:00
parent c0c67e5de9
commit 3f598a4ce6
4 changed files with 20 additions and 6 deletions

View File

@ -8,6 +8,8 @@ public interface ServerStateListener {
void serverStopped(); void serverStopped();
void serverFailed();
void hotspotTurnedOn(WifiConfiguration wifiConfiguration); void hotspotTurnedOn(WifiConfiguration wifiConfiguration);
void hotspotFailed(); void hotspotFailed();

View File

@ -22,7 +22,7 @@ public class WebServerHelper {
} }
public void startServerHelper(ServerStateListener stateListener) { public void startServerHelper(ServerStateListener stateListener) {
// 1. Get port from settings screen // 1. Get port from settings screen
// 2. Ask user to change port in settings if port is in use. // 2. Ask user to change port in settings if port is in use.
// OR // OR
@ -30,7 +30,13 @@ public class WebServerHelper {
if (!isServerStarted && startAndroidWebServer()) { if (!isServerStarted && startAndroidWebServer()) {
isServerStarted = true; isServerStarted = true;
stateListener.serverStarted(getIpAddress() + ":" + port); String ip = getIpAddress();
ip = ip.replaceAll("\n", "");
if (ip.length() == 0) {
stateListener.serverFailed();
} else {
stateListener.serverStarted("http://" + ip + ":" + port);
}
} }
} }
@ -85,10 +91,10 @@ public class WebServerHelper {
ip += "Something Wrong! " + e.toString() + "\n"; ip += "Something Wrong! " + e.toString() + "\n";
} }
return "http://" + ip; return ip;
} }
public static String getAddress() { public static String getAddress() {
return getIpAddress() + ":" + port; return "http://" + getIpAddress() + ":" + port;
} }
} }

View File

@ -18,6 +18,7 @@ import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
@ -375,9 +376,8 @@ public class ZimHostActivity extends BaseActivity implements
} }
@Override public void serverStarted(String ip) { @Override public void serverStarted(String ip) {
ip = ip.replaceAll("\n", "");
this.ip = ip; this.ip = ip;
serverTextView.setText(getString(R.string.server_started_message) + " " + ip); serverTextView.setText(getString(R.string.server_started_message) + " " + this.ip);
startServerButton.setText(getString(R.string.stop_server_label)); startServerButton.setText(getString(R.string.stop_server_label));
startServerButton.setBackgroundColor(getResources().getColor(R.color.stopServer)); startServerButton.setBackgroundColor(getResources().getColor(R.color.stopServer));
isServerStarted = true; isServerStarted = true;
@ -390,6 +390,11 @@ public class ZimHostActivity extends BaseActivity implements
isServerStarted = false; isServerStarted = false;
} }
@Override public void serverFailed() {
String message = String.format(getString(R.string.server_failed_message));
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
@Override public void hotspotTurnedOn(WifiConfiguration wifiConfiguration) { @Override public void hotspotTurnedOn(WifiConfiguration wifiConfiguration) {
//Show an alert dialog for hotspot details //Show an alert dialog for hotspot details

View File

@ -31,6 +31,7 @@
<string name="hotspot_start">Starting hotspot</string> <string name="hotspot_start">Starting hotspot</string>
<string name="hotspot_running">Running Hotspot</string> <string name="hotspot_running">Running Hotspot</string>
<string name="stop_hotspot_button">STOP</string> <string name="stop_hotspot_button">STOP</string>
<string name="server_failed_message">Couldnt start server. Please turn on your hotspot</string>
<string name="hotspot_turned_on">Hotspot turned on</string> <string name="hotspot_turned_on">Hotspot turned on</string>
<string name="hotspot_details_message">Following are the details of your local hotspot.</string> <string name="hotspot_details_message">Following are the details of your local hotspot.</string>
<string name="hotspot_ssid_label">SSID : </string> <string name="hotspot_ssid_label">SSID : </string>