WRITE_Settings permission not given bug

Handle if write settings permission is not given.
This commit is contained in:
Adeel Zafar 2019-07-06 20:34:28 +05:00
parent 5a8cbb2efc
commit 16e310793d
2 changed files with 20 additions and 24 deletions

View File

@ -411,7 +411,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
drawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); drawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
wifiHotspotManager = new WifiHotspotManager(this); wifiHotspotManager = new WifiHotspotManager(this);
wifiHotspotManager.showWritePermissionSettings();
serviceIntent = new Intent(this, HotspotService.class); serviceIntent = new Intent(this, HotspotService.class);
} }
@ -955,8 +954,10 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
toggleHotspot(); toggleHotspot();
} else { } else {
if (showWritePermissionSettings()) { //request permission and if already granted switch hotspot.
switchHotspot(); switchHotspot();
} }
}
default: default:
break; break;
@ -1085,7 +1086,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
if (wifiHotspotManager.isWifiApEnabled()) { if (wifiHotspotManager.isWifiApEnabled()) {
startService(ACTION_TURN_OFF_BEFORE_O); startService(ACTION_TURN_OFF_BEFORE_O);
} else { } else {
//Check if user's hotspot is enabled
if (isMobileDataEnabled(this)) { if (isMobileDataEnabled(this)) {
mobileDataDialog(); mobileDataDialog();
@ -1154,6 +1154,22 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
} }
} }
//To get write permission settings, we use this method.
private boolean showWritePermissionSettings() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
if (!Settings.System.canWrite(this)) {
Log.v("DANG", " " + !Settings.System.canWrite(this));
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + this.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
return false;
}
}
return true; //Permission already given
}
@SuppressWarnings("SameReturnValue") @SuppressWarnings("SameReturnValue")
@OnLongClick(R.id.bottom_toolbar_bookmark) @OnLongClick(R.id.bottom_toolbar_bookmark)
boolean goToBookmarks() { boolean goToBookmarks() {
@ -1589,8 +1605,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
} }
} }
updateWidgets(this); updateWidgets(this);
wifiHotspotManager.showWritePermissionSettings();
} }
private void updateBottomToolbarVisibility() { private void updateBottomToolbarVisibility() {
@ -1802,7 +1816,6 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
} }
} }
return; return;
//Checking the result code for LocationSettings resolution //Checking the result code for LocationSettings resolution
case 101: case 101:
final LocationSettingsStates states = LocationSettingsStates.fromIntent(data); final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);

View File

@ -1,13 +1,10 @@
package org.kiwix.kiwixmobile.wifi_hotspot; package org.kiwix.kiwixmobile.wifi_hotspot;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.provider.Settings;
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;
@ -34,20 +31,6 @@ public class WifiHotspotManager {
wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE); wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
} }
//To get write permission settings, we use this method.
public void showWritePermissionSettings() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
if (!Settings.System.canWrite(this.context)) {
Log.v("DANG", " " + !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);
}
}
}
// This method enables/disables the wifi access point // This method enables/disables the wifi access point
// It is used for API<26 // It is used for API<26
public boolean setWifiEnabled(WifiConfiguration wifiConfig, boolean enabled) { public boolean setWifiEnabled(WifiConfiguration wifiConfig, boolean enabled) {