2015-01-16 00:04:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 The CyanogenMod Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.cyanogenmod.setupwizard.util;
|
|
|
|
|
2015-01-19 22:29:43 +00:00
|
|
|
import android.accounts.AccountManager;
|
2015-05-07 23:21:10 +00:00
|
|
|
import android.app.AppGlobals;
|
2015-01-16 00:04:44 +00:00
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.Context;
|
2015-02-18 01:09:32 +00:00
|
|
|
import android.content.pm.ComponentInfo;
|
|
|
|
import android.content.pm.PackageInfo;
|
2015-01-16 00:04:44 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2015-10-14 19:44:53 +00:00
|
|
|
import android.hardware.fingerprint.FingerprintManager;
|
2015-01-16 00:04:44 +00:00
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.NetworkInfo;
|
|
|
|
import android.net.wifi.WifiManager;
|
2015-02-10 20:26:59 +00:00
|
|
|
import android.os.UserHandle;
|
|
|
|
import android.os.UserManager;
|
2015-09-01 23:19:32 +00:00
|
|
|
import android.service.persistentdata.PersistentDataBlockManager;
|
2015-04-15 00:28:37 +00:00
|
|
|
import android.telephony.ServiceState;
|
2015-01-16 00:04:44 +00:00
|
|
|
import android.telephony.SubscriptionManager;
|
|
|
|
import android.telephony.TelephonyManager;
|
2016-01-25 19:19:15 +00:00
|
|
|
import android.text.TextUtils;
|
2015-02-18 01:09:32 +00:00
|
|
|
import android.util.Log;
|
2015-01-16 00:04:44 +00:00
|
|
|
|
2015-10-14 19:44:53 +00:00
|
|
|
/*import com.android.internal.os.IKillSwitchService;*/
|
2015-04-15 00:28:37 +00:00
|
|
|
import com.cyanogenmod.setupwizard.SetupWizardApp;
|
|
|
|
|
2016-01-25 19:19:15 +00:00
|
|
|
import cyanogenmod.providers.CMSettings;
|
|
|
|
|
|
|
|
import static android.content.res.ThemeConfig.SYSTEM_DEFAULT;
|
2015-01-16 00:04:44 +00:00
|
|
|
|
|
|
|
public class SetupWizardUtils {
|
|
|
|
|
|
|
|
private static final String TAG = SetupWizardUtils.class.getSimpleName();
|
|
|
|
|
2016-04-26 20:43:22 +00:00
|
|
|
public static final String GOOGLE_SETUPWIZARD_PACKAGE = "com.google.android.setupwizard";
|
2015-01-16 00:04:44 +00:00
|
|
|
|
|
|
|
private SetupWizardUtils(){}
|
|
|
|
|
|
|
|
public static void tryEnablingWifi(Context context) {
|
|
|
|
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
|
|
|
|
if (!wifiManager.isWifiEnabled()) {
|
|
|
|
wifiManager.setWifiEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isNetworkConnected(Context context) {
|
|
|
|
ConnectivityManager connectivityManager =
|
|
|
|
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
|
|
|
|
return networkInfo != null && networkInfo.isConnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isWifiConnected(Context context) {
|
|
|
|
ConnectivityManager connectivityManager =
|
|
|
|
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
|
|
|
return mWifi != null && mWifi.isConnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isMobileDataEnabled(Context context) {
|
2015-02-05 00:53:28 +00:00
|
|
|
try {
|
|
|
|
TelephonyManager tm =
|
|
|
|
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
|
|
|
return tm.getDataEnabled();
|
|
|
|
} catch (Exception e) {
|
|
|
|
return false;
|
2015-01-16 00:04:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setMobileDataEnabled(Context context, boolean enabled) {
|
|
|
|
TelephonyManager tm =
|
|
|
|
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
|
|
|
if (tm.isMultiSimEnabled()) {
|
2015-03-23 00:18:31 +00:00
|
|
|
int phoneId = SubscriptionManager.from(context).getDefaultDataPhoneId();
|
2015-01-16 00:04:44 +00:00
|
|
|
android.provider.Settings.Global.putInt(context.getContentResolver(),
|
2015-02-05 00:53:28 +00:00
|
|
|
android.provider.Settings.Global.MOBILE_DATA + phoneId, enabled ? 1 : 0);
|
2015-03-23 00:18:31 +00:00
|
|
|
int subId = SubscriptionManager.getDefaultDataSubId();
|
2015-10-14 19:44:53 +00:00
|
|
|
tm.setDataEnabled(subId, enabled);
|
2015-01-16 00:04:44 +00:00
|
|
|
} else {
|
|
|
|
android.provider.Settings.Global.putInt(context.getContentResolver(),
|
|
|
|
android.provider.Settings.Global.MOBILE_DATA, enabled ? 1 : 0);
|
2015-02-05 00:53:28 +00:00
|
|
|
tm.setDataEnabled(enabled);
|
2015-01-16 00:04:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 16:44:31 +00:00
|
|
|
public static boolean hasWifi(Context context) {
|
|
|
|
PackageManager packageManager = context.getPackageManager();
|
|
|
|
return packageManager.hasSystemFeature(PackageManager.FEATURE_WIFI);
|
|
|
|
}
|
|
|
|
|
2015-01-16 00:04:44 +00:00
|
|
|
public static boolean hasTelephony(Context context) {
|
|
|
|
PackageManager packageManager = context.getPackageManager();
|
|
|
|
return packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isMultiSimDevice(Context context) {
|
|
|
|
TelephonyManager tm =
|
|
|
|
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
|
|
|
return tm.isMultiSimEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isGSMPhone(Context context) {
|
|
|
|
TelephonyManager tm =
|
|
|
|
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
|
|
|
int phoneType = tm.getPhoneType();
|
|
|
|
return phoneType == TelephonyManager.PHONE_TYPE_GSM;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isSimMissing(Context context) {
|
|
|
|
TelephonyManager tm =
|
|
|
|
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
2015-04-18 04:40:43 +00:00
|
|
|
int simCount = SubscriptionManager.from(context).getDefaultDataPhoneId();
|
|
|
|
for (int i = 0; i < simCount; i++) {
|
|
|
|
int simState = tm.getSimState(i);
|
|
|
|
if (simState != TelephonyManager.SIM_STATE_ABSENT &&
|
|
|
|
simState != TelephonyManager.SIM_STATE_UNKNOWN) {
|
|
|
|
return false;
|
2015-01-16 00:04:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-07 23:21:10 +00:00
|
|
|
public static boolean isDeviceLocked() {
|
2015-10-14 19:44:53 +00:00
|
|
|
/* IBinder b = ServiceManager.getService(Context.KILLSWITCH_SERVICE);
|
2015-05-07 23:21:10 +00:00
|
|
|
IKillSwitchService service = IKillSwitchService.Stub.asInterface(b);
|
|
|
|
if (service != null) {
|
|
|
|
try {
|
|
|
|
return service.isDeviceLocked();
|
|
|
|
} catch (Exception e) {
|
|
|
|
// silently fail
|
|
|
|
}
|
2015-10-14 19:44:53 +00:00
|
|
|
}*/
|
2015-05-07 23:21:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-01 23:19:32 +00:00
|
|
|
public static boolean frpEnabled(Context context) {
|
|
|
|
final PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
|
|
|
|
context.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
|
|
|
|
return pdbManager != null
|
|
|
|
&& pdbManager.getDataBlockSize() > 0
|
|
|
|
&& !pdbManager.getOemUnlockEnabled();
|
|
|
|
}
|
|
|
|
|
2015-05-07 23:21:10 +00:00
|
|
|
public static boolean hasKillSwitch() {
|
2015-10-14 19:44:53 +00:00
|
|
|
/* IBinder b = ServiceManager.getService(Context.KILLSWITCH_SERVICE);
|
2015-05-07 23:21:10 +00:00
|
|
|
IKillSwitchService service = IKillSwitchService.Stub.asInterface(b);
|
|
|
|
if (service != null) {
|
|
|
|
try {
|
|
|
|
return service.hasKillSwitch();
|
|
|
|
} catch (Exception e) {
|
|
|
|
// silently fail
|
|
|
|
}
|
2015-10-14 19:44:53 +00:00
|
|
|
} */
|
2015-05-07 23:21:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean hasAuthorized() {
|
|
|
|
return ((SetupWizardApp) AppGlobals.getInitialApplication()).isAuthorized();
|
|
|
|
}
|
|
|
|
|
2015-04-15 00:28:37 +00:00
|
|
|
public static boolean isRadioReady(Context context, ServiceState state) {
|
|
|
|
final SetupWizardApp setupWizardApp = (SetupWizardApp)context.getApplicationContext();
|
|
|
|
if (setupWizardApp.isRadioReady()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
final boolean ready = state != null
|
|
|
|
&& state.getState() != ServiceState.STATE_POWER_OFF;
|
|
|
|
setupWizardApp.setRadioReady(ready);
|
|
|
|
return ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-10 20:26:59 +00:00
|
|
|
public static boolean isGuestUser(Context context) {
|
|
|
|
UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
|
|
|
return userManager.isGuestUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isOwner() {
|
|
|
|
return UserHandle.getCallingUserHandle().isOwner();
|
|
|
|
}
|
|
|
|
|
2015-01-19 22:29:43 +00:00
|
|
|
public static boolean accountExists(Context context, String accountType) {
|
|
|
|
return AccountManager.get(context).getAccountsByType(accountType).length > 0;
|
|
|
|
}
|
|
|
|
|
2015-12-21 16:55:35 +00:00
|
|
|
public static boolean isPackageInstalled(Context context, String packageName) {
|
|
|
|
PackageManager pm = context.getPackageManager();
|
|
|
|
try {
|
|
|
|
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
|
|
|
|
return true;
|
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 21:52:37 +00:00
|
|
|
public static void disableSetupWizard(Context context) {
|
2015-02-18 01:09:32 +00:00
|
|
|
disableComponent(context, context.getPackageName(),
|
|
|
|
"com.cyanogenmod.setupwizard.ui.SetupWizardActivity");
|
2015-06-17 20:56:09 +00:00
|
|
|
disableComponent(context, context.getPackageName(),
|
|
|
|
"com.cyanogenmod.setupwizard.setup.FinishSetupReceiver");
|
2015-02-18 01:09:32 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 21:52:37 +00:00
|
|
|
public static void disableGMSSetupWizard(Context context) {
|
2015-02-18 01:09:32 +00:00
|
|
|
try {
|
|
|
|
PackageInfo packageInfo = context.getPackageManager()
|
|
|
|
.getPackageInfo(GOOGLE_SETUPWIZARD_PACKAGE,
|
|
|
|
PackageManager.GET_ACTIVITIES |
|
|
|
|
PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES);
|
|
|
|
disableComponentArray(context, packageInfo.activities);
|
|
|
|
disableComponentArray(context, packageInfo.services);
|
|
|
|
disableComponentArray(context, packageInfo.receivers);
|
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
2015-03-09 21:49:31 +00:00
|
|
|
Log.e(TAG, "Unable to disable GMS");
|
2015-02-18 01:09:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 19:08:03 +00:00
|
|
|
public static boolean enableGMSSetupWizard(Context context) {
|
2015-02-18 01:09:32 +00:00
|
|
|
try {
|
|
|
|
PackageInfo packageInfo = context.getPackageManager()
|
|
|
|
.getPackageInfo(GOOGLE_SETUPWIZARD_PACKAGE,
|
|
|
|
PackageManager.GET_ACTIVITIES |
|
|
|
|
PackageManager.GET_RECEIVERS | PackageManager.GET_SERVICES);
|
|
|
|
enableComponentArray(context, packageInfo.activities);
|
|
|
|
enableComponentArray(context, packageInfo.services);
|
|
|
|
enableComponentArray(context, packageInfo.receivers);
|
2015-04-09 19:08:03 +00:00
|
|
|
return true;
|
2015-02-18 01:09:32 +00:00
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
2015-04-09 19:08:03 +00:00
|
|
|
Log.e(TAG, "Unable to enable GMS");
|
|
|
|
return false;
|
2015-02-18 01:09:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 21:52:37 +00:00
|
|
|
private static void disableComponentArray(Context context, ComponentInfo[] components) {
|
2015-02-18 01:09:32 +00:00
|
|
|
if(components != null) {
|
|
|
|
ComponentInfo[] componentInfos = components;
|
|
|
|
for(int i = 0; i < componentInfos.length; i++) {
|
|
|
|
disableComponent(context, componentInfos[i].packageName, componentInfos[i].name);
|
2015-01-16 00:04:44 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-18 01:09:32 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 21:52:37 +00:00
|
|
|
private static void disableComponent(Context context, String packageName, String name) {
|
2015-02-18 01:09:32 +00:00
|
|
|
disableComponent(context, new ComponentName(packageName, name));
|
|
|
|
}
|
|
|
|
|
2015-02-23 21:52:37 +00:00
|
|
|
private static void disableComponent(Context context, ComponentName component) {
|
2015-02-18 01:09:32 +00:00
|
|
|
context.getPackageManager().setComponentEnabledSetting(component,
|
2016-01-12 22:43:36 +00:00
|
|
|
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
|
2015-02-18 01:09:32 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 21:52:37 +00:00
|
|
|
private static void enableComponentArray(Context context, ComponentInfo[] components) {
|
2015-02-18 01:09:32 +00:00
|
|
|
if(components != null) {
|
|
|
|
ComponentInfo[] componentInfos = components;
|
|
|
|
for(int i = 0; i < componentInfos.length; i++) {
|
|
|
|
enableComponent(context, componentInfos[i].packageName, componentInfos[i].name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 21:52:37 +00:00
|
|
|
private static void enableComponent(Context context, String packageName, String name) {
|
2015-02-18 01:09:32 +00:00
|
|
|
enableComponent(context, new ComponentName(packageName, name));
|
|
|
|
}
|
|
|
|
|
2015-02-23 21:52:37 +00:00
|
|
|
private static void enableComponent(Context context, ComponentName component) {
|
2015-02-18 01:09:32 +00:00
|
|
|
context.getPackageManager().setComponentEnabledSetting(component,
|
|
|
|
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
|
2015-01-16 00:04:44 +00:00
|
|
|
}
|
2015-04-23 05:30:19 +00:00
|
|
|
|
|
|
|
public static boolean hasLeanback(Context context) {
|
|
|
|
PackageManager packageManager = context.getPackageManager();
|
|
|
|
return packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
|
|
|
|
}
|
|
|
|
|
2015-07-08 00:30:20 +00:00
|
|
|
public static boolean hasFingerprint(Context context) {
|
2015-10-14 19:44:53 +00:00
|
|
|
FingerprintManager fingerprintManager = (FingerprintManager)
|
|
|
|
context.getSystemService(Context.FINGERPRINT_SERVICE);
|
|
|
|
return fingerprintManager.isHardwareDetected();
|
2015-07-08 00:30:20 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 19:19:15 +00:00
|
|
|
public static String getDefaultThemePackageName(Context context) {
|
|
|
|
final String defaultThemePkg = CMSettings.Secure.getString(context.getContentResolver(),
|
|
|
|
CMSettings.Secure.DEFAULT_THEME_PACKAGE);
|
|
|
|
if (!TextUtils.isEmpty(defaultThemePkg)) {
|
|
|
|
PackageManager pm = context.getPackageManager();
|
|
|
|
try {
|
|
|
|
if (pm.getPackageInfo(defaultThemePkg, 0) != null) {
|
|
|
|
return defaultThemePkg;
|
|
|
|
}
|
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
// doesn't exist so system will be default
|
|
|
|
Log.w(TAG, "Default theme " + defaultThemePkg + " not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SYSTEM_DEFAULT;
|
|
|
|
}
|
|
|
|
|
2015-04-23 05:30:19 +00:00
|
|
|
public static final ComponentName mTvwifisettingsActivity =
|
|
|
|
new ComponentName("com.android.tv.settings",
|
|
|
|
"com.android.tv.settings.connectivity.setup.WifiSetupActivity");
|
2015-04-24 02:54:36 +00:00
|
|
|
|
|
|
|
public static final ComponentName mTvAddAccessorySettingsActivity =
|
|
|
|
new ComponentName("com.android.tv.settings",
|
|
|
|
"com.android.tv.settings.accessories.AddAccessoryActivity");
|
2015-01-16 00:04:44 +00:00
|
|
|
}
|