1 /* 2 * Copyright (C) 2020 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.notification; 18 19 import android.util.StatsEvent; 20 21 import com.android.server.notification.SysUiStatsEvent.Builder; 22 23 import java.util.ArrayList; 24 import java.util.HashMap; 25 import java.util.List; 26 27 /** 28 * Wrapper for SysUiStatsEvent that implements validation. 29 */ 30 public class WrappedSysUiStatsEvent { 31 32 static class WrappedBuilder extends Builder { 33 private ArrayList<Object> mValues; 34 private HashMap<Integer, HashMap<Byte, Object>> mAnnotations; 35 private int mAtomId; 36 private int mLastIndex; 37 private boolean mBuilt; 38 WrappedBuilder(StatsEvent.Builder builder)39 WrappedBuilder(StatsEvent.Builder builder) { 40 super(builder); 41 mValues = new ArrayList<>(); 42 mAnnotations = new HashMap<>(); 43 mValues.add(0); // proto fields are 1-based 44 } 45 46 @Override setAtomId(int atomId)47 public Builder setAtomId(int atomId) { 48 mAtomId = atomId; 49 super.setAtomId(atomId); 50 return this; 51 } 52 53 @Override writeInt(int value)54 public Builder writeInt(int value) { 55 addValue(Integer.valueOf(value)); 56 super.writeInt(value); 57 return this; 58 } 59 60 @Override addBooleanAnnotation(byte annotation, boolean value)61 public Builder addBooleanAnnotation(byte annotation, boolean value) { 62 addAnnotation(annotation, Boolean.valueOf(value)); 63 super.addBooleanAnnotation(annotation, value); 64 return this; 65 } 66 67 @Override writeString(String value)68 public Builder writeString(String value) { 69 addValue(value); 70 super.writeString(value); 71 return this; 72 } 73 74 @Override writeBoolean(boolean value)75 public Builder writeBoolean(boolean value) { 76 addValue(Boolean.valueOf(value)); 77 super.writeBoolean(value); 78 return this; 79 } 80 81 @Override build()82 public StatsEvent build() { 83 mBuilt = true; 84 return super.build(); 85 } 86 getValue(int index)87 public Object getValue(int index) { 88 return index < mValues.size() ? mValues.get(index) : null; 89 } 90 91 /** useful to make assertTrue() statements more readable. */ getBoolean(int index)92 public boolean getBoolean(int index) { 93 return (Boolean) mValues.get(index); 94 } 95 96 /** useful to make assertTrue() statements more readable. */ getInt(int index)97 public int getInt(int index) { 98 return (Integer) mValues.get(index); 99 } 100 101 /** useful to make assertTrue() statements more readable. */ getString(int index)102 public String getString(int index) { 103 return (String) mValues.get(index); 104 } 105 addValue(Object value)106 private void addValue(Object value) { 107 mLastIndex = mValues.size(); 108 mValues.add(value); 109 } 110 addAnnotation(byte annotation, Object value)111 private void addAnnotation(byte annotation, Object value) { 112 Integer key = Integer.valueOf(mLastIndex); 113 if (!mAnnotations.containsKey(key)) { 114 mAnnotations.put(key, new HashMap<>()); 115 } 116 mAnnotations.get(key).put(Byte.valueOf(annotation), value); 117 } 118 getBooleanAnnotation(int i, byte a)119 public boolean getBooleanAnnotation(int i, byte a) { 120 return ((Boolean) mAnnotations.get(Integer.valueOf(i)).get(Byte.valueOf(a))) 121 .booleanValue(); 122 } 123 getAtomId()124 public int getAtomId() { 125 return mAtomId; 126 } 127 } 128 129 static class WrappedBuilderFactory extends SysUiStatsEvent.BuilderFactory { 130 public List<WrappedBuilder> builders; 131 WrappedBuilderFactory()132 WrappedBuilderFactory() { 133 builders = new ArrayList<>(); 134 } 135 136 @Override newBuilder()137 Builder newBuilder() { 138 WrappedBuilder b = new WrappedBuilder(StatsEvent.newBuilder()); 139 builders.add(b); 140 return b; 141 } 142 } 143 } 144