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