Skip to content

Commit 68d54d9

Browse files
committed
fix(core.runtime): 修复Receiver的actions为空的NPE
fix #755
1 parent 669f8b5 commit 68d54d9

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

projects/sdk/core/runtime/src/main/java/com/tencent/shadow/core/runtime/ShadowApplication.java

+12-11
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
import android.content.res.Configuration;
3030
import android.os.Build;
3131

32-
import java.util.Arrays;
3332
import java.util.HashMap;
34-
import java.util.List;
3533
import java.util.Map;
3634

3735
/**
@@ -41,7 +39,7 @@ public class ShadowApplication extends ShadowContext {
4139

4240
private Application mHostApplication;
4341

44-
private Map<String, List<String>> mBroadcasts;
42+
private Map<String, String[]> mBroadcasts;
4543

4644
private ShadowAppComponentFactory mAppComponentFactory;
4745

@@ -68,16 +66,19 @@ public void onCreate() {
6866

6967
isCallOnCreate = true;
7068

71-
for (Map.Entry<String, List<String>> entry : mBroadcasts.entrySet()) {
69+
for (Map.Entry<String, String[]> entry : mBroadcasts.entrySet()) {
7270
try {
73-
Class<?> clazz = mPluginClassLoader.loadClass(entry.getKey());
71+
String receiverClassname = entry.getKey();
72+
Class<?> clazz = mPluginClassLoader.loadClass(receiverClassname);
7473
BroadcastReceiver receiver = ((BroadcastReceiver) clazz.newInstance());
75-
mAppComponentFactory.instantiateReceiver(mPluginClassLoader, entry.getKey(), null);
74+
mAppComponentFactory.instantiateReceiver(mPluginClassLoader, receiverClassname, null);
7675

7776
IntentFilter intentFilter = new IntentFilter();
78-
for (String action:entry.getValue()
79-
) {
80-
intentFilter.addAction(action);
77+
String[] receiverActions = entry.getValue();
78+
if (receiverActions != null) {
79+
for (String action : receiverActions) {
80+
intentFilter.addAction(action);
81+
}
8182
}
8283
registerReceiver(receiver, intentFilter);
8384
} catch (Exception e) {
@@ -151,10 +152,10 @@ public void setHostApplicationContextAsBase(Context hostAppContext) {
151152
}
152153

153154
public void setBroadcasts(PluginManifest.ReceiverInfo[] receiverInfos) {
154-
Map<String, List<String>> classNameToActions = new HashMap<>();
155+
Map<String, String[]> classNameToActions = new HashMap<>();
155156
if (receiverInfos != null) {
156157
for (PluginManifest.ReceiverInfo receiverInfo : receiverInfos) {
157-
classNameToActions.put(receiverInfo.className, Arrays.asList(receiverInfo.actions));
158+
classNameToActions.put(receiverInfo.className, receiverInfo.actions);
158159
}
159160
}
160161
mBroadcasts = classNameToActions;

projects/test/plugin/general-cases/test-plugin-general-cases/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<action android:name="com.tencent.test.normal.action" />
6868
</intent-filter>
6969
</receiver>
70+
<receiver android:name=".lib.usecases.receiver.ReceiverWithoutAction" />
7071

7172
<activity android:name=".lib.usecases.provider.TestFileProviderActivity" />
7273

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Tencent Shadow available.
3+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
4+
*
5+
* Licensed under the BSD 3-Clause License (the "License"); you may not use
6+
* this file except in compliance with the License. You may obtain a copy of
7+
* the License at
8+
*
9+
* https://opensource.org/licenses/BSD-3-Clause
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package com.tencent.shadow.test.plugin.general_cases.lib.usecases.receiver;
20+
21+
import android.content.BroadcastReceiver;
22+
import android.content.Context;
23+
import android.content.Intent;
24+
25+
/**
26+
* 测试插件中包含无Action的Receiver能否正常启动
27+
*/
28+
public class ReceiverWithoutAction extends BroadcastReceiver {
29+
30+
@Override
31+
public void onReceive(Context context, Intent intent) {
32+
}
33+
}

0 commit comments

Comments
 (0)