Refactor ZimHostActivity

This commit is contained in:
Adeel Zafar 2019-08-08 22:53:37 +05:00
parent 28bb0e5d25
commit 09925dbf9c
2 changed files with 19 additions and 39 deletions

View File

@ -21,7 +21,7 @@ public class WebServerHelper {
private TextView textViewIpAccess; private TextView textViewIpAccess;
private EditText editTextPort; private EditText editTextPort;
public static boolean isStarted; public static boolean isStarted;
int port; static int port;
private CoordinatorLayout coordinatorLayout; private CoordinatorLayout coordinatorLayout;
static ServerStateListener listener; static ServerStateListener listener;
String TAG = WebServerHelper.this.getClass().getSimpleName(); String TAG = WebServerHelper.this.getClass().getSimpleName();
@ -63,7 +63,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
String getIpAddress() { static String getIpAddress() {
String ip = ""; String ip = "";
try { try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
@ -99,4 +99,8 @@ public class WebServerHelper {
Log.v("TAG", "Returning : " + "http://" + ip); Log.v("TAG", "Returning : " + "http://" + ip);
return "http://" + ip; return "http://" + ip;
} }
public static String getAddress() {
return getIpAddress() + ":" + port;
}
} }

View File

@ -4,7 +4,6 @@ import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.IntentSender; import android.content.IntentSender;
import android.content.ServiceConnection; import android.content.ServiceConnection;
@ -15,7 +14,6 @@ import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
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.TextView;
@ -45,8 +43,8 @@ import org.kiwix.kiwixmobile.wifi_hotspot.HotspotService;
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.ZimFileSelectFragment; import org.kiwix.kiwixmobile.zim_manager.fileselect_view.ZimFileSelectFragment;
import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle;
import static org.kiwix.kiwixmobile.webserver.WebServerHelper.getAddress;
import static org.kiwix.kiwixmobile.webserver.WebServerHelper.isStarted; import static org.kiwix.kiwixmobile.webserver.WebServerHelper.isStarted;
import static org.kiwix.kiwixmobile.webserver.WebServerHelper.stopAndroidWebServer;
public class ZimHostActivity extends AppCompatActivity implements public class ZimHostActivity extends AppCompatActivity implements
ServerStateListener { ServerStateListener {
@ -61,7 +59,6 @@ public class ZimHostActivity extends AppCompatActivity implements
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;
HotspotService hotspotService; HotspotService hotspotService;
String ip; String ip;
String TAG = ZimHostActivity.this.getClass().getSimpleName(); String TAG = ZimHostActivity.this.getClass().getSimpleName();
@ -118,9 +115,8 @@ public class ZimHostActivity extends AppCompatActivity implements
//if (isMobileDataEnabled(context)) { //if (isMobileDataEnabled(context)) {
// mobileDataDialog(); // mobileDataDialog();
//} else { //} else {
if (flag) { if (isStarted) {
serverStopped(); serverStopped();
isStarted = false;
} else { } else {
startHotspotDialog(); startHotspotDialog();
} }
@ -166,6 +162,14 @@ public class ZimHostActivity extends AppCompatActivity implements
@Override protected void onResume() { @Override protected void onResume() {
super.onResume(); super.onResume();
if (isStarted) {
ip = getAddress();
ip = ip.replaceAll("\n", "");
serverTextView.setText(
getString(R.string.server_started_message) + " " + ip);
startServerButton.setText(getString(R.string.stop_server_label));
}
} }
// This method checks if mobile data is enabled in user's device. // This method checks if mobile data is enabled in user's device.
@ -209,32 +213,6 @@ public class ZimHostActivity extends AppCompatActivity implements
} }
} }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
//Code for webserver
if (isStarted) {
new android.app.AlertDialog.Builder(this)
.setTitle("WARNING")
.setMessage("You've already a server running")
.setPositiveButton(getResources().getString(android.R.string.ok),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton(getResources().getString(android.R.string.cancel), null)
.show();
} else {
finish();
}
return true;
}
}
return false;
}
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch (requestCode) { switch (requestCode) {
@ -260,8 +238,6 @@ public class ZimHostActivity extends AppCompatActivity implements
@Override protected void onDestroy() { @Override protected void onDestroy() {
super.onDestroy(); super.onDestroy();
stopAndroidWebServer();
isStarted = false;
} }
private void setUpToolbar() { private void setUpToolbar() {
@ -385,13 +361,13 @@ public class ZimHostActivity extends AppCompatActivity implements
this.ip = ip; this.ip = ip;
serverTextView.setText(getString(R.string.server_started_message) + " " + ip); serverTextView.setText(getString(R.string.server_started_message) + " " + ip);
startServerButton.setText(getString(R.string.stop_server_label)); startServerButton.setText(getString(R.string.stop_server_label));
flag = true; isStarted = true;
} }
@Override public void serverStopped() { @Override public void serverStopped() {
serverTextView.setText(getString(R.string.server_textview_default_message)); serverTextView.setText(getString(R.string.server_textview_default_message));
startServerButton.setText(getString(R.string.start_server_label)); startServerButton.setText(getString(R.string.start_server_label));
flag = false; isStarted = false;
} }
@Override public void hotspotTurnedOn(WifiConfiguration wifiConfiguration) { @Override public void hotspotTurnedOn(WifiConfiguration wifiConfiguration) {
@ -426,7 +402,7 @@ public class ZimHostActivity extends AppCompatActivity implements
@Override protected void onSaveInstanceState(Bundle outState) { @Override protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
if (flag) { if (isStarted) {
outState.putString(IP_STATE_KEY, ip); outState.putString(IP_STATE_KEY, ip);
} }
} }