1 /* 2 * Copyright (C) 2023 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.car.portraitlauncher.common; 18 19 import static android.car.user.CarUserManager.USER_LIFECYCLE_EVENT_TYPE_SWITCHING; 20 import static android.car.user.CarUserManager.USER_LIFECYCLE_EVENT_TYPE_UNLOCKED; 21 22 import android.car.Car; 23 import android.car.user.CarUserManager; 24 import android.car.user.UserLifecycleEventFilter; 25 import android.content.Context; 26 import android.util.Log; 27 28 import androidx.annotation.NonNull; 29 30 /** 31 * Centralized receiver to listen and trigger callbacks for certain user events. Currently listens 32 * for: 33 * - User switching events 34 * - User unlocked events 35 */ 36 public final class UserEventReceiver { 37 public static final String TAG = UserEventReceiver.class.getSimpleName(); 38 private int mUserId; 39 private Car mCar; 40 private CarUserManager mCarUserManager; 41 private Callback mCallback; 42 private CarUserManager.UserLifecycleListener mUserListener = 43 new CarUserManager.UserLifecycleListener() { 44 @Override 45 public void onEvent(@NonNull CarUserManager.UserLifecycleEvent event) { 46 if (mCallback == null) return; 47 if (event.getEventType() == USER_LIFECYCLE_EVENT_TYPE_SWITCHING 48 && event.getPreviousUserId() == mUserId) { 49 mCallback.onUserSwitching(); 50 } else if (event.getEventType() == USER_LIFECYCLE_EVENT_TYPE_UNLOCKED 51 && event.getUserId() == mUserId) { 52 mCallback.onUserUnlock(); 53 } 54 } 55 }; 56 57 /** Registers an observer to receive user switch events. */ register(Context context, Callback callback)58 public void register(Context context, Callback callback) { 59 if (mCar != null) { 60 Log.w(TAG, "already registered"); 61 return; 62 } 63 mUserId = context.getUserId(); 64 mCar = Car.createCar(context); 65 if (mCar == null) { 66 throw new IllegalStateException("Unable to connect to car service"); 67 } 68 mCarUserManager = mCar.getCarManager(CarUserManager.class); 69 mCallback = callback; 70 UserLifecycleEventFilter filter = 71 new UserLifecycleEventFilter.Builder() 72 .addEventType(USER_LIFECYCLE_EVENT_TYPE_UNLOCKED) 73 .addEventType(USER_LIFECYCLE_EVENT_TYPE_SWITCHING) 74 .build(); 75 mCarUserManager.addListener(context.getMainExecutor(), filter, mUserListener); 76 } 77 78 /** Unregisters this observer. */ unregister()79 public void unregister() { 80 if (mCar == null || mCarUserManager == null) { 81 return; 82 } 83 mCarUserManager.removeListener(mUserListener); 84 mCar.disconnect(); 85 mCarUserManager = null; 86 mCar = null; 87 mCallback = null; 88 } 89 90 /** Callback interface for {@link UserEventReceiver}. */ 91 public interface Callback { 92 93 /** Callback triggered when user switch is received. */ onUserSwitching()94 void onUserSwitching(); 95 96 /** Callback triggered when user unlock is received. */ onUserUnlock()97 void onUserUnlock(); 98 } 99 } 100