Skip to content

Commit 3616031

Browse files
committed
add hotspot toggle feature also for for Android pre-Oreo; lower minsdk to 21
1 parent 1529445 commit 3616031

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77

88
defaultConfig {
99
applicationId "it.pgp.currenttoggles"
10-
minSdkVersion 24
10+
minSdkVersion 21
1111
targetSdkVersion 28
1212
versionCode 111220816
1313
versionName "1.1.1"

app/src/main/java/it/pgp/currenttoggles/MainActivity.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import it.pgp.currenttoggles.utils.RootHandler;
2727
import it.pgp.currenttoggles.utils.oreoap.MyOnStartTetheringCallback;
2828
import it.pgp.currenttoggles.utils.oreoap.MyOreoWifiManager;
29+
import it.pgp.currenttoggles.utils.oreoap.PreOreoWifiManager;
2930

3031
public class MainActivity extends Activity {
3132

@@ -239,18 +240,28 @@ public static void toggleEnergySaving(Context context) {
239240
}
240241

241242
public static void toggleHotspot(Context context) {
242-
if (!Settings.System.canWrite(context)) {
243+
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.System.canWrite(context)) {
243244
launchWriteSettings(context);
244245
}
245246
else {
246-
MyOreoWifiManager apManager = new MyOreoWifiManager(context);
247-
if(apManager.isTetherActive()) {
248-
apManager.stopTethering();
249-
Toast.makeText(context, "AP stopped", Toast.LENGTH_SHORT).show();
247+
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
248+
PreOreoWifiManager apManager = new PreOreoWifiManager(context);
249+
boolean targetState = !apManager.isApOn();
250+
if(apManager.configApState(targetState))
251+
Toast.makeText(context, "AP "+(targetState?"started":"stopped"), Toast.LENGTH_SHORT).show();
252+
else
253+
Toast.makeText(context, "Unable to change AP state", Toast.LENGTH_SHORT).show();
250254
}
251255
else {
252-
apManager.startTethering(new MyOnStartTetheringCallback());
253-
Toast.makeText(context, "AP started", Toast.LENGTH_SHORT).show();
256+
MyOreoWifiManager apManager = new MyOreoWifiManager(context);
257+
if(apManager.isTetherActive()) {
258+
apManager.stopTethering();
259+
Toast.makeText(context, "AP stopped", Toast.LENGTH_SHORT).show();
260+
}
261+
else {
262+
apManager.startTethering(new MyOnStartTetheringCallback());
263+
Toast.makeText(context, "AP started", Toast.LENGTH_SHORT).show();
264+
}
254265
}
255266
}
256267
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package it.pgp.currenttoggles.utils.oreoap;
2+
3+
import android.content.Context;
4+
import android.net.wifi.WifiConfiguration;
5+
import android.net.wifi.WifiManager;
6+
7+
import java.lang.reflect.Method;
8+
9+
public class PreOreoWifiManager {
10+
final Context context;
11+
12+
public PreOreoWifiManager(Context context) {
13+
this.context = context;
14+
}
15+
16+
public boolean isApOn() {
17+
WifiManager wifimanager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
18+
try {
19+
Method method = wifimanager.getClass().getDeclaredMethod("isWifiApEnabled");
20+
method.setAccessible(true);
21+
return (Boolean) method.invoke(wifimanager);
22+
}
23+
catch (Throwable ignored) {}
24+
return false;
25+
}
26+
27+
// toggle wifi hotspot on or off
28+
public boolean configApState(boolean expectedState) {
29+
WifiManager wifimanager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
30+
WifiConfiguration wificonfiguration = null;
31+
try {
32+
// if WiFi is on, turn it off // FIXME isApOn checks ap, not wifi
33+
if(isApOn()) wifimanager.setWifiEnabled(false);
34+
Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
35+
// method.invoke(wifimanager, wificonfiguration, !isApOn(context));
36+
method.invoke(wifimanager, wificonfiguration, expectedState);
37+
return true;
38+
}
39+
catch (Exception e) {
40+
e.printStackTrace();
41+
}
42+
return false;
43+
}
44+
}

0 commit comments

Comments
 (0)