blob: b6a48e2ed087e659692bd565b05d3ed905f721ac [file] [log] [blame]
Uriel Sade298f6c02018-12-19 20:34:43 -08001/*
Uriel Saded81ddc92018-12-21 15:16:56 -08002 * Copyright (C) 2019 The Android Open Source Project
Uriel Sade298f6c02018-12-19 20:34:43 -08003 *
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 */
16package com.android.car.assist.client;
17
Uriel Saded81ddc92018-12-21 15:16:56 -080018import static android.app.Notification.Action.SEMANTIC_ACTION_MARK_AS_READ;
19import static android.app.Notification.Action.SEMANTIC_ACTION_REPLY;
20
Uriel Sade298f6c02018-12-19 20:34:43 -080021import android.app.Notification;
Uriel Saded81ddc92018-12-21 15:16:56 -080022import android.app.RemoteInput;
Uriel Sade298f6c02018-12-19 20:34:43 -080023import android.content.Context;
24import android.os.Bundle;
25import android.provider.Settings;
26import android.service.notification.StatusBarNotification;
27import android.util.Log;
Uriel Saded81ddc92018-12-21 15:16:56 -080028import android.widget.Toast;
Uriel Sade298f6c02018-12-19 20:34:43 -080029
30import androidx.core.app.NotificationCompat;
31
32import com.android.car.assist.CarVoiceInteractionSession;
Uriel Saded81ddc92018-12-21 15:16:56 -080033import com.android.car.assist.client.tts.TextToSpeechHelper;
Uriel Sade298f6c02018-12-19 20:34:43 -080034import com.android.internal.app.AssistUtils;
35
36import java.util.Arrays;
37import java.util.Collections;
38import java.util.HashSet;
39import java.util.List;
40import java.util.Objects;
41import java.util.Set;
42import java.util.stream.Collectors;
43import java.util.stream.IntStream;
44
45/**
46 * Util class providing helper methods to interact with the current active voice service,
47 * while ensuring that the active voice service has the required permissions.
48 */
49public class CarAssistUtils {
50 public static final String TAG = "CarAssistUtils";
Uriel Saded81ddc92018-12-21 15:16:56 -080051 private static final List<Integer> REQUIRED_SEMANTIC_ACTIONS = Collections.unmodifiableList(
Uriel Sade298f6c02018-12-19 20:34:43 -080052 Arrays.asList(
Uriel Saded81ddc92018-12-21 15:16:56 -080053 SEMANTIC_ACTION_MARK_AS_READ
Uriel Sade298f6c02018-12-19 20:34:43 -080054 )
55 );
56
Ritwika Mitraaaf0c172019-01-30 08:25:03 -080057 private static final List<Integer> SUPPORTED_SEMANTIC_ACTIONS = Collections.unmodifiableList(
58 Arrays.asList(
59 SEMANTIC_ACTION_MARK_AS_READ,
60 SEMANTIC_ACTION_REPLY
61 )
62 );
Uriel Saded81ddc92018-12-21 15:16:56 -080063
64 private final TextToSpeechHelper.Listener mListener = new TextToSpeechHelper.Listener() {
65 @Override
66 public void onTextToSpeechStarted() {
67 if (Log.isLoggable(TAG, Log.DEBUG)) {
68 Log.d(TAG, "onTextToSpeechStarted");
69 }
70 }
71
72 @Override
73 public void onTextToSpeechStopped(boolean error) {
74 if (Log.isLoggable(TAG, Log.DEBUG)) {
75 Log.d(TAG, "onTextToSpeechStopped");
76 }
77 if (error) {
78 Toast.makeText(mContext, mErrorMessage, Toast.LENGTH_LONG).show();
79 }
80 }
81 };
Uriel Sade298f6c02018-12-19 20:34:43 -080082
83 private final Context mContext;
84 private final AssistUtils mAssistUtils;
Uriel Saded81ddc92018-12-21 15:16:56 -080085 private final FallbackAssistant mFallbackAssistant;
86 private final String mErrorMessage;
Uriel Sade298f6c02018-12-19 20:34:43 -080087
88 public CarAssistUtils(Context context) {
Uriel Sade298f6c02018-12-19 20:34:43 -080089 mContext = context;
Uriel Saded81ddc92018-12-21 15:16:56 -080090 mAssistUtils = new AssistUtils(context);
91 mFallbackAssistant = new FallbackAssistant(new TextToSpeechHelper(context));
92 mErrorMessage = context.getString(R.string.assist_action_failed_toast);
Uriel Sade298f6c02018-12-19 20:34:43 -080093 }
94
95 /**
96 * Returns true if the current active assistant has notification listener permissions.
97 */
98 public boolean assistantIsNotificationListener() {
99 final String activeComponent = mAssistUtils.getActiveServiceComponentName()
100 .flattenToString();
Ritwika Mitraeb040682019-02-01 15:36:23 -0800101 int slashIndex = activeComponent.indexOf("/");
102 final String activePackage = activeComponent.substring(0, slashIndex);
103
Uriel Sade298f6c02018-12-19 20:34:43 -0800104 final String listeners = Settings.Secure.getString(mContext.getContentResolver(),
105 Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
106
Ritwika Mitraeb040682019-02-01 15:36:23 -0800107 if (listeners != null) {
108 for (String listener : Arrays.asList(listeners.split(":"))) {
109 if (listener.contains(activePackage)) {
110 return true;
111 }
112 }
113 }
114
115 return false;
Uriel Sade298f6c02018-12-19 20:34:43 -0800116 }
117
118 /**
119 * Checks whether the notification is a car-compatible messaging notification.
120 *
121 * @param sbn The notification being checked.
122 * @return true if the notification is a car-compatible messaging notification.
123 */
124 public static boolean isCarCompatibleMessagingNotification(StatusBarNotification sbn) {
125 return hasMessagingStyle(sbn)
126 && hasRequiredAssistantCallbacks(sbn)
127 && replyCallbackHasRemoteInput(sbn)
128 && assistantCallbacksShowNoUi(sbn);
129 }
130
131 /** Returns true if the semantic action provided can be supported. */
132 public static boolean isSupportedSemanticAction(int semanticAction) {
Uriel Saded81ddc92018-12-21 15:16:56 -0800133 return SUPPORTED_SEMANTIC_ACTIONS.contains(semanticAction);
Uriel Sade298f6c02018-12-19 20:34:43 -0800134 }
135
136 /**
137 * Returns true if the notification has a messaging style.
138 * <p/>
139 * This is the case if the notification in question was provided an instance of
140 * {@link Notification.MessagingStyle} (or an instance of
141 * {@link NotificationCompat.MessagingStyle} if {@link NotificationCompat} was used).
142 */
143 private static boolean hasMessagingStyle(StatusBarNotification sbn) {
144 return NotificationCompat.MessagingStyle
145 .extractMessagingStyleFromNotification(sbn.getNotification()) != null;
146 }
147
148 /**
149 * Returns true if the notification has the required Assistant callbacks to be considered
150 * a car-compatible messaging notification. The callbacks must be unambiguous, therefore false
151 * is returned if multiple callbacks exist for any semantic action that is supported.
152 */
153 private static boolean hasRequiredAssistantCallbacks(StatusBarNotification sbn) {
154 List<Integer> semanticActionList = Arrays.stream(sbn.getNotification().actions)
155 .map(Notification.Action::getSemanticAction)
Uriel Saded81ddc92018-12-21 15:16:56 -0800156 .filter(REQUIRED_SEMANTIC_ACTIONS::contains)
Uriel Sade298f6c02018-12-19 20:34:43 -0800157 .collect(Collectors.toList());
158 Set<Integer> semanticActionSet = new HashSet<>(semanticActionList);
159
160 return semanticActionList.size() == semanticActionSet.size()
Uriel Saded81ddc92018-12-21 15:16:56 -0800161 && semanticActionSet.containsAll(REQUIRED_SEMANTIC_ACTIONS);
Uriel Sade298f6c02018-12-19 20:34:43 -0800162 }
163
164 /**
Uriel Saded81ddc92018-12-21 15:16:56 -0800165 * Returns true if the reply callback has at least one {@link RemoteInput}.
Uriel Sade298f6c02018-12-19 20:34:43 -0800166 * <p/>
167 * Precondition: There exists only one reply callback.
168 */
169 private static boolean replyCallbackHasRemoteInput(StatusBarNotification sbn) {
170 return Arrays.stream(sbn.getNotification().actions)
Uriel Saded81ddc92018-12-21 15:16:56 -0800171 .filter(action -> action.getSemanticAction() == SEMANTIC_ACTION_REPLY)
Uriel Sade298f6c02018-12-19 20:34:43 -0800172 .map(Notification.Action::getRemoteInputs)
Uriel Saded81ddc92018-12-21 15:16:56 -0800173 .filter(Objects::nonNull)
174 .anyMatch(remoteInputs -> remoteInputs.length > 0);
Uriel Sade298f6c02018-12-19 20:34:43 -0800175 }
176
177 /** Returns true if all Assistant callbacks indicate that they show no UI, false otherwise. */
178 private static boolean assistantCallbacksShowNoUi(StatusBarNotification sbn) {
179 final Notification notification = sbn.getNotification();
180 return IntStream.range(0, notification.actions.length)
181 .mapToObj(i -> NotificationCompat.getAction(notification, i))
182 .filter(Objects::nonNull)
Uriel Saded81ddc92018-12-21 15:16:56 -0800183 .filter(action -> SUPPORTED_SEMANTIC_ACTIONS.contains(action.getSemanticAction()))
Uriel Sade298f6c02018-12-19 20:34:43 -0800184 .noneMatch(NotificationCompat.Action::getShowsUserInterface);
185 }
186
187 /**
Uriel Saded81ddc92018-12-21 15:16:56 -0800188 * Requests a given action from the current active Assistant.
189 *
Ritwika Mitraeb040682019-02-01 15:36:23 -0800190 * @param sbn the notification payload to deliver to assistant
Uriel Sade298f6c02018-12-19 20:34:43 -0800191 * @param semanticAction the semantic action that is to be requested
192 * @return true if the request was successful
193 */
Uriel Saded81ddc92018-12-21 15:16:56 -0800194 public boolean requestAssistantVoiceAction(StatusBarNotification sbn, int semanticAction) {
195 if (!isCarCompatibleMessagingNotification(sbn)) {
196 Log.w(TAG, "Assistant action requested for non-compatible notification.");
197 return false;
Uriel Sade298f6c02018-12-19 20:34:43 -0800198 }
199
Uriel Saded81ddc92018-12-21 15:16:56 -0800200 switch (semanticAction) {
201 case SEMANTIC_ACTION_MARK_AS_READ:
202 return readMessageNotification(sbn);
203 case SEMANTIC_ACTION_REPLY:
204 return replyMessageNotification(sbn);
205 default:
206 return false;
207 }
Uriel Sade298f6c02018-12-19 20:34:43 -0800208 }
209
210 /**
211 * Requests a read action for the notification from the current active Assistant.
Uriel Saded81ddc92018-12-21 15:16:56 -0800212 * If the Assistant is cannot handle the request, a fallback implementation will attempt to
213 * handle it.
Uriel Sade298f6c02018-12-19 20:34:43 -0800214 *
215 * @param sbn the notification to deliver as the payload
Uriel Saded81ddc92018-12-21 15:16:56 -0800216 * @return true if the read request was handled successfully
Uriel Sade298f6c02018-12-19 20:34:43 -0800217 */
218 private boolean readMessageNotification(StatusBarNotification sbn) {
Uriel Saded81ddc92018-12-21 15:16:56 -0800219 return requestAction(BundleBuilder.buildAssistantReadBundle(sbn))
220 || mFallbackAssistant.handleReadAction(sbn, mListener);
Uriel Sade298f6c02018-12-19 20:34:43 -0800221 }
222
223 /**
224 * Requests a reply action for the notification from the current active Assistant.
Uriel Saded81ddc92018-12-21 15:16:56 -0800225 * If the Assistant is cannot handle the request, a fallback implementation will attempt to
226 * handle it.
Uriel Sade298f6c02018-12-19 20:34:43 -0800227 *
228 * @param sbn the notification to deliver as the payload
Uriel Saded81ddc92018-12-21 15:16:56 -0800229 * @return true if the reply request was handled successfully
Uriel Sade298f6c02018-12-19 20:34:43 -0800230 */
231 private boolean replyMessageNotification(StatusBarNotification sbn) {
Uriel Saded81ddc92018-12-21 15:16:56 -0800232 return requestAction(BundleBuilder.buildAssistantReplyBundle(sbn))
233 || mFallbackAssistant.handleErrorMessage(mErrorMessage, mListener);
Uriel Sade298f6c02018-12-19 20:34:43 -0800234 }
235
Uriel Saded81ddc92018-12-21 15:16:56 -0800236 private boolean requestAction(Bundle payloadArguments) {
237 return assistantIsNotificationListener()
Uriel Sade298f6c02018-12-19 20:34:43 -0800238 && mAssistUtils.showSessionForActiveService(payloadArguments,
239 CarVoiceInteractionSession.SHOW_SOURCE_NOTIFICATION, null, null);
240 }
241}