1 /*
2  * Copyright (C) 2020 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.cellbroadcastreceiver;
18 
19 import android.app.NotificationManager;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.provider.Telephony;
24 
25 import com.android.internal.annotations.VisibleForTesting;
26 
27 /**
28  * {@link BroadcastReceiver} used for handling internal broadcasts (e.g. generated from
29  * {@link android.app.PendingIntent}s).
30  */
31 public class CellBroadcastInternalReceiver extends BroadcastReceiver {
32 
33     /**
34      * helper method for easier testing. To generate a new CellBroadcastTask
35      * @param deliveryTime message delivery time
36      */
37     @VisibleForTesting
getCellBroadcastTask(Context context, long deliveryTime)38     public void getCellBroadcastTask(Context context, long deliveryTime) {
39         new CellBroadcastContentProvider.AsyncCellBroadcastTask(context.getContentResolver())
40                 .execute(new CellBroadcastContentProvider.CellBroadcastOperation() {
41                     @Override
42                     public boolean execute(CellBroadcastContentProvider provider) {
43                         return provider.markBroadcastRead(Telephony.CellBroadcasts.DELIVERY_TIME,
44                                 deliveryTime);
45                     }
46                 });
47     }
48 
49     /**
50      * This method's purpose if to enable unit testing
51      */
52     @VisibleForTesting
startConfigServiceToEnableChannels(Context context)53     public void startConfigServiceToEnableChannels(Context context) {
54         CellBroadcastReceiver.startConfigService(context,
55                 CellBroadcastConfigService.ACTION_ENABLE_CHANNELS);
56     }
57 
58     @Override
onReceive(Context context, Intent intent)59     public void onReceive(Context context, Intent intent) {
60         if (CellBroadcastReceiver.ACTION_MARK_AS_READ.equals(intent.getAction())) {
61             final long deliveryTime = intent.getLongExtra(
62                     CellBroadcastReceiver.EXTRA_DELIVERY_TIME, -1);
63             final int notificationId = intent.getIntExtra(
64                     CellBroadcastReceiver.EXTRA_NOTIF_ID,
65                     CellBroadcastAlertService.NOTIFICATION_ID);
66             // Stop playing alert sound/vibration/speech (if started)
67             context.stopService(new Intent(context, CellBroadcastAlertAudio.class));
68             CellBroadcastAlertReminder.cancelAlertReminder();
69             context.getSystemService(NotificationManager.class).cancel(notificationId);
70             getCellBroadcastTask(context, deliveryTime);
71         } else if (CellBroadcastReceiver.CELLBROADCAST_START_CONFIG_ACTION.equals(
72                 intent.getAction())) {
73             startConfigServiceToEnableChannels(context);
74         }
75     }
76 }
77