• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2021 The Android Open Source Project
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 
17 package com.android.car.voicecontrol;
18 
19 import android.app.PendingIntent;
20 import android.bluetooth.BluetoothDevice;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.util.Log;
24 
25 import com.android.car.assist.CarVoiceInteractionSession;
26 import com.android.car.telephony.common.PhoneNumber;
27 
28 import java.util.List;
29 
30 /**
31  * Utility class for Direct Send SMS functionality.
32  */
33 public final class DirectSendUtils {
34     private static final String TAG = "Mica.DirectSendUtils";
35 
36     public static final String AMBIGUOUS_RESULT = "AMBIGUOUS_RESULT";
37 
38     /**
39      * Callback interface used for chaining direct send user flow.
40      * See {@link VoicePlateActivity#askToDisambiguateDeviceAddress} and
41      * {@link VoicePlateActivity#askToDisambiguatePhoneNumber}.
42      */
43     public interface ResultsCallback {
44         /**
45          * Callback used when user prompt is successful.
46          * @param results The resulting Strings from user disambiguation.
47          */
onSuccess(String... results)48         void onSuccess(String... results);
49 
50         /**
51          * Callback used when user prompt is unsuccessful.
52          * @param error The error string to be presented.
53          */
onFailure(String error)54         void onFailure(String error);
55     }
56 
57     /**
58      * Sends attached Pending Intent for SMS direct send.
59      */
sendSMS( Context context, PendingIntent pendingIntent, String address, String number, String message)60     public static void sendSMS(
61             Context context,
62             PendingIntent pendingIntent,
63             String address,
64             String number,
65             String message) {
66         Intent intent = new Intent();
67         intent.putExtra(CarVoiceInteractionSession.KEY_PHONE_NUMBER, number);
68         intent.putExtra(CarVoiceInteractionSession.KEY_DEVICE_ADDRESS, address);
69         intent.putExtra(Intent.EXTRA_TEXT, message);
70 
71         try {
72             Log.d(TAG, "Sending SMS Pending Intent");
73             pendingIntent.send(context, 0, intent);
74         } catch (PendingIntent.CanceledException ex) {
75             Log.e(TAG, "Pending Intent canceled: " + ex);
76         }
77     }
78 
79     /**
80      * Formats the user prompt string for number disambiguation.
81      */
formatNumberAskString(Context context, List<PhoneNumber> numbers)82     public static String formatNumberAskString(Context context, List<PhoneNumber> numbers) {
83         String str = context.getString(R.string.speech_reply_request_number);
84         String[] key = context.getResources().getStringArray(R.array.speech_reply_ordinals);
85         int i = 0;
86         for (PhoneNumber number : numbers) {
87             str += String.format(" Say %s for %s.", key[i], number.getRawNumber());
88             i++;
89             if (i > 2) {
90                 break;
91             }
92         }
93 
94         return str;
95     }
96 
97     /**
98      * Formats the user prompt string for device disambiguation.
99      */
formatDeviceAskString(Context context, List<BluetoothDevice> devices)100     public static String formatDeviceAskString(Context context, List<BluetoothDevice> devices) {
101         String str = context.getString(R.string.speech_reply_request_device);
102         String[] key = context.getResources().getStringArray(R.array.speech_reply_ordinals);
103         int i = 0;
104         for (BluetoothDevice device : devices) {
105             str += String.format(" Say %s for %s.", key[i], device.getName());
106             i++;
107             if (i > 2) {
108                 break;
109             }
110         }
111 
112         return str;
113     }
114 }
115