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 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 @Override 55 public void run(int userId) { 56 Settings.Global.putInt(mContext.getContentResolver(), 57 Settings.Global.DEVICE_PROVISIONING_MOBILE_DATA_ENABLED, 1); 58 59 if (mUtils.isConnectedToNetwork(mContext)) { 60 success(); 61 return; 62 } 63 64 mTaskDone = false; 65 mHandler = new Handler(); 66 mNetworkMonitor.startListening(this); 67 68 // NetworkMonitor will call onNetworkConnected. 69 // Post time out event in case the NetworkMonitor doesn't call back. 70 mTimeoutRunnable = () -> finishTask(false); 71 mHandler.postDelayed(mTimeoutRunnable, RECONNECT_TIMEOUT_MS); 72 } 73 74 @Override 75 public int getStatusMsgId() { 76 return R.string.progress_connect_to_mobile_network; 77 } 78 79 @Override 80 public void onNetworkConnected() { 81 ProvisionLogger.logd("onNetworkConnected"); 82 if (mUtils.isConnectedToNetwork(mContext)) { 83 ProvisionLogger.logd("Connected to the mobile network"); 84 finishTask(true); 85 // Remove time out callback. 86 mHandler.removeCallbacks(mTimeoutRunnable); 87 } 88 } 89 90 private synchronized void finishTask(boolean isSuccess) { 91 if (mTaskDone) { 92 return; 93 } 94 95 mTaskDone = true; 96 mNetworkMonitor.stopListening(); 97 if (isSuccess) { 98 success(); 99 } else { 100 error(0); 101 } 102 } 103 } 104