2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.settings;
19 import android.app.Activity;
20 import android.app.AlarmManager;
21 import android.app.PendingIntent;
22 import android.app.Service;
23 import android.app.usage.UsageStatsManager;
24 import android.bluetooth.BluetoothAdapter;
25 import android.bluetooth.BluetoothPan;
26 import android.bluetooth.BluetoothProfile;
27 import android.bluetooth.BluetoothProfile.ServiceListener;
28 import android.content.BroadcastReceiver;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.IntentFilter;
32 import android.content.SharedPreferences;
33 import android.content.pm.PackageManager;
34 import android.content.pm.ResolveInfo;
35 import android.net.ConnectivityManager;
36 import android.os.IBinder;
37 import android.os.ResultReceiver;
38 import android.os.SystemClock;
39 import android.text.TextUtils;
40 import android.util.ArrayMap;
41 import android.util.Log;
43 import com.android.internal.annotations.VisibleForTesting;
44 import com.android.settingslib.TetherUtil;
46 import java.util.ArrayList;
47 import java.util.List;
49 public class TetherService extends Service {
50 private static final String TAG = "TetherService";
51 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
54 public static final String EXTRA_RESULT = "EntitlementResult";
56 // Activity results to match the activity provision protocol.
57 // Default to something not ok.
58 private static final int RESULT_DEFAULT = Activity.RESULT_CANCELED;
59 private static final int RESULT_OK = Activity.RESULT_OK;
61 private static final String TETHER_CHOICE = "TETHER_TYPE";
62 private static final int MS_PER_HOUR = 60 * 60 * 1000;
64 private static final String PREFS = "tetherPrefs";
65 private static final String KEY_TETHERS = "currentTethers";
67 private int mCurrentTypeIndex;
68 private boolean mInProvisionCheck;
69 private UsageStatsManagerWrapper mUsageManagerWrapper;
70 private ArrayList<Integer> mCurrentTethers;
71 private ArrayMap<Integer, List<ResultReceiver>> mPendingCallbacks;
74 public IBinder onBind(Intent intent) {
79 public void onCreate() {
81 if (DEBUG) Log.d(TAG, "Creating TetherService");
82 String provisionResponse = getResources().getString(
83 com.android.internal.R.string.config_mobile_hotspot_provision_response);
84 registerReceiver(mReceiver, new IntentFilter(provisionResponse),
85 android.Manifest.permission.CONNECTIVITY_INTERNAL, null);
86 SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE);
87 mCurrentTethers = stringToTethers(prefs.getString(KEY_TETHERS, ""));
88 mCurrentTypeIndex = 0;
89 mPendingCallbacks = new ArrayMap<>(3);
90 mPendingCallbacks.put(ConnectivityManager.TETHERING_WIFI, new ArrayList<ResultReceiver>());
91 mPendingCallbacks.put(ConnectivityManager.TETHERING_USB, new ArrayList<ResultReceiver>());
92 mPendingCallbacks.put(
93 ConnectivityManager.TETHERING_BLUETOOTH, new ArrayList<ResultReceiver>());
94 if (mUsageManagerWrapper == null) {
95 mUsageManagerWrapper = new UsageStatsManagerWrapper(this);
100 public int onStartCommand(Intent intent, int flags, int startId) {
101 if (intent.hasExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE)) {
102 int type = intent.getIntExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE,
103 ConnectivityManager.TETHERING_INVALID);
104 ResultReceiver callback =
105 intent.getParcelableExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK);
106 if (callback != null) {
107 List<ResultReceiver> callbacksForType = mPendingCallbacks.get(type);
108 if (callbacksForType != null) {
109 callbacksForType.add(callback);
111 // Invalid tether type. Just ignore this request and report failure.
112 callback.send(ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE, null);
114 return START_NOT_STICKY;
118 if (!mCurrentTethers.contains(type)) {
119 if (DEBUG) Log.d(TAG, "Adding tether " + type);
120 mCurrentTethers.add(type);
124 if (intent.hasExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE)) {
125 if (!mInProvisionCheck) {
126 int type = intent.getIntExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE,
127 ConnectivityManager.TETHERING_INVALID);
128 int index = mCurrentTethers.indexOf(type);
129 if (DEBUG) Log.d(TAG, "Removing tether " + type + ", index " + index);
131 removeTypeAtIndex(index);
133 cancelAlarmIfNecessary();
135 if (DEBUG) Log.d(TAG, "Don't cancel alarm during provisioning");
139 // Only set the alarm if we have one tether, meaning the one just added,
140 // to avoid setting it when it was already set previously for another
142 if (intent.getBooleanExtra(ConnectivityManager.EXTRA_SET_ALARM, false)
143 && mCurrentTethers.size() == 1) {
147 if (intent.getBooleanExtra(ConnectivityManager.EXTRA_RUN_PROVISION, false)) {
148 startProvisioning(mCurrentTypeIndex);
149 } else if (!mInProvisionCheck) {
150 // If we aren't running any provisioning, no reason to stay alive.
151 if (DEBUG) Log.d(TAG, "Stopping self. startid: " + startId);
153 return START_NOT_STICKY;
155 // We want to be started if we are killed accidently, so that we can be sure we finish
157 return START_REDELIVER_INTENT;
161 public void onDestroy() {
162 if (mInProvisionCheck) {
163 Log.e(TAG, "TetherService getting destroyed while mid-provisioning"
164 + mCurrentTethers.get(mCurrentTypeIndex));
166 SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE);
167 prefs.edit().putString(KEY_TETHERS, tethersToString(mCurrentTethers)).commit();
169 if (DEBUG) Log.d(TAG, "Destroying TetherService");
170 unregisterReceiver(mReceiver);
174 private void removeTypeAtIndex(int index) {
175 mCurrentTethers.remove(index);
176 // If we are currently in the middle of a check, we may need to adjust the
177 // index accordingly.
178 if (DEBUG) Log.d(TAG, "mCurrentTypeIndex: " + mCurrentTypeIndex);
179 if (index <= mCurrentTypeIndex && mCurrentTypeIndex > 0) {
184 private ArrayList<Integer> stringToTethers(String tethersStr) {
185 ArrayList<Integer> ret = new ArrayList<Integer>();
186 if (TextUtils.isEmpty(tethersStr)) return ret;
188 String[] tethersSplit = tethersStr.split(",");
189 for (int i = 0; i < tethersSplit.length; i++) {
190 ret.add(Integer.parseInt(tethersSplit[i]));
195 private String tethersToString(ArrayList<Integer> tethers) {
196 final StringBuffer buffer = new StringBuffer();
197 final int N = tethers.size();
198 for (int i = 0; i < N; i++) {
202 buffer.append(tethers.get(i));
205 return buffer.toString();
208 private void disableWifiTethering() {
209 ConnectivityManager cm =
210 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
211 cm.stopTethering(ConnectivityManager.TETHERING_WIFI);
214 private void disableUsbTethering() {
215 ConnectivityManager cm =
216 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
217 cm.setUsbTethering(false);
220 private void disableBtTethering() {
221 final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
222 if (adapter != null) {
223 adapter.getProfileProxy(this, new ServiceListener() {
225 public void onServiceDisconnected(int profile) { }
228 public void onServiceConnected(int profile, BluetoothProfile proxy) {
229 ((BluetoothPan) proxy).setBluetoothTethering(false);
230 adapter.closeProfileProxy(BluetoothProfile.PAN, proxy);
232 }, BluetoothProfile.PAN);
236 private void startProvisioning(int index) {
237 if (index < mCurrentTethers.size()) {
238 Intent intent = getProvisionBroadcastIntent(index);
239 setEntitlementAppActive(index);
241 if (DEBUG) Log.d(TAG, "Sending provisioning broadcast: " + intent.getAction()
242 + " type: " + mCurrentTethers.get(index));
244 sendBroadcast(intent);
245 mInProvisionCheck = true;
249 private Intent getProvisionBroadcastIntent(int index) {
250 String provisionAction = getResources().getString(
251 com.android.internal.R.string.config_mobile_hotspot_provision_app_no_ui);
252 Intent intent = new Intent(provisionAction);
253 int type = mCurrentTethers.get(index);
254 intent.putExtra(TETHER_CHOICE, type);
255 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
260 private void setEntitlementAppActive(int index) {
261 final PackageManager packageManager = getPackageManager();
262 Intent intent = getProvisionBroadcastIntent(index);
263 List<ResolveInfo> resolvers =
264 packageManager.queryBroadcastReceivers(intent, PackageManager.MATCH_ALL);
265 if (resolvers.isEmpty()) {
266 Log.e(TAG, "No found BroadcastReceivers for provision intent.");
270 for (ResolveInfo resolver : resolvers) {
271 if (resolver.activityInfo.applicationInfo.isSystemApp()) {
272 String packageName = resolver.activityInfo.packageName;
273 mUsageManagerWrapper.setAppInactive(packageName, false);
278 private void scheduleAlarm() {
279 Intent intent = new Intent(this, TetherService.class);
280 intent.putExtra(ConnectivityManager.EXTRA_RUN_PROVISION, true);
282 PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
283 AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
284 int period = getResources().getInteger(
285 com.android.internal.R.integer.config_mobile_hotspot_provision_check_period);
286 long periodMs = period * MS_PER_HOUR;
287 long firstTime = SystemClock.elapsedRealtime() + periodMs;
288 if (DEBUG) Log.d(TAG, "Scheduling alarm at interval " + periodMs);
289 alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, periodMs,
294 * Cancels the recheck alarm only if no tethering is currently active.
296 * Runs in the background, to get access to bluetooth service that takes time to bind.
298 public static void cancelRecheckAlarmIfNecessary(final Context context, int type) {
299 Intent intent = new Intent(context, TetherService.class);
300 intent.putExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE, type);
301 context.startService(intent);
304 private void cancelAlarmIfNecessary() {
305 if (mCurrentTethers.size() != 0) {
306 if (DEBUG) Log.d(TAG, "Tethering still active, not cancelling alarm");
309 Intent intent = new Intent(this, TetherService.class);
310 PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
311 AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
312 alarmManager.cancel(pendingIntent);
313 if (DEBUG) Log.d(TAG, "Tethering no longer active, canceling recheck");
316 private void fireCallbacksForType(int type, int result) {
317 List<ResultReceiver> callbacksForType = mPendingCallbacks.get(type);
318 if (callbacksForType == null) {
321 int errorCode = result == RESULT_OK ? ConnectivityManager.TETHER_ERROR_NO_ERROR :
322 ConnectivityManager.TETHER_ERROR_PROVISION_FAILED;
323 for (ResultReceiver callback : callbacksForType) {
324 if (DEBUG) Log.d(TAG, "Firing result: " + errorCode + " to callback");
325 callback.send(errorCode, null);
327 callbacksForType.clear();
330 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
332 public void onReceive(Context context, Intent intent) {
333 if (DEBUG) Log.d(TAG, "Got provision result " + intent);
334 String provisionResponse = getResources().getString(
335 com.android.internal.R.string.config_mobile_hotspot_provision_response);
337 if (provisionResponse.equals(intent.getAction())) {
338 if (!mInProvisionCheck) {
339 Log.e(TAG, "Unexpected provision response " + intent);
342 int checkType = mCurrentTethers.get(mCurrentTypeIndex);
343 mInProvisionCheck = false;
344 int result = intent.getIntExtra(EXTRA_RESULT, RESULT_DEFAULT);
345 if (result != RESULT_OK) {
347 case ConnectivityManager.TETHERING_WIFI:
348 disableWifiTethering();
350 case ConnectivityManager.TETHERING_BLUETOOTH:
351 disableBtTethering();
353 case ConnectivityManager.TETHERING_USB:
354 disableUsbTethering();
358 fireCallbacksForType(checkType, result);
360 if (++mCurrentTypeIndex >= mCurrentTethers.size()) {
361 // We are done with all checks, time to die.
364 // Start the next check in our list.
365 startProvisioning(mCurrentTypeIndex);
372 void setUsageStatsManagerWrapper(UsageStatsManagerWrapper wrapper) {
373 mUsageManagerWrapper = wrapper;
377 * A static helper class used for tests. UsageStatsManager cannot be mocked out becasue
378 * it's marked final. This class can be mocked out instead.
381 public static class UsageStatsManagerWrapper {
382 private final UsageStatsManager mUsageStatsManager;
384 UsageStatsManagerWrapper(Context context) {
385 mUsageStatsManager = (UsageStatsManager)
386 context.getSystemService(Context.USAGE_STATS_SERVICE);
389 void setAppInactive(String packageName, boolean isInactive) {
390 mUsageStatsManager.setAppInactive(packageName, isInactive);