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.
This commit is contained in:
Marco Romano 2023-07-18 15:11:11 +02:00 committed by GitHub
parent e4a7fcc60c
commit 78773b68e7
6 changed files with 42 additions and 55 deletions

View file

@ -16,7 +16,6 @@
package io.element.android.appnav.loggedin package io.element.android.appnav.loggedin
import android.app.Activity
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
@ -32,14 +31,12 @@ fun LoggedInView(
state: LoggedInState, state: LoggedInState,
modifier: Modifier = Modifier modifier: Modifier = Modifier
) { ) {
val activity = LocalContext.current as? Activity val context = LocalContext.current
PermissionsView( PermissionsView(
state = state.permissionsState, state = state.permissionsState,
modifier = modifier, modifier = modifier,
openSystemSettings = { openSystemSettings = context::openAppSettingsPage
activity?.let { openAppSettingsPage(it) }
}
) )
} }

View file

@ -76,8 +76,7 @@ class RoomDetailsNode @AssistedInject constructor(
val permalinkResult = alias?.let { PermalinkBuilder.permalinkForRoomAlias(it) } val permalinkResult = alias?.let { PermalinkBuilder.permalinkForRoomAlias(it) }
?: PermalinkBuilder.permalinkForRoomId(room.roomId) ?: PermalinkBuilder.permalinkForRoomId(room.roomId)
permalinkResult.onSuccess { permalink -> permalinkResult.onSuccess { permalink ->
startSharePlainTextIntent( context.startSharePlainTextIntent(
context = context,
activityResultLauncher = null, activityResultLauncher = null,
chooserTitle = context.getString(R.string.screen_room_details_share_room_title), chooserTitle = context.getString(R.string.screen_room_details_share_room_title),
text = permalink, text = permalink,
@ -91,8 +90,7 @@ class RoomDetailsNode @AssistedInject constructor(
private fun onShareMember(context: Context, member: RoomMember) { private fun onShareMember(context: Context, member: RoomMember) {
val permalinkResult = PermalinkBuilder.permalinkForUser(member.userId) val permalinkResult = PermalinkBuilder.permalinkForUser(member.userId)
permalinkResult.onSuccess { permalink -> permalinkResult.onSuccess { permalink ->
startSharePlainTextIntent( context.startSharePlainTextIntent(
context = context,
activityResultLauncher = null, activityResultLauncher = null,
chooserTitle = context.getString(R.string.screen_room_details_share_room_title), chooserTitle = context.getString(R.string.screen_room_details_share_room_title),
text = permalink, text = permalink,

View file

@ -68,8 +68,7 @@ class RoomMemberDetailsNode @AssistedInject constructor(
fun onShareUser() { fun onShareUser() {
val permalinkResult = PermalinkBuilder.permalinkForUser(inputs.roomMemberId) val permalinkResult = PermalinkBuilder.permalinkForUser(inputs.roomMemberId)
permalinkResult.onSuccess { permalink -> permalinkResult.onSuccess { permalink ->
startSharePlainTextIntent( context.startSharePlainTextIntent(
context = context,
activityResultLauncher = null, activityResultLauncher = null,
chooserTitle = context.getString(R.string.screen_room_details_share_room_title), chooserTitle = context.getString(R.string.screen_room_details_share_room_title),
text = permalink, text = permalink,

View file

@ -76,9 +76,9 @@ fun Context.getApplicationLabel(packageName: String): String {
/** /**
* Return true it the user has enabled the do not disturb mode. * 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. // We cannot use NotificationManagerCompat here.
val setting = context.getSystemService<NotificationManager>()!!.currentInterruptionFilter val setting = getSystemService<NotificationManager>()!!.currentInterruptionFilter
return setting == NotificationManager.INTERRUPTION_FILTER_NONE || return setting == NotificationManager.INTERRUPTION_FILTER_NONE ||
setting == NotificationManager.INTERRUPTION_FILTER_ALARMS 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". * will return false and the notification privacy will fallback to "LOW_DETAIL".
*/ */
@SuppressLint("BatteryLife") @SuppressLint("BatteryLife")
fun requestDisablingBatteryOptimization(activity: Activity, activityResultLauncher: ActivityResultLauncher<Intent>) { fun Context.requestDisablingBatteryOptimization(activityResultLauncher: ActivityResultLauncher<Intent>) {
val intent = Intent() val intent = Intent()
intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
intent.data = Uri.parse("package:" + activity.packageName) intent.data = Uri.parse("package:$packageName")
activityResultLauncher.launch(intent) activityResultLauncher.launch(intent)
} }
@ -106,50 +106,48 @@ fun requestDisablingBatteryOptimization(activity: Activity, activityResultLaunch
/** /**
* Copy a text to the clipboard, and display a Toast when done. * 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 text the text to copy
* @param toastMessage content of the toast message as a String resource. Null for no toast * @param toastMessage content of the toast message as a String resource. Null for no toast
*/ */
fun copyToClipboard( fun Context.copyToClipboard(
context: Context,
text: CharSequence, text: CharSequence,
toastMessage: String? = null toastMessage: String? = null
) { ) {
CopyToClipboardUseCase(context).execute(text) CopyToClipboardUseCase(this).execute(text)
toastMessage?.let { context.toast(it) } toastMessage?.let { toast(it) }
} }
/** /**
* Shows notification settings for the current app. * 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 * In android O will directly opens the notification settings, in lower version it will show the App settings
*/ */
fun startNotificationSettingsIntent(context: Context, activityResultLauncher: ActivityResultLauncher<Intent>) { fun Context.startNotificationSettingsIntent(activityResultLauncher: ActivityResultLauncher<Intent>) {
val intent = Intent() val intent = Intent()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName) intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
} else { } else {
intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 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) activityResultLauncher.launch(intent)
} }
fun openAppSettingsPage( fun Context.openAppSettingsPage(
activity: Activity, noActivityFoundMessage: String = getString(R.string.error_no_compatible_app_found),
noActivityFoundMessage: String = activity.getString(R.string.error_no_compatible_app_found),
) { ) {
try { try {
activity.startActivity( startActivity(
Intent().apply { Intent().apply {
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
data = Uri.fromParts("package", activity.packageName, null) data = Uri.fromParts("package", packageName, null)
} }
) )
} catch (activityNotFoundException: ActivityNotFoundException) { } catch (activityNotFoundException: ActivityNotFoundException) {
activity.toast(noActivityFoundMessage) toast(noActivityFoundMessage)
} }
} }
@ -157,52 +155,49 @@ fun openAppSettingsPage(
* Shows notification system settings for the given channel id. * Shows notification system settings for the given channel id.
*/ */
@TargetApi(Build.VERSION_CODES.O) @TargetApi(Build.VERSION_CODES.O)
fun startNotificationChannelSettingsIntent(activity: Activity, channelID: String) { fun Activity.startNotificationChannelSettingsIntent(channelID: String) {
if (!supportNotificationChannels()) return if (!supportNotificationChannels()) return
val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply { 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) putExtra(Settings.EXTRA_CHANNEL_ID, channelID)
} }
activity.startActivity(intent) startActivity(intent)
} }
fun startAddGoogleAccountIntent( fun Context.startAddGoogleAccountIntent(
context: Context,
activityResultLauncher: ActivityResultLauncher<Intent>, activityResultLauncher: ActivityResultLauncher<Intent>,
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) val intent = Intent(Settings.ACTION_ADD_ACCOUNT)
intent.putExtra(Settings.EXTRA_ACCOUNT_TYPES, arrayOf("com.google")) intent.putExtra(Settings.EXTRA_ACCOUNT_TYPES, arrayOf("com.google"))
try { try {
activityResultLauncher.launch(intent) activityResultLauncher.launch(intent)
} catch (activityNotFoundException: ActivityNotFoundException) { } catch (activityNotFoundException: ActivityNotFoundException) {
context.toast(noActivityFoundMessage) toast(noActivityFoundMessage)
} }
} }
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
fun startInstallFromSourceIntent( fun Context.startInstallFromSourceIntent(
context: Context,
activityResultLauncher: ActivityResultLauncher<Intent>, activityResultLauncher: ActivityResultLauncher<Intent>,
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) 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 { try {
activityResultLauncher.launch(intent) activityResultLauncher.launch(intent)
} catch (activityNotFoundException: ActivityNotFoundException) { } catch (activityNotFoundException: ActivityNotFoundException) {
context.toast(noActivityFoundMessage) toast(noActivityFoundMessage)
} }
} }
fun startSharePlainTextIntent( fun Context.startSharePlainTextIntent(
context: Context,
activityResultLauncher: ActivityResultLauncher<Intent>?, activityResultLauncher: ActivityResultLauncher<Intent>?,
chooserTitle: String?, chooserTitle: String?,
text: String, text: String,
subject: String? = null, subject: String? = null,
extraTitle: 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) val share = Intent(Intent.ACTION_SEND)
share.type = "text/plain" share.type = "text/plain"
@ -220,17 +215,16 @@ fun startSharePlainTextIntent(
if (activityResultLauncher != null) { if (activityResultLauncher != null) {
activityResultLauncher.launch(intent) activityResultLauncher.launch(intent)
} else { } else {
context.startActivity(intent) startActivity(intent)
} }
} catch (activityNotFoundException: ActivityNotFoundException) { } catch (activityNotFoundException: ActivityNotFoundException) {
context.toast(noActivityFoundMessage) toast(noActivityFoundMessage)
} }
} }
fun startImportTextFromFileIntent( fun Context.startImportTextFromFileIntent(
context: Context,
activityResultLauncher: ActivityResultLauncher<Intent>, activityResultLauncher: ActivityResultLauncher<Intent>,
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 { val intent = Intent(Intent.ACTION_GET_CONTENT).apply {
type = "text/plain" type = "text/plain"
@ -238,7 +232,7 @@ fun startImportTextFromFileIntent(
try { try {
activityResultLauncher.launch(intent) activityResultLauncher.launch(intent)
} catch (activityNotFoundException: ActivityNotFoundException) { } catch (activityNotFoundException: ActivityNotFoundException) {
context.toast(noActivityFoundMessage) toast(noActivityFoundMessage)
} }
} }

View file

@ -37,8 +37,7 @@ class InviteFriendsUseCase @Inject constructor(
permalinkResult.fold( permalinkResult.fold(
onSuccess = { permalink -> onSuccess = { permalink ->
val appName = buildMeta.applicationName val appName = buildMeta.applicationName
startSharePlainTextIntent( activity.startSharePlainTextIntent(
context = activity,
activityResultLauncher = null, activityResultLauncher = null,
chooserTitle = stringProvider.getString(CommonStrings.action_invite_friends), chooserTitle = stringProvider.getString(CommonStrings.action_invite_friends),
text = stringProvider.getString(CommonStrings.invite_friends_text, appName, permalink), text = stringProvider.getString(CommonStrings.invite_friends_text, appName, permalink),

View file

@ -165,15 +165,15 @@ class NotificationChannels @Inject constructor(
private fun supportNotificationChannels() = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) private fun supportNotificationChannels() = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
fun openSystemSettingsForSilentCategory(activity: Activity) { fun openSystemSettingsForSilentCategory(activity: Activity) {
startNotificationChannelSettingsIntent(activity, SILENT_NOTIFICATION_CHANNEL_ID) activity.startNotificationChannelSettingsIntent(SILENT_NOTIFICATION_CHANNEL_ID)
} }
fun openSystemSettingsForNoisyCategory(activity: Activity) { fun openSystemSettingsForNoisyCategory(activity: Activity) {
startNotificationChannelSettingsIntent(activity, NOISY_NOTIFICATION_CHANNEL_ID) activity.startNotificationChannelSettingsIntent(NOISY_NOTIFICATION_CHANNEL_ID)
} }
fun openSystemSettingsForCallCategory(activity: Activity) { fun openSystemSettingsForCallCategory(activity: Activity) {
startNotificationChannelSettingsIntent(activity, CALL_NOTIFICATION_CHANNEL_ID) activity.startNotificationChannelSettingsIntent(CALL_NOTIFICATION_CHANNEL_ID)
} }
} }
} }