1 /* 2 * Copyright (C) 2016 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.network; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.content.pm.PackageManager; 22 import android.content.res.Resources; 23 import android.net.ConnectivityManager; 24 import android.os.Bundle; 25 import android.os.ResultReceiver; 26 import android.os.UserHandle; 27 import android.telephony.SubscriptionManager; 28 import android.util.Log; 29 30 import com.android.settings.Utils; 31 32 /** 33 * Activity which acts as a proxy to the tether provisioning app for sanity checks and permission 34 * restrictions. Specifically, the provisioning apps require 35 * {@link android.permission.CONNECTIVITY_INTERNAL}, while this activity can be started by a caller 36 * with {@link android.permission.TETHER_PRIVILEGED}. 37 */ 38 public class TetherProvisioningActivity extends Activity { 39 private static final int PROVISION_REQUEST = 0; 40 private static final String TAG = "TetherProvisioningAct"; 41 private static final String EXTRA_TETHER_TYPE = "TETHER_TYPE"; 42 private static final String EXTRA_SUBID = "subId"; 43 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 44 private ResultReceiver mResultReceiver; 45 46 @Override onCreate(Bundle savedInstanceState)47 public void onCreate(Bundle savedInstanceState) { 48 super.onCreate(savedInstanceState); 49 50 mResultReceiver = (ResultReceiver)getIntent().getParcelableExtra( 51 ConnectivityManager.EXTRA_PROVISION_CALLBACK); 52 53 final int tetherType = getIntent().getIntExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, 54 ConnectivityManager.TETHERING_INVALID); 55 56 final int tetherSubId = getIntent().getIntExtra(EXTRA_SUBID, 57 SubscriptionManager.INVALID_SUBSCRIPTION_ID); 58 final int subId = SubscriptionManager.getDefaultDataSubscriptionId(); 59 if (tetherSubId != subId) { 60 Log.e(TAG, "This Provisioning request is outdated, current subId: " + subId); 61 return; 62 } 63 final Resources res = Utils.getResourcesForSubId(this, subId); 64 final String[] provisionApp = res.getStringArray( 65 com.android.internal.R.array.config_mobile_hotspot_provision_app); 66 67 final Intent intent = new Intent(Intent.ACTION_MAIN); 68 intent.setClassName(provisionApp[0], provisionApp[1]); 69 intent.putExtra(EXTRA_TETHER_TYPE, tetherType); 70 if (DEBUG) { 71 Log.d(TAG, "Starting provisioning app: " + provisionApp[0] + "." + provisionApp[1]); 72 } 73 74 if (getPackageManager().queryIntentActivities(intent, 75 PackageManager.MATCH_DEFAULT_ONLY).isEmpty()) { 76 Log.e(TAG, "Provisioning app is configured, but not available."); 77 mResultReceiver.send(ConnectivityManager.TETHER_ERROR_PROVISION_FAILED, null); 78 finish(); 79 return; 80 } 81 82 startActivityForResultAsUser(intent, PROVISION_REQUEST, UserHandle.CURRENT); 83 } 84 85 @Override onActivityResult(int requestCode, int resultCode, Intent intent)86 public void onActivityResult(int requestCode, int resultCode, Intent intent) { 87 super.onActivityResult(requestCode, resultCode, intent); 88 if (requestCode == PROVISION_REQUEST) { 89 if (DEBUG) Log.d(TAG, "Got result from app: " + resultCode); 90 int result = resultCode == Activity.RESULT_OK ? 91 ConnectivityManager.TETHER_ERROR_NO_ERROR : 92 ConnectivityManager.TETHER_ERROR_PROVISION_FAILED; 93 mResultReceiver.send(result, null); 94 finish(); 95 } 96 } 97 } 98