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 @android.ravenwood.annotation.RavenwoodKeepWholeClass
30 public final class SomeArgs {
31 
32     private static final int MAX_POOL_SIZE = 10;
33 
34     private static SomeArgs sPool;
35     private static int sPoolSize;
36     private static Object sPoolLock = new Object();
37 
38     private SomeArgs mNext;
39 
40     private boolean mInPool;
41 
42     static final int WAIT_NONE = 0;
43     static final int WAIT_WAITING = 1;
44     static final int WAIT_FINISHED = 2;
45     int mWaitState = WAIT_NONE;
46 
47     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
48     public Object arg1;
49     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
50     public Object arg2;
51     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
52     public Object arg3;
53     public Object arg4;
54     public Object arg5;
55     public Object arg6;
56     public Object arg7;
57     public int argi1;
58     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
59     public int argi2;
60     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
61     public int argi3;
62     public int argi4;
63     public int argi5;
64     public int argi6;
65     public long argl1;
66     public long argl2;
67 
SomeArgs()68     private SomeArgs() {
69         /* do nothing - reduce visibility */
70     }
71 
72     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
obtain()73     public static SomeArgs obtain() {
74         synchronized (sPoolLock) {
75             if (sPoolSize > 0) {
76                 SomeArgs args = sPool;
77                 sPool = sPool.mNext;
78                 args.mNext = null;
79                 args.mInPool = false;
80                 sPoolSize--;
81                 return args;
82             } else {
83                 return new SomeArgs();
84             }
85         }
86     }
87 
complete()88     public void complete() {
89         synchronized (this) {
90             if (mWaitState != WAIT_WAITING) {
91                 throw new IllegalStateException("Not waiting");
92             }
93             mWaitState = WAIT_FINISHED;
94             notifyAll();
95         }
96     }
97 
98     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
recycle()99     public void recycle() {
100         if (mInPool) {
101             throw new IllegalStateException("Already recycled.");
102         }
103         if (mWaitState != WAIT_NONE) {
104             return;
105         }
106         synchronized (sPoolLock) {
107             clear();
108             if (sPoolSize < MAX_POOL_SIZE) {
109                 mNext = sPool;
110                 mInPool = true;
111                 sPool = this;
112                 sPoolSize++;
113             }
114         }
115     }
116 
clear()117     private void clear() {
118         arg1 = null;
119         arg2 = null;
120         arg3 = null;
121         arg4 = null;
122         arg5 = null;
123         arg6 = null;
124         arg7 = null;
125         argi1 = 0;
126         argi2 = 0;
127         argi3 = 0;
128         argi4 = 0;
129         argi5 = 0;
130         argi6 = 0;
131         argl1 = 0;
132         argl2 = 0;
133     }
134 }
135