From 2289a8821ff4bcfdef0553795e7d8fe4d3834092 Mon Sep 17 00:00:00 2001 From: Adeel Zafar Date: Tue, 28 May 2019 16:50:25 +0500 Subject: [PATCH] Add wifi hotspot manager class This classes manages all the wifi-hotspot logic. It uses WifiManager and WifiConfiguration classes by Android. Add method to check show write permission settings. Add method setWifiEnabled to setup wifi hotspot Add setters/getters for WifiConfiguration --- .../wifi_hotspot/WifiHotspotManager.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java 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 new file mode 100644 index 000000000..9a69464f0 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/wifi_hotspot/WifiHotspotManager.java @@ -0,0 +1,68 @@ +package org.kiwix.kiwixmobile.wifi_hotspot; + +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiManager; +import android.os.Build; +import android.provider.Settings; +import android.util.Log; +import java.lang.reflect.Method; + +public class WifiHotspotManager { + private final WifiManager wifiManager; + private Context context; + + public WifiHotspotManager(Context context) { + this.context = context; + wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE); + } + + public void showWritePermissionSettings(boolean force) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (force || !Settings.System.canWrite(this.context)) { + Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS); + intent.setData(Uri.parse("package:" + this.context.getPackageName())); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + this.context.startActivity(intent); + } + } + } + + public boolean setWifiEnabled(WifiConfiguration wifiConfig, boolean enabled) { + try { + if (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; + } + } + + public 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; + } + } + + 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; + } + } +}