Skip to content

Commit

Permalink
feat(core): 支持resolveActivity使用显式Intent
Browse files Browse the repository at this point in the history
隐式Intent从原理上也可以支持,但工作量较大。由于现有API不支持查询Intent Filter,所以需要在编译期单独处理Manifest自行保存带到插件信息中供Loader使用。

#464
  • Loading branch information
shifujun committed Jan 13, 2021
1 parent 807ecd5 commit 2b49470
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.tencent.shadow.core.loader.managers

import android.content.ComponentName
import android.content.Intent
import android.content.pm.*
import com.tencent.shadow.core.runtime.PluginPackageManager

Expand Down Expand Up @@ -78,4 +79,19 @@ internal class PluginPackageManagerImpl(private val hostPackageManager: PackageM
&& it.applicationInfo.uid == uid
}.flatMap { it.providers.asIterable() }
}

override fun resolveActivity(intent: Intent, flags: Int): ResolveInfo {
val hostResolveInfo = hostPackageManager.resolveActivity(intent, flags)
return if (hostResolveInfo?.activityInfo == null) {
ResolveInfo().apply {
activityInfo = allPluginPackageInfo()
.flatMap { it.activities.asIterable() }
.find {
it.name == intent.component?.className
}
}
} else {
hostResolveInfo
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

import android.annotation.TargetApi;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.VersionedPackage;
import android.os.Build;

Expand Down Expand Up @@ -67,4 +69,8 @@ public static List<ProviderInfo> queryContentProviders(ClassLoader classLoaderOf
return getPluginPackageManager(classLoaderOfInvokeCode).queryContentProviders(processName, uid, flags);
}

public static ResolveInfo resolveActivity(ClassLoader classLoaderOfInvokeCode, Intent intent, int flags) {
return getPluginPackageManager(classLoaderOfInvokeCode).resolveActivity(intent, flags);
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.tencent.shadow.core.runtime;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;

import java.util.List;

Expand All @@ -18,4 +20,6 @@ public interface PluginPackageManager {
ProviderInfo resolveContentProvider(String name, int flags);

List<ProviderInfo> queryContentProviders(String processName, int uid, int flags);

ResolveInfo resolveActivity(Intent intent, int flags);
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class PackageManagerTransform : SpecificTransform() {
"getPackageInfo",
"resolveContentProvider",
"queryContentProviders",
"resolveActivity",
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public void testQueryContentProvidersName() {
matchTextWithViewTag("queryContentProviders/size", "2");
matchTextWithViewTag("queryContentProviders/name", "[android.support.v4.content.FileProvider, com.tencent.shadow.test.plugin.general_cases.lib.usecases.provider.TestProvider]");
}

@Test
public void testResolveActivityByExplicitIntent() {
matchTextWithViewTag("resolveActivity/explicit", "com.tencent.shadow.test.plugin.general_cases.lib.usecases.packagemanager.TestPackageManagerActivity");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ public void testQueryContentProvidersName() {
matchTextWithViewTag("queryContentProviders/size", "2");
matchTextWithViewTag("queryContentProviders/name", "[android.support.v4.content.FileProvider, com.tencent.shadow.test.plugin.general_cases.lib.usecases.provider.TestProvider]");
}

@Test
public void testResolveActivityByExplicitIntent() {
matchTextWithViewTag("resolveActivity/explicit", "com.tencent.shadow.test.plugin.general_cases.lib.usecases.packagemanager.TestPackageManagerActivity");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@

<activity android:name=".usecases.view.TestViewConstructorCache" />

<activity android:name=".usecases.packagemanager.TestPackageManagerActivity" />
<activity android:name=".usecases.packagemanager.TestPackageManagerActivity">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="com.tencent.test.implicit.intent" />
</intent-filter>
</activity>

<service android:name=".usecases.service.TestService" />

<receiver android:name=".usecases.receiver.MyReceiver">
<intent-filter>
<action android:name="com.tencent.test.normal.action"/>
<action android:name="com.tencent.test.normal.action" />
</intent-filter>
</receiver>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.ViewGroup;

Expand All @@ -48,6 +50,7 @@ protected void onCreate(Bundle savedInstanceState) {
getActivityInfo(viewGroup);
getPackageInfo(viewGroup);
queryContentProviders(viewGroup);
resolveActivityByExplicitIntent(viewGroup);
}

private void getApplicationInfo(ViewGroup viewGroup) {
Expand Down Expand Up @@ -181,4 +184,25 @@ private void queryContentProviders(ViewGroup viewGroup) {
)
);
}

private void resolveActivityByExplicitIntent(ViewGroup viewGroup) {
PackageManager packageManager = getPackageManager();
Intent intent = new Intent(this, this.getClass());
ResolveInfo resolveInfo = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);

String name;
if (resolveInfo != null && resolveInfo.activityInfo != null) {
name = resolveInfo.activityInfo.name;
} else {
name = "";
}
viewGroup.addView(
UiUtil.makeItem(
this,
"resolveActivity/explicit",
"resolveActivity/explicit",
name
)
);
}
}

0 comments on commit 2b49470

Please sign in to comment.