1 /*
2  * Copyright (C) 2019 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.systemui.statusbar.policy;
18 
19 import android.net.Uri;
20 import android.os.RemoteException;
21 import android.service.notification.StatusBarNotification;
22 import android.util.Log;
23 
24 import androidx.annotation.NonNull;
25 
26 import com.android.internal.statusbar.IStatusBarService;
27 import com.android.systemui.dagger.SysUISingleton;
28 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
29 import com.android.systemui.statusbar.notification.collection.notifcollection.CommonNotifCollection;
30 import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener;
31 
32 import javax.inject.Inject;
33 
34 /**
35  * Handles granting and revoking inline URI grants associated with RemoteInputs.
36  */
37 @SysUISingleton
38 public class RemoteInputUriController {
39 
40     private final IStatusBarService mStatusBarManagerService;
41     private static final String TAG = "RemoteInputUriController";
42 
43     @Inject
RemoteInputUriController(IStatusBarService statusBarService)44     public RemoteInputUriController(IStatusBarService statusBarService) {
45         mStatusBarManagerService = statusBarService;
46     }
47 
48     /**
49      * Attach this controller as a listener to the provided NotificationEntryManager to ensure
50      * that RemoteInput URI grants are cleaned up when the notification entry is removed from
51      * the shade.
52      */
attach(CommonNotifCollection manager)53     public void attach(CommonNotifCollection manager) {
54         manager.addCollectionListener(mInlineUriListener);
55     }
56 
57     /**
58      * Create a temporary grant which allows the app that submitted the notification access to the
59      * specified URI.
60      */
grantInlineReplyUriPermission(StatusBarNotification sbn, Uri data)61     public void grantInlineReplyUriPermission(StatusBarNotification sbn, Uri data) {
62         try {
63             mStatusBarManagerService.grantInlineReplyUriPermission(
64                     sbn.getKey(), data, sbn.getUser(), sbn.getPackageName());
65         } catch (Exception e) {
66             Log.e(TAG, "Failed to grant URI permissions:" + e.getMessage(), e);
67         }
68     }
69 
70     /**
71      * Ensures that inline URI permissions are cleared when notification entries are removed from
72      * the shade.
73      */
74     private final NotifCollectionListener mInlineUriListener = new NotifCollectionListener() {
75         @Override
76         public void onEntryRemoved(@NonNull NotificationEntry entry, int reason) {
77             try {
78                 mStatusBarManagerService.clearInlineReplyUriPermissions(entry.getKey());
79             } catch (RemoteException ex) {
80                 ex.rethrowFromSystemServer();
81             }
82         }
83     };
84 }
85