Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 1 | /* |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 2 | * Copyright (C) 2019 The Android Open Source Project |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 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 | package com.android.car.assist.client; |
| 17 | |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 18 | import static android.app.Notification.Action.SEMANTIC_ACTION_MARK_AS_READ; |
| 19 | import static android.app.Notification.Action.SEMANTIC_ACTION_REPLY; |
| 20 | |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 21 | import static com.android.car.assist.CarVoiceInteractionSession.EXCEPTION_NOTIFICATION_LISTENER_PERMISSIONS_MISSING; |
| 22 | |
Ritwika Mitra | adb2bfe | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Ritwika Mitra | 95cf93c | 2019-04-29 16:40:04 -0700 | [diff] [blame] | 24 | import android.app.ActivityManager; |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 25 | import android.app.Notification; |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 26 | import android.app.RemoteInput; |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.os.Bundle; |
| 29 | import android.provider.Settings; |
| 30 | import android.service.notification.StatusBarNotification; |
| 31 | import android.util.Log; |
| 32 | |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 33 | import androidx.annotation.StringDef; |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 34 | import androidx.core.app.NotificationCompat; |
| 35 | |
| 36 | import com.android.car.assist.CarVoiceInteractionSession; |
| 37 | import com.android.internal.app.AssistUtils; |
| 38 | |
Priyank Singh | 128ff29 | 2019-04-29 20:07:36 -0700 | [diff] [blame] | 39 | import java.util.ArrayList; |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 40 | import java.util.Arrays; |
| 41 | import java.util.Collections; |
| 42 | import java.util.HashSet; |
| 43 | import java.util.List; |
| 44 | import java.util.Objects; |
| 45 | import java.util.Set; |
| 46 | import java.util.stream.Collectors; |
| 47 | import java.util.stream.IntStream; |
| 48 | |
| 49 | /** |
| 50 | * Util class providing helper methods to interact with the current active voice service, |
| 51 | * while ensuring that the active voice service has the required permissions. |
| 52 | */ |
| 53 | public class CarAssistUtils { |
| 54 | public static final String TAG = "CarAssistUtils"; |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 55 | private static final List<Integer> REQUIRED_SEMANTIC_ACTIONS = Collections.unmodifiableList( |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 56 | Arrays.asList( |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 57 | SEMANTIC_ACTION_MARK_AS_READ |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 58 | ) |
| 59 | ); |
| 60 | |
Ritwika Mitra | aaf0c17 | 2019-01-30 08:25:03 -0800 | [diff] [blame] | 61 | private static final List<Integer> SUPPORTED_SEMANTIC_ACTIONS = Collections.unmodifiableList( |
| 62 | Arrays.asList( |
| 63 | SEMANTIC_ACTION_MARK_AS_READ, |
| 64 | SEMANTIC_ACTION_REPLY |
| 65 | ) |
| 66 | ); |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 67 | |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 68 | private final Context mContext; |
| 69 | private final AssistUtils mAssistUtils; |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 70 | private final FallbackAssistant mFallbackAssistant; |
| 71 | private final String mErrorMessage; |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 72 | private final boolean mIsFallbackAssistantEnabled; |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 73 | |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 74 | /** Interface used to receive callbacks from voice action requests. */ |
| 75 | public interface ActionRequestCallback { |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 76 | /** |
| 77 | * The action was successfully completed either by the active or fallback assistant. |
| 78 | **/ |
| 79 | String RESULT_SUCCESS = "SUCCESS"; |
| 80 | |
| 81 | /** |
| 82 | * The action was not successfully completed, but the active assistant has been prompted to |
| 83 | * alert the user of this error and handle it. The caller of this callback is recommended |
| 84 | * to NOT alert the user of this error again. |
| 85 | */ |
| 86 | String RESULT_FAILED_WITH_ERROR_HANDLED = "FAILED_WITH_ERROR_HANDLED"; |
| 87 | |
| 88 | /** |
| 89 | * The action has not been successfully completed, and the error has not been handled. |
| 90 | **/ |
| 91 | String RESULT_FAILED = "FAILED"; |
| 92 | |
| 93 | /** |
| 94 | * The list of result states. |
| 95 | */ |
| 96 | @StringDef({RESULT_FAILED, RESULT_FAILED_WITH_ERROR_HANDLED, RESULT_SUCCESS}) |
| 97 | @interface ResultState { |
| 98 | } |
| 99 | |
| 100 | /** Callback containing the result of completing the voice action request. */ |
| 101 | void onResult(@ResultState String state); |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 104 | public CarAssistUtils(Context context) { |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 105 | mContext = context; |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 106 | mAssistUtils = new AssistUtils(context); |
Ritwika Mitra | adb2bfe | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 107 | mFallbackAssistant = new FallbackAssistant(context); |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 108 | mErrorMessage = context.getString(R.string.assist_action_failed_toast); |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 109 | mIsFallbackAssistantEnabled = |
| 110 | context.getResources().getBoolean(R.bool.config_enableFallbackAssistant); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @return {@code true} if there is an active assistant. |
| 115 | */ |
| 116 | public boolean hasActiveAssistant() { |
| 117 | return mAssistUtils.getActiveServiceComponentName() != null; |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns true if the current active assistant has notification listener permissions. |
| 122 | */ |
| 123 | public boolean assistantIsNotificationListener() { |
Ritwika Mitra | 3ebfef5 | 2019-06-03 09:56:02 -0700 | [diff] [blame] | 124 | if (!hasActiveAssistant()) { |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 125 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 126 | Log.d(TAG, "No active assistant was found."); |
| 127 | } |
| 128 | return false; |
| 129 | } |
Ritwika Mitra | 3ebfef5 | 2019-06-03 09:56:02 -0700 | [diff] [blame] | 130 | final String activeComponent = mAssistUtils.getActiveServiceComponentName() |
| 131 | .flattenToString(); |
Ritwika Mitra | eb04068 | 2019-02-01 15:36:23 -0800 | [diff] [blame] | 132 | int slashIndex = activeComponent.indexOf("/"); |
| 133 | final String activePackage = activeComponent.substring(0, slashIndex); |
| 134 | |
Ritwika Mitra | 95cf93c | 2019-04-29 16:40:04 -0700 | [diff] [blame] | 135 | final String listeners = Settings.Secure.getStringForUser(mContext.getContentResolver(), |
| 136 | Settings.Secure.ENABLED_NOTIFICATION_LISTENERS, ActivityManager.getCurrentUser()); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 137 | |
Ritwika Mitra | 73da858 | 2019-05-21 11:56:20 -0700 | [diff] [blame] | 138 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 139 | Log.d(TAG, "Current user: " + ActivityManager.getCurrentUser() |
| 140 | + " has active voice service: " + activePackage + " and enabled notification " |
| 141 | + " listeners: " + listeners); |
| 142 | } |
| 143 | |
Ritwika Mitra | eb04068 | 2019-02-01 15:36:23 -0800 | [diff] [blame] | 144 | if (listeners != null) { |
| 145 | for (String listener : Arrays.asList(listeners.split(":"))) { |
| 146 | if (listener.contains(activePackage)) { |
| 147 | return true; |
| 148 | } |
| 149 | } |
| 150 | } |
Ritwika Mitra | cdc9944 | 2019-04-26 09:55:11 -0700 | [diff] [blame] | 151 | Log.w(TAG, "No notification listeners found for assistant: " + activeComponent); |
Ritwika Mitra | eb04068 | 2019-02-01 15:36:23 -0800 | [diff] [blame] | 152 | return false; |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Checks whether the notification is a car-compatible messaging notification. |
| 157 | * |
| 158 | * @param sbn The notification being checked. |
| 159 | * @return true if the notification is a car-compatible messaging notification. |
| 160 | */ |
| 161 | public static boolean isCarCompatibleMessagingNotification(StatusBarNotification sbn) { |
| 162 | return hasMessagingStyle(sbn) |
| 163 | && hasRequiredAssistantCallbacks(sbn) |
Ritwika Mitra | fdc114d | 2019-07-15 10:41:48 -0700 | [diff] [blame^] | 164 | && ((getReplyAction(sbn.getNotification()) == null) |
| 165 | || replyCallbackHasRemoteInput(sbn)) |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 166 | && assistantCallbacksShowNoUi(sbn); |
| 167 | } |
| 168 | |
| 169 | /** Returns true if the semantic action provided can be supported. */ |
| 170 | public static boolean isSupportedSemanticAction(int semanticAction) { |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 171 | return SUPPORTED_SEMANTIC_ACTIONS.contains(semanticAction); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Returns true if the notification has a messaging style. |
| 176 | * <p/> |
| 177 | * This is the case if the notification in question was provided an instance of |
| 178 | * {@link Notification.MessagingStyle} (or an instance of |
| 179 | * {@link NotificationCompat.MessagingStyle} if {@link NotificationCompat} was used). |
| 180 | */ |
| 181 | private static boolean hasMessagingStyle(StatusBarNotification sbn) { |
| 182 | return NotificationCompat.MessagingStyle |
| 183 | .extractMessagingStyleFromNotification(sbn.getNotification()) != null; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Returns true if the notification has the required Assistant callbacks to be considered |
| 188 | * a car-compatible messaging notification. The callbacks must be unambiguous, therefore false |
| 189 | * is returned if multiple callbacks exist for any semantic action that is supported. |
| 190 | */ |
| 191 | private static boolean hasRequiredAssistantCallbacks(StatusBarNotification sbn) { |
Priyank Singh | 128ff29 | 2019-04-29 20:07:36 -0700 | [diff] [blame] | 192 | List<Integer> semanticActionList = getAllActions(sbn.getNotification()) |
| 193 | .stream() |
| 194 | .map(NotificationCompat.Action::getSemanticAction) |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 195 | .filter(REQUIRED_SEMANTIC_ACTIONS::contains) |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 196 | .collect(Collectors.toList()); |
| 197 | Set<Integer> semanticActionSet = new HashSet<>(semanticActionList); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 198 | return semanticActionList.size() == semanticActionSet.size() |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 199 | && semanticActionSet.containsAll(REQUIRED_SEMANTIC_ACTIONS); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Priyank Singh | 128ff29 | 2019-04-29 20:07:36 -0700 | [diff] [blame] | 202 | /** Retrieves all visible and invisible {@link Action}s from the {@link #notification}. */ |
Ritwika Mitra | adb2bfe | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 203 | public static List<NotificationCompat.Action> getAllActions(Notification notification) { |
Priyank Singh | 128ff29 | 2019-04-29 20:07:36 -0700 | [diff] [blame] | 204 | List<NotificationCompat.Action> actions = new ArrayList<>(); |
| 205 | actions.addAll(NotificationCompat.getInvisibleActions(notification)); |
| 206 | for (int i = 0; i < NotificationCompat.getActionCount(notification); i++) { |
| 207 | actions.add(NotificationCompat.getAction(notification, i)); |
| 208 | } |
| 209 | return actions; |
| 210 | } |
| 211 | |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 212 | /** |
Ritwika Mitra | adb2bfe | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 213 | * Retrieves the {@link NotificationCompat.Action} containing the |
| 214 | * {@link NotificationCompat.Action#SEMANTIC_ACTION_MARK_AS_READ} semantic action. |
| 215 | */ |
| 216 | @Nullable |
| 217 | public static NotificationCompat.Action getMarkAsReadAction(Notification notification) { |
| 218 | for (NotificationCompat.Action action : getAllActions(notification)) { |
| 219 | if (action.getSemanticAction() |
| 220 | == NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ) { |
| 221 | return action; |
| 222 | } |
| 223 | } |
| 224 | return null; |
| 225 | } |
| 226 | |
| 227 | /** |
Ritwika Mitra | fdc114d | 2019-07-15 10:41:48 -0700 | [diff] [blame^] | 228 | * Retrieves the {@link NotificationCompat.Action} containing the |
| 229 | * {@link NotificationCompat.Action#SEMANTIC_ACTION_REPLY} semantic action. |
| 230 | */ |
| 231 | @Nullable |
| 232 | private static NotificationCompat.Action getReplyAction(Notification notification) { |
| 233 | for (NotificationCompat.Action action : getAllActions(notification)) { |
| 234 | if (action.getSemanticAction() |
| 235 | == NotificationCompat.Action.SEMANTIC_ACTION_REPLY) { |
| 236 | return action; |
| 237 | } |
| 238 | } |
| 239 | return null; |
| 240 | } |
| 241 | |
| 242 | /** |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 243 | * Returns true if the reply callback has at least one {@link RemoteInput}. |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 244 | * <p/> |
| 245 | * Precondition: There exists only one reply callback. |
| 246 | */ |
| 247 | private static boolean replyCallbackHasRemoteInput(StatusBarNotification sbn) { |
| 248 | return Arrays.stream(sbn.getNotification().actions) |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 249 | .filter(action -> action.getSemanticAction() == SEMANTIC_ACTION_REPLY) |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 250 | .map(Notification.Action::getRemoteInputs) |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 251 | .filter(Objects::nonNull) |
| 252 | .anyMatch(remoteInputs -> remoteInputs.length > 0); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** Returns true if all Assistant callbacks indicate that they show no UI, false otherwise. */ |
| 256 | private static boolean assistantCallbacksShowNoUi(StatusBarNotification sbn) { |
| 257 | final Notification notification = sbn.getNotification(); |
| 258 | return IntStream.range(0, notification.actions.length) |
| 259 | .mapToObj(i -> NotificationCompat.getAction(notification, i)) |
| 260 | .filter(Objects::nonNull) |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 261 | .filter(action -> SUPPORTED_SEMANTIC_ACTIONS.contains(action.getSemanticAction())) |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 262 | .noneMatch(NotificationCompat.Action::getShowsUserInterface); |
| 263 | } |
| 264 | |
| 265 | /** |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 266 | * Requests a given action from the current active Assistant. |
| 267 | * |
Ritwika Mitra | adb2bfe | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 268 | * @param sbn the notification payload to deliver to assistant |
| 269 | * @param voiceAction must be a valid {@link CarVoiceInteractionSession} VOICE_ACTION |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 270 | * @param callback the callback to issue on success/error |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 271 | */ |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 272 | public void requestAssistantVoiceAction(StatusBarNotification sbn, String voiceAction, |
| 273 | ActionRequestCallback callback) { |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 274 | if (!isCarCompatibleMessagingNotification(sbn)) { |
| 275 | Log.w(TAG, "Assistant action requested for non-compatible notification."); |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 276 | callback.onResult(ActionRequestCallback.RESULT_FAILED); |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 277 | return; |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Ritwika Mitra | adb2bfe | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 280 | switch (voiceAction) { |
| 281 | case CarVoiceInteractionSession.VOICE_ACTION_READ_NOTIFICATION: |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 282 | readMessageNotification(sbn, callback); |
| 283 | return; |
Ritwika Mitra | adb2bfe | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 284 | case CarVoiceInteractionSession.VOICE_ACTION_REPLY_NOTIFICATION: |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 285 | replyMessageNotification(sbn, callback); |
| 286 | return; |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 287 | default: |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 288 | Log.w(TAG, "Requested Assistant action for unsupported semantic action."); |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 289 | callback.onResult(ActionRequestCallback.RESULT_FAILED); |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 290 | return; |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 291 | } |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Requests a read action for the notification from the current active Assistant. |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 296 | * If the Assistant cannot handle the request, a fallback implementation will attempt to |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 297 | * handle it. |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 298 | * |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 299 | * @param sbn the notification to deliver as the payload |
| 300 | * @param callback the callback to issue on success/error |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 301 | */ |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 302 | private void readMessageNotification(StatusBarNotification sbn, |
| 303 | ActionRequestCallback callback) { |
| 304 | Bundle args = BundleBuilder.buildAssistantReadBundle(sbn); |
| 305 | String action = CarVoiceInteractionSession.VOICE_ACTION_READ_NOTIFICATION; |
| 306 | |
| 307 | requestAction(action, sbn, args, callback); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Requests a reply action for the notification from the current active Assistant. |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 312 | * If the Assistant cannot handle the request, a fallback implementation will attempt to |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 313 | * handle it. |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 314 | * |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 315 | * @param sbn the notification to deliver as the payload |
| 316 | * @param callback the callback to issue on success/error |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 317 | */ |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 318 | private void replyMessageNotification(StatusBarNotification sbn, |
| 319 | ActionRequestCallback callback) { |
| 320 | Bundle args = BundleBuilder.buildAssistantReplyBundle(sbn); |
| 321 | String action = CarVoiceInteractionSession.VOICE_ACTION_REPLY_NOTIFICATION; |
| 322 | |
| 323 | requestAction(action, sbn, args, callback); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 326 | private void requestAction(String action, StatusBarNotification sbn, Bundle payloadArguments, |
| 327 | ActionRequestCallback callback) { |
Ritwika Mitra | 3ebfef5 | 2019-06-03 09:56:02 -0700 | [diff] [blame] | 328 | |
| 329 | if (!hasActiveAssistant()) { |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 330 | if (mIsFallbackAssistantEnabled) { |
| 331 | handleFallback(sbn, action, callback); |
| 332 | } else { |
| 333 | // If there is no active assistant, and fallback assistant is not enabled, then |
Ritwika Mitra | 3ebfef5 | 2019-06-03 09:56:02 -0700 | [diff] [blame] | 334 | // there is nothing for us to do. |
| 335 | callback.onResult(ActionRequestCallback.RESULT_FAILED); |
| 336 | } |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | if (!assistantIsNotificationListener()) { |
| 341 | if (mIsFallbackAssistantEnabled) { |
| 342 | handleFallback(sbn, action, callback); |
| 343 | } else { |
| 344 | // If there is an active assistant, alert them to request permissions. |
| 345 | String resultState = requestHandleMissingPermission() |
| 346 | ? ActionRequestCallback.RESULT_FAILED_WITH_ERROR_HANDLED |
| 347 | : ActionRequestCallback.RESULT_FAILED; |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 348 | callback.onResult(resultState); |
| 349 | } |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 350 | return; |
Ritwika Mitra | cdc9944 | 2019-04-26 09:55:11 -0700 | [diff] [blame] | 351 | } |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 352 | |
| 353 | boolean success = mAssistUtils.showSessionForActiveService(payloadArguments, |
| 354 | CarVoiceInteractionSession.SHOW_SOURCE_NOTIFICATION, null, null); |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 355 | String resultState = ActionRequestCallback.RESULT_FAILED; |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 356 | if (success) { |
| 357 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 358 | Log.d(TAG, "Launching active assistant for action: " + action); |
| 359 | } |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 360 | resultState = ActionRequestCallback.RESULT_SUCCESS; |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 361 | } else { |
| 362 | Log.w(TAG, "Failed to launch active assistant."); |
| 363 | } |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 364 | callback.onResult(resultState); |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | private void handleFallback(StatusBarNotification sbn, String action, |
| 368 | ActionRequestCallback callback) { |
| 369 | FallbackAssistant.Listener listener = new FallbackAssistant.Listener() { |
| 370 | @Override |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 371 | public void onMessageRead(boolean hasError) { |
| 372 | String resultState = hasError ? ActionRequestCallback.RESULT_FAILED |
| 373 | : ActionRequestCallback.RESULT_SUCCESS; |
| 374 | // Only change the resultState if fallback failed, and assistant successfully |
| 375 | // alerted to prompt user for permissions. |
Ritwika Mitra | 3ebfef5 | 2019-06-03 09:56:02 -0700 | [diff] [blame] | 376 | if (hasActiveAssistant() && requestHandleMissingPermission() |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 377 | && resultState.equals(ActionRequestCallback.RESULT_FAILED)) { |
| 378 | resultState = ActionRequestCallback.RESULT_FAILED_WITH_ERROR_HANDLED; |
| 379 | } |
| 380 | callback.onResult(resultState); |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 381 | } |
| 382 | }; |
| 383 | |
| 384 | switch (action) { |
| 385 | case CarVoiceInteractionSession.VOICE_ACTION_READ_NOTIFICATION: |
| 386 | mFallbackAssistant.handleReadAction(sbn, listener); |
| 387 | break; |
| 388 | case CarVoiceInteractionSession.VOICE_ACTION_REPLY_NOTIFICATION: |
| 389 | mFallbackAssistant.handleErrorMessage(mErrorMessage, listener); |
| 390 | break; |
| 391 | default: |
| 392 | Log.w(TAG, "Requested unsupported FallbackAssistant action."); |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 393 | callback.onResult(ActionRequestCallback.RESULT_FAILED); |
Ritwika Mitra | 35968f8 | 2019-05-22 16:05:28 -0700 | [diff] [blame] | 394 | return; |
| 395 | } |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 396 | } |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 397 | |
| 398 | /** |
| 399 | * Requests the active voice service to handle the permissions missing error. |
| 400 | * |
| 401 | * @return {@code true} if active assistant was successfully alerted. |
| 402 | **/ |
Ritwika Mitra | 3ebfef5 | 2019-06-03 09:56:02 -0700 | [diff] [blame] | 403 | private boolean requestHandleMissingPermission() { |
Ritwika Mitra | 60ab82a | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 404 | Bundle payloadArguments = BundleBuilder |
| 405 | .buildAssistantHandleExceptionBundle( |
| 406 | EXCEPTION_NOTIFICATION_LISTENER_PERMISSIONS_MISSING); |
| 407 | boolean requestedSuccessfully = mAssistUtils.showSessionForActiveService(payloadArguments, |
| 408 | CarVoiceInteractionSession.SHOW_SOURCE_NOTIFICATION, null, null); |
| 409 | if (!requestedSuccessfully) { |
| 410 | Log.w(TAG, "Failed to alert assistant to request permissions from user"); |
| 411 | } |
| 412 | return requestedSuccessfully; |
| 413 | } |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 414 | } |