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.dialer.ui.calllog;
18 
19 import android.app.Application;
20 import android.text.format.DateUtils;
21 
22 import androidx.annotation.NonNull;
23 import androidx.lifecycle.AndroidViewModel;
24 import androidx.lifecycle.LiveData;
25 
26 import com.android.car.arch.common.FutureData;
27 import com.android.car.arch.common.LiveDataFunctions;
28 import com.android.car.dialer.livedata.CallHistoryLiveData;
29 import com.android.car.dialer.livedata.HeartBeatLiveData;
30 import com.android.car.dialer.ui.common.UiCallLogLiveData;
31 import com.android.car.telephony.common.InMemoryPhoneBook;
32 
33 import java.util.List;
34 
35 /** View model for CallHistoryFragment which provides call history live data. */
36 public class CallHistoryViewModel extends AndroidViewModel {
37     private UiCallLogLiveData mUiCallLogLiveData;
38     private LiveData<FutureData<List<Object>>> mUiCallLogFutureData;
39 
CallHistoryViewModel(@onNull Application application)40     public CallHistoryViewModel(@NonNull Application application) {
41         super(application);
42         mUiCallLogLiveData = new UiCallLogLiveData(application.getApplicationContext(),
43                 new HeartBeatLiveData(DateUtils.MINUTE_IN_MILLIS),
44                 CallHistoryLiveData.newInstance(application.getApplicationContext()),
45                 InMemoryPhoneBook.get().getContactsLiveData());
46 
47         mUiCallLogFutureData = LiveDataFunctions.loadingSwitchMap(mUiCallLogLiveData,
48                 input -> LiveDataFunctions.dataOf(input));
49     }
50 
51     /** Returns the {@link LiveData} for call history list {@link FutureData}. */
getCallHistory()52     public LiveData<FutureData<List<Object>>> getCallHistory() {
53         return mUiCallLogFutureData;
54     }
55 }
56