Remove (useless, potentially dangerous) casts to AppCompatActivity

This commit is contained in:
MM20 2022-02-18 23:33:26 +01:00
parent 3c3cac83c8
commit 6b59aa5118
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
19 changed files with 82 additions and 74 deletions

View File

@ -196,7 +196,7 @@ fun AppItem(
label = stringResource(R.string.menu_app_info),
icon = Icons.Rounded.Info
) {
viewModel.openAppInfo(context as AppCompatActivity)
viewModel.openAppInfo(context)
})
toolbarActions.add(
@ -204,7 +204,7 @@ fun AppItem(
label = stringResource(R.string.menu_launch),
icon = Icons.Rounded.OpenInNew,
action = {
viewModel.launch(context as AppCompatActivity)
viewModel.launch(context)
}
)
)
@ -216,7 +216,7 @@ fun AppItem(
icon = Icons.Rounded.Share
) {
scope.launch {
viewModel.shareApkFile(context as AppCompatActivity)
viewModel.shareApkFile(context)
}
}
} else {
@ -228,7 +228,7 @@ fun AppItem(
label = stringResource(R.string.share_menu_store_link, storeDetails.label),
icon = Icons.Rounded.Share,
action = {
viewModel.shareStoreLink(context as AppCompatActivity, storeDetails.url)
viewModel.shareStoreLink(context, storeDetails.url)
}
),
DefaultToolbarAction(
@ -236,7 +236,7 @@ fun AppItem(
icon = Icons.Rounded.Share
) {
scope.launch {
viewModel.shareApkFile(context as AppCompatActivity)
viewModel.shareApkFile(context)
}
}
)
@ -250,7 +250,7 @@ fun AppItem(
label = stringResource(R.string.menu_uninstall),
icon = Icons.Rounded.Delete,
) {
viewModel.uninstall(context as AppCompatActivity)
viewModel.uninstall(context)
onBack()
}
)

View File

