| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The CyanogenMod 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.cyanogenmod.setupwizard.util; |
| 18 | |
| cretin45 | 48ca24e | 2015-01-19 14:29:43 -0800 | [diff] [blame] | 19 | import android.accounts.AccountManager; |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 22 | import android.content.pm.ComponentInfo; |
| 23 | import android.content.pm.PackageInfo; |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 25 | import android.net.ConnectivityManager; |
| 26 | import android.net.NetworkInfo; |
| 27 | import android.net.wifi.WifiManager; |
| cretin45 | 8683794 | 2015-02-10 12:26:59 -0800 | [diff] [blame] | 28 | import android.os.UserHandle; |
| 29 | import android.os.UserManager; |
| cretin45 | f654deb | 2015-04-14 17:28:37 -0700 | [diff] [blame^] | 30 | import android.telephony.ServiceState; |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 31 | import android.telephony.SubscriptionManager; |
| 32 | import android.telephony.TelephonyManager; |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 33 | import android.util.Log; |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 34 | |
| Steve Kondik | c225cd5 | 2015-03-22 17:18:31 -0700 | [diff] [blame] | 35 | import com.android.internal.telephony.SubscriptionController; |
| 36 | |
| cretin45 | f654deb | 2015-04-14 17:28:37 -0700 | [diff] [blame^] | 37 | import com.cyanogenmod.setupwizard.SetupWizardApp; |
| 38 | |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 39 | import com.google.android.gms.common.ConnectionResult; |
| 40 | import com.google.android.gms.common.GooglePlayServicesUtil; |
| 41 | |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 42 | public class SetupWizardUtils { |
| 43 | |
| 44 | private static final String TAG = SetupWizardUtils.class.getSimpleName(); |
| 45 | |
| 46 | private static final String GOOGLE_SETUPWIZARD_PACKAGE = "com.google.android.setupwizard"; |
| 47 | |
| 48 | private SetupWizardUtils(){} |
| 49 | |
| 50 | public static void tryEnablingWifi(Context context) { |
| 51 | WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); |
| 52 | if (!wifiManager.isWifiEnabled()) { |
| 53 | wifiManager.setWifiEnabled(true); |
| 54 | } |
| 55 | } |
| 56 | |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 57 | public static boolean isNetworkConnected(Context context) { |
| 58 | ConnectivityManager connectivityManager = |
| 59 | (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 60 | NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); |
| 61 | return networkInfo != null && networkInfo.isConnected(); |
| 62 | } |
| 63 | |
| 64 | public static boolean isWifiConnected(Context context) { |
| 65 | ConnectivityManager connectivityManager = |
| 66 | (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 67 | NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); |
| 68 | return mWifi != null && mWifi.isConnected(); |
| 69 | } |
| 70 | |
| 71 | public static boolean isMobileDataEnabled(Context context) { |
| cretin45 | b0e3ace | 2015-02-04 16:53:28 -0800 | [diff] [blame] | 72 | try { |
| 73 | TelephonyManager tm = |
| 74 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 75 | return tm.getDataEnabled(); |
| 76 | } catch (Exception e) { |
| 77 | return false; |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | public static void setMobileDataEnabled(Context context, boolean enabled) { |
| 82 | TelephonyManager tm = |
| 83 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 84 | if (tm.isMultiSimEnabled()) { |
| Steve Kondik | c225cd5 | 2015-03-22 17:18:31 -0700 | [diff] [blame] | 85 | int phoneId = SubscriptionManager.from(context).getDefaultDataPhoneId(); |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 86 | android.provider.Settings.Global.putInt(context.getContentResolver(), |
| cretin45 | b0e3ace | 2015-02-04 16:53:28 -0800 | [diff] [blame] | 87 | android.provider.Settings.Global.MOBILE_DATA + phoneId, enabled ? 1 : 0); |
| Steve Kondik | c225cd5 | 2015-03-22 17:18:31 -0700 | [diff] [blame] | 88 | int subId = SubscriptionManager.getDefaultDataSubId(); |
| cretin45 | b0e3ace | 2015-02-04 16:53:28 -0800 | [diff] [blame] | 89 | tm.setDataEnabledUsingSubId(subId, enabled); |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 90 | } else { |
| 91 | android.provider.Settings.Global.putInt(context.getContentResolver(), |
| 92 | android.provider.Settings.Global.MOBILE_DATA, enabled ? 1 : 0); |
| cretin45 | b0e3ace | 2015-02-04 16:53:28 -0800 | [diff] [blame] | 93 | tm.setDataEnabled(enabled); |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | public static boolean hasTelephony(Context context) { |
| 98 | PackageManager packageManager = context.getPackageManager(); |
| 99 | return packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY); |
| 100 | } |
| 101 | |
| 102 | public static boolean isMultiSimDevice(Context context) { |
| 103 | TelephonyManager tm = |
| 104 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 105 | return tm.isMultiSimEnabled(); |
| 106 | } |
| 107 | |
| 108 | public static boolean isGSMPhone(Context context) { |
| 109 | TelephonyManager tm = |
| 110 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 111 | int phoneType = tm.getPhoneType(); |
| 112 | return phoneType == TelephonyManager.PHONE_TYPE_GSM; |
| 113 | } |
| 114 | |
| 115 | public static boolean isSimMissing(Context context) { |
| 116 | TelephonyManager tm = |
| 117 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| Roman Birg | 98b5eb9 | 2015-03-26 03:44:35 -0700 | [diff] [blame] | 118 | SubscriptionController subscriptionController = SubscriptionController.getInstance(); |
| 119 | if (subscriptionController != null) { |
| 120 | int simCount = subscriptionController.getActiveSubInfoCount(); |
| 121 | for (int i = 0; i < simCount; i++) { |
| 122 | int simState = tm.getSimState(i); |
| 123 | if (simState != TelephonyManager.SIM_STATE_ABSENT && |
| 124 | simState != TelephonyManager.SIM_STATE_UNKNOWN) { |
| 125 | return false; |
| 126 | } |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | return true; |
| 130 | } |
| 131 | |
| cretin45 | f654deb | 2015-04-14 17:28:37 -0700 | [diff] [blame^] | 132 | public static boolean isRadioReady(Context context, ServiceState state) { |
| 133 | final SetupWizardApp setupWizardApp = (SetupWizardApp)context.getApplicationContext(); |
| 134 | if (setupWizardApp.isRadioReady()) { |
| 135 | return true; |
| 136 | } else { |
| 137 | final boolean ready = state != null |
| 138 | && state.getState() != ServiceState.STATE_POWER_OFF; |
| 139 | setupWizardApp.setRadioReady(ready); |
| 140 | return ready; |
| 141 | } |
| 142 | |
| 143 | } |
| 144 | |
| cretin45 | 8683794 | 2015-02-10 12:26:59 -0800 | [diff] [blame] | 145 | public static boolean isGuestUser(Context context) { |
| 146 | UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 147 | return userManager.isGuestUser(); |
| 148 | } |
| 149 | |
| 150 | public static boolean isOwner() { |
| 151 | return UserHandle.getCallingUserHandle().isOwner(); |
| 152 | } |
| 153 | |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 154 | public static boolean hasGMS(Context context) { |
| 155 | return GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) != |
| 156 | ConnectionResult.SERVICE_MISSING; |
| 157 | } |
| 158 | |
| cretin45 | 48ca24e | 2015-01-19 14:29:43 -0800 | [diff] [blame] | 159 | public static boolean accountExists(Context context, String accountType) { |
| 160 | return AccountManager.get(context).getAccountsByType(accountType).length > 0; |
| 161 | } |
| 162 | |
| cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 163 | public static void disableSetupWizard(Context context) { |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 164 | disableComponent(context, context.getPackageName(), |
| 165 | "com.cyanogenmod.setupwizard.ui.SetupWizardActivity"); |
| 166 | } |
| 167 | |
| cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 168 | public static void disableGMSSetupWizard(Context context) { |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 169 | try { |
| 170 | PackageInfo packageInfo = context.getPackageManager() |
| 171 | .getPackageInfo(GOOGLE_SETUPWIZARD_PACKAGE, |
| 172 | PackageManager.GET_ACTIVITIES | |
| 173 | PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES); |
| 174 | disableComponentArray(context, packageInfo.activities); |
| 175 | disableComponentArray(context, packageInfo.services); |
| 176 | disableComponentArray(context, packageInfo.receivers); |
| 177 | } catch (PackageManager.NameNotFoundException e) { |
| cretin45 | 0d31b31 | 2015-03-09 14:49:31 -0700 | [diff] [blame] | 178 | Log.e(TAG, "Unable to disable GMS"); |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| cretin45 | bfd7ca0 | 2015-04-09 12:08:03 -0700 | [diff] [blame] | 182 | public static boolean enableGMSSetupWizard(Context context) { |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 183 | try { |
| 184 | PackageInfo packageInfo = context.getPackageManager() |
| 185 | .getPackageInfo(GOOGLE_SETUPWIZARD_PACKAGE, |
| 186 | PackageManager.GET_ACTIVITIES | |
| 187 | PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES); |
| 188 | enableComponentArray(context, packageInfo.activities); |
| 189 | enableComponentArray(context, packageInfo.services); |
| 190 | enableComponentArray(context, packageInfo.receivers); |
| cretin45 | bfd7ca0 | 2015-04-09 12:08:03 -0700 | [diff] [blame] | 191 | return true; |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 192 | } catch (PackageManager.NameNotFoundException e) { |
| cretin45 | bfd7ca0 | 2015-04-09 12:08:03 -0700 | [diff] [blame] | 193 | Log.e(TAG, "Unable to enable GMS"); |
| 194 | return false; |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 198 | private static void disableComponentArray(Context context, ComponentInfo[] components) { |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 199 | if(components != null) { |
| 200 | ComponentInfo[] componentInfos = components; |
| 201 | for(int i = 0; i < componentInfos.length; i++) { |
| 202 | disableComponent(context, componentInfos[i].packageName, componentInfos[i].name); |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 203 | } |
| 204 | } |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 207 | private static void disableComponent(Context context, String packageName, String name) { |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 208 | disableComponent(context, new ComponentName(packageName, name)); |
| 209 | } |
| 210 | |
| cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 211 | private static void disableComponent(Context context, ComponentName component) { |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 212 | context.getPackageManager().setComponentEnabledSetting(component, |
| cretin45 | 508cfbb | 2015-02-04 14:00:52 -0800 | [diff] [blame] | 213 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0); |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 216 | private static void enableComponentArray(Context context, ComponentInfo[] components) { |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 217 | if(components != null) { |
| 218 | ComponentInfo[] componentInfos = components; |
| 219 | for(int i = 0; i < componentInfos.length; i++) { |
| 220 | enableComponent(context, componentInfos[i].packageName, componentInfos[i].name); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 225 | private static void enableComponent(Context context, String packageName, String name) { |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 226 | enableComponent(context, new ComponentName(packageName, name)); |
| 227 | } |
| 228 | |
| cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 229 | private static void enableComponent(Context context, ComponentName component) { |
| cretin45 | 79840ec | 2015-02-17 17:09:32 -0800 | [diff] [blame] | 230 | context.getPackageManager().setComponentEnabledSetting(component, |
| 231 | PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); |
| cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 232 | } |
| 233 | } |