Search in sources :

Example 66 with KeyguardManager

use of android.app.KeyguardManager in project PhoneProfilesPlus by henrichg.

the class PhoneProfilesService method onStartCommand.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "intent=" + intent);
    serviceRunning = true;
    if (!doForFirstStart(intent)) {
        if (intent != null) {
            if (intent.getBooleanExtra(EXTRA_CLEAR_SERVICE_FOREGROUND, false)) {
                PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "EXTRA_CLEAR_SERVICE_FOREGROUND");
                removeProfileNotification(this);
            }
            if (intent.getBooleanExtra(EXTRA_SWITCH_KEYGUARD, false)) {
                PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "EXTRA_SWITCH_KEYGUARD");
                Context appContext = getApplicationContext();
                boolean isScreenOn;
                // if (android.os.Build.VERSION.SDK_INT >= 20)
                // {
                // Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
                // isScreenOn = display.getState() == Display.STATE_ON;
                // }
                // else
                // {
                PowerManager pm = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE);
                isScreenOn = ((pm != null) && pm.isScreenOn());
                // }
                boolean secureKeyguard;
                if (keyguardManager == null)
                    keyguardManager = (KeyguardManager) appContext.getSystemService(Activity.KEYGUARD_SERVICE);
                if (keyguardManager != null) {
                    secureKeyguard = keyguardManager.isKeyguardSecure();
                    PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "secureKeyguard=" + secureKeyguard);
                    if (!secureKeyguard) {
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand xxx", "getLockScreenDisabled=" + ActivateProfileHelper.getLockScreenDisabled(appContext));
                        if (isScreenOn) {
                            PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "screen on");
                            if (ActivateProfileHelper.getLockScreenDisabled(appContext)) {
                                PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "Keyguard.disable(), START_STICKY");
                                reenableKeyguard();
                                disableKeyguard();
                            } else {
                                PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "Keyguard.reenable(), stopSelf(), START_NOT_STICKY");
                                reenableKeyguard();
                            }
                        }
                    }
                }
            }
            if (intent.getBooleanExtra(EXTRA_REGISTER_RECEIVERS_AND_JOBS, false)) {
                PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "EXTRA_REGISTER_RECEIVERS_AND_JOBS");
                registerReceiversAndJobs();
            }
            if (intent.getBooleanExtra(EXTRA_UNREGISTER_RECEIVERS_AND_JOBS, false)) {
                PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "EXTRA_UNREGISTER_RECEIVERS_AND_JOBS");
                unregisterReceiversAndJobs();
            }
            if (intent.getBooleanExtra(EXTRA_REREGISTER_RECEIVERS_AND_JOBS, false)) {
                PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "EXTRA_REREGISTER_RECEIVERS_AND_JOBS");
                reregisterReceiversAndJobs();
            }
            if (intent.getBooleanExtra(EXTRA_SIMULATE_RINGING_CALL, false)) {
                PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "EXTRA_SIMULATE_RINGING_CALL");
                doSimulatingRingingCall(intent);
            }
            if (intent.getBooleanExtra(EXTRA_START_STOP_SCANNER, false)) {
                PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "EXTRA_START_STOP_SCANNER");
                boolean forScreenOn = intent.getBooleanExtra(EXTRA_FOR_SCREEN_ON, false);
                switch(intent.getIntExtra(EXTRA_START_STOP_SCANNER_TYPE, 0)) {
                    case PPApplication.SCANNER_START_GEOFENCE_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_START_GEOFENCE_SCANNER");
                        startGeofenceScanner(true, true, true, false);
                        scheduleGeofenceScannerJob(true, true, false, false);
                        break;
                    case PPApplication.SCANNER_STOP_GEOFENCE_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_STOP_GEOFENCE_SCANNER");
                        startGeofenceScanner(false, true, false, false);
                        scheduleGeofenceScannerJob(false, false, false, false);
                        break;
                    case PPApplication.SCANNER_START_ORIENTATION_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_START_ORIENTATION_SCANNER");
                        startOrientationScanner(true, true, true);
                        break;
                    case PPApplication.SCANNER_STOP_ORIENTATION_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_STOP_ORIENTATION_SCANNER");
                        startOrientationScanner(false, true, false);
                        break;
                    case PPApplication.SCANNER_START_PHONE_STATE_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_START_PHONE_STATE_SCANNER");
                        PhoneStateScanner.forceStart = false;
                        startPhoneStateScanner(true, true, true, false, false);
                        break;
                    case PPApplication.SCANNER_STOP_PHONE_STATE_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_STOP_PHONE_STATE_SCANNER");
                        startPhoneStateScanner(false, true, false, false, false);
                        break;
                    case PPApplication.SCANNER_REGISTER_RECEIVERS_FOR_WIFI_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_REGISTER_RECEIVERS_FOR_WIFI_SCANNER");
                        registerWifiConnectionBroadcastReceiver(true, true, false);
                        registerWifiStateChangedBroadcastReceiver(true, true, false);
                        registerWifiAPStateChangeBroadcastReceiver(true, true, false);
                        registerWifiScannerReceiver(true, true, false);
                        break;
                    case PPApplication.SCANNER_FORCE_REGISTER_RECEIVERS_FOR_WIFI_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_FORCE_REGISTER_RECEIVERS_FOR_WIFI_SCANNER");
                        registerWifiConnectionBroadcastReceiver(true, false, true);
                        registerWifiStateChangedBroadcastReceiver(true, false, true);
                        registerWifiAPStateChangeBroadcastReceiver(true, false, true);
                        registerWifiScannerReceiver(true, false, true);
                        break;
                    case PPApplication.SCANNER_REGISTER_RECEIVERS_FOR_BLUETOOTH_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_REGISTER_RECEIVERS_FOR_BLUETOOTH_SCANNER");
                        // registerBluetoothConnectionBroadcastReceiver(true, false, true, false);
                        registerBluetoothStateChangedBroadcastReceiver(true, true, false);
                        registerBluetoothScannerReceivers(true, true, false);
                        break;
                    case PPApplication.SCANNER_FORCE_REGISTER_RECEIVERS_FOR_BLUETOOTH_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_FORCE_REGISTER_RECEIVERS_FOR_BLUETOOTH_SCANNER");
                        // registerBluetoothConnectionBroadcastReceiver(true, false, false, true);
                        registerBluetoothStateChangedBroadcastReceiver(true, false, true);
                        registerBluetoothScannerReceivers(true, false, true);
                        break;
                    case PPApplication.SCANNER_RESTART_WIFI_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_RESTART_WIFI_SCANNER");
                        registerWifiConnectionBroadcastReceiver(true, true, false);
                        registerWifiStateChangedBroadcastReceiver(true, true, false);
                        registerWifiAPStateChangeBroadcastReceiver(true, true, false);
                        registerWifiScannerReceiver(true, true, false);
                        scheduleWifiJob(true, true, forScreenOn, false, false, true);
                        break;
                    case PPApplication.SCANNER_RESTART_BLUETOOTH_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_RESTART_BLUETOOTH_SCANNER");
                        // registerBluetoothConnectionBroadcastReceiver(true, false, true, false);
                        registerBluetoothStateChangedBroadcastReceiver(true, true, false);
                        registerBluetoothScannerReceivers(true, true, false);
                        scheduleBluetoothJob(true, true, forScreenOn, false, true);
                        break;
                    case PPApplication.SCANNER_RESTART_PHONE_STATE_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_RESTART_PHONE_STATE_SCANNER");
                        PhoneStateScanner.forceStart = false;
                        startPhoneStateScanner(true, true, true, false, true);
                        break;
                    case PPApplication.SCANNER_FORCE_START_PHONE_STATE_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_FORCE_START_PHONE_STATE_SCANNER");
                        PhoneStateScanner.forceStart = true;
                        startPhoneStateScanner(true, false, false, true, false);
                        break;
                    case PPApplication.SCANNER_RESTART_GEOFENCE_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_RESTART_GEOFENCE_SCANNER");
                        registerLocationModeChangedBroadcastReceiver(true, true);
                        startGeofenceScanner(true, true, true, forScreenOn);
                        scheduleGeofenceScannerJob(true, true, forScreenOn, true);
                        break;
                    case PPApplication.SCANNER_RESTART_ORIENTATION_SCANNER:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_RESTART_ORIENTATION_SCANNER");
                        startOrientationScanner(true, false, true);
                        break;
                    case PPApplication.SCANNER_RESTART_ALL_SCANNERS:
                        PPApplication.logE("$$$ PhoneProfilesService.onStartCommand", "SCANNER_RESTART_ALL_SCANNERS");
                        registerWifiConnectionBroadcastReceiver(true, true, false);
                        registerWifiStateChangedBroadcastReceiver(true, true, false);
                        registerWifiAPStateChangeBroadcastReceiver(true, true, false);
                        registerWifiScannerReceiver(true, true, false);
                        scheduleWifiJob(true, true, forScreenOn, false, false, true);
                        // registerBluetoothConnectionBroadcastReceiver(true, false, true, false);
                        registerBluetoothStateChangedBroadcastReceiver(true, true, false);
                        registerBluetoothScannerReceivers(true, true, false);
                        scheduleBluetoothJob(true, true, forScreenOn, false, true);
                        PhoneStateScanner.forceStart = false;
                        startPhoneStateScanner(true, true, true, false, true);
                        registerLocationModeChangedBroadcastReceiver(true, true);
                        startGeofenceScanner(true, false, true, forScreenOn);
                        scheduleGeofenceScannerJob(true, true, forScreenOn, true);
                        startOrientationScanner(true, false, true);
                        break;
                }
            }
        }
    }
    // stopped, so return sticky.
    return START_STICKY;
}
Also used : Context(android.content.Context) PowerManager(android.os.PowerManager) KeyguardManager(android.app.KeyguardManager)

