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.dialer.calllog.notifier; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.SharedPreferences; 22 import android.support.v4.content.LocalBroadcastManager; 23 import com.android.dialer.calllog.constants.IntentNames; 24 import com.android.dialer.calllog.constants.SharedPrefKeys; 25 import com.android.dialer.common.LogUtil; 26 import com.android.dialer.inject.ApplicationContext; 27 import com.android.dialer.storage.Unencrypted; 28 import javax.inject.Inject; 29 import javax.inject.Singleton; 30 31 /** 32 * Notifies that a refresh of the annotated call log needs to be started/cancelled. 33 * 34 * <p>Methods in this class are usually invoked when the underlying data backing the annotated call 35 * log change. 36 * 37 * <p>For example, a {@link android.database.ContentObserver} for the system call log can use {@link 38 * #markDirtyAndNotify()} to force the annotated call log to be rebuilt. 39 */ 40 @Singleton 41 public class RefreshAnnotatedCallLogNotifier { 42 43 private final Context appContext; 44 private final SharedPreferences sharedPreferences; 45 46 @Inject RefreshAnnotatedCallLogNotifier( @pplicationContext Context appContext, @Unencrypted SharedPreferences sharedPreferences)47 RefreshAnnotatedCallLogNotifier( 48 @ApplicationContext Context appContext, @Unencrypted SharedPreferences sharedPreferences) { 49 this.appContext = appContext; 50 this.sharedPreferences = sharedPreferences; 51 } 52 53 /** 54 * Mark the annotated call log as "dirty" and notify that it needs to be refreshed. 55 * 56 * <p>This will force a rebuild by skip checking whether the annotated call log is "dirty". 57 */ markDirtyAndNotify()58 public void markDirtyAndNotify() { 59 LogUtil.enterBlock("RefreshAnnotatedCallLogNotifier.markDirtyAndNotify"); 60 61 sharedPreferences.edit().putBoolean(SharedPrefKeys.FORCE_REBUILD, true).apply(); 62 notify(/* checkDirty = */ false); 63 } 64 65 /** 66 * Notifies that the annotated call log needs to be refreshed. 67 * 68 * <p>Note that the notification is sent as a broadcast, which means the annotated call log might 69 * not be refreshed if there is no corresponding receiver registered. 70 * 71 * @param checkDirty Whether to check if the annotated call log is "dirty" before proceeding to 72 * rebuild it. 73 */ notify(boolean checkDirty)74 public void notify(boolean checkDirty) { 75 LogUtil.i("RefreshAnnotatedCallLogNotifier.notify", "checkDirty = %s", checkDirty); 76 77 Intent intent = new Intent(); 78 intent.setAction(IntentNames.ACTION_REFRESH_ANNOTATED_CALL_LOG); 79 intent.putExtra(IntentNames.EXTRA_CHECK_DIRTY, checkDirty); 80 81 LocalBroadcastManager.getInstance(appContext).sendBroadcast(intent); 82 } 83 84 /** 85 * Notifies to cancel refreshing the annotated call log. 86 * 87 * <p>Note that this method does not guarantee the job to be cancelled. As the notification is 88 * sent as a broadcast, please see the corresponding receiver for details about cancelling the 89 * job. 90 */ cancel()91 public void cancel() { 92 LogUtil.enterBlock("RefreshAnnotatedCallLogNotifier.cancel"); 93 94 Intent intent = new Intent(); 95 intent.setAction(IntentNames.ACTION_CANCEL_REFRESHING_ANNOTATED_CALL_LOG); 96 97 LocalBroadcastManager.getInstance(appContext).sendBroadcast(intent); 98 } 99 } 100