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 package com.android.settings.network; 17 18 import static android.content.Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT; 19 import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; 20 import static android.os.UserHandle.myUserId; 21 import static android.os.UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS; 22 23 import static com.android.settingslib.RestrictedLockUtils.hasBaseUserRestriction; 24 25 import android.content.ActivityNotFoundException; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.content.res.Resources; 29 import android.net.ConnectivityManager; 30 import android.net.NetworkInfo; 31 import android.net.Uri; 32 import android.os.Bundle; 33 import android.os.UserManager; 34 import android.support.v7.preference.Preference; 35 import android.telephony.TelephonyManager; 36 import android.text.TextUtils; 37 import android.util.Log; 38 39 import com.android.settings.R; 40 import com.android.settings.core.PreferenceControllerMixin; 41 import com.android.settingslib.Utils; 42 import com.android.settingslib.core.AbstractPreferenceController; 43 import com.android.settingslib.core.lifecycle.LifecycleObserver; 44 import com.android.settingslib.core.lifecycle.events.OnCreate; 45 import com.android.settingslib.core.lifecycle.events.OnSaveInstanceState; 46 47 import java.util.List; 48 49 50 public class MobilePlanPreferenceController extends AbstractPreferenceController 51 implements PreferenceControllerMixin, LifecycleObserver, OnCreate, OnSaveInstanceState { 52 53 public interface MobilePlanPreferenceHost { showMobilePlanMessageDialog()54 void showMobilePlanMessageDialog(); 55 } 56 57 public static final int MANAGE_MOBILE_PLAN_DIALOG_ID = 1; 58 59 private static final String TAG = "MobilePlanPrefContr"; 60 private static final String KEY_MANAGE_MOBILE_PLAN = "manage_mobile_plan"; 61 private static final String SAVED_MANAGE_MOBILE_PLAN_MSG = "mManageMobilePlanMessage"; 62 63 private final UserManager mUserManager; 64 private final boolean mIsSecondaryUser; 65 private final MobilePlanPreferenceHost mHost; 66 67 private ConnectivityManager mCm; 68 private TelephonyManager mTm; 69 70 private String mMobilePlanDialogMessage; 71 MobilePlanPreferenceController(Context context, MobilePlanPreferenceHost host)72 public MobilePlanPreferenceController(Context context, 73 MobilePlanPreferenceHost host) { 74 super(context); 75 mHost = host; 76 mCm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 77 mTm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 78 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 79 mIsSecondaryUser = !mUserManager.isAdminUser(); 80 } 81 82 @Override handlePreferenceTreeClick(Preference preference)83 public boolean handlePreferenceTreeClick(Preference preference) { 84 if (mHost != null && KEY_MANAGE_MOBILE_PLAN.equals(preference.getKey())) { 85 mMobilePlanDialogMessage = null; 86 onManageMobilePlanClick(); 87 } 88 return false; 89 } 90 91 @Override onCreate(Bundle savedInstanceState)92 public void onCreate(Bundle savedInstanceState) { 93 if (savedInstanceState != null) { 94 mMobilePlanDialogMessage = savedInstanceState.getString(SAVED_MANAGE_MOBILE_PLAN_MSG); 95 } 96 Log.d(TAG, "onCreate: mMobilePlanDialogMessage=" + mMobilePlanDialogMessage); 97 } 98 99 @Override onSaveInstanceState(Bundle outState)100 public void onSaveInstanceState(Bundle outState) { 101 if (!TextUtils.isEmpty(mMobilePlanDialogMessage)) { 102 outState.putString(SAVED_MANAGE_MOBILE_PLAN_MSG, mMobilePlanDialogMessage); 103 } 104 } 105 getMobilePlanDialogMessage()106 public String getMobilePlanDialogMessage() { 107 return mMobilePlanDialogMessage; 108 } 109 setMobilePlanDialogMessage(String messasge)110 public void setMobilePlanDialogMessage(String messasge) { 111 mMobilePlanDialogMessage = messasge; 112 } 113 114 @Override isAvailable()115 public boolean isAvailable() { 116 final boolean isPrefAllowedOnDevice = mContext.getResources().getBoolean( 117 com.android.settings.R.bool.config_show_mobile_plan); 118 final boolean isPrefAllowedForUser = !mIsSecondaryUser 119 && !Utils.isWifiOnly(mContext) 120 && !hasBaseUserRestriction(mContext, DISALLOW_CONFIG_MOBILE_NETWORKS, myUserId()); 121 return isPrefAllowedForUser && isPrefAllowedOnDevice; 122 } 123 @Override getPreferenceKey()124 public String getPreferenceKey() { 125 return KEY_MANAGE_MOBILE_PLAN; 126 } 127 onManageMobilePlanClick()128 private void onManageMobilePlanClick() { 129 Resources resources = mContext.getResources(); 130 NetworkInfo ni = mCm.getActiveNetworkInfo(); 131 if (mTm.hasIccCard() && (ni != null)) { 132 // Check for carrier apps that can handle provisioning first 133 Intent provisioningIntent = new Intent(Intent.ACTION_CARRIER_SETUP); 134 List<String> carrierPackages = 135 mTm.getCarrierPackageNamesForIntent(provisioningIntent); 136 if (carrierPackages != null && !carrierPackages.isEmpty()) { 137 if (carrierPackages.size() != 1) { 138 Log.w(TAG, "Multiple matching carrier apps found, launching the first."); 139 } 140 provisioningIntent.setPackage(carrierPackages.get(0)); 141 mContext.startActivity(provisioningIntent); 142 return; 143 } 144 145 // Get provisioning URL 146 String url = mCm.getMobileProvisioningUrl(); 147 if (!TextUtils.isEmpty(url)) { 148 Intent intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, 149 Intent.CATEGORY_APP_BROWSER); 150 intent.setData(Uri.parse(url)); 151 intent.setFlags(FLAG_ACTIVITY_BROUGHT_TO_FRONT | FLAG_ACTIVITY_NEW_TASK); 152 try { 153 mContext.startActivity(intent); 154 } catch (ActivityNotFoundException e) { 155 Log.w(TAG, "onManageMobilePlanClick: startActivity failed" + e); 156 } 157 } else { 158 // No provisioning URL 159 String operatorName = mTm.getSimOperatorName(); 160 if (TextUtils.isEmpty(operatorName)) { 161 // Use NetworkOperatorName as second choice in case there is no 162 // SPN (Service Provider Name on the SIM). Such as with T-mobile. 163 operatorName = mTm.getNetworkOperatorName(); 164 if (TextUtils.isEmpty(operatorName)) { 165 mMobilePlanDialogMessage = 166 resources.getString(R.string.mobile_unknown_sim_operator); 167 } else { 168 mMobilePlanDialogMessage = resources.getString( 169 R.string.mobile_no_provisioning_url, operatorName); 170 } 171 } else { 172 mMobilePlanDialogMessage = 173 resources.getString(R.string.mobile_no_provisioning_url, operatorName); 174 } 175 } 176 } else if (mTm.hasIccCard() == false) { 177 // No sim card 178 mMobilePlanDialogMessage = resources.getString(R.string.mobile_insert_sim_card); 179 } else { 180 // NetworkInfo is null, there is no connection 181 mMobilePlanDialogMessage = resources.getString(R.string.mobile_connect_to_internet); 182 } 183 if (!TextUtils.isEmpty(mMobilePlanDialogMessage)) { 184 Log.d(TAG, "onManageMobilePlanClick: message=" + mMobilePlanDialogMessage); 185 if (mHost != null) { 186 mHost.showMobilePlanMessageDialog(); 187 } else { 188 Log.d(TAG, "Missing host fragment, cannot show message dialog."); 189 } 190 } 191 } 192 } 193