Home | History | Annotate | Download | only in metrics
      1 /*
      2  * Copyright (C) 2018 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 package com.android.cts.devicepolicy.metrics;
     17 
     18 import android.stats.devicepolicy.StringList;
     19 import android.stats.devicepolicy.EventId;
     20 import com.android.os.AtomsProto.DevicePolicyEvent;
     21 import java.util.Arrays;
     22 import java.util.Locale;
     23 import java.util.Objects;
     24 
     25 /**
     26  * Wrapper over <code>DevicePolicyEvent</code> atom as defined in
     27  * <code>frameworks/base/cmds/statsd/src/atoms.proto</code>.
     28  * @see Builder
     29  */
     30 public final class DevicePolicyEventWrapper {
     31     private final int mEventId;
     32     private final int mIntValue;
     33     private final boolean mBooleanValue;
     34     private final long mTimePeriodMs;
     35     private final String[] mStringArrayValue;
     36     private final String mAdminPackageName;
     37 
     38     private DevicePolicyEventWrapper(Builder builder) {
     39         mEventId = builder.mEventId;
     40         mIntValue = builder.mIntValue;
     41         mBooleanValue = builder.mBooleanValue;
     42         mTimePeriodMs = builder.mTimePeriodMs;
     43         mStringArrayValue = builder.mStringArrayValue;
     44         mAdminPackageName = builder.mAdminPackageName;
     45     }
     46 
     47     /**
     48      * Constructs a {@link DevicePolicyEventWrapper} from a <code>DevicePolicyEvent</code> atom.
     49      */
     50     static DevicePolicyEventWrapper fromDevicePolicyAtom(DevicePolicyEvent atom) {
     51         return new Builder(atom.getEventId().getNumber())
     52                 .setAdminPackageName(atom.getAdminPackageName())
     53                 .setInt(atom.getIntegerValue())
     54                 .setBoolean(atom.getBooleanValue())
     55                 .setTimePeriod(atom.getTimePeriodMillis())
     56                 .setStrings(getStringArray(atom.getStringListValue()))
     57                 .build();
     58     }
     59 
     60     /**
     61      * Converts a <code>StringList</code> proto object to <code>String[]</code>.
     62      */
     63     private static String[] getStringArray(StringList stringList) {
     64         final int count = stringList.getStringValueCount();
     65         if (count == 0) {
     66             return null;
     67         }
     68         final String[] result = new String[count];
     69         for (int i = 0; i < count; i++) {
     70             result[i] = stringList.getStringValue(i);
     71         }
     72         return result;
     73     }
     74 
     75     int getEventId() {
     76         return mEventId;
     77     }
     78 
     79     @Override
     80     public boolean equals(Object obj) {
     81         if (!(obj instanceof DevicePolicyEventWrapper)) {
     82             return false;
     83         }
     84         final DevicePolicyEventWrapper other = (DevicePolicyEventWrapper) obj;
     85         return mEventId == other.mEventId
     86                 && mIntValue == other.mIntValue
     87                 && mBooleanValue == other.mBooleanValue
     88                 && mTimePeriodMs == other.mTimePeriodMs
     89                 && Objects.equals(mAdminPackageName, other.mAdminPackageName)
     90                 && Arrays.equals(mStringArrayValue, other.mStringArrayValue);
     91     }
     92 
     93     @Override
     94     public String toString() {
     95         return String.format(Locale.getDefault(), "{ eventId: %s(%d), intValue: %d, "
     96                         + "booleanValue: %s, timePeriodMs: %d, adminPackageName: %s, "
     97                         + "stringArrayValue: %s }",
     98                 EventId.forNumber(mEventId), mEventId, mIntValue, mBooleanValue,
     99                 mTimePeriodMs, mAdminPackageName, Arrays.toString(mStringArrayValue));
    100     }
    101 
    102     /**
    103      * Builder class for {@link DevicePolicyEventWrapper}.
    104      */
    105     public static class Builder {
    106         private final int mEventId;
    107         private int mIntValue;
    108         private boolean mBooleanValue;
    109         private long mTimePeriodMs;
    110         private String[] mStringArrayValue;
    111         // Default value for Strings when getting dump is "", not null.
    112         private String mAdminPackageName = "";
    113 
    114         public Builder(int eventId) {
    115             this.mEventId = eventId;
    116         }
    117 
    118         public Builder setInt(int value) {
    119             mIntValue = value;
    120             return this;
    121         }
    122 
    123         public Builder setBoolean(boolean value) {
    124             mBooleanValue = value;
    125             return this;
    126         }
    127 
    128         public Builder setTimePeriod(long timePeriodMs) {
    129             mTimePeriodMs = timePeriodMs;
    130             return this;
    131         }
    132 
    133         public Builder setStrings(String... values) {
    134             mStringArrayValue = values;
    135             return this;
    136         }
    137 
    138         public Builder setStrings(String value, String[] values) {
    139             if (values == null) {
    140                 throw new IllegalArgumentException("values cannot be null.");
    141             }
    142             mStringArrayValue = new String[values.length + 1];
    143             mStringArrayValue[0] = value;
    144             System.arraycopy(values, 0, mStringArrayValue, 1, values.length);
    145             return this;
    146         }
    147 
    148         public Builder setStrings(String value1, String value2, String[] values) {
    149             if (values == null) {
    150                 throw new IllegalArgumentException("values cannot be null.");
    151             }
    152             mStringArrayValue = new String[values.length + 2];
    153             mStringArrayValue[0] = value1;
    154             mStringArrayValue[1] = value2;
    155             System.arraycopy(values, 0, mStringArrayValue, 2, values.length);
    156             return this;
    157         }
    158 
    159         public Builder setAdminPackageName(String packageName) {
    160             mAdminPackageName = packageName;
    161             return this;
    162         }
    163 
    164         public DevicePolicyEventWrapper build() {
    165             return new DevicePolicyEventWrapper(this);
    166         }
    167     }
    168 }
    169