@@ -76,9 +76,9 @@ fun Context.getApplicationLabel(packageName: String): String {
7676/* *
7777 * Return true it the user has enabled the do not disturb mode.
7878 */
79- fun isDoNotDisturbModeOn (context : Context ): Boolean {
79+ fun Context. isDoNotDisturbModeOn (): Boolean {
8080 // We cannot use NotificationManagerCompat here.
81- val setting = context. getSystemService<NotificationManager >()!! .currentInterruptionFilter
81+ val setting = getSystemService<NotificationManager >()!! .currentInterruptionFilter
8282
8383 return setting == NotificationManager .INTERRUPTION_FILTER_NONE ||
8484 setting == NotificationManager .INTERRUPTION_FILTER_ALARMS
@@ -92,10 +92,10 @@ fun isDoNotDisturbModeOn(context: Context): Boolean {
9292 * will return false and the notification privacy will fallback to "LOW_DETAIL".
9393 */
9494@SuppressLint(" BatteryLife" )
95- fun requestDisablingBatteryOptimization (activity : Activity , activityResultLauncher : ActivityResultLauncher <Intent >) {
95+ fun Context. requestDisablingBatteryOptimization (activityResultLauncher : ActivityResultLauncher <Intent >) {
9696 val intent = Intent ()
9797 intent.action = Settings .ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
98- intent.data = Uri .parse(" package:" + activity. packageName)
98+ intent.data = Uri .parse(" package:$ packageName" )
9999 activityResultLauncher.launch(intent)
100100}
101101
@@ -106,103 +106,98 @@ fun requestDisablingBatteryOptimization(activity: Activity, activityResultLaunch
106106/* *
107107 * Copy a text to the clipboard, and display a Toast when done.
108108 *
109- * @param context the context
109+ * @receiver the context
110110 * @param text the text to copy
111111 * @param toastMessage content of the toast message as a String resource. Null for no toast
112112 */
113- fun copyToClipboard (
114- context : Context ,
113+ fun Context.copyToClipboard (
115114 text : CharSequence ,
116115 toastMessage : String? = null
117116) {
118- CopyToClipboardUseCase (context ).execute(text)
119- toastMessage?.let { context. toast(it) }
117+ CopyToClipboardUseCase (this ).execute(text)
118+ toastMessage?.let { toast(it) }
120119}
121120
122121/* *
123122 * Shows notification settings for the current app.
124123 * In android O will directly opens the notification settings, in lower version it will show the App settings
125124 */
126- fun startNotificationSettingsIntent (context : Context , activityResultLauncher : ActivityResultLauncher <Intent >) {
125+ fun Context. startNotificationSettingsIntent (activityResultLauncher : ActivityResultLauncher <Intent >) {
127126 val intent = Intent ()
128127 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
129128 intent.action = Settings .ACTION_APP_NOTIFICATION_SETTINGS
130- intent.putExtra(Settings .EXTRA_APP_PACKAGE , context. packageName)
129+ intent.putExtra(Settings .EXTRA_APP_PACKAGE , packageName)
131130 } else {
132131 intent.action = Settings .ACTION_APPLICATION_DETAILS_SETTINGS
133132 intent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
134- intent.data = Uri .fromParts(" package" , context. packageName, null )
133+ intent.data = Uri .fromParts(" package" , packageName, null )
135134 }
136135 activityResultLauncher.launch(intent)
137136}
138137
139- fun openAppSettingsPage (
140- activity : Activity ,
141- noActivityFoundMessage : String = activity.getString(R .string.error_no_compatible_app_found),
138+ fun Context.openAppSettingsPage (
139+ noActivityFoundMessage : String = getString(R .string.error_no_compatible_app_found),
142140) {
143141 try {
144- activity. startActivity(
142+ startActivity(
145143 Intent ().apply {
146144 action = Settings .ACTION_APPLICATION_DETAILS_SETTINGS
147145 addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
148- data = Uri .fromParts(" package" , activity. packageName, null )
146+ data = Uri .fromParts(" package" , packageName, null )
149147 }
150148 )
151149 } catch (activityNotFoundException: ActivityNotFoundException ) {
152- activity. toast(noActivityFoundMessage)
150+ toast(noActivityFoundMessage)
153151 }
154152}
155153
156154/* *
157155 * Shows notification system settings for the given channel id.
158156 */
159157@TargetApi(Build .VERSION_CODES .O )
160- fun startNotificationChannelSettingsIntent (activity : Activity , channelID : String ) {
158+ fun Activity. startNotificationChannelSettingsIntent (channelID : String ) {
161159 if (! supportNotificationChannels()) return
162160 val intent = Intent (Settings .ACTION_CHANNEL_NOTIFICATION_SETTINGS ).apply {
163- putExtra(Settings .EXTRA_APP_PACKAGE , activity. packageName)
161+ putExtra(Settings .EXTRA_APP_PACKAGE , packageName)
164162 putExtra(Settings .EXTRA_CHANNEL_ID , channelID)
165163 }
166- activity. startActivity(intent)
164+ startActivity(intent)
167165}
168166
169- fun startAddGoogleAccountIntent (
170- context : Context ,
167+ fun Context.startAddGoogleAccountIntent (
171168 activityResultLauncher : ActivityResultLauncher <Intent >,
172- noActivityFoundMessage : String = context. getString(R .string.error_no_compatible_app_found),
169+ noActivityFoundMessage : String = getString(R .string.error_no_compatible_app_found),
173170) {
174171 val intent = Intent (Settings .ACTION_ADD_ACCOUNT )
175172 intent.putExtra(Settings .EXTRA_ACCOUNT_TYPES , arrayOf(" com.google" ))
176173 try {
177174 activityResultLauncher.launch(intent)
178175 } catch (activityNotFoundException: ActivityNotFoundException ) {
179- context. toast(noActivityFoundMessage)
176+ toast(noActivityFoundMessage)
180177 }
181178}
182179
183180@RequiresApi(Build .VERSION_CODES .O )
184- fun startInstallFromSourceIntent (
185- context : Context ,
181+ fun Context.startInstallFromSourceIntent (
186182 activityResultLauncher : ActivityResultLauncher <Intent >,
187- noActivityFoundMessage : String = context. getString(R .string.error_no_compatible_app_found),
183+ noActivityFoundMessage : String = getString(R .string.error_no_compatible_app_found),
188184) {
189185 val intent = Intent (Settings .ACTION_MANAGE_UNKNOWN_APP_SOURCES )
190- .setData(Uri .parse(String .format(" package:%s" , context. packageName)))
186+ .setData(Uri .parse(String .format(" package:%s" , packageName)))
191187 try {
192188 activityResultLauncher.launch(intent)
193189 } catch (activityNotFoundException: ActivityNotFoundException ) {
194- context. toast(noActivityFoundMessage)
190+ toast(noActivityFoundMessage)
195191 }
196192}
197193
198- fun startSharePlainTextIntent (
199- context : Context ,
194+ fun Context.startSharePlainTextIntent (
200195 activityResultLauncher : ActivityResultLauncher <Intent >? ,
201196 chooserTitle : String? ,
202197 text : String ,
203198 subject : String? = null,
204199 extraTitle : String? = null,
205- noActivityFoundMessage : String = context. getString(R .string.error_no_compatible_app_found),
200+ noActivityFoundMessage : String = getString(R .string.error_no_compatible_app_found),
206201) {
207202 val share = Intent (Intent .ACTION_SEND )
208203 share.type = " text/plain"
@@ -220,25 +215,24 @@ fun startSharePlainTextIntent(
220215 if (activityResultLauncher != null ) {
221216 activityResultLauncher.launch(intent)
222217 } else {
223- context. startActivity(intent)
218+ startActivity(intent)
224219 }
225220 } catch (activityNotFoundException: ActivityNotFoundException ) {
226- context. toast(noActivityFoundMessage)
221+ toast(noActivityFoundMessage)
227222 }
228223}
229224
230- fun startImportTextFromFileIntent (
231- context : Context ,
225+ fun Context.startImportTextFromFileIntent (
232226 activityResultLauncher : ActivityResultLauncher <Intent >,
233- noActivityFoundMessage : String = context. getString(R .string.error_no_compatible_app_found),
227+ noActivityFoundMessage : String = getString(R .string.error_no_compatible_app_found),
234228) {
235229 val intent = Intent (Intent .ACTION_GET_CONTENT ).apply {
236230 type = " text/plain"
237231 }
238232 try {
239233 activityResultLauncher.launch(intent)
240234 } catch (activityNotFoundException: ActivityNotFoundException ) {
241- context. toast(noActivityFoundMessage)
235+ toast(noActivityFoundMessage)
242236 }
243237}
244238
0 commit comments