Java Code Examples for android.app.KeyguardManager#inKeyguardRestrictedInputMode()

The following examples show how to use android.app.KeyguardManager#inKeyguardRestrictedInputMode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OverlayServiceCommon.java    From heads-up with GNU General Public License v3.0 7 votes vote down vote up
private boolean isLocked() {
    if (preferences.getBoolean("off_as_locked", false)) {
        initPowerManager();
        if (!powerManager.isScreenOn()) {
            isLocked = true;
            return isLocked;
        }
    }

    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
    final boolean isKeyguardLocked;
    if (Build.VERSION.SDK_INT >= 16)
         isKeyguardLocked = keyguardManager.isKeyguardLocked();
    else isKeyguardLocked = keyguardManager.inKeyguardRestrictedInputMode();

    Mlog.v(logTag, isKeyguardLocked + " " + LOCKSCREEN_APPS.contains(currentPackage));
    isLocked = isKeyguardLocked || (currentPackage != null && LOCKSCREEN_APPS.contains(currentPackage));
    return isLocked;
}
 
Example 2
Source File: ShadowsocksRunnerActivity.java    From Maying with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    boolean locked = km.inKeyguardRestrictedInputMode();
    if (locked) {
        IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT);
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) {
                    mServiceBoundContext.attachService();
                }
            }
        };
        registerReceiver(receiver, filter);
    } else {
        mServiceBoundContext.attachService();
    }
    finish();
}
 
Example 3
Source File: MainActivity.java    From linphone-android with GNU General Public License v3.0 6 votes vote down vote up
private void requestPermissionsIfNotGranted(String[] perms, int resultCode) {
    ArrayList<String> permissionsToAskFor = new ArrayList<>();
    if (perms != null) { // This is created (or not) by the child activity
        for (String permissionToHave : perms) {
            if (!checkPermission(permissionToHave)) {
                permissionsToAskFor.add(permissionToHave);
            }
        }
    }

    if (permissionsToAskFor.size() > 0) {
        for (String permission : permissionsToAskFor) {
            Log.i("[Permission] Requesting " + permission + " permission");
        }
        String[] permissions = new String[permissionsToAskFor.size()];
        permissions = permissionsToAskFor.toArray(permissions);

        KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        boolean locked = km.inKeyguardRestrictedInputMode();
        if (!locked) {
            // This is to workaround an infinite loop of pause/start in Activity issue
            // if incoming call ends while screen if off and locked
            ActivityCompat.requestPermissions(this, permissions, resultCode);
        }
    }
}
 
Example 4
Source File: VideoActivity.java    From BambooPlayer with Apache License 2.0 6 votes vote down vote up
@Override
public void onResume() {
	super.onResume();
	if (!mCreated)
		return;
	if (isInitialized()) {
		KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
		if (!keyguardManager.inKeyguardRestrictedInputMode()) {
			startPlayer();
		}
	} else {
		if (mCloseComplete) {
			reOpen();
		}
	}
}
 
Example 5
Source File: PopupNotificationActivity.java    From Yahala-Messenger with MIT License 6 votes vote down vote up
private void handleIntent(Intent intent) {
    KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
    if (km.inKeyguardRestrictedInputMode() || !ApplicationLoader.isScreenOn) {
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_DIM_BEHIND |
                        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    } else {
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    if (currentMessageObject == null) {
        currentMessageNum = 0;
    }
    getNewMessage();
}
 
Example 6
Source File: PopupNotificationActivity.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private void handleIntent(Intent intent) {
    isReply = intent != null && intent.getBooleanExtra("force", false);
    popupMessages.clear();
    if (isReply) {
        int account = intent != null ? intent.getIntExtra("currentAccount", UserConfig.selectedAccount) : UserConfig.selectedAccount;
        popupMessages.addAll(NotificationsController.getInstance(account).popupReplyMessages);
    } else {
        for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
            if (UserConfig.getInstance(a).isClientActivated()) {
                popupMessages.addAll(NotificationsController.getInstance(a).popupMessages);
            }
        }
    }
    KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
    if (km.inKeyguardRestrictedInputMode() || !ApplicationLoader.isScreenOn) {
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_DIM_BEHIND |
                        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    } else {
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    if (currentMessageObject == null) {
        currentMessageNum = 0;
    }
    getNewMessage();
}
 
Example 7
Source File: ProtifyActivity.java    From sbt-android-protify with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle state) {
    super.onCreate(state);
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    boolean isOn = Build.VERSION.SDK_INT < 7 || pm.isScreenOn();
    boolean isKG = km.inKeyguardRestrictedInputMode();
    if (!isOn || isKG) {
        finish();
        return;
    }
    TextView tv = new TextView(this);
    tv.setText("Protifying code...");
    float d = getResources().getDisplayMetrics().density;
    tv.setPadding(asDp(8, d), asDp(8, d), asDp(8, d), asDp(8, d));
    setContentView(tv);
    if (Build.VERSION.SDK_INT >= 11 &&
            (state == null || !state.getBoolean(STATE_SAVED, false))) {
        recreate();
    } else {
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        });
    }
}
 
