Skip to content

Commit adcd0e8

Browse files
author
athol.yi
committed
全部方式支持进程重启保持能力,防止hostService被系统kill后数据丢失
高版本系统需要在启动Activity中添加调用fix方法 升级版本3.3.2
1 parent 07c0808 commit adcd0e8

File tree

15 files changed

+218
-143
lines changed

15 files changed

+218
-143
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ plugins {
1717
}
1818

1919
ext {
20-
version = "3.3.1"
20+
version = "3.3.2"
2121
groupId = 'com.codyer.ElegantBus'
2222
minSdkVersion = 21
2323
targetSdkVersion = 32

ipc/src/aidl/java/cody/bus/ElegantBusService.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* ************************************************************
33
* 文件:ElegantBusService.java 模块:ElegantBus.ipc 项目:ElegantBus
4-
* 当前修改时间:2023年06月01日 17:08:51
5-
* 上次修改时间:2023年06月01日 16:46:07
4+
* 当前修改时间:2023年06月06日 11:07:31
5+
* 上次修改时间:2023年06月06日 10:33:34
66
* 作者:Cody.yi https://github.com/codyer
77
*
88
* 描述:ElegantBus.ipc
@@ -17,6 +17,8 @@
1717
import android.os.Binder;
1818
import android.os.IBinder;
1919

20+
import cody.bus.db.EventDataBase;
21+
2022

2123
/**
2224
* 跨进程事件总线支持服务 aidl 实现
@@ -25,6 +27,12 @@ public class ElegantBusService extends Service {
2527

2628
private final Binder mBinder = new ProcessManager();
2729

30+
@Override
31+
public void onCreate() {
32+
super.onCreate();
33+
EventDataBase.init(this, ElegantUtil.getHostPackageName(this));
34+
}
35+
2836
@Override
2937
public IBinder onBind(Intent intent) {
3038
return mBinder;

ipc/src/aidl/java/cody/bus/MultiProcessImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* ************************************************************
33
* 文件:MultiProcessImpl.java 模块:ElegantBus.ipc 项目:ElegantBus
4-
* 当前修改时间:2023年06月05日 20:59:58
5-
* 上次修改时间:2023年06月05日 20:43:19
4+
* 当前修改时间:2023年06月06日 11:07:31
5+
* 上次修改时间:2023年06月06日 10:33:35
66
* 作者:Cody.yi https://github.com/codyer
77
*
88
* 描述:ElegantBus.ipc
@@ -19,6 +19,8 @@
1919
import android.os.IBinder;
2020
import android.os.RemoteException;
2121

22+
import cody.bus.db.EventDataBase;
23+
2224
/**
2325
* 支持进程间事件总线的扩展,每个进程有一个实例 aidl 实现
2426
*/
Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* ************************************************************
33
* 文件:ProcessManager.java 模块:ElegantBus.ipc 项目:ElegantBus
4-
* 当前修改时间:2023年06月01日 17:08:51
5-
* 上次修改时间:2023年06月01日 10:29:02
4+
* 当前修改时间:2023年06月06日 11:07:31
5+
* 上次修改时间:2023年06月06日 10:40:09
66
* 作者:Cody.yi https://github.com/codyer
77
*
88
* 描述:ElegantBus.ipc
@@ -15,14 +15,15 @@
1515
import android.os.RemoteCallbackList;
1616
import android.os.RemoteException;
1717

18-
import java.util.Map;
19-
import java.util.concurrent.ConcurrentHashMap;
18+
import java.util.List;
19+
20+
import cody.bus.db.EventBean;
21+
import cody.bus.db.EventDataBase;
2022

2123
/**
2224
* 多进程共享一个实例
2325
*/
2426
public class ProcessManager extends IProcessManager.Stub {
25-
private final Map<String, EventWrapper> mEventCache = new ConcurrentHashMap<>();
2627
private final RemoteCallbackList<IProcessCallback> mRemoteCallbackList = new RemoteCallbackList<>();
2728

2829
public static MultiProcess ready() {
@@ -32,6 +33,10 @@ public static MultiProcess ready() {
3233
return BusFactory.getDelegate();
3334
}
3435

36+
public ProcessManager() {
37+
ElegantLog.d("ProcessManager is initialized.");
38+
}
39+
3540
@Override
3641
public void register(IProcessCallback callback) throws RemoteException {
3742
mRemoteCallbackList.register(callback);
@@ -46,15 +51,19 @@ public void unregister(IProcessCallback callback) {
4651
}
4752

4853
@Override
49-
public void resetSticky(final EventWrapper eventWrapper) throws RemoteException {
50-
removeEventFromCache(eventWrapper);
51-
callbackToOtherProcess(eventWrapper, MultiProcess.MSG_ON_RESET_STICKY);
54+
public void resetSticky(final EventWrapper eventWrapper) {
55+
BusFactory.ready().getSingleExecutorService().execute(() -> {
56+
removeEventFromCache(eventWrapper);
57+
callbackToOtherProcess(eventWrapper, MultiProcess.MSG_ON_RESET_STICKY);
58+
});
5259
}
5360

5461
@Override
55-
public void postToProcessManager(final EventWrapper eventWrapper) throws RemoteException {
56-
putEventToCache(eventWrapper);
57-
callbackToOtherProcess(eventWrapper, MultiProcess.MSG_ON_POST);
62+
public void postToProcessManager(final EventWrapper eventWrapper) {
63+
BusFactory.ready().getSingleExecutorService().execute(() -> {
64+
putEventToCache(eventWrapper);
65+
callbackToOtherProcess(eventWrapper, MultiProcess.MSG_ON_POST);
66+
});
5867
}
5968

6069
/**
@@ -64,7 +73,7 @@ public void postToProcessManager(final EventWrapper eventWrapper) throws RemoteE
6473
*/
6574
private void putEventToCache(final EventWrapper eventWrapper) {
6675
ElegantLog.d("Service receive event, add to cache, Event = " + eventWrapper.toString());
67-
mEventCache.put(eventWrapper.getKey(), eventWrapper);
76+
EventDataBase.getInstance().eventDao().insert(DataUtil.convert(eventWrapper));
6877
}
6978

7079
/**
@@ -74,27 +83,31 @@ private void putEventToCache(final EventWrapper eventWrapper) {
7483
*/
7584
private void removeEventFromCache(final EventWrapper eventWrapper) {
7685
ElegantLog.d("Service receive event, remove from cache, Event = " + eventWrapper.toString());
77-
mEventCache.remove(eventWrapper.getKey());
86+
EventDataBase.getInstance().eventDao().delete(DataUtil.convert(eventWrapper));
7887
}
79-
88+
8089
/**
8190
* 转发事件总线
8291
*
8392
* @param eventWrapper 发送值
84-
* @throws RemoteException 异常
8593
*/
86-
private void callbackToOtherProcess(EventWrapper eventWrapper, int what) throws RemoteException {
94+
private void callbackToOtherProcess(EventWrapper eventWrapper, int what) {
8795
int count = mRemoteCallbackList.beginBroadcast();
88-
for (int i = 0; i < count; i++) {
89-
IProcessCallback callback = mRemoteCallbackList.getBroadcastItem(i);
90-
if (callback == null) continue;
91-
if (ElegantUtil.isSameProcess(callback.processName(), eventWrapper.processName)) {
92-
ElegantLog.d("This is in same process, already posted, Event = " + eventWrapper);
93-
} else {
94-
ElegantLog.d("call back " + what + " to other process : " + callback.processName() + ", Event = " +
95-
eventWrapper);
96-
callback.call(eventWrapper, what);
96+
try {
97+
for (int i = 0; i < count; i++) {
98+
IProcessCallback callback = mRemoteCallbackList.getBroadcastItem(i);
99+
if (callback == null) continue;
100+
if (ElegantUtil.isSameProcess(callback.processName(), eventWrapper.processName)) {
101+
ElegantLog.d("This is in same process, already posted, Event = " + eventWrapper);
102+
} else {
103+
ElegantLog.d("call back " + what + " to other process : " + callback.processName() + ", Event = " +
104+
eventWrapper);
105+
106+
callback.call(eventWrapper, what);
107+
}
97108
}
109+
} catch (RemoteException e) {
110+
e.printStackTrace();
98111
}
99112
mRemoteCallbackList.finishBroadcast();
100113
}
@@ -103,12 +116,18 @@ private void callbackToOtherProcess(EventWrapper eventWrapper, int what) throws
103116
* 转发 粘性事件到新的进程
104117
*
105118
* @param callback 进程回调
106-
* @throws RemoteException 异常
107119
*/
108-
private void postStickyValueToNewProcess(final IProcessCallback callback) throws RemoteException {
109-
ElegantLog.d("Post all sticky event to new process : " + callback.processName());
110-
for (EventWrapper item : mEventCache.values()) {
111-
callback.call(item, MultiProcess.MSG_ON_POST_STICKY);
112-
}
120+
private void postStickyValueToNewProcess(final IProcessCallback callback) {
121+
BusFactory.ready().getSingleExecutorService().execute(() -> {
122+
try {
123+
ElegantLog.d("Post all sticky event to new process : " + callback.processName());
124+
List<EventBean> caches = EventDataBase.getInstance().eventDao().getAllList();
125+
for (EventBean item : caches) {
126+
callback.call(DataUtil.convert(item), MultiProcess.MSG_ON_POST_STICKY);
127+
}
128+
} catch (RemoteException e) {
129+
e.printStackTrace();
130+
}
131+
});
113132
}
114133
}

ipc/src/provider/java/cody/bus/DataUtil.java renamed to ipc/src/main/java/cody/bus/DataUtil.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* ************************************************************
33
* 文件:DataUtil.java 模块:ElegantBus.ipc.main 项目:ElegantBus
4-
* 当前修改时间:2023年06月02日 11:27:48
5-
* 上次修改时间:2023年06月02日 11:26:43
4+
* 当前修改时间:2023年06月06日 11:07:31
5+
* 上次修改时间:2023年06月06日 10:33:34
66
* 作者:Cody.yi https://github.com/codyer
77
*
88
* 描述:ElegantBus.ipc.main
@@ -20,6 +20,10 @@
2020

2121
public class DataUtil {
2222

23+
public static EventWrapper convert(EventBean bean) {
24+
return new EventWrapper(bean.processName, bean.group, bean.event, bean.type, bean.json, bean.multiProcess);
25+
}
26+
2327
public static EventBean convert(EventWrapper wrapper) {
2428
EventBean bean = new EventBean();
2529
bean.key = wrapper.getKey();
@@ -33,18 +37,6 @@ public static EventBean convert(EventWrapper wrapper) {
3337
bean.time = System.currentTimeMillis();
3438
return bean;
3539
}
36-
/*
37-
public static ContentValues convert(EventWrapper wrapper) {
38-
ContentValues values = new ContentValues();
39-
values.put(BusColumnInfo.KEY, wrapper.getKey());
40-
values.put(BusColumnInfo.PROCESS_NAME, wrapper.processName);
41-
values.put(BusColumnInfo.GROUP, wrapper.group);
42-
values.put(BusColumnInfo.EVENT, wrapper.event);
43-
values.put(BusColumnInfo.TYPE, wrapper.type);
44-
values.put(BusColumnInfo.JSON, wrapper.json);
45-
values.put(BusColumnInfo.MULTI_PROCESS, wrapper.multiProcess);
46-
return values;
47-
}*/
4840

4941
public static EventBean convert(ContentValues values) {
5042
EventBean bean = new EventBean();
File renamed without changes.

ipc/src/provider/java/cody/bus/db/EventBean.java renamed to ipc/src/main/java/cody/bus/db/EventBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* ************************************************************
33
* 文件:EventBean.java 模块:ElegantBus.ipc.main 项目:ElegantBus
4-
* 当前修改时间:2023年06月02日 16:58:02
5-
* 上次修改时间:2023年06月02日 16:57:26
4+
* 当前修改时间:2023年06月06日 11:07:31
5+
* 上次修改时间:2023年06月02日 16:58:02
66
* 作者:Cody.yi https://github.com/codyer
77
*
88
* 描述:ElegantBus.ipc.main

ipc/src/provider/java/cody/bus/db/EventDao.java renamed to ipc/src/main/java/cody/bus/db/EventDao.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* ************************************************************
33
* 文件:EventDao.java 模块:ElegantBus.ipc.main 项目:ElegantBus
4-
* 当前修改时间:2023年06月02日 16:58:02
5-
* 上次修改时间:2023年06月02日 16:57:27
4+
* 当前修改时间:2023年06月06日 11:07:31
5+
* 上次修改时间:2023年06月06日 09:46:27
66
* 作者:Cody.yi https://github.com/codyer
77
*
88
* 描述:ElegantBus.ipc.main
@@ -21,13 +21,21 @@
2121
import androidx.room.Query;
2222
import androidx.room.Update;
2323

24+
import java.util.List;
25+
2426
@Dao
2527
public interface EventDao {
2628
@Query("SELECT * FROM EVENT_CACHE_TABLE where valid > 0 order by time ASC")
2729
Cursor getAllCursor();
2830

31+
@Query("SELECT * FROM EVENT_CACHE_TABLE where valid > 0 order by time ASC")
32+
List<EventBean> getAllList();
33+
34+
@Query("SELECT * FROM EVENT_CACHE_TABLE where `key`=:key")
35+
Cursor getByKeyCursor(String key);
36+
2937
@Query("SELECT * FROM EVENT_CACHE_TABLE where `key`=:key")
30-
Cursor getByKey(String key);
38+
EventBean getByKey(String key);
3139

3240
@Insert(onConflict = OnConflictStrategy.REPLACE)
3341
void insert(EventBean event);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* ************************************************************
3+
* 文件:EventDataBase.java 模块:ElegantBus.ipc.main 项目:ElegantBus
4+
* 当前修改时间:2023年06月06日 11:07:31
5+
* 上次修改时间:2023年06月06日 10:28:01
6+
* 作者:Cody.yi https://github.com/codyer
7+
*
8+
* 描述:ElegantBus.ipc.main
9+
* Copyright (c) 2023
10+
* ************************************************************
11+
*/
12+
13+
package cody.bus.db;
14+
15+
import android.content.Context;
16+
17+
import androidx.room.Database;
18+
import androidx.room.Room;
19+
import androidx.room.RoomDatabase;
20+
21+
import cody.bus.ElegantLog;
22+
23+
@Database(entities = {EventBean.class}, version = 1, exportSchema = false)
24+
public abstract class EventDataBase extends RoomDatabase {
25+
public abstract EventDao eventDao();
26+
27+
private static volatile EventDataBase sInstance;
28+
29+
public static EventDataBase getInstance() {
30+
if (sInstance == null) {
31+
ElegantLog.e("ProcessManager is not initialized.");
32+
}
33+
return sInstance;
34+
}
35+
36+
public static void init(Context context, String pkgName) {
37+
if (sInstance == null) {
38+
synchronized (EventDataBase.class) {
39+
if (sInstance == null) {
40+
sInstance = create(context, pkgName);
41+
}
42+
}
43+
}
44+
}
45+
46+
private static EventDataBase create(final Context context, String dbName) {
47+
return Room.databaseBuilder(context, EventDataBase.class, dbName + ".db").build();
48+
}
49+
}

0 commit comments

Comments
 (0)