• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.car.carlauncher;
18 
19 import android.app.Application;
20 import android.car.Car;
21 import android.car.CarNotConnectedException;
22 import android.car.CarProjectionManager;
23 import android.car.CarProjectionManager.ProjectionStatusListener;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.graphics.drawable.Drawable;
27 import android.os.UserManager;
28 
29 import androidx.annotation.Nullable;
30 import androidx.annotation.VisibleForTesting;
31 import androidx.lifecycle.AndroidViewModel;
32 import androidx.lifecycle.LiveData;
33 import androidx.lifecycle.MediatorLiveData;
34 import androidx.lifecycle.MutableLiveData;
35 import androidx.lifecycle.Observer;
36 import androidx.lifecycle.Transformations;
37 import androidx.lifecycle.ViewModel;
38 
39 import java.util.Arrays;
40 import java.util.Collections;
41 import java.util.List;
42 import java.util.Objects;
43 
44 /**
45  * Implementation {@link ViewModel} for {@link ContextualFragment}.
46  *
47  * Returns the first non-null {@link ContextualInfo} from a set of delegates.
48  */
49 public class ContextualViewModel extends AndroidViewModel {
50     private final MediatorLiveData<ContextualInfo> mContextualInfo = new MediatorLiveData<>();
51 
52     private final List<LiveData<ContextualInfo>> mInfoDelegates;
53 
54     private Car mCar;
55 
ContextualViewModel(Application application)56     public ContextualViewModel(Application application) {
57         this(application, null);
58     }
59 
60     @VisibleForTesting
ContextualViewModel(Application application, CarProjectionManager carProjectionManager)61     ContextualViewModel(Application application, CarProjectionManager carProjectionManager) {
62         super(application);
63 
64         if (carProjectionManager == null) {
65             mCar = Car.createCar(application);
66             carProjectionManager =
67                     (CarProjectionManager) mCar.getCarManager(Car.PROJECTION_SERVICE);
68         }
69 
70         mInfoDelegates =
71                 Collections.unmodifiableList(Arrays.asList(
72                         new ProjectionContextualInfoLiveData(application, carProjectionManager),
73                         new WeatherContextualInfoLiveData(application)
74                 ));
75 
76         Observer<Object> observer = x -> updateLiveData();
77         for (LiveData<ContextualInfo> delegate : mInfoDelegates) {
78             mContextualInfo.addSource(delegate, observer);
79         }
80     }
81 
82     @Override
onCleared()83     protected void onCleared() {
84         if (mCar != null && mCar.isConnected()) {
85             mCar.disconnect();
86             mCar = null;
87         }
88         super.onCleared();
89     }
90 
updateLiveData()91     private void updateLiveData() {
92         for (LiveData<ContextualInfo> delegate : mInfoDelegates) {
93             ContextualInfo value = delegate.getValue();
94             if (value != null) {
95                 mContextualInfo.setValue(value);
96                 return;
97             }
98         }
99 
100         mContextualInfo.setValue(null);
101     }
102 
getContextualInfo()103     public LiveData<ContextualInfo> getContextualInfo() {
104         return mContextualInfo;
105     }
106 }
107