blob: 1aeda140ed817475b6b0703188b16c86216eb76f [file] [log] [blame]
cretin450328b872015-01-15 16:04:44 -08001/*
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
17package com.cyanogenmod.setupwizard.util;
18
cretin4548ca24e2015-01-19 14:29:43 -080019import android.accounts.AccountManager;
cretin450328b872015-01-15 16:04:44 -080020import android.content.ComponentName;
21import android.content.Context;
cretin4579840ec2015-02-17 17:09:32 -080022import android.content.pm.ComponentInfo;
23import android.content.pm.PackageInfo;
cretin450328b872015-01-15 16:04:44 -080024import android.content.pm.PackageManager;
cretin450328b872015-01-15 16:04:44 -080025import android.net.ConnectivityManager;
26import android.net.NetworkInfo;
27import android.net.wifi.WifiManager;
cretin4586837942015-02-10 12:26:59 -080028import android.os.UserHandle;
29import android.os.UserManager;
cretin45f654deb2015-04-14 17:28:37 -070030import android.telephony.ServiceState;
cretin450328b872015-01-15 16:04:44 -080031import android.telephony.SubscriptionManager;
32import android.telephony.TelephonyManager;
cretin4579840ec2015-02-17 17:09:32 -080033import android.util.Log;
cretin450328b872015-01-15 16:04:44 -080034
Steve Kondikc225cd52015-03-22 17:18:31 -070035import com.android.internal.telephony.SubscriptionController;
36
cretin45f654deb2015-04-14 17:28:37 -070037import com.cyanogenmod.setupwizard.SetupWizardApp;
38
cretin450328b872015-01-15 16:04:44 -080039import com.google.android.gms.common.ConnectionResult;
40import com.google.android.gms.common.GooglePlayServicesUtil;
41
cretin450328b872015-01-15 16:04:44 -080042public 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
cretin450328b872015-01-15 16:04:44 -080057 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) {
cretin45b0e3ace2015-02-04 16:53:28 -080072 try {
73 TelephonyManager tm =
74 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
75 return tm.getDataEnabled();
76 } catch (Exception e) {
77 return false;
cretin450328b872015-01-15 16:04:44 -080078 }
79 }
80
81 public static void setMobileDataEnabled(Context context, boolean enabled) {
82 TelephonyManager tm =
83 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
cretin450328b872015-01-15 16:04:44 -080084 if (tm.isMultiSimEnabled()) {
Steve Kondikc225cd52015-03-22 17:18:31 -070085 int phoneId = SubscriptionManager.from(context).getDefaultDataPhoneId();
cretin450328b872015-01-15 16:04:44 -080086 android.provider.Settings.Global.putInt(context.getContentResolver(),
cretin45b0e3ace2015-02-04 16:53:28 -080087 android.provider.Settings.Global.MOBILE_DATA + phoneId, enabled ? 1 : 0);
Steve Kondikc225cd52015-03-22 17:18:31 -070088 int subId = SubscriptionManager.getDefaultDataSubId();
cretin45b0e3ace2015-02-04 16:53:28 -080089 tm.setDataEnabledUsingSubId(subId, enabled);
cretin450328b872015-01-15 16:04:44 -080090 } else {
91 android.provider.Settings.Global.putInt(context.getContentResolver(),
92 android.provider.Settings.Global.MOBILE_DATA, enabled ? 1 : 0);
cretin45b0e3ace2015-02-04 16:53:28 -080093 tm.setDataEnabled(enabled);
cretin450328b872015-01-15 16:04:44 -080094 }
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 Birg98b5eb92015-03-26 03:44:35 -0700118 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 }
cretin450328b872015-01-15 16:04:44 -0800127 }
128 }
129 return true;
130 }
131
cretin45f654deb2015-04-14 17:28:37 -0700132 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
cretin4586837942015-02-10 12:26:59 -0800145 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
cretin450328b872015-01-15 16:04:44 -0800154 public static boolean hasGMS(Context context) {
155 return GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) !=
156 ConnectionResult.SERVICE_MISSING;
157 }
158
cretin4548ca24e2015-01-19 14:29:43 -0800159 public static boolean accountExists(Context context, String accountType) {
160 return AccountManager.get(context).getAccountsByType(accountType).length > 0;
161 }
162
cretin4501f21da2015-02-23 13:52:37 -0800163 public static void disableSetupWizard(Context context) {
cretin4579840ec2015-02-17 17:09:32 -0800164 disableComponent(context, context.getPackageName(),
165 "com.cyanogenmod.setupwizard.ui.SetupWizardActivity");
166 }
167
cretin4501f21da2015-02-23 13:52:37 -0800168 public static void disableGMSSetupWizard(Context context) {
cretin4579840ec2015-02-17 17:09:32 -0800169 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) {
cretin450d31b312015-03-09 14:49:31 -0700178 Log.e(TAG, "Unable to disable GMS");
cretin4579840ec2015-02-17 17:09:32 -0800179 }
180 }
181
cretin45bfd7ca02015-04-09 12:08:03 -0700182 public static boolean enableGMSSetupWizard(Context context) {
cretin4579840ec2015-02-17 17:09:32 -0800183 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);
cretin45bfd7ca02015-04-09 12:08:03 -0700191 return true;
cretin4579840ec2015-02-17 17:09:32 -0800192 } catch (PackageManager.NameNotFoundException e) {
cretin45bfd7ca02015-04-09 12:08:03 -0700193 Log.e(TAG, "Unable to enable GMS");
194 return false;
cretin4579840ec2015-02-17 17:09:32 -0800195 }
196 }
197
cretin4501f21da2015-02-23 13:52:37 -0800198 private static void disableComponentArray(Context context, ComponentInfo[] components) {
cretin4579840ec2015-02-17 17:09:32 -0800199 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);
cretin450328b872015-01-15 16:04:44 -0800203 }
204 }
cretin4579840ec2015-02-17 17:09:32 -0800205 }
206
cretin4501f21da2015-02-23 13:52:37 -0800207 private static void disableComponent(Context context, String packageName, String name) {
cretin4579840ec2015-02-17 17:09:32 -0800208 disableComponent(context, new ComponentName(packageName, name));
209 }
210
cretin4501f21da2015-02-23 13:52:37 -0800211 private static void disableComponent(Context context, ComponentName component) {
cretin4579840ec2015-02-17 17:09:32 -0800212 context.getPackageManager().setComponentEnabledSetting(component,
cretin45508cfbb2015-02-04 14:00:52 -0800213 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
cretin4579840ec2015-02-17 17:09:32 -0800214 }
215
cretin4501f21da2015-02-23 13:52:37 -0800216 private static void enableComponentArray(Context context, ComponentInfo[] components) {
cretin4579840ec2015-02-17 17:09:32 -0800217 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
cretin4501f21da2015-02-23 13:52:37 -0800225 private static void enableComponent(Context context, String packageName, String name) {
cretin4579840ec2015-02-17 17:09:32 -0800226 enableComponent(context, new ComponentName(packageName, name));
227 }
228
cretin4501f21da2015-02-23 13:52:37 -0800229 private static void enableComponent(Context context, ComponentName component) {
cretin4579840ec2015-02-17 17:09:32 -0800230 context.getPackageManager().setComponentEnabledSetting(component,
231 PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
cretin450328b872015-01-15 16:04:44 -0800232 }
233}