Skip to content

Commit afd5ae3

Browse files
committed
Update sample project, add call adapter sample code.
1 parent fb1e1ad commit afd5ae3

File tree

5 files changed

+133
-2
lines changed

5 files changed

+133
-2
lines changed

sample/src/main/java/com/example/andlinker/BindingActivity.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,20 @@
77
import android.widget.Toast;
88

99
import com.codezjx.andlinker.AndLinker;
10+
import com.codezjx.andlinker.Call;
11+
import com.codezjx.andlinker.Callback;
12+
import com.codezjx.andlinker.adapter.OriginalCallAdapterFactory;
13+
import com.codezjx.andlinker.adapter.rxjava2.RxJava2CallAdapterFactory;
14+
import com.codezjx.andlinker.annotation.ClassName;
15+
import com.codezjx.andlinker.annotation.MethodName;
16+
17+
import java.util.List;
1018

1119
import butterknife.ButterKnife;
1220
import butterknife.OnClick;
21+
import io.reactivex.Observable;
22+
import io.reactivex.android.schedulers.AndroidSchedulers;
23+
import io.reactivex.schedulers.Schedulers;
1324

1425
public class BindingActivity extends AppCompatActivity {
1526

@@ -18,6 +29,7 @@ public class BindingActivity extends AppCompatActivity {
1829

1930
private AndLinker mLinker;
2031
private IRemoteService mRemoteService;
32+
private IRemoteTask mRemoteTask;
2133

2234
@Override
2335
protected void onCreate(Bundle savedInstanceState) {
@@ -28,14 +40,20 @@ protected void onCreate(Bundle savedInstanceState) {
2840
mLinker = new AndLinker.Builder(this)
2941
.packageName(REMOTE_SERVICE_PKG)
3042
.action(REMOTE_SERVICE_ACTION)
43+
// Specify the callback executor by yourself
44+
//.addCallAdapterFactory(OriginalCallAdapterFactory.create(callbackExecutor))
45+
.addCallAdapterFactory(OriginalCallAdapterFactory.create()) // Basic
46+
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // RxJava2
3147
.build();
3248
mLinker.bind();
3349
mLinker.registerObject(mRemoteCallback);
3450

3551
mRemoteService = mLinker.create(IRemoteService.class);
52+
mRemoteTask = mLinker.create(IRemoteTask.class);
3653
}
3754

38-
@OnClick({R.id.btn_pid, R.id.btn_basic_types, R.id.btn_callback, R.id.btn_directional, R.id.btn_oneway})
55+
@OnClick({R.id.btn_pid, R.id.btn_basic_types, R.id.btn_call_adapter, R.id.btn_rxjava2_call_adapter,
56+
R.id.btn_callback, R.id.btn_directional, R.id.btn_oneway})
3957
public void onClick(View view) {
4058
switch (view.getId()) {
4159
case R.id.btn_pid:
@@ -45,6 +63,32 @@ public void onClick(View view) {
4563
case R.id.btn_basic_types:
4664
mRemoteService.basicTypes(1, 2L, true, 3.0f, 4.0d, "str");
4765
break;
66+
case R.id.btn_call_adapter:
67+
Call<Integer> call = mRemoteTask.remoteCalculate(10, 20);
68+
69+
// Synchronous Request
70+
// int result = call.execute();
71+
// Log.d("BindingActivity", "remoteCalculate() result:" + result);
72+
73+
// Asynchronous Request
74+
call.enqueue(new Callback<Integer>() {
75+
@Override
76+
public void onResponse(Call<Integer> call, Integer response) {
77+
Toast.makeText(BindingActivity.this, "remoteCalculate() onResponse: " + response, Toast.LENGTH_SHORT).show();
78+
}
79+
80+
@Override
81+
public void onFailure(Call<Integer> call, Throwable t) {
82+
Toast.makeText(BindingActivity.this, "remoteCalculate() failure: " + t.getMessage(), Toast.LENGTH_SHORT).show();
83+
}
84+
});
85+
break;
86+
case R.id.btn_rxjava2_call_adapter:
87+
mRemoteTask.getDatas()
88+
.subscribeOn(Schedulers.io())
89+
.observeOn(AndroidSchedulers.mainThread())
90+
.subscribe(datas -> Toast.makeText(this, "getDatas() return: " + datas, Toast.LENGTH_LONG).show());
91+
break;
4892
case R.id.btn_callback:
4993
mRemoteService.registerCallback(mRemoteCallback);
5094
break;
@@ -78,4 +122,19 @@ public void onValueChange(int value) {
78122
Toast.makeText(BindingActivity.this, "Server callback value: " + value, Toast.LENGTH_SHORT).show();
79123
}
80124
};
125+
126+
127+
/**
128+
* Copy the original interface, wrap the return type of the method, keep the original @ClassName and @MethodName.
129+
*/
130+
@ClassName("com.example.andlinker.IRemoteTask")
131+
public interface IRemoteTask {
132+
133+
@MethodName("remoteCalculate")
134+
Call<Integer> remoteCalculate(int a, int b);
135+
136+
@MethodName("getDatas")
137+
Observable<List<ParcelableObj>> getDatas();
138+
139+
}
81140
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.andlinker;
2+
3+
import com.codezjx.andlinker.annotation.ClassName;
4+
import com.codezjx.andlinker.annotation.MethodName;
5+
6+
import java.util.List;
7+
8+
/**
9+
* Created by codezjx on 2018/3/14.<br/>
10+
*/
11+
@ClassName("com.example.andlinker.IRemoteTask")
12+
public interface IRemoteTask {
13+
14+
@MethodName("remoteCalculate")
15+
int remoteCalculate(int a, int b);
16+
17+
@MethodName("getDatas")
18+
List<ParcelableObj> getDatas();
19+
20+
}

sample/src/main/java/com/example/andlinker/ParcelableObj.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public void writeToParcel(Parcel dest, int flags) {
2929
public ParcelableObj() {
3030
}
3131

32+
public ParcelableObj(int type, float value, String msg) {
33+
mType = type;
34+
mValue = value;
35+
mMsg = msg;
36+
}
37+
3238
protected ParcelableObj(Parcel in) {
3339
readFromParcel(in);
3440
}

sample/src/main/java/com/example/andlinker/RemoteService.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
import com.codezjx.andlinker.AndLinkerBinder;
1313

14+
import java.util.ArrayList;
1415
import java.util.Arrays;
16+
import java.util.List;
1517

1618
public class RemoteService extends Service {
1719

@@ -25,6 +27,7 @@ public void onCreate() {
2527
Log.d(TAG, "Service onCreate()");
2628
mLinkerBinder = AndLinkerBinder.Factory.newBinder();
2729
mLinkerBinder.registerObject(mRemoteService);
30+
mLinkerBinder.registerObject(mRemoteTask);
2831
}
2932

3033
@Override
@@ -77,4 +80,33 @@ public void onewayMethod(String msg) {
7780
Log.d(TAG, "After oneway method server.");
7881
}
7982
};
83+
84+
private final IRemoteTask mRemoteTask = new IRemoteTask() {
85+
@Override
86+
public int remoteCalculate(int a, int b) {
87+
Log.d(TAG, "Call remoteCalculate(): " + a + ", " + b);
88+
// Simulate slow task
89+
try {
90+
Thread.sleep(3000);
91+
} catch (InterruptedException e) {
92+
e.printStackTrace();
93+
}
94+
return (a + b) * 100;
95+
}
96+
97+
@Override
98+
public List<ParcelableObj> getDatas() {
99+
Log.d(TAG, "Call getDatas().");
100+
// Simulate slow task
101+
try {
102+
Thread.sleep(3000);
103+
} catch (InterruptedException e) {
104+
e.printStackTrace();
105+
}
106+
ArrayList<ParcelableObj> datas = new ArrayList<>();
107+
datas.add(new ParcelableObj(1, 11.1f, "hello"));
108+
datas.add(new ParcelableObj(2, 22.2f, "world"));
109+
return datas;
110+
}
111+
};
80112
}

sample/src/main/res/layout/activity_binding.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,27 @@
1919
android:layout_height="wrap_content"
2020
android:text="Basic types"
2121
app:layout_constraintTop_toBottomOf="@id/btn_pid"/>
22+
23+
<Button
24+
android:id="@+id/btn_call_adapter"
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:text="Call adapter"
28+
app:layout_constraintTop_toBottomOf="@id/btn_basic_types"/>
29+
30+
<Button
31+
android:id="@+id/btn_rxjava2_call_adapter"
32+
android:layout_width="wrap_content"
33+
android:layout_height="wrap_content"
34+
android:text="RxJava2 call adapter"
35+
app:layout_constraintTop_toBottomOf="@id/btn_call_adapter"/>
2236

2337
<Button
2438
android:id="@+id/btn_callback"
2539
android:layout_width="wrap_content"
2640
android:layout_height="wrap_content"
2741
android:text="Callback"
28-
app:layout_constraintTop_toBottomOf="@id/btn_basic_types"/>
42+
app:layout_constraintTop_toBottomOf="@id/btn_rxjava2_call_adapter"/>
2943

3044
<Button
3145
android:id="@+id/btn_directional"

0 commit comments

Comments
 (0)