diff options
Diffstat (limited to 'java')
14 files changed, 436 insertions, 71 deletions
diff --git a/java/com/android/dialer/app/calllog/CallLogNotificationsService.java b/java/com/android/dialer/app/calllog/CallLogNotificationsService.java index 5949141f1..10e30ff72 100644 --- a/java/com/android/dialer/app/calllog/CallLogNotificationsService.java +++ b/java/com/android/dialer/app/calllog/CallLogNotificationsService.java @@ -31,6 +31,7 @@ import com.android.dialer.common.Assert; import com.android.dialer.common.LogUtil; import com.android.dialer.common.concurrent.DialerExecutor.Worker; import com.android.dialer.common.concurrent.DialerExecutorComponent; +import com.android.dialer.notification.missedcalls.MissedCallNotificationCanceller; import com.android.dialer.telecom.TelecomUtil; import com.android.dialer.util.PermissionsUtil; @@ -87,14 +88,6 @@ public class CallLogNotificationsService extends IntentService { context.startService(serviceIntent); } - public static void markSingleNewVoicemailAsOld(Context context, @Nullable Uri voicemailUri) { - LogUtil.enterBlock("CallLogNotificationsService.markSingleNewVoicemailAsOld"); - Intent serviceIntent = new Intent(context, CallLogNotificationsService.class); - serviceIntent.setAction(CallLogNotificationsService.ACTION_MARK_SINGLE_NEW_VOICEMAIL_AS_OLD); - serviceIntent.setData(voicemailUri); - context.startService(serviceIntent); - } - public static void cancelAllMissedCalls(Context context) { LogUtil.enterBlock("CallLogNotificationsService.cancelAllMissedCalls"); DialerExecutorComponent.get(context) @@ -175,7 +168,7 @@ public class CallLogNotificationsService extends IntentService { case ACTION_CANCEL_SINGLE_MISSED_CALL: Uri callUri = intent.getData(); CallLogNotificationsQueryHelper.markSingleMissedCallInCallLogAsRead(this, callUri); - MissedCallNotifier.cancelSingleMissedCallNotification(this, callUri); + MissedCallNotificationCanceller.cancelSingle(this, callUri); TelecomUtil.cancelMissedCallsNotification(this); break; case ACTION_CALL_BACK_FROM_MISSED_CALL_NOTIFICATION: @@ -196,7 +189,7 @@ public class CallLogNotificationsService extends IntentService { LogUtil.enterBlock("CallLogNotificationsService.cancelAllMissedCallsBackground"); Assert.isWorkerThread(); CallLogNotificationsQueryHelper.markAllMissedCallsInCallLogAsRead(context); - MissedCallNotifier.cancelAllMissedCallNotifications(context); + MissedCallNotificationCanceller.cancelAll(context); TelecomUtil.cancelMissedCallsNotification(context); } diff --git a/java/com/android/dialer/app/calllog/MissedCallNotifier.java b/java/com/android/dialer/app/calllog/MissedCallNotifier.java index 417f8f0f9..14bbdfa56 100644 --- a/java/com/android/dialer/app/calllog/MissedCallNotifier.java +++ b/java/com/android/dialer/app/calllog/MissedCallNotifier.java @@ -58,7 +58,9 @@ import com.android.dialer.duo.DuoConstants; import com.android.dialer.enrichedcall.FuzzyPhoneNumberMatcher; import com.android.dialer.notification.DialerNotificationManager; import com.android.dialer.notification.NotificationChannelId; -import com.android.dialer.notification.NotificationManagerUtils; +import com.android.dialer.notification.missedcalls.MissedCallConstants; +import com.android.dialer.notification.missedcalls.MissedCallNotificationCanceller; +import com.android.dialer.notification.missedcalls.MissedCallNotificationTags; import com.android.dialer.phonenumbercache.ContactInfo; import com.android.dialer.phonenumberutil.PhoneNumberHelper; import com.android.dialer.precall.PreCall; @@ -71,18 +73,6 @@ import java.util.Set; /** Creates a notification for calls that the user missed (neither answered nor rejected). */ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { - /** Prefix used to generate a unique tag for each missed call notification. */ - private static final String NOTIFICATION_TAG_PREFIX = "MissedCall_"; - /** Common ID for all missed call notifications. */ - private static final int NOTIFICATION_ID = 1; - /** Tag for the group summary notification. */ - private static final String GROUP_SUMMARY_NOTIFICATION_TAG = "GroupSummary_MissedCall"; - /** - * Key used to associate all missed call notifications and the summary as belonging to a single - * group. - */ - private static final String GROUP_KEY = "MissedCallGroup"; - private final Context context; private final CallLogNotificationsQueryHelper callLogNotificationsQueryHelper; @@ -126,7 +116,7 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { if ((newCalls != null && newCalls.isEmpty()) || count == 0) { // No calls to notify about: clear the notification. CallLogNotificationsQueryHelper.markAllMissedCallsInCallLogAsRead(context); - cancelAllMissedCallNotifications(context); + MissedCallNotificationCanceller.cancelAll(context); return; } @@ -226,7 +216,10 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { LogUtil.i("MissedCallNotifier.updateMissedCallNotification", "adding missed call notification"); DialerNotificationManager.notify( - context, GROUP_SUMMARY_NOTIFICATION_TAG, NOTIFICATION_ID, notification); + context, + MissedCallConstants.GROUP_SUMMARY_NOTIFICATION_TAG, + MissedCallConstants.NOTIFICATION_ID, + notification); if (useCallList) { // Do not repost active notifications to prevent erasing post call notes. @@ -240,7 +233,10 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { String callTag = getNotificationTagForCall(call); if (!activeTags.contains(callTag)) { DialerNotificationManager.notify( - context, callTag, NOTIFICATION_ID, getNotificationForCall(call, null)); + context, + callTag, + MissedCallConstants.NOTIFICATION_ID, + getNotificationForCall(call, null)); } } } @@ -286,29 +282,8 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { } } - public static void cancelAllMissedCallNotifications(@NonNull Context context) { - NotificationManagerUtils.cancelAllInGroup(context, GROUP_KEY); - } - - public static void cancelSingleMissedCallNotification( - @NonNull Context context, @Nullable Uri callUri) { - if (callUri == null) { - LogUtil.e( - "MissedCallNotifier.cancelSingleMissedCallNotification", - "unable to cancel notification, uri is null"); - return; - } - // This will also dismiss the group summary if there are no more missed call notifications. - DialerNotificationManager.cancel( - context, getNotificationTagForCallUri(callUri), NOTIFICATION_ID); - } - private static String getNotificationTagForCall(@NonNull NewCall call) { - return getNotificationTagForCallUri(call.callsUri); - } - - private static String getNotificationTagForCallUri(@NonNull Uri callUri) { - return NOTIFICATION_TAG_PREFIX + callUri; + return MissedCallNotificationTags.getNotificationTagForCallUri(call.callsUri); } @WorkerThread @@ -324,7 +299,7 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { DialerNotificationManager.notify( context, getNotificationTagForCall(call), - NOTIFICATION_ID, + MissedCallConstants.NOTIFICATION_ID, getNotificationForCall(call, note)); return; } @@ -408,7 +383,7 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { private Notification.Builder createNotificationBuilder() { return new Notification.Builder(context) - .setGroup(GROUP_KEY) + .setGroup(MissedCallConstants.GROUP_KEY) .setSmallIcon(android.R.drawable.stat_notify_missed_call) .setColor(context.getResources().getColor(R.color.dialer_theme_color, null)) .setAutoCancel(true) @@ -437,7 +412,7 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { public void callBackFromMissedCall(String number, Uri callUri) { closeSystemDialogs(context); CallLogNotificationsQueryHelper.markSingleMissedCallInCallLogAsRead(context, callUri); - cancelSingleMissedCallNotification(context, callUri); + MissedCallNotificationCanceller.cancelSingle(context, callUri); DialerUtils.startActivityWithErrorToast( context, PreCall.getIntent( @@ -450,7 +425,7 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { public void sendSmsFromMissedCall(String number, Uri callUri) { closeSystemDialogs(context); CallLogNotificationsQueryHelper.markSingleMissedCallInCallLogAsRead(context, callUri); - cancelSingleMissedCallNotification(context, callUri); + MissedCallNotificationCanceller.cancelSingle(context, callUri); DialerUtils.startActivityWithErrorToast( context, IntentUtil.getSendSmsIntent(number).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); } diff --git a/java/com/android/dialer/calllog/CallLogComponent.java b/java/com/android/dialer/calllog/CallLogComponent.java index c7db2a1b8..bb5bfee2a 100644 --- a/java/com/android/dialer/calllog/CallLogComponent.java +++ b/java/com/android/dialer/calllog/CallLogComponent.java @@ -27,6 +27,8 @@ public abstract class CallLogComponent { public abstract RefreshAnnotatedCallLogWorker getRefreshAnnotatedCallLogWorker(); + public abstract ClearMissedCalls getClearMissedCalls(); + public static CallLogComponent get(Context context) { return ((HasComponent) ((HasRootComponent) context.getApplicationContext()).component()) .callLogComponent(); diff --git a/java/com/android/dialer/calllog/ClearMissedCalls.java b/java/com/android/dialer/calllog/ClearMissedCalls.java new file mode 100644 index 000000000..d216e7b88 --- /dev/null +++ b/java/com/android/dialer/calllog/ClearMissedCalls.java @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.dialer.calllog; + +import android.annotation.SuppressLint; +import android.content.ContentValues; +import android.content.Context; +import android.net.Uri; +import android.provider.CallLog.Calls; +import android.support.v4.os.UserManagerCompat; +import com.android.dialer.common.LogUtil; +import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor; +import com.android.dialer.common.concurrent.Annotations.Ui; +import com.android.dialer.common.database.Selection; +import com.android.dialer.inject.ApplicationContext; +import com.android.dialer.notification.missedcalls.MissedCallNotificationCanceller; +import com.android.dialer.util.PermissionsUtil; +import com.google.common.collect.ImmutableSet; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; +import java.util.Collection; +import javax.inject.Inject; + +/** + * Clears missed calls. This includes cancelling notifications and updating the "NEW" status in the + * system call log. + */ +public final class ClearMissedCalls { + + private final Context appContext; + private final ListeningExecutorService backgroundExecutor; + private final ListeningExecutorService uiThreadExecutor; + + @Inject + ClearMissedCalls( + @ApplicationContext Context appContext, + @BackgroundExecutor ListeningExecutorService backgroundExecutor, + @Ui ListeningExecutorService uiThreadExecutor) { + this.appContext = appContext; + this.backgroundExecutor = backgroundExecutor; + this.uiThreadExecutor = uiThreadExecutor; + } + + /** + * Cancels all missed call notifications and marks all "new" missed calls in the system call log + * as "not new". + */ + public ListenableFuture<Void> clearAll() { + ListenableFuture<Void> markNewFuture = markNotNew(ImmutableSet.of()); + ListenableFuture<Void> cancelNotificationsFuture = + uiThreadExecutor.submit( + () -> { + MissedCallNotificationCanceller.cancelAll(appContext); + return null; + }); + + // Note on this usage of whenAllComplete: + // -The returned future completes when all sub-futures complete (whether they fail or not) + // -The returned future fails if any sub-future fails + return Futures.whenAllComplete(markNewFuture, cancelNotificationsFuture) + .call( + () -> { + // Calling get() is necessary to propagate failures. + markNewFuture.get(); + cancelNotificationsFuture.get(); + return null; + }, + MoreExecutors.directExecutor()); + } + + /** + * For the provided set of IDs from the system call log, cancels their missed call notifications + * and marks them "not new". + * + * @param ids IDs from the system call log (see {@link Calls#_ID}}. + */ + public ListenableFuture<Void> clearBySystemCallLogId(Collection<Long> ids) { + ListenableFuture<Void> markNewFuture = markNotNew(ids); + ListenableFuture<Void> cancelNotificationsFuture = + uiThreadExecutor.submit( + () -> { + for (long id : ids) { + Uri callUri = Calls.CONTENT_URI.buildUpon().appendPath(Long.toString(id)).build(); + MissedCallNotificationCanceller.cancelSingle(appContext, callUri); + } + return null; + }); + + // Note on this usage of whenAllComplete: + // -The returned future completes when all sub-futures complete (whether they fail or not) + // -The returned future fails if any sub-future fails + return Futures.whenAllComplete(markNewFuture, cancelNotificationsFuture) + .call( + () -> { + // Calling get() is necessary to propagate failures. + markNewFuture.get(); + cancelNotificationsFuture.get(); + return null; + }, + MoreExecutors.directExecutor()); + } + + /** + * Marks all provided system call log IDs as not new, or if the provided collection is empty, + * marks all calls as not new. + */ + @SuppressLint("MissingPermission") + private ListenableFuture<Void> markNotNew(Collection<Long> ids) { + return backgroundExecutor.submit( + () -> { + if (!UserManagerCompat.isUserUnlocked(appContext)) { + LogUtil.e("ClearMissedCalls.markNotNew", "locked"); + return null; + } + if (!PermissionsUtil.hasCallLogWritePermissions(appContext)) { + LogUtil.e("ClearMissedCalls.markNotNew", "no permission"); + return null; + } + + ContentValues values = new ContentValues(); + values.put(Calls.NEW, 0); + + Selection.Builder selectionBuilder = + Selection.builder() + .and(Selection.column(Calls.NEW).is("=", 1)) + .and(Selection.column(Calls.TYPE).is("=", Calls.MISSED_TYPE)); + if (!ids.isEmpty()) { + selectionBuilder.and(Selection.column(Calls._ID).in(toStrings(ids))); + } + Selection selection = selectionBuilder.build(); + appContext + .getContentResolver() + .update( + Calls.CONTENT_URI, + values, + selection.getSelection(), + selection.getSelectionArgs()); + return null; + }); + } + + private static String[] toStrings(Collection<Long> longs) { + String[] strings = new String[longs.size()]; + int i = 0; + for (long value : longs) { + strings[i++] = Long.toString(value); + } + return strings; + } +} diff --git a/java/com/android/dialer/calllog/ui/NewCallLogFragment.java b/java/com/android/dialer/calllog/ui/NewCallLogFragment.java index 10f75ef07..5e676f072 100644 --- a/java/com/android/dialer/calllog/ui/NewCallLogFragment.java +++ b/java/com/android/dialer/calllog/ui/NewCallLogFragment.java @@ -18,6 +18,7 @@ package com.android.dialer.calllog.ui; import android.database.Cursor; import android.os.Bundle; import android.support.annotation.Nullable; +import android.support.annotation.VisibleForTesting; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager.LoaderCallbacks; import android.support.v4.content.Loader; @@ -31,10 +32,14 @@ import com.android.dialer.calllog.CallLogFramework; import com.android.dialer.calllog.CallLogFramework.CallLogUi; import com.android.dialer.calllog.RefreshAnnotatedCallLogWorker; import com.android.dialer.common.LogUtil; +import com.android.dialer.common.concurrent.DefaultFutureCallback; import com.android.dialer.common.concurrent.DialerExecutorComponent; import com.android.dialer.common.concurrent.ThreadUtil; import com.android.dialer.common.concurrent.UiListener; +import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.MoreExecutors; +import java.util.concurrent.TimeUnit; /** The "new" call log fragment implementation, which is built on top of the annotated call log. */ public final class NewCallLogFragment extends Fragment @@ -46,13 +51,19 @@ public final class NewCallLogFragment extends Fragment * the simulator, using this value results in ~6 refresh cycles (on a release build) to write 120 * call log entries. */ - private static final long WAIT_MILLIS = 100L; + private static final long REFRESH_ANNOTATED_CALL_LOG_WAIT_MILLIS = 100L; + + @VisibleForTesting + static final long MARK_ALL_CALLS_READ_WAIT_MILLIS = TimeUnit.SECONDS.toMillis(3); private RefreshAnnotatedCallLogWorker refreshAnnotatedCallLogWorker; private UiListener<Void> refreshAnnotatedCallLogListener; private RecyclerView recyclerView; @Nullable private Runnable refreshAnnotatedCallLogRunnable; + private boolean shouldMarkCallsRead = false; + private final Runnable setShouldMarkCallsReadTrue = () -> shouldMarkCallsRead = true; + public NewCallLogFragment() { LogUtil.enterBlock("NewCallLogFragment.NewCallLogFragment"); } @@ -103,6 +114,13 @@ public final class NewCallLogFragment extends Fragment ((NewCallLogAdapter) recyclerView.getAdapter()).clearCache(); recyclerView.getAdapter().notifyDataSetChanged(); } + + // We shouldn't mark the calls as read immediately when the 3 second timer expires because we + // don't want to disrupt the UI; instead we set a bit indicating to mark them read when the user + // leaves the fragment (in onPause). + shouldMarkCallsRead = false; + ThreadUtil.getUiThreadHandler() + .postDelayed(setShouldMarkCallsReadTrue, MARK_ALL_CALLS_READ_WAIT_MILLIS); } @Override @@ -113,9 +131,17 @@ public final class NewCallLogFragment extends Fragment // This is pending work that we don't actually need to follow through with. ThreadUtil.getUiThreadHandler().removeCallbacks(refreshAnnotatedCallLogRunnable); + ThreadUtil.getUiThreadHandler().removeCallbacks(setShouldMarkCallsReadTrue); CallLogFramework callLogFramework = CallLogComponent.get(getContext()).callLogFramework(); callLogFramework.detachUi(); + + if (shouldMarkCallsRead) { + Futures.addCallback( + CallLogComponent.get(getContext()).getClearMissedCalls().clearAll(), + new DefaultFutureCallback<>(), + MoreExecutors.directExecutor()); + } } @Override @@ -159,7 +185,8 @@ public final class NewCallLogFragment extends Fragment throw new RuntimeException(throwable); }); }; - ThreadUtil.getUiThreadHandler().postDelayed(refreshAnnotatedCallLogRunnable, WAIT_MILLIS); + ThreadUtil.getUiThreadHandler() + .postDelayed(refreshAnnotatedCallLogRunnable, REFRESH_ANNOTATED_CALL_LOG_WAIT_MILLIS); } @Override diff --git a/java/com/android/dialer/calllog/ui/menu/NewCallLogMenu.java b/java/com/android/dialer/calllog/ui/menu/NewCallLogMenu.java index 81c05135f..02724e628 100644 --- a/java/com/android/dialer/calllog/ui/menu/NewCallLogMenu.java +++ b/java/com/android/dialer/calllog/ui/menu/NewCallLogMenu.java @@ -17,10 +17,15 @@ package com.android.dialer.calllog.ui.menu; import android.content.Context; +import android.provider.CallLog.Calls; import android.view.View; +import com.android.dialer.calllog.CallLogComponent; import com.android.dialer.calllog.model.CoalescedRow; +import com.android.dialer.common.concurrent.DefaultFutureCallback; import com.android.dialer.contactactions.ContactActionBottomSheet; import com.android.dialer.glidephotomanager.GlidePhotoManager; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.MoreExecutors; /** Handles configuration of the bottom sheet menus for call log entries. */ public final class NewCallLogMenu { @@ -28,11 +33,23 @@ public final class NewCallLogMenu { /** Creates and returns the OnClickListener which opens the menu for the provided row. */ public static View.OnClickListener createOnClickListener( Context context, CoalescedRow row, GlidePhotoManager glidePhotoManager) { - return (view) -> - ContactActionBottomSheet.show( - context, - PrimaryAction.fromRow(context, row), - Modules.fromRow(context, row), - glidePhotoManager); + return view -> { + ContactActionBottomSheet.show( + context, + PrimaryAction.fromRow(context, row), + Modules.fromRow(context, row), + glidePhotoManager); + + // If the user opens the bottom sheet for a new call, clear the notifications and make the row + // not bold immediately. To do this, mark all of the calls in group as not new. + if (row.isNew() && row.callType() == Calls.MISSED_TYPE) { + Futures.addCallback( + CallLogComponent.get(context) + .getClearMissedCalls() + .clearBySystemCallLogId(row.coalescedIds().getCoalescedIdList()), + new DefaultFutureCallback<>(), + MoreExecutors.directExecutor()); + } + }; } } diff --git a/java/com/android/dialer/common/concurrent/DefaultFutureCallback.java b/java/com/android/dialer/common/concurrent/DefaultFutureCallback.java new file mode 100644 index 000000000..93ad0faf0 --- /dev/null +++ b/java/com/android/dialer/common/concurrent/DefaultFutureCallback.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.dialer.common.concurrent; + +import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.MoreExecutors; + +/** + * Returns a {@link FutureCallback} which does nothing on success and crashes the application on + * failure. + * + * <p>You generally shouldn't use this for futures which should be tied to UI, for those use {@link + * UiListener}. + * + * <p>Can be safely used with {@link MoreExecutors#directExecutor()} + */ +public final class DefaultFutureCallback<T> implements FutureCallback<T> { + + @Override + public void onSuccess(T unused) {} + + @Override + public void onFailure(Throwable throwable) { + ThreadUtil.getUiThreadHandler() + .post( + () -> { + throw new RuntimeException(throwable); + }); + } +} diff --git a/java/com/android/dialer/common/concurrent/UiThreadExecutor.java b/java/com/android/dialer/common/concurrent/UiThreadExecutor.java index ec51ed334..8378d69ce 100644 --- a/java/com/android/dialer/common/concurrent/UiThreadExecutor.java +++ b/java/com/android/dialer/common/concurrent/UiThreadExecutor.java @@ -31,7 +31,7 @@ import javax.inject.Inject; public class UiThreadExecutor extends AbstractListeningExecutorService { @Inject - UiThreadExecutor() {} + public UiThreadExecutor() {} @Override public void shutdown() { diff --git a/java/com/android/dialer/main/impl/NewMainActivityPeer.java b/java/com/android/dialer/main/impl/NewMainActivityPeer.java index 789648928..0a85667a1 100644 --- a/java/com/android/dialer/main/impl/NewMainActivityPeer.java +++ b/java/com/android/dialer/main/impl/NewMainActivityPeer.java @@ -16,16 +16,22 @@ package com.android.dialer.main.impl; +import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; +import com.android.dialer.calllog.CallLogComponent; import com.android.dialer.calllog.ui.NewCallLogFragment; +import com.android.dialer.common.concurrent.DefaultFutureCallback; import com.android.dialer.main.MainActivityPeer; import com.android.dialer.main.impl.bottomnav.BottomNavBar; import com.android.dialer.main.impl.bottomnav.BottomNavBar.OnBottomNavTabSelectedListener; import com.android.dialer.main.impl.bottomnav.BottomNavBar.TabIndex; import com.android.dialer.voicemail.listui.NewVoicemailFragment; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.MoreExecutors; /** MainActivityPeer that implements the new fragments. */ public class NewMainActivityPeer implements MainActivityPeer { @@ -40,7 +46,8 @@ public class NewMainActivityPeer implements MainActivityPeer { public void onActivityCreate(Bundle saveInstanceState) { mainActivity.setContentView(R.layout.main_activity); MainBottomNavBarBottomNavTabListener bottomNavBarBottomNavTabListener = - new MainBottomNavBarBottomNavTabListener(mainActivity.getSupportFragmentManager()); + new MainBottomNavBarBottomNavTabListener( + mainActivity.getSupportFragmentManager(), mainActivity.getApplicationContext()); BottomNavBar bottomNav = mainActivity.findViewById(R.id.bottom_nav_bar); bottomNav.addOnTabSelectedListener(bottomNavBarBottomNavTabListener); bottomNav.selectTab(TabIndex.SPEED_DIAL); @@ -77,9 +84,12 @@ public class NewMainActivityPeer implements MainActivityPeer { private static final String VOICEMAIL_TAG = "voicemail"; private final FragmentManager supportFragmentManager; + private final Context appContext; - private MainBottomNavBarBottomNavTabListener(FragmentManager supportFragmentManager) { + private MainBottomNavBarBottomNavTabListener( + FragmentManager supportFragmentManager, Context appContext) { this.supportFragmentManager = supportFragmentManager; + this.appContext = appContext; } @Override @@ -126,8 +136,18 @@ public class NewMainActivityPeer implements MainActivityPeer { private void hideAllFragments() { FragmentTransaction supportTransaction = supportFragmentManager.beginTransaction(); - if (supportFragmentManager.findFragmentByTag(CALL_LOG_TAG) != null) { - supportTransaction.hide(supportFragmentManager.findFragmentByTag(CALL_LOG_TAG)); + Fragment callLogFragment = supportFragmentManager.findFragmentByTag(CALL_LOG_TAG); + if (callLogFragment != null) { + if (callLogFragment.isVisible()) { + // If the user taps any bottom nav button and the call log is showing, immediately cancel + // missed calls (unbold them and clear their notifications). + Futures.addCallback( + // TODO(zachh): Use dagger to create Peer and MainBottomNavBarBottomNavTabListener. + CallLogComponent.get(appContext).getClearMissedCalls().clearAll(), + new DefaultFutureCallback<>(), + MoreExecutors.directExecutor()); + } + supportTransaction.hide(callLogFragment); } if (supportFragmentManager.findFragmentByTag(VOICEMAIL_TAG) != null) { supportTransaction.hide(supportFragmentManager.findFragmentByTag(VOICEMAIL_TAG)); diff --git a/java/com/android/dialer/main/impl/bottomnav/BottomNavBar.java b/java/com/android/dialer/main/impl/bottomnav/BottomNavBar.java index a580aee68..2945e39a9 100644 --- a/java/com/android/dialer/main/impl/bottomnav/BottomNavBar.java +++ b/java/com/android/dialer/main/impl/bottomnav/BottomNavBar.java @@ -162,6 +162,7 @@ public final class BottomNavBar extends LinearLayout { } } + @TabIndex public int getSelectedTab() { return selectedTab; } diff --git a/java/com/android/dialer/notification/missedcalls/MissedCallConstants.java b/java/com/android/dialer/notification/missedcalls/MissedCallConstants.java new file mode 100644 index 000000000..8553ea58e --- /dev/null +++ b/java/com/android/dialer/notification/missedcalls/MissedCallConstants.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.dialer.notification.missedcalls; + +/** Constants related to missed call notifications. */ +public final class MissedCallConstants { + + /** Prefix used to generate a unique tag for each missed call notification. */ + public static final String NOTIFICATION_TAG_PREFIX = "MissedCall_"; + + /** Common ID for all missed call notifications. */ + public static final int NOTIFICATION_ID = 1; + + /** Tag for the group summary notification. */ + public static final String GROUP_SUMMARY_NOTIFICATION_TAG = "GroupSummary_MissedCall"; + + /** + * Key used to associate all missed call notifications and the summary as belonging to a single + * group. + */ + public static final String GROUP_KEY = "MissedCallGroup"; +} diff --git a/java/com/android/dialer/notification/missedcalls/MissedCallNotificationCanceller.java b/java/com/android/dialer/notification/missedcalls/MissedCallNotificationCanceller.java new file mode 100644 index 000000000..8798c2127 --- /dev/null +++ b/java/com/android/dialer/notification/missedcalls/MissedCallNotificationCanceller.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.dialer.notification.missedcalls; + +import android.content.Context; +import android.net.Uri; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import com.android.dialer.common.LogUtil; +import com.android.dialer.notification.DialerNotificationManager; +import com.android.dialer.notification.NotificationManagerUtils; + +/** Cancels missed calls notifications. */ +public final class MissedCallNotificationCanceller { + + /** Cancels all missed call notifications. */ + public static void cancelAll(@NonNull Context context) { + NotificationManagerUtils.cancelAllInGroup(context, MissedCallConstants.GROUP_KEY); + } + + /** Cancels a missed call notification for a single call. */ + public static void cancelSingle(@NonNull Context context, @Nullable Uri callUri) { + if (callUri == null) { + LogUtil.e( + "MissedCallNotificationCanceller.cancelSingle", + "unable to cancel notification, uri is null"); + return; + } + // This will also dismiss the group summary if there are no more missed call notifications. + DialerNotificationManager.cancel( + context, + MissedCallNotificationTags.getNotificationTagForCallUri(callUri), + MissedCallConstants.NOTIFICATION_ID); + } +} diff --git a/java/com/android/dialer/notification/missedcalls/MissedCallNotificationTags.java b/java/com/android/dialer/notification/missedcalls/MissedCallNotificationTags.java new file mode 100644 index 000000000..64f28eeec --- /dev/null +++ b/java/com/android/dialer/notification/missedcalls/MissedCallNotificationTags.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.dialer.notification.missedcalls; + +import android.net.Uri; +import android.support.annotation.NonNull; + +/** Static methods related to missed call notification tags. */ +public final class MissedCallNotificationTags { + + /** Gets the notification tag for a single call. */ + public static String getNotificationTagForCallUri(@NonNull Uri callUri) { + return MissedCallConstants.NOTIFICATION_TAG_PREFIX + callUri; + } +} diff --git a/java/com/android/dialer/simulator/impl/SimulatorSimCallManager.java b/java/com/android/dialer/simulator/impl/SimulatorSimCallManager.java index 00899fd69..f28393c0c 100644 --- a/java/com/android/dialer/simulator/impl/SimulatorSimCallManager.java +++ b/java/com/android/dialer/simulator/impl/SimulatorSimCallManager.java @@ -29,6 +29,7 @@ import android.telecom.TelecomManager; import android.telephony.TelephonyManager; import com.android.dialer.common.Assert; import com.android.dialer.common.LogUtil; +import com.android.dialer.strictmode.StrictModeUtils; import java.util.Arrays; import java.util.List; import java.util.Random; @@ -53,17 +54,23 @@ public class SimulatorSimCallManager { static void register(@NonNull Context context) { LogUtil.enterBlock("SimulatorSimCallManager.register"); Assert.isNotNull(context); - TelecomManager telecomManager = context.getSystemService(TelecomManager.class); - telecomManager.registerPhoneAccount(buildSimCallManagerAccount(context)); - telecomManager.registerPhoneAccount(buildVideoProviderAccount(context)); + StrictModeUtils.bypass( + () -> { + TelecomManager telecomManager = context.getSystemService(TelecomManager.class); + telecomManager.registerPhoneAccount(buildSimCallManagerAccount(context)); + telecomManager.registerPhoneAccount(buildVideoProviderAccount(context)); + }); } static void unregister(@NonNull Context context) { LogUtil.enterBlock("SimulatorSimCallManager.unregister"); Assert.isNotNull(context); - TelecomManager telecomManager = context.getSystemService(TelecomManager.class); - telecomManager.unregisterPhoneAccount(getSimCallManagerHandle(context)); - telecomManager.unregisterPhoneAccount(getVideoProviderHandle(context)); + StrictModeUtils.bypass( + () -> { + TelecomManager telecomManager = context.getSystemService(TelecomManager.class); + telecomManager.unregisterPhoneAccount(getSimCallManagerHandle(context)); + telecomManager.unregisterPhoneAccount(getVideoProviderHandle(context)); + }); } @NonNull |