summaryrefslogtreecommitdiff
path: root/src/com/android/car/messenger/MessengerService.java
blob: 0b0e4e4a3c47d8fd607021c5196cb1c0f74e5e3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.android.car.messenger;


import android.app.Notification;
import android.app.Notification.Action;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.media.AudioAttributes;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import androidx.core.app.NotificationCompat;
import androidx.core.app.RemoteInput;

import com.android.car.messenger.bluetooth.BluetoothMonitor;
import com.android.car.messenger.log.L;

/** Service responsible for handling SMS messaging events from paired Bluetooth devices. */
public class MessengerService extends Service {
    private final static String TAG = "CM.MessengerService";

    /* ACTIONS */
    /** Used to start this service at boot-complete. Takes no arguments. */
    public static final String ACTION_START = "com.android.car.messenger.ACTION_START";

    /** Used to reply to message with voice input; triggered by an assistant. */
    public static final String ACTION_VOICE_REPLY = "com.android.car.messenger.ACTION_VOICE_REPLY";

    /** Used to clear notification state when user dismisses notification. */
    public static final String ACTION_CLEAR_NOTIFICATION_STATE =
            "com.android.car.messenger.ACTION_CLEAR_NOTIFICATION_STATE";

    /** Used to mark a notification as read **/
    public static final String ACTION_MARK_AS_READ =
            "com.android.car.messenger.ACTION_MARK_AS_READ";

    /** Used to notify when a sms is received. Takes no arguments. */
    public static final String ACTION_RECEIVED_SMS =
            "com.android.car.messenger.ACTION_RECEIVED_SMS";

    /** Used to notify when a mms is received. Takes no arguments. */
    public static final String ACTION_RECEIVED_MMS =
            "com.android.car.messenger.ACTION_RECEIVED_MMS";

    /* EXTRAS */
    /** Key under which the {@link SenderKey} is provided. */
    public static final String EXTRA_SENDER_KEY = "com.android.car.messenger.EXTRA_SENDER_KEY";

    /**
     * The resultKey of the {@link RemoteInput} which is sent in the reply callback {@link Action}.
     */
    public static final String REMOTE_INPUT_KEY = "REMOTE_INPUT_KEY";

    /* NOTIFICATIONS */
    static final String SMS_CHANNEL_ID = "SMS_CHANNEL_ID";
    private static final String APP_RUNNING_CHANNEL_ID = "APP_RUNNING_CHANNEL_ID";
    private static final int SERVICE_STARTED_NOTIFICATION_ID = Integer.MAX_VALUE;

    /** Delegate class used to handle this services' actions */
    private MessengerDelegate mMessengerDelegate;

    /** Notifies this service of new bluetooth actions */
    private BluetoothMonitor mBluetoothMonitor;

    /* Binding boilerplate */
    private final IBinder mBinder = new LocalBinder();

    public class LocalBinder extends Binder {
        MessengerService getService() {
            return MessengerService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        L.d(TAG, "onCreate");

        mMessengerDelegate = new MessengerDelegate(this);
        mBluetoothMonitor = new BluetoothMonitor(this);
        mBluetoothMonitor.registerListener(mMessengerDelegate);
        sendServiceRunningNotification();
    }


    private void sendServiceRunningNotification() {
        NotificationManager notificationManager = getSystemService(NotificationManager.class);

        if (notificationManager == null) {
            L.e(TAG, "Failed to get NotificationManager instance");
            return;
        }

        // Create notification channel for app running notification
        {
            NotificationChannel appRunningNotificationChannel =
                    new NotificationChannel(APP_RUNNING_CHANNEL_ID,
                            getString(R.string.app_running_msg_channel_name),
                            NotificationManager.IMPORTANCE_MIN);
            notificationManager.createNotificationChannel(appRunningNotificationChannel);
        }

        {
            AudioAttributes attributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();
            NotificationChannel smsChannel = new NotificationChannel(SMS_CHANNEL_ID,
                    getString(R.string.sms_channel_name),
                    NotificationManager.IMPORTANCE_HIGH);
            smsChannel.setDescription(getString(R.string.sms_channel_description));
            smsChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, attributes);
            notificationManager.createNotificationChannel(smsChannel);
        }

        final Notification notification =
                new NotificationCompat.Builder(this, APP_RUNNING_CHANNEL_ID)
                        .setSmallIcon(R.drawable.ic_message)
                        .setContentTitle(getString(R.string.app_running_msg_notification_title))
                        .setContentText(getString(R.string.app_running_msg_notification_content))
                        .build();
        startForeground(SERVICE_STARTED_NOTIFICATION_ID, notification);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        L.d(TAG, "onDestroy");
        mMessengerDelegate.onDestroy();
        mBluetoothMonitor.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final int result = START_STICKY;

        if (intent == null || intent.getAction() == null) return result;

        final String action = intent.getAction();

        if (!hasRequiredArgs(intent)) {
            L.e(TAG, "Dropping command: %s. Reason: Missing required argument.", action);
            return result;
        }

        switch (action) {
            case ACTION_START:
                // NO-OP
                break;
            case ACTION_VOICE_REPLY:
                voiceReply(intent);
                break;
            case ACTION_CLEAR_NOTIFICATION_STATE:
                clearNotificationState(intent);
                break;
            case ACTION_MARK_AS_READ:
                markAsRead(intent);
                break;
            case ACTION_RECEIVED_SMS:
                // NO-OP
                break;
            case ACTION_RECEIVED_MMS:
                // NO-OP
                break;
            case TelephonyManager.ACTION_RESPOND_VIA_MESSAGE:
                respondViaMessage(intent);
                break;
            default:
                L.w(TAG, "Unsupported action: %s", action);
        }

        return result;
    }

    /**
     * Checks that the intent has all of the required arguments for its requested action.
     *
     * @param intent the intent to check
     * @return true if the intent has all of the required {@link Bundle} args for its action
     */
    private static boolean hasRequiredArgs(Intent intent) {
        switch (intent.getAction()) {
            case ACTION_VOICE_REPLY:
            case ACTION_CLEAR_NOTIFICATION_STATE:
            case ACTION_MARK_AS_READ:
                if (!intent.hasExtra(EXTRA_SENDER_KEY)) {
                    L.w(TAG, "Intent %s missing sender-key extra.", intent.getAction());
                    return false;
                }
                return true;
            default:
                // For unknown actions, default to true. We'll report an error for these later.
                return true;
        }
    }

    /**
     * Sends a reply, meant to be used from a caller originating from voice input.
     *
     * @param intent intent containing {@link MessengerService#EXTRA_SENDER_KEY} and
     *               a {@link RemoteInput} with {@link MessengerService#REMOTE_INPUT_KEY} resultKey
     */
    public void voiceReply(Intent intent) {
        final SenderKey senderKey = intent.getParcelableExtra(EXTRA_SENDER_KEY);
        final Bundle bundle = RemoteInput.getResultsFromIntent(intent);
        if (bundle == null) {
            L.e(TAG, "Dropping voice reply. Received null RemoteInput result!");
            return;
        }
        final CharSequence message = bundle.getCharSequence(REMOTE_INPUT_KEY);
        L.d(TAG, "voiceReply");
        if (!TextUtils.isEmpty(message)) {
            mMessengerDelegate.sendMessage(senderKey, message.toString());
        }
    }

    /**
     * Clears notification(s) associated with a given sender key.
     *
     * @param intent intent containing {@link MessengerService#EXTRA_SENDER_KEY} bundle argument
     */
    public void clearNotificationState(Intent intent) {
        final SenderKey senderKey = intent.getParcelableExtra(EXTRA_SENDER_KEY);
        L.d(TAG, "clearNotificationState");
        mMessengerDelegate.clearNotifications(key -> key.equals(senderKey));
    }

    /**
     * Mark a conversation associated with a given sender key as read.
     *
     * @param intent intent containing {@link MessengerService#EXTRA_SENDER_KEY} bundle argument
     */
    public void markAsRead(Intent intent) {
        final SenderKey senderKey = intent.getParcelableExtra(EXTRA_SENDER_KEY);
        L.d(TAG, "markAsRead");
        mMessengerDelegate.markAsRead(senderKey);
    }

    /**
     * Respond to a call via text message.
     *
     * @param intent intent containing a URI describing the recipient and the URI schema
     */
    public void respondViaMessage(Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras == null) {
            L.v(TAG, "Called to send SMS but no extras");
            return;
        }

        // TODO: get senderKey from the recipient's address, and sendMessage() to it.
    }
}