Search in sources :

Example 71 with KeyguardManager

use of android.app.KeyguardManager in project android_packages_apps_Dialer by LineageOS.

the class SpecialCharSequenceMgr method handleAdnEntry.

/**
 * Handle ADN requests by filling in the SIM contact number into the requested EditText.
 *
 * <p>This code works alongside the Asynchronous query handler {@link QueryHandler} and query
 * cancel handler implemented in {@link SimContactQueryCookie}.
 */
static boolean handleAdnEntry(Context context, String input, EditText textField) {
    /* ADN entries are of the form "N(N)(N)#" */
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager == null || telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_GSM) {
        return false;
    }
    // if the phone is keyguard-restricted, then just ignore this
    // input.  We want to make sure that sim card contacts are NOT
    // exposed unless the phone is unlocked, and this code can be
    // accessed from the emergency dialer.
    KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    if (keyguardManager.inKeyguardRestrictedInputMode()) {
        return false;
    }
    int len = input.length();
    if ((len > 1) && (len < 5) && (input.endsWith("#"))) {
        try {
            // get the ordinal number of the sim contact
            final int index = Integer.parseInt(input.substring(0, len - 1));
            // The original code that navigated to a SIM Contacts list view did not
            // highlight the requested contact correctly, a requirement for PTCRB
            // certification.  This behaviour is consistent with the UI paradigm
            // for touch-enabled lists, so it does not make sense to try to work
            // around it.  Instead we fill in the the requested phone number into
            // the dialer text field.
            // create the async query handler
            final QueryHandler handler = new QueryHandler(context.getContentResolver());
            // create the cookie object
            final SimContactQueryCookie sc = new SimContactQueryCookie(index - 1, handler, ADN_QUERY_TOKEN);
            // setup the cookie fields
            sc.contactNum = index - 1;
            sc.setTextField(textField);
            // create the progress dialog
            sc.progressDialog = new ProgressDialog(context);
            sc.progressDialog.setTitle(R.string.simContacts_title);
            sc.progressDialog.setMessage(context.getText(R.string.simContacts_emptyLoading));
            sc.progressDialog.setIndeterminate(true);
            sc.progressDialog.setCancelable(true);
            sc.progressDialog.setOnCancelListener(sc);
            sc.progressDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
            List<PhoneAccountHandle> subscriptionAccountHandles = PhoneAccountUtils.getSubscriptionPhoneAccounts(context);
            Context applicationContext = context.getApplicationContext();
            boolean hasUserSelectedDefault = subscriptionAccountHandles.contains(TelecomUtil.getDefaultOutgoingPhoneAccount(applicationContext, PhoneAccount.SCHEME_TEL));
            if (subscriptionAccountHandles.size() <= 1 || hasUserSelectedDefault) {
                Uri uri = TelecomUtil.getAdnUriForPhoneAccount(applicationContext, null);
                handleAdnQuery(handler, sc, uri);
            } else {
                SelectPhoneAccountListener callback = new HandleAdnEntryAccountSelectedCallback(applicationContext, handler, sc);
                DialogFragment dialogFragment = SelectPhoneAccountDialogFragment.newInstance(subscriptionAccountHandles, callback, null);
                dialogFragment.show(((Activity) context).getFragmentManager(), TAG_SELECT_ACCT_FRAGMENT);
            }
            return true;
        } catch (NumberFormatException ex) {
        // Ignore
        }
    }
    return false;
}
Also used : Context(android.content.Context) NoNullCursorAsyncQueryHandler(com.android.contacts.common.database.NoNullCursorAsyncQueryHandler) PhoneAccountHandle(android.telecom.PhoneAccountHandle) DialogFragment(android.app.DialogFragment) SelectPhoneAccountDialogFragment(com.android.contacts.common.widget.SelectPhoneAccountDialogFragment) ProgressDialog(android.app.ProgressDialog) Uri(android.net.Uri) TelephonyManager(android.telephony.TelephonyManager) SelectPhoneAccountListener(com.android.contacts.common.widget.SelectPhoneAccountDialogFragment.SelectPhoneAccountListener) KeyguardManager(android.app.KeyguardManager)

