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.internal.logging; 18 19 import static java.lang.Math.max; 20 import static java.lang.Math.min; 21 22 import com.android.internal.annotations.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 30 * first use; try to give it a long lifetime. Safe for concurrent use. 31 */ 32 public class InstanceIdSequence { 33 protected final int mInstanceIdMax; 34 private final Random mRandom = new SecureRandom(); 35 36 /** 37 * Constructs a sequence with identifiers [1, instanceIdMax]. Capped at INSTANCE_ID_MAX. 38 * @param instanceIdMax Limiting value of identifiers. Normally positive: otherwise you get 39 * an all-1 sequence. 40 */ InstanceIdSequence(int instanceIdMax)41 public InstanceIdSequence(int instanceIdMax) { 42 mInstanceIdMax = min(max(1, instanceIdMax), InstanceId.INSTANCE_ID_MAX); 43 } 44 45 /** 46 * Gets the next instance from the sequence. Safe for concurrent use. 47 * @return new InstanceId 48 */ newInstanceId()49 public InstanceId newInstanceId() { 50 return newInstanceIdInternal(1 + mRandom.nextInt(mInstanceIdMax)); 51 } 52 53 /** 54 * Factory function for instance IDs, used for testing. 55 * @param id 56 * @return new InstanceId(id) 57 */ 58 @VisibleForTesting newInstanceIdInternal(int id)59 protected InstanceId newInstanceIdInternal(int id) { 60 return new InstanceId(id); 61 } 62 } 63