mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-23 04:33:54 -04:00
Use Service Context
Use Binder Fix no response from activity in onResume
This commit is contained in:
parent
09925dbf9c
commit
7bbc07d3bf
@ -23,14 +23,13 @@ public class WebServerHelper {
|
|||||||
public static boolean isStarted;
|
public static boolean isStarted;
|
||||||
static int port;
|
static int port;
|
||||||
private CoordinatorLayout coordinatorLayout;
|
private CoordinatorLayout coordinatorLayout;
|
||||||
static ServerStateListener listener;
|
|
||||||
String TAG = WebServerHelper.this.getClass().getSimpleName();
|
String TAG = WebServerHelper.this.getClass().getSimpleName();
|
||||||
|
|
||||||
public WebServerHelper(Context context) {
|
public WebServerHelper(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startServerHelper() {
|
public void startServerHelper(ServerStateListener stateListener) {
|
||||||
//TO DO:
|
//TO DO:
|
||||||
//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.
|
||||||
@ -38,16 +37,14 @@ public class WebServerHelper {
|
|||||||
//Always use 8080 and when its not available then iterate this number.
|
//Always use 8080 and when its not available then iterate this number.
|
||||||
if (!isStarted && startAndroidWebServer()) {
|
if (!isStarted && startAndroidWebServer()) {
|
||||||
isStarted = true;
|
isStarted = true;
|
||||||
listener = (ServerStateListener) context;
|
stateListener.serverStarted(getIpAddress() + ":" + port);
|
||||||
listener.serverStarted(getIpAddress() + ":" + port);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean stopAndroidWebServer(ServerStateListener stateListener) {
|
||||||
public static boolean stopAndroidWebServer() {
|
|
||||||
if (isStarted ) {
|
if (isStarted ) {
|
||||||
isStarted = false;
|
isStarted = false;
|
||||||
listener.serverStopped();
|
stateListener.serverStopped();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -55,6 +55,8 @@ public class ZimHostActivity extends AppCompatActivity implements
|
|||||||
|
|
||||||
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";
|
||||||
|
public static final String ACTION_CHECK_HOTSPOT_STATE = "Check_hotspot_state";
|
||||||
|
public static final String ACTION_START_SERVER = "start_server";
|
||||||
private final String IP_STATE_KEY = "ip_state_key";
|
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;
|
||||||
@ -88,6 +90,7 @@ public class ZimHostActivity extends AppCompatActivity implements
|
|||||||
HotspotService.HotspotBinder binder = (HotspotService.HotspotBinder) service;
|
HotspotService.HotspotBinder binder = (HotspotService.HotspotBinder) service;
|
||||||
hotspotService = binder.getService();
|
hotspotService = binder.getService();
|
||||||
bound = true;
|
bound = true;
|
||||||
|
hotspotService.registerCallBack(ZimHostActivity.this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -104,8 +107,6 @@ public class ZimHostActivity extends AppCompatActivity implements
|
|||||||
|
|
||||||
serviceIntent = new Intent(this, HotspotService.class);
|
serviceIntent = new Intent(this, HotspotService.class);
|
||||||
|
|
||||||
bindService();
|
|
||||||
|
|
||||||
startServerButton.setOnClickListener(new View.OnClickListener() {
|
startServerButton.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override public void onClick(View v) {
|
@Override public void onClick(View v) {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
@ -126,6 +127,16 @@ public class ZimHostActivity extends AppCompatActivity implements
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override protected void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
bindService();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override protected void onStop() {
|
||||||
|
super.onStop();
|
||||||
|
unbindService();
|
||||||
|
}
|
||||||
|
|
||||||
private void bindService() {
|
private void bindService() {
|
||||||
|
|
||||||
bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
|
bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
|
||||||
@ -145,13 +156,8 @@ public class ZimHostActivity extends AppCompatActivity implements
|
|||||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
|
||||||
== PackageManager.PERMISSION_GRANTED) {
|
== PackageManager.PERMISSION_GRANTED) {
|
||||||
|
|
||||||
if (hotspotService.checkHotspotState(this)) //If hotspot is already enabled, turn it off
|
startService(ACTION_CHECK_HOTSPOT_STATE); //If hotspot is already enabled, turn it off
|
||||||
{
|
|
||||||
startService(ACTION_TURN_OFF_AFTER_O);
|
|
||||||
} else //If hotspot is not already enabled, then turn it on.
|
|
||||||
{
|
|
||||||
setupLocationServices();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
//Ask location permission if not granted
|
//Ask location permission if not granted
|
||||||
ActivityCompat.requestPermissions(this,
|
ActivityCompat.requestPermissions(this,
|
||||||
@ -312,14 +318,13 @@ public class ZimHostActivity extends AppCompatActivity implements
|
|||||||
|
|
||||||
builder.setNeutralButton(getString(R.string.hotspot_dialog_neutral_button), (dialog, id) -> {
|
builder.setNeutralButton(getString(R.string.hotspot_dialog_neutral_button), (dialog, id) -> {
|
||||||
//TO DO: START SERVER WITHIN THE SERVICE.
|
//TO DO: START SERVER WITHIN THE SERVICE.
|
||||||
WebServerHelper webServerHelper = new WebServerHelper(this);
|
|
||||||
//Adding a handler because sometimes hotspot can take time to turn on.
|
//Adding a handler because sometimes hotspot can take time to turn on.
|
||||||
//TO DO: Add a progress dialog instead of handler
|
//TO DO: Add a progress dialog instead of handler
|
||||||
final Handler handler = new Handler();
|
final Handler handler = new Handler();
|
||||||
handler.postDelayed(new Runnable() {
|
handler.postDelayed(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
webServerHelper.startServerHelper();
|
startService(ACTION_START_SERVER);
|
||||||
}
|
}
|
||||||
}, 7000);
|
}, 7000);
|
||||||
});
|
});
|
||||||
@ -374,13 +379,13 @@ public class ZimHostActivity extends AppCompatActivity implements
|
|||||||
|
|
||||||
//Show an alert dialog for hotspot details
|
//Show an alert dialog for hotspot details
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(this, dialogStyle());
|
AlertDialog.Builder builder = new AlertDialog.Builder(this, dialogStyle());
|
||||||
WebServerHelper webServerHelper = new WebServerHelper(this);
|
|
||||||
builder.setPositiveButton(android.R.string.ok, (dialog, id) -> {
|
builder.setPositiveButton(android.R.string.ok, (dialog, id) -> {
|
||||||
final Handler handler = new Handler();
|
final Handler handler = new Handler();
|
||||||
handler.postDelayed(new Runnable() {
|
handler.postDelayed(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
webServerHelper.startServerHelper();
|
startService(ACTION_START_SERVER);
|
||||||
|
//webServerHelper.startServerHelper();
|
||||||
}
|
}
|
||||||
}, 2000);
|
}, 2000);
|
||||||
});
|
});
|
||||||
@ -397,6 +402,13 @@ public class ZimHostActivity extends AppCompatActivity implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public void hotspotState(Boolean state) {
|
@Override public void hotspotState(Boolean state) {
|
||||||
|
if (state) //if hotspot is already enabled, turn it off.
|
||||||
|
{
|
||||||
|
startService(ACTION_TURN_OFF_AFTER_O);
|
||||||
|
} else //If hotspot is not already enabled, then turn it on.
|
||||||
|
{
|
||||||
|
setupLocationServices();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,10 +20,12 @@ 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 org.kiwix.kiwixmobile.webserver.ServerStateListener;
|
||||||
|
import org.kiwix.kiwixmobile.webserver.WebServerHelper;
|
||||||
|
|
||||||
|
import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_CHECK_HOTSPOT_STATE;
|
||||||
|
import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_START_SERVER;
|
||||||
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;
|
||||||
import static org.kiwix.kiwixmobile.webserver.WebServerHelper.stopAndroidWebServer;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HotspotService is used to add a foreground service for the wifi hotspot.
|
* HotspotService is used to add a foreground service for the wifi hotspot.
|
||||||
@ -39,12 +41,15 @@ public class HotspotService extends Service {
|
|||||||
private NotificationCompat.Builder builder;
|
private NotificationCompat.Builder builder;
|
||||||
ServerStateListener serverStateListener;
|
ServerStateListener serverStateListener;
|
||||||
IBinder serviceBinder = new HotspotBinder();
|
IBinder serviceBinder = new HotspotBinder();
|
||||||
|
WebServerHelper webServerHelper;
|
||||||
String TAG = HotspotService.this.getClass().getSimpleName();
|
String TAG = HotspotService.this.getClass().getSimpleName();
|
||||||
|
|
||||||
@Override public void onCreate() {
|
@Override public void onCreate() {
|
||||||
|
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
|
||||||
|
hotspotManager = new WifiHotspotManager(this);
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
stopReceiver = new BroadcastReceiver() {
|
stopReceiver = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
@ -56,6 +61,9 @@ public class HotspotService extends Service {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
registerReceiver(stopReceiver, new IntentFilter(ACTION_STOP));
|
registerReceiver(stopReceiver, new IntentFilter(ACTION_STOP));
|
||||||
|
|
||||||
|
webServerHelper = new WebServerHelper(this);
|
||||||
|
|
||||||
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
startForeground(HOTSPOT_NOTIFICATION_ID,
|
startForeground(HOTSPOT_NOTIFICATION_ID,
|
||||||
buildForegroundNotification(getString(R.string.hotspot_start), false));
|
buildForegroundNotification(getString(R.string.hotspot_start), false));
|
||||||
@ -64,9 +72,16 @@ public class HotspotService extends Service {
|
|||||||
@Override public int onStartCommand(Intent intent, int flags, int startId) {
|
@Override public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
switch (intent.getAction()) {
|
switch (intent.getAction()) {
|
||||||
|
|
||||||
|
case ACTION_CHECK_HOTSPOT_STATE:
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
serverStateListener.hotspotState(hotspotManager.checkHotspotState());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case ACTION_TURN_ON_AFTER_O:
|
case ACTION_TURN_ON_AFTER_O:
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
hotspotManager.turnOnHotspot();
|
//serverStateListener.hotspotTurnedOn(hotspotManager.turnOnHotspot());
|
||||||
|
hotspotManager.turnOnHotspot(serverStateListener);
|
||||||
updateNotification(getString(R.string.hotspot_running), true);
|
updateNotification(getString(R.string.hotspot_running), true);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -76,6 +91,10 @@ public class HotspotService extends Service {
|
|||||||
stopHotspot();
|
stopHotspot();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case ACTION_START_SERVER:
|
||||||
|
webServerHelper.startServerHelper(serverStateListener);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -116,10 +135,12 @@ public class HotspotService extends Service {
|
|||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.O)
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
void stopHotspot() {
|
void stopHotspot() {
|
||||||
hotspotManager.turnOffHotspot();
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
hotspotManager.turnOffHotspot();
|
||||||
|
}
|
||||||
stopForeground(true);
|
stopForeground(true);
|
||||||
stopSelf();
|
stopSelf();
|
||||||
stopAndroidWebServer();
|
webServerHelper.stopAndroidWebServer(serverStateListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -142,14 +163,6 @@ public class HotspotService extends Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
|
||||||
public boolean checkHotspotState(Context context) {
|
|
||||||
if (hotspotManager == null) {
|
|
||||||
hotspotManager = new WifiHotspotManager(context);
|
|
||||||
}
|
|
||||||
return hotspotManager.checkHotspotState();
|
|
||||||
}
|
|
||||||
|
|
||||||
public class HotspotBinder extends Binder {
|
public class HotspotBinder extends Binder {
|
||||||
|
|
||||||
public HotspotService getService() {
|
public HotspotService getService() {
|
||||||
|
@ -7,12 +7,8 @@ import android.os.Build;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import androidx.annotation.RequiresApi;
|
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.ServerStateListener;
|
||||||
import org.kiwix.kiwixmobile.webserver.WebServerHelper;
|
|
||||||
|
|
||||||
import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* WifiHotstopManager class makes use of the Android's WifiManager and WifiConfiguration class
|
* WifiHotstopManager class makes use of the Android's WifiManager and WifiConfiguration class
|
||||||
@ -26,18 +22,17 @@ public class WifiHotspotManager {
|
|||||||
WifiManager.LocalOnlyHotspotReservation hotspotReservation;
|
WifiManager.LocalOnlyHotspotReservation hotspotReservation;
|
||||||
boolean oreoenabled;
|
boolean oreoenabled;
|
||||||
WifiConfiguration currentConfig;
|
WifiConfiguration currentConfig;
|
||||||
ServerStateListener serverStateListener;
|
boolean actionCompleted;
|
||||||
String TAG = WifiHotspotManager.this.getClass().getSimpleName();
|
String TAG = WifiHotspotManager.this.getClass().getSimpleName();
|
||||||
|
|
||||||
public WifiHotspotManager(Context context) {
|
public WifiHotspotManager(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
|
wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
|
||||||
serverStateListener = (ServerStateListener) context;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Workaround to turn on hotspot for Oreo versions
|
//Workaround to turn on hotspot for Oreo versions
|
||||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||||
public void turnOnHotspot() {
|
public void turnOnHotspot(ServerStateListener serverStateListener) {
|
||||||
if (!oreoenabled) {
|
if (!oreoenabled) {
|
||||||
wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
|
wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
|
||||||
|
|
||||||
@ -68,6 +63,7 @@ public class WifiHotspotManager {
|
|||||||
public void onFailed(int reason) {
|
public void onFailed(int reason) {
|
||||||
super.onFailed(reason);
|
super.onFailed(reason);
|
||||||
Log.v(TAG, "Local Hotspot failed to start");
|
Log.v(TAG, "Local Hotspot failed to start");
|
||||||
|
actionCompleted = false;
|
||||||
}
|
}
|
||||||
}, new Handler());
|
}, new Handler());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user