1 /* 2 * Copyright (C) 2012 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.os; 18 19 import android.compat.annotation.UnsupportedAppUsage; 20 import android.os.Build; 21 22 /** 23 * Helper class for passing more arguments though a message 24 * and avoiding allocation of a custom class for wrapping the 25 * arguments. This class maintains a pool of instances and 26 * it is responsibility of the client to recycle and instance 27 * once it is no longer used. 28 */ 29 public final class SomeArgs { 30 31 private static final int MAX_POOL_SIZE = 10; 32 33 private static SomeArgs sPool; 34 private static int sPoolSize; 35 private static Object sPoolLock = new Object(); 36 37 private SomeArgs mNext; 38 39 private boolean mInPool; 40 41 static final int WAIT_NONE = 0; 42 static final int WAIT_WAITING = 1; 43 static final int WAIT_FINISHED = 2; 44 int mWaitState = WAIT_NONE; 45 46 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 47 public Object arg1; 48 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 49 public Object arg2; 50 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 51 public Object arg3; 52 public Object arg4; 53 public Object arg5; 54 public Object arg6; 55 public Object arg7; 56 public Object arg8; 57 public Object arg9; 58 public int argi1; 59 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 60 public int argi2; 61 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) 62 public int argi3; 63 public int argi4; 64 public int argi5; 65 public int argi6; 66 SomeArgs()67 private SomeArgs() { 68 /* do nothing - reduce visibility */ 69 } 70 71 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) obtain()72 public static SomeArgs obtain() { 73 synchronized (sPoolLock) { 74 if (sPoolSize > 0) { 75 SomeArgs args = sPool; 76 sPool = sPool.mNext; 77 args.mNext = null; 78 args.mInPool = false; 79 sPoolSize--; 80 return args; 81 } else { 82 return new SomeArgs(); 83 } 84 } 85 } 86 complete()87 public void complete() { 88 synchronized (this) { 89 if (mWaitState != WAIT_WAITING) { 90 throw new IllegalStateException("Not waiting"); 91 } 92 mWaitState = WAIT_FINISHED; 93 notifyAll(); 94 } 95 } 96 97 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) recycle()98 public void recycle() { 99 if (mInPool) { 100 throw new IllegalStateException("Already recycled."); 101 } 102 if (mWaitState != WAIT_NONE) { 103 return; 104 } 105 synchronized (sPoolLock) { 106 clear(); 107 if (sPoolSize < MAX_POOL_SIZE) { 108 mNext = sPool; 109 mInPool = true; 110 sPool = this; 111 sPoolSize++; 112 } 113 } 114 } 115 clear()116 private void clear() { 117 arg1 = null; 118 arg2 = null; 119 arg3 = null; 120 arg4 = null; 121 arg5 = null; 122 arg6 = null; 123 arg7 = null; 124 arg8 = null; 125 arg9 = null; 126 argi1 = 0; 127 argi2 = 0; 128 argi3 = 0; 129 argi4 = 0; 130 argi5 = 0; 131 argi6 = 0; 132 } 133 } 134