/
Proxy.java
76 lines (61 loc) · 1.84 KB
/
Proxy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.baronzhang.ipc.proxy;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import com.baronzhang.ipc.Book;
import com.baronzhang.ipc.server.BookManager;
import com.baronzhang.ipc.server.Stub;
import java.util.List;
/**
* @author baronzhang (baron[dot]zhanglei[at]gmail[dot]com)
* 05/01/2018
*/
public class Proxy implements BookManager {
private static final String DESCRIPTOR = "com.baronzhang.ipc.server.BookManager";
private IBinder remote;
public Proxy(IBinder remote) {
this.remote = remote;
}
public String getInterfaceDescriptor() {
return DESCRIPTOR;
}
@Override
public List<Book> getBooks() throws RemoteException {
Parcel data = Parcel.obtain();
Parcel replay = Parcel.obtain();
List<Book> result;
try {
data.writeInterfaceToken(DESCRIPTOR);
remote.transact(Stub.TRANSAVTION_getBooks, data, replay, 0);
replay.readException();
result = replay.createTypedArrayList(Book.CREATOR);
} finally {
replay.recycle();
data.recycle();
}
return result;
}
@Override
public void addBook(Book book) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel replay = Parcel.obtain();
try {
data.writeInterfaceToken(DESCRIPTOR);
if (book != null) {
data.writeInt(1);
book.writeToParcel(data, 0);
} else {
data.writeInt(0);
}
remote.transact(Stub.TRANSAVTION_addBook, data, replay, 0);
replay.readException();
} finally {
replay.recycle();
data.recycle();
}
}
@Override
public IBinder asBinder() {
return remote;
}
}