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 | 146ed85 | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Ritwika Mitra | 3eb6893 | 2019-04-29 16:40:04 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 24 | import android.app.Notification; |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 25 | import android.app.RemoteInput; |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.os.Bundle; |
| 28 | import android.provider.Settings; |
| 29 | import android.service.notification.StatusBarNotification; |
| 30 | import android.util.Log; |
| 31 | |
| 32 | import androidx.core.app.NotificationCompat; |
| 33 | |
| 34 | import com.android.car.assist.CarVoiceInteractionSession; |
| 35 | import com.android.internal.app.AssistUtils; |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 36 | import com.android.internal.app.IVoiceActionCheckCallback; |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 37 | |
Priyank Singh | 128ff29 | 2019-04-29 20:07:36 -0700 | [diff] [blame] | 38 | import java.util.ArrayList; |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 39 | import java.util.Arrays; |
| 40 | import java.util.Collections; |
| 41 | import java.util.HashSet; |
| 42 | import java.util.List; |
| 43 | import java.util.Objects; |
| 44 | import java.util.Set; |
| 45 | import java.util.stream.Collectors; |
| 46 | import java.util.stream.IntStream; |
| 47 | |
| 48 | /** |
| 49 | * Util class providing helper methods to interact with the current active voice service, |
| 50 | * while ensuring that the active voice service has the required permissions. |
| 51 | */ |
| 52 | public class CarAssistUtils { |
| 53 | public static final String TAG = "CarAssistUtils"; |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 54 | private static final List<Integer> REQUIRED_SEMANTIC_ACTIONS = Collections.unmodifiableList( |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 55 | Arrays.asList( |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 56 | SEMANTIC_ACTION_MARK_AS_READ |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 57 | ) |
| 58 | ); |
| 59 | |
Ritwika Mitra | aaf0c17 | 2019-01-30 08:25:03 -0800 | [diff] [blame] | 60 | private static final List<Integer> SUPPORTED_SEMANTIC_ACTIONS = Collections.unmodifiableList( |
| 61 | Arrays.asList( |
| 62 | SEMANTIC_ACTION_MARK_AS_READ, |
| 63 | SEMANTIC_ACTION_REPLY |
| 64 | ) |
| 65 | ); |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 66 | |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 67 | private final Context mContext; |
| 68 | private final AssistUtils mAssistUtils; |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 69 | private final FallbackAssistant mFallbackAssistant; |
| 70 | private final String mErrorMessage; |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 71 | |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 72 | /** Interface used to receive callbacks from voice action requests. */ |
| 73 | public interface ActionRequestCallback { |
| 74 | /** Callback issued from a voice request on success/error. */ |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 75 | void onResult(boolean hasError); |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 78 | public CarAssistUtils(Context context) { |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 79 | mContext = context; |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 80 | mAssistUtils = new AssistUtils(context); |
Ritwika Mitra | 146ed85 | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 81 | mFallbackAssistant = new FallbackAssistant(context); |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 82 | mErrorMessage = context.getString(R.string.assist_action_failed_toast); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns true if the current active assistant has notification listener permissions. |
| 87 | */ |
| 88 | public boolean assistantIsNotificationListener() { |
| 89 | final String activeComponent = mAssistUtils.getActiveServiceComponentName() |
| 90 | .flattenToString(); |
Ritwika Mitra | ac5f151 | 2019-02-04 10:21:14 -0800 | [diff] [blame] | 91 | int slashIndex = activeComponent.indexOf("/"); |
| 92 | final String activePackage = activeComponent.substring(0, slashIndex); |
| 93 | |
Ritwika Mitra | 3eb6893 | 2019-04-29 16:40:04 -0700 | [diff] [blame] | 94 | final String listeners = Settings.Secure.getStringForUser(mContext.getContentResolver(), |
| 95 | Settings.Secure.ENABLED_NOTIFICATION_LISTENERS, ActivityManager.getCurrentUser()); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 96 | |
Ritwika Mitra | f89855b | 2019-05-21 11:56:20 -0700 | [diff] [blame] | 97 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 98 | Log.d(TAG, "Current user: " + ActivityManager.getCurrentUser() |
| 99 | + " has active voice service: " + activePackage + " and enabled notification " |
| 100 | + " listeners: " + listeners); |
| 101 | } |
| 102 | |
Ritwika Mitra | ac5f151 | 2019-02-04 10:21:14 -0800 | [diff] [blame] | 103 | if (listeners != null) { |
| 104 | for (String listener : Arrays.asList(listeners.split(":"))) { |
| 105 | if (listener.contains(activePackage)) { |
| 106 | return true; |
| 107 | } |
| 108 | } |
| 109 | } |
Ritwika Mitra | 295d91a | 2019-04-26 09:55:11 -0700 | [diff] [blame] | 110 | Log.w(TAG, "No notification listeners found for assistant: " + activeComponent); |
Ritwika Mitra | ac5f151 | 2019-02-04 10:21:14 -0800 | [diff] [blame] | 111 | return false; |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Checks whether the notification is a car-compatible messaging notification. |
| 116 | * |
| 117 | * @param sbn The notification being checked. |
| 118 | * @return true if the notification is a car-compatible messaging notification. |
| 119 | */ |
| 120 | public static boolean isCarCompatibleMessagingNotification(StatusBarNotification sbn) { |
| 121 | return hasMessagingStyle(sbn) |
| 122 | && hasRequiredAssistantCallbacks(sbn) |
| 123 | && replyCallbackHasRemoteInput(sbn) |
| 124 | && assistantCallbacksShowNoUi(sbn); |
| 125 | } |
| 126 | |
| 127 | /** Returns true if the semantic action provided can be supported. */ |
| 128 | public static boolean isSupportedSemanticAction(int semanticAction) { |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 129 | return SUPPORTED_SEMANTIC_ACTIONS.contains(semanticAction); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns true if the notification has a messaging style. |
| 134 | * <p/> |
| 135 | * This is the case if the notification in question was provided an instance of |
| 136 | * {@link Notification.MessagingStyle} (or an instance of |
| 137 | * {@link NotificationCompat.MessagingStyle} if {@link NotificationCompat} was used). |
| 138 | */ |
| 139 | private static boolean hasMessagingStyle(StatusBarNotification sbn) { |
| 140 | return NotificationCompat.MessagingStyle |
| 141 | .extractMessagingStyleFromNotification(sbn.getNotification()) != null; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns true if the notification has the required Assistant callbacks to be considered |
| 146 | * a car-compatible messaging notification. The callbacks must be unambiguous, therefore false |
| 147 | * is returned if multiple callbacks exist for any semantic action that is supported. |
| 148 | */ |
| 149 | private static boolean hasRequiredAssistantCallbacks(StatusBarNotification sbn) { |
Priyank Singh | 128ff29 | 2019-04-29 20:07:36 -0700 | [diff] [blame] | 150 | List<Integer> semanticActionList = getAllActions(sbn.getNotification()) |
| 151 | .stream() |
| 152 | .map(NotificationCompat.Action::getSemanticAction) |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 153 | .filter(REQUIRED_SEMANTIC_ACTIONS::contains) |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 154 | .collect(Collectors.toList()); |
| 155 | Set<Integer> semanticActionSet = new HashSet<>(semanticActionList); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 156 | return semanticActionList.size() == semanticActionSet.size() |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 157 | && semanticActionSet.containsAll(REQUIRED_SEMANTIC_ACTIONS); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Priyank Singh | 128ff29 | 2019-04-29 20:07:36 -0700 | [diff] [blame] | 160 | /** Retrieves all visible and invisible {@link Action}s from the {@link #notification}. */ |
Ritwika Mitra | 146ed85 | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 161 | public static List<NotificationCompat.Action> getAllActions(Notification notification) { |
Priyank Singh | 128ff29 | 2019-04-29 20:07:36 -0700 | [diff] [blame] | 162 | List<NotificationCompat.Action> actions = new ArrayList<>(); |
| 163 | actions.addAll(NotificationCompat.getInvisibleActions(notification)); |
| 164 | for (int i = 0; i < NotificationCompat.getActionCount(notification); i++) { |
| 165 | actions.add(NotificationCompat.getAction(notification, i)); |
| 166 | } |
| 167 | return actions; |
| 168 | } |
| 169 | |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 170 | /** |
Ritwika Mitra | 146ed85 | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 171 | * Retrieves the {@link NotificationCompat.Action} containing the |
| 172 | * {@link NotificationCompat.Action#SEMANTIC_ACTION_MARK_AS_READ} semantic action. |
| 173 | */ |
| 174 | @Nullable |
| 175 | public static NotificationCompat.Action getMarkAsReadAction(Notification notification) { |
| 176 | for (NotificationCompat.Action action : getAllActions(notification)) { |
| 177 | if (action.getSemanticAction() |
| 178 | == NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ) { |
| 179 | return action; |
| 180 | } |
| 181 | } |
| 182 | return null; |
| 183 | } |
| 184 | |
| 185 | /** |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 186 | * Returns true if the reply callback has at least one {@link RemoteInput}. |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 187 | * <p/> |
| 188 | * Precondition: There exists only one reply callback. |
| 189 | */ |
| 190 | private static boolean replyCallbackHasRemoteInput(StatusBarNotification sbn) { |
| 191 | return Arrays.stream(sbn.getNotification().actions) |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 192 | .filter(action -> action.getSemanticAction() == SEMANTIC_ACTION_REPLY) |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 193 | .map(Notification.Action::getRemoteInputs) |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 194 | .filter(Objects::nonNull) |
| 195 | .anyMatch(remoteInputs -> remoteInputs.length > 0); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** Returns true if all Assistant callbacks indicate that they show no UI, false otherwise. */ |
| 199 | private static boolean assistantCallbacksShowNoUi(StatusBarNotification sbn) { |
| 200 | final Notification notification = sbn.getNotification(); |
| 201 | return IntStream.range(0, notification.actions.length) |
| 202 | .mapToObj(i -> NotificationCompat.getAction(notification, i)) |
| 203 | .filter(Objects::nonNull) |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 204 | .filter(action -> SUPPORTED_SEMANTIC_ACTIONS.contains(action.getSemanticAction())) |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 205 | .noneMatch(NotificationCompat.Action::getShowsUserInterface); |
| 206 | } |
| 207 | |
| 208 | /** |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 209 | * Requests a given action from the current active Assistant. |
| 210 | * |
Ritwika Mitra | 146ed85 | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 211 | * @param sbn the notification payload to deliver to assistant |
| 212 | * @param voiceAction must be a valid {@link CarVoiceInteractionSession} VOICE_ACTION |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 213 | * @param callback the callback to issue on success/error |
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 | public void requestAssistantVoiceAction(StatusBarNotification sbn, String voiceAction, |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 216 | ActionRequestCallback callback) { |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 217 | if (!isCarCompatibleMessagingNotification(sbn)) { |
| 218 | Log.w(TAG, "Assistant action requested for non-compatible notification."); |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 219 | callback.onResult(/* hasError= */ true); |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 220 | return; |
| 221 | } |
| 222 | |
Ritwika Mitra | 146ed85 | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 223 | switch (voiceAction) { |
| 224 | case CarVoiceInteractionSession.VOICE_ACTION_READ_NOTIFICATION: |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 225 | readMessageNotification(sbn, callback); |
| 226 | return; |
Ritwika Mitra | 146ed85 | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 227 | case CarVoiceInteractionSession.VOICE_ACTION_REPLY_NOTIFICATION: |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 228 | replyMessageNotification(sbn, callback); |
| 229 | return; |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 230 | default: |
Ritwika Mitra | 146ed85 | 2019-05-01 10:33:06 -0700 | [diff] [blame] | 231 | Log.w(TAG, "Requested Assistant action for unsupported semantic action."); |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 232 | callback.onResult(/* hasError= */ true); |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 233 | return; |
Uriel Sade | d81ddc9 | 2018-12-21 15:16:56 -0800 | [diff] [blame] | 234 | } |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Requests a read action for the notification from the current active Assistant. |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 239 | * 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] | 240 | * handle it. |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 241 | * |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 242 | * @param sbn the notification to deliver as the payload |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 243 | * @param callback the callback to issue on success/error |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 244 | */ |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 245 | private void readMessageNotification(StatusBarNotification sbn, |
| 246 | ActionRequestCallback callback) { |
| 247 | Bundle args = BundleBuilder.buildAssistantReadBundle(sbn); |
| 248 | String action = CarVoiceInteractionSession.VOICE_ACTION_READ_NOTIFICATION; |
| 249 | |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 250 | requestAction(action, sbn, args, callback); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Requests a reply action for the notification from the current active Assistant. |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 255 | * 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] | 256 | * handle it. |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 257 | * |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 258 | * @param sbn the notification to deliver as the payload |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 259 | * @param callback the callback to issue on success/error |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 260 | */ |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 261 | private void replyMessageNotification(StatusBarNotification sbn, |
| 262 | ActionRequestCallback callback) { |
| 263 | Bundle args = BundleBuilder.buildAssistantReplyBundle(sbn); |
| 264 | String action = CarVoiceInteractionSession.VOICE_ACTION_REPLY_NOTIFICATION; |
| 265 | |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 266 | requestAction(action, sbn, args, callback); |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 269 | private void requestAction(String action, StatusBarNotification sbn, Bundle payloadArguments, |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 270 | ActionRequestCallback callback) { |
| 271 | |
| 272 | if (!assistantIsNotificationListener()) { |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 273 | handleFallback(sbn, action, callback); |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 274 | return; |
| 275 | } |
| 276 | |
| 277 | IVoiceActionCheckCallback actionCheckCallback = new IVoiceActionCheckCallback.Stub() { |
| 278 | @Override |
| 279 | public void onComplete(List<String> supportedActions) { |
| 280 | boolean success; |
| 281 | if (supportedActions != null && supportedActions.contains(action)) { |
| 282 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 283 | Log.d(TAG, "Launching active Assistant for action: " + action); |
| 284 | } |
| 285 | success = mAssistUtils.showSessionForActiveService(payloadArguments, |
| 286 | SHOW_SOURCE_NOTIFICATION, null, null); |
| 287 | } else { |
| 288 | Log.w(TAG, "Active Assistant does not support voice action: " + action); |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 289 | success = false; |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 290 | } |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 291 | callback.onResult(/* hasError= */ !success); |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 292 | } |
| 293 | }; |
| 294 | |
| 295 | Set<String> actionSet = new HashSet<>(Collections.singletonList(action)); |
| 296 | mAssistUtils.getActiveServiceSupportedActions(actionSet, actionCheckCallback); |
| 297 | } |
| 298 | |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 299 | private void handleFallback(StatusBarNotification sbn, String action, |
| 300 | ActionRequestCallback callback) { |
| 301 | FallbackAssistant.Listener listener = new FallbackAssistant.Listener() { |
| 302 | @Override |
| 303 | public void onMessageRead(boolean error) { |
| 304 | callback.onResult(error); |
| 305 | } |
| 306 | }; |
| 307 | |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 308 | switch (action) { |
| 309 | case CarVoiceInteractionSession.VOICE_ACTION_READ_NOTIFICATION: |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 310 | mFallbackAssistant.handleReadAction(sbn, listener); |
| 311 | break; |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 312 | case CarVoiceInteractionSession.VOICE_ACTION_REPLY_NOTIFICATION: |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 313 | mFallbackAssistant.handleErrorMessage(mErrorMessage, listener); |
| 314 | break; |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 315 | default: |
Ritwika Mitra | eeb910c | 2019-05-23 12:32:06 -0700 | [diff] [blame^] | 316 | Log.w(TAG, "Requested unsupported FallbackAssistant action."); |
| 317 | callback.onResult(/* hasError= */ true); |
| 318 | return; |
Uriel Sade | 3703dbd | 2019-01-16 12:55:19 -0800 | [diff] [blame] | 319 | } |
Uriel Sade | 298f6c0 | 2018-12-19 20:34:43 -0800 | [diff] [blame] | 320 | } |
| 321 | } |