Skip to content

Commit 57aee16

Browse files
wintmainwosys
authored andcommitted
[foundation][refactor]AIDL sample adjustment
Move AIDL sample to single folder
1 parent 61f9515 commit 57aee16

File tree

11 files changed

+151
-66
lines changed

11 files changed

+151
-66
lines changed

app-catalog/samples/foundation/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
apply from: "$rootDir/gradle/sample-build.gradle"
22

3+
apply plugin: 'kotlin-parcelize'
4+
35
android {
46
sourceSets {
57
main {

app-catalog/samples/foundation/src/main/AndroidManifest.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,15 @@
354354
</receiver>
355355

356356
<service
357-
android:name=".PeopleRemoteService"
357+
android:name=".aidl.PeopleRemoteService"
358358
android:process=":remote"
359359
android:enabled="true"
360360
android:exported="true" />
361+
362+
<activity
363+
android:name=".aidl.AidlSampleActivity"
364+
android:exported="true"
365+
android:theme="@style/Theme.AppCompat.DayNight" />
361366
</application>
362367

363368
</manifest>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.wintmain.foundation.aidl;
2+
3+
import com.wintmain.foundation.aidl.Book;
4+
5+
parcelable Book;

app-catalog/samples/foundation/src/main/aidl/com/wintmain/foundation/INewPeopleListener.aidl renamed to app-catalog/samples/foundation/src/main/aidl/com/wintmain/foundation/aidl/INewPeopleListener.aidl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// INewPeopleListener.aidl
2-
package com.wintmain.foundation;
2+
package com.wintmain.foundation.aidl;
33

4-
import com.wintmain.foundation.People;
4+
import com.wintmain.foundation.aidl.People;
55

66
// AIDL中无法使用普通接口,所以创建此文件
77

app-catalog/samples/foundation/src/main/aidl/com/wintmain/foundation/IPeopleManager.aidl renamed to app-catalog/samples/foundation/src/main/aidl/com/wintmain/foundation/aidl/IPeopleManager.aidl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
// IPeopleManager.aidl
22
// AIDL接口中只支持方法,不支持声明静态常量
3-
package com.wintmain.foundation;
3+
package com.wintmain.foundation.aidl;
44

55
// 自定义的Parcelable对象和AIDL对象不管是否和当前的AIDL文件位于同一个包内,也必须要显式import进来
66

7-
import com.wintmain.foundation.People;
8-
import com.wintmain.foundation.INewPeopleListener;
7+
import com.wintmain.foundation.aidl.Book;
8+
import com.wintmain.foundation.aidl.People;
9+
import com.wintmain.foundation.aidl.INewPeopleListener;
910

1011
interface IPeopleManager {
1112
List<People> getPeopleList();
1213
// addPeople方法的参数中有in关键字,因为aidl中除了基本数据类型,其它类型的参数必须标上方向:in、out或者inout,
1314
// 我们需根据实现需要去指定参数类型,因为这在底层实现是有开销的。
1415
void addPeople(in People people);
1516

17+
void addPeopleAndBook(in People people, in Book book);
18+
1619
// 扩展新增代码:新增接口
1720
void registerListener(INewPeopleListener listener);
1821
void unregisterListener(INewPeopleListener listener);

app-catalog/samples/foundation/src/main/aidl/com/wintmain/foundation/People.aidl renamed to app-catalog/samples/foundation/src/main/aidl/com/wintmain/foundation/aidl/People.aidl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// People.aidl
2-
package com.wintmain.foundation;
2+
package com.wintmain.foundation.aidl;
33

44
// Declare any non-default types here with import statements
55

@@ -14,7 +14,7 @@ package com.wintmain.foundation;
1414
// Parcelable的作用是序列化对象,因为跨进程通信传输对象必须是以序列化和反序列化的方式进行。
1515
// Parcelable的实现有些繁琐,但性能效率很高。
1616

17-
import com.wintmain.foundation.People;
17+
import com.wintmain.foundation.aidl.People;
1818

19-
// People should be declared in a file called com/wintmain/foundation/People.aidl
19+
// People should be declared in a file called com/wintmain/foundation/aidl/People.aidl
2020
parcelable People;

app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/PlaceHolderActivity.java

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020
import android.content.ComponentName;
2121
import android.content.Context;
2222
import android.content.Intent;
23-
import android.content.ServiceConnection;
2423
import android.os.Bundle;
25-
import android.os.IBinder;
26-
import android.os.RemoteException;
2724
import android.provider.AlarmClock;
2825
import android.widget.TextView;
2926
import androidx.appcompat.app.AppCompatActivity;
@@ -39,19 +36,12 @@
3936
description = "一个为了测试API调用的入口",
4037
tags = "A-Self_demos")
4138
public class PlaceHolderActivity extends AppCompatActivity {
42-
private static final String TAG = PlaceHolderActivity.class.getSimpleName();
43-
IPeopleManager peopleManager;
4439

4540
@Override
4641
protected void onCreate(Bundle savedInstanceState) {
4742
super.onCreate(savedInstanceState);
4843
setContentView(R.layout.pft_activity_main);
4944

50-
// AIDL示例 S
51-
Intent intent = new Intent(this, PeopleRemoteService.class);
52-
bindService(intent, mConnection, BIND_AUTO_CREATE);
53-
// AIDL示例 E
54-
5545
// 初始化一些三方库
5646
initLibs(getApplication());
5747

@@ -87,20 +77,6 @@ public void onRightClick(TitleBarExt titleBar) {
8777
});
8878
}
8979

90-
@Override
91-
protected void onDestroy() {
92-
// 移除监听
93-
if (peopleManager != null && peopleManager.asBinder().isBinderAlive()) {
94-
try {
95-
peopleManager.unregisterListener(mNewPeopleListener);
96-
} catch (RemoteException e) {
97-
android.util.Log.d(TAG, "catch:" + e.getMessage());
98-
}
99-
}
100-
unbindService(mConnection);
101-
super.onDestroy();
102-
}
103-
10480
private void initLibs(Application application) {
10581
// 初始化 TitleBar 默认样式
10682
TitleBarExt.setDefaultStyle(
@@ -124,34 +100,4 @@ public TextView newRightView(Context context) {
124100
// 初始化 Toast
125101
ToastUtils.init(this.getApplication());
126102
}
127-
128-
129-
private final ServiceConnection mConnection = new ServiceConnection() {
130-
public void onServiceConnected(ComponentName className, IBinder service) {
131-
android.util.Log.d(TAG, "onServiceConnected...");
132-
peopleManager = IPeopleManager.Stub.asInterface(service);
133-
try {
134-
People people = new People(1000, "wintmain");
135-
// 注册监听
136-
peopleManager.registerListener(mNewPeopleListener);
137-
peopleManager.addPeople(people);
138-
} catch (RemoteException e) {
139-
android.util.Log.d(TAG, "catch:" + e.getMessage());
140-
}
141-
}
142-
public void onServiceDisconnected(ComponentName className) {
143-
android.util.Log.d(TAG, "onServiceDisconnected!!!");
144-
ToastUtils.show("onServiceDisconnected!!!");
145-
peopleManager = null;
146-
}
147-
};
148-
149-
150-
// 定义监听接口
151-
private final INewPeopleListener mNewPeopleListener = new INewPeopleListener.Stub() {
152-
@Override
153-
public void onNewPeople(People newPeople) {
154-
ToastUtils.show("onNewPeople: " + newPeople);
155-
}
156-
};
157103
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2023-2025 wintmain
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+
* https://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 com.wintmain.foundation.aidl
18+
19+
import android.content.ComponentName
20+
import android.content.Intent
21+
import android.content.ServiceConnection
22+
import android.os.Bundle
23+
import android.os.IBinder
24+
import android.os.RemoteException
25+
import android.util.Log
26+
import androidx.appcompat.app.AppCompatActivity
27+
import com.google.android.catalog.framework.annotations.Sample
28+
import lib.wintmain.toaster.toast.ToastUtils
29+
30+
@Sample(name = "AIDL-SampleActivity", description = "AIDL简单示例", tags =["A-Self_demos"])
31+
class AidlSampleActivity: AppCompatActivity() {
32+
companion object {
33+
private const val TAG = "AidlSampleActivity"
34+
}
35+
var peopleManager: IPeopleManager? = null
36+
37+
override fun onCreate(savedInstanceState: Bundle?) {
38+
super.onCreate(savedInstanceState)
39+
// 初始化 Toast
40+
ToastUtils.init(this.application)
41+
42+
// 页面打开的时候就开始绑定服务
43+
val intent = Intent(this, PeopleRemoteService::class.java)
44+
bindService(intent, mConnection, BIND_AUTO_CREATE)
45+
}
46+
47+
override fun onDestroy() {
48+
// 移除监听
49+
peopleManager?.let { manager ->
50+
if (manager.asBinder().isBinderAlive) {
51+
try {
52+
manager.unregisterListener(mNewPeopleListener)
53+
} catch (e: RemoteException) {
54+
Log.d(TAG, "catch:" + e.message)
55+
}
56+
}
57+
}
58+
unbindService(mConnection)
59+
super.onDestroy()
60+
}
61+
62+
private val mConnection: ServiceConnection = object : ServiceConnection {
63+
override fun onServiceConnected(className: ComponentName, service: IBinder) {
64+
Log.d(TAG, "onServiceConnected...")
65+
peopleManager = IPeopleManager.Stub.asInterface(service)
66+
peopleManager?.run {
67+
try {
68+
val people = People(1000, "wintmain")
69+
val book = Book(1, "Book")
70+
// 注册监听
71+
registerListener(mNewPeopleListener)
72+
addPeople(people)
73+
addPeopleAndBook(people, book)
74+
} catch (e: RemoteException) {
75+
Log.d(TAG, "catch:" + e.message)
76+
}
77+
}
78+
}
79+
80+
override fun onServiceDisconnected(className: ComponentName) {
81+
Log.d(TAG, "onServiceDisconnected!!!")
82+
ToastUtils.show("onServiceDisconnected!!!")
83+
peopleManager = null
84+
}
85+
}
86+
87+
// 定义监听接口
88+
private val mNewPeopleListener: INewPeopleListener = object : INewPeopleListener.Stub() {
89+
override fun onNewPeople(newPeople: People) {
90+
ToastUtils.show("onNewPeople: $newPeople")
91+
}
92+
}
93+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2023-2025 wintmain
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+
* https://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 com.wintmain.foundation.aidl
18+
19+
import android.os.Parcelable
20+
import kotlinx.android.parcel.Parcelize
21+
22+
@Parcelize
23+
data class Book(
24+
val pId: Int,
25+
val pName: String
26+
) : Parcelable

app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/People.java renamed to app-catalog/samples/foundation/src/main/java/com/wintmain/foundation/aidl/People.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.wintmain.foundation;
17+
package com.wintmain.foundation.aidl;
1818

1919
import android.os.Parcel;
2020
import android.os.Parcelable;

0 commit comments

Comments
 (0)