Example 8
Source File: MediaButtonReceiver.java    From media-button-router with Apache License 2.0 5 votes vote down vote up
/**
 * Shows the selector dialog that allows the user to decide which music
 * player should receiver the media button press intent.
 * 
 * @param context
 *            The context.
 * @param intent
 *            The intent to forward.
 * @param keyEvent
 *            The key event
 */
private void showSelector(Context context, Intent intent, KeyEvent keyEvent) {
    KeyguardManager manager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    boolean locked = manager.inKeyguardRestrictedInputMode();

    Intent showForwardView = new Intent(Constants.INTENT_ACTION_VIEW_MEDIA_BUTTON_LIST);
    showForwardView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    showForwardView.putExtras(intent);
    showForwardView.setClassName(context,
            locked ? ReceiverSelectorLocked.class.getName() : ReceiverSelector.class.getName());

    /* COMMENTED OUT FOR MARKET RELEASE Log.i(TAG, "Media Button Receiver: starting selector activity for keyevent: " + keyEvent); */

    if (locked) {

        // XXX See if this actually makes a difference, might
        // not be needed if we move more things to onCreate?
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        // acquire temp wake lock
        WakeLock wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG);
        wakeLock.setReferenceCounted(false);

        // Our app better display within 3 seconds or we have
        // bigger issues.
        wakeLock.acquire(3000);
    }
    context.startActivity(showForwardView);
}
 
Example 9
Source File: ScreenUtils.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * Return whether screen is locked.
 *
 * @return {@code true}: yes<br>{@code false}: no
 */
public static boolean isScreenLock() {
    KeyguardManager km =
            (KeyguardManager) Utils.getApp().getSystemService(Context.KEYGUARD_SERVICE);
    if (km == null) return false;
    return km.inKeyguardRestrictedInputMode();
}
 
Example 10
Source File: PopupNotificationActivity.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private void handleIntent(Intent intent) {
    isReply = intent != null && intent.getBooleanExtra("force", false);
    popupMessages.clear();
    if (isReply) {
        int account = intent != null ? intent.getIntExtra("currentAccount", UserConfig.selectedAccount) : UserConfig.selectedAccount;
        popupMessages.addAll(NotificationsController.getInstance(account).popupReplyMessages);
    } else {
        for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
            if (UserConfig.getInstance(a).isClientActivated()) {
                popupMessages.addAll(NotificationsController.getInstance(a).popupMessages);
            }
        }
    }
    KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
    if (km.inKeyguardRestrictedInputMode() || !ApplicationLoader.isScreenOn) {
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_DIM_BEHIND |
                        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    } else {
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    if (currentMessageObject == null) {
        currentMessageNum = 0;
    }
    getNewMessage();
}
 
Example 11
Source File: ScreenUtils.java    From Common with Apache License 2.0 5 votes vote down vote up
/**
 * Return whether screen is locked.
 *
 * @return {@code true}: yes<br>{@code false}: no
 */
public static boolean isScreenLock(@NonNull final Context context) {
    KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    if (km == null) {
        return false;
    }
    return km.inKeyguardRestrictedInputMode();
}
 
Example 12
Source File: ScreenUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 判断是否锁屏
 * @return {@code true} yes, {@code false} no
 */
public static boolean isScreenLock() {
    try {
        KeyguardManager keyguardManager = AppUtils.getKeyguardManager();
        return keyguardManager != null && keyguardManager.inKeyguardRestrictedInputMode();
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "isScreenLock");
    }
    return false;
}
 
Example 13
Source File: MainActivity.java    From linphone-android with GNU General Public License v3.0 5 votes vote down vote up
public void requestPermissionIfNotGranted(String permission) {
    if (!checkPermission(permission)) {
        Log.i("[Permission] Requesting " + permission + " permission");

        String[] permissions = new String[] {permission};
        KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        boolean locked = km.inKeyguardRestrictedInputMode();
        if (!locked) {
            // This is to workaround an infinite loop of pause/start in Activity issue
            // if incoming call ends while screen if off and locked
            ActivityCompat.requestPermissions(this, permissions, FRAGMENT_SPECIFIC_PERMISSION);
        }
    }
}
 
Example 14
Source File: AppCompatDlalog.java    From stynico with MIT License 4 votes vote down vote up
/**
 * 系统是否在锁屏状态
 *
 * @return
 */
private boolean isScreenLocked()
{
    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    return keyguardManager.inKeyguardRestrictedInputMode();
}
 
