move device locked check to common place

This commit is contained in:
Helium314 2025-06-01 13:13:00 +02:00
parent 12c1cb0cd2
commit 180bd179c5
3 changed files with 20 additions and 23 deletions

View file

@ -0,0 +1,13 @@
package helium314.keyboard.compat
import android.app.KeyguardManager
import android.content.Context
import android.os.Build
fun isDeviceLocked(context: Context): Boolean {
val keyguardManager = context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
keyguardManager.isDeviceLocked
else
keyguardManager.isKeyguardLocked
}

View file

@ -6,12 +6,11 @@
package helium314.keyboard.keyboard; package helium314.keyboard.keyboard;
import android.app.KeyguardManager;
import android.content.Context; import android.content.Context;
import android.os.Build;
import android.text.InputType; import android.text.InputType;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
import helium314.keyboard.compat.IsLockedCompatKt;
import helium314.keyboard.keyboard.internal.KeyboardBuilder; import helium314.keyboard.keyboard.internal.KeyboardBuilder;
import helium314.keyboard.keyboard.internal.KeyboardIconsSet; import helium314.keyboard.keyboard.internal.KeyboardIconsSet;
import helium314.keyboard.keyboard.internal.KeyboardParams; import helium314.keyboard.keyboard.internal.KeyboardParams;
@ -207,14 +206,8 @@ public final class KeyboardLayoutSet {
params.mEditorInfo = editorInfo; params.mEditorInfo = editorInfo;
params.mIsPasswordField = InputTypeUtils.isPasswordInputType(editorInfo.inputType); params.mIsPasswordField = InputTypeUtils.isPasswordInputType(editorInfo.inputType);
// When the device is still locked, features like showing the IME setting app need to // When the device is still locked, features like showing the IME setting app need to be locked down.
// be locked down. params.mDeviceLocked = IsLockedCompatKt.isDeviceLocked(context);
final KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
params.mDeviceLocked = km.isDeviceLocked();
} else {
params.mDeviceLocked = km.isKeyguardLocked();
}
} }
public static KeyboardLayoutSet buildEmojiClipBottomRow(final Context context, @Nullable final EditorInfo ei) { public static KeyboardLayoutSet buildEmojiClipBottomRow(final Context context, @Nullable final EditorInfo ei) {
@ -252,7 +245,7 @@ public final class KeyboardLayoutSet {
mParams.mVoiceInputKeyEnabled = enabled; mParams.mVoiceInputKeyEnabled = enabled;
return this; return this;
} }
public Builder setNumberRowEnabled(final boolean enabled) { public Builder setNumberRowEnabled(final boolean enabled) {
mParams.mNumberRowEnabled = enabled; mParams.mNumberRowEnabled = enabled;
return this; return this;

View file

@ -6,14 +6,12 @@
package helium314.keyboard.latin.suggestions package helium314.keyboard.latin.suggestions
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.KeyguardManager
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.SharedPreferences.OnSharedPreferenceChangeListener import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.graphics.Color import android.graphics.Color
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.text.TextUtils import android.text.TextUtils
import android.util.AttributeSet import android.util.AttributeSet
import android.util.TypedValue import android.util.TypedValue
@ -31,6 +29,7 @@ import android.widget.RelativeLayout
import android.widget.TextView import android.widget.TextView
import androidx.core.view.isVisible import androidx.core.view.isVisible
import helium314.keyboard.accessibility.AccessibilityUtils import helium314.keyboard.accessibility.AccessibilityUtils
import helium314.keyboard.compat.isDeviceLocked
import helium314.keyboard.keyboard.KeyboardSwitcher import helium314.keyboard.keyboard.KeyboardSwitcher
import helium314.keyboard.keyboard.MainKeyboardView import helium314.keyboard.keyboard.MainKeyboardView
import helium314.keyboard.keyboard.PopupKeysPanel import helium314.keyboard.keyboard.PopupKeysPanel
@ -249,11 +248,7 @@ class SuggestionStripView(context: Context, attrs: AttributeSet?, defStyle: Int)
fun setToolbarVisibility(toolbarVisible: Boolean) { fun setToolbarVisibility(toolbarVisible: Boolean) {
// avoid showing toolbar keys when locked // avoid showing toolbar keys when locked
val km = context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager val locked = isDeviceLocked(context)
val locked = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) // todo: move this check to compat (also other uses)
km.isDeviceLocked
else
km.isKeyguardLocked
pinnedKeys.isVisible = !locked && !toolbarVisible pinnedKeys.isVisible = !locked && !toolbarVisible
suggestionsStrip.isVisible = locked || !toolbarVisible suggestionsStrip.isVisible = locked || !toolbarVisible
toolbarContainer.isVisible = !locked && toolbarVisible toolbarContainer.isVisible = !locked && toolbarVisible
@ -609,11 +604,7 @@ class SuggestionStripView(context: Context, attrs: AttributeSet?, defStyle: Int)
} }
// hide pinned keys if device is locked, and avoid expanding toolbar // hide pinned keys if device is locked, and avoid expanding toolbar
val km = context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager val hideToolbarKeys = isDeviceLocked(context)
val hideToolbarKeys = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
km.isDeviceLocked
else
km.isKeyguardLocked
toolbarExpandKey.setOnClickListener(if (hideToolbarKeys || !toolbarIsExpandable) null else this) toolbarExpandKey.setOnClickListener(if (hideToolbarKeys || !toolbarIsExpandable) null else this)
pinnedKeys.visibility = if (hideToolbarKeys) GONE else suggestionsStrip.visibility pinnedKeys.visibility = if (hideToolbarKeys) GONE else suggestionsStrip.visibility
isExternalSuggestionVisible = false isExternalSuggestionVisible = false