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
17package android.util;
18
19import android.app.Service;
20import android.content.Intent;
21import android.os.IBinder;
22
23import java.io.IOException;
24
25/**
26 * Service to interact with a {@link MemoryIntArray} in another process.
27 */
28public class RemoteMemoryIntArrayService extends Service {
29    private final Object mLock = new Object();
30
31    private MemoryIntArray mArray;
32
33    @Override
34    public IBinder onBind(Intent intent) {
35        return new android.util.IRemoteMemoryIntArray.Stub() {
36
37            @Override
38            public void create(int size, boolean clientWritable) {
39                synchronized (mLock) {
40                    try {
41                        mArray = new MemoryIntArray(size, clientWritable);
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                    try {
88                        return mArray.size();
89                    } catch (IOException e) {
90                        throw new IllegalStateException(e);
91                    }
92                }
93            }
94
95            @Override
96            public void close() {
97                synchronized (mLock) {
98                    try {
99                        mArray.close();
100                    } catch (IOException e) {
101                        throw new IllegalStateException(e);
102                    }
103                }
104            }
105
106            @Override
107            public boolean isClosed() {
108                synchronized (mLock) {
109                    return mArray.isClosed();
110                }
111            }
112        };
113    }
114}
115