1 /* 2 * Copyright (C) 2019 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.settings; 18 19 import android.content.Context; 20 21 import androidx.lifecycle.LiveData; 22 import androidx.lifecycle.MutableLiveData; 23 24 /** 25 * A class that has an observable for the current user. 26 */ 27 public class CurrentUserObservable { 28 29 private final CurrentUserTracker mTracker; 30 31 private final MutableLiveData<Integer> mCurrentUser = new MutableLiveData<Integer>() { 32 @Override 33 protected void onActive() { 34 super.onActive(); 35 mTracker.startTracking(); 36 } 37 38 @Override 39 protected void onInactive() { 40 super.onInactive(); 41 mTracker.startTracking(); 42 } 43 }; 44 CurrentUserObservable(Context context)45 public CurrentUserObservable(Context context) { 46 mTracker = new CurrentUserTracker(context) { 47 @Override 48 public void onUserSwitched(int newUserId) { 49 mCurrentUser.setValue(newUserId); 50 } 51 }; 52 } 53 54 /** 55 * Returns the current user that can be observed. 56 */ getCurrentUser()57 public LiveData<Integer> getCurrentUser() { 58 if (mCurrentUser.getValue() == null) { 59 mCurrentUser.setValue(mTracker.getCurrentUserId()); 60 } 61 return mCurrentUser; 62 } 63 } 64