-
Notifications
You must be signed in to change notification settings - Fork 7
/
ContextExtension.kt
107 lines (99 loc) · 3.58 KB
/
ContextExtension.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.rakuten.tech.mobile.sdkutils
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.content.res.Configuration
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.rakuten.tech.mobile.sdkutils.logger.Logger
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
/**
* android.content.Context Extensions.
*/
object ContextExtension {
private const val PLAY_SERVICES_RESOLUTION_REQUEST = 9000
private val logger = Logger(ContextExtension::class.java.simpleName)
/**
* Get the resource id from drawable folder.
*
* @param resourceName the resource name.
* @return the drawable resource id.
*/
fun Context.getDrawableResourceId(resourceName: String?): Int =
this.resources.getIdentifier(resourceName, "drawable", this.packageName)
/**
* Get the resource id from raw folder.
*
* @param resourceName the resource name.
* @return the raw resource id.
*/
fun Context.getRawResourceId(resourceName: String?): Int =
this.resources.getIdentifier(resourceName, "raw", this.packageName)
/**
* Get the desired file path exists in assets folder.
*
* @param fileName the file name.
* @return the asset path of the file.
*/
@SuppressWarnings("ReturnCount", "NestedBlockDepth")
fun Context.getAssetsFilePath(fileName: String?): String? {
if (fileName != null) {
val cacheFile = File(this.cacheDir, fileName)
try {
this.assets.open(fileName).use { input ->
FileOutputStream(cacheFile).use { output ->
input.copyTo(output)
}
}
} catch (ex: IOException) {
logger.error(ex, "Failed to fetch media file from assets folder")
return null
}
return cacheFile.path
}
return null
}
/**
* Get the device mode (Dark or Light).
*
* @return true if dark mode otherwise false for light mode.
*/
@SuppressWarnings("ReturnCount")
fun Context.isDarkMode(): Boolean {
when (this.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
Configuration.UI_MODE_NIGHT_NO -> return false
Configuration.UI_MODE_NIGHT_YES -> return true
}
return false
}
/**
* Checks whether a particular permission has been granted.
*
* @return true if permission is granted, false otherwise.
*/
fun Context.hasPermission(permission: String): Boolean {
return this.packageManager.checkPermission(permission, this.packageName) ==
PackageManager.PERMISSION_GRANTED
}
/**
* Check the device to make sure it has the Google Play Services APK.
*
* If it doesn't, display a dialog that allows users to download the APK from the Google Play
* Store or enable it in the device's system settings.
*
* @return true if play services are available, false otherwise.
*/
fun Activity.checkPlayServices(): Boolean {
val googleApi = GoogleApiAvailability.getInstance()
val result = googleApi.isGooglePlayServicesAvailable(this)
if (result != ConnectionResult.SUCCESS) {
if (googleApi.isUserResolvableError(result)) {
googleApi.getErrorDialog(this, result, PLAY_SERVICES_RESOLUTION_REQUEST)?.show()
}
return false
}
return true
}
}