From e7cab7ac1d298f25934a470cb447e60cd2ab916e Mon Sep 17 00:00:00 2001 From: Marco Romano Date: Tue, 18 Jul 2023 15:11:11 +0200 Subject: [PATCH] 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. --- .../android/appnav/loggedin/LoggedInView.kt | 7 +- .../roomdetails/impl/RoomDetailsNode.kt | 6 +- .../members/details/RoomMemberDetailsNode.kt | 3 +- .../androidutils/system/SystemUtils.kt | 72 +++++++++---------- .../deeplink/usecase/InviteFriendsUseCase.kt | 3 +- .../channels/NotificationChannels.kt | 6 +- 6 files changed, 42 insertions(+), 55 deletions(-) diff --git a/appnav/src/main/kotlin/io/element/android/appnav/loggedin/LoggedInView.kt b/appnav/src/main/kotlin/io/element/android/appnav/loggedin/LoggedInView.kt index 3f415c3dc1..dc1faa0e2d 100644 --- a/appnav/src/main/kotlin/io/element/android/appnav/loggedin/LoggedInView.kt +++ b/appnav/src/main/kotlin/io/element/android/appnav/loggedin/LoggedInView.kt @@ -16,7 +16,6 @@ package io.element.android.appnav.loggedin -import android.app.Activity import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext @@ -32,14 +31,12 @@ fun LoggedInView( state: LoggedInState, modifier: Modifier = Modifier ) { - val activity = LocalContext.current as? Activity + val context = LocalContext.current PermissionsView( state = state.permissionsState, modifier = modifier, - openSystemSettings = { - activity?.let { openAppSettingsPage(it) } - } + openSystemSettings = context::openAppSettingsPage ) } diff --git a/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsNode.kt b/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsNode.kt index 4eb70d96e3..b74cf7aaf1 100644 --- a/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsNode.kt +++ b/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/RoomDetailsNode.kt @@ -76,8 +76,7 @@ class RoomDetailsNode @AssistedInject constructor( val permalinkResult = alias?.let { PermalinkBuilder.permalinkForRoomAlias(it) } ?: PermalinkBuilder.permalinkForRoomId(room.roomId) permalinkResult.onSuccess { permalink -> - startSharePlainTextIntent( - context = context, + context.startSharePlainTextIntent( activityResultLauncher = null, chooserTitle = context.getString(R.string.screen_room_details_share_room_title), text = permalink, @@ -91,8 +90,7 @@ class RoomDetailsNode @AssistedInject constructor( private fun onShareMember(context: Context, member: RoomMember) { val permalinkResult = PermalinkBuilder.permalinkForUser(member.userId) permalinkResult.onSuccess { permalink -> - startSharePlainTextIntent( - context = context, + context.startSharePlainTextIntent( activityResultLauncher = null, chooserTitle = context.getString(R.string.screen_room_details_share_room_title), text = permalink, diff --git a/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/members/details/RoomMemberDetailsNode.kt b/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/members/details/RoomMemberDetailsNode.kt index 5425fdf79a..54b86f973c 100644 --- a/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/members/details/RoomMemberDetailsNode.kt +++ b/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/members/details/RoomMemberDetailsNode.kt @@ -68,8 +68,7 @@ class RoomMemberDetailsNode @AssistedInject constructor( fun onShareUser() { val permalinkResult = PermalinkBuilder.permalinkForUser(inputs.roomMemberId) permalinkResult.onSuccess { permalink -> - startSharePlainTextIntent( - context = context, + context.startSharePlainTextIntent( activityResultLauncher = null, chooserTitle = context.getString(R.string.screen_room_details_share_room_title), text = permalink, diff --git a/libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/system/SystemUtils.kt b/libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/system/SystemUtils.kt index c48d52a1fc..65a5dc9e0d 100644 --- a/libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/system/SystemUtils.kt +++ b/libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/system/SystemUtils.kt @@ -76,9 +76,9 @@ fun Context.getApplicationLabel(packageName: String): String { /** * Return true it the user has enabled the do not disturb mode. */ -fun isDoNotDisturbModeOn(context: Context): Boolean { +fun Context.isDoNotDisturbModeOn(): Boolean { // We cannot use NotificationManagerCompat here. - val setting = context.getSystemService()!!.currentInterruptionFilter + val setting = getSystemService()!!.currentInterruptionFilter return setting == NotificationManager.INTERRUPTION_FILTER_NONE || setting == NotificationManager.INTERRUPTION_FILTER_ALARMS @@ -92,10 +92,10 @@ fun isDoNotDisturbModeOn(context: Context): Boolean { * will return false and the notification privacy will fallback to "LOW_DETAIL". */ @SuppressLint("BatteryLife") -fun requestDisablingBatteryOptimization(activity: Activity, activityResultLauncher: ActivityResultLauncher) { +fun Context.requestDisablingBatteryOptimization(activityResultLauncher: ActivityResultLauncher) { val intent = Intent() intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS - intent.data = Uri.parse("package:" + activity.packageName) + intent.data = Uri.parse("package:$packageName") activityResultLauncher.launch(intent) } @@ -106,50 +106,48 @@ fun requestDisablingBatteryOptimization(activity: Activity, activityResultLaunch /** * Copy a text to the clipboard, and display a Toast when done. * - * @param context the context + * @receiver the context * @param text the text to copy * @param toastMessage content of the toast message as a String resource. Null for no toast */ -fun copyToClipboard( - context: Context, +fun Context.copyToClipboard( text: CharSequence, toastMessage: String? = null ) { - CopyToClipboardUseCase(context).execute(text) - toastMessage?.let { context.toast(it) } + CopyToClipboardUseCase(this).execute(text) + toastMessage?.let { toast(it) } } /** * Shows notification settings for the current app. * In android O will directly opens the notification settings, in lower version it will show the App settings */ -fun startNotificationSettingsIntent(context: Context, activityResultLauncher: ActivityResultLauncher) { +fun Context.startNotificationSettingsIntent(activityResultLauncher: ActivityResultLauncher) { val intent = Intent() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS - intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName) + intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName) } else { intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - intent.data = Uri.fromParts("package", context.packageName, null) + intent.data = Uri.fromParts("package", packageName, null) } activityResultLauncher.launch(intent) } -fun openAppSettingsPage( - activity: Activity, - noActivityFoundMessage: String = activity.getString(R.string.error_no_compatible_app_found), +fun Context.openAppSettingsPage( + noActivityFoundMessage: String = getString(R.string.error_no_compatible_app_found), ) { try { - activity.startActivity( + startActivity( Intent().apply { action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - data = Uri.fromParts("package", activity.packageName, null) + data = Uri.fromParts("package", packageName, null) } ) } catch (activityNotFoundException: ActivityNotFoundException) { - activity.toast(noActivityFoundMessage) + toast(noActivityFoundMessage) } } @@ -157,52 +155,49 @@ fun openAppSettingsPage( * Shows notification system settings for the given channel id. */ @TargetApi(Build.VERSION_CODES.O) -fun startNotificationChannelSettingsIntent(activity: Activity, channelID: String) { +fun Activity.startNotificationChannelSettingsIntent(channelID: String) { if (!supportNotificationChannels()) return val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply { - putExtra(Settings.EXTRA_APP_PACKAGE, activity.packageName) + putExtra(Settings.EXTRA_APP_PACKAGE, packageName) putExtra(Settings.EXTRA_CHANNEL_ID, channelID) } - activity.startActivity(intent) + startActivity(intent) } -fun startAddGoogleAccountIntent( - context: Context, +fun Context.startAddGoogleAccountIntent( activityResultLauncher: ActivityResultLauncher, - noActivityFoundMessage: String = context.getString(R.string.error_no_compatible_app_found), + noActivityFoundMessage: String = getString(R.string.error_no_compatible_app_found), ) { val intent = Intent(Settings.ACTION_ADD_ACCOUNT) intent.putExtra(Settings.EXTRA_ACCOUNT_TYPES, arrayOf("com.google")) try { activityResultLauncher.launch(intent) } catch (activityNotFoundException: ActivityNotFoundException) { - context.toast(noActivityFoundMessage) + toast(noActivityFoundMessage) } } @RequiresApi(Build.VERSION_CODES.O) -fun startInstallFromSourceIntent( - context: Context, +fun Context.startInstallFromSourceIntent( activityResultLauncher: ActivityResultLauncher, - noActivityFoundMessage: String = context.getString(R.string.error_no_compatible_app_found), + noActivityFoundMessage: String = getString(R.string.error_no_compatible_app_found), ) { val intent = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES) - .setData(Uri.parse(String.format("package:%s", context.packageName))) + .setData(Uri.parse(String.format("package:%s", packageName))) try { activityResultLauncher.launch(intent) } catch (activityNotFoundException: ActivityNotFoundException) { - context.toast(noActivityFoundMessage) + toast(noActivityFoundMessage) } } -fun startSharePlainTextIntent( - context: Context, +fun Context.startSharePlainTextIntent( activityResultLauncher: ActivityResultLauncher?, chooserTitle: String?, text: String, subject: String? = null, extraTitle: String? = null, - noActivityFoundMessage: String = context.getString(R.string.error_no_compatible_app_found), + noActivityFoundMessage: String = getString(R.string.error_no_compatible_app_found), ) { val share = Intent(Intent.ACTION_SEND) share.type = "text/plain" @@ -220,17 +215,16 @@ fun startSharePlainTextIntent( if (activityResultLauncher != null) { activityResultLauncher.launch(intent) } else { - context.startActivity(intent) + startActivity(intent) } } catch (activityNotFoundException: ActivityNotFoundException) { - context.toast(noActivityFoundMessage) + toast(noActivityFoundMessage) } } -fun startImportTextFromFileIntent( - context: Context, +fun Context.startImportTextFromFileIntent( activityResultLauncher: ActivityResultLauncher, - noActivityFoundMessage: String = context.getString(R.string.error_no_compatible_app_found), + noActivityFoundMessage: String = getString(R.string.error_no_compatible_app_found), ) { val intent = Intent(Intent.ACTION_GET_CONTENT).apply { type = "text/plain" @@ -238,7 +232,7 @@ fun startImportTextFromFileIntent( try { activityResultLauncher.launch(intent) } catch (activityNotFoundException: ActivityNotFoundException) { - context.toast(noActivityFoundMessage) + toast(noActivityFoundMessage) } } diff --git a/libraries/deeplink/src/main/kotlin/io/element/android/libraries/deeplink/usecase/InviteFriendsUseCase.kt b/libraries/deeplink/src/main/kotlin/io/element/android/libraries/deeplink/usecase/InviteFriendsUseCase.kt index 67f7330a0c..8ee94c31fe 100644 --- a/libraries/deeplink/src/main/kotlin/io/element/android/libraries/deeplink/usecase/InviteFriendsUseCase.kt +++ b/libraries/deeplink/src/main/kotlin/io/element/android/libraries/deeplink/usecase/InviteFriendsUseCase.kt @@ -37,8 +37,7 @@ class InviteFriendsUseCase @Inject constructor( permalinkResult.fold( onSuccess = { permalink -> val appName = buildMeta.applicationName - startSharePlainTextIntent( - context = activity, + activity.startSharePlainTextIntent( activityResultLauncher = null, chooserTitle = stringProvider.getString(CommonStrings.action_invite_friends), text = stringProvider.getString(CommonStrings.invite_friends_text, appName, permalink), diff --git a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/channels/NotificationChannels.kt b/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/channels/NotificationChannels.kt index fc4838e9a3..0624b863ed 100644 --- a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/channels/NotificationChannels.kt +++ b/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/channels/NotificationChannels.kt @@ -165,15 +165,15 @@ class NotificationChannels @Inject constructor( private fun supportNotificationChannels() = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) fun openSystemSettingsForSilentCategory(activity: Activity) { - startNotificationChannelSettingsIntent(activity, SILENT_NOTIFICATION_CHANNEL_ID) + activity.startNotificationChannelSettingsIntent(SILENT_NOTIFICATION_CHANNEL_ID) } fun openSystemSettingsForNoisyCategory(activity: Activity) { - startNotificationChannelSettingsIntent(activity, NOISY_NOTIFICATION_CHANNEL_ID) + activity.startNotificationChannelSettingsIntent(NOISY_NOTIFICATION_CHANNEL_ID) } fun openSystemSettingsForCallCategory(activity: Activity) { - startNotificationChannelSettingsIntent(activity, CALL_NOTIFICATION_CHANNEL_ID) + activity.startNotificationChannelSettingsIntent(CALL_NOTIFICATION_CHANNEL_ID) } } }