Skip to content

Commit dc594cb

Browse files
CriosChanAwkwardPeak7
authored andcommitted
Docs: URL intent filter (#14330)
* Docs: URL intent filter * Docs: Changes requested by Luigi * fix typos and improve clarity --------- Co-authored-by: AwkwardPeak7 <48650614+AwkwardPeak7@users.noreply.github.com>
1 parent c098203 commit dc594cb

File tree

1 file changed

+91
-8
lines changed

1 file changed

+91
-8
lines changed

CONTRIBUTING.md

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -485,16 +485,48 @@ them may cause you more headache than necessary.
485485
486486
#### URL intent filter
487487
488-
Extensions can define URL intent filters by defining it inside a custom `AndroidManifest.xml` file.
489-
(Example TBD.)
488+
Extensions can define a URL pattern so that these URLs can be opened in Mihon.
489+
490+
To do this, you need two files:
491+
- `AndroidManifest.xml` which must be placed in the root directory of your extension (Example: `src/id/riztranslation/AndroidManifest.xml`)
492+
- `UrlActivity.kt` which should be placed next to your main file. (Example: `src/id/riztranslation/src/eu/kanade/tachiyomi/extension/id/riztranslation/UrlActivity.kt`)
493+
494+
`AndroidManifest.xml` example :
495+
```xml
496+
<?xml version="1.0" encoding="utf-8"?>
497+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
498+
499+
<application>
500+
<activity
501+
android:name=".id.riztranslation.UrlActivity"
502+
android:excludeFromRecents="true"
503+
android:exported="true"
504+
android:theme="@android:style/Theme.NoDisplay">
505+
<intent-filter>
506+
<action android:name="android.intent.action.VIEW" />
507+
508+
<category android:name="android.intent.category.DEFAULT" />
509+
<category android:name="android.intent.category.BROWSABLE" />
510+
511+
<data
512+
android:host="riztranslation.pages.dev"
513+
android:pathPattern="/..*"
514+
android:scheme="https" />
515+
<data
516+
android:host="riztranslation.rf.gd"
517+
android:pathPattern="/..*"
518+
android:scheme="https" />
519+
</intent-filter>
520+
</activity>
521+
</application>
522+
</manifest>
523+
```
490524
491-
To test if the URL intent filter is working as expected, you can try opening the website in a browser
492-
and navigating to the endpoint that was added as a filter or clicking a hyperlink. Alternatively,
493-
you can use the `adb` command below.
525+
The `AndroidManifest.xml` file will contain an `android:name` attribute that refers to the “path” of your `UrlActivity.kt` file. For example, if the extension is Riztranslation, the `android:name` will be `.id.riztranslation.UrlActivity`.
494526
495-
```console
496-
$ adb shell am start -d "<your-link>" -a android.intent.action.VIEW
497-
```
527+
Next, you have the `<data android:scheme=“https” android:host=“host” android:pathPattern=“/..*” />` element; you can have it multiple times, which allows you to specify the URL that can be opened in Mihon. You can read more about this [here](https://developer.android.com/guide/topics/manifest/data-element).
528+
529+
Now, as for `UrlActivity`, you can just use the example below.
498530
499531
> [!CAUTION]
500532
> The activity does not support any Kotlin Intrinsics specific methods or calls,
@@ -504,6 +536,57 @@ $ adb shell am start -d "<your-link>" -a android.intent.action.VIEW
504536
> You can use Kotlin Intrinsics in the extension source class, this limitation only
505537
> applies to the activity classes.
506538
539+
To explain how it works, it will trigger Mihon's `SEARCH` action, passing the URL as a query and specifying that it comes from your extension to narrow down the search. Avoid putting any logic in this file; instead, implement it in your extension's class.
540+
541+
```kotlin
542+
class UrlActivity : Activity() {
543+
override fun onCreate(savedInstanceState: Bundle?) {
544+
super.onCreate(savedInstanceState)
545+
val intentData = intent?.data?.toString()
546+
if (intentData != null) {
547+
val mainIntent = Intent().apply {
548+
action = "eu.kanade.tachiyomi.SEARCH"
549+
putExtra("query", intentData)
550+
putExtra("filter", packageName)
551+
}
552+
try {
553+
startActivity(mainIntent)
554+
} catch (e: ActivityNotFoundException) {
555+
Log.e("RiztranslationUrl", e.toString())
556+
}
557+
} else {
558+
Log.e("RiztranslationUrl", "could not parse uri from intent $intent")
559+
}
560+
561+
finish()
562+
exitProcess(0)
563+
}
564+
}
565+
```
566+
567+
Now all you need to do is adapt the search function (`fetchSearchManga`) in your extension so that, given a URL, it returns a single manga that matches that URL. For example:
568+
```kotlin
569+
if (query.startsWith("https://")) {
570+
val url = query.toHttpUrlOrNull()
571+
if (url != null && url.host == baseUrl.toHttpUrl().host) {
572+
val typeIndex = url.pathSegments.indexOfFirst { it == "detail" || it == "view" }
573+
if (typeIndex != -1 && typeIndex + 1 < url.pathSize) {
574+
val id = url.pathSegments[typeIndex + 1]
575+
return GET("$apiUrl/Book?select=id,judul,cover&type=not.ilike.*novel*&id=eq.$id", apiHeaders)
576+
}
577+
}
578+
}
579+
```
580+
581+
To test if the URL intent filter is working as expected, you can try opening the website in a browser
582+
and navigating to the endpoint that was added as a filter or clicking a hyperlink. Alternatively,
583+
you can use the `adb` command below.
584+
585+
```console
586+
$ adb shell am start -d "<your-link>" -a android.intent.action.VIEW
587+
```
588+
You can find a complete example of how URLs work in the [Riztranslation extension](https://github.com/keiyoushi/extensions-source/tree/main/src/id/riztranslation).
589+
507590
#### Update strategy
508591
509592
There is some cases where titles in a source will always only have the same chapter list

0 commit comments

Comments
 (0)