Example 72 with KeyguardManager

use of android.app.KeyguardManager in project android_packages_apps_Settings by omnirom.

the class Utils method confirmWorkProfileCredentials.

private static boolean confirmWorkProfileCredentials(Context context, int userId) {
    final KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    final Intent unlockIntent = km.createConfirmDeviceCredentialIntent(null, null, userId);
    if (unlockIntent != null) {
        context.startActivity(unlockIntent);
        return true;
    } else {
        return false;
    }
}
Also used : Intent(android.content.Intent) KeyguardManager(android.app.KeyguardManager)

Example 73 with KeyguardManager

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

the class ActivateProfileHelper method execute.

public static void execute(final Context context, Profile _profile) /*, boolean _interactive*/
{
    // separate ringing and notification - is marked with @Hide :-(
    // Settings.System.putInt(context.getContentResolver(), Settings.System.NOTIFICATIONS_USE_RING_VOLUME, 0);
    final Profile profile = Profile.getMappedProfile(_profile, context);
    // setup volume and ringer mode
    ActivateProfileHelper.executeForVolumes(context, profile, PhoneCallBroadcastReceiver.LINKMODE_NONE, true);
    // 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 tones
    // moved to executeForVolumes
    // setTones(profile);
    // setup radio preferences
    ActivateProfileHelper.executeForRadios(context, profile);
    // 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(Context.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() {
                        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
    boolean setLockScreen = false;
    switch(profile._deviceKeyguard) {
        case 1:
            // enable lockscreen
            setLockScreenDisabled(context, false);
            setLockScreen = true;
            break;
        case 2:
            // disable lockscreen
            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(Context.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();
                if (isScreenOn && !keyguardShowing) {
                    try {
                        // start PhoneProfilesService
                        // PPApplication.firstStartServiceStarted = false;
                        Intent serviceIntent = new Intent(context, PhoneProfilesService.class);
                        serviceIntent.putExtra(PhoneProfilesService.EXTRA_SWITCH_KEYGUARD, true);
                        PPApplication.startPPService(context, serviceIntent);
                    } catch (Exception ignored) {
                    }
                }
            }
        }
    }
    // setup brightness
    if (Permissions.checkProfileScreenBrightness(context, profile, null)) {
        if (profile.getDeviceBrightnessChange()) {
            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) {
                            ActivateProfileHelper.executeRootForAdaptiveBrightness(context, profile);
                        }
                    }
                }
            } 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() {
                        createBrightnessView(profile, context);
                    }
                });
            }
        // else
        // createBrightnessView(profile, context);
        }
    }
    // setup rotate
    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(context, profile);
        }
    }
    // Intent rootServiceIntent;
    // set power save mode
    ActivateProfileHelper.setPowerSaveMode(context, profile);
    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(context, profile);
                }
            }
        }
    }
    if (profile._deviceRunApplicationChange == 1) {
        ActivateProfileHelper.executeForRunApplications(context, profile);
    }
    // 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);
    }
}
Also used : PowerManager(android.os.PowerManager) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) ComponentName(android.content.ComponentName) KeyguardManager(android.app.KeyguardManager) IOException(java.io.IOException)

Example 74 with KeyguardManager

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

the class PhoneProfilesService method showProfileNotification.

