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 e7a615ea71
commit e7cab7ac1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 55 deletions

View file

@ -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
)
}

View file

@ -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,

View file

@ -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,

View file

@ -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<NotificationManager>()!!.currentInterruptionFilter
val setting = getSystemService<NotificationManager>()!!.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<Intent>) {
fun Context.requestDisablingBatteryOptimization(activityResultLauncher: ActivityResultLauncher<Intent>) {
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<Intent>) {
fun Context.startNotificationSettingsIntent(activityResultLauncher: ActivityResultLauncher<Intent>) {
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<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)
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<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)
.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<Intent>?,
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<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 {
type = "text/plain"
@ -238,7 +232,7 @@ fun startImportTextFromFileIntent(
try {
activityResultLauncher.launch(intent)
} catch (activityNotFoundException: ActivityNotFoundException) {
context.toast(noActivityFoundMessage)
toast(noActivityFoundMessage)
}
}

View file

@ -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),

View file

@ -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)
}
}
}