1 /* 2 * Copyright (C) 2017 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.dialer.calllog.datasources; 18 19 import android.content.ContentValues; 20 import android.content.Context; 21 import android.support.annotation.MainThread; 22 import android.support.annotation.WorkerThread; 23 import com.android.dialer.calllog.database.contract.AnnotatedCallLogContract; 24 import com.google.common.util.concurrent.ListenableFuture; 25 import java.util.List; 26 27 /** 28 * A source of data for one or more columns in the annotated call log. 29 * 30 * <p>Data sources have three lifecycle operations, which are always called on the same thread and 31 * in the same order for a particular "checkDirtyAndRebuild" cycle. However, not all operations are 32 * always invoked. 33 * 34 * <ol> 35 * <li>{@link #isDirty(Context)}: Invoked only if the framework doesn't yet know if a rebuild is 36 * necessary. 37 * <li>{@link #fill(Context, CallLogMutations)}: Invoked only if the framework determined a 38 * rebuild is necessary. 39 * <li>{@link #onSuccessfulFill(Context)}: Invoked if and only if fill was previously called and 40 * the mutations provided by the previous fill operation succeeded in being applied. 41 * </ol> 42 * 43 * <p>Because {@link #isDirty(Context)} is not always invoked, {@link #fill(Context, 44 * CallLogMutations)} shouldn't rely on any state saved during {@link #isDirty(Context)}. It 45 * <em>is</em> safe to assume that {@link #onSuccessfulFill(Context)} refers to the previous fill 46 * operation. 47 * 48 * <p>The same data source objects may be reused across multiple checkDirtyAndRebuild cycles, so 49 * implementors should take care to clear any internal state at the start of a new cycle. 50 * 51 * <p>{@link #coalesce(List)} may be called from any worker thread at any time. 52 */ 53 public interface CallLogDataSource { 54 55 /** 56 * A lightweight check which runs frequently to detect if the annotated call log is out of date 57 * with respect to this data source. 58 * 59 * <p>This is typically used to detect external changes to the underlying data source which have 60 * been made in such a way that the dialer application was not notified. 61 * 62 * <p>Most implementations of this method will rely on some sort of last modified timestamp. If it 63 * is impossible for a data source to be modified without the dialer application being notified, 64 * this method may immediately return false. 65 * 66 * @see CallLogDataSource class doc for complete lifecyle information 67 */ isDirty(Context appContext)68 ListenableFuture<Boolean> isDirty(Context appContext); 69 70 /** 71 * Computes the set of mutations necessary to update the annotated call log with respect to this 72 * data source. 73 * 74 * @see CallLogDataSource class doc for complete lifecyle information 75 * @param mutations the set of mutations which this method should contribute to. Note that it may 76 * contain inserts from the system call log, and these inserts should be modified by each data 77 * source. 78 */ fill(Context appContext, CallLogMutations mutations)79 ListenableFuture<Void> fill(Context appContext, CallLogMutations mutations); 80 81 /** 82 * Called after database mutations have been applied to all data sources. This is useful for 83 * saving state such as the timestamp of the last row processed in an underlying database. Note 84 * that all mutations across all data sources are applied in a single transaction. 85 * 86 * @see CallLogDataSource class doc for complete lifecyle information 87 */ onSuccessfulFill(Context appContext)88 ListenableFuture<Void> onSuccessfulFill(Context appContext); 89 90 /** 91 * Combines raw annotated call log rows into a single coalesced row. 92 * 93 * <p>May be called by any worker thread at any time so implementations should take care to be 94 * threadsafe. (Ideally no state should be required to implement this.) 95 * 96 * @param individualRowsSortedByTimestampDesc group of fully populated rows from {@link 97 * AnnotatedCallLogContract.AnnotatedCallLog} which need to be combined for display purposes. 98 * This method should not modify this list. 99 * @return a partial {@link AnnotatedCallLogContract.CoalescedAnnotatedCallLog} row containing 100 * only columns which this data source is responsible for, which is the result of aggregating 101 * {@code individualRowsSortedByTimestampDesc}. 102 */ 103 @WorkerThread coalesce(List<ContentValues> individualRowsSortedByTimestampDesc)104 ContentValues coalesce(List<ContentValues> individualRowsSortedByTimestampDesc); 105 106 @MainThread registerContentObservers(Context appContext)107 void registerContentObservers(Context appContext); 108 } 109