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
126 lines (120 loc) · 4.3 KB
/
Copy pathContext.kt
File metadata and controls
126 lines (120 loc) · 4.3 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
/*
* 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.content.Context
import android.content.Intent
import android.content.res.TypedArray
import android.net.Uri
import android.os.Bundle
import android.support.annotation.AttrRes
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()
}
}
/**
* Creates an [Intent] for the component [T] with explicitly specified parameters
*
* @see [Intent.setAction]
* @see [Intent.setData]
* @see [Intent.setType]
* @see [Intent.setDataAndType]
* @see [Intent.addCategory]
* @see [Intent.putExtras]
*/
inline fun <reified T : Any> Context.intentFor(
action: String? = null,
data: Uri? = null,
flags: Int? = null,
type: String? = null,
categories: Set<String> = emptySet(),
extras: Bundle? = null
): Intent {
val intent = intentOf(
action = action,
data = data,
flags = flags,
type = type,
categories = categories,
extras = extras
)
intent.setClass(this, T::class.java)
return intent
}