Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.tencent.shadow.core.loader

import android.content.Context
import android.content.pm.PackageInfo
import android.os.Handler
import android.os.Looper
import android.os.Parcel
Expand All @@ -32,7 +33,6 @@ import com.tencent.shadow.core.loader.delegates.ShadowContentProviderDelegate
import com.tencent.shadow.core.loader.delegates.ShadowDelegate
import com.tencent.shadow.core.loader.exceptions.LoadPluginException
import com.tencent.shadow.core.loader.infos.PluginParts
import com.tencent.shadow.core.loader.managers.CommonPluginPackageManager
import com.tencent.shadow.core.loader.managers.ComponentManager
import com.tencent.shadow.core.loader.managers.PluginContentProviderManager
import com.tencent.shadow.core.loader.managers.PluginServiceManager
Expand Down Expand Up @@ -75,7 +75,10 @@ abstract class ShadowPluginLoader(hostAppContext: Context) : DelegateProvider, D

abstract val mExceptionReporter: Reporter

private val mCommonPluginPackageManager = CommonPluginPackageManager()
/**
* @GuardedBy("mLock")
*/
private val mPluginPackageInfoSet: MutableSet<PackageInfo> = hashSetOf()

private lateinit var mPluginServiceManager: PluginServiceManager

Expand Down Expand Up @@ -164,8 +167,8 @@ abstract class ShadowPluginLoader(hostAppContext: Context) : DelegateProvider, D

return LoadPluginBloc.loadPlugin(
mExecutorService,
mAbi,
mCommonPluginPackageManager,
mPluginPackageInfoSet,
::allPluginPackageInfo,
mComponentManager,
mLock,
mPluginPartsMap,
Expand All @@ -175,6 +178,12 @@ abstract class ShadowPluginLoader(hostAppContext: Context) : DelegateProvider, D
mShadowRemoteViewCreatorProvider)
}

private fun allPluginPackageInfo(): Array<PackageInfo> {
mLock.withLock {
return mPluginPackageInfoSet.toTypedArray()
}
}