Example 67 with KeyguardManager

use of android.app.KeyguardManager in project PhoneProfilesPlus by henrichg.

the class Profile method isProfilePreferenceAllowed.

static int isProfilePreferenceAllowed(String preferenceKey, Context context) {
    int featurePresented = PPApplication.PREFERENCE_NOT_ALLOWED;
    if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_AIRPLANE_MODE)) {
        // {
        if (PPApplication.isRooted()) {
            // device is rooted
            if (PPApplication.settingsBinaryExists())
                featurePresented = PPApplication.PREFERENCE_ALLOWED;
            else
                PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_SETTINGS_NOT_FOUND;
        } else
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_ROOTED;
    // }
    // else
    // featurePresented = PPApplication.PREFERENCE_ALLOWED;
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_WIFI)) {
        if (PPApplication.hasSystemFeature(context, PackageManager.FEATURE_WIFI))
            // device has Wifi
            featurePresented = PPApplication.PREFERENCE_ALLOWED;
        else
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_HARDWARE;
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_BLUETOOTH)) {
        if (PPApplication.hasSystemFeature(context, PackageManager.FEATURE_BLUETOOTH))
            // device has bluetooth
            featurePresented = PPApplication.PREFERENCE_ALLOWED;
        else
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_HARDWARE;
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_MOBILE_DATA)) {
        boolean mobileDataSupported;
        if (!PPApplication.hasSystemFeature(context, PackageManager.FEATURE_TELEPHONY)) {
            ConnectivityManager cm = null;
            try {
                cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            } catch (Exception ignored) {
            // java.lang.NullPointerException: missing IConnectivityManager
            // Dual SIM?? Bug in Android ???
            }
            if (cm != null) {
                /*if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                        Network[] networks = cm.getAllNetworks();
                        for (Network network : networks) {
                            NetworkInfo ni = cm.getNetworkInfo(network);
                            Log.d("Profile.isProfilePreferenceAllowed", "ni.getType()="+ni.getType());
                            if (ni.getType() == ConnectivityManager.TYPE_MOBILE) {
                                Log.d("Profile.isProfilePreferenceAllowed", "network type = mobile data");
                                mobileDataSupported = true;
                                break;
                            }
                        }
                    }
                    else {*/
                NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                mobileDataSupported = ni != null;
            // }
            } else
                mobileDataSupported = false;
        } else
            mobileDataSupported = true;
        if (mobileDataSupported) {
            // Log.d("Profile.isProfilePreferenceAllowed", "mobile data supported");
            if (android.os.Build.VERSION.SDK_INT >= 21) {
                // not working :-/
                if (Permissions.hasPermission(context, Manifest.permission.MODIFY_PHONE_STATE)) {
                    if (ActivateProfileHelper.canSetMobileData(context))
                        featurePresented = PPApplication.PREFERENCE_ALLOWED;
                } else if (PPApplication.isRooted()) {
                    // device is rooted
                    // if (serviceBinaryExists() && telephonyServiceExists(context, PREF_PROFILE_DEVICE_MOBILE_DATA))
                    featurePresented = PPApplication.PREFERENCE_ALLOWED;
                } else
                    PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_ROOTED;
            } else {
                if (ActivateProfileHelper.canSetMobileData(context))
                    featurePresented = PPApplication.PREFERENCE_ALLOWED;
                else {
                    PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_SUPPORTED_BY_SYSTEM;
                    PPApplication.notAllowedReasonDetail = context.getString(R.string.preference_not_allowed_reason_detail_cant_be_change);
                }
            }
        } else {
            // Log.d("Profile.isProfilePreferenceAllowed", "mobile data not supported");
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_HARDWARE;
        }
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_MOBILE_DATA_PREFS)) {
        if (PPApplication.hasSystemFeature(context, PackageManager.FEATURE_TELEPHONY)) {
            featurePresented = PPApplication.PREFERENCE_ALLOWED;
        } else
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_HARDWARE;
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_GPS)) {
        if (PPApplication.hasSystemFeature(context, PackageManager.FEATURE_LOCATION_GPS)) {
            // adb shell pm grant sk.henrichg.phoneprofilesplus android.permission.WRITE_SECURE_SETTINGS
            if (Permissions.hasPermission(context, Manifest.permission.WRITE_SECURE_SETTINGS)) {
                if (ActivateProfileHelper.canSetMobileData(context))
                    featurePresented = PPApplication.PREFERENCE_ALLOWED;
            } else if (PPApplication.isRooted()) {
                // device is rooted
                if (PPApplication.settingsBinaryExists())
                    featurePresented = PPApplication.PREFERENCE_ALLOWED;
                else
                    PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_SETTINGS_NOT_FOUND;
            } else if (ActivateProfileHelper.canExploitGPS(context)) {
                featurePresented = PPApplication.PREFERENCE_ALLOWED;
            } else
                PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_ROOTED;
        } else
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_HARDWARE;
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_NFC)) {
        if (PPApplication.hasSystemFeature(context, PackageManager.FEATURE_NFC)) {
            PPApplication.logE("PPApplication.hardwareCheck", "NFC=presented");
            // device has nfc
            if (Permissions.hasPermission(context, Manifest.permission.WRITE_SECURE_SETTINGS)) {
                featurePresented = PPApplication.PREFERENCE_ALLOWED;
            } else if (PPApplication.isRooted())
                featurePresented = PPApplication.PREFERENCE_ALLOWED;
            else
                PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_ROOTED;
        } else {
            PPApplication.logE("PPApplication.hardwareCheck", "NFC=not presented");
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_HARDWARE;
        }
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_WIFI_AP)) {
        if (PPApplication.hasSystemFeature(context, PackageManager.FEATURE_WIFI)) {
            // device has Wifi
            if (android.os.Build.VERSION.SDK_INT < 26) {
                if (WifiApManager.canExploitWifiAP(context))
                    featurePresented = PPApplication.PREFERENCE_ALLOWED;
                else {
                    PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_SUPPORTED_BY_SYSTEM;
                    PPApplication.notAllowedReasonDetail = context.getString(R.string.preference_not_allowed_reason_detail_cant_be_change);
                }
            } else if (PPApplication.isRooted()) {
                // device is rooted
                if (ActivateProfileHelper.wifiServiceExists(Profile.PREF_PROFILE_DEVICE_WIFI_AP)) {
                    if (PPApplication.serviceBinaryExists())
                        featurePresented = PPApplication.PREFERENCE_ALLOWED;
                    else
                        PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_SERVICE_NOT_FOUND;
                } else {
                    PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_SUPPORTED_BY_SYSTEM;
                    PPApplication.notAllowedReasonDetail = context.getString(R.string.preference_not_allowed_reason_detail_cant_be_change);
                }
            } else
                PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_ROOTED;
        } else
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_HARDWARE;
        PPApplication.logE("$$$ WifiAP", "Profile.isProfilePreferenceAllowed-featurePresented=" + featurePresented);
        PPApplication.logE("$$$ WifiAP", "Profile.isProfilePreferenceAllowed-PPApplication.notAllowedReason=" + PPApplication.notAllowedReason);
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_VIBRATE_WHEN_RINGING)) {
        if (android.os.Build.VERSION.SDK_INT == 23) {
            if (PPApplication.isRooted()) {
                // device is rooted
                if (PPApplication.settingsBinaryExists())
                    featurePresented = PPApplication.PREFERENCE_ALLOWED;
                else
                    PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_SETTINGS_NOT_FOUND;
            } else
                PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_ROOTED;
        } else
            featurePresented = PPApplication.PREFERENCE_ALLOWED;
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_ADAPTIVE_BRIGHTNESS)) {
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            if (android.os.Build.VERSION.SDK_INT >= 23) {
                /* not working (private secure settings) :-/
                    if (Permissions.hasPermission(context, Manifest.permission.WRITE_SECURE_SETTINGS)) {
                        featurePresented = PPApplication.PREFERENCE_ALLOWED;
                    }
                    else {*/
                if (PPApplication.isRooted()) {
                    // device is rooted
                    if (PPApplication.settingsBinaryExists())
                        featurePresented = PPApplication.PREFERENCE_ALLOWED;
                    else
                        PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_SETTINGS_NOT_FOUND;
                } else
                    PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_ROOTED;
            // }
            } else
                featurePresented = PPApplication.PREFERENCE_ALLOWED;
        } else {
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_SUPPORTED_BY_SYSTEM;
            PPApplication.notAllowedReasonDetail = context.getString(R.string.preference_not_allowed_reason_detail_old_android);
        }
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_POWER_SAVE_MODE)) {
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            if (Permissions.hasPermission(context, Manifest.permission.WRITE_SECURE_SETTINGS)) {
                featurePresented = PPApplication.PREFERENCE_ALLOWED;
            } else if (PPApplication.isRooted()) {
                // device is rooted
                if (PPApplication.settingsBinaryExists())
                    featurePresented = PPApplication.PREFERENCE_ALLOWED;
                else
                    PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_SETTINGS_NOT_FOUND;
            } else
                PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_ROOTED;
        } else {
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_SUPPORTED_BY_SYSTEM;
            PPApplication.notAllowedReasonDetail = context.getString(R.string.preference_not_allowed_reason_detail_old_android);
        }
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_NETWORK_TYPE)) {
        if (PPApplication.hasSystemFeature(context, PackageManager.FEATURE_TELEPHONY)) {
            final TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (telephonyManager != null) {
                final int phoneType = telephonyManager.getPhoneType();
                if ((phoneType == TelephonyManager.PHONE_TYPE_GSM) || (phoneType == TelephonyManager.PHONE_TYPE_CDMA)) {
                    if (PPApplication.isRooted()) {
                        // device is rooted
                        if (ActivateProfileHelper.telephonyServiceExists(Profile.PREF_PROFILE_DEVICE_NETWORK_TYPE)) {
                            if (PPApplication.serviceBinaryExists())
                                featurePresented = PPApplication.PREFERENCE_ALLOWED;
                            else
                                PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_SERVICE_NOT_FOUND;
                        } else {
                            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_SUPPORTED_BY_SYSTEM;
                            PPApplication.notAllowedReasonDetail = context.getString(R.string.preference_not_allowed_reason_detail_network_type);
                        }
                    } else
                        PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_ROOTED;
                } else {
                    PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_SUPPORTED_BY_SYSTEM;
                    PPApplication.notAllowedReasonDetail = context.getString(R.string.preference_not_allowed_reason_detail_network_type);
                }
            } else {
                PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_SUPPORTED_BY_SYSTEM;
                PPApplication.notAllowedReasonDetail = context.getString(R.string.preference_not_allowed_reason_detail_network_type);
            }
        } else
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_HARDWARE;
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_NOTIFICATION_LED)) {
        int value = Settings.System.getInt(context.getContentResolver(), "notification_light_pulse", -10);
        if ((value != -10) && (android.os.Build.VERSION.SDK_INT >= 23)) {
            /* not working (private secure settings) :-/
                if (Permissions.hasPermission(context, Manifest.permission.WRITE_SECURE_SETTINGS)) {
                    featurePresented = PPApplication.PREFERENCE_ALLOWED;
                }
                else*/
            if (PPApplication.isRooted()) {
                // device is rooted
                if (PPApplication.settingsBinaryExists())
                    featurePresented = PPApplication.PREFERENCE_ALLOWED;
                else
                    PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_SETTINGS_NOT_FOUND;
            } else
                PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_ROOTED;
        } else if (value != -10)
            featurePresented = PPApplication.PREFERENCE_ALLOWED;
        else {
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_SUPPORTED_BY_SYSTEM;
            PPApplication.notAllowedReasonDetail = context.getString(R.string.preference_not_allowed_reason_detail_old_android);
        }
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_KEYGUARD)) {
        boolean secureKeyguard;
        KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Activity.KEYGUARD_SERVICE);
        if (keyguardManager != null) {
            secureKeyguard = keyguardManager.isKeyguardSecure();
            if (secureKeyguard) {
                PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_SUPPORTED_BY_APPLICATION;
                PPApplication.notAllowedReasonDetail = context.getString(R.string.preference_not_allowed_reason_detail_secure_lock);
            } else
                featurePresented = PPApplication.PREFERENCE_ALLOWED;
        }
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_CONNECT_TO_SSID)) {
        if (PPApplication.hasSystemFeature(context, PackageManager.FEATURE_WIFI))
            // device has Wifi
            featurePresented = PPApplication.PREFERENCE_ALLOWED;
        else
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_HARDWARE;
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_APPLICATION_DISABLE_WIFI_SCANNING)) {
        if (PPApplication.hasSystemFeature(context, PackageManager.FEATURE_WIFI))
            // device has Wifi
            featurePresented = PPApplication.PREFERENCE_ALLOWED;
        else
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_HARDWARE;
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_APPLICATION_DISABLE_BLUETOOTH_SCANNING)) {
        if (PPApplication.hasSystemFeature(context, PackageManager.FEATURE_BLUETOOTH))
            // device has bluetooth
            featurePresented = PPApplication.PREFERENCE_ALLOWED;
        else
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_HARDWARE;
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_WIFI_AP_PREFS)) {
        if (PPApplication.hasSystemFeature(context, PackageManager.FEATURE_WIFI)) {
            featurePresented = PPApplication.PREFERENCE_ALLOWED;
        } else
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_HARDWARE;
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_APPLICATION_DISABLE_MOBILE_CELL_SCANNING)) {
        if (PPApplication.hasSystemFeature(context, PackageManager.FEATURE_TELEPHONY))
            featurePresented = PPApplication.PREFERENCE_ALLOWED;
        else
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_HARDWARE;
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_APPLICATION_DISABLE_ORIENTATION_SCANNING)) {
        boolean enabled = (PhoneProfilesService.getAccelerometerSensor(context.getApplicationContext()) != null) && (PhoneProfilesService.getMagneticFieldSensor(context.getApplicationContext()) != null) && (PhoneProfilesService.getAccelerometerSensor(context.getApplicationContext()) != null);
        if (enabled)
            featurePresented = PPApplication.PREFERENCE_ALLOWED;
        else
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_HARDWARE;
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_HEADS_UP_NOTIFICATIONS)) {
        int value = Settings.Global.getInt(context.getContentResolver(), "heads_up_notifications_enabled", -10);
        if ((value != -10) && (android.os.Build.VERSION.SDK_INT >= 21)) {
            if (Permissions.hasPermission(context, Manifest.permission.WRITE_SECURE_SETTINGS)) {
                featurePresented = PPApplication.PREFERENCE_ALLOWED;
            } else if (PPApplication.isRooted()) {
                // device is rooted
                if (PPApplication.settingsBinaryExists())
                    featurePresented = PPApplication.PREFERENCE_ALLOWED;
                else
                    PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_SETTINGS_NOT_FOUND;
            } else
                PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_ROOTED;
        } else if (value != -10)
            featurePresented = PPApplication.PREFERENCE_ALLOWED;
        else {
            PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NOT_SUPPORTED_BY_SYSTEM;
            PPApplication.notAllowedReasonDetail = context.getString(R.string.preference_not_allowed_reason_detail_old_android);
        }
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_RUN_APPLICATION_CHANGE)) {
        // if (AccessibilityServiceBroadcastReceiver.isExtenderInstalled(context.getApplicationContext()))
        featurePresented = PPApplication.PREFERENCE_ALLOWED;
    // else
    // PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_EXTENDER_INSTALLED;
    } else if (preferenceKey.equals(Profile.PREF_PROFILE_DEVICE_FORCE_STOP_APPLICATION_CHANGE)) {
        // if (AccessibilityServiceBroadcastReceiver.isExtenderInstalled(context.getApplicationContext()))
        featurePresented = PPApplication.PREFERENCE_ALLOWED;
    // else
    // PPApplication.notAllowedReason = PPApplication.PREFERENCE_NOT_ALLOWED_NO_EXTENDER_INSTALLED;
    } else
        featurePresented = PPApplication.PREFERENCE_ALLOWED;
    return featurePresented;
}
Also used : NetworkInfo(android.net.NetworkInfo) TelephonyManager(android.telephony.TelephonyManager) ConnectivityManager(android.net.ConnectivityManager) KeyguardManager(android.app.KeyguardManager) SuppressLint(android.annotation.SuppressLint)

Example 68 with KeyguardManager

use of android.app.KeyguardManager in project PhoneProfilesPlus by henrichg.

the class ActivateProfileHelper method execute.

static void execute(final Context context, final Profile _profile) /*, boolean merged, */
/*boolean _interactive*/
{
    PPApplication.logE("##### ActivateProfileHelper.execute", "xxx");
    // unlink ring and notifications - it is @Hide :-(
    // Settings.System.putInt(context.getContentResolver(), Settings.System.NOTIFICATIONS_USE_RING_VOLUME, 0);
    final Profile profile = Profile.getMappedProfile(_profile, context);
    // setup volume
    ActivateProfileHelper.executeForVolumes(profile, true, context);
    // set vibration on touch
    if (Permissions.checkProfileVibrationOnTouch(context, profile, null)) {
        switch(profile._vibrationOnTouch) {
            case 1:
                Settings.System.putInt(context.getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED, 1);
                break;
            case 2:
                Settings.System.putInt(context.getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED, 0);
                break;
        }
    }
    // // setup radio preferences
    ActivateProfileHelper.executeForRadios(profile, context);
    // setup auto-sync
    try {
        boolean _isAutoSync = ContentResolver.getMasterSyncAutomatically();
        boolean _setAutoSync = false;
        switch(profile._deviceAutoSync) {
            case 1:
                if (!_isAutoSync) {
                    _isAutoSync = true;
                    _setAutoSync = true;
                }
                break;
            case 2:
                if (_isAutoSync) {
                    _isAutoSync = false;
                    _setAutoSync = true;
                }
                break;
            case 3:
                _isAutoSync = !_isAutoSync;
                _setAutoSync = true;
                break;
        }
        if (_setAutoSync)
            ContentResolver.setMasterSyncAutomatically(_isAutoSync);
    }// fixed DeadObjectException
     catch (Exception ignored) {
    }
    // screen timeout
    if (Permissions.checkProfileScreenTimeout(context, profile, null)) {
        PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE);
        if ((pm != null) && pm.isScreenOn()) {
            // Log.d("ActivateProfileHelper.execute","screen on");
            if (PPApplication.screenTimeoutHandler != null) {
                PPApplication.screenTimeoutHandler.post(new Runnable() {

                    public void run() {
                        PPApplication.logE("ActivateProfileHelper.execute", "screenTimeoutHandler");
                        setScreenTimeout(profile._deviceScreenTimeout, context);
                    }
                });
            }
        // else
        // setScreenTimeout(profile._deviceScreenTimeout);
        } else {
            // Log.d("ActivateProfileHelper.execute","screen off");
            setActivatedProfileScreenTimeout(context, profile._deviceScreenTimeout);
        }
    }
    // else
    // PPApplication.setActivatedProfileScreenTimeout(context, 0);
    // on/off lock screen
    // PPApplication.logE("$$$ ActivateProfileHelper.execute","keyguard");
    boolean setLockScreen = false;
    switch(profile._deviceKeyguard) {
        case 1:
            // enable lock screen
            PPApplication.logE("$$$ ActivateProfileHelper.execute", "keyguard=ON");
            setLockScreenDisabled(context, false);
            setLockScreen = true;
            break;
        case 2:
            // disable lock screen
            PPApplication.logE("$$$ ActivateProfileHelper.execute", "keyguard=OFF");
            setLockScreenDisabled(context, true);
            setLockScreen = true;
            break;
    }
    if (setLockScreen) {
        boolean isScreenOn;
        // if (android.os.Build.VERSION.SDK_INT >= 20)
        // {
        // Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        // isScreenOn = display.getState() != Display.STATE_OFF;
        // }
        // else
        // {
        PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE);
        if (pm != null) {
            isScreenOn = pm.isScreenOn();
            // }
            PPApplication.logE("$$$ ActivateProfileHelper.execute", "isScreenOn=" + isScreenOn);
            boolean keyguardShowing;
            KeyguardManager kgMgr = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
            if (kgMgr != null) {
                keyguardShowing = kgMgr.isKeyguardLocked();
                PPApplication.logE("$$$ ActivateProfileHelper.execute", "keyguardShowing=" + keyguardShowing);
                if (isScreenOn && !keyguardShowing) {
                    try {
                        // start PhoneProfilesService
                        // PPApplication.firstStartServiceStarted = false;
                        Intent serviceIntent = new Intent(context, PhoneProfilesService.class);
                        serviceIntent.putExtra(PhoneProfilesService.EXTRA_SWITCH_KEYGUARD, true);
                        serviceIntent.putExtra(PhoneProfilesService.EXTRA_ONLY_START, false);
                        PPApplication.startPPService(context, serviceIntent);
                    } catch (Exception ignored) {
                    }
                }
            }
        }
    }
    // setup display brightness
    if (Permissions.checkProfileScreenBrightness(context, profile, null)) {
        if (profile.getDeviceBrightnessChange()) {
            PPApplication.logE("ActivateProfileHelper.execute", "set brightness: profile=" + profile._name);
            PPApplication.logE("ActivateProfileHelper.execute", "set brightness: _deviceBrightness=" + profile._deviceBrightness);
            if (profile.getDeviceBrightnessAutomatic()) {
                Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
                Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, profile.getDeviceBrightnessManualValue(context));
                if (Profile.isProfilePreferenceAllowed(Profile.PREF_PROFILE_DEVICE_ADAPTIVE_BRIGHTNESS, context) == PPApplication.PREFERENCE_ALLOWED) {
                    if (// Not working in Android M (exception)
                    android.os.Build.VERSION.SDK_INT < 23)
                        Settings.System.putFloat(context.getContentResolver(), ADAPTIVE_BRIGHTNESS_SETTING_NAME, profile.getDeviceBrightnessAdaptiveValue(context));
                    else {
                        try {
                            Settings.System.putFloat(context.getContentResolver(), ADAPTIVE_BRIGHTNESS_SETTING_NAME, profile.getDeviceBrightnessAdaptiveValue(context));
                        } catch (Exception ee) {
                            // run service for execute radios
                            ActivateProfileHelper.executeRootForAdaptiveBrightness(profile, context);
                        }
                    }
                }
            } else {
                Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
                Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, profile.getDeviceBrightnessManualValue(context));
            }
            if (PPApplication.brightnessHandler != null) {
                PPApplication.brightnessHandler.post(new Runnable() {

                    public void run() {
                        PPApplication.logE("ActivateProfileHelper.execute", "brightnessHandler");
                        createBrightnessView(context);
                    }
                });
            }
        // else
        // createBrightnessView(context);
        }
    }
    // setup rotation
    if (Permissions.checkProfileAutoRotation(context, profile, null)) {
        switch(profile._deviceAutoRotate) {
            case 1:
                // set autorotate on
                Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
                Settings.System.putInt(context.getContentResolver(), Settings.System.USER_ROTATION, Surface.ROTATION_0);
                break;
            case 2:
                // set autorotate off
                // degree 0
                Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
                Settings.System.putInt(context.getContentResolver(), Settings.System.USER_ROTATION, Surface.ROTATION_0);
                break;
            case 3:
                // set autorotate off
                // degree 90
                Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
                Settings.System.putInt(context.getContentResolver(), Settings.System.USER_ROTATION, Surface.ROTATION_90);
                break;
            case 4:
                // set autorotate off
                // degree 180
                Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
                Settings.System.putInt(context.getContentResolver(), Settings.System.USER_ROTATION, Surface.ROTATION_180);
                break;
            case 5:
                // set autorotate off
                // degree 270
                Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
                Settings.System.putInt(context.getContentResolver(), Settings.System.USER_ROTATION, Surface.ROTATION_270);
                break;
        }
    }
    // set notification led
    if (profile._notificationLed != 0) {
        // if (Permissions.checkProfileNotificationLed(context, profile)) { not needed for Android 6+, because root is required
        switch(profile._notificationLed) {
            case 1:
                setNotificationLed(context, 1);
                break;
            case 2:
                setNotificationLed(context, 0);
                break;
        }
    // }
    }
    // setup wallpaper
    if (Permissions.checkProfileWallpaper(context, profile, null)) {
        if (profile._deviceWallpaperChange == 1) {
            ActivateProfileHelper.executeForWallpaper(profile, context);
        }
    }
    // set power save mode
    ActivateProfileHelper.setPowerSaveMode(profile, context);
    if (Permissions.checkProfileLockDevice(context, profile, null)) {
        if (profile._lockDevice != 0) {
            boolean keyguardLocked;
            KeyguardManager kgMgr = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
            if (kgMgr != null) {
                keyguardLocked = kgMgr.isKeyguardLocked();
                PPApplication.logE("---$$$ ActivateProfileHelper.execute", "keyguardLocked=" + keyguardLocked);
                if (!keyguardLocked) {
                    ActivateProfileHelper.lockDevice(profile, context);
                }
            }
        }
    }
    if (profile._deviceRunApplicationChange == 1) {
        ActivateProfileHelper.executeForRunApplications(profile, context);
    }
    if (profile._applicationDisableWifiScanning != 0) {
        ApplicationPreferences.getSharedPreferences(context);
        SharedPreferences.Editor editor = ApplicationPreferences.preferences.edit();
        editor.putBoolean(ApplicationPreferences.PREF_APPLICATION_EVENT_WIFI_ENABLE_SCANNING, profile._applicationDisableWifiScanning == 2);
        editor.apply();
        PPApplication.restartWifiScanner(context, false);
    }
    if (profile._applicationDisableBluetoothScanning != 0) {
        ApplicationPreferences.getSharedPreferences(context);
        SharedPreferences.Editor editor = ApplicationPreferences.preferences.edit();
        editor.putBoolean(ApplicationPreferences.PREF_APPLICATION_EVENT_BLUETOOTH_ENABLE_SCANNING, profile._applicationDisableBluetoothScanning == 2);
        editor.apply();
        PPApplication.restartBluetoothScanner(context, false);
    }
    if (profile._applicationDisableLocationScanning != 0) {
        ApplicationPreferences.getSharedPreferences(context);
        SharedPreferences.Editor editor = ApplicationPreferences.preferences.edit();
        editor.putBoolean(ApplicationPreferences.PREF_APPLICATION_EVENT_LOCATION_ENABLE_SCANNING, profile._applicationDisableLocationScanning == 2);
        editor.apply();
        PPApplication.restartGeofenceScanner(context, false);
    }
    if (profile._applicationDisableMobileCellScanning != 0) {
        ApplicationPreferences.getSharedPreferences(context);
        SharedPreferences.Editor editor = ApplicationPreferences.preferences.edit();
        editor.putBoolean(ApplicationPreferences.PREF_APPLICATION_EVENT_MOBILE_CELL_ENABLE_SCANNING, profile._applicationDisableMobileCellScanning == 2);
        editor.apply();
        PPApplication.restartPhoneStateScanner(context, false);
    }
    if (profile._applicationDisableOrientationScanning != 0) {
        ApplicationPreferences.getSharedPreferences(context);
        SharedPreferences.Editor editor = ApplicationPreferences.preferences.edit();
        editor.putBoolean(ApplicationPreferences.PREF_APPLICATION_EVENT_ORIENTATION_ENABLE_SCANNING, profile._applicationDisableOrientationScanning == 2);
        editor.apply();
        PPApplication.restartOrientationScanner(context);
    }
    // set heads-up notifications
    if (profile._headsUpNotifications != 0) {
        switch(profile._headsUpNotifications) {
            case 1:
                setHeadsUpNotifications(context, 1);
                break;
            case 2:
                setHeadsUpNotifications(context, 0);
                break;
        }
    }
    PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE);
    KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    if (Profile.isProfilePreferenceAllowed(Profile.PREF_PROFILE_DEVICE_MOBILE_DATA_PREFS, context) == PPApplication.PREFERENCE_ALLOWED) {
        if (profile._deviceMobileDataPrefs == 1) {
            if ((pm != null) && pm.isScreenOn() && (myKM != null) && !myKM.isKeyguardLocked()) {
                boolean ok = true;
                try {
                    Intent intent = new Intent(Intent.ACTION_MAIN, null);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity"));
                    context.startActivity(intent);
                } catch (Exception e) {
                    ok = false;
                }
                if (!ok) {
                    ok = true;
                    try {
                        final Intent intent = new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        final ComponentName componentName = new ComponentName("com.android.phone", "com.android.phone.Settings");
                        intent.setComponent(componentName);
                        context.startActivity(intent);
                    } catch (Exception e) {
                        ok = false;
                    }
                }
                if (!ok) {
                    try {
                        final Intent intent = new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                    } catch (Exception ignored) {
                    }
                }
            } else {
                boolean ok = false;
                Intent intent = new Intent(Intent.ACTION_MAIN, null);
                intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity"));
                if (GlobalGUIRoutines.activityIntentExists(intent, context))
                    ok = true;
                if (!ok) {
                    intent = new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS);
                    intent.setComponent(new ComponentName("com.android.phone", "com.android.phone.Settings"));
                    if (GlobalGUIRoutines.activityIntentExists(intent, context))
                        ok = true;
                }
                if (!ok) {
                    intent = new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS);
                    if (GlobalGUIRoutines.activityIntentExists(intent, context))
                        ok = true;
                }
                if (ok) {
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    String title = context.getString(R.string.profile_activation_interactive_preference_notification_title) + " " + profile._name;
                    String text = context.getString(R.string.profile_activation_interactive_preference_notification_text) + " " + context.getString(R.string.profile_preferences_deviceMobileDataPrefs);
                    showNotificationForInteractiveParameters(context, title, text, intent, PPApplication.PROFILE_ACTIVATION_MOBILE_DATA_PREFS_NOTIFICATION_ID);
                }
            }
        }
    }
    // {  No check only GPS
    if (profile._deviceLocationServicePrefs == 1) {
        if ((pm != null) && pm.isScreenOn() && (myKM != null) && !myKM.isKeyguardLocked()) {
            try {
                final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } catch (Exception ignored) {
            }
        } else {
            final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            if (GlobalGUIRoutines.activityIntentExists(intent, context)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                String title = context.getString(R.string.profile_activation_interactive_preference_notification_title) + " " + profile._name;
                String text = context.getString(R.string.profile_activation_interactive_preference_notification_text) + " " + context.getString(R.string.profile_preferences_deviceLocationServicePrefs);
                showNotificationForInteractiveParameters(context, title, text, intent, PPApplication.PROFILE_ACTIVATION_LOCATION_PREFS_NOTIFICATION_ID);
            }
        }
    }
    // }
    if (profile._deviceWiFiAPPrefs == 1) {
        if ((pm != null) && pm.isScreenOn() && (myKM != null) && !myKM.isKeyguardLocked()) {
            try {
                Intent intent = new Intent(Intent.ACTION_MAIN, null);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.TetherSettings"));
                context.startActivity(intent);
            } catch (Exception ignored) {
            }
        } else {
            Intent intent = new Intent(Intent.ACTION_MAIN, null);
            intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.TetherSettings"));
            if (GlobalGUIRoutines.activityIntentExists(intent, context)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                String title = context.getString(R.string.profile_activation_interactive_preference_notification_title) + " " + profile._name;
                String text = context.getString(R.string.profile_activation_interactive_preference_notification_text) + " " + context.getString(R.string.profile_preferences_deviceWiFiAPPrefs);
                showNotificationForInteractiveParameters(context, title, text, intent, PPApplication.PROFILE_ACTIVATION_WIFI_AP_PREFS_NOTIFICATION_ID);
            }
        }
    }
    // / !!!! must be last !!!!
    if (profile._deviceForceStopApplicationChange == 1) {
        ActivateProfileHelper.executeForForceStopApplications(profile, context);
    }
