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) |
| 166 | && replyCallbackHasRemoteInput(sbn) |
| 167 | && assistantCallbacksShowNoUi(sbn); |
| 168 | } |
| 169 | |
| 170 | /** Returns true if the semantic action provided can be supported. */ |
| 171 | public static boolean isSupportedSemanticAction(int semanticAction) { |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 172 | return SUPPORTED_SEMANTIC_ACTIONS.contains(semanticAction); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Returns true if the notification has a messaging style. |
| 177 | * <p/> |
| 178 | * This is the case if the notification in question was provided an instance of |
| 179 | * {@link Notification.MessagingStyle} (or an instance of |
| 180 | * {@link NotificationCompat.MessagingStyle} if {@link NotificationCompat} was used). |
| 181 | */ |
| 182 | private static boolean hasMessagingStyle(StatusBarNotification sbn) { |
| 183 | return NotificationCompat.MessagingStyle |
| 184 | .extractMessagingStyleFromNotification(sbn.getNotification()) != null; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Returns true if the notification has the required Assistant callbacks to be considered |
| 189 | * a car-compatible messaging notification. The callbacks must be unambiguous, therefore false |
| 190 | * is returned if multiple callbacks exist for any semantic action that is supported. |
| 191 | */ |
| 192 | private static boolean hasRequiredAssistantCallbacks(StatusBarNotification sbn) { |
Priyank Singh | 128ff29 | 2019-04-29 20:07:36 -0700 | [diff] [blame] | 193 | List<Integer> semanticActionList = getAllActions(sbn.getNotification()) |
| 194 | .stream() |
| 195 | .map(NotificationCompat.Action::getSemanticAction) |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 196 | .filter(REQUIRED_SEMANTIC_ACTIONS::contains) |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 197 | .collect(Collectors.toList()); |
| 198 | Set<Integer> semanticActionSet = new HashSet<>(semanticActionList); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 199 | return semanticActionList.size() == semanticActionSet.size() |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 200 | && semanticActionSet.containsAll(REQUIRED_SEMANTIC_ACTIONS); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Priyank Singh | 128ff29 | 2019-04-29 20:07:36 -0700 | [diff] [blame] | 203 | /** Retrieves all visible and invisible {@link Action}s from the {@link #notification}. */ |
Ritwika Mitra | 146ed85 | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 204 | public static List<NotificationCompat.Action> getAllActions(Notification notification) { |
Priyank Singh | 128ff29 | 2019-04-29 20:07:36 -0700 | [diff] [blame] | 205 | List<NotificationCompat.Action> actions = new ArrayList<>(); |
| 206 | actions.addAll(NotificationCompat.getInvisibleActions(notification)); |
| 207 | for (int i = 0; i < NotificationCompat.getActionCount(notification); i++) { |
| 208 | actions.add(NotificationCompat.getAction(notification, i)); |
| 209 | } |
| 210 | return actions; |
| 211 | } |
| 212 | |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 213 | /** |
Ritwika Mitra | 146ed85 | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 214 | * Retrieves the {@link NotificationCompat.Action} containing the |
| 215 | * {@link NotificationCompat.Action#SEMANTIC_ACTION_MARK_AS_READ} semantic action. |
| 216 | */ |
| 217 | @Nullable |
| 218 | public static NotificationCompat.Action getMarkAsReadAction(Notification notification) { |
| 219 | for (NotificationCompat.Action action : getAllActions(notification)) { |
| 220 | if (action.getSemanticAction() |
| 221 | == NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ) { |
| 222 | return action; |
| 223 | } |
| 224 | } |
| 225 | return null; |
| 226 | } |
| 227 | |
| 228 | /** |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 229 | * Returns true if the reply callback has at least one {@link RemoteInput}. |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 230 | * <p/> |
| 231 | * Precondition: There exists only one reply callback. |
| 232 | */ |
| 233 | private static boolean replyCallbackHasRemoteInput(StatusBarNotification sbn) { |
| 234 | return Arrays.stream(sbn.getNotification().actions) |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 235 | .filter(action -> action.getSemanticAction() == SEMANTIC_ACTION_REPLY) |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 236 | .map(Notification.Action::getRemoteInputs) |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 237 | .filter(Objects::nonNull) |
| 238 | .anyMatch(remoteInputs -> remoteInputs.length > 0); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /** Returns true if all Assistant callbacks indicate that they show no UI, false otherwise. */ |
| 242 | private static boolean assistantCallbacksShowNoUi(StatusBarNotification sbn) { |
| 243 | final Notification notification = sbn.getNotification(); |
| 244 | return IntStream.range(0, notification.actions.length) |
| 245 | .mapToObj(i -> NotificationCompat.getAction(notification, i)) |
| 246 | .filter(Objects::nonNull) |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 247 | .filter(action -> SUPPORTED_SEMANTIC_ACTIONS.contains(action.getSemanticAction())) |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 248 | .noneMatch(NotificationCompat.Action::getShowsUserInterface); |
| 249 | } |
| 250 | |
| 251 | /** |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 252 | * Requests a given action from the current active Assistant. |
| 253 | * |
Ritwika Mitra | 146ed85 | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 254 | * @param sbn the notification payload to deliver to assistant |
| 255 | * @param voiceAction must be a valid {@link CarVoiceInteractionSession} VOICE_ACTION |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame] | 256 | * @param callback the callback to issue on success/error |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 257 | */ |
Ritwika Mitra | 146ed85 | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 258 | public void requestAssistantVoiceAction(StatusBarNotification sbn, String voiceAction, |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 259 | ActionRequestCallback callback) { |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 260 | if (!isCarCompatibleMessagingNotification(sbn)) { |
| 261 | Log.w(TAG, "Assistant action requested for non-compatible notification."); |
Ritwika Mitra | 2f0ffb2 | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 262 | callback.onResult(ActionRequestCallback.RESULT_FAILED); |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | |
Ritwika Mitra | 146ed85 | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 266 | switch (voiceAction) { |
| 267 | case CarVoiceInteractionSession.VOICE_ACTION_READ_NOTIFICATION: |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 268 | readMessageNotification(sbn, callback); |
| 269 | return; |
Ritwika Mitra | 146ed85 | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 270 | case CarVoiceInteractionSession.VOICE_ACTION_REPLY_NOTIFICATION: |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 271 | replyMessageNotification(sbn, callback); |
| 272 | return; |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 273 | default: |
Ritwika Mitra | 146ed85 | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 274 | Log.w(TAG, "Requested Assistant action for unsupported semantic action."); |
Ritwika Mitra | 2f0ffb2 | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 275 | callback.onResult(ActionRequestCallback.RESULT_FAILED); |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 276 | return; |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 277 | } |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Requests a read action for the notification from the current active Assistant. |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame] | 282 | * 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] | 283 | * handle it. |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 284 | * |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame] | 285 | * @param sbn the notification to deliver as the payload |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 286 | * @param callback the callback to issue on success/error |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 287 | */ |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 288 | private void readMessageNotification(StatusBarNotification sbn, |
| 289 | ActionRequestCallback callback) { |
| 290 | Bundle args = BundleBuilder.buildAssistantReadBundle(sbn); |
| 291 | String action = CarVoiceInteractionSession.VOICE_ACTION_READ_NOTIFICATION; |
| 292 | |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame] | 293 | requestAction(action, sbn, args, callback); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Requests a reply 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 replyMessageNotification(StatusBarNotification sbn, |
| 305 | ActionRequestCallback callback) { |
| 306 | Bundle args = BundleBuilder.buildAssistantReplyBundle(sbn); |
| 307 | String action = CarVoiceInteractionSession.VOICE_ACTION_REPLY_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 | |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame] | 312 | private void requestAction(String action, StatusBarNotification sbn, Bundle payloadArguments, |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 313 | ActionRequestCallback callback) { |
| 314 | |
Ritwika Mitra | 2f0ffb2 | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 315 | if (!hasActiveAssistant()) { |
| 316 | if (mIsFallbackAssistantEnabled) { |
| 317 | handleFallback(sbn, action, callback); |
| 318 | } else { |
| 319 | // If there is no active assistant, and fallback assistant is not enabled, then |
| 320 | // there is nothing for us to do. |
| 321 | callback.onResult(ActionRequestCallback.RESULT_FAILED); |
| 322 | } |
| 323 | return; |
| 324 | } |
| 325 | |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 326 | if (!assistantIsNotificationListener()) { |
Ritwika Mitra | 2f0ffb2 | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 327 | if (mIsFallbackAssistantEnabled) { |
| 328 | handleFallback(sbn, action, callback); |
| 329 | } else { |
| 330 | // If there is an active assistant, alert them to request permissions. |
Ritwika Mitra | 22d50c9 | 2019-05-31 10:24:48 -0700 | [diff] [blame^] | 331 | Bundle handleExceptionBundle = BundleBuilder |
| 332 | .buildAssistantHandleExceptionBundle( |
| 333 | EXCEPTION_NOTIFICATION_LISTENER_PERMISSIONS_MISSING); |
| 334 | fireAssistantAction(CarVoiceInteractionSession.VOICE_ACTION_HANDLE_EXCEPTION, |
| 335 | handleExceptionBundle, callback); |
Ritwika Mitra | 2f0ffb2 | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 336 | } |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 337 | return; |
| 338 | } |
| 339 | |
Ritwika Mitra | 22d50c9 | 2019-05-31 10:24:48 -0700 | [diff] [blame^] | 340 | fireAssistantAction(action, payloadArguments, callback); |
| 341 | } |
| 342 | |
| 343 | private void fireAssistantAction(String action, Bundle payloadArguments, |
| 344 | ActionRequestCallback callback) { |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 345 | IVoiceActionCheckCallback actionCheckCallback = new IVoiceActionCheckCallback.Stub() { |
| 346 | @Override |
| 347 | public void onComplete(List<String> supportedActions) { |
Ritwika Mitra | 2f0ffb2 | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 348 | String resultState = ActionRequestCallback.RESULT_FAILED; |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 349 | if (supportedActions != null && supportedActions.contains(action)) { |
| 350 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 351 | Log.d(TAG, "Launching active Assistant for action: " + action); |
| 352 | } |
Ritwika Mitra | 2f0ffb2 | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 353 | if (mAssistUtils.showSessionForActiveService(payloadArguments, |
| 354 | SHOW_SOURCE_NOTIFICATION, null, null)) { |
| 355 | resultState = ActionRequestCallback.RESULT_SUCCESS; |
| 356 | } |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 357 | } else { |
| 358 | Log.w(TAG, "Active Assistant does not support voice action: " + action); |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 359 | } |
Ritwika Mitra | 2f0ffb2 | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 360 | callback.onResult(resultState); |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 361 | } |
| 362 | }; |
| 363 | |
| 364 | Set<String> actionSet = new HashSet<>(Collections.singletonList(action)); |
| 365 | mAssistUtils.getActiveServiceSupportedActions(actionSet, actionCheckCallback); |
| 366 | } |
| 367 | |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame] | 368 | private void handleFallback(StatusBarNotification sbn, String action, |
| 369 | ActionRequestCallback callback) { |
| 370 | FallbackAssistant.Listener listener = new FallbackAssistant.Listener() { |
| 371 | @Override |
Ritwika Mitra | 2f0ffb2 | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 372 | public void onMessageRead(boolean hasError) { |
Ritwika Mitra | 22d50c9 | 2019-05-31 10:24:48 -0700 | [diff] [blame^] | 373 | // Tracks if the FallbackAssistant successfully handled the action. |
| 374 | final String fallbackActionResult = hasError ? ActionRequestCallback.RESULT_FAILED |
Ritwika Mitra | 2f0ffb2 | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 375 | : ActionRequestCallback.RESULT_SUCCESS; |
Ritwika Mitra | 22d50c9 | 2019-05-31 10:24:48 -0700 | [diff] [blame^] | 376 | if (hasActiveAssistant()) { |
| 377 | fireAssistantAction(CarVoiceInteractionSession.VOICE_ACTION_HANDLE_EXCEPTION, |
| 378 | null, new ActionRequestCallback() { |
| 379 | @Override |
| 380 | public void onResult(String requestActionFromAssistantResult) { |
| 381 | if (fallbackActionResult.equals( |
| 382 | ActionRequestCallback.RESULT_FAILED) |
| 383 | && requestActionFromAssistantResult |
| 384 | == ActionRequestCallback.RESULT_SUCCESS) { |
| 385 | // Only change the callback.ResultState if fallback failed, |
| 386 | // and assistant session is shown. |
| 387 | callback.onResult( |
| 388 | ActionRequestCallback |
| 389 | .RESULT_FAILED_WITH_ERROR_HANDLED); |
| 390 | } else { |
| 391 | callback.onResult(fallbackActionResult); |
| 392 | } |
| 393 | } |
| 394 | }); |
| 395 | } else { |
| 396 | callback.onResult(fallbackActionResult); |
Ritwika Mitra | 2f0ffb2 | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 397 | } |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame] | 398 | } |
| 399 | }; |
| 400 | |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 401 | switch (action) { |
| 402 | case CarVoiceInteractionSession.VOICE_ACTION_READ_NOTIFICATION: |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame] | 403 | mFallbackAssistant.handleReadAction(sbn, listener); |
| 404 | break; |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 405 | case CarVoiceInteractionSession.VOICE_ACTION_REPLY_NOTIFICATION: |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame] | 406 | mFallbackAssistant.handleErrorMessage(mErrorMessage, listener); |
| 407 | break; |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 408 | default: |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame] | 409 | Log.w(TAG, "Requested unsupported FallbackAssistant action."); |
Ritwika Mitra | 2f0ffb2 | 2019-05-23 10:10:00 -0700 | [diff] [blame] | 410 | callback.onResult(ActionRequestCallback.RESULT_FAILED); |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame] | 411 | return; |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 412 | } |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 413 | } |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 414 | } |