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 557
Expand file tree
/
Copy pathContext.kt
More file actions
149 lines (140 loc) · 5.75 KB
/
Copy pathContext.kt
File metadata and controls
149 lines (140 loc) · 5.75 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* 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.
*/
@file:Suppress("NOTHING_TO_INLINE") // Aliases to public API.
package androidx.core.content
import android.content.Context
import android.content.res.Resources
import android.content.res.TypedArray
import android.support.annotation.AttrRes
import android.support.annotation.BoolRes
import android.support.annotation.DimenRes
import android.support.annotation.IntegerRes
import android.support.annotation.RequiresApi
import android.support.annotation.StyleRes
import android.util.AttributeSet
/**
* 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()
}
}
/**
* Return a boolean associated with a particular resource ID. This can be
* used with any integral resource value, and will return true if it is
* non-zero.
*
* @param id The desired resource identifier, as generated by the aapt
* tool. This integer encodes the package, type, and resource
* entry. The value 0 is an invalid identifier.
*
* @throws android.content.res.Resources.NotFoundException Throws NotFoundException if the given ID does not exist.
*
* @return Returns the boolean value contained in the resource.
*/
@Throws(Resources.NotFoundException::class)
inline fun Context.getBoolean(@BoolRes id: Int) = resources.getBoolean(id)
/**
* Return an integer associated with a particular resource ID.
*
* @param id The desired resource identifier, as generated by the aapt
* tool. This integer encodes the package, type, and resource
* entry. The value 0 is an invalid identifier.
*
* @throws android.content.res.Resources.NotFoundException Throws NotFoundException if the given ID does not exist.
*
* @return Returns the integer value contained in the resource.
*/
@Throws(Resources.NotFoundException::class)
inline fun Context.getInteger(@IntegerRes id: Int) = resources.getInteger(id)
/**
* Retrieve a dimensional for a particular resource ID. Unit
* conversions are based on the current {@link DisplayMetrics} associated
* with the resources.
*
* @param id The desired resource identifier, as generated by the aapt
* tool. This integer encodes the package, type, and resource
* entry. The value 0 is an invalid identifier.
*
* @return Resource dimension value multiplied by the appropriate
* metric.
*
* @throws Resources.NotFoundException Throws NotFoundException if the given ID does not exist.
*
* @see android.content.res.Resources.getDimensionPixelOffset
* @see android.content.res.Resources.getDimensionPixelSize
*/
@Throws(Resources.NotFoundException::class)
inline fun Context.getDimension(@DimenRes id: Int) = resources.getDimension(id)