1 /* 2 * Copyright (C) 2016 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 android.util; 18 19 import android.app.Service; 20 import android.content.Intent; 21 import android.os.IBinder; 22 23 import java.io.IOException; 24 25 /** 26 * Service to interact with a {@link MemoryIntArray} in another process. 27 */ 28 public class RemoteMemoryIntArrayService extends Service { 29 private final Object mLock = new Object(); 30 31 private MemoryIntArray mArray; 32 33 @Override onBind(Intent intent)34 public IBinder onBind(Intent intent) { 35 return new android.util.IRemoteMemoryIntArray.Stub() { 36 37 @Override 38 public void create(int size) { 39 synchronized (mLock) { 40 try { 41 mArray = new MemoryIntArray(size); 42 } catch (IOException e) { 43 throw new IllegalStateException(e); 44 } 45 } 46 } 47 48 @Override 49 public MemoryIntArray peekInstance() { 50 synchronized (mLock) { 51 return mArray; 52 } 53 } 54 55 @Override 56 public boolean isWritable() { 57 synchronized (mLock) { 58 return mArray.isWritable(); 59 } 60 } 61 62 @Override 63 public int get(int index) { 64 synchronized (mLock) { 65 try { 66 return mArray.get(index); 67 } catch (IOException e) { 68 throw new IllegalStateException(e); 69 } 70 } 71 } 72 73 @Override 74 public void set(int index, int value) { 75 synchronized (mLock) { 76 try { 77 mArray.set(index, value); 78 } catch (IOException e) { 79 throw new IllegalStateException(e); 80 } 81 } 82 } 83 84 @Override 85 public int size() { 86 synchronized (mLock) { 87 return mArray.size(); 88 } 89 } 90 91 @Override 92 public void close() { 93 synchronized (mLock) { 94 try { 95 mArray.close(); 96 } catch (IOException e) { 97 throw new IllegalStateException(e); 98 } 99 } 100 } 101 102 @Override 103 public boolean isClosed() { 104 synchronized (mLock) { 105 return mArray.isClosed(); 106 } 107 } 108 109 @Override 110 public void accessLastElementInRemoteProcess(MemoryIntArray array) { 111 try { 112 array.get(array.size() - 1); 113 } catch (IOException e) { 114 throw new IllegalStateException(e); 115 } 116 } 117 }; 118 } 119 } 120