Skip to content

Commit

Permalink
fix(core.runtime): 修复Receiver的actions为空的NPE
Browse files Browse the repository at this point in the history
fix #755
  • Loading branch information
shifujun committed Jan 7, 2022
1 parent 669f8b5 commit 68d54d9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
import android.content.res.Configuration;
import android.os.Build;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -41,7 +39,7 @@ public class ShadowApplication extends ShadowContext {

private Application mHostApplication;

private Map<String, List<String>> mBroadcasts;
private Map<String, String[]> mBroadcasts;

private ShadowAppComponentFactory mAppComponentFactory;

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

isCallOnCreate = true;

for (Map.Entry<String, List<String>> entry : mBroadcasts.entrySet()) {
for (Map.Entry<String, String[]> entry : mBroadcasts.entrySet()) {
try {
Class<?> clazz = mPluginClassLoader.loadClass(entry.getKey());
String receiverClassname = entry.getKey();
Class<?> clazz = mPluginClassLoader.loadClass(receiverClassname);
BroadcastReceiver receiver = ((BroadcastReceiver) clazz.newInstance());
mAppComponentFactory.instantiateReceiver(mPluginClassLoader, entry.getKey(), null);
mAppComponentFactory.instantiateReceiver(mPluginClassLoader, receiverClassname, null);

IntentFilter intentFilter = new IntentFilter();
for (String action:entry.getValue()
) {
intentFilter.addAction(action);
String[] receiverActions = entry.getValue();
if (receiverActions != null) {
for (String action : receiverActions) {
intentFilter.addAction(action);
}
}
registerReceiver(receiver, intentFilter);
} catch (Exception e) {
Expand Down Expand Up @@ -151,10 +152,10 @@ public void setHostApplicationContextAsBase(Context hostAppContext) {
}

public void setBroadcasts(PluginManifest.ReceiverInfo[] receiverInfos) {
Map<String, List<String>> classNameToActions = new HashMap<>();
Map<String, String[]> classNameToActions = new HashMap<>();
if (receiverInfos != null) {
for (PluginManifest.ReceiverInfo receiverInfo : receiverInfos) {
classNameToActions.put(receiverInfo.className, Arrays.asList(receiverInfo.actions));
classNameToActions.put(receiverInfo.className, receiverInfo.actions);
}
}
mBroadcasts = classNameToActions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<action android:name="com.tencent.test.normal.action" />
</intent-filter>
</receiver>
<receiver android:name=".lib.usecases.receiver.ReceiverWithoutAction" />

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Tencent is pleased to support the open source community by making Tencent Shadow available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.tencent.shadow.test.plugin.general_cases.lib.usecases.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
* 测试插件中包含无Action的Receiver能否正常启动
*/
public class ReceiverWithoutAction extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
}
}

0 comments on commit 68d54d9

Please sign in to comment.