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