override fun getHostActivityDelegate(aClass: Class<out HostActivityDelegator>): HostActivityDelegate {
return ShadowActivityDelegate(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
package com.tencent.shadow.core.loader.blocs

import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.res.Resources
import com.tencent.shadow.core.loader.classloaders.PluginClassLoader
import com.tencent.shadow.core.loader.exceptions.CreateApplicationException
import com.tencent.shadow.core.loader.infos.PluginInfo
import com.tencent.shadow.core.loader.managers.ComponentManager
import com.tencent.shadow.core.loader.managers.PluginPackageManager
import com.tencent.shadow.core.runtime.ShadowApplication
import com.tencent.shadow.core.runtime.remoteview.ShadowRemoteViewCreatorProvider

Expand All @@ -36,30 +37,30 @@ object CreateApplicationBloc {
@Throws(CreateApplicationException::class)
fun createShadowApplication(
pluginClassLoader: PluginClassLoader,
appClassName: String?,
pluginPackageManager: PluginPackageManager,
pluginInfo: PluginInfo,
resources: Resources,
hostAppContext: Context,
componentManager: ComponentManager,
remoteViewCreatorProvider: ShadowRemoteViewCreatorProvider?
remoteViewCreatorProvider: ShadowRemoteViewCreatorProvider?,
applicationInfo: ApplicationInfo
): ShadowApplication {
try {
val appClassName = pluginInfo.applicationClassName
val shadowApplication : ShadowApplication;
shadowApplication = if (appClassName != null) {
val appClass = pluginClassLoader.loadClass(appClassName)
ShadowApplication::class.java.cast(appClass.newInstance())
} else {
object : ShadowApplication(){}
}
val partKey = pluginPackageManager.pluginInfo.partKey
val partKey = pluginInfo.partKey
shadowApplication.setPluginResources(resources)
shadowApplication.setPluginClassLoader(pluginClassLoader)
shadowApplication.setPluginComponentLauncher(componentManager)
shadowApplication.setHostApplicationContextAsBase(hostAppContext)
shadowApplication.setBroadcasts(componentManager.getBroadcastsByPartKey(partKey))
shadowApplication.setLibrarySearchPath(pluginClassLoader.getLibrarySearchPath())
shadowApplication.setDexPath(pluginClassLoader.getDexPath())
shadowApplication.setBusinessName(pluginPackageManager.pluginInfo.businessName)
shadowApplication.applicationInfo = applicationInfo
shadowApplication.setBusinessName(pluginInfo.businessName)
shadowApplication.setPluginPartKey(partKey)
shadowApplication.remoteViewCreatorProvider = remoteViewCreatorProvider
return shadowApplication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
package com.tencent.shadow.core.loader.blocs

import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import com.tencent.shadow.core.common.InstalledApk
import com.tencent.shadow.core.load_parameters.LoadParameters
import com.tencent.shadow.core.loader.exceptions.LoadPluginException
import com.tencent.shadow.core.loader.infos.PluginParts
import com.tencent.shadow.core.loader.managers.CommonPluginPackageManager
import com.tencent.shadow.core.loader.managers.ComponentManager
import com.tencent.shadow.core.loader.managers.PluginPackageManager
import com.tencent.shadow.core.runtime.PluginPartInfo
Expand All @@ -41,8 +41,8 @@ object LoadPluginBloc {
@Throws(LoadPluginException::class)
fun loadPlugin(
executorService: ExecutorService,
abi: String,
commonPluginPackageManager: CommonPluginPackageManager,
pluginPackageInfoSet: MutableSet<PackageInfo>,
allPluginPackageInfo: () -> (Array<PackageInfo>),
componentManager: ComponentManager,
lock: ReentrantLock,
pluginPartsMap: MutableMap<String, PluginParts>,
Expand All @@ -63,6 +63,7 @@ object LoadPluginBloc {
val getPackageInfo = executorService.submit(Callable {
val archiveFilePath = installedApk.apkFilePath
val packageManager = hostAppContext.packageManager

val packageArchiveInfo = packageManager.getPackageArchiveInfo(
archiveFilePath,
PackageManager.GET_ACTIVITIES
Expand All @@ -72,13 +73,21 @@ object LoadPluginBloc {
or PackageManager.GET_SIGNATURES
)
?: throw NullPointerException("getPackageArchiveInfo return null.archiveFilePath==$archiveFilePath")
packageArchiveInfo.applicationInfo.nativeLibraryDir = installedApk.libraryPath

lock.withLock { pluginPackageInfoSet.add(packageArchiveInfo) }
packageArchiveInfo
})

val buildPluginInfo = executorService.submit(Callable {
val packageInfo = getPackageInfo.get()
ParsePluginApkBloc.parse(packageInfo, loadParameters, hostAppContext)
})

val buildPackageManager = executorService.submit(Callable {
val packageInfo = getPackageInfo.get()
val pluginInfo = ParsePluginApkBloc.parse(packageInfo, loadParameters, hostAppContext)
PluginPackageManager(commonPluginPackageManager, pluginInfo)
val hostPackageManager = hostAppContext.packageManager
PluginPackageManager(hostPackageManager, packageInfo, allPluginPackageInfo)
})

val buildResources = executorService.submit(Callable {
Expand All @@ -88,18 +97,18 @@ object LoadPluginBloc {

val buildApplication = executorService.submit(Callable {
val pluginClassLoader = buildClassLoader.get()
val pluginPackageManager = buildPackageManager.get()
val resources = buildResources.get()
val pluginInfo = pluginPackageManager.pluginInfo
val pluginInfo = buildPluginInfo.get()
val packageInfo = getPackageInfo.get()

CreateApplicationBloc.createShadowApplication(
pluginClassLoader,
pluginInfo.applicationClassName,
pluginPackageManager,
pluginInfo,
resources,
hostAppContext,
componentManager,
remoteViewCreatorProvider
remoteViewCreatorProvider,
packageInfo.applicationInfo
)
})

Expand All @@ -110,7 +119,7 @@ object LoadPluginBloc {
val pluginPackageManager = buildPackageManager.get()
val pluginClassLoader = buildClassLoader.get()
val resources = buildResources.get()
val pluginInfo = pluginPackageManager.pluginInfo
val pluginInfo = buildPluginInfo.get()
val shadowApplication = buildApplication.get()
lock.withLock {
componentManager.addPluginApkInfo(pluginInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ object ParsePluginApkBloc {
, partKey
, packageArchiveInfo.applicationInfo.packageName
, packageArchiveInfo.applicationInfo.className
, packageArchiveInfo.applicationInfo.metaData
, packageArchiveInfo.versionCode
, packageArchiveInfo.versionName
, packageArchiveInfo.signatures
)
packageArchiveInfo.activities?.forEach {
pluginInfo.putActivityInfo(PluginActivityInfo(it.name, it.themeResource, it))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ import java.io.File
*
*/
class PluginClassLoader(
private val dexPath: String,
dexPath: String,
optimizedDirectory: File?,
private val librarySearchPath: String?,
librarySearchPath: String?,
parent: ClassLoader,
private val specialClassLoader: ClassLoader?, hostWhiteList: Array<String>?
) : BaseDexClassLoader(dexPath, optimizedDirectory, librarySearchPath, parent) {
Expand Down Expand Up @@ -94,9 +94,6 @@ class PluginClassLoader(
}
}

fun getLibrarySearchPath() = librarySearchPath


private fun String.startWith(array: Array<String>): Boolean {
for (str in array) {
if (startsWith(str)) {
Expand All @@ -105,6 +102,5 @@ class PluginClassLoader(
}
return false
}

fun getDexPath() = dexPath
}

Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ class ShadowActivityDelegate(private val mDI: DI) : HostActivityDelegate, Shadow
pluginActivity.setPluginComponentLauncher(mComponentManager)
pluginActivity.setPluginApplication(mPluginApplication)
pluginActivity.setShadowApplication(mPluginApplication)
pluginActivity.setLibrarySearchPath(mPluginClassLoader.getLibrarySearchPath())
pluginActivity.setDexPath(mPluginClassLoader.getDexPath())
pluginActivity.applicationInfo = mPluginApplication.applicationInfo
pluginActivity.setBusinessName(mBusinessName)
pluginActivity.setPluginPartKey(mPartKey)
pluginActivity.remoteViewCreatorProvider = mRemoteViewCreatorProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,11 @@

package com.tencent.shadow.core.loader.infos

import android.content.pm.Signature
import android.os.Bundle

class PluginInfo(
val businessName: String?,
val partKey: String,
val packageName: String,
val applicationClassName: String?,
val metaData: Bundle?,
val versionCode: Int,
val versionName: String,
val signatures: Array<Signature>
val applicationClassName: String?
) {
private val _mActivities: MutableSet<PluginActivityInfo> = HashSet()
private val _mServices: MutableSet<PluginServiceInfo> = HashSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.tencent.shadow.core.loader.managers

import android.annotation.TargetApi
import android.content.ComponentName
import android.content.Intent
import android.content.IntentFilter
Expand All @@ -26,39 +27,59 @@ import android.content.res.Resources
import android.content.res.XmlResourceParser
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.UserHandle
import com.tencent.shadow.core.loader.ImplementLater
import com.tencent.shadow.core.loader.infos.PluginInfo

class PluginPackageManager(val commonPluginPackageManager: CommonPluginPackageManager,
val pluginInfo: PluginInfo) : PackageManager() {
override fun getApplicationInfo(packageName: String?, flags: Int): ApplicationInfo {
val applicationInfo = ApplicationInfo()
applicationInfo.metaData = pluginInfo.metaData
applicationInfo.className = pluginInfo.applicationClassName
return applicationInfo
}

override fun getPackageInfo(packageName: String?, flags: Int): PackageInfo? {
if (pluginInfo.packageName == packageName) {
val info = PackageInfo()
info.versionCode = pluginInfo.versionCode
info.versionName = pluginInfo.versionName
info.signatures = pluginInfo.signatures
return info;
}
return null;
}

class PluginPackageManager(private val hostPackageManager: PackageManager,
private val packageInfo: PackageInfo,
private val allPluginPackageInfo: () -> (Array<PackageInfo>))
: PackageManager() {
override fun getApplicationInfo(packageName: String?, flags: Int): ApplicationInfo =
if (packageInfo.applicationInfo.packageName == packageName) {
packageInfo.applicationInfo
} else {
hostPackageManager.getApplicationInfo(packageName, flags)
}

override fun getPackageInfo(packageName: String?, flags: Int): PackageInfo? =
if (packageInfo.applicationInfo.packageName == packageName) {
packageInfo
} else {
hostPackageManager.getPackageInfo(packageName, flags)
}

@TargetApi(Build.VERSION_CODES.O)
override fun getPackageInfo(versionedPackage: VersionedPackage?, flags: Int): PackageInfo? =
if (packageInfo.applicationInfo.packageName == versionedPackage?.packageName) {
packageInfo
} else {
hostPackageManager.getPackageInfo(versionedPackage, flags)
}

override fun getActivityInfo(component: ComponentName, flags: Int): ActivityInfo {
val find = pluginInfo.mActivities.find {
it.className == component.className
if (component.packageName == packageInfo.applicationInfo.packageName) {
val pluginActivityInfo = allPluginPackageInfo()
.flatMap { it.activities.asIterable() }.find {
it.name == component.className
}
if (pluginActivityInfo != null) {
return pluginActivityInfo
}
}
if (find == null) {
return commonPluginPackageManager.getActivityInfo(component, flags)
} else {
return find.activityInfo
return hostPackageManager.getActivityInfo(component, flags)
}

override fun resolveContentProvider(name: String?, flags: Int): ProviderInfo? {
val pluginProviderInfo = allPluginPackageInfo()
.flatMap { it.providers.asIterable() }.find {
it.authority == name
}
if (pluginProviderInfo != null) {
return pluginProviderInfo
}

return hostPackageManager.resolveContentProvider(name, flags)
}

override fun canonicalToCurrentPackageNames(names: Array<out String>?): Array<String> {
Expand Down Expand Up @@ -125,13 +146,6 @@ class PluginPackageManager(val commonPluginPackageManager: CommonPluginPackageMa
ImplementLater()
}

override fun resolveContentProvider(name: String?, flags: Int): ProviderInfo? {
val find = pluginInfo.mProviders.find {
it.authority == name
}
return find?.providerInfo
}

override fun getApplicationEnabledSetting(packageName: String?): Int {
ImplementLater()
}
Expand Down Expand Up @@ -240,10 +254,6 @@ class PluginPackageManager(val commonPluginPackageManager: CommonPluginPackageMa
ImplementLater()
}

override fun getPackageInfo(versionedPackage: VersionedPackage?, flags: Int): PackageInfo {
ImplementLater()
}

override fun getPackagesHoldingPermissions(permissions: Array<out String>?, flags: Int): MutableList<PackageInfo> {
ImplementLater()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ class PluginServiceManager(private val mPluginLoader: ShadowPluginLoader, privat
service.setPluginClassLoader(tmpShadowDelegate.getPluginClassLoader())
service.setShadowApplication(tmpShadowDelegate.getPluginApplication())
service.setPluginComponentLauncher(tmpShadowDelegate.getComponentManager())
service.setLibrarySearchPath(tmpShadowDelegate.getPluginClassLoader().getLibrarySearchPath())
service.setDexPath(tmpShadowDelegate.getPluginClassLoader().getDexPath())
service.applicationInfo = tmpShadowDelegate.getPluginApplication().applicationInfo
service.setBusinessName(businessName)
service.setPluginPartKey(partKey)

Expand Down
Loading