Fixed local hotspot not stopping bug on devices >=O

Now checking hotspotstate from within hotspot service
using the context from main activity.
This commit is contained in:
Adeel Zafar 2019-07-05 20:47:31 +05:00
parent 1f5a52484f
commit ecf134a632
3 changed files with 27 additions and 14 deletions

View File

@ -170,6 +170,7 @@ import static org.kiwix.kiwixmobile.utils.Constants.TAG_KIWIX;
import static org.kiwix.kiwixmobile.utils.LanguageUtils.getResourceString;
import static org.kiwix.kiwixmobile.utils.StyleUtils.dialogStyle;
import static org.kiwix.kiwixmobile.utils.UpdateUtils.reformatProviderUrl;
import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.checkHotspotState;
public class MainActivity extends BaseActivity implements WebViewCallback,
MainContract.View {
@ -1083,7 +1084,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
private void switchHotspot() {
if (wifiHotspotManager.isWifiApEnabled()) {
startService(ACTION_TURN_OFF_BEFORE_O);
//wifiHotspotManager.setWifiEnabled(null, false);
//hotspotManager.setWifiEnabled(null, false);
} else {
//Check if user's hotspot is enabled
if (isMobileDataEnabled(this)) {
@ -1093,7 +1094,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
// potentially add data to the intent
//i.putExtra("TURN_ON_HOTSPOT_BEFORE_O", "turnOnHotspotBeforeO");
startService(ACTION_TURN_ON_BEFORE_O);
//wifiHotspotManager.setWifiEnabled(null, true);
//hotspotManager.setWifiEnabled(null, true);
}
}
}
@ -1131,11 +1132,11 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
Log.v("DANG", "Turn off 0");
if (wifiHotspotManager.checkHotspotState()) //If hotspot is already enabled, turn it off
if (checkHotspotState(this)) //If hotspot is already enabled, turn it off
{
Log.v("DANG", "Turn off 1");
startService(ACTION_TURN_OFF_AFTER_O);
//wifiHotspotManager.turnOffHotspot();
//hotspotManager.turnOffHotspot();
} else //If hotspot is not already enabled, then turn it on.
{
setupLocationServices();
@ -2318,7 +2319,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
//if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startService(ACTION_TURN_ON_AFTER_O);
//wifiHotspotManager.turnOnHotspot();
//hotspotManager.turnOnHotspot();
//}
} catch (ApiException exception) {
switch (exception.getStatusCode()) {

View File

@ -13,6 +13,7 @@ import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import org.kiwix.kiwixmobile.R;
import org.kiwix.kiwixmobile.main.MainActivity;
@ -28,14 +29,16 @@ public class HotspotService extends Service {
public static final String ACTION_START = "hotspot_start";
public static final String ACTION_STOP = "hotspot_stop";
public static final String ACTION_STATUS = "hotspot_status";
private WifiHotspotManager wifiHotspotManager;
public static WifiHotspotManager hotspotManager;
private BroadcastReceiver stopReceiver;
private NotificationManager notificationManager;
private NotificationCompat.Builder builder;
@Override public void onCreate() {
super.onCreate();
wifiHotspotManager = new WifiHotspotManager(this);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
hotspotManager = new WifiHotspotManager(this);
}
stopReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
@ -53,28 +56,28 @@ public class HotspotService extends Service {
@Override public int onStartCommand(Intent intent, int flags, int startId) {
switch (intent.getAction()) {
case ACTION_TURN_ON_BEFORE_O:
if (wifiHotspotManager.setWifiEnabled(null, true)) {
if (hotspotManager.setWifiEnabled(null, true)) {
updateNotification(getString(R.string.hotspot_running), true);
}
break;
case ACTION_TURN_ON_AFTER_O:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.v("DANG","Coming after 3");
wifiHotspotManager.turnOnHotspot();
hotspotManager.turnOnHotspot();
//if(it gets turned on successfully) then it goes to catch in MainActivity
updateNotification(getString(R.string.hotspot_running), true);
Log.v("DANG","Coming after calling updateNotification 8");
}
break;
case ACTION_TURN_OFF_BEFORE_O:
wifiHotspotManager.setWifiEnabled(null, false);
hotspotManager.setWifiEnabled(null, false);
stopForeground(true);
stopSelf();
break;
case ACTION_TURN_OFF_AFTER_O:
Log.v("DANG","Turn off 3");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
wifiHotspotManager.turnOffHotspot();
hotspotManager.turnOffHotspot();
}
stopForeground(true);
stopSelf();
@ -119,10 +122,10 @@ public class HotspotService extends Service {
private void stopHotspot() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
wifiHotspotManager.turnOffHotspot();
hotspotManager.turnOffHotspot();
} else {
Log.v("DANG", "Coming yes");
wifiHotspotManager.setWifiEnabled(null, false);
hotspotManager.setWifiEnabled(null, false);
}
stopForeground(true);
stopSelf();
@ -149,4 +152,13 @@ public class HotspotService extends Service {
Log.v("DANG","Building notification channel end : 1.1");
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
public static boolean checkHotspotState(Context context) {
if (hotspotManager == null) {
Log.v("DANG", "hotspotManager initialized");
hotspotManager = new WifiHotspotManager(context);
}
return hotspotManager.checkHotspotState();
}
}

View File

@ -85,7 +85,7 @@ public class WifiHotspotManager {
+ " \n SSID is : "
+ currentConfig.SSID);
//hotspotDetailsDialog();
hotspotDetailsDialog();
oreoenabled = true;
}