Dirk Vogt | 74af2bf | 2016-05-31 11:21:46 +0200 | [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.fairphone.setupwizard.util; |
| 18 | |
| 19 | import android.accounts.AccountManager; |
| 20 | import android.app.AppGlobals; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.pm.ComponentInfo; |
| 24 | import android.content.pm.PackageInfo; |
| 25 | import android.content.pm.PackageManager; |
| 26 | import android.net.ConnectivityManager; |
| 27 | import android.net.NetworkInfo; |
| 28 | import android.net.wifi.WifiManager; |
| 29 | import android.os.IBinder; |
| 30 | import android.os.ServiceManager; |
| 31 | import android.os.UserHandle; |
| 32 | import android.os.UserManager; |
| 33 | import android.service.persistentdata.PersistentDataBlockManager; |
| 34 | import android.telephony.ServiceState; |
| 35 | import android.telephony.SubscriptionManager; |
| 36 | import android.telephony.TelephonyManager; |
| 37 | import android.util.Log; |
| 38 | |
| 39 | import com.android.internal.widget.LockPatternUtils; |
| 40 | import com.fairphone.setupwizard.SetupWizardApp; |
| 41 | |
| 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 | |
| 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) { |
| 72 | try { |
| 73 | TelephonyManager tm = |
| 74 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 75 | return tm.getDataEnabled(); |
| 76 | } catch (Exception e) { |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public static void setMobileDataEnabled(Context context, boolean enabled) { |
| 82 | TelephonyManager tm = |
| 83 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 84 | if (tm.isMultiSimEnabled()) { |
| 85 | int phoneId = SubscriptionManager.from(context).getDefaultDataPhoneId(); |
| 86 | android.provider.Settings.Global.putInt(context.getContentResolver(), |
| 87 | android.provider.Settings.Global.MOBILE_DATA + phoneId, enabled ? 1 : 0); |
| 88 | int subId = SubscriptionManager.getDefaultDataSubId(); |
| 89 | tm.setDataEnabledUsingSubId(subId, enabled); |
| 90 | } else { |
| 91 | android.provider.Settings.Global.putInt(context.getContentResolver(), |
| 92 | android.provider.Settings.Global.MOBILE_DATA, enabled ? 1 : 0); |
| 93 | tm.setDataEnabled(enabled); |
| 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); |
| 118 | int simCount = SubscriptionManager.from(context).getDefaultDataPhoneId(); |
| 119 | for (int i = 0; i < simCount; i++) { |
| 120 | int simState = tm.getSimState(i); |
| 121 | if (simState != TelephonyManager.SIM_STATE_ABSENT && |
| 122 | simState != TelephonyManager.SIM_STATE_UNKNOWN) { |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | public static boolean isDeviceLocked() { |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | public static boolean frpEnabled(Context context) { |
| 134 | final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager) |
| 135 | context.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE); |
| 136 | return pdbManager != null |
| 137 | && pdbManager.getDataBlockSize() > 0 |
| 138 | && !pdbManager.getOemUnlockEnabled(); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | public static boolean hasAuthorized() { |
| 143 | return ((SetupWizardApp) AppGlobals.getInitialApplication()).isAuthorized(); |
| 144 | } |
| 145 | |
| 146 | public static boolean isRadioReady(Context context, ServiceState state) { |
| 147 | final SetupWizardApp setupWizardApp = (SetupWizardApp)context.getApplicationContext(); |
| 148 | if (setupWizardApp.isRadioReady()) { |
| 149 | return true; |
| 150 | } else { |
| 151 | final boolean ready = state != null |
| 152 | && state.getState() != ServiceState.STATE_POWER_OFF; |
| 153 | setupWizardApp.setRadioReady(ready); |
| 154 | return ready; |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |
| 159 | public static boolean isGuestUser(Context context) { |
| 160 | UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 161 | return userManager.isGuestUser(); |
| 162 | } |
| 163 | |
| 164 | public static boolean isOwner() { |
| 165 | return UserHandle.getCallingUserHandle().isOwner(); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | public static boolean accountExists(Context context, String accountType) { |
| 170 | return AccountManager.get(context).getAccountsByType(accountType).length > 0; |
| 171 | } |
| 172 | |
| 173 | public static boolean isPackageInstalled(Context context, String packageName) { |
| 174 | PackageManager pm = context.getPackageManager(); |
| 175 | try { |
| 176 | pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); |
| 177 | return true; |
| 178 | } catch (PackageManager.NameNotFoundException e) { |
| 179 | return false; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | public static void disableSetupWizard(Context context) { |
| 184 | disableComponent(context, context.getPackageName(), |
| 185 | "com.fairphone.setupwizard.ui.SetupWizardActivity"); |
| 186 | disableComponent(context, context.getPackageName(), |
| 187 | "com.fairphone.setupwizard.setup.FinishSetupReceiver"); |
| 188 | } |
| 189 | |
| 190 | private static void disableComponentArray(Context context, ComponentInfo[] components) { |
| 191 | if(components != null) { |
| 192 | ComponentInfo[] componentInfos = components; |
| 193 | for(int i = 0; i < componentInfos.length; i++) { |
| 194 | disableComponent(context, componentInfos[i].packageName, componentInfos[i].name); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | private static void disableComponent(Context context, String packageName, String name) { |
| 200 | disableComponent(context, new ComponentName(packageName, name)); |
| 201 | } |
| 202 | |
| 203 | private static void disableComponent(Context context, ComponentName component) { |
| 204 | context.getPackageManager().setComponentEnabledSetting(component, |
| 205 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); |
| 206 | } |
| 207 | |
| 208 | private static void enableComponentArray(Context context, ComponentInfo[] components) { |
| 209 | if(components != null) { |
| 210 | ComponentInfo[] componentInfos = components; |
| 211 | for(int i = 0; i < componentInfos.length; i++) { |
| 212 | enableComponent(context, componentInfos[i].packageName, componentInfos[i].name); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | private static void enableComponent(Context context, String packageName, String name) { |
| 218 | enableComponent(context, new ComponentName(packageName, name)); |
| 219 | } |
| 220 | |
| 221 | private static void enableComponent(Context context, ComponentName component) { |
| 222 | context.getPackageManager().setComponentEnabledSetting(component, |
| 223 | PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); |
| 224 | } |
| 225 | |
| 226 | public static boolean hasLeanback(Context context) { |
| 227 | PackageManager packageManager = context.getPackageManager(); |
| 228 | return packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | public static final ComponentName mTvwifisettingsActivity = |
| 233 | new ComponentName("com.android.tv.settings", |
| 234 | "com.android.tv.settings.connectivity.setup.WifiSetupActivity"); |
| 235 | |
| 236 | public static final ComponentName mTvAddAccessorySettingsActivity = |
| 237 | new ComponentName("com.android.tv.settings", |
| 238 | "com.android.tv.settings.accessories.AddAccessoryActivity"); |
| 239 | } |