Example 15
Source File: ScreenReceiver.java    From always-on-amoled with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onReceive(final Context context, Intent intent) {
    prefs = new Prefs(context);
    prefs.apply();
    this.context = context;
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        MainService.isScreenOn = false;
        Globals.sensorIsScreenOff = true;
        Utils.logInfo(TAG, "Screen turned off\nShown:" + Globals.isShown);
        if (Globals.isShown && !MainService.isScreenOn) {
            // Screen turned off with service running, wake up device
            turnScreenOn(context, true);
        } else {
            //Checking if was killed by delay or naturally, if so, don't restart the service
            if (Globals.killedByDelay) {
                Globals.killedByDelay = false;
                Utils.logDebug(SCREEN_RECEIVER_LOG_TAG, "Killed by delay and won't restart");
                return;
            }
            // Start service when screen is off
            if (!Globals.inCall && prefs.enabled) {
                boolean toStart = shouldStart();
                Utils.logDebug("SHOULD START ", String.valueOf(toStart));
                if (toStart) {
                    if (prefs.startAfterLock) {
                        final KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
                        if (myKM.inKeyguardRestrictedInputMode()) {
                            //Screen is locked, start the service
                            Utils.logDebug(SCREEN_RECEIVER_LOG_TAG, "Device is locked");
                            context.startService(new Intent(context, MainService.class));
                            Globals.isShown = true;
                        } else {
                            //Screen is unlocked, wait until the lock timeout is over before starting the service.
                            int startDelay;
                            if (!doesDeviceHaveSecuritySetup(context)) {
                                Globals.noLock = true;
                                context.startService(new Intent(context, MainService.class));
                                Globals.isShown = true;
                                Utils.logDebug(SCREEN_RECEIVER_LOG_TAG, "Device is unlocked");
                            } else {
                                Utils.logDebug(SCREEN_RECEIVER_LOG_TAG, "Device is locked but has a timeout");
                                try {
                                    startDelay = Settings.Secure.getInt(context.getContentResolver(), "lock_screen_lock_after_timeout", 5000);
                                    if (startDelay == -1)
                                        startDelay = (int) Settings.Secure.getLong(context.getContentResolver(), "lock_screen_lock_after_timeout", 5000);
                                    Utils.logDebug(SCREEN_RECEIVER_LOG_TAG, "Lock time out " + String.valueOf(startDelay));
                                } catch (Exception settingNotFound) {
                                    startDelay = 0;
                                }
                                if (startDelay > 0) {
                                    new Handler().postDelayed(() -> {
                                        if (myKM.inKeyguardRestrictedInputMode()) {
                                            context.startService(new Intent(context, MainService.class));
                                            Globals.isShown = true;
                                        }
                                    }, startDelay);
                                } else {
                                    context.startService(new Intent(context, MainService.class));
                                    Globals.isShown = true;
                                }
                            }
                        }
                    } else {
                        context.startService(new Intent(context, MainService.class));
                        Globals.isShown = true;
                    }
                }
            }
        }
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        MainService.isScreenOn = true;
        if (Globals.waitingForApp)
            Globals.waitingForApp = false;
        Utils.logInfo(TAG, "Screen turned on\nShown:" + Globals.isShown);
    }
}
 
Example 16
Source File: AppUtils.java    From WaterMonitor with Apache License 2.0 4 votes vote down vote up
public static boolean isInLockScreen() {
    KeyguardManager keyguardManager = (KeyguardManager) AppApplication.getContext().getSystemService(KEYGUARD_SERVICE);
    return keyguardManager.inKeyguardRestrictedInputMode();
}
 
Example 17
Source File: RxDeviceTool.java    From RxTools-master with Apache License 2.0 2 votes vote down vote up
/**
 * 判断是否锁屏
 *
 * @param context 上下文
 * @return {@code true}: 是<br>{@code false}: 否
 */
public static boolean isScreenLock(Context context) {
    KeyguardManager km = (KeyguardManager) context
            .getSystemService(Context.KEYGUARD_SERVICE);
    return km.inKeyguardRestrictedInputMode();
}
 
Example 18
Source File: ScreenUtil.java    From TitleLayout with Apache License 2.0 2 votes vote down vote up
/**
 * 判断是否锁屏
 *
 * @return {@code true}: 是<br>{@code false}: 否
 */
public static boolean isScreenLock(Context context) {
    KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    return km.inKeyguardRestrictedInputMode();
}
 
Example 19
Source File: DeviceUtils.java    From BigApp_Discuz_Android with Apache License 2.0 2 votes vote down vote up
/**
 * 是否解锁
 *
 * @param context
 * @return
 */
public static boolean isKeyguard(Context context) {
    KeyguardManager mKeyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    boolean flag = mKeyguardManager.inKeyguardRestrictedInputMode();
    return flag;
}
 
Example 20
Source File: AppHelper.java    From Utils with Apache License 2.0 2 votes vote down vote up
/**
 * telephone is sleeping
 *
 * @param context
 * @return
 */
public static boolean isSleeping(Context context) {
    KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    return keyguardManager.inKeyguardRestrictedInputMode();
}