@ -8,14 +8,9 @@ import android.graphics.drawable.Drawable
import android.net.Uri
import android.provider.Settings
import android.service.notification.StatusBarNotification
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import androidx.core.content.getSystemService
import de.mm20.launcher2.badges.BadgeRepository
import de.mm20.launcher2.crashreporter.CrashReporter
import de.mm20.launcher2.favorites.FavoritesRepository
import de.mm20.launcher2.icons.IconRepository
import de.mm20.launcher2.icons.LauncherIcon
import de.mm20.launcher2.ktx.tryStartActivity
import de.mm20.launcher2.notifications.NotificationRepository
import de.mm20.launcher2.search.data.AppShortcut
@ -25,7 +20,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
class AppItemVM(
@ -41,8 +35,8 @@ class AppItemVM(
notificationRepository.cancelNotification(notification)
}
fun openAppInfo(activity: AppCompatActivity) {
activity.tryStartActivity(
fun openAppInfo(context: Context) {
context.tryStartActivity(
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.parse("package:${app.`package`}")
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
@ -50,14 +44,14 @@ class AppItemVM(
)
}
suspend fun shareApkFile(activity: AppCompatActivity) {
suspend fun shareApkFile(context: Context) {
val fileCopy = java.io.File(
activity.cacheDir,
context.cacheDir,
"${app.`package`}-${app.version}.apk"
)
withContext(Dispatchers.IO) {
try {
val info = activity.packageManager
val info = context.packageManager
.getApplicationInfo(app.`package`, 0)
val file = java.io.File(info.publicSourceDir)
@ -73,30 +67,30 @@ class AppItemVM(
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val uri = FileProvider.getUriForFile(
activity,
activity.applicationContext.packageName + ".fileprovider",
context,
context.applicationContext.packageName + ".fileprovider",
fileCopy
)
shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
shareIntent.type = "application/vnd.android.package-archive"
withContext(Dispatchers.Main) {
activity.startActivity(Intent.createChooser(shareIntent, null))
context.startActivity(Intent.createChooser(shareIntent, null))
}
}
fun shareStoreLink(activity: AppCompatActivity, url: String) {
fun shareStoreLink(context: Context, url: String) {
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.putExtra(Intent.EXTRA_TEXT, url)
shareIntent.type = "text/plain"
activity.startActivity(Intent.createChooser(shareIntent, null))
context.startActivity(Intent.createChooser(shareIntent, null))
}
val canUninstall = app.flags and ApplicationInfo.FLAG_SYSTEM == 0
fun uninstall(activity: AppCompatActivity) {
fun uninstall(context: Context) {
val intent = Intent(Intent.ACTION_DELETE)
intent.data = Uri.parse("package:" + app.`package`)
activity.startActivity(intent)
context.startActivity(intent)
}

View File

@ -135,7 +135,7 @@ fun CalendarItem(
modifier = Modifier
.fillMaxWidth()
.clickable {
viewModel.openLocation(context as AppCompatActivity)
viewModel.openLocation(context)
}
) {
Icon(
@ -199,7 +199,7 @@ fun CalendarItem(
label = stringResource(R.string.calendar_menu_open_externally),
icon = Icons.Rounded.OpenInNew,
action = {
viewModel.launch(context as AppCompatActivity)
viewModel.launch(context)
onBack()
}
)

View File

@ -4,8 +4,6 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.text.format.DateUtils
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import de.mm20.launcher2.ktx.tryStartActivity
import de.mm20.launcher2.search.data.CalendarEvent
import de.mm20.launcher2.ui.R
@ -50,15 +48,34 @@ class CalendarItemVM(
}
fun formatTime(context: Context): String {
if (calendarEvent.allDay) return DateUtils.formatDateRange(context, calendarEvent.startTime, calendarEvent.endTime, DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_SHOW_WEEKDAY)
return DateUtils.formatDateRange(context, calendarEvent.startTime, calendarEvent.endTime, DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_SHOW_TIME or DateUtils.FORMAT_SHOW_WEEKDAY)
if (calendarEvent.allDay) return DateUtils.formatDateRange(
context,
calendarEvent.startTime,
calendarEvent.endTime,
DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_SHOW_WEEKDAY
)
return DateUtils.formatDateRange(
context,
calendarEvent.startTime,
calendarEvent.endTime,
DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_SHOW_TIME or DateUtils.FORMAT_SHOW_WEEKDAY
)
}
fun openLocation(context: AppCompatActivity) {
fun openLocation(context: Context) {
context.tryStartActivity(
Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("geo:0,0?q=${URLEncoder.encode(calendarEvent.location, "utf8")}"))
.setData(
Uri.parse(
"geo:0,0?q=${
URLEncoder.encode(
calendarEvent.location,
"utf8"
)
}"
)
)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
)
}

View File

@ -1,5 +1,6 @@
package de.mm20.launcher2.ui.launcher.search.common
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.ui.geometry.Rect
import androidx.core.app.ActivityOptionsCompat
@ -44,10 +45,11 @@ abstract class SearchableItemVM(
}
fun launch(context: AppCompatActivity, bounds: Rect? = null): Boolean {
val options = if (bounds != null) {
fun launch(context: Context, bounds: Rect? = null): Boolean {
val view = (context as? AppCompatActivity)?.window?.decorView
val options = if (bounds != null && view != null) {
ActivityOptionsCompat.makeClipRevealAnimation(
context.window.decorView,
view,
bounds.left.toInt(),
bounds.top.toInt(),
bounds.width.toInt(),

View File

@ -66,7 +66,7 @@ fun GridItem(modifier: Modifier = Modifier, item: Searchable) {
badge = badge,
icon = icon,
onClick = {
if (!launchOnPress || !viewModel.launch(context as AppCompatActivity, bounds)) {
if (!launchOnPress || !viewModel.launch(context, bounds)) {
showPopup = true
}
},

View File

@ -32,11 +32,14 @@ import kotlinx.coroutines.delay
import kotlin.math.ceil
@Composable
fun SearchResultGrid(items: List<Searchable>) {
fun SearchResultGrid(
items: List<Searchable>,
modifier: Modifier = Modifier,
) {
val columns = LocalGridColumns.current
Column(
modifier = Modifier.animateContentSize().fillMaxWidth().padding(4.dp)
modifier = modifier.animateContentSize().fillMaxWidth().padding(4.dp)
) {
for (i in 0 until ceil(items.size / columns.toFloat()).toInt()) {
Row {

View File

@ -149,7 +149,7 @@ fun ListItem(modifier: Modifier = Modifier, item: Searchable) {
modifier = Modifier.combinedClickable(
enabled = !showDetails,
onClick = {
if (!viewModel.launch(context as AppCompatActivity, bounds)) {
if (!viewModel.launch(context, bounds)) {
showDetails = true
}
},

View File

@ -110,7 +110,7 @@ fun ContactItem(
modifier = Modifier.padding(end = 16.dp),
text = it.label,
onClick = {
viewModel.contact(context as AppCompatActivity, it)
viewModel.contact(context, it)
}
)
}
@ -134,7 +134,7 @@ fun ContactItem(
modifier = Modifier.padding(end = 16.dp),
text = it.label,
onClick = {
viewModel.contact(context as AppCompatActivity, it)
viewModel.contact(context, it)
}
)
}
@ -158,7 +158,7 @@ fun ContactItem(
modifier = Modifier.padding(end = 16.dp),
text = it.label,
onClick = {
viewModel.contact(context as AppCompatActivity, it)
viewModel.contact(context, it)
}
)
}
@ -182,7 +182,7 @@ fun ContactItem(
modifier = Modifier.padding(end = 16.dp),
text = it.label,
onClick = {
viewModel.contact(context as AppCompatActivity, it)
viewModel.contact(context, it)
}
)
}
@ -206,7 +206,7 @@ fun ContactItem(
modifier = Modifier.padding(end = 16.dp),
text = it.label,
onClick = {
viewModel.contact(context as AppCompatActivity, it)
viewModel.contact(context, it)
}
)
}
@ -263,7 +263,7 @@ fun ContactItem(
label = stringResource(R.string.calendar_menu_open_externally),
icon = Icons.Rounded.OpenInNew,
action = {
viewModel.launch(context as AppCompatActivity)
viewModel.launch(context)
}
)
)

View File

@ -1,20 +1,20 @@
package de.mm20.launcher2.ui.launcher.search.contacts
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import de.mm20.launcher2.ktx.tryStartActivity
import de.mm20.launcher2.search.data.Contact
import de.mm20.launcher2.search.data.ContactInfo
import de.mm20.launcher2.ui.launcher.search.common.SearchableItemVM
import org.koin.core.component.KoinComponent
class ContactItemVM(
val contact: Contact
): SearchableItemVM(contact) {
fun contact(context: AppCompatActivity, contactInfo: ContactInfo) {
) : SearchableItemVM(contact) {
fun contact(context: Context, contactInfo: ContactInfo) {
context.tryStartActivity(
Intent(Intent.ACTION_VIEW).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setData(Uri.parse(contactInfo.data))
Intent(Intent.ACTION_VIEW).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setData(Uri.parse(contactInfo.data))
)
}
}

View File

@ -164,7 +164,7 @@ fun FileItem(
label = stringResource(R.string.menu_open_file),
icon = Icons.Rounded.OpenInNew,
action = {
viewModel.launch(context as AppCompatActivity)
viewModel.launch(context)
}
)
)
@ -174,7 +174,7 @@ fun FileItem(
label = stringResource(R.string.menu_share),
icon = Icons.Rounded.Share,
action = {
viewModel.share(context as AppCompatActivity)
viewModel.share(context)
}
))
}

View File

@ -1,7 +1,7 @@
package de.mm20.launcher2.ui.launcher.search.files
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import de.mm20.launcher2.files.FileRepository
import de.mm20.launcher2.search.data.File
@ -21,7 +21,7 @@ class FileItemVM(
fileRepository.deleteFile(file)
}
fun share(context: AppCompatActivity) {
fun share(context: Context) {
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val uri = FileProvider.getUriForFile(

View File

@ -98,7 +98,7 @@ fun AppItem(
label = stringResource(R.string.menu_app_info),
icon = Icons.Rounded.Info
) {
viewModel.openAppInfo(context as AppCompatActivity)
viewModel.openAppInfo(context)
})
Toolbar(

View File

@ -1,15 +1,15 @@
package de.mm20.launcher2.ui.launcher.search.shortcut
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import androidx.appcompat.app.AppCompatActivity
import de.mm20.launcher2.ktx.tryStartActivity
import de.mm20.launcher2.search.data.AppShortcut
import de.mm20.launcher2.ui.launcher.search.common.SearchableItemVM
class ShortcutItemVM(private val shortcut: AppShortcut) : SearchableItemVM(shortcut) {
fun openAppInfo(context: AppCompatActivity) {
fun openAppInfo(context: Context) {
context.tryStartActivity(
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.parse("package:${shortcut.launcherShortcut.`package`}")

View File

@ -47,7 +47,7 @@ fun WebsiteItem(
Column(
modifier = modifier.clickable {
viewModel.launch(context as AppCompatActivity)
viewModel.launch(context)
}
) {
if (website.image.isNotBlank()) {
@ -104,7 +104,7 @@ fun WebsiteItem(
label = stringResource(R.string.menu_share),
icon= Icons.Rounded.Share,
action = {
viewModel.share(context as AppCompatActivity)
viewModel.share(context)
}
)
)

View File

@ -1,18 +1,15 @@
package de.mm20.launcher2.ui.launcher.search.website
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import de.mm20.launcher2.favorites.FavoritesRepository
import de.mm20.launcher2.search.data.Website
import de.mm20.launcher2.ui.launcher.search.common.SearchableItemVM
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
class WebsiteItemVM(
private val website: Website
): SearchableItemVM(website) {
) : SearchableItemVM(website) {
fun share(context: AppCompatActivity) {
fun share(context: Context) {
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.putExtra(
Intent.EXTRA_TEXT,

View File

@ -47,7 +47,7 @@ fun WikipediaItem(
Column(
modifier = modifier.clickable {
viewModel.launch(context as AppCompatActivity)
viewModel.launch(context)
}
) {
if (!wikipedia.image.isNullOrEmpty()) {
@ -110,7 +110,7 @@ fun WikipediaItem(
label = stringResource(R.string.menu_share),
icon = Icons.Rounded.Share,
action = {
viewModel.share(context as AppCompatActivity)
viewModel.share(context)
}
)
)

View File

@ -1,20 +1,16 @@
package de.mm20.launcher2.ui.launcher.search.wikipedia
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import androidx.core.text.HtmlCompat
import de.mm20.launcher2.favorites.FavoritesRepository
import de.mm20.launcher2.search.data.Wikipedia
import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.launcher.search.common.SearchableItemVM
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
class WikipediaItemVM(
private val wikipedia: Wikipedia
) : SearchableItemVM(wikipedia) {
fun share(context: AppCompatActivity) {
fun share(context: Context) {
val text = HtmlCompat.fromHtml(wikipedia.text, HtmlCompat.FROM_HTML_MODE_LEGACY).toString()
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.putExtra(

View File

@ -418,7 +418,6 @@ class ApplicationDetailRepresentation : Representation, KoinComponent {
private fun shareApk(context: Context, app: Application) {
val handler = Handler()
val progressDialog = ProgressDialog(context)
progressDialog.setMessage(context.getString(R.string.dialog_wait))
progressDialog.show()
val executor = Executors.newSingleThreadExecutor()
executor.execute {