1 /* 2 * Copyright (C) 2016 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.packageinstaller.shadows; 18 19 import android.content.Context; 20 21 import com.android.internal.logging.MetricsLogger; 22 23 import libcore.util.Objects; 24 25 import org.robolectric.annotation.Implementation; 26 import org.robolectric.annotation.Implements; 27 28 import java.util.ArrayList; 29 30 /** 31 * MetricsLogger that just adds logs to a list 32 */ 33 @Implements(MetricsLogger.class) 34 public class ShadowMetricsLogger { 35 /** Collected logs */ 36 private static ArrayList<Log> sLogs = new ArrayList<>(); 37 38 /** 39 * Clear all previously collected logs 40 */ clearLogs()41 public static void clearLogs() { 42 sLogs.clear(); 43 } 44 45 /** 46 * @return All logs collected since the last {@link #clearLogs()}. 47 */ getLogs()48 public static ArrayList<Log> getLogs() { 49 return sLogs; 50 } 51 52 @Implementation action(Context context, int category, String pkg)53 public static void action(Context context, int category, String pkg) { 54 sLogs.add(new Log(context, category, pkg)); 55 } 56 57 public static class Log { 58 public final Context context; 59 public final int category; 60 public final String pkg; 61 Log(Context context, int category, String pkg)62 public Log(Context context, int category, String pkg) { 63 this.context = context; 64 this.category = category; 65 this.pkg = pkg; 66 } 67 68 @Override equals(Object obj)69 public boolean equals(Object obj) { 70 if (obj == null || !(obj instanceof Log)) { 71 return false; 72 } else { 73 Log otherLog = (Log) obj; 74 75 return Objects.equal(otherLog.context, context) && otherLog.category == category 76 && Objects.equal(otherLog.pkg, pkg); 77 } 78 } 79 80 @Override hashCode()81 public int hashCode() { 82 return ((context == null) ? 0 : context.hashCode()) + category + ((pkg == null) ? 0 83 : pkg.hashCode()); 84 } 85 } 86 } 87