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.ui.menu; 18 19 import android.Manifest.permission; 20 import android.content.Context; 21 import android.provider.CallLog; 22 import android.provider.CallLog.Calls; 23 import android.support.annotation.Nullable; 24 import android.support.annotation.RequiresPermission; 25 import com.android.dialer.CoalescedIds; 26 import com.android.dialer.common.Assert; 27 import com.android.dialer.common.LogUtil; 28 import com.android.dialer.common.concurrent.DialerExecutor.Worker; 29 import com.android.dialer.common.concurrent.DialerExecutorComponent; 30 import com.android.dialer.common.database.Selection; 31 import com.android.dialer.historyitemactions.HistoryItemActionModule; 32 import java.lang.ref.WeakReference; 33 import java.util.ArrayList; 34 import java.util.List; 35 36 /** {@link HistoryItemActionModule} for deleting a call log item in the new call log. */ 37 public final class DeleteCallLogItemModule implements HistoryItemActionModule { 38 private static final String TAG = DeleteCallLogItemModule.class.getName(); 39 40 private final Context context; 41 private final CoalescedIds coalescedIds; 42 DeleteCallLogItemModule(Context context, CoalescedIds coalescedIds)43 public DeleteCallLogItemModule(Context context, CoalescedIds coalescedIds) { 44 this.context = context; 45 this.coalescedIds = coalescedIds; 46 } 47 48 @Override getStringId()49 public int getStringId() { 50 return R.string.delete; 51 } 52 53 @Override getDrawableId()54 public int getDrawableId() { 55 return R.drawable.quantum_ic_delete_vd_theme_24; 56 } 57 58 @Override onClick()59 public boolean onClick() { 60 DialerExecutorComponent.get(context) 61 .dialerExecutorFactory() 62 .createNonUiTaskBuilder(new CallLogItemDeletionWorker(context)) 63 .build() 64 .executeSerial(coalescedIds); 65 return true; 66 } 67 68 /** 69 * A {@link Worker} that deletes a call log item. 70 * 71 * <p>It takes as input the IDs of all call log records that are coalesced into the item to be 72 * deleted. 73 */ 74 private static class CallLogItemDeletionWorker implements Worker<CoalescedIds, Void> { 75 private final WeakReference<Context> contextWeakReference; 76 CallLogItemDeletionWorker(Context context)77 CallLogItemDeletionWorker(Context context) { 78 contextWeakReference = new WeakReference<>(context); 79 } 80 81 @Nullable 82 @Override 83 @RequiresPermission(value = permission.WRITE_CALL_LOG) doInBackground(CoalescedIds coalescedIds)84 public Void doInBackground(CoalescedIds coalescedIds) throws Throwable { 85 Context context = contextWeakReference.get(); 86 if (context == null) { 87 LogUtil.e(TAG, "Unable to delete an call log item due to null context."); 88 return null; 89 } 90 91 Selection selection = 92 Selection.builder() 93 .and(Selection.column(CallLog.Calls._ID).in(getCallLogIdsAsStrings(coalescedIds))) 94 .build(); 95 int numRowsDeleted = 96 context 97 .getContentResolver() 98 .delete(Calls.CONTENT_URI, selection.getSelection(), selection.getSelectionArgs()); 99 100 if (numRowsDeleted != coalescedIds.getCoalescedIdCount()) { 101 LogUtil.e( 102 TAG, 103 "Deleting call log item is unsuccessful. %d of %d rows are deleted.", 104 numRowsDeleted, 105 coalescedIds.getCoalescedIdCount()); 106 } 107 108 return null; 109 } 110 getCallLogIdsAsStrings(CoalescedIds coalescedIds)111 private static List<String> getCallLogIdsAsStrings(CoalescedIds coalescedIds) { 112 Assert.checkArgument(coalescedIds.getCoalescedIdCount() > 0); 113 114 List<String> idStrings = new ArrayList<>(coalescedIds.getCoalescedIdCount()); 115 116 for (long callLogId : coalescedIds.getCoalescedIdList()) { 117 idStrings.add(String.valueOf(callLogId)); 118 } 119 120 return idStrings; 121 } 122 } 123 } 124