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.launcher3.logging; 18 19 import static java.lang.Math.max; 20 import static java.lang.Math.min; 21 22 import androidx.annotation.VisibleForTesting; 23 24 import java.security.SecureRandom; 25 import java.util.Random; 26 27 /** 28 * Generates random InstanceIds in range [1, instanceIdMax] for passing to 29 * UiEventLogger.logWithInstanceId(). Holds a SecureRandom, which self-seeds on first use; try to 30 * give it a long lifetime. Safe for concurrent use. 31 * 32 * Copy of frameworks/base/core/java/com/android/internal/logging/InstanceIdSequence.java 33 */ 34 public class InstanceIdSequence { 35 protected final int mInstanceIdMax; 36 private final Random mRandom = new SecureRandom(); 37 38 /** 39 * Constructs a sequence with identifiers [1, instanceIdMax]. Capped at INSTANCE_ID_MAX. 40 * @param instanceIdMax Limiting value of identifiers. Normally positive: otherwise you get 41 * an all-1 sequence. 42 */ InstanceIdSequence(int instanceIdMax)43 public InstanceIdSequence(int instanceIdMax) { 44 mInstanceIdMax = min(max(1, instanceIdMax), InstanceId.INSTANCE_ID_MAX); 45 } 46 47 /** 48 * Constructs a sequence with identifiers [1, InstanceId.INSTANCE_ID_MAX]. 49 */ InstanceIdSequence()50 public InstanceIdSequence() { 51 this(InstanceId.INSTANCE_ID_MAX); 52 } 53 54 /** 55 * Gets the next instance from the sequence. Safe for concurrent use. 56 * @return new InstanceId 57 */ newInstanceId()58 public InstanceId newInstanceId() { 59 return newInstanceIdInternal(1 + mRandom.nextInt(mInstanceIdMax)); 60 } 61 62 /** 63 * Factory function for instance IDs, used for testing. 64 * @param id 65 * @return new InstanceId(id) 66 */ 67 @VisibleForTesting newInstanceIdInternal(int id)68 protected InstanceId newInstanceIdInternal(int id) { 69 return new InstanceId(id); 70 } 71 } 72