1 /*
2  * Copyright (C) 2015 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.packageinstaller.permission.utils;
18 
19 import android.content.pm.PackageInfo;
20 import android.util.EventLog;
21 import com.android.packageinstaller.permission.model.AppPermissionGroup;
22 
23 import java.util.List;
24 
25 public final class SafetyNetLogger {
26 
27     // The log tag used by SafetyNet to pick entries from the event log.
28     private static final int SNET_NET_EVENT_LOG_TAG = 0x534e4554;
29 
30     // Log tag for the result of permissions request.
31     private static final String PERMISSIONS_REQUESTED = "permissions_requested";
32 
33     // Log tag for the result of permissions toggling.
34     private static final String PERMISSIONS_TOGGLED = "permissions_toggled";
35 
SafetyNetLogger()36     private SafetyNetLogger() {
37         /* do nothing */
38     }
39 
logPermissionsRequested(PackageInfo packageInfo, List<AppPermissionGroup> groups)40     public static void logPermissionsRequested(PackageInfo packageInfo,
41             List<AppPermissionGroup> groups) {
42         EventLog.writeEvent(SNET_NET_EVENT_LOG_TAG, PERMISSIONS_REQUESTED,
43                 packageInfo.applicationInfo.uid, buildChangedGroupForPackageMessage(
44                         packageInfo.packageName, groups));
45     }
46 
logPermissionsToggled(String packageName, List<AppPermissionGroup> groups)47     public static void logPermissionsToggled(String packageName, List<AppPermissionGroup> groups) {
48         EventLog.writeEvent(SNET_NET_EVENT_LOG_TAG, PERMISSIONS_TOGGLED,
49                 android.os.Process.myUid(), buildChangedGroupForPackageMessage(
50                         packageName, groups));
51     }
52 
buildChangedGroupForPackageMessage(String packageName, List<AppPermissionGroup> groups)53     private static String buildChangedGroupForPackageMessage(String packageName,
54             List<AppPermissionGroup> groups) {
55         StringBuilder builder = new StringBuilder();
56 
57         builder.append(packageName).append(':');
58 
59         final int groupCount = groups.size();
60         for (int i = 0; i < groupCount; i++) {
61             AppPermissionGroup group = groups.get(i);
62             if (i > 0) {
63                 builder.append(';');
64             }
65             builder.append(group.getName()).append('|');
66             builder.append(group.areRuntimePermissionsGranted()).append('|');
67             builder.append(group.getFlags());
68         }
69 
70         return builder.toString();
71     }
72 }
73