jovanak | edba98c | 2018-09-14 15:46:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.systemui.statusbar.car; |
| 18 | |
| 19 | import android.car.userlib.CarUserManagerHelper; |
| 20 | import android.content.Context; |
| 21 | import android.os.CountDownTimer; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | import com.android.systemui.R; |
| 25 | |
| 26 | import androidx.annotation.GuardedBy; |
| 27 | |
| 28 | /** |
| 29 | * Wrapper for a countdown timer that switches to Guest if the user has been driving with |
| 30 | * the keyguard up for configurable number of seconds. |
| 31 | */ |
| 32 | public class SwitchToGuestTimer { |
| 33 | private static final String TAG = "SwitchToGuestTimer"; |
| 34 | |
| 35 | // After how many ms CountdownTimer.onTick gets triggered. |
| 36 | private static final int COUNTDOWN_INTERVAL_MS = 1000; |
| 37 | |
| 38 | private final CarUserManagerHelper mCarUserManagerHelper; |
| 39 | private final Object mTimerLock; |
| 40 | private final String mGuestName; |
| 41 | private final int mTimeoutMs; |
| 42 | private final boolean mEnabled; |
| 43 | |
| 44 | @GuardedBy("mTimerLock") |
| 45 | private CountDownTimer mSwitchToGuestTimer; |
| 46 | |
| 47 | public SwitchToGuestTimer(Context context) { |
| 48 | mCarUserManagerHelper = new CarUserManagerHelper(context); |
| 49 | mGuestName = context.getResources().getString(R.string.car_guest); |
| 50 | mTimeoutMs = context.getResources().getInteger(R.integer.driving_on_keyguard_timeout_ms); |
| 51 | |
| 52 | // Lock prevents multiple timers being started. |
| 53 | mTimerLock = new Object(); |
| 54 | |
| 55 | // If milliseconds to switch is a negative number, the feature is disabled. |
| 56 | mEnabled = mTimeoutMs >= 0; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Starts the timer if it's not already running. |
| 61 | */ |
| 62 | public void start() { |
| 63 | if (!mEnabled) { |
| 64 | logD("Switching to guest after driving on keyguard is disabled."); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | synchronized (mTimerLock) { |
| 69 | if (mSwitchToGuestTimer != null) { |
| 70 | logD("Timer is already running."); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | mSwitchToGuestTimer = new CountDownTimer(mTimeoutMs, COUNTDOWN_INTERVAL_MS) { |
| 75 | @Override |
| 76 | public void onTick(long msUntilFinished) { |
| 77 | logD("Ms until switching to guest: " + Long.toString(msUntilFinished)); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void onFinish() { |
jovanak | 2469ca7 | 2018-09-19 16:30:04 -0700 | [diff] [blame] | 82 | mCarUserManagerHelper.startGuestSession(mGuestName); |
jovanak | edba98c | 2018-09-14 15:46:24 -0700 | [diff] [blame] | 83 | cancel(); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | logI("Starting timer"); |
| 88 | mSwitchToGuestTimer.start(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Cancels the running timer. |
| 94 | */ |
| 95 | public void cancel() { |
| 96 | synchronized (mTimerLock) { |
| 97 | if (mSwitchToGuestTimer != null) { |
| 98 | logI("Cancelling timer"); |
| 99 | mSwitchToGuestTimer.cancel(); |
| 100 | mSwitchToGuestTimer = null; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private void logD(String message) { |
| 106 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 107 | Log.d(TAG, message); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private void logI(String message) { |
| 112 | if (Log.isLoggable(TAG, Log.INFO)) { |
| 113 | Log.i(TAG, message); |
| 114 | } |
| 115 | } |
| 116 | } |