From 7bbc07d3bff5295e128f046b386ec636bcc0834b Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Fri, 9 Aug 2019 18:47:44 +0500 Subject: [PATCH] Use Service Context Use Binder Fix no response from activity in onResume --- .../webserver/WebServerHelper.java | 11 ++---- .../webserver/ZimHostActivity.java | 38 ++++++++++++------- .../wifi_hotspot/HotspotService.java | 37 ++++++++++++------ .../wifi_hotspot/WifiHotspotManager.java | 10 ++--- 4 files changed, 57 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java index bd794b64f..7997e4557 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.java @@ -23,14 +23,13 @@ public class WebServerHelper { public static boolean isStarted; static int port; private CoordinatorLayout coordinatorLayout; - static ServerStateListener listener; String TAG = WebServerHelper.this.getClass().getSimpleName(); public WebServerHelper(Context context) { this.context = context; } - public void startServerHelper() { + public void startServerHelper(ServerStateListener stateListener) { //TO DO: //1. Get port from settings screen //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. if (!isStarted && startAndroidWebServer()) { isStarted = true; - listener = (ServerStateListener) context; - listener.serverStarted(getIpAddress() + ":" + port); + stateListener.serverStarted(getIpAddress() + ":" + port); } } - - public static boolean stopAndroidWebServer() { + public static boolean stopAndroidWebServer(ServerStateListener stateListener) { if (isStarted ) { isStarted = false; - listener.serverStopped(); + stateListener.serverStopped(); return true; } return false; diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java index e501b0d21..7a4e36c59 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java @@ -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_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 static final int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 102; private Intent serviceIntent; @@ -88,6 +90,7 @@ public class ZimHostActivity extends AppCompatActivity implements HotspotService.HotspotBinder binder = (HotspotService.HotspotBinder) service; hotspotService = binder.getService(); bound = true; + hotspotService.registerCallBack(ZimHostActivity.this); } @Override @@ -104,8 +107,6 @@ public class ZimHostActivity extends AppCompatActivity implements serviceIntent = new Intent(this, HotspotService.class); - bindService(); - startServerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { 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() { 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) == PackageManager.PERMISSION_GRANTED) { - if (hotspotService.checkHotspotState(this)) //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(); - } + startService(ACTION_CHECK_HOTSPOT_STATE); //If hotspot is already enabled, turn it off + } else { //Ask location permission if not granted ActivityCompat.requestPermissions(this, @@ -312,14 +318,13 @@ public class ZimHostActivity extends AppCompatActivity implements builder.setNeutralButton(getString(R.string.hotspot_dialog_neutral_button), (dialog, id) -> { //TO DO: START SERVER WITHIN THE SERVICE. - WebServerHelper webServerHelper = new WebServerHelper(this); //Adding a handler because sometimes hotspot can take time to turn on. //TO DO: Add a progress dialog instead of handler final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { - webServerHelper.startServerHelper(); + startService(ACTION_START_SERVER); } }, 7000); }); @@ -374,13 +379,13 @@ public class ZimHostActivity extends AppCompatActivity implements //Show an alert dialog for hotspot details AlertDialog.Builder builder = new AlertDialog.Builder(this, dialogStyle()); - WebServerHelper webServerHelper = new WebServerHelper(this); builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { - webServerHelper.startServerHelper(); + startService(ACTION_START_SERVER); + //webServerHelper.startServerHelper(); } }, 2000); }); @@ -397,6 +402,13 @@ public class ZimHostActivity extends AppCompatActivity implements } @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(); + } } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java index 9aa0ed9ce..7caf443b9 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/HotspotService.java @@ -20,10 +20,12 @@ import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.main.MainActivity; import org.kiwix.kiwixmobile.utils.Constants; 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_ON_AFTER_O; -import static org.kiwix.kiwixmobile.webserver.WebServerHelper.stopAndroidWebServer; /** * 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; ServerStateListener serverStateListener; IBinder serviceBinder = new HotspotBinder(); + WebServerHelper webServerHelper; String TAG = HotspotService.this.getClass().getSimpleName(); @Override public void onCreate() { super.onCreate(); + hotspotManager = new WifiHotspotManager(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { stopReceiver = new BroadcastReceiver() { @Override @@ -56,6 +61,9 @@ public class HotspotService extends Service { }; } registerReceiver(stopReceiver, new IntentFilter(ACTION_STOP)); + + webServerHelper = new WebServerHelper(this); + notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); startForeground(HOTSPOT_NOTIFICATION_ID, 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) { 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: 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); } break; @@ -76,6 +91,10 @@ public class HotspotService extends Service { stopHotspot(); } break; + + case ACTION_START_SERVER: + webServerHelper.startServerHelper(serverStateListener); + break; default: break; } @@ -116,10 +135,12 @@ public class HotspotService extends Service { @RequiresApi(Build.VERSION_CODES.O) void stopHotspot() { - hotspotManager.turnOffHotspot(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + hotspotManager.turnOffHotspot(); + } stopForeground(true); stopSelf(); - stopAndroidWebServer(); + webServerHelper.stopAndroidWebServer(serverStateListener); } @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 HotspotService getService() { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java index eab6201c5..a7bcbfaaa 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -7,12 +7,8 @@ import android.os.Build; import android.os.Handler; 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; /** * WifiHotstopManager class makes use of the Android's WifiManager and WifiConfiguration class @@ -26,18 +22,17 @@ public class WifiHotspotManager { WifiManager.LocalOnlyHotspotReservation hotspotReservation; boolean oreoenabled; WifiConfiguration currentConfig; - ServerStateListener serverStateListener; + boolean actionCompleted; String TAG = WifiHotspotManager.this.getClass().getSimpleName(); public WifiHotspotManager(Context context) { this.context = context; wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE); - serverStateListener = (ServerStateListener) context; } //Workaround to turn on hotspot for Oreo versions @RequiresApi(api = Build.VERSION_CODES.O) - public void turnOnHotspot() { + public void turnOnHotspot(ServerStateListener serverStateListener) { if (!oreoenabled) { wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() { @@ -68,6 +63,7 @@ public class WifiHotspotManager { public void onFailed(int reason) { super.onFailed(reason); Log.v(TAG, "Local Hotspot failed to start"); + actionCompleted = false; } }, new Handler()); }