Skip to content

Commit e7cab7a

Browse files
author
Marco Romano
authored
Make the functions in SystemUtils extensions (#899)
- They are now all extensions over `Context` or `Activity` (when `Context` is not enough) (some of them already were). - Allows for IDE completion.
1 parent e7a615e commit e7cab7a

File tree

6 files changed

+42
-55
lines changed

6 files changed

+42
-55
lines changed

appnav/src/main/kotlin/io/element/android/appnav/loggedin/LoggedInView.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.element.android.appnav.loggedin
1818

19-
import android.app.Activity
2019
import androidx.compose.runtime.Composable
2120
import androidx.compose.ui.Modifier
2221
import androidx.compose.ui.platform.LocalContext
@@ -32,14 +31,12 @@ fun LoggedInView(
3231
state: LoggedInState,
3332
modifier: Modifier = Modifier
3433
) {
35-
val activity = LocalContext.current as? Activity
34+
val context = LocalContext.current
3635

3736
PermissionsView(
3837
state = state.permissionsState,
3938
modifier = modifier,
40-
openSystemSettings = {
41-
activity?.let { openAppSettingsPage(it) }
42-
}
39+
openSystemSettings = context::openAppSettingsPage
4340
)
4441
}
4542

features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsNode.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ class RoomDetailsNode @AssistedInject constructor(
7676
val permalinkResult = alias?.let { PermalinkBuilder.permalinkForRoomAlias(it) }
7777
?: PermalinkBuilder.permalinkForRoomId(room.roomId)
7878
permalinkResult.onSuccess { permalink ->
79-
startSharePlainTextIntent(
80-
context = context,
79+
context.startSharePlainTextIntent(
8180
activityResultLauncher = null,
8281
chooserTitle = context.getString(R.string.screen_room_details_share_room_title),
8382
text = permalink,
@@ -91,8 +90,7 @@ class RoomDetailsNode @AssistedInject constructor(
9190
private fun onShareMember(context: Context, member: RoomMember) {
9291
val permalinkResult = PermalinkBuilder.permalinkForUser(member.userId)
9392
permalinkResult.onSuccess { permalink ->
94-
startSharePlainTextIntent(
95-
context = context,
93+
context.startSharePlainTextIntent(
9694
activityResultLauncher = null,
9795
chooserTitle = context.getString(R.string.screen_room_details_share_room_title),
9896
text = permalink,

features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/members/details/RoomMemberDetailsNode.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ class RoomMemberDetailsNode @AssistedInject constructor(
6868
fun onShareUser() {
6969
val permalinkResult = PermalinkBuilder.permalinkForUser(inputs.roomMemberId)
7070
permalinkResult.onSuccess { permalink ->
71-
startSharePlainTextIntent(
72-
context = context,
71+
context.startSharePlainTextIntent(
7372
activityResultLauncher = null,
7473
chooserTitle = context.getString(R.string.screen_room_details_share_room_title),
7574
text = permalink,

libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/system/SystemUtils.kt

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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

libraries/deeplink/src/main/kotlin/io/element/android/libraries/deeplink/usecase/InviteFriendsUseCase.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ class InviteFriendsUseCase @Inject constructor(
3737
permalinkResult.fold(
3838
onSuccess = { permalink ->
3939
val appName = buildMeta.applicationName
40-
startSharePlainTextIntent(
41-
context = activity,
40+
activity.startSharePlainTextIntent(
4241
activityResultLauncher = null,
4342
chooserTitle = stringProvider.getString(CommonStrings.action_invite_friends),
4443
text = stringProvider.getString(CommonStrings.invite_friends_text, appName, permalink),

libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/channels/NotificationChannels.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,15 @@ class NotificationChannels @Inject constructor(
165165
private fun supportNotificationChannels() = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
166166

167167
fun openSystemSettingsForSilentCategory(activity: Activity) {
168-
startNotificationChannelSettingsIntent(activity, SILENT_NOTIFICATION_CHANNEL_ID)
168+
activity.startNotificationChannelSettingsIntent(SILENT_NOTIFICATION_CHANNEL_ID)
169169
}
170170

171171
fun openSystemSettingsForNoisyCategory(activity: Activity) {
172-
startNotificationChannelSettingsIntent(activity, NOISY_NOTIFICATION_CHANNEL_ID)
172+
activity.startNotificationChannelSettingsIntent(NOISY_NOTIFICATION_CHANNEL_ID)
173173
}
174174

175175
fun openSystemSettingsForCallCategory(activity: Activity) {
176-
startNotificationChannelSettingsIntent(activity, CALL_NOTIFICATION_CHANNEL_ID)
176+
activity.startNotificationChannelSettingsIntent(CALL_NOTIFICATION_CHANNEL_ID)
177177
}
178178
}
179179
}

0 commit comments

Comments
 (0)