prompt user for WRITE_SETTINGS permission when setting up Hotspot
closes #656
This commit is contained in:
parent
f90b030e76
commit
570b532bd6
@ -1,5 +1,6 @@
|
|||||||
package org.fdroid.fdroid.views.swap;
|
package org.fdroid.fdroid.views.swap;
|
||||||
|
|
||||||
|
import android.annotation.TargetApi;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
@ -12,8 +13,10 @@ import android.content.ServiceConnection;
|
|||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
import android.provider.Settings;
|
||||||
import android.support.annotation.ColorRes;
|
import android.support.annotation.ColorRes;
|
||||||
import android.support.annotation.LayoutRes;
|
import android.support.annotation.LayoutRes;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
@ -110,6 +113,7 @@ public class SwapWorkflowActivity extends AppCompatActivity {
|
|||||||
private static final int REQUEST_BLUETOOTH_ENABLE_FOR_SWAP = 2;
|
private static final int REQUEST_BLUETOOTH_ENABLE_FOR_SWAP = 2;
|
||||||
private static final int REQUEST_BLUETOOTH_DISCOVERABLE = 3;
|
private static final int REQUEST_BLUETOOTH_DISCOVERABLE = 3;
|
||||||
private static final int REQUEST_BLUETOOTH_ENABLE_FOR_SEND = 4;
|
private static final int REQUEST_BLUETOOTH_ENABLE_FOR_SEND = 4;
|
||||||
|
private static final int REQUEST_WRITE_SETTINGS_PERMISSION = 5;
|
||||||
|
|
||||||
private Toolbar toolbar;
|
private Toolbar toolbar;
|
||||||
private InnerView currentView;
|
private InnerView currentView;
|
||||||
@ -250,32 +254,37 @@ public class SwapWorkflowActivity extends AppCompatActivity {
|
|||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
).setPositiveButton(R.string.wifi, new DialogInterface.OnClickListener() {
|
.setPositiveButton(R.string.wifi, new DialogInterface.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
|
startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
).setNegativeButton(R.string.wifi_ap, new DialogInterface.OnClickListener() {
|
.setNegativeButton(R.string.wifi_ap, new DialogInterface.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
promptToSetupWifiAP();
|
if (Build.VERSION.SDK_INT >= 26) {
|
||||||
|
showTetheringSettings();
|
||||||
|
} else if (Build.VERSION.SDK_INT >= 23 && !Settings.System.canWrite(getBaseContext())) {
|
||||||
|
requestWriteSettingsPermission();
|
||||||
|
} else {
|
||||||
|
setupWifiAP();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
).create().show();
|
})
|
||||||
|
.create().show();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void promptToSetupWifiAP() {
|
private void setupWifiAP() {
|
||||||
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
||||||
WifiApControl ap = WifiApControl.getInstance(this);
|
WifiApControl ap = WifiApControl.getInstance(this);
|
||||||
wifiManager.setWifiEnabled(false);
|
wifiManager.setWifiEnabled(false);
|
||||||
if (!ap.enable()) {
|
if (ap.enable()) {
|
||||||
Log.e(TAG, "Could not enable WiFi AP.");
|
Toast.makeText(this, R.string.swap_toast_hotspot_enabled, Toast.LENGTH_SHORT).show();
|
||||||
// TODO: Feedback to user?
|
|
||||||
} else {
|
} else {
|
||||||
Utils.debugLog(TAG, "WiFi AP enabled.");
|
Toast.makeText(this, R.string.swap_toast_could_not_enable_hotspot, Toast.LENGTH_LONG).show();
|
||||||
// TODO: Seems to be broken some times...
|
Log.e(TAG, "Could not enable WiFi AP.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -419,6 +428,29 @@ public class SwapWorkflowActivity extends AppCompatActivity {
|
|||||||
inflateInnerView(R.layout.swap_select_apps);
|
inflateInnerView(R.layout.swap_select_apps);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On {@code android-26}, only apps with privileges can access
|
||||||
|
* {@code WRITE_SETTINGS}. So this just shows the tethering settings
|
||||||
|
* for the user to do it themselves.
|
||||||
|
*/
|
||||||
|
public void showTetheringSettings() {
|
||||||
|
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
|
||||||
|
intent.addCategory(Intent.CATEGORY_LAUNCHER);
|
||||||
|
final ComponentName cn = new ComponentName("com.android.settings",
|
||||||
|
"com.android.settings.TetherSettings");
|
||||||
|
intent.setComponent(cn);
|
||||||
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
startActivity(intent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TargetApi(23)
|
||||||
|
public void requestWriteSettingsPermission() {
|
||||||
|
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
|
||||||
|
Uri.parse("package:" + getPackageName()));
|
||||||
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
startActivityForResult(intent, REQUEST_WRITE_SETTINGS_PERMISSION);
|
||||||
|
}
|
||||||
|
|
||||||
public void sendFDroid() {
|
public void sendFDroid() {
|
||||||
// If Bluetooth has not been enabled/turned on, then enabling device discoverability
|
// If Bluetooth has not been enabled/turned on, then enabling device discoverability
|
||||||
// will automatically enable Bluetooth.
|
// will automatically enable Bluetooth.
|
||||||
@ -568,6 +600,10 @@ public class SwapWorkflowActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
} else if (requestCode == CONNECT_TO_SWAP && resultCode == Activity.RESULT_OK) {
|
} else if (requestCode == CONNECT_TO_SWAP && resultCode == Activity.RESULT_OK) {
|
||||||
finish();
|
finish();
|
||||||
|
} else if (requestCode == REQUEST_WRITE_SETTINGS_PERMISSION) {
|
||||||
|
if (Build.VERSION.SDK_INT >= 23 && Settings.System.canWrite(this)) {
|
||||||
|
setupWifiAP();
|
||||||
|
}
|
||||||
} else if (requestCode == REQUEST_BLUETOOTH_ENABLE_FOR_SWAP) {
|
} else if (requestCode == REQUEST_BLUETOOTH_ENABLE_FOR_SWAP) {
|
||||||
|
|
||||||
if (resultCode == RESULT_OK) {
|
if (resultCode == RESULT_OK) {
|
||||||
@ -595,12 +631,13 @@ public class SwapWorkflowActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The process for setting up bluetooth is as follows:
|
* The process for setting up bluetooth is as follows:
|
||||||
* * Assume we have bluetooth available (otherwise the button which allowed us to start
|
* <ul>
|
||||||
* the bluetooth process should not have been available).
|
* <li>Assume we have bluetooth available (otherwise the button which allowed us to start
|
||||||
* * Ask user to enable (if not enabled yet).
|
* the bluetooth process should not have been available)</li>
|
||||||
* * Start bluetooth server socket.
|
* <li>Ask user to enable (if not enabled yet)</li>
|
||||||
* * Enable bluetooth discoverability, so that people can connect to our server socket.
|
* <li>Start bluetooth server socket</li>
|
||||||
*
|
* <li>Enable bluetooth discoverability, so that people can connect to our server socket.</li>
|
||||||
|
* </ul>
|
||||||
* Note that this is a little different than the usual process for bluetooth _clients_, which
|
* Note that this is a little different than the usual process for bluetooth _clients_, which
|
||||||
* involves pairing and connecting with other devices.
|
* involves pairing and connecting with other devices.
|
||||||
*/
|
*/
|
||||||
|
@ -461,6 +461,8 @@ This often occurs with apps installed via Google Play or other sources, if they
|
|||||||
<string name="swap_not_enabled">Swapping not enabled</string>
|
<string name="swap_not_enabled">Swapping not enabled</string>
|
||||||
<string name="swap_not_enabled_description">Before swapping, your device must be made visible.</string>
|
<string name="swap_not_enabled_description">Before swapping, your device must be made visible.</string>
|
||||||
<string name="swap_toast_invalid_url">Invalid URL for swapping: %1$s</string>
|
<string name="swap_toast_invalid_url">Invalid URL for swapping: %1$s</string>
|
||||||
|
<string name="swap_toast_hotspot_enabled">Wi-Fi Hotspot enabled</string>
|
||||||
|
<string name="swap_toast_could_not_enable_hotspot">Could not enable Wi-Fi Hotspot!</string>
|
||||||
|
|
||||||
<string name="install_confirm">needs access to</string>
|
<string name="install_confirm">needs access to</string>
|
||||||
<string name="install_confirm_update">Do you want to install an update
|
<string name="install_confirm_update">Do you want to install an update
|
||||||
|
Loading…
x
Reference in New Issue
Block a user