1 /*
2  * Copyright (C) 2022 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.eventlib.premade;
18 
19 import android.app.admin.DelegatedAdminReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.Uri;
23 
24 import com.android.eventlib.events.delegatedadminreceivers.DelegatedAdminChoosePrivateKeyAliasEvent;
25 import com.android.eventlib.events.delegatedadminreceivers.DelegatedAdminChoosePrivateKeyAliasEvent.DelegatedAdminChoosePrivateKeyAliasEventLogger;
26 import com.android.eventlib.events.delegatedadminreceivers.DelegatedAdminSecurityLogsAvailableEvent;
27 import com.android.eventlib.events.delegatedadminreceivers.DelegatedAdminSecurityLogsAvailableEvent.DelegatedAdminSecurityLogsAvailableEventLogger;
28 import com.android.eventlib.events.deviceadminreceivers.DelegatedAdminNetworkLogsAvailableEvent;
29 import com.android.eventlib.events.deviceadminreceivers.DelegatedAdminNetworkLogsAvailableEvent.DelegatedAdminNetworkLogsAvailableEventLogger;
30 
31 /**
32  * {@link DelegatedAdminReceiver} which logs all callbacks using EventLib.
33  */
34 @SuppressWarnings("NewApi")
35 public class EventLibDelegatedAdminReceiver extends DelegatedAdminReceiver {
36 
37     private String mOverrideDelegatedAdminReceiverClassName;
38 
setOverrideDelegatedAdminReceiverClassName( String overrideDelegatedAdminReceiverClassName)39     public void setOverrideDelegatedAdminReceiverClassName(
40             String overrideDelegatedAdminReceiverClassName) {
41         mOverrideDelegatedAdminReceiverClassName = overrideDelegatedAdminReceiverClassName;
42     }
43 
44     /**
45      * Get the class name for this {@link DelegatedAdminReceiver}.
46      *
47      * <p>This will account for the name being overridden.
48      */
className()49     public String className() {
50         if (mOverrideDelegatedAdminReceiverClassName != null) {
51             return mOverrideDelegatedAdminReceiverClassName;
52         } else {
53             return EventLibDelegatedAdminReceiver.class.getName();
54         }
55     }
56 
57     @Override
onChoosePrivateKeyAlias(Context context, Intent intent, int uid, Uri uri, String alias)58     public String onChoosePrivateKeyAlias(Context context, Intent intent, int uid, Uri uri,
59             String alias) {
60         DelegatedAdminChoosePrivateKeyAliasEventLogger logger =
61                 DelegatedAdminChoosePrivateKeyAliasEvent
62                         .logger(this, context, intent, uid, uri, alias);
63 
64         if (mOverrideDelegatedAdminReceiverClassName != null) {
65             logger.setDelegatedAdminReceiver(mOverrideDelegatedAdminReceiverClassName);
66         }
67 
68         logger.log();
69 
70         // TODO(b/198280332) Allow TestApp to return values for methods.
71         if (uri == null) {
72             return null;
73         }
74         return uri.getQueryParameter("alias");
75     }
76 
77     @Override
onNetworkLogsAvailable(Context context, Intent intent, long batchToken, int networkLogsCount)78     public void onNetworkLogsAvailable(Context context, Intent intent, long batchToken,
79             int networkLogsCount) {
80         DelegatedAdminNetworkLogsAvailableEventLogger logger =
81                 DelegatedAdminNetworkLogsAvailableEvent
82                         .logger(this, context, intent, batchToken, networkLogsCount);
83 
84         if (mOverrideDelegatedAdminReceiverClassName != null) {
85             logger.setDelegatedAdminReceiver(mOverrideDelegatedAdminReceiverClassName);
86         }
87 
88         logger.log();
89     }
90 
91     @Override
onSecurityLogsAvailable(Context context, Intent intent)92     public void onSecurityLogsAvailable(Context context, Intent intent) {
93         DelegatedAdminSecurityLogsAvailableEventLogger logger =
94                 DelegatedAdminSecurityLogsAvailableEvent.logger(this, context, intent);
95 
96         if (mOverrideDelegatedAdminReceiverClassName != null) {
97             logger.setDelegatedAdminReceiver(mOverrideDelegatedAdminReceiverClassName);
98         }
99 
100         logger.log();
101     }
102 }
103