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