blob: fd63d486d0491399e3a2559706600534c3b78ee2 [file] [log] [blame]
Adrian Roos7a4f3d42014-05-02 12:12:20 +02001/*
2 * Copyright (C) 2014 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
17package com.android.server.trust;
18
19import android.content.ComponentName;
20import android.os.SystemClock;
21import android.os.UserHandle;
Adrian Roos94e15a52015-04-16 12:23:18 -070022import android.service.trust.TrustAgentService;
Adrian Roos7a4f3d42014-05-02 12:12:20 +020023import android.util.TimeUtils;
24
25import java.io.PrintWriter;
26import java.util.ArrayDeque;
27import java.util.Iterator;
28
29/**
30 * An archive of trust events.
31 */
32public class TrustArchive {
33 private static final int TYPE_GRANT_TRUST = 0;
34 private static final int TYPE_REVOKE_TRUST = 1;
35 private static final int TYPE_TRUST_TIMEOUT = 2;
36 private static final int TYPE_AGENT_DIED = 3;
Adrian Roos7d59b4f2014-05-27 20:01:31 +020037 private static final int TYPE_AGENT_CONNECTED = 4;
38 private static final int TYPE_AGENT_STOPPED = 5;
Adrian Roos7861c662014-07-25 15:37:28 +020039 private static final int TYPE_MANAGING_TRUST = 6;
Adrian Roos7a4f3d42014-05-02 12:12:20 +020040
41 private static final int HISTORY_LIMIT = 200;
42
43 private static class Event {
44 final int type;
45 final int userId;
46 final ComponentName agent;
47 final long elapsedTimestamp;
48
49 // grantTrust
50 final String message;
51 final long duration;
Adrian Roos94e15a52015-04-16 12:23:18 -070052 final int flags;
Adrian Roos7a4f3d42014-05-02 12:12:20 +020053
Adrian Roos7861c662014-07-25 15:37:28 +020054 // managingTrust
55 final boolean managingTrust;
56
Adrian Roos7a4f3d42014-05-02 12:12:20 +020057 private Event(int type, int userId, ComponentName agent, String message,
Adrian Roos94e15a52015-04-16 12:23:18 -070058 long duration, int flags, boolean managingTrust) {
Adrian Roos7a4f3d42014-05-02 12:12:20 +020059 this.type = type;
60 this.userId = userId;
61 this.agent = agent;
62 this.elapsedTimestamp = SystemClock.elapsedRealtime();
63 this.message = message;
64 this.duration = duration;
Adrian Roos94e15a52015-04-16 12:23:18 -070065 this.flags = flags;
Adrian Roos7861c662014-07-25 15:37:28 +020066 this.managingTrust = managingTrust;
Adrian Roos7a4f3d42014-05-02 12:12:20 +020067 }
68 }
69
70 ArrayDeque<Event> mEvents = new ArrayDeque<Event>();
71
72 public void logGrantTrust(int userId, ComponentName agent, String message,
Adrian Roos94e15a52015-04-16 12:23:18 -070073 long duration, int flags) {
Adrian Roos7a4f3d42014-05-02 12:12:20 +020074 addEvent(new Event(TYPE_GRANT_TRUST, userId, agent, message, duration,
Adrian Roos94e15a52015-04-16 12:23:18 -070075 flags, false));
Adrian Roos7a4f3d42014-05-02 12:12:20 +020076 }
77
78 public void logRevokeTrust(int userId, ComponentName agent) {
Adrian Roos94e15a52015-04-16 12:23:18 -070079 addEvent(new Event(TYPE_REVOKE_TRUST, userId, agent, null, 0, 0, false));
Adrian Roos7a4f3d42014-05-02 12:12:20 +020080 }
81
82 public void logTrustTimeout(int userId, ComponentName agent) {
Adrian Roos94e15a52015-04-16 12:23:18 -070083 addEvent(new Event(TYPE_TRUST_TIMEOUT, userId, agent, null, 0, 0, false));
Adrian Roos7a4f3d42014-05-02 12:12:20 +020084 }
85
86 public void logAgentDied(int userId, ComponentName agent) {
Adrian Roos94e15a52015-04-16 12:23:18 -070087 addEvent(new Event(TYPE_AGENT_DIED, userId, agent, null, 0, 0, false));
Adrian Roos7a4f3d42014-05-02 12:12:20 +020088 }
89
Adrian Roos7d59b4f2014-05-27 20:01:31 +020090 public void logAgentConnected(int userId, ComponentName agent) {
Adrian Roos94e15a52015-04-16 12:23:18 -070091 addEvent(new Event(TYPE_AGENT_CONNECTED, userId, agent, null, 0, 0, false));
Adrian Roos7d59b4f2014-05-27 20:01:31 +020092 }
93
94 public void logAgentStopped(int userId, ComponentName agent) {
Adrian Roos94e15a52015-04-16 12:23:18 -070095 addEvent(new Event(TYPE_AGENT_STOPPED, userId, agent, null, 0, 0, false));
Adrian Roos7861c662014-07-25 15:37:28 +020096 }
97
98 public void logManagingTrust(int userId, ComponentName agent, boolean managing) {
Adrian Roos94e15a52015-04-16 12:23:18 -070099 addEvent(new Event(TYPE_MANAGING_TRUST, userId, agent, null, 0, 0, managing));
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200100 }
101
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200102 private void addEvent(Event e) {
103 if (mEvents.size() >= HISTORY_LIMIT) {
104 mEvents.removeFirst();
105 }
106 mEvents.addLast(e);
107 }
108
109 public void dump(PrintWriter writer, int limit, int userId, String linePrefix,
110 boolean duplicateSimpleNames) {
111 int count = 0;
112 Iterator<Event> iter = mEvents.descendingIterator();
113 while (iter.hasNext() && count < limit) {
114 Event ev = iter.next();
115 if (userId != UserHandle.USER_ALL && userId != ev.userId) {
116 continue;
117 }
118
119 writer.print(linePrefix);
120 writer.printf("#%-2d %s %s: ", count, formatElapsed(ev.elapsedTimestamp),
121 dumpType(ev.type));
122 if (userId == UserHandle.USER_ALL) {
123 writer.print("user="); writer.print(ev.userId); writer.print(", ");
124 }
125 writer.print("agent=");
126 if (duplicateSimpleNames) {
127 writer.print(ev.agent.flattenToShortString());
128 } else {
129 writer.print(getSimpleName(ev.agent));
130 }
131 switch (ev.type) {
132 case TYPE_GRANT_TRUST:
Adrian Roos94e15a52015-04-16 12:23:18 -0700133 writer.printf(", message=\"%s\", duration=%s, flags=%s",
134 ev.message, formatDuration(ev.duration), dumpGrantFlags(ev.flags));
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200135 break;
Adrian Roos7861c662014-07-25 15:37:28 +0200136 case TYPE_MANAGING_TRUST:
137 writer.printf(", managingTrust=" + ev.managingTrust);
138 break;
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200139 default:
140 }
141 writer.println();
142 count++;
143 }
144 }
145
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200146 public static String formatDuration(long duration) {
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200147 StringBuilder sb = new StringBuilder();
148 TimeUtils.formatDuration(duration, sb);
149 return sb.toString();
150 }
151
152 private static String formatElapsed(long elapsed) {
153 long delta = elapsed - SystemClock.elapsedRealtime();
154 long wallTime = delta + System.currentTimeMillis();
155 return TimeUtils.logTimeOfDay(wallTime);
156 }
157
158 /* package */ static String getSimpleName(ComponentName cn) {
159 String name = cn.getClassName();
160 int idx = name.lastIndexOf('.');
161 if (idx < name.length() && idx >= 0) {
162 return name.substring(idx + 1);
163 } else {
164 return name;
165 }
166 }
167
168 private String dumpType(int type) {
169 switch (type) {
170 case TYPE_GRANT_TRUST:
171 return "GrantTrust";
172 case TYPE_REVOKE_TRUST:
173 return "RevokeTrust";
174 case TYPE_TRUST_TIMEOUT:
175 return "TrustTimeout";
176 case TYPE_AGENT_DIED:
177 return "AgentDied";
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200178 case TYPE_AGENT_CONNECTED:
179 return "AgentConnected";
180 case TYPE_AGENT_STOPPED:
181 return "AgentStopped";
Adrian Roos7861c662014-07-25 15:37:28 +0200182 case TYPE_MANAGING_TRUST:
183 return "ManagingTrust";
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200184 default:
185 return "Unknown(" + type + ")";
186 }
187 }
Adrian Roos94e15a52015-04-16 12:23:18 -0700188
189 private String dumpGrantFlags(int flags) {
190 StringBuilder sb = new StringBuilder();
191 if ((flags & TrustAgentService.FLAG_GRANT_TRUST_INITIATED_BY_USER) != 0) {
192 if (sb.length() != 0) sb.append('|');
193 sb.append("INITIATED_BY_USER");
194 }
195 if ((flags & TrustAgentService.FLAG_GRANT_TRUST_DISMISS_KEYGUARD) != 0) {
196 if (sb.length() != 0) sb.append('|');
197 sb.append("DISMISS_KEYGUARD");
198 }
199 if (sb.length() == 0) {
200 sb.append('0');
201 }
202 return sb.toString();
203 }
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200204}