1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 package com.android.settings; 18 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; 42 43 import com.android.internal.annotations.VisibleForTesting; 44 import com.android.settingslib.TetherUtil; 45 46 import java.util.ArrayList; 47 import java.util.List; 48 49 public class TetherService extends Service { 50 private static final String TAG = "TetherService"; 51 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 52 53 @VisibleForTesting 54 public static final String EXTRA_RESULT = "EntitlementResult"; 55 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; 60 61 private static final String TETHER_CHOICE = "TETHER_TYPE"; 62 private static final int MS_PER_HOUR = 60 * 60 * 1000; 63 64 private static final String PREFS = "tetherPrefs"; 65 private static final String KEY_TETHERS = "currentTethers"; 66 67 private int mCurrentTypeIndex; 68 private boolean mInProvisionCheck; 69 private UsageStatsManagerWrapper mUsageManagerWrapper; 70 private ArrayList<Integer> mCurrentTethers; 71 private ArrayMap<Integer, List<ResultReceiver>> mPendingCallbacks; 72 73 @Override onBind(Intent intent)74 public IBinder onBind(Intent intent) { 75 return null; 76 } 77 78 @Override onCreate()79 public void onCreate() { 80 super.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); 96 } 97 } 98 99 @Override onStartCommand(Intent intent, int flags, int startId)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); 110 } else { 111 // Invalid tether type. Just ignore this request and report failure. 112 callback.send(ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE, null); 113 stopSelf(); 114 return START_NOT_STICKY; 115 } 116 } 117 118 if (!mCurrentTethers.contains(type)) { 119 if (DEBUG) Log.d(TAG, "Adding tether " + type); 120 mCurrentTethers.add(type); 121 } 122 } 123 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); 130 if (index >= 0) { 131 removeTypeAtIndex(index); 132 } 133 cancelAlarmIfNecessary(); 134 } else { 135 if (DEBUG) Log.d(TAG, "Don't cancel alarm during provisioning"); 136 } 137 } 138 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 141 // type. 142 if (intent.getBooleanExtra(ConnectivityManager.EXTRA_SET_ALARM, false) 143 && mCurrentTethers.size() == 1) { 144 scheduleAlarm(); 145 } 146 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); 152 stopSelf(); 153 return START_NOT_STICKY; 154 } 155 // We want to be started if we are killed accidently, so that we can be sure we finish 156 // the check. 157 return START_REDELIVER_INTENT; 158 } 159 160 @Override onDestroy()161 public void onDestroy() { 162 if (mInProvisionCheck) { 163 Log.e(TAG, "TetherService getting destroyed while mid-provisioning" 164 + mCurrentTethers.get(mCurrentTypeIndex)); 165 } 166 SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE); 167 prefs.edit().putString(KEY_TETHERS, tethersToString(mCurrentTethers)).commit(); 168 169 if (DEBUG) Log.d(TAG, "Destroying TetherService"); 170 unregisterReceiver(mReceiver); 171 super.onDestroy(); 172 } 173 removeTypeAtIndex(int index)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) { 180 mCurrentTypeIndex--; 181 } 182 } 183 stringToTethers(String tethersStr)184 private ArrayList<Integer> stringToTethers(String tethersStr) { 185 ArrayList<Integer> ret = new ArrayList<Integer>(); 186 if (TextUtils.isEmpty(tethersStr)) return ret; 187 188 String[] tethersSplit = tethersStr.split(","); 189 for (int i = 0; i < tethersSplit.length; i++) { 190 ret.add(Integer.parseInt(tethersSplit[i])); 191 } 192 return ret; 193 } 194 tethersToString(ArrayList<Integer> tethers)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++) { 199 if (i != 0) { 200 buffer.append(','); 201 } 202 buffer.append(tethers.get(i)); 203 } 204 205 return buffer.toString(); 206 } 207 disableWifiTethering()208 private void disableWifiTethering() { 209 ConnectivityManager cm = 210 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 211 cm.stopTethering(ConnectivityManager.TETHERING_WIFI); 212 } 213 disableUsbTethering()214 private void disableUsbTethering() { 215 ConnectivityManager cm = 216 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 217 cm.setUsbTethering(false); 218 } 219 disableBtTethering()220 private void disableBtTethering() { 221 final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 222 if (adapter != null) { 223 adapter.getProfileProxy(this, new ServiceListener() { 224 @Override 225 public void onServiceDisconnected(int profile) { } 226 227 @Override 228 public void onServiceConnected(int profile, BluetoothProfile proxy) { 229 ((BluetoothPan) proxy).setBluetoothTethering(false); 230 adapter.closeProfileProxy(BluetoothProfile.PAN, proxy); 231 } 232 }, BluetoothProfile.PAN); 233 } 234 } 235 startProvisioning(int index)236 private void startProvisioning(int index) { 237 if (index < mCurrentTethers.size()) { 238 Intent intent = getProvisionBroadcastIntent(index); 239 setEntitlementAppActive(index); 240 241 if (DEBUG) Log.d(TAG, "Sending provisioning broadcast: " + intent.getAction() 242 + " type: " + mCurrentTethers.get(index)); 243 244 sendBroadcast(intent); 245 mInProvisionCheck = true; 246 } 247 } 248 getProvisionBroadcastIntent(int index)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); 256 257 return intent; 258 } 259 setEntitlementAppActive(int index)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."); 267 return; 268 } 269 270 for (ResolveInfo resolver : resolvers) { 271 if (resolver.activityInfo.applicationInfo.isSystemApp()) { 272 String packageName = resolver.activityInfo.packageName; 273 mUsageManagerWrapper.setAppInactive(packageName, false); 274 } 275 } 276 } 277 scheduleAlarm()278 private void scheduleAlarm() { 279 Intent intent = new Intent(this, TetherService.class); 280 intent.putExtra(ConnectivityManager.EXTRA_RUN_PROVISION, true); 281 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, 290 pendingIntent); 291 } 292 293 /** 294 * Cancels the recheck alarm only if no tethering is currently active. 295 * 296 * Runs in the background, to get access to bluetooth service that takes time to bind. 297 */ cancelRecheckAlarmIfNecessary(final Context context, int type)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); 302 } 303 cancelAlarmIfNecessary()304 private void cancelAlarmIfNecessary() { 305 if (mCurrentTethers.size() != 0) { 306 if (DEBUG) Log.d(TAG, "Tethering still active, not cancelling alarm"); 307 return; 308 } 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"); 314 } 315 fireCallbacksForType(int type, int result)316 private void fireCallbacksForType(int type, int result) { 317 List<ResultReceiver> callbacksForType = mPendingCallbacks.get(type); 318 if (callbacksForType == null) { 319 return; 320 } 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); 326 } 327 callbacksForType.clear(); 328 } 329 330 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 331 @Override 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); 336 337 if (provisionResponse.equals(intent.getAction())) { 338 if (!mInProvisionCheck) { 339 Log.e(TAG, "Unexpected provision response " + intent); 340 return; 341 } 342 int checkType = mCurrentTethers.get(mCurrentTypeIndex); 343 mInProvisionCheck = false; 344 int result = intent.getIntExtra(EXTRA_RESULT, RESULT_DEFAULT); 345 if (result != RESULT_OK) { 346 switch (checkType) { 347 case ConnectivityManager.TETHERING_WIFI: 348 disableWifiTethering(); 349 break; 350 case ConnectivityManager.TETHERING_BLUETOOTH: 351 disableBtTethering(); 352 break; 353 case ConnectivityManager.TETHERING_USB: 354 disableUsbTethering(); 355 break; 356 } 357 } 358 fireCallbacksForType(checkType, result); 359 360 if (++mCurrentTypeIndex >= mCurrentTethers.size()) { 361 // We are done with all checks, time to die. 362 stopSelf(); 363 } else { 364 // Start the next check in our list. 365 startProvisioning(mCurrentTypeIndex); 366 } 367 } 368 } 369 }; 370 371 @VisibleForTesting setUsageStatsManagerWrapper(UsageStatsManagerWrapper wrapper)372 void setUsageStatsManagerWrapper(UsageStatsManagerWrapper wrapper) { 373 mUsageManagerWrapper = wrapper; 374 } 375 376 /** 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. 379 */ 380 @VisibleForTesting 381 public static class UsageStatsManagerWrapper { 382 private final UsageStatsManager mUsageStatsManager; 383 UsageStatsManagerWrapper(Context context)384 UsageStatsManagerWrapper(Context context) { 385 mUsageStatsManager = (UsageStatsManager) 386 context.getSystemService(Context.USAGE_STATS_SERVICE); 387 } 388 setAppInactive(String packageName, boolean isInactive)389 void setAppInactive(String packageName, boolean isInactive) { 390 mUsageStatsManager.setAppInactive(packageName, isInactive); 391 } 392 } 393 } 394