1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.launcher3.logging; 17 18 import static com.android.launcher3.logging.KeyboardStateManager.KeyboardState.NO_IME_ACTION; 19 20 import android.os.SystemClock; 21 22 /** 23 * Class to maintain keyboard states. 24 */ 25 public class KeyboardStateManager { 26 private long mUpdatedTime; 27 private int mImeHeightPx; 28 // Height of the keyboard when it's shown. 29 // mImeShownHeightPx>=mImeHeightPx always. 30 private int mImeShownHeightPx; 31 32 public enum KeyboardState { 33 NO_IME_ACTION, 34 SHOW, 35 HIDE, 36 } 37 38 private KeyboardState mKeyboardState; 39 KeyboardStateManager(int defaultImeShownHeightPx)40 public KeyboardStateManager(int defaultImeShownHeightPx) { 41 mKeyboardState = NO_IME_ACTION; 42 mImeShownHeightPx = defaultImeShownHeightPx; 43 } 44 45 /** 46 * Returns time when keyboard state was updated. 47 */ getLastUpdatedTime()48 public long getLastUpdatedTime() { 49 return mUpdatedTime; 50 } 51 52 /** 53 * Returns current keyboard state. 54 */ getKeyboardState()55 public KeyboardState getKeyboardState() { 56 return mKeyboardState; 57 } 58 59 /** 60 * Setter method to set keyboard state. 61 */ setKeyboardState(KeyboardState keyboardState)62 public void setKeyboardState(KeyboardState keyboardState) { 63 mUpdatedTime = SystemClock.elapsedRealtime(); 64 mKeyboardState = keyboardState; 65 } 66 67 /** 68 * Returns keyboard's current height. 69 */ getImeHeight()70 public int getImeHeight() { 71 return mImeHeightPx; 72 } 73 74 /** 75 * Returns keyboard's height in pixels when shown. 76 */ getImeShownHeight()77 public int getImeShownHeight() { 78 return mImeShownHeightPx; 79 } 80 81 /** 82 * Setter method to set keyboard height in pixels. 83 */ setImeHeight(int imeHeightPx)84 public void setImeHeight(int imeHeightPx) { 85 mImeHeightPx = imeHeightPx; 86 if (mImeHeightPx > 0) { 87 // Update the mImeShownHeightPx with the actual ime height when shown and store it 88 // for future sessions. 89 mImeShownHeightPx = mImeHeightPx; 90 } 91 } 92 } 93