Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.trust; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.os.SystemClock; |
| 21 | import android.os.UserHandle; |
| 22 | import android.util.TimeUtils; |
| 23 | |
| 24 | import java.io.PrintWriter; |
| 25 | import java.util.ArrayDeque; |
| 26 | import java.util.Iterator; |
| 27 | |
| 28 | /** |
| 29 | * An archive of trust events. |
| 30 | */ |
| 31 | public class TrustArchive { |
| 32 | private static final int TYPE_GRANT_TRUST = 0; |
| 33 | private static final int TYPE_REVOKE_TRUST = 1; |
| 34 | private static final int TYPE_TRUST_TIMEOUT = 2; |
| 35 | private static final int TYPE_AGENT_DIED = 3; |
Adrian Roos | 7d59b4f | 2014-05-27 20:01:31 +0200 | [diff] [blame] | 36 | private static final int TYPE_AGENT_CONNECTED = 4; |
| 37 | private static final int TYPE_AGENT_STOPPED = 5; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 38 | private static final int TYPE_MANAGING_TRUST = 6; |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 39 | |
| 40 | private static final int HISTORY_LIMIT = 200; |
| 41 | |
| 42 | private static class Event { |
| 43 | final int type; |
| 44 | final int userId; |
| 45 | final ComponentName agent; |
| 46 | final long elapsedTimestamp; |
| 47 | |
| 48 | // grantTrust |
| 49 | final String message; |
| 50 | final long duration; |
| 51 | final boolean userInitiated; |
| 52 | |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 53 | // managingTrust |
| 54 | final boolean managingTrust; |
| 55 | |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 56 | private Event(int type, int userId, ComponentName agent, String message, |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 57 | long duration, boolean userInitiated, boolean managingTrust) { |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 58 | this.type = type; |
| 59 | this.userId = userId; |
| 60 | this.agent = agent; |
| 61 | this.elapsedTimestamp = SystemClock.elapsedRealtime(); |
| 62 | this.message = message; |
| 63 | this.duration = duration; |
| 64 | this.userInitiated = userInitiated; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 65 | this.managingTrust = managingTrust; |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | ArrayDeque<Event> mEvents = new ArrayDeque<Event>(); |
| 70 | |
| 71 | public void logGrantTrust(int userId, ComponentName agent, String message, |
| 72 | long duration, boolean userInitiated) { |
| 73 | addEvent(new Event(TYPE_GRANT_TRUST, userId, agent, message, duration, |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 74 | userInitiated, false)); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | public void logRevokeTrust(int userId, ComponentName agent) { |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 78 | addEvent(new Event(TYPE_REVOKE_TRUST, userId, agent, null, 0, false, false)); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | public void logTrustTimeout(int userId, ComponentName agent) { |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 82 | addEvent(new Event(TYPE_TRUST_TIMEOUT, userId, agent, null, 0, false, false)); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | public void logAgentDied(int userId, ComponentName agent) { |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 86 | addEvent(new Event(TYPE_AGENT_DIED, userId, agent, null, 0, false, false)); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Adrian Roos | 7d59b4f | 2014-05-27 20:01:31 +0200 | [diff] [blame] | 89 | public void logAgentConnected(int userId, ComponentName agent) { |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 90 | addEvent(new Event(TYPE_AGENT_CONNECTED, userId, agent, null, 0, false, false)); |
Adrian Roos | 7d59b4f | 2014-05-27 20:01:31 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | public void logAgentStopped(int userId, ComponentName agent) { |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 94 | addEvent(new Event(TYPE_AGENT_STOPPED, userId, agent, null, 0, false, false)); |
| 95 | } |
| 96 | |
| 97 | public void logManagingTrust(int userId, ComponentName agent, boolean managing) { |
| 98 | addEvent(new Event(TYPE_MANAGING_TRUST, userId, agent, null, 0, false, managing)); |
Adrian Roos | 7d59b4f | 2014-05-27 20:01:31 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 101 | private void addEvent(Event e) { |
| 102 | if (mEvents.size() >= HISTORY_LIMIT) { |
| 103 | mEvents.removeFirst(); |
| 104 | } |
| 105 | mEvents.addLast(e); |
| 106 | } |
| 107 | |
| 108 | public void dump(PrintWriter writer, int limit, int userId, String linePrefix, |
| 109 | boolean duplicateSimpleNames) { |
| 110 | int count = 0; |
| 111 | Iterator<Event> iter = mEvents.descendingIterator(); |
| 112 | while (iter.hasNext() && count < limit) { |
| 113 | Event ev = iter.next(); |
| 114 | if (userId != UserHandle.USER_ALL && userId != ev.userId) { |
| 115 | continue; |
| 116 | } |
| 117 | |
| 118 | writer.print(linePrefix); |
| 119 | writer.printf("#%-2d %s %s: ", count, formatElapsed(ev.elapsedTimestamp), |
| 120 | dumpType(ev.type)); |
| 121 | if (userId == UserHandle.USER_ALL) { |
| 122 | writer.print("user="); writer.print(ev.userId); writer.print(", "); |
| 123 | } |
| 124 | writer.print("agent="); |
| 125 | if (duplicateSimpleNames) { |
| 126 | writer.print(ev.agent.flattenToShortString()); |
| 127 | } else { |
| 128 | writer.print(getSimpleName(ev.agent)); |
| 129 | } |
| 130 | switch (ev.type) { |
| 131 | case TYPE_GRANT_TRUST: |
Adrian Roos | 2bb8bfd | 2014-08-13 02:06:54 +0200 | [diff] [blame] | 132 | writer.printf(", message=\"%s\", duration=%s, initiatedByUser=%d", |
| 133 | ev.message, formatDuration(ev.duration), ev.userInitiated ? 1 : 0); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 134 | break; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 135 | case TYPE_MANAGING_TRUST: |
| 136 | writer.printf(", managingTrust=" + ev.managingTrust); |
| 137 | break; |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 138 | default: |
| 139 | } |
| 140 | writer.println(); |
| 141 | count++; |
| 142 | } |
| 143 | } |
| 144 | |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 145 | public static String formatDuration(long duration) { |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 146 | StringBuilder sb = new StringBuilder(); |
| 147 | TimeUtils.formatDuration(duration, sb); |
| 148 | return sb.toString(); |
| 149 | } |
| 150 | |
| 151 | private static String formatElapsed(long elapsed) { |
| 152 | long delta = elapsed - SystemClock.elapsedRealtime(); |
| 153 | long wallTime = delta + System.currentTimeMillis(); |
| 154 | return TimeUtils.logTimeOfDay(wallTime); |
| 155 | } |
| 156 | |
| 157 | /* package */ static String getSimpleName(ComponentName cn) { |
| 158 | String name = cn.getClassName(); |
| 159 | int idx = name.lastIndexOf('.'); |
| 160 | if (idx < name.length() && idx >= 0) { |
| 161 | return name.substring(idx + 1); |
| 162 | } else { |
| 163 | return name; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | private String dumpType(int type) { |
| 168 | switch (type) { |
| 169 | case TYPE_GRANT_TRUST: |
| 170 | return "GrantTrust"; |
| 171 | case TYPE_REVOKE_TRUST: |
| 172 | return "RevokeTrust"; |
| 173 | case TYPE_TRUST_TIMEOUT: |
| 174 | return "TrustTimeout"; |
| 175 | case TYPE_AGENT_DIED: |
| 176 | return "AgentDied"; |
Adrian Roos | 7d59b4f | 2014-05-27 20:01:31 +0200 | [diff] [blame] | 177 | case TYPE_AGENT_CONNECTED: |
| 178 | return "AgentConnected"; |
| 179 | case TYPE_AGENT_STOPPED: |
| 180 | return "AgentStopped"; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 181 | case TYPE_MANAGING_TRUST: |
| 182 | return "ManagingTrust"; |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 183 | default: |
| 184 | return "Unknown(" + type + ")"; |
| 185 | } |
| 186 | } |
| 187 | } |