-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 从AndroidManifest中解析Receiver的action信息
ComponentManager子类不再需要手动配置静态广播。
- Loading branch information
Showing
11 changed files
with
216 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...sdk/core/loader/src/main/kotlin/com/tencent/shadow/core/loader/blocs/ParseManifestBloc.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.tencent.shadow.core.loader.blocs | ||
|
||
import android.content.Context | ||
import android.content.IntentFilter | ||
import android.content.res.Resources | ||
import com.tencent.shadow.core.common.InstalledApk | ||
import com.tencent.shadow.core.loader.infos.ManifestInfo | ||
import com.tencent.shadow.core.loader.infos.ManifestInfo.Receiver | ||
import com.tencent.shadow.core.loader.infos.ManifestInfo.ReceiverIntentInfo | ||
import org.xmlpull.v1.XmlPullParser | ||
|
||
/** | ||
* 解析插件的AndroidManifest.xml文件 | ||
* 由于系统开放接口不提供广播的action信息,此处采用手动解析的方式处理,减少插件化的适配工作 | ||
* 后续对于AndroidManifest.xml的处理可在此基础上扩展 | ||
* | ||
* @author xuedizi2009@163.com | ||
*/ | ||
object ParseManifestBloc { | ||
|
||
private const val ANDROID_RESOURCES = "http://schemas.android.com/apk/res/android" | ||
private const val TAG_RECEIVER = "receiver" | ||
private const val TAG_INTENT_FILTER = "intent-filter" | ||
private const val TAG_ACTION = "action" | ||
private const val ATTR_NAME = "name" | ||
|
||
@Throws(Exception::class) | ||
fun parse(context: Context, installedApk: InstalledApk): ManifestInfo = ManifestInfo().apply { | ||
val resources: Resources = newResource(context, installedApk) | ||
val parser = resources.assets.openXmlResourceParser("AndroidManifest.xml") | ||
val outerDepth = parser.depth | ||
var type: Int | ||
while (parser.next().also { type = it } != XmlPullParser.END_DOCUMENT | ||
&& (type != XmlPullParser.END_TAG || parser.depth > outerDepth) | ||
) { | ||
if (type == XmlPullParser.START_TAG) { | ||
parseBroadcastReceiver(parser, this) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 此处如果使用[LoadPluginBloc.loadPlugin]构建的resources,将包含WebView的resources,故需要重新创建 | ||
*/ | ||
private fun newResource(context: Context, installedApk: InstalledApk): Resources { | ||
val packageArchiveInfo = | ||
context.packageManager.getPackageArchiveInfo(installedApk.apkFilePath, 0)!! | ||
val packageManager = context.packageManager | ||
packageArchiveInfo.applicationInfo?.publicSourceDir = installedApk.apkFilePath | ||
packageArchiveInfo.applicationInfo?.sourceDir = installedApk.apkFilePath | ||
return packageManager.getResourcesForApplication(packageArchiveInfo.applicationInfo) | ||
} | ||
|
||
private fun parseBroadcastReceiver(parser: XmlPullParser, manifestInfo: ManifestInfo) { | ||
if (TAG_RECEIVER == parser.name) { | ||
val receiver = Receiver(parser.getAttributeValue(ANDROID_RESOURCES, ATTR_NAME)) | ||
val outerDepth = parser.depth | ||
var type: Int | ||
while (parser.next().also { type = it } != XmlPullParser.END_DOCUMENT | ||
&& (type != XmlPullParser.END_TAG | ||
|| parser.depth > outerDepth)) { | ||
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { | ||
continue | ||
} | ||
if (TAG_INTENT_FILTER == parser.name) { | ||
val receiverInfo = ReceiverIntentInfo() | ||
parserIntent(parser, receiverInfo) | ||
receiver.intents.add(receiverInfo) | ||
} | ||
} | ||
manifestInfo.receivers.add(receiver) | ||
} | ||
} | ||
|
||
private fun parserIntent(parser: XmlPullParser, intentFilter: IntentFilter) { | ||
val outerDepth = parser.depth | ||
var type: Int | ||
while (parser.next().also { type = it } != XmlPullParser.END_DOCUMENT | ||
&& (type != XmlPullParser.END_TAG || parser.depth > outerDepth) | ||
) { | ||
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { | ||
continue | ||
} | ||
if (TAG_ACTION == parser.name) { | ||
val value = parser.getAttributeValue(ANDROID_RESOURCES, ATTR_NAME) | ||
intentFilter.addAction(value) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...ects/sdk/core/loader/src/main/kotlin/com/tencent/shadow/core/loader/infos/ManifestInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.tencent.shadow.core.loader.infos | ||
|
||
import android.content.IntentFilter | ||
|
||
/** | ||
* 插件AndroidManifest.xml信息存储类 | ||
* | ||
* @author xuedizi2009@163.com | ||
*/ | ||
class ManifestInfo { | ||
val receivers = mutableListOf<Receiver>() | ||
|
||
class Receiver(val name: String) { | ||
var intents = mutableListOf<ReceiverIntentInfo>() | ||
|
||
fun actions(): MutableList<String> { | ||
val actions = mutableListOf<String>() | ||
this.intents.forEach { intentInfo -> | ||
intentInfo.actionsIterator().forEach { action -> | ||
actions.add(action) | ||
} | ||
} | ||
return actions | ||
} | ||
} | ||
|
||
class ReceiverIntentInfo : IntentFilter() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...dk/core/loader/src/main/kotlin/com/tencent/shadow/core/loader/infos/PluginReceiverInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.tencent.shadow.core.loader.infos | ||
|
||
import android.content.pm.ActivityInfo | ||
import android.os.Parcel | ||
import android.os.Parcelable | ||
import android.os.Parcelable.Creator | ||
|
||
/** | ||
* 插件广播数据 | ||
* @author xuedizi2009@163.com | ||
*/ | ||
class PluginReceiverInfo( | ||
className: String?, | ||
private val activityInfo: ActivityInfo?, | ||
val actions: List<String>? | ||
) : Parcelable, PluginComponentInfo(className) { | ||
constructor(parcel: Parcel) : this( | ||
parcel.readString(), | ||
parcel.readParcelable(ActivityInfo::class.java.classLoader), | ||
parcel.createStringArrayList() | ||
) | ||
|
||
override fun writeToParcel(parcel: Parcel, flags: Int) { | ||
parcel.writeString(className) | ||
parcel.writeParcelable(activityInfo, flags) | ||
parcel.writeStringList(actions) | ||
} | ||
|
||
override fun describeContents(): Int { | ||
return 0 | ||
} | ||
|
||
companion object CREATOR : Creator<PluginReceiverInfo> { | ||
override fun createFromParcel(parcel: Parcel): PluginReceiverInfo { | ||
return PluginReceiverInfo(parcel) | ||
} | ||
|
||
override fun newArray(size: Int): Array<PluginReceiverInfo?> { | ||
return arrayOfNulls(size) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.