atrost | e36b1a1 | 2019-08-28 15:40:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.internal.compat; |
| 18 | |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 19 | import android.util.Log; |
| 20 | import android.util.Slog; |
atrost | e36b1a1 | 2019-08-28 15:40:37 +0100 | [diff] [blame] | 21 | |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 22 | import com.android.internal.annotations.GuardedBy; |
atrost | 46333da | 2019-10-10 14:24:21 +0100 | [diff] [blame] | 23 | import com.android.internal.annotations.VisibleForTesting; |
Muhammad Qureshi | 7310704 | 2020-01-28 10:31:03 -0800 | [diff] [blame] | 24 | import com.android.internal.util.FrameworkStatsLog; |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 25 | |
atrost | 893f84d | 2019-09-30 11:29:44 +0100 | [diff] [blame] | 26 | import java.util.HashMap; |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 27 | import java.util.HashSet; |
atrost | 893f84d | 2019-09-30 11:29:44 +0100 | [diff] [blame] | 28 | import java.util.Map; |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 29 | import java.util.Objects; |
| 30 | import java.util.Set; |
| 31 | |
atrost | e36b1a1 | 2019-08-28 15:40:37 +0100 | [diff] [blame] | 32 | /** |
| 33 | * A helper class to report changes to stats log. |
| 34 | * |
| 35 | * @hide |
| 36 | */ |
| 37 | public final class ChangeReporter { |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 38 | private static final String TAG = "CompatibilityChangeReporter"; |
| 39 | private int mSource; |
| 40 | |
atrost | 46333da | 2019-10-10 14:24:21 +0100 | [diff] [blame] | 41 | private static final class ChangeReport { |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 42 | long mChangeId; |
| 43 | int mState; |
| 44 | |
atrost | 893f84d | 2019-09-30 11:29:44 +0100 | [diff] [blame] | 45 | ChangeReport(long changeId, int state) { |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 46 | mChangeId = changeId; |
| 47 | mState = state; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public boolean equals(Object o) { |
| 52 | if (this == o) return true; |
| 53 | if (o == null || getClass() != o.getClass()) return false; |
| 54 | ChangeReport that = (ChangeReport) o; |
atrost | 893f84d | 2019-09-30 11:29:44 +0100 | [diff] [blame] | 55 | return mChangeId == that.mChangeId |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 56 | && mState == that.mState; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public int hashCode() { |
atrost | 893f84d | 2019-09-30 11:29:44 +0100 | [diff] [blame] | 61 | return Objects.hash(mChangeId, mState); |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
atrost | 893f84d | 2019-09-30 11:29:44 +0100 | [diff] [blame] | 65 | // Maps uid to a set of ChangeReports (that were reported for that uid). |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 66 | @GuardedBy("mReportedChanges") |
atrost | 893f84d | 2019-09-30 11:29:44 +0100 | [diff] [blame] | 67 | private final Map<Integer, Set<ChangeReport>> mReportedChanges; |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 68 | |
atrost | 46333da | 2019-10-10 14:24:21 +0100 | [diff] [blame] | 69 | // When true will of every time to debug (logcat). |
| 70 | private boolean mDebugLogAll; |
| 71 | |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 72 | public ChangeReporter(int source) { |
| 73 | mSource = source; |
atrost | 893f84d | 2019-09-30 11:29:44 +0100 | [diff] [blame] | 74 | mReportedChanges = new HashMap<>(); |
atrost | 46333da | 2019-10-10 14:24:21 +0100 | [diff] [blame] | 75 | mDebugLogAll = false; |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
atrost | 893f84d | 2019-09-30 11:29:44 +0100 | [diff] [blame] | 79 | * Report the change to stats log and to the debug log if the change was not previously |
| 80 | * logged already. |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 81 | * |
| 82 | * @param uid affected by the change |
| 83 | * @param changeId the reported change id |
| 84 | * @param state of the reported change - enabled/disabled/only logged |
| 85 | */ |
| 86 | public void reportChange(int uid, long changeId, int state) { |
atrost | 46333da | 2019-10-10 14:24:21 +0100 | [diff] [blame] | 87 | if (shouldWriteToStatsLog(uid, changeId, state)) { |
Muhammad Qureshi | 7310704 | 2020-01-28 10:31:03 -0800 | [diff] [blame] | 88 | FrameworkStatsLog.write(FrameworkStatsLog.APP_COMPATIBILITY_CHANGE_REPORTED, uid, |
| 89 | changeId, state, mSource); |
atrost | 46333da | 2019-10-10 14:24:21 +0100 | [diff] [blame] | 90 | } |
| 91 | if (shouldWriteToDebug(uid, changeId, state)) { |
| 92 | debugLog(uid, changeId, state); |
| 93 | } |
| 94 | markAsReported(uid, new ChangeReport(changeId, state)); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Start logging all the time to logcat. |
| 99 | */ |
| 100 | public void startDebugLogAll() { |
| 101 | mDebugLogAll = true; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Stop logging all the time to logcat. |
| 106 | */ |
| 107 | public void stopDebugLogAll() { |
| 108 | mDebugLogAll = false; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * Returns whether the next report should be logged to statsLog. |
| 114 | * |
| 115 | * @param uid affected by the change |
| 116 | * @param changeId the reported change id |
| 117 | * @param state of the reported change - enabled/disabled/only logged |
| 118 | * @return true if the report should be logged |
| 119 | */ |
| 120 | @VisibleForTesting |
| 121 | public boolean shouldWriteToStatsLog(int uid, long changeId, int state) { |
| 122 | return !isAlreadyReported(uid, new ChangeReport(changeId, state)); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns whether the next report should be logged to logcat. |
| 127 | * |
| 128 | * @param uid affected by the change |
| 129 | * @param changeId the reported change id |
| 130 | * @param state of the reported change - enabled/disabled/only logged |
| 131 | * @return true if the report should be logged |
| 132 | */ |
| 133 | @VisibleForTesting |
| 134 | public boolean shouldWriteToDebug(int uid, long changeId, int state) { |
| 135 | return mDebugLogAll || !isAlreadyReported(uid, new ChangeReport(changeId, state)); |
| 136 | } |
| 137 | |
| 138 | private boolean isAlreadyReported(int uid, ChangeReport report) { |
| 139 | synchronized (mReportedChanges) { |
| 140 | Set<ChangeReport> reportedChangesForUid = mReportedChanges.get(uid); |
| 141 | if (reportedChangesForUid == null) { |
| 142 | return false; |
| 143 | } else { |
| 144 | return reportedChangesForUid.contains(report); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | private void markAsReported(int uid, ChangeReport report) { |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 150 | synchronized (mReportedChanges) { |
atrost | 893f84d | 2019-09-30 11:29:44 +0100 | [diff] [blame] | 151 | Set<ChangeReport> reportedChangesForUid = mReportedChanges.get(uid); |
| 152 | if (reportedChangesForUid == null) { |
| 153 | mReportedChanges.put(uid, new HashSet<ChangeReport>()); |
| 154 | reportedChangesForUid = mReportedChanges.get(uid); |
| 155 | } |
atrost | 46333da | 2019-10-10 14:24:21 +0100 | [diff] [blame] | 156 | reportedChangesForUid.add(report); |
atrost | 893f84d | 2019-09-30 11:29:44 +0100 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Clears the saved information about a given uid. Requests to report uid again will be reported |
| 162 | * regardless to the past reports. |
| 163 | * |
| 164 | * <p> Only intended to be called from PlatformCompat. |
| 165 | * |
| 166 | * @param uid to reset |
| 167 | */ |
| 168 | public void resetReportedChanges(int uid) { |
| 169 | synchronized (mReportedChanges) { |
| 170 | mReportedChanges.remove(uid); |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | private void debugLog(int uid, long changeId, int state) { |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 175 | String message = String.format("Compat change id reported: %d; UID %d; state: %s", changeId, |
| 176 | uid, stateToString(state)); |
Muhammad Qureshi | 7310704 | 2020-01-28 10:31:03 -0800 | [diff] [blame] | 177 | if (mSource == FrameworkStatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__SYSTEM_SERVER) { |
atrost | 6624ffa | 2019-09-20 16:30:07 +0100 | [diff] [blame] | 178 | Slog.d(TAG, message); |
| 179 | } else { |
| 180 | Log.d(TAG, message); |
| 181 | } |
| 182 | |
| 183 | } |
atrost | e36b1a1 | 2019-08-28 15:40:37 +0100 | [diff] [blame] | 184 | |
| 185 | /** |
Muhammad Qureshi | 7310704 | 2020-01-28 10:31:03 -0800 | [diff] [blame] | 186 | * Transforms FrameworkStatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE enum to a string. |
atrost | e36b1a1 | 2019-08-28 15:40:37 +0100 | [diff] [blame] | 187 | * |
| 188 | * @param state to transform |
| 189 | * @return a string representing the state |
| 190 | */ |
| 191 | private static String stateToString(int state) { |
| 192 | switch (state) { |
Muhammad Qureshi | 7310704 | 2020-01-28 10:31:03 -0800 | [diff] [blame] | 193 | case FrameworkStatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__LOGGED: |
atrost | e36b1a1 | 2019-08-28 15:40:37 +0100 | [diff] [blame] | 194 | return "LOGGED"; |
Muhammad Qureshi | 7310704 | 2020-01-28 10:31:03 -0800 | [diff] [blame] | 195 | case FrameworkStatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__ENABLED: |
atrost | e36b1a1 | 2019-08-28 15:40:37 +0100 | [diff] [blame] | 196 | return "ENABLED"; |
Muhammad Qureshi | 7310704 | 2020-01-28 10:31:03 -0800 | [diff] [blame] | 197 | case FrameworkStatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE__DISABLED: |
atrost | e36b1a1 | 2019-08-28 15:40:37 +0100 | [diff] [blame] | 198 | return "DISABLED"; |
| 199 | default: |
| 200 | return "UNKNOWN"; |
| 201 | } |
| 202 | } |
atrost | e36b1a1 | 2019-08-28 15:40:37 +0100 | [diff] [blame] | 203 | } |