// profile notification -------------------------------------------
@SuppressLint("NewApi")
public void showProfileNotification(DataWrapper dataWrapper) {
    if (ActivateProfileHelper.lockRefresh)
        // no refresh notification
        return;
    if (serviceRunning && ((Build.VERSION.SDK_INT >= 26) || ApplicationPreferences.notificationStatusBar(dataWrapper.context))) {
        // close showed notification
        // notificationManager.cancel(PPApplication.NOTIFICATION_ID);
        Intent intent = new Intent(dataWrapper.context, ActivateProfileActivity.class);
        // clear all opened activities
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(PPApplication.EXTRA_STARTUP_SOURCE, PPApplication.STARTUP_SOURCE_NOTIFICATION);
        PendingIntent pIntent = PendingIntent.getActivity(dataWrapper.context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification.Builder notificationBuilder;
        boolean miui = (PPApplication.romManufacturer != null) && (PPApplication.romManufacturer.compareToIgnoreCase("xiaomi") == 0) && (android.os.Build.VERSION.SDK_INT >= 24);
        RemoteViews contentView;
        /*if (ApplicationPreferences.notificationTheme(dataWrapper.context).equals("1"))
                contentView = new RemoteViews(dataWrapper.context.getPackageName(), R.layout.notification_drawer_dark);
            else
            if (ApplicationPreferences.notificationTheme(dataWrapper.context).equals("2"))
                contentView = new RemoteViews(dataWrapper.context.getPackageName(), R.layout.notification_drawer_light);
            else {*/
        if (miui)
            contentView = new RemoteViews(dataWrapper.context.getPackageName(), R.layout.notification_drawer_miui);
        else
            contentView = new RemoteViews(dataWrapper.context.getPackageName(), R.layout.notification_drawer);
        // }
        boolean isIconResourceID;
        String iconIdentifier;
        String profileName;
        Bitmap iconBitmap;
        Bitmap preferencesIndicator;
        Profile profile = dataWrapper.getActivatedProfileFromDB(true, ApplicationPreferences.notificationPrefIndicator(dataWrapper.context));
        if (profile != null) {
            isIconResourceID = profile.getIsIconResourceID();
            iconIdentifier = profile.getIconIdentifier();
            profileName = profile.getProfileNameWithDuration(false, dataWrapper.context);
            iconBitmap = profile._iconBitmap;
            preferencesIndicator = profile._preferencesIndicator;
        } else {
            isIconResourceID = true;
            iconIdentifier = Profile.PROFILE_ICON_DEFAULT;
            profileName = dataWrapper.context.getResources().getString(R.string.profiles_header_profile_name_no_activated);
            iconBitmap = null;
            preferencesIndicator = null;
        }
        notificationBuilder = new Notification.Builder(dataWrapper.context);
        notificationBuilder.setContentIntent(pIntent);
        if (Build.VERSION.SDK_INT >= 21)
            notificationBuilder.setColor(ContextCompat.getColor(dataWrapper.context, R.color.primary));
        if (Build.VERSION.SDK_INT >= 26) {
            PPApplication.createProfileNotificationChannel(/*profile, */
            dataWrapper.context);
            notificationBuilder.setChannelId(PPApplication.PROFILE_NOTIFICATION_CHANNEL);
        // notificationBuilder.setSettingsText("Pokus");
        } else {
            if (ApplicationPreferences.notificationShowInStatusBar(dataWrapper.context)) {
                KeyguardManager myKM = (KeyguardManager) dataWrapper.context.getSystemService(Context.KEYGUARD_SERVICE);
                if (myKM != null) {
                    // boolean screenUnlocked = !myKM.inKeyguardRestrictedInputMode();
                    boolean screenUnlocked = !myKM.isKeyguardLocked();
                    // boolean screenUnlocked = getScreenUnlocked(context);
                    if ((ApplicationPreferences.notificationHideInLockScreen(dataWrapper.context) && (!screenUnlocked)) || ((profile != null) && profile._hideStatusBarIcon))
                        notificationBuilder.setPriority(Notification.PRIORITY_MIN);
                    else
                        notificationBuilder.setPriority(Notification.PRIORITY_DEFAULT);
                } else
                    notificationBuilder.setPriority(Notification.PRIORITY_DEFAULT);
            } else
                notificationBuilder.setPriority(Notification.PRIORITY_MIN);
        }
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            notificationBuilder.setCategory(Notification.CATEGORY_STATUS);
            notificationBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
        }
        notificationBuilder.setTicker(profileName);
        if (isIconResourceID) {
            int iconSmallResource;
            if (iconBitmap != null) {
                if (ApplicationPreferences.notificationStatusBarStyle(dataWrapper.context).equals("0")) {
                    // colorful icon
                    // FC in Note 4, 6.0.1 :-/
                    boolean isNote4 = (PPApplication.romManufacturer != null) && (PPApplication.romManufacturer.compareToIgnoreCase("samsung") == 0) && /*(Build.MODEL.startsWith("SM-N910") ||  // Samsung Note 4
                                 Build.MODEL.startsWith("SM-G900")     // Samsung Galaxy S5
                                ) &&*/
                    (android.os.Build.VERSION.SDK_INT == 23);
                    // Log.d("ActivateProfileHelper.showNotification","isNote4="+isNote4);
                    if ((android.os.Build.VERSION.SDK_INT >= 23) && (!isNote4)) {
                        notificationBuilder.setSmallIcon(ColorNotificationIcon.getFromBitmap(iconBitmap));
                    } else {
                        iconSmallResource = dataWrapper.context.getResources().getIdentifier(iconIdentifier + "_notify_color", "drawable", dataWrapper.context.getPackageName());
                        if (iconSmallResource == 0)
                            iconSmallResource = R.drawable.ic_profile_default;
                        notificationBuilder.setSmallIcon(iconSmallResource);
                    }
                } else {
                    // native icon
                    iconSmallResource = dataWrapper.context.getResources().getIdentifier(iconIdentifier + "_notify", "drawable", dataWrapper.context.getPackageName());
                    if (iconSmallResource == 0)
                        iconSmallResource = R.drawable.ic_profile_default_notify;
                    notificationBuilder.setSmallIcon(iconSmallResource);
                }
                contentView.setImageViewBitmap(R.id.notification_activated_profile_icon, iconBitmap);
            } else {
                if (ApplicationPreferences.notificationStatusBarStyle(dataWrapper.context).equals("0")) {
                    // colorful icon
                    iconSmallResource = dataWrapper.context.getResources().getIdentifier(iconIdentifier + "_notify_color", "drawable", dataWrapper.context.getPackageName());
                    if (iconSmallResource == 0)
                        iconSmallResource = R.drawable.ic_profile_default;
                    notificationBuilder.setSmallIcon(iconSmallResource);
                    int iconLargeResource = dataWrapper.context.getResources().getIdentifier(iconIdentifier, "drawable", dataWrapper.context.getPackageName());
                    if (iconLargeResource == 0)
                        iconLargeResource = R.drawable.ic_profile_default;
                    Bitmap largeIcon = BitmapFactory.decodeResource(dataWrapper.context.getResources(), iconLargeResource);
                    contentView.setImageViewBitmap(R.id.notification_activated_profile_icon, largeIcon);
                } else {
                    // native icon
                    iconSmallResource = dataWrapper.context.getResources().getIdentifier(iconIdentifier + "_notify", "drawable", dataWrapper.context.getPackageName());
                    if (iconSmallResource == 0)
                        iconSmallResource = R.drawable.ic_profile_default_notify;
                    notificationBuilder.setSmallIcon(iconSmallResource);
                    int iconLargeResource = dataWrapper.context.getResources().getIdentifier(iconIdentifier, "drawable", dataWrapper.context.getPackageName());
                    if (iconLargeResource == 0)
                        iconLargeResource = R.drawable.ic_profile_default;
                    Bitmap largeIcon = BitmapFactory.decodeResource(dataWrapper.context.getResources(), iconLargeResource);
                    contentView.setImageViewBitmap(R.id.notification_activated_profile_icon, largeIcon);
                }
            }
        } else {
            // FC in Note 4, 6.0.1 :-/
            boolean isNote4 = (PPApplication.romManufacturer != null) && (PPApplication.romManufacturer.compareToIgnoreCase("samsung") == 0) && /*(Build.MODEL.startsWith("SM-N910") ||  // Samsung Note 4
                         Build.MODEL.startsWith("SM-G900")     // Samsung Galaxy S5
                        ) &&*/
            (android.os.Build.VERSION.SDK_INT == 23);
            // Log.d("ActivateProfileHelper.showNotification","isNote4="+isNote4);
            if ((Build.VERSION.SDK_INT >= 23) && (!isNote4) && (iconBitmap != null)) {
                notificationBuilder.setSmallIcon(ColorNotificationIcon.getFromBitmap(iconBitmap));
            } else {
                int iconSmallResource;
                if (ApplicationPreferences.notificationStatusBarStyle(dataWrapper.context).equals("0"))
                    iconSmallResource = R.drawable.ic_profile_default;
                else
                    iconSmallResource = R.drawable.ic_profile_default_notify;
                notificationBuilder.setSmallIcon(iconSmallResource);
            }
            if (iconBitmap != null)
                contentView.setImageViewBitmap(R.id.notification_activated_profile_icon, iconBitmap);
            else
                contentView.setImageViewResource(R.id.notification_activated_profile_icon, R.drawable.ic_profile_default);
        }
        if (ApplicationPreferences.notificationTextColor(dataWrapper.context).equals("1")) {
            contentView.setTextColor(R.id.notification_activated_profile_name, Color.BLACK);
        } else if (ApplicationPreferences.notificationTextColor(dataWrapper.context).equals("2")) {
            contentView.setTextColor(R.id.notification_activated_profile_name, Color.WHITE);
        }
        contentView.setTextViewText(R.id.notification_activated_profile_name, profileName);
        if ((preferencesIndicator != null) && (ApplicationPreferences.notificationPrefIndicator(dataWrapper.context)))
            contentView.setImageViewBitmap(R.id.notification_activated_profile_pref_indicator, preferencesIndicator);
        else
            contentView.setImageViewResource(R.id.notification_activated_profile_pref_indicator, R.drawable.ic_empty);
        if (android.os.Build.VERSION.SDK_INT >= 24) {
            // workaround for MIUI :-(
            if (!miui)
                notificationBuilder.setStyle(new Notification.DecoratedCustomViewStyle());
            notificationBuilder.setCustomContentView(contentView);
        } else
            notificationBuilder.setContent(contentView);
        try {
            Notification notification = notificationBuilder.build();
            if ((Build.VERSION.SDK_INT >= 26) || ApplicationPreferences.notificationStatusBarPermanent(dataWrapper.context)) {
                // notification.flags |= Notification.FLAG_NO_CLEAR;
                notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
            } else {
                setAlarmForNotificationCancel(dataWrapper.context);
            }
            if ((Build.VERSION.SDK_INT >= 26) || ApplicationPreferences.notificationStatusBarPermanent(dataWrapper.context))
                startForeground(PPApplication.PROFILE_NOTIFICATION_ID, notification);
            else {
                NotificationManager notificationManager = (NotificationManager) dataWrapper.context.getSystemService(Context.NOTIFICATION_SERVICE);
                if (notificationManager != null)
                    notificationManager.notify(PPApplication.PROFILE_NOTIFICATION_ID, notification);
            }
        } catch (Exception ignored) {
        }
    } else {
        if ((Build.VERSION.SDK_INT >= 26) || ApplicationPreferences.notificationStatusBarPermanent(dataWrapper.context))
            stopForeground(true);
        else {
            NotificationManager notificationManager = (NotificationManager) dataWrapper.context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (notificationManager != null)
                notificationManager.cancel(PPApplication.PROFILE_NOTIFICATION_ID);
        }
    }
}
Also used : NotificationManager(android.app.NotificationManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification) SuppressLint(android.annotation.SuppressLint) RemoteViews(android.widget.RemoteViews) Bitmap(android.graphics.Bitmap) PendingIntent(android.app.PendingIntent) KeyguardManager(android.app.KeyguardManager) SuppressLint(android.annotation.SuppressLint)

Example 75 with KeyguardManager

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

the class PhoneProfilesService method onStartCommand.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    PPApplication.logE("PhoneProfilesService.onStartCommand", "xxx");
    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();
                            }
                        }
                    }
                }
            }
        }
    }
    // stopped, so return sticky.
    return START_STICKY;
}
Also used : Context(android.content.Context) PowerManager(android.os.PowerManager) KeyguardManager(android.app.KeyguardManager)

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