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.server.people.data;
18 
19 import android.annotation.UserIdInt;
20 import android.app.ActivityManager;
21 import android.app.AlarmManager;
22 import android.app.PendingIntent;
23 import android.app.people.ConversationStatus;
24 import android.content.BroadcastReceiver;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.net.Uri;
29 import android.os.Binder;
30 import android.os.CancellationSignal;
31 
32 import com.android.server.LocalServices;
33 import com.android.server.people.PeopleServiceInternal;
34 import com.android.server.pm.PackageManagerService;
35 
36 /**
37  * If a {@link ConversationStatus} is added to the system with an expiration time, remove that
38  * status at that time
39  */
40 public class ConversationStatusExpirationBroadcastReceiver extends BroadcastReceiver {
41 
42     static final String ACTION = "ConversationStatusExpiration";
43     static final String EXTRA_USER_ID = "userId";
44     static final int REQUEST_CODE = 10;
45     static final String SCHEME = "expStatus";
46 
scheduleExpiration(Context context, @UserIdInt int userId, String pkg, String conversationId, ConversationStatus status)47     void scheduleExpiration(Context context, @UserIdInt int userId, String pkg,
48             String conversationId, ConversationStatus status) {
49         final long identity = Binder.clearCallingIdentity();
50         try {
51             final PendingIntent pi = PendingIntent.getBroadcast(context,
52                     REQUEST_CODE,
53                     new Intent(ACTION)
54                             .setPackage(PackageManagerService.PLATFORM_PACKAGE_NAME)
55                             .setData(new Uri.Builder().scheme(SCHEME)
56                                     .appendPath(getKey(userId, pkg, conversationId, status))
57                                     .build())
58                             .addFlags(Intent.FLAG_RECEIVER_FOREGROUND)
59                             .putExtra(EXTRA_USER_ID, userId),
60                     PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
61             context.getSystemService(AlarmManager.class).setExactAndAllowWhileIdle(
62                     AlarmManager.RTC_WAKEUP, status.getEndTimeMillis(), pi);
63         } finally {
64             Binder.restoreCallingIdentity(identity);
65         }
66     }
67 
getKey(@serIdInt int userId, String pkg, String conversationId, ConversationStatus status)68     private static String getKey(@UserIdInt int userId, String pkg,
69             String conversationId, ConversationStatus status) {
70         return userId + pkg + conversationId + status.getId();
71     }
72 
getFilter()73     static IntentFilter getFilter() {
74         IntentFilter conversationStatusFilter =
75                 new IntentFilter(ConversationStatusExpirationBroadcastReceiver.ACTION);
76         conversationStatusFilter.addDataScheme(
77                 ConversationStatusExpirationBroadcastReceiver.SCHEME);
78         return conversationStatusFilter;
79     }
80 
81     @Override
onReceive(Context context, Intent intent)82     public void onReceive(Context context, Intent intent) {
83         String action = intent.getAction();
84         if (action == null) {
85             return;
86         }
87         if (ACTION.equals(action)) {
88             new Thread(() -> {
89                 PeopleServiceInternal peopleServiceInternal =
90                         LocalServices.getService(PeopleServiceInternal.class);
91                 peopleServiceInternal.pruneDataForUser(intent.getIntExtra(EXTRA_USER_ID,
92                         ActivityManager.getCurrentUser()), new CancellationSignal());
93             }).start();
94         }
95     }
96 }
97