1 /*
2  * Copyright 2018, 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.managedprovisioning.task;
18 
19 import android.content.Context;
20 import android.os.Handler;
21 import android.provider.Settings;
22 
23 import com.android.managedprovisioning.common.ProvisionLogger;
24 import com.android.managedprovisioning.common.Utils;
25 import com.android.managedprovisioning.model.ProvisioningParams;
26 import com.android.managedprovisioning.task.wifi.NetworkMonitor;
27 
28 /**
29  * A task that enables mobile data and waits for it to successfully connect. If connection times out
30  * {@link #error(int)} will be called.
31  */
32 public class ConnectMobileNetworkTask extends AbstractProvisioningTask
33         implements NetworkMonitor.NetworkConnectedCallback {
34     private static final int RECONNECT_TIMEOUT_MS = 600000;
35 
36     private final NetworkMonitor mNetworkMonitor;
37 
38     private Handler mHandler;
39     private boolean mTaskDone = false;
40 
41     private final Utils mUtils;
42     private Runnable mTimeoutRunnable;
43 
ConnectMobileNetworkTask( Context context, ProvisioningParams provisioningParams, Callback callback)44     public ConnectMobileNetworkTask(
45             Context context,
46             ProvisioningParams provisioningParams,
47             Callback callback) {
48         super(context, provisioningParams, callback);
49         mNetworkMonitor = new NetworkMonitor(context, /* waitForValidated */ true);
50         mUtils = new Utils();
51     }
52 
53     /**
54      * Sets {@link Settings.Global#DEVICE_PROVISIONING_MOBILE_DATA_ENABLED} to 1, and if not already
55      * connected to the network, starts listening for a connection. Calls {@link #success()} when
56      * connected or {@link #error(int)} if it times out after 10 minutes.
57      */
58     @Override
run(int userId)59     public void run(int userId) {
60         Settings.Global.putInt(mContext.getContentResolver(),
61                 Settings.Global.DEVICE_PROVISIONING_MOBILE_DATA_ENABLED, 1);
62 
63         if (isLegacyConnected()) {
64             success();
65             return;
66         }
67 
68         if ((mProvisioningParams.isNfc
69                 || mProvisioningParams.isQrProvisioning)
70                 && mUtils.isMobileNetworkConnectedToInternet(mContext)) {
71             success();
72             return;
73         }
74 
75         mTaskDone = false;
76         mHandler = new Handler();
77         mNetworkMonitor.startListening(this);
78 
79         // NetworkMonitor will call onNetworkConnected.
80         // Post time out event in case the NetworkMonitor doesn't call back.
81         mTimeoutRunnable = () -> finishTask(false);
82         mHandler.postDelayed(mTimeoutRunnable, RECONNECT_TIMEOUT_MS);
83     }
84 
85     @Override
onNetworkConnected()86     public void onNetworkConnected() {
87         ProvisionLogger.logd("onNetworkConnected");
88         if (isLegacyConnected()
89                 || ((mProvisioningParams.isNfc || mProvisioningParams.isQrProvisioning)
90                 // TODO: instead of having NetworkMonitor wait for any default network (which could
91                 // be wifi), and checking that any mobile network (which may not be the default) is
92                 // connected, either:
93                 // - If the device should wait for mobile data to be default before continuing,
94                 //   have NetworkMonitor check that the default network it obtains has
95                 //   TRANSPORT_MOBILE. This will typically never happen when wifi is usable.
96                 // - If the device should continue when mobile data is connected but not default,
97                 //   have NetworkMonitor use requestNetwork with TRANSPORT_MOBILE and have
98                 //   all subsequent network usage explicitly specify the obtained mobile network
99                 //   with APIs such as Network.openConnection or Network.bindSocket.
100                 && mUtils.isMobileNetworkConnectedToInternet(mContext))) {
101             ProvisionLogger.logd("Connected to mobile data");
102             finishTask(true);
103             // Remove time out callback.
104             mHandler.removeCallbacks(mTimeoutRunnable);
105         }
106     }
107 
isLegacyConnected()108     private boolean isLegacyConnected() {
109         return !mProvisioningParams.isNfc
110                 && !mProvisioningParams.isQrProvisioning
111                 && mUtils.isConnectedToNetwork(mContext);
112     }
113 
finishTask(boolean isSuccess)114     private synchronized void finishTask(boolean isSuccess) {
115         if (mTaskDone) {
116             return;
117         }
118 
119         mTaskDone = true;
120         mNetworkMonitor.stopListening();
121         if (isSuccess) {
122             success();
123         } else {
124             error(0);
125         }
126     }
127 }
128