OSDN Git Service

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