This repository was archived by the owner on Nov 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 560
Expand file tree
/
Copy pathContext.kt
More file actions
115 lines (109 loc) · 4.21 KB
/
Context.kt
File metadata and controls
115 lines (109 loc) · 4.21 KB
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
108
109
110
111
112
113
114
115
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.core.content
import android.app.Service
import android.app.job.JobInfo
import android.app.job.JobScheduler
import android.content.ComponentName
import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import androidx.annotation.AttrRes
import androidx.annotation.RequiresApi
import androidx.annotation.StyleRes
/**
* Return the handle to a system-level service by class.
*
* The return type of this function intentionally uses a
* [platform type](https://kotlinlang.org/docs/reference/java-interop.html#null-safety-and-platform-types)
* to allow callers to decide whether they require a service be present or can tolerate its absence.
*
* @see Context.getSystemService(Class)
*/
@RequiresApi(23)
@Suppress("HasPlatformType") // Intentionally propagating platform type with unknown nullability.
inline fun <reified T> Context.systemService() = getSystemService(T::class.java)
/**
* Executes [block] on a [TypedArray] receiver. The [TypedArray] holds the attribute
* values in [set] that are listed in [attrs]. In addition, if the given [AttributeSet]
* specifies a style class (through the `style` attribute), that style will be applied
* on top of the base attributes it defines.
*
* @param set The base set of attribute values.
* @param attrs The desired attributes to be retrieved. These attribute IDs must be
* sorted in ascending order.
* @param defStyleAttr An attribute in the current theme that contains a reference to
* a style resource that supplies defaults values for the [TypedArray].
* Can be 0 to not look for defaults.
* @param defStyleRes A resource identifier of a style resource that supplies default values
* for the [TypedArray], used only if [defStyleAttr] is 0 or can not be found
* in the theme. Can be 0 to not look for defaults.
*
* @see Context.obtainStyledAttributes
* @see android.content.res.Resources.Theme.obtainStyledAttributes
*/
inline fun Context.withStyledAttributes(
set: AttributeSet? = null,
attrs: IntArray,
@AttrRes defStyleAttr: Int = 0,
@StyleRes defStyleRes: Int = 0,
block: TypedArray.() -> Unit
) {
val typedArray = obtainStyledAttributes(set, attrs, defStyleAttr, defStyleRes)
try {
typedArray.block()
} finally {
typedArray.recycle()
}
}
/**
* Executes [block] on a [TypedArray] receiver. The [TypedArray] holds the the values
* defined by the style resource [resourceId] which are listed in [attrs].
*
* @param attrs The desired attributes. These attribute IDs must be sorted in ascending order.
*
* @see Context.obtainStyledAttributes
* @see android.content.res.Resources.Theme.obtainStyledAttributes
*/
inline fun Context.withStyledAttributes(
@StyleRes resourceId: Int,
attrs: IntArray,
block: TypedArray.() -> Unit
) {
val typedArray = obtainStyledAttributes(resourceId, attrs)
try {
typedArray.block()
} finally {
typedArray.recycle()
}
}
/**
* Schedules a job with the Service [T].
*
* @param jobId the id of the job being scheduled
* @param buildSequence a lambda to set the appropriate JobInfo.Builder params
*/
@RequiresApi(21)
inline fun <reified T : Service> Context.scheduleJob(
jobId: Int,
buildSequence: JobInfo.Builder.() -> Unit
): Int {
val info = JobInfo.Builder(jobId, ComponentName(this, T::class.java))
.apply(buildSequence)
.build()
val scheduler = this.getSystemService(Context.JOB_SCHEDULER_SERVICE) as? JobScheduler
return scheduler?.schedule(info) ?: JobScheduler.RESULT_FAILURE
}