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 81c3b4640..6870c14ce 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostActivity.java @@ -2,10 +2,12 @@ package org.kiwix.kiwixmobile.webserver; import android.Manifest; import android.app.Activity; +import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.IntentFilter; import android.content.IntentSender; import android.content.pm.PackageManager; import android.net.ConnectivityManager; @@ -28,6 +30,7 @@ import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; import com.google.android.gms.common.api.ApiException; import com.google.android.gms.common.api.ResolvableApiException; import com.google.android.gms.location.LocationRequest; @@ -45,16 +48,18 @@ import org.kiwix.kiwixmobile.zim_manager.fileselect_view.ZimFileSelectFragment; import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle; import static org.kiwix.kiwixmobile.webserver.WebServerHelper.isStarted; import static org.kiwix.kiwixmobile.webserver.WebServerHelper.stopAndroidWebServer; -import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.checkHotspotState; public class ZimHostActivity extends AppCompatActivity implements ServerStateListener { Button startServerButton; TextView serverTextView; + private BroadcastReceiver broadcastReceiver; + private boolean bound; 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"; private final String IP_STATE_KEY = "ip_state_key"; private static final int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 102; private Intent serviceIntent; @@ -89,6 +94,21 @@ public class ZimHostActivity extends AppCompatActivity implements serviceIntent = new Intent(this, HotspotService.class); Context context = this; + broadcastReceiver = new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + if (intent.getBooleanExtra("hotspot_state", false)) { + startService(ACTION_TURN_OFF_AFTER_O); + } else //If hotspot is not already enabled, then turn it on. + { + setupLocationServices(); + } + } + }; + LocalBroadcastManager.getInstance(this) + .registerReceiver(broadcastReceiver, new IntentFilter(ACTION_CHECK_HOTSPOT_STATE)); + startServerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -115,13 +135,7 @@ public class ZimHostActivity extends AppCompatActivity implements //Check if location permissions are granted if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { - if (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); } else { //Ask location permission if not granted ActivityCompat.requestPermissions(this, @@ -130,6 +144,12 @@ public class ZimHostActivity extends AppCompatActivity implements } } + @Override protected void onResume() { + super.onResume(); + LocalBroadcastManager.getInstance(this) + .registerReceiver(broadcastReceiver, new IntentFilter(ACTION_CHECK_HOTSPOT_STATE)); + } + // This method checks if mobile data is enabled in user's device. static boolean isMobileDataEnabled(Context context) { boolean enabled = false; @@ -236,7 +256,7 @@ public class ZimHostActivity extends AppCompatActivity implements toolbar.setNavigationOnClickListener(v -> onBackPressed()); } - private void setupLocationServices() { + void setupLocationServices() { LocationRequest mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(10); mLocationRequest.setSmallestDisplacement(10); @@ -318,7 +338,7 @@ public class ZimHostActivity extends AppCompatActivity implements dialog.show(); } - private void startService(String ACTION) { + void startService(String ACTION) { serviceIntent.setAction(ACTION); this.startService(serviceIntent); } 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 e9750dd02..21a8c5ed8 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 @@ -15,10 +15,12 @@ import android.util.Log; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.main.MainActivity; import org.kiwix.kiwixmobile.utils.Constants; +import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_CHECK_HOTSPOT_STATE; 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; @@ -31,13 +33,15 @@ import static org.kiwix.kiwixmobile.webserver.WebServerHelper.stopAndroidWebServ public class HotspotService extends Service { private static final int HOTSPOT_NOTIFICATION_ID = 666; private static final String ACTION_STOP = "hotspot_stop"; - private static WifiHotspotManager hotspotManager; + private WifiHotspotManager hotspotManager; private BroadcastReceiver stopReceiver; private NotificationManager notificationManager; private NotificationCompat.Builder builder; String TAG = HotspotService.this.getClass().getSimpleName(); @Override public void onCreate() { + + hotspotManager = new WifiHotspotManager(this); super.onCreate(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -59,6 +63,12 @@ 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) { + sendBroadcast(hotspotManager.checkHotspotState()); + } + break; + case ACTION_TURN_ON_AFTER_O: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { hotspotManager.turnOnHotspot(); @@ -81,6 +91,14 @@ public class HotspotService extends Service { return null; } + private void sendBroadcast(boolean success) { + Intent intent = new Intent( + ACTION_CHECK_HOTSPOT_STATE); //put the same message as in the filter you used in the activity when registering the receiver + intent.putExtra("hotspot_state", success); + LocalBroadcastManager.getInstance(this).sendBroadcast(intent); + } + + private Notification buildForegroundNotification(String status, boolean showStopButton) { Log.v(TAG, "Building notification " + status); builder = new NotificationCompat.Builder(this); @@ -136,12 +154,4 @@ public class HotspotService extends Service { notificationManager.createNotificationChannel(hotspotServiceChannel); } } - - @RequiresApi(api = Build.VERSION_CODES.O) - public static boolean checkHotspotState(Context context) { - if (hotspotManager == null) { - hotspotManager = new WifiHotspotManager(context); - } - return hotspotManager.checkHotspotState(); - } }