Reuse search result item viewmodels

This commit is contained in:
MM20
2023-04-21 13:37:00 +02:00
parent d1482ad112
commit df1aa8c119
29 changed files with 658 additions and 481 deletions
@@ -3,14 +3,18 @@ package de.mm20.launcher2.search.data
import android.content.ActivityNotFoundException
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.LauncherActivityInfo
import android.content.pm.LauncherApps
import android.content.pm.PackageManager
import android.graphics.drawable.AdaptiveIconDrawable
import android.net.Uri
import android.os.Bundle
import android.os.Process
import android.os.UserHandle
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import androidx.core.content.getSystemService
import de.mm20.launcher2.applications.R
import de.mm20.launcher2.compat.PackageManagerCompat
@@ -44,6 +48,9 @@ data class LauncherApp(
val isMainProfile = launcherActivityInfo.user == Process.myUserHandle()
val canUninstall: Boolean
get() = flags and ApplicationInfo.FLAG_SYSTEM == 0 && isMainProfile
override val domain: String = Domain
override val preferDetailsOverLaunch: Boolean = false
@@ -148,7 +155,60 @@ data class LauncherApp(
}
}
fun uninstall(context: Context) {
val intent = Intent(Intent.ACTION_DELETE)
intent.data = Uri.parse("package:$`package`")
context.startActivity(intent)
}
fun openAppInfo(context: Context) {
val launcherApps = context.getSystemService<LauncherApps>()!!
launcherApps.startAppDetailsActivity(
ComponentName(`package`, activity),
getUser(),
null,
null
)
}
suspend fun shareApkFile(context: Context) {
val launcherApps = context.getSystemService<LauncherApps>()!!
val fileCopy = java.io.File(
context.cacheDir,
"${`package`}-${version}.apk"
)
withContext(Dispatchers.IO) {
try {
val user = getUser()
val info = if (user != null) {
launcherApps.getApplicationInfo(`package`, 0, user)
} else {
context.packageManager.getApplicationInfo(`package`, 0)
}
val file = java.io.File(info.publicSourceDir)
try {
file.copyTo(fileCopy, false)
} catch (e: FileAlreadyExistsException) {
// Do nothing. If the file is already there we don't have to copy it again.
}
} catch (e: PackageManager.NameNotFoundException) {
}
}
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val uri = FileProvider.getUriForFile(
context,
context.applicationContext.packageName + ".fileprovider",
fileCopy
)
shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
shareIntent.type = "application/vnd.android.package-archive"
withContext(Dispatchers.Main) {
context.startActivity(Intent.createChooser(shareIntent, null))
}
}
companion object {
private fun getStoreLinkForInstaller(
@@ -3,6 +3,7 @@ package de.mm20.launcher2.search.data
import android.content.ContentUris
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.CalendarContract
import de.mm20.launcher2.icons.ColorLayer
@@ -10,6 +11,7 @@ import de.mm20.launcher2.icons.StaticLauncherIcon
import de.mm20.launcher2.icons.TextLayer
import de.mm20.launcher2.ktx.tryStartActivity
import de.mm20.launcher2.search.SavableSearchable
import java.net.URLEncoder
import java.text.SimpleDateFormat
data class CalendarEvent(
@@ -57,6 +59,23 @@ data class CalendarEvent(
return context.tryStartActivity(getLaunchIntent(), options)
}
fun openLocation(context: Context) {
context.tryStartActivity(
Intent(Intent.ACTION_VIEW)
.setData(
Uri.parse(
"geo:0,0?q=${
URLEncoder.encode(
location,
"utf8"
)
}"
)
)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
)
}
companion object {
const val Domain = "calendar"
}
@@ -129,6 +129,11 @@ interface File : SavableSearchable {
val isDeletable: Boolean
get() = false
val canShare: Boolean
get() = false
fun share(context: Context) {}
suspend fun delete(context: Context) {}
}
@@ -335,4 +335,20 @@ data class LocalFile(
return metaData
}
}
override val canShare: Boolean
get() = !isDirectory
override fun share(context: Context) {
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val uri = FileProvider.getUriForFile(
context,
context.applicationContext.packageName + ".fileprovider",
java.io.File(path)
)
shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
shareIntent.type = mimeType
context.startActivity(Intent.createChooser(shareIntent, null))
}
}
@@ -90,6 +90,16 @@ data class Website(
return context.tryStartActivity(getLaunchIntent(), options)
}
fun share(context: Context) {
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.putExtra(
Intent.EXTRA_TEXT,
"${label}\n\n${description}\n\n${url}"
)
shareIntent.type = "text/plain"
context.startActivity(Intent.createChooser(shareIntent, null))
}
companion object {
const val Domain = "web"
}
@@ -7,6 +7,7 @@ import android.net.Uri
import android.os.Bundle
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.content.ContextCompat
import androidx.core.text.HtmlCompat
import de.mm20.launcher2.icons.ColorLayer
import de.mm20.launcher2.icons.StaticLauncherIcon
import de.mm20.launcher2.icons.TintedIconLayer
@@ -53,6 +54,18 @@ data class Wikipedia(
return context.tryStartActivity(getLaunchIntent(), options)
}
fun share(context: Context) {
val text = HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY).toString()
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.putExtra(
Intent.EXTRA_TEXT, "${label}\n\n" +
"${text.substring(0, 200)}\n\n" +
url
)
shareIntent.type = "text/plain"
context.startActivity(Intent.createChooser(shareIntent, null))
}
companion object {
const val Domain = "wikipedia"
}