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 */
16package com.android.car.dialer.livedata;
17
18import android.arch.lifecycle.LiveData;
19import android.content.ContentResolver;
20import android.content.Context;
21import android.content.CursorLoader;
22import android.database.ContentObserver;
23import android.net.Uri;
24import android.os.Handler;
25
26import com.android.car.dialer.telecom.PhoneLoader;
27import com.android.car.dialer.ui.CallLogListingTask;
28
29import java.util.List;
30
31/**
32 * Live data which loads call history.
33 */
34public class CallHistoryLiveData extends LiveData<List<CallLogListingTask.CallLogItem>> {
35
36    private final Context mContext;
37    private final ContentResolver mContentResolver;
38    private CursorLoader mCursorLoader;
39    private CallLogContentObserver mCallLogContentObserver = new CallLogContentObserver(
40            new Handler());
41
42    public CallHistoryLiveData(Context context) {
43        this.mContext = context;
44        mContentResolver = context.getContentResolver();
45    }
46
47    @Override
48    protected void onActive() {
49        super.onActive();
50        mCursorLoader = PhoneLoader.registerCallObserver(getCallHistoryFilterType(),
51                mContext,
52                (loader, cursor) -> {
53                    CallLogListingTask task = new CallLogListingTask(mContext, cursor,
54                            this::setValue);
55                    task.execute();
56                });
57
58        mContentResolver.registerContentObserver(mCursorLoader.getUri(),
59                false, mCallLogContentObserver);
60    }
61
62    @Override
63    protected void onInactive() {
64        super.onInactive();
65        mContentResolver.unregisterContentObserver(mCallLogContentObserver);
66    }
67
68    protected int getCallHistoryFilterType() {
69        return PhoneLoader.CALL_TYPE_ALL;
70    }
71
72    /**
73     * A {@link ContentObserver} that is responsible for reloading the user's recent calls.
74     */
75    private class CallLogContentObserver extends ContentObserver {
76        public CallLogContentObserver(Handler handler) {
77            super(handler);
78        }
79
80        @Override
81        public void onChange(boolean selfChange) {
82            onChange(selfChange, null);
83        }
84
85        @Override
86        public void onChange(boolean selfChange, Uri uri) {
87            mCursorLoader.startLoading();
88        }
89    }
90}
91