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 17 package com.google.android.setupcompat.logging; 18 19 import static com.google.android.setupcompat.internal.Validations.assertLengthInRange; 20 21 import android.os.Bundle; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 import androidx.annotation.NonNull; 25 import com.google.android.setupcompat.internal.Preconditions; 26 import com.google.android.setupcompat.util.ObjectUtils; 27 import java.util.regex.Pattern; 28 29 /** 30 * A metric key represents a validated “string key” and a "screen name" that is associated with the 31 * values reported by the API consumer. 32 */ 33 public final class MetricKey implements Parcelable { 34 35 private static final String METRIC_KEY_BUNDLE_NAME_KEY = "MetricKey_name"; 36 private static final String METRIC_KEY_BUNDLE_SCREEN_NAME_KEY = "MetricKey_screenName"; 37 private static final String METRIC_KEY_BUNDLE_VERSION = "MetricKey_version"; 38 private static final int VERSION = 1; 39 40 /** 41 * Creates a new instance of MetricKey. 42 * 43 * <p>NOTE: 44 * 45 * <ul> 46 * <li>Length of {@code name} should be in range of 5-30 characters, only alpha numeric 47 * characters are allowed. 48 * <li>Length of {@code screenName} should be in range of 5-50 characters, only alpha numeric 49 * characters are allowed. 50 * </ul> 51 */ get(@onNull String name, @NonNull String screenName)52 public static MetricKey get(@NonNull String name, @NonNull String screenName) { 53 assertLengthInRange(name, "MetricKey.name", MIN_METRIC_KEY_LENGTH, MAX_METRIC_KEY_LENGTH); 54 assertLengthInRange( 55 screenName, "MetricKey.screenName", MIN_SCREEN_NAME_LENGTH, MAX_SCREEN_NAME_LENGTH); 56 Preconditions.checkArgument( 57 METRIC_KEY_PATTERN.matcher(name).matches(), 58 "Invalid MetricKey, only alpha numeric characters are allowed."); 59 Preconditions.checkArgument( 60 METRIC_KEY_PATTERN.matcher(screenName).matches(), 61 "Invalid MetricKey, only alpha numeric characters are allowed."); 62 return new MetricKey(name, screenName); 63 } 64 65 /** Converts {@link MetricKey} into {@link Bundle}. */ fromMetricKey(MetricKey metricKey)66 public static Bundle fromMetricKey(MetricKey metricKey) { 67 Preconditions.checkNotNull(metricKey, "MetricKey cannot be null."); 68 Bundle bundle = new Bundle(); 69 bundle.putInt(METRIC_KEY_BUNDLE_VERSION, VERSION); 70 bundle.putString(METRIC_KEY_BUNDLE_NAME_KEY, metricKey.name()); 71 bundle.putString(METRIC_KEY_BUNDLE_SCREEN_NAME_KEY, metricKey.screenName()); 72 return bundle; 73 } 74 75 /** Converts {@link Bundle} into {@link MetricKey}. */ toMetricKey(Bundle bundle)76 public static MetricKey toMetricKey(Bundle bundle) { 77 Preconditions.checkNotNull(bundle, "Bundle cannot be null"); 78 return MetricKey.get( 79 bundle.getString(METRIC_KEY_BUNDLE_NAME_KEY), 80 bundle.getString(METRIC_KEY_BUNDLE_SCREEN_NAME_KEY)); 81 } 82 83 public static final Creator<MetricKey> CREATOR = 84 new Creator<MetricKey>() { 85 @Override 86 public MetricKey createFromParcel(Parcel in) { 87 return new MetricKey(in.readString(), in.readString()); 88 } 89 90 @Override 91 public MetricKey[] newArray(int size) { 92 return new MetricKey[size]; 93 } 94 }; 95 96 /** Returns the name of the metric key. */ name()97 public String name() { 98 return name; 99 } 100 101 /** Returns the name of the metric key. */ screenName()102 public String screenName() { 103 return screenName; 104 } 105 106 @Override describeContents()107 public int describeContents() { 108 return 0; 109 } 110 111 @Override writeToParcel(Parcel parcel, int i)112 public void writeToParcel(Parcel parcel, int i) { 113 parcel.writeString(name); 114 parcel.writeString(screenName); 115 } 116 117 @Override equals(Object o)118 public boolean equals(Object o) { 119 if (this == o) { 120 return true; 121 } 122 if (!(o instanceof MetricKey)) { 123 return false; 124 } 125 MetricKey metricKey = (MetricKey) o; 126 return ObjectUtils.equals(name, metricKey.name) 127 && ObjectUtils.equals(screenName, metricKey.screenName); 128 } 129 130 @Override hashCode()131 public int hashCode() { 132 return ObjectUtils.hashCode(name, screenName); 133 } 134 MetricKey(String name, String screenName)135 private MetricKey(String name, String screenName) { 136 this.name = name; 137 this.screenName = screenName; 138 } 139 140 private final String name; 141 private final String screenName; 142 143 private static final int MIN_SCREEN_NAME_LENGTH = 5; 144 private static final int MIN_METRIC_KEY_LENGTH = 5; 145 private static final int MAX_SCREEN_NAME_LENGTH = 50; 146 private static final int MAX_METRIC_KEY_LENGTH = 30; 147 private static final Pattern METRIC_KEY_PATTERN = Pattern.compile("^[a-zA-Z][a-zA-Z0-9_]+"); 148 } 149