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.R; 24 import com.android.managedprovisioning.common.ProvisionLogger; 25 import com.android.managedprovisioning.common.Utils; 26 import com.android.managedprovisioning.model.ProvisioningParams; 27 import com.android.managedprovisioning.task.wifi.NetworkMonitor; 28 29 /** 30 * A task that enables mobile data and waits for it to successfully connect. If connection times out 31 * {@link #error(int)} will be called. 32 */ 33 public class ConnectMobileNetworkTask extends AbstractProvisioningTask 34 implements NetworkMonitor.NetworkConnectedCallback { 35 private static final int RECONNECT_TIMEOUT_MS = 60000; 36 37 private final NetworkMonitor mNetworkMonitor; 38 39 private Handler mHandler; 40 private boolean mTaskDone = false; 41 42 private final Utils mUtils; 43 private Runnable mTimeoutRunnable; 44 ConnectMobileNetworkTask( Context context, ProvisioningParams provisioningParams, Callback callback)45 public ConnectMobileNetworkTask( 46 Context context, 47 ProvisioningParams provisioningParams, 48 Callback callback) { 49 super(context, provisioningParams, callback); 50 mNetworkMonitor = new NetworkMonitor(context); 51 mUtils = new Utils(); 52 } 53 54 /** 55 * Sets {@link Settings.Global#DEVICE_PROVISIONING_MOBILE_DATA_ENABLED} to 1, and if not already 56 * connected to the network, starts listening for a connection. Calls {@link #success()} when 57 * connected or {@link #error(int)} if it times out after 10 minutes. 58 */ 59 @Override run(int userId)60 public void run(int userId) { 61 Settings.Global.putInt(mContext.getContentResolver(), 62 Settings.Global.DEVICE_PROVISIONING_MOBILE_DATA_ENABLED, 1); 63 64 if (mUtils.isConnectedToNetwork(mContext)) { 65 success(); 66 return; 67 } 68 69 mTaskDone = false; 70 mHandler = new Handler(); 71 mNetworkMonitor.startListening(this); 72 73 // NetworkMonitor will call onNetworkConnected. 74 // Post time out event in case the NetworkMonitor doesn't call back. 75 mTimeoutRunnable = () -> finishTask(false); 76 mHandler.postDelayed(mTimeoutRunnable, RECONNECT_TIMEOUT_MS); 77 } 78 79 @Override getStatusMsgId()80 public int getStatusMsgId() { 81 return R.string.progress_connect_to_mobile_network; 82 } 83 84 @Override onNetworkConnected()85 public void onNetworkConnected() { 86 ProvisionLogger.logd("onNetworkConnected"); 87 if (mUtils.isConnectedToNetwork(mContext)) { 88 ProvisionLogger.logd("Connected to the mobile network"); 89 finishTask(true); 90 // Remove time out callback. 91 mHandler.removeCallbacks(mTimeoutRunnable); 92 } 93 } 94 finishTask(boolean isSuccess)95 private synchronized void finishTask(boolean isSuccess) { 96 if (mTaskDone) { 97 return; 98 } 99 100 mTaskDone = true; 101 mNetworkMonitor.stopListening(); 102 if (isSuccess) { 103 success(); 104 } else { 105 error(0); 106 } 107 } 108 } 109