Remove hotspot code for API<26 from WifiHotspotManager.java

Remove the code for turning on hotspot programmatically for API<26.
This commit is contained in:
Adeel Zafar 2019-08-01 23:28:53 +05:00
parent 995b20ce14
commit 543175d7a6

View File

@ -8,7 +8,6 @@ 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 androidx.appcompat.app.AlertDialog;
import java.lang.reflect.Method;
import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.R;
import org.kiwix.kiwixmobile.webserver.WebServerHelper; import org.kiwix.kiwixmobile.webserver.WebServerHelper;
@ -32,23 +31,6 @@ public class WifiHotspotManager {
wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE); wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
} }
// This method enables/disables the wifi access point
// It is used for API<26
public boolean setWifiEnabled(WifiConfiguration wifiConfig, boolean enabled) {
try {
if (enabled) { //disables wifi hotspot if it's already enabled
wifiManager.setWifiEnabled(false);
}
Method method = wifiManager.getClass()
.getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
return (Boolean) method.invoke(wifiManager, wifiConfig, enabled);
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
return false;
}
}
//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() {
@ -103,87 +85,20 @@ public class WifiHotspotManager {
return hotspotReservation != null; return hotspotReservation != null;
} }
// This method returns the current state of the Wifi access point
private WIFI_AP_STATE_ENUMS getWifiApState() {
try {
Method method = wifiManager.getClass().getMethod("getWifiApState");
int tmp = ((Integer) method.invoke(wifiManager));
// Fix for Android 4
if (tmp >= 10) {
tmp = tmp - 10;
}
return WIFI_AP_STATE_ENUMS.class.getEnumConstants()[tmp];
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
return WIFI_AP_STATE_ENUMS.WIFI_AP_STATE_FAILED;
}
}
//This method returns if wifi access point is enabled or not
public boolean isWifiApEnabled() {
return getWifiApState() == WIFI_AP_STATE_ENUMS.WIFI_AP_STATE_ENABLED;
}
//This method is to get the wifi ap configuration
private WifiConfiguration getWifiApConfiguration() {
try {
Method method = wifiManager.getClass().getMethod("getWifiApConfiguration");
return (WifiConfiguration) method.invoke(wifiManager);
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
return null;
}
}
//This method is to set the wifi ap configuration
public boolean setWifiApConfiguration(WifiConfiguration wifiConfig) {
try {
Method method =
wifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);
return (Boolean) method.invoke(wifiManager, wifiConfig);
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
return false;
}
}
public void hotspotDetailsDialog() { public void hotspotDetailsDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle()); AlertDialog.Builder builder = new AlertDialog.Builder(context, dialogStyle());
WebServerHelper webServerHelper = new WebServerHelper(context); WebServerHelper webServerHelper = new WebServerHelper(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setPositiveButton(android.R.string.ok, (dialog, id) -> { builder.setPositiveButton(android.R.string.ok, (dialog, id) -> {
webServerHelper.startServerDialog(); webServerHelper.startServerDialog();
}); });
} else {
builder.setPositiveButton(android.R.string.ok, (dialog, id) -> {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
webServerHelper.startServerDialog();
}
}, 6000);
});
}
builder.setTitle(context.getString(R.string.hotspot_turned_on)); builder.setTitle(context.getString(R.string.hotspot_turned_on));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setMessage( builder.setMessage(
context.getString(R.string.hotspot_details_message) + "\n" + context.getString( context.getString(R.string.hotspot_details_message) + "\n" + context.getString(
R.string.hotspot_ssid_label) + " " + currentConfig.SSID + "\n" + context.getString( R.string.hotspot_ssid_label) + " " + currentConfig.SSID + "\n" + context.getString(
R.string.hotspot_pass_label) + " " + currentConfig.preSharedKey); R.string.hotspot_pass_label) + " " + currentConfig.preSharedKey);
} else {
currentConfig = getWifiApConfiguration();
builder.setMessage(
context.getString(R.string.hotspot_details_message) + "\n" + context.getString(
R.string.hotspot_ssid_label) + " " + currentConfig.SSID + "\n" + context.getString(
R.string.hotspot_pass_label) + " " + currentConfig.preSharedKey);
}
builder.setCancelable(false); builder.setCancelable(false);
AlertDialog dialog = builder.create(); AlertDialog dialog = builder.create();
dialog.show(); dialog.show();