// throw new RuntimeException("test Crashlytics + TopExceptionHandler");
}
Also used : PowerManager(android.os.PowerManager) SharedPreferences(android.content.SharedPreferences) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) ComponentName(android.content.ComponentName) KeyguardManager(android.app.KeyguardManager) IOException(java.io.IOException)

Example 69 with KeyguardManager

use of android.app.KeyguardManager in project andOTP by andOTP.

the class CredentialsPreference method saveValues.

private void saveValues() {
    byte[] newKey = null;
    if (settings.getEncryption() == EncryptionType.PASSWORD) {
        if (value == AuthMethod.NONE || value == AuthMethod.DEVICE) {
            UIHelper.showGenericDialog(getContext(), R.string.settings_dialog_title_error, R.string.settings_dialog_msg_auth_invalid_with_encryption);
            return;
        }
    }
    if (value == AuthMethod.DEVICE) {
        KeyguardManager km = (KeyguardManager) getContext().getSystemService(KEYGUARD_SERVICE);
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
            Toast.makeText(getContext(), R.string.settings_toast_auth_device_pre_lollipop, Toast.LENGTH_LONG).show();
            return;
        } else if (!km.isKeyguardSecure()) {
            Toast.makeText(getContext(), R.string.settings_toast_auth_device_not_secure, Toast.LENGTH_LONG).show();
            return;
        }
    }
    if (value == AuthMethod.PASSWORD || value == AuthMethod.PIN) {
        String password = passwordInput.getText().toString();
        if (!password.isEmpty()) {
            newKey = settings.setAuthCredentials(password);
        } else {
            return;
        }
    }
    if (settings.getEncryption() == EncryptionType.PASSWORD) {
        if (newKey == null || encryptionChangeCallback == null)
            return;
        if (!encryptionChangeCallback.testEncryptionChange(newKey))
            return;
    }
    persistString(value.toString().toLowerCase());
    setSummary(entries.get(entryValues.indexOf(value)));
}
Also used : KeyguardManager(android.app.KeyguardManager)

