• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 
20 /**
21  * SQLite table for storing app op accesses.
22  */
23 final class DiscreteOpsTable {
24     private static final String TABLE_NAME = "app_op_accesses";
25     private static final String INDEX_APP_OP = "app_op_access_index";
26 
27     static final class Columns {
28         /** Auto increment primary key. */
29         static final String ID = "id";
30         /** UID of the package accessing private data. */
31         static final String UID = "uid";
32         /** Package accessing private data. */
33         static final String PACKAGE_NAME = "package_name";
34         /** The device from which the private data is accessed. */
35         static final String DEVICE_ID = "device_id";
36         /** Op code representing private data i.e. location, mic etc. */
37         static final String OP_CODE = "op_code";
38         /** Attribution tag provided when accessing the private data. */
39         static final String ATTRIBUTION_TAG = "attribution_tag";
40         /** Timestamp when private data is accessed, number of milliseconds that have passed
41          * since Unix epoch */
42         static final String ACCESS_TIME = "access_time";
43         /** For how long the private data is accessed. */
44         static final String ACCESS_DURATION = "access_duration";
45         /** App process state, whether the app is in foreground, background or cached etc. */
46         static final String UID_STATE = "uid_state";
47         /** App op flags */
48         static final String OP_FLAGS = "op_flags";
49         /** Attribution flags */
50         static final String ATTRIBUTION_FLAGS = "attribution_flags";
51         /** Chain id */
52         static final String CHAIN_ID = "chain_id";
53     }
54 
55     static final int UID_INDEX = 1;
56     static final int PACKAGE_NAME_INDEX = 2;
57     static final int DEVICE_ID_INDEX = 3;
58     static final int OP_CODE_INDEX = 4;
59     static final int ATTRIBUTION_TAG_INDEX = 5;
60     static final int ACCESS_TIME_INDEX = 6;
61     static final int ACCESS_DURATION_INDEX = 7;
62     static final int UID_STATE_INDEX = 8;
63     static final int OP_FLAGS_INDEX = 9;
64     static final int ATTRIBUTION_FLAGS_INDEX = 10;
65     static final int CHAIN_ID_INDEX = 11;
66 
67     static final String CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS "
68             + TABLE_NAME + "("
69             + Columns.ID + " INTEGER PRIMARY KEY,"
70             + Columns.UID + " INTEGER,"
71             + Columns.PACKAGE_NAME + " TEXT,"
72             + Columns.DEVICE_ID + " TEXT NOT NULL,"
73             + Columns.OP_CODE + " INTEGER,"
74             + Columns.ATTRIBUTION_TAG + " TEXT,"
75             + Columns.ACCESS_TIME + " INTEGER,"
76             + Columns.ACCESS_DURATION + " INTEGER,"
77             + Columns.UID_STATE + " INTEGER,"
78             + Columns.OP_FLAGS + " INTEGER,"
79             + Columns.ATTRIBUTION_FLAGS + " INTEGER,"
80             + Columns.CHAIN_ID + " INTEGER"
81             + ")";
82 
83     static final String INSERT_TABLE_SQL = "INSERT INTO " + TABLE_NAME + "("
84             + Columns.UID + ", "
85             + Columns.PACKAGE_NAME + ", "
86             + Columns.DEVICE_ID + ", "
87             + Columns.OP_CODE + ", "
88             + Columns.ATTRIBUTION_TAG + ", "
89             + Columns.ACCESS_TIME + ", "
90             + Columns.ACCESS_DURATION + ", "
91             + Columns.UID_STATE + ", "
92             + Columns.OP_FLAGS + ", "
93             + Columns.ATTRIBUTION_FLAGS + ", "
94             + Columns.CHAIN_ID + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
95 
96     static final String SELECT_MAX_ATTRIBUTION_CHAIN_ID = "SELECT MAX(" + Columns.CHAIN_ID + ")"
97             + " FROM " + TABLE_NAME;
98 
99     static final String SELECT_TABLE_DATA = "SELECT DISTINCT "
100             + Columns.UID + ","
101             + Columns.PACKAGE_NAME + ","
102             + Columns.DEVICE_ID + ","
103             + Columns.OP_CODE + ","
104             + Columns.ATTRIBUTION_TAG + ","
105             + Columns.ACCESS_TIME + ","
106             + Columns.ACCESS_DURATION + ","
107             + Columns.UID_STATE + ","
108             + Columns.OP_FLAGS + ","
109             + Columns.ATTRIBUTION_FLAGS + ","
110             + Columns.CHAIN_ID
111             + " FROM " + TABLE_NAME;
112 
113     static final String DELETE_TABLE_DATA = "DELETE FROM " + TABLE_NAME;
114 
115     static final String DELETE_TABLE_DATA_BEFORE_ACCESS_TIME = "DELETE FROM " + TABLE_NAME
116             + " WHERE " + Columns.ACCESS_TIME + " < ?";
117 
118     static final String DELETE_DATA_FOR_UID_PACKAGE = "DELETE FROM " + DiscreteOpsTable.TABLE_NAME
119             + " WHERE " + Columns.UID + " = ? AND " + Columns.PACKAGE_NAME + " = ?";
120 
121     static final String OFFSET_ACCESS_TIME = "UPDATE " + DiscreteOpsTable.TABLE_NAME
122             + " SET " + Columns.ACCESS_TIME + " = ACCESS_TIME - ?";
123 
124     // Index on access time, uid and op code
125     static final String CREATE_INDEX_SQL = "CREATE INDEX IF NOT EXISTS "
126             + INDEX_APP_OP + " ON " + TABLE_NAME
127             + " (" + Columns.ACCESS_TIME + ", " + Columns.UID + ", " + Columns.OP_CODE + ")";
128 }
129