Skip to content

Instantly share code, notes, and snippets.

@r4zzz4k
Last active August 13, 2016 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r4zzz4k/ba15a41a9b2d5997b2be to your computer and use it in GitHub Desktop.
Save r4zzz4k/ba15a41a9b2d5997b2be to your computer and use it in GitHub Desktop.
Utility that automatically generates intent-filter to process files by extensions
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
<application ...>
<activity ...>
<!--suppress AndroidElementNotAllowed -->
<intent-filter-ext exts="mp3,ogg" max-nesting="16" tools:ignore="MissingPrefix"/>
</activity>
</application>
</manifest>
apply plugin: 'com.android.application'
apply from: 'exts.gradle'
android {
...
applicationVariants.all { variant ->
...
processManifest(variant)
}
}
def newIntentFilter(activity) {
def filter = new Node(activity, 'intent-filter')
new Node(filter, 'action', ['android:name': 'android.intent.action.VIEW'])
new Node(filter, 'category', ['android:name': 'android.intent.category.DEFAULT'])
new Node(filter, 'category', ['android:name': 'android.intent.category.BROWSABLE'])
return filter
}
def processManifestIntentFilterEx(xml) {
xml.application.each { application ->
application.'activity'.each { activity ->
activity.'intent-filter-ext'.each { filter ->
def exts = filter.@exts
def maxNesting = filter.@'max-nesting'.toInteger()
logger.info("Processing intent-filter-ext: exts=$exts, max-nesting=$maxNesting")
activity.remove(filter)
for(nesting in 1..maxNesting) {
for(curExt in exts.tokenize(',')) {
def pathPattern = '.*\\\\.' * nesting + curExt
def intFilter = newIntentFilter(activity)
new Node(intFilter, 'data', [
'android:host': '*',
'android:pathPattern': pathPattern
])
def intFilterSchemes = newIntentFilter(activity)
new Node(intFilterSchemes, 'data', [
'android:host': '*',
'android:scheme': 'file',
'android:pathPattern': pathPattern
])
new Node(intFilterSchemes, 'data', [
'android:host': '*',
'android:scheme': 'http',
'android:pathPattern': pathPattern
])
new Node(intFilterSchemes, 'data', [
'android:host': '*',
'android:scheme': 'https',
'android:pathPattern': pathPattern
])
new Node(intFilterSchemes, 'data', [
'android:host': '*',
'android:scheme': 'content',
'android:pathPattern': pathPattern
])
def intFilterSchemesMimes = newIntentFilter(activity)
new Node(intFilterSchemesMimes, 'data', [
'android:host': '*',
'android:scheme': 'file',
'android:mimeType': '*/*',
'android:pathPattern': pathPattern
])
new Node(intFilterSchemesMimes, 'data', [
'android:host': '*',
'android:scheme': 'http',
'android:mimeType': '*/*',
'android:pathPattern': pathPattern
])
new Node(intFilterSchemesMimes, 'data', [
'android:host': '*',
'android:scheme': 'https',
'android:mimeType': '*/*',
'android:pathPattern': pathPattern
])
new Node(intFilterSchemesMimes, 'data', [
'android:host': '*',
'android:scheme': 'content',
'android:mimeType': '*/*',
'android:pathPattern': pathPattern
])
}
}
}
}
}
return xml
}
ext.processManifest = { variant ->
variant.outputs.each { output ->
output.processManifest.doLast {
def writer = new StringWriter()
def manifestFile = output.processManifest.manifestOutputFile
def textIn = manifestFile.getText('UTF-8')
def xmlIn = new XmlParser().parseText(textIn)
def xmlOut = processManifestIntentFilterEx(xmlIn)
new XmlNodePrinter(new PrintWriter(writer)).print(xmlOut)
def textOut = writer.toString()
manifestFile.write(textOut, 'UTF-8')
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment