This repository has been archived by the owner on Sep 21, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(policy): check enable password helper
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
app/src/main/java/org/flyve/mdm/agent/utils/DeviceLockedHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.flyve.mdm.agent.utils; | ||
|
||
import android.annotation.TargetApi; | ||
import android.app.KeyguardManager; | ||
import android.content.ContentResolver; | ||
import android.content.Context; | ||
import android.os.Build; | ||
import android.provider.Settings; | ||
|
||
/* | ||
* Copyright © 2017 Teclib. All rights reserved. | ||
* | ||
* This file is part of flyve-mdm-android | ||
* | ||
* flyve-mdm-android is a subproject of Flyve MDM. Flyve MDM is a mobile | ||
* device management software. | ||
* | ||
* Flyve MDM is free software: you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; either version 3 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* Flyve MDM is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* ------------------------------------------------------------------------------ | ||
* @author Rafael Hernandez | ||
* @date 28/12/17 | ||
* @copyright Copyright © 2017 Teclib. All rights reserved. | ||
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html | ||
* @link https://github.com/flyve-mdm/flyve-mdm-android | ||
* @link https://flyve-mdm.com | ||
* ------------------------------------------------------------------------------ | ||
*/ | ||
public class DeviceLockedHelper { | ||
|
||
private Context context; | ||
|
||
public DeviceLockedHelper(Context context) { | ||
this.context = context; | ||
} | ||
|
||
public boolean isDeviceScreenLocked() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
return isDeviceLocked(); | ||
} else { | ||
return isPatternSet() || isPassOrPinSet(); | ||
} | ||
} | ||
|
||
/** | ||
* @return true if pattern set, false if not (or if an issue when checking) | ||
*/ | ||
private boolean isPatternSet() { | ||
ContentResolver cr = context.getContentResolver(); | ||
try { | ||
int lockPatternEnable = Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED); | ||
return lockPatternEnable == 1; | ||
} catch (Settings.SettingNotFoundException e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* @return true if pass or pin set | ||
*/ | ||
private boolean isPassOrPinSet() { | ||
try { | ||
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); //api 16+ | ||
return keyguardManager.isKeyguardSecure(); | ||
} catch (Exception ex) { | ||
FlyveLog.e(ex.getMessage()); | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* @return true if pass or pin or pattern loacks screen | ||
*/ | ||
@TargetApi(23) | ||
private boolean isDeviceLocked() { | ||
try { | ||
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); //api 23+ | ||
return keyguardManager.isDeviceSecure(); | ||
} catch (Exception ex) { | ||
FlyveLog.e(ex.getMessage()); | ||
return false; | ||
} | ||
} | ||
} |