-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathManifestAppInfoProvider.kt
88 lines (76 loc) · 3.36 KB
/
ManifestAppInfoProvider.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package net.dean.jraw.android
import android.content.Context
import android.content.pm.PackageManager
import android.os.Bundle
import net.dean.jraw.http.UserAgent
/**
* This class produces [AppInfo] instances from application manifest metadata.
*
* Include this in your `AndroidManifest.xml`:
*
* ```xml
* <application android:name="foo" (...)>
* (...)
*
* <meta-data
* android:name="net.dean.jraw.android.REDDIT_USERNAME"
* android:value="(your reddit username)" />
*
* <meta-data
* android:name="net.dean.jraw.android.CLIENT_ID"
* android:value="(your client ID)" />
*
* <meta-data
* android:name="net.dean.jraw.android.REDIRECT_URL"
* android:value="(your redirect URL)" />
* </application>
* ```
*
* If the application's ID is "com.example.app", then the resulting AppInfo's UserAgent will be
* something like this: `android:com.example.app:${version} (by /u/${username})` where `${version}`
* is `BuildConfig.VERSION_NAME`.
*
* If this doesn't suit your needs, use `net.dean.jraw.android.USER_AGENT_OVERRIDE` instead of
* `net.dean.jraw.android.REDDIT_USERNAME` to specify a custom user agent.
*/
class ManifestAppInfoProvider(private val context: Context) : AppInfoProvider {
override fun provide(): AppInfo {
// Get the app's <meta-data> tags from the manifest
val metadata = context
.packageManager
.getApplicationInfo(context.packageName, PackageManager.GET_META_DATA)
.metaData
// Pull version from here instead of BuildConfig. The end result will be the same for the
// user.
val version = context.packageManager.getPackageInfo(context.packageName, 0).versionName
val ua = userAgent(metadata, version)
return AppInfo(
clientId = requireString(metadata, KEY_CLIENT_ID, "client ID"),
redirectUrl = requireString(metadata, KEY_REDIRECT_URL, "redirect URL"),
userAgent = ua
)
}
private fun userAgent(b: Bundle, version: String): UserAgent {
val username = b.getString(KEY_REDDIT_USERNAME)
if (username != null)
return UserAgent(PLATFORM, context.packageName, version, username)
val override = b.getString(KEY_USER_AGENT_OVERRIDE)
if (override != null)
return UserAgent(override)
throw IllegalStateException("Could produce a UserAgent from the manifest. Make sure to " +
"include a <meta-data> tag with either the $KEY_REDDIT_USERNAME or " +
"$KEY_USER_AGENT_OVERRIDE key.")
}
private fun requireString(bundle: Bundle, key: String, what: String): String {
return bundle.getString(key) ?:
throw IllegalStateException("Could not produce a $what from the manifest. Make " +
"sure to include a <meta-data> tag with the $key key.")
}
companion object {
internal const val KEY_USER_AGENT_OVERRIDE = "net.dean.jraw.android.USER_AGENT_OVERRIDE"
internal const val KEY_REDDIT_USERNAME = "net.dean.jraw.android.REDDIT_USERNAME"
internal const val KEY_CLIENT_ID = "net.dean.jraw.android.CLIENT_ID"
internal const val KEY_REDIRECT_URL = "net.dean.jraw.android.REDIRECT_URL"
private const val PLATFORM = "android"
}
}