| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import android.content.ComponentName; |
| 20 |
import android.content.Context; |
| 21 |
import android.content.Intent; |
| 22 |
import android.content.ServiceConnection; |
| 23 |
import android.os.Build; |
| 24 |
import android.os.IBinder; |
| 25 |
import android.os.RemoteException; |
| 26 |
import android.os.SystemClock; |
| 27 |
|
| 28 |
import androidx.test.InstrumentationRegistry; |
| 29 |
|
| 30 |
import java.io.Closeable; |
| 31 |
import java.io.IOException; |
| 32 |
import java.util.concurrent.TimeoutException; |
| 33 |
|
| 34 |
final class RemoteIntArray implements ServiceConnection, Closeable { |
| 35 |
private static final long BIND_REMOTE_SERVICE_TIMEOUT = |
| 36 |
Build.IS_ENG ? 120000 : 10000; |
| 37 |
|
| 38 |
private final Object mLock = new Object(); |
| 39 |
|
| 40 |
private final Intent mIntent = new Intent(); |
| 41 |
|
| 42 |
private android.util.IRemoteMemoryIntArray mRemoteInstance; |
| 43 |
|
| 44 |
public RemoteIntArray(int size) throws IOException, TimeoutException { |
| 45 |
mIntent.setComponent(new ComponentName(InstrumentationRegistry.getContext(), |
| 46 |
RemoteMemoryIntArrayService.class)); |
| 47 |
synchronized (mLock) { |
| 48 |
if (mRemoteInstance == null) { |
| 49 |
bindLocked(); |
| 50 |
} |
| 51 |
try { |
| 52 |
mRemoteInstance.create(size); |
| 53 |
} catch (RemoteException e) { |
| 54 |
throw new IOException(e); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public MemoryIntArray peekInstance() { |
| 60 |
try { |
| 61 |
return mRemoteInstance.peekInstance(); |
| 62 |
} catch (RemoteException e) { |
| 63 |
throw new RuntimeException(e); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public boolean isWritable() { |
| 68 |
try { |
| 69 |
return mRemoteInstance.isWritable(); |
| 70 |
} catch (RemoteException e) { |
| 71 |
throw new RuntimeException(e); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public int get(int index) throws IOException { |
| 76 |
try { |
| 77 |
return mRemoteInstance.get(index); |
| 78 |
} catch (RemoteException e) { |
| 79 |
throw new IOException(e); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public void set(int index, int value) throws IOException { |
| 84 |
try { |
| 85 |
mRemoteInstance.set(index, value); |
| 86 |
} catch (RemoteException e) { |
| 87 |
throw new IOException(e); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public int size() throws IOException { |
| 92 |
try { |
| 93 |
return mRemoteInstance.size(); |
| 94 |
} catch (RemoteException e) { |
| 95 |
throw new IOException(e); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public void close() { |
| 100 |
try { |
| 101 |
mRemoteInstance.close(); |
| 102 |
} catch (RemoteException e) { |
| 103 |
throw new RuntimeException(e); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public boolean isClosed() { |
| 108 |
try { |
| 109 |
return mRemoteInstance.isClosed(); |
| 110 |
} catch (RemoteException e) { |
| 111 |
throw new RuntimeException(e); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
private void bindLocked() throws TimeoutException { |
| 116 |
if (mRemoteInstance != null) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
InstrumentationRegistry.getContext().bindService(mIntent, this, Context.BIND_AUTO_CREATE); |
| 121 |
|
| 122 |
final long startMillis = SystemClock.uptimeMillis(); |
| 123 |
while (true) { |
| 124 |
if (mRemoteInstance != null) { |
| 125 |
break; |
| 126 |
} |
| 127 |
final long elapsedMillis = SystemClock.uptimeMillis() - startMillis; |
| 128 |
final long remainingMillis = BIND_REMOTE_SERVICE_TIMEOUT - elapsedMillis; |
| 129 |
if (remainingMillis <= 0) { |
| 130 |
throw new TimeoutException("Cannot get spooler!"); |
| 131 |
} |
| 132 |
try { |
| 133 |
mLock.wait(remainingMillis); |
| 134 |
} catch (InterruptedException ie) { |
| 135 |
|
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
mLock.notifyAll(); |
| 140 |
} |
| 141 |
|
| 142 |
public void destroy() { |
| 143 |
synchronized (mLock) { |
| 144 |
if (mRemoteInstance == null) { |
| 145 |
return; |
| 146 |
} |
| 147 |
mRemoteInstance = null; |
| 148 |
InstrumentationRegistry.getContext().unbindService(this); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
public void accessLastElementInRemoteProcess(MemoryIntArray array) { |
| 153 |
try { |
| 154 |
mRemoteInstance.accessLastElementInRemoteProcess(array); |
| 155 |
} catch (RemoteException e) { |
| 156 |
throw new RuntimeException(e); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public void onServiceConnected(ComponentName name, IBinder service) { |
| 162 |
synchronized (mLock) { |
| 163 |
mRemoteInstance = android.util.IRemoteMemoryIntArray.Stub.asInterface(service); |
| 164 |
mLock.notifyAll(); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public void onServiceDisconnected(ComponentName name) { |
| 170 |
synchronized (mLock) { |
| 171 |
mRemoteInstance = null; |
| 172 |
} |
| 173 |
} |
| 174 |
} |