Example 70 with KeyguardManager

use of android.app.KeyguardManager in project mobile-messaging-sdk-android by infobip.

the class DeviceInformation method isDeviceSecure.

public static boolean isDeviceSecure(Context context) {
    // Starting with android 6.0 calling isLockScreenDisabled fails altogether because the signature has changed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        KeyguardManager keyguardMgr = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        return keyguardMgr != null && keyguardMgr.isDeviceSecure();
    }
    try {
        String LOCKSCREEN_UTILS_CLASSNAME = "com.android.internal.widget.LockPatternUtils";
        Class<?> lockUtilsClass = Class.forName(LOCKSCREEN_UTILS_CLASSNAME);
        Object lockUtils = lockUtilsClass.getConstructor(Context.class).newInstance(context);
        Method method = lockUtilsClass.getMethod("getActivePasswordQuality");
        // Starting with android 5.x this fails with InvocationTargetException (caused by SecurityException - MANAGE_USERS permission is
        // required because internally some additional logic was added to return false if one can switch between several users)
        // -> therefore if no exception is thrown, we know the screen lock setting is set to Pattern, PIN/PW or something else other than 'None' or 'Swipe'
        Integer lockProtectionLevel = (Integer) method.invoke(lockUtils);
        return lockProtectionLevel >= DevicePolicyManager.PASSWORD_QUALITY_SOMETHING;
    } catch (InvocationTargetException ignored) {
    } catch (Exception e) {
        MobileMessagingLogger.e("Error detecting whether screen lock is disabled: " + e);
    }
    return false;
}
Also used : Context(android.content.Context) Method(java.lang.reflect.Method) KeyguardManager(android.app.KeyguardManager) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

KeyguardManager (android.app.KeyguardManager)94 Intent (android.content.Intent)44 PendingIntent (android.app.PendingIntent)18 PowerManager (android.os.PowerManager)17 Context (android.content.Context)12 SuppressLint (android.annotation.SuppressLint)10 IIntentSender (android.content.IIntentSender)10 IntentSender (android.content.IntentSender)10 FingerprintManager (android.hardware.fingerprint.FingerprintManager)7 Resources (android.content.res.Resources)5 TelephonyManager (android.telephony.TelephonyManager)5 TargetApi (android.annotation.TargetApi)4 ConnectivityManager (android.net.ConnectivityManager)4 RemoteException (android.os.RemoteException)4 Activity (android.app.Activity)3 NotificationManager (android.app.NotificationManager)3 Bitmap (android.graphics.Bitmap)3 NetworkInfo (android.net.NetworkInfo)3 DialogFragment (android.app.DialogFragment)2 Notification (android.app.Notification)2