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