Add a periodic check of the tethering provisioning

Add a service that handles the check through broadcasts which are
defined through configs, similar to the previous configs for the
activity.

Depends on I1f6e2d954562c5a16a0de60dac625005ec3e5c50

Bug: 18453076
Change-Id: I515d72706e9ca37877e67c44427af1b75b146390
This commit is contained in:
Jason Monk
2014-12-10 17:21:51 -05:00
parent f84c6ee01c
commit 37832d661a
6 changed files with 366 additions and 91 deletions

View File

@@ -351,9 +351,15 @@
</intent-filter>
</activity>
<activity android:name=".EnableWifiTether"
android:theme="@style/Transparent"
android:excludeFromRecents="true" />
<service android:name=".TetherService"
android:exported="true"
android:permission="android.permission.CONNECTIVITY_INTERNAL" />
<receiver
android:name=".HotspotOffReceiver" >
<intent-filter>
<action android:name="android.net.wifi.WIFI_AP_STATE_CHANGED" />
</intent-filter>
</receiver>
<activity android:name="Settings$TetherSettingsActivity"
android:label="@string/tether_settings_title_all"

View File

@@ -1,78 +0,0 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.SystemProperties;
import android.util.Log;
import android.provider.Settings;
public class EnableWifiTether extends Activity {
private static final String TETHER_CHOICE = "TETHER_TYPE";
private String[] mProvisionApp;
private static final int PROVISION_REQUEST = 0;
private static final int WIFI_TETHERING = 0;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mProvisionApp = getResources().getStringArray(
com.android.internal.R.array.config_mobile_hotspot_provision_app);
startProvisioning();
}
private void startProvisioning() {
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
intent.putExtra(TETHER_CHOICE, WIFI_TETHERING);
startActivityForResult(intent, PROVISION_REQUEST);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == PROVISION_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
enableTethering();
}
finish();
}
}
private void enableTethering() {
final ContentResolver cr = getContentResolver();
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
int wifiState = wifiManager.getWifiState();
if ((wifiState == WifiManager.WIFI_STATE_ENABLING) ||
(wifiState == WifiManager.WIFI_STATE_ENABLED)) {
wifiManager.setWifiEnabled(false);
Settings.Global.putInt(cr, Settings.Global.WIFI_SAVED_STATE, 1);
}
wifiManager.setWifiApEnabled(null, true);
}
}

View File

@@ -0,0 +1,25 @@
package com.android.settings;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
/**
* This receiver catches when quick settings turns off the hotspot, so we can
* cancel the alarm in that case. All other cancels are handled in tethersettings.
*/
public class HotspotOffReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (WifiManager.WIFI_AP_STATE_CHANGED_ACTION.equals(intent.getAction())) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_DISABLED) {
// The hotspot has been turned off, we don't need to recheck tethering.
TetherService.cancelRecheckAlarmIfNecessary(context, TetherSettings.WIFI_TETHERING);
}
}
}
}

View File

@@ -0,0 +1,307 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothPan;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothProfile.ServiceListener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import com.android.settings.wifi.WifiApEnabler;
import java.util.ArrayList;
public class TetherService extends Service {
private static final String TAG = "TetherService";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
public static final String EXTRA_SET_ALARM = "extraSetAlarm";
public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
public static final String EXTRA_ENABLE_WIFI_TETHER = "extraEnableWifiTether";
private static final String EXTRA_RESULT = "EntitlementResult";
// Activity results to match the activity provision protocol.
// Default to something not ok.
private static final int RESULT_DEFAULT = Activity.RESULT_CANCELED;
private static final int RESULT_OK = Activity.RESULT_OK;
private static final String TETHER_CHOICE = "TETHER_TYPE";
private static final int MS_PER_HOUR = 60 * 60 * 1000;
private static final String PREFS = "tetherPrefs";
private static final String KEY_TETHERS = "currentTethers";
private int mCurrentTypeIndex;
private boolean mEnableWifiAfterCheck;
private boolean mInProvisionCheck;
private ArrayList<Integer> mCurrentTethers;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
if (DEBUG) Log.d(TAG, "Creating WifiProvisionService");
String provisionResponse = getResources().getString(
com.android.internal.R.string.config_mobile_hotspot_provision_response);
registerReceiver(mReceiver, new IntentFilter(provisionResponse),
android.Manifest.permission.CONNECTIVITY_INTERNAL, null);
SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE);
mCurrentTethers = stringToTethers(prefs.getString(KEY_TETHERS, ""));
mCurrentTypeIndex = 0;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.hasExtra(EXTRA_ADD_TETHER_TYPE)) {
int type = intent.getIntExtra(EXTRA_ADD_TETHER_TYPE, TetherSettings.INVALID);
if (!mCurrentTethers.contains(type)) {
if (DEBUG) Log.d(TAG, "Adding tether " + type);
mCurrentTethers.add(type);
}
}
if (intent.hasExtra(EXTRA_REM_TETHER_TYPE)) {
int type = intent.getIntExtra(EXTRA_REM_TETHER_TYPE, TetherSettings.INVALID);
if (DEBUG) Log.d(TAG, "Removing tether " + type);
int index = mCurrentTethers.indexOf(type);
if (index >= 0) {
mCurrentTethers.remove(index);
// If we are currently in the middle of a check, we may need to adjust the
// index accordingly.
if (index <= mCurrentTypeIndex && mCurrentTypeIndex > 0) {
mCurrentTypeIndex--;
}
}
cancelAlarmIfNecessary();
}
// Only set the alarm if we have one tether, meaning the one just added,
// to avoid setting it when it was already set previously for another
// type.
if (intent.getBooleanExtra(EXTRA_SET_ALARM, false)
&& mCurrentTethers.size() == 1) {
scheduleAlarm();
}
if (intent.getBooleanExtra(EXTRA_ENABLE_WIFI_TETHER, false)) {
mEnableWifiAfterCheck = true;
}
if (intent.getBooleanExtra(EXTRA_RUN_PROVISION, false)) {
startProvisioning(mCurrentTypeIndex);
} else if (!mInProvisionCheck) {
// If we aren't running any provisioning, no reason to stay alive.
stopSelf();
return START_NOT_STICKY;
}
// We want to be started if we are killed accidently, so that we can be sure we finish
// the check.
return START_STICKY;
}
@Override
public void onDestroy() {
if (mInProvisionCheck) {
Log.e(TAG, "TetherService getting destroyed while mid-provisioning"
+ mCurrentTethers.get(mCurrentTypeIndex));
}
SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE);
prefs.edit().putString(KEY_TETHERS, tethersToString(mCurrentTethers)).commit();
if (DEBUG) Log.d(TAG, "Destroying WifiProvisionService");
unregisterReceiver(mReceiver);
super.onDestroy();
}
private ArrayList<Integer> stringToTethers(String tethersStr) {
ArrayList<Integer> ret = new ArrayList<Integer>();
if (TextUtils.isEmpty(tethersStr)) return ret;
String[] tethersSplit = tethersStr.split(",");
for (int i = 0; i < tethersSplit.length; i++) {
ret.add(Integer.parseInt(tethersSplit[i]));
}
return ret;
}
private String tethersToString(ArrayList<Integer> tethers) {
final StringBuffer buffer = new StringBuffer();
final int N = tethers.size();
for (int i = 0; i < N; i++) {
if (i != 0) {
buffer.append(',');
}
buffer.append(tethers.get(i));
}
return buffer.toString();
}
private void enableWifiTetheringIfNeeded() {
if (!isHotspotEnabled(this)) {
new WifiApEnabler(this, null).setSoftapEnabled(true);
}
}
private void disableWifiTethering() {
WifiApEnabler enabler = new WifiApEnabler(this, null);
enabler.setSoftapEnabled(false);
}
private void disableUsbTethering() {
ConnectivityManager cm =
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
cm.setUsbTethering(false);
}
private void disableBtTethering() {
final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null) {
adapter.getProfileProxy(this, new ServiceListener() {
@Override
public void onServiceDisconnected(int profile) { }
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
((BluetoothPan) proxy).setBluetoothTethering(false);
adapter.closeProfileProxy(BluetoothProfile.PAN, proxy);
}
}, BluetoothProfile.PAN);
}
}
private void startProvisioning(int index) {
String provisionAction = getResources().getString(
com.android.internal.R.string.config_mobile_hotspot_provision_app_no_ui);
if (DEBUG) Log.d(TAG, "Sending provisioning broadcast: " + provisionAction + " type: "
+ mCurrentTethers.get(index));
Intent intent = new Intent(provisionAction);
intent.putExtra(TETHER_CHOICE, mCurrentTethers.get(index));
intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
sendBroadcast(intent);
mInProvisionCheck = true;
}
private static boolean isHotspotEnabled(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
return wifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED;
}
public static void scheduleRecheckAlarm(Context context, int type) {
Intent intent = new Intent(context, TetherService.class);
intent.putExtra(EXTRA_ADD_TETHER_TYPE, type);
intent.putExtra(EXTRA_SET_ALARM, true);
context.startService(intent);
}
private void scheduleAlarm() {
Intent intent = new Intent(this, TetherService.class);
intent.putExtra(EXTRA_RUN_PROVISION, true);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
int period = getResources().getInteger(
com.android.internal.R.integer.config_mobile_hotspot_provision_check_period);
long periodMs = period * MS_PER_HOUR;
long firstTime = SystemClock.elapsedRealtime() + periodMs;
if (DEBUG) Log.d(TAG, "Scheduling alarm at interval " + periodMs);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, periodMs,
pendingIntent);
}
/**
* Cancels the recheck alarm only if no tethering is currently active.
*
* Runs in the background, to get access to bluetooth service that takes time to bind.
*/
public static void cancelRecheckAlarmIfNecessary(final Context context, int type) {
Intent intent = new Intent(context, TetherService.class);
intent.putExtra(EXTRA_REM_TETHER_TYPE, type);
context.startService(intent);
}
private void cancelAlarmIfNecessary() {
if (mCurrentTethers.size() != 0) {
if (DEBUG) Log.d(TAG, "Tethering still active, not cancelling alarm");
return;
}
Intent intent = new Intent(this, TetherService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
if (DEBUG) Log.d(TAG, "Tethering no longer active, canceling recheck");
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (DEBUG) Log.d(TAG, "Got provision result " + intent);
String provisionResponse = context.getResources().getString(
com.android.internal.R.string.config_mobile_hotspot_provision_response);
if (provisionResponse.equals(intent.getAction())) {
mInProvisionCheck = false;
int checkType = mCurrentTethers.get(mCurrentTypeIndex);
if (intent.getIntExtra(EXTRA_RESULT, RESULT_DEFAULT) == RESULT_OK) {
if (checkType == TetherSettings.WIFI_TETHERING && mEnableWifiAfterCheck) {
enableWifiTetheringIfNeeded();
mEnableWifiAfterCheck = false;
}
} else {
switch (checkType) {
case TetherSettings.WIFI_TETHERING:
disableWifiTethering();
break;
case TetherSettings.BLUETOOTH_TETHERING:
disableBtTethering();
break;
case TetherSettings.USB_TETHERING:
disableUsbTethering();
break;
}
}
if (++mCurrentTypeIndex == mCurrentTethers.size()) {
// We are done with all checks, time to die.
stopSelf();
} else {
// Start the next check in our list.
startProvisioning(mCurrentTypeIndex);
}
}
}
};
}

View File

@@ -93,10 +93,10 @@ public class TetherSettings extends SettingsPreferenceFragment
private boolean mBluetoothEnableForTether;
private static final int INVALID = -1;
private static final int WIFI_TETHERING = 0;
private static final int USB_TETHERING = 1;
private static final int BLUETOOTH_TETHERING = 2;
public static final int INVALID = -1;
public static final int WIFI_TETHERING = 0;
public static final int USB_TETHERING = 1;
public static final int BLUETOOTH_TETHERING = 2;
/* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */
private int mTetherChoice = INVALID;
@@ -456,6 +456,9 @@ public class TetherSettings extends SettingsPreferenceFragment
if (enable) {
startProvisioningIfNecessary(WIFI_TETHERING);
} else {
if (isProvisioningNeeded(mProvisionApp)) {
TetherService.cancelRecheckAlarmIfNecessary(getActivity(), WIFI_TETHERING);
}
mWifiApEnabler.setSoftapEnabled(false);
}
return false;
@@ -505,6 +508,7 @@ public class TetherSettings extends SettingsPreferenceFragment
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == PROVISION_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
TetherService.scheduleRecheckAlarm(getActivity(), mTetherChoice);
startTethering();
} else {
//BT and USB need switch turned off on failure
@@ -572,6 +576,9 @@ public class TetherSettings extends SettingsPreferenceFragment
if (newState) {
startProvisioningIfNecessary(USB_TETHERING);
} else {
if (isProvisioningNeeded(mProvisionApp)) {
TetherService.cancelRecheckAlarmIfNecessary(getActivity(), USB_TETHERING);
}
setUsbTethering(newState);
}
} else if (preference == mBluetoothTether) {
@@ -580,6 +587,9 @@ public class TetherSettings extends SettingsPreferenceFragment
if (bluetoothTetherState) {
startProvisioningIfNecessary(BLUETOOTH_TETHERING);
} else {
if (isProvisioningNeeded(mProvisionApp)) {
TetherService.cancelRecheckAlarmIfNecessary(getActivity(), BLUETOOTH_TETHERING);
}
boolean errored = false;
String [] tethered = cm.getTetheredIfaces();

View File

@@ -68,15 +68,16 @@ public class WifiApEnabler {
} else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
enableWifiSwitch();
}
}
};
public WifiApEnabler(Context context, SwitchPreference switchPreference) {
mContext = context;
mSwitch = switchPreference;
mOriginalSummary = switchPreference.getSummary();
mOriginalSummary = switchPreference != null ? switchPreference.getSummary() : "";
if (switchPreference != null) {
switchPreference.setPersistent(false);
}
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mCm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
@@ -121,11 +122,15 @@ public class WifiApEnabler {
}
if (mWifiManager.setWifiApEnabled(null, enable)) {
if (mSwitch != null) {
/* Disable here, enabled on receiving success broadcast */
mSwitch.setEnabled(false);
}
} else {
if (mSwitch != null) {
mSwitch.setSummary(R.string.wifi_error);
}
}
/**
* If needed, restore Wifi on tether disable