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.dialer.sms;
18 
19 import static com.android.car.messenger.common.MessagingUtils.ACTION_DIRECT_SEND;
20 
21 import android.app.Notification;
22 import android.app.NotificationChannel;
23 import android.app.NotificationManager;
24 import android.app.Service;
25 import android.content.Intent;
26 import android.os.IBinder;
27 import android.util.Log;
28 
29 import com.android.car.dialer.R;
30 import com.android.car.messenger.common.MessagingUtils;
31 
32 import dagger.hilt.android.AndroidEntryPoint;
33 
34 /**
35  * Service used to send SMS from the assistant
36  */
37 @AndroidEntryPoint(Service.class)
38 public class MessagingService extends Hilt_MessagingService {
39     private static final String TAG = "CD.MessagingService";
40 
41     private static final String CHANNEL_ID = "ID";
42 
43     @Override
onCreate()44     public void onCreate() {
45         super.onCreate();
46         sendServiceRunningNotification();
47     }
48 
sendServiceRunningNotification()49     private void sendServiceRunningNotification() {
50         NotificationManager mgr = getSystemService(NotificationManager.class);
51 
52         NotificationChannel channel = new NotificationChannel(
53                 CHANNEL_ID,
54                 "title",
55                 NotificationManager.IMPORTANCE_LOW);
56         mgr.createNotificationChannel(channel);
57 
58         Notification notification = new Notification.Builder(this, CHANNEL_ID)
59                 .setContentTitle(getString(R.string.sms_notification_title))
60                 .setContentText(getString(R.string.sms_notification_text))
61                 .build();
62 
63         startForeground(Integer.MAX_VALUE, notification);
64     }
65 
66     @Override
onBind(Intent intent)67     public IBinder onBind(Intent intent) {
68         return null;
69     }
70 
71     @Override
onStartCommand(Intent intent, int flags, int startId)72     public int onStartCommand(Intent intent, int flags, int startId) {
73         if (intent == null || intent.getAction() == null) {
74             return START_STICKY;
75         }
76 
77         String action = intent.getAction();
78 
79         Log.d(TAG, "action: " + action);
80 
81         switch (action) {
82             case ACTION_DIRECT_SEND:
83                 MessagingUtils.directSend(this, intent);
84                 break;
85             default:
86                 break;
87         }
88 
89         return START_STICKY;
90     }
91 }
92