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 package com.android.car.dialer.ui.calllog;
17 
18 import android.os.Bundle;
19 import android.view.View;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.Nullable;
23 import androidx.fragment.app.Fragment;
24 import androidx.lifecycle.ViewModelProviders;
25 
26 import com.android.car.dialer.Constants;
27 import com.android.car.dialer.R;
28 import com.android.car.dialer.ui.common.DialerListBaseFragment;
29 import com.android.car.dialer.ui.contact.ContactDetailsFragment;
30 import com.android.car.telephony.common.Contact;
31 
32 /** Fragment for call history page. */
33 public class CallHistoryFragment extends DialerListBaseFragment implements
34         CallLogAdapter.OnShowContactDetailListener {
35     private static final String CONTACT_DETAIL_FRAGMENT_TAG = "CONTACT_DETAIL_FRAGMENT_TAG";
36 
37     private CallLogAdapter mCallLogAdapter;
38 
newInstance()39     public static CallHistoryFragment newInstance() {
40         return new CallHistoryFragment();
41     }
42 
43     @Override
onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)44     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
45         super.onViewCreated(view, savedInstanceState);
46         // Don't recreate the adapter if we already have one, so that the list items
47         // will display immediately upon the view being recreated. If they're not displayed
48         // immediately, we won't remember our scroll position.
49         if (mCallLogAdapter == null) {
50             mCallLogAdapter = new CallLogAdapter(
51                     getContext(), /* onShowContactDetailListener= */this);
52         }
53         getRecyclerView().setAdapter(mCallLogAdapter);
54 
55         CallHistoryViewModel viewModel = ViewModelProviders.of(this).get(
56                 CallHistoryViewModel.class);
57 
58         viewModel.getCallHistory().observe(this, uiCallLogs -> {
59             if (uiCallLogs.isLoading()) {
60                 showLoading();
61             } else if (uiCallLogs.getData().isEmpty()) {
62                 showEmpty(Constants.INVALID_RES_ID, R.string.call_logs_empty,
63                         R.string.available_after_sync);
64             } else {
65                 mCallLogAdapter.setUiCallLogs(uiCallLogs.getData());
66                 showContent();
67             }
68         });
69     }
70 
71     @Override
onShowContactDetail(Contact contact)72     public void onShowContactDetail(Contact contact) {
73         Fragment contactDetailsFragment = ContactDetailsFragment.newInstance(contact);
74         pushContentFragment(contactDetailsFragment, CONTACT_DETAIL_FRAGMENT_TAG);
75     }
76 }
77