1 /* 2 * Copyright (C) 2025 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.appop; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.app.AppOpsManager; 22 import android.content.ContentResolver; 23 import android.os.RemoteCallback; 24 25 import java.io.PrintWriter; 26 import java.text.SimpleDateFormat; 27 import java.util.Date; 28 29 /** 30 * A registry to record app operation access events, which are generated upon an application's 31 * access to private data or system resources. These events are stored in both aggregated 32 * and individual/discrete formats. 33 */ 34 public interface HistoricalRegistryInterface { 35 /** 36 * A callback to inform system components are ready. 37 */ systemReady(@onNull ContentResolver resolver)38 void systemReady(@NonNull ContentResolver resolver); 39 40 /** 41 * Callback for system shutdown. 42 */ shutdown()43 void shutdown(); 44 45 /** 46 * Dumps aggregated/historical events to the console based on the filters. 47 */ dump(String prefix, PrintWriter pw, int filterUid, @Nullable String filterPackage, @Nullable String filterAttributionTag, int filterOp, @AppOpsManager.HistoricalOpsRequestFilter int filter)48 void dump(String prefix, PrintWriter pw, int filterUid, 49 @Nullable String filterPackage, @Nullable String filterAttributionTag, int filterOp, 50 @AppOpsManager.HistoricalOpsRequestFilter int filter); 51 52 /** 53 * Dumps discrete/individual events to the console based on filters. 54 */ dumpDiscreteData(@onNull PrintWriter pw, int uidFilter, @Nullable String packageNameFilter, @Nullable String attributionTagFilter, @AppOpsManager.HistoricalOpsRequestFilter int filter, int dumpOp, @NonNull SimpleDateFormat sdf, @NonNull Date date, @NonNull String prefix, int nDiscreteOps)55 void dumpDiscreteData(@NonNull PrintWriter pw, int uidFilter, 56 @Nullable String packageNameFilter, @Nullable String attributionTagFilter, 57 @AppOpsManager.HistoricalOpsRequestFilter int filter, int dumpOp, 58 @NonNull SimpleDateFormat sdf, @NonNull Date date, @NonNull String prefix, 59 int nDiscreteOps); 60 61 /** 62 * Record duration for given op. 63 */ increaseOpAccessDuration(int op, int uid, @NonNull String packageName, @NonNull String deviceId, @Nullable String attributionTag, @AppOpsManager.UidState int uidState, @AppOpsManager.OpFlags int flags, long eventStartTime, long increment, @AppOpsManager.AttributionFlags int attributionFlags, int attributionChainId)64 void increaseOpAccessDuration(int op, int uid, @NonNull String packageName, 65 @NonNull String deviceId, @Nullable String attributionTag, 66 @AppOpsManager.UidState int uidState, 67 @AppOpsManager.OpFlags int flags, long eventStartTime, long increment, 68 @AppOpsManager.AttributionFlags int attributionFlags, int attributionChainId); 69 70 /** 71 * Record access counts for given op. 72 */ incrementOpAccessedCount(int op, int uid, @NonNull String packageName, @NonNull String deviceId, @Nullable String attributionTag, @AppOpsManager.UidState int uidState, @AppOpsManager.OpFlags int flags, long accessTime, @AppOpsManager.AttributionFlags int attributionFlags, int attributionChainId, int accessCount)73 void incrementOpAccessedCount(int op, int uid, @NonNull String packageName, 74 @NonNull String deviceId, @Nullable String attributionTag, 75 @AppOpsManager.UidState int uidState, 76 @AppOpsManager.OpFlags int flags, long accessTime, 77 @AppOpsManager.AttributionFlags int attributionFlags, int attributionChainId, 78 int accessCount); 79 80 /** 81 * Record rejected counts for given op. 82 */ incrementOpRejectedCount(int op, int uid, @NonNull String packageName, @Nullable String attributionTag, @AppOpsManager.UidState int uidState, @AppOpsManager.OpFlags int flags)83 void incrementOpRejectedCount(int op, int uid, @NonNull String packageName, 84 @Nullable String attributionTag, @AppOpsManager.UidState int uidState, 85 @AppOpsManager.OpFlags int flags); 86 87 /** 88 * Read historical ops from both aggregated and discrete events based on input filter. 89 */ getHistoricalOps(int uid, @Nullable String packageName, @Nullable String attributionTag, @Nullable String[] opNames, @AppOpsManager.OpHistoryFlags int historyFlags, @AppOpsManager.HistoricalOpsRequestFilter int filter, long beginTimeMillis, long endTimeMillis, @AppOpsManager.OpFlags int flags, @Nullable String[] attributionExemptPkgs, @NonNull RemoteCallback callback)90 void getHistoricalOps(int uid, @Nullable String packageName, @Nullable String attributionTag, 91 @Nullable String[] opNames, @AppOpsManager.OpHistoryFlags int historyFlags, 92 @AppOpsManager.HistoricalOpsRequestFilter int filter, long beginTimeMillis, 93 long endTimeMillis, 94 @AppOpsManager.OpFlags int flags, @Nullable String[] attributionExemptPkgs, 95 @NonNull RemoteCallback callback); 96 97 /** 98 * Remove app op events for a given UID and package. 99 */ clearHistory(int uid, String packageName)100 void clearHistory(int uid, String packageName); 101 102 /** 103 * A periodic callback from {@link AppOpsService} to flush the in memory discrete 104 * app op events to disk/database. 105 */ writeAndClearDiscreteHistory()106 void writeAndClearDiscreteHistory(); 107 108 /** 109 * A callback flush the in memory app op events to disk/database. 110 */ persistPendingHistory()111 void persistPendingHistory(); 112 113 /** 114 * Set history parameters. 115 * 116 * @param mode - Whether historical registry is Active, Passive or Disabled. 117 * @param baseSnapshotInterval - Interval between 2 snapshots, default 15 minutes. 118 * @param intervalCompressionMultiplier - Interval compression multiplier, default is 10. 119 */ setHistoryParameters(@ppOpsManager.HistoricalMode int mode, long baseSnapshotInterval, long intervalCompressionMultiplier)120 void setHistoryParameters(@AppOpsManager.HistoricalMode int mode, 121 long baseSnapshotInterval, long intervalCompressionMultiplier); 122 123 /** 124 * Reset history parameters to defaults. 125 */ resetHistoryParameters()126 void resetHistoryParameters(); 127 128 /** 129 * Remove all app op accesses from both aggregated and individual event's storage. 130 */ clearAllHistory()131 void clearAllHistory(); 132 133 /** 134 * Offsets the history by the given duration. 135 */ offsetHistory(long offsetMillis)136 void offsetHistory(long offsetMillis); 137 138 /** 139 * Retrieve historical app op stats for a period form disk. 140 */ getHistoricalOpsFromDiskRaw(int uid, @Nullable String packageName, @Nullable String attributionTag, @Nullable String[] opNames, @AppOpsManager.OpHistoryFlags int historyFlags, @AppOpsManager.HistoricalOpsRequestFilter int filter, long beginTimeMillis, long endTimeMillis, @AppOpsManager.OpFlags int flags, String[] attributionExemptedPackages, @NonNull RemoteCallback callback)141 void getHistoricalOpsFromDiskRaw(int uid, @Nullable String packageName, 142 @Nullable String attributionTag, @Nullable String[] opNames, 143 @AppOpsManager.OpHistoryFlags int historyFlags, 144 @AppOpsManager.HistoricalOpsRequestFilter int filter, 145 long beginTimeMillis, long endTimeMillis, @AppOpsManager.OpFlags int flags, 146 String[] attributionExemptedPackages, @NonNull RemoteCallback callback); 147 148 /** 149 * Adds ops to the history directly. This could be useful for testing especially 150 * when the historical registry operates in passive mode. 151 */ addHistoricalOps(AppOpsManager.HistoricalOps ops)152 void addHistoricalOps(AppOpsManager.HistoricalOps ops); 153 } 154