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 17 package com.android.systemui.complication; 18 19 import androidx.lifecycle.LiveData; 20 21 import com.android.systemui.dreams.DreamOverlayStateController; 22 23 import java.util.Collection; 24 25 import javax.inject.Inject; 26 27 /** 28 * {@link ComplicationCollectionLiveData} wraps 29 * {@link DreamOverlayStateController#getComplications()} to provide an observable 30 * {@link Complication} data set tied to a lifecycle. This should not be directly accessed. Instead, 31 * clients should access the data from {@link ComplicationCollectionViewModel}. 32 */ 33 public class ComplicationCollectionLiveData extends LiveData<Collection<Complication>> { 34 final DreamOverlayStateController mDreamOverlayStateController; 35 36 final DreamOverlayStateController.Callback mStateControllerCallback; 37 38 { 39 mStateControllerCallback = new DreamOverlayStateController.Callback() { 40 @Override 41 public void onComplicationsChanged() { 42 setValue(mDreamOverlayStateController.getComplications()); 43 } 44 45 @Override 46 public void onAvailableComplicationTypesChanged() { 47 setValue(mDreamOverlayStateController.getComplications()); 48 } 49 }; 50 } 51 52 @Inject ComplicationCollectionLiveData(DreamOverlayStateController stateController)53 public ComplicationCollectionLiveData(DreamOverlayStateController stateController) { 54 super(); 55 mDreamOverlayStateController = stateController; 56 } 57 58 @Override onActive()59 protected void onActive() { 60 super.onActive(); 61 mDreamOverlayStateController.addCallback(mStateControllerCallback); 62 setValue(mDreamOverlayStateController.getComplications()); 63 } 64 65 @Override onInactive()66 protected void onInactive() { 67 mDreamOverlayStateController.removeCallback(mStateControllerCallback); 68 super.onInactive(); 69 } 70 } 71