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