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