1 /* 2 * Copyright (C) 2021 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 17 package com.android.server.sensorprivacy; 18 19 import static android.hardware.SensorPrivacyManager.StateTypes.DISABLED; 20 import static android.hardware.SensorPrivacyManager.StateTypes.ENABLED; 21 22 import static com.android.server.sensorprivacy.SensorPrivacyService.getCurrentTimeMillis; 23 24 class SensorState { 25 26 private int mStateType; 27 private long mLastChange; 28 SensorState(int stateType)29 SensorState(int stateType) { 30 mStateType = stateType; 31 mLastChange = getCurrentTimeMillis(); 32 } 33 SensorState(int stateType, long lastChange)34 SensorState(int stateType, long lastChange) { 35 mStateType = stateType; 36 mLastChange = Math.min(getCurrentTimeMillis(), lastChange); 37 } 38 SensorState(SensorState sensorState)39 SensorState(SensorState sensorState) { 40 mStateType = sensorState.getState(); 41 mLastChange = sensorState.getLastChange(); 42 } 43 setState(int stateType)44 boolean setState(int stateType) { 45 if (mStateType != stateType) { 46 mStateType = stateType; 47 mLastChange = getCurrentTimeMillis(); 48 return true; 49 } 50 return false; 51 } 52 getState()53 int getState() { 54 return mStateType; 55 } 56 getLastChange()57 long getLastChange() { 58 return mLastChange; 59 } 60 61 // Below are some convenience members for when dealing with enabled/disabled 62 enabledToState(boolean enabled)63 private static int enabledToState(boolean enabled) { 64 return enabled ? ENABLED : DISABLED; 65 } 66 SensorState(boolean enabled)67 SensorState(boolean enabled) { 68 this(enabledToState(enabled)); 69 } 70 setEnabled(boolean enabled)71 boolean setEnabled(boolean enabled) { 72 return setState(enabledToState(enabled)); 73 } 74 isEnabled()75 boolean isEnabled() { 76 return getState() == ENABLED; 77 } 78 79 } 80