1 /*
2  * Copyright (C) 2020 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.companiondevicesupport.feature.trust.ui;
18 
19 import android.annotation.NonNull;
20 
21 import androidx.lifecycle.MutableLiveData;
22 import androidx.lifecycle.ViewModel;
23 
24 import com.android.car.companiondevicesupport.api.external.AssociatedDevice;
25 import com.android.car.companiondevicesupport.api.internal.trust.TrustedDevice;
26 
27 import java.util.List;
28 
29 /**
30  * ViewModel that shares trusted device data between {@link TrustedDeviceActivity} and
31  * {@link TrustedDeviceDetailFragment}.
32  */
33 public class TrustedDeviceViewModel extends ViewModel {
34     private final MutableLiveData<List<TrustedDevice>> mTrustedDevices = new MutableLiveData<>();
35     private final MutableLiveData<AssociatedDevice> mAssociatedDevice = new MutableLiveData<>(null);
36     private final MutableLiveData<TrustedDevice> mDeviceToDisable =
37             new MutableLiveData<>(null);
38     private final MutableLiveData<AssociatedDevice> mDeviceToEnable =
39             new MutableLiveData<>(null);
40     private final MutableLiveData<TrustedDevice> mDeviceDisabled = new MutableLiveData<>(null);
41     private final MutableLiveData<TrustedDevice> mDeviceEnabled = new MutableLiveData<>(null);
42 
43     /**
44      * Set trusted devices.
45      *
46      * @param devices Trusted devices.
47      */
setTrustedDevices(@onNull List<TrustedDevice> devices)48     void setTrustedDevices(@NonNull List<TrustedDevice> devices) {
49         mTrustedDevices.postValue(devices);
50     }
51 
52     /**
53      * Set current associated device.
54      *
55      * @param device Associated device.
56      */
setAssociatedDevice(@onNull AssociatedDevice device)57     void setAssociatedDevice(@NonNull AssociatedDevice device) {
58         mAssociatedDevice.setValue(device);
59     }
60 
61     /**
62      * Set the trusted device to disable.
63      *
64      * @param device The trusted device to disable.
65      */
setDeviceToDisable(TrustedDevice device)66     void setDeviceToDisable(TrustedDevice device) {
67         mDeviceToDisable.setValue(device);
68     }
69 
70     /**
71      * Set the associated device to enroll.
72      *
73      * @param device The associated device to enroll.
74      */
setDeviceToEnable(AssociatedDevice device)75     void setDeviceToEnable(AssociatedDevice device) {
76         mDeviceToEnable.setValue(device);
77     }
78 
79     /** Set the disabled trusted device. */
setDisabledDevice(TrustedDevice device)80     void setDisabledDevice(TrustedDevice device) {
81         mDeviceDisabled.postValue(device);
82     }
83 
84     /** Set the enabled trusted device. */
setEnabledDevice(TrustedDevice device)85     void setEnabledDevice(TrustedDevice device) {
86         mDeviceEnabled.postValue(device);
87     }
88 
89     /** Get trusted device list. It will return an empty list if there's no trusted device. */
getTrustedDevices()90     MutableLiveData<List<TrustedDevice>> getTrustedDevices() {
91         return mTrustedDevices;
92     }
93 
94     /** Get current associated device. */
getAssociatedDevice()95     MutableLiveData<AssociatedDevice> getAssociatedDevice() {
96         return mAssociatedDevice;
97     }
98 
99     /** Get the trusted device to disable. */
getDeviceToDisable()100     MutableLiveData<TrustedDevice> getDeviceToDisable() {
101         return mDeviceToDisable;
102     }
103 
104     /** Get the associated device to enroll. */
getDeviceToEnable()105     MutableLiveData<AssociatedDevice> getDeviceToEnable() {
106         return mDeviceToEnable;
107     }
108 
109     /** Get the disabled trusted device. */
getDisabledDevice()110     MutableLiveData<TrustedDevice> getDisabledDevice() {
111         return mDeviceDisabled;
112     }
113 
114     /** Get the enabled trusted device. */
getEnabledDevice()115     MutableLiveData<TrustedDevice> getEnabledDevice() {
116         return mDeviceEnabled;
117     }
118 }
119