feat: add intent to apply icon packs externally (#6643)
Add ApplyIconPackActivity that handles app.lawnchair.APPLY_ICONS intent, allowing third-party icon pack apps to directly apply their icon pack to Lawnchair without requiring users to navigate to settings. - Register activity with intent-filter in AndroidManifest - Validate icon pack before showing confirmation sheet - Reuse iconPackIntents from PreferenceViewModel
This commit is contained in:
1 parent
31027746cb
commit
604c42fa0a
4 files changed
+179
-1
No files matched your search
@@ -106,6 +106,16 @@
|
||||
android:name="com.android.launcher3.secondarydisplay.SecondaryDisplayLauncher"
|
||||
tools:node="remove" />
|
||||
|
||||
<activity
|
||||
android:name="app.lawnchair.ui.ApplyIconPackActivity"
|
||||
android:exported="true"
|
||||
android:theme="@style/Theme.Transparent">
|
||||
<intent-filter>
|
||||
<action android:name="app.lawnchair.APPLY_ICONS" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name="app.lawnchair.BlankActivity"
|
||||
android:theme="@style/Theme.Transparent" />
|
||||
|
||||
@@ -271,6 +271,7 @@
|
||||
-->
|
||||
<!-- General strings -->
|
||||
<string name="icon_style_label">Icon style</string>
|
||||
<string name="apply_icon_pack_title">Apply icon pack</string>
|
||||
<string name="icon_shape_label">Icon shape</string>
|
||||
<string name="icon_sizes">Icon size</string>
|
||||
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* Copyright 2026, Lawnchair
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package app.lawnchair.ui
|
||||
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.requiredWidth
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.ModalBottomSheet
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.rememberModalBottomSheetState
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import app.lawnchair.icons.CustomAdaptiveIconDrawable
|
||||
import app.lawnchair.preferences.PreferenceManager
|
||||
import app.lawnchair.ui.preferences.components.layout.PreferenceGroup
|
||||
import app.lawnchair.ui.preferences.components.layout.PreferenceTemplate
|
||||
import app.lawnchair.ui.preferences.iconPackIntents
|
||||
import app.lawnchair.ui.theme.EdgeToEdge
|
||||
import app.lawnchair.ui.theme.LawnchairTheme
|
||||
import com.android.launcher3.R
|
||||
import com.google.accompanist.drawablepainter.rememberDrawablePainter
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class ApplyIconPackActivity : ComponentActivity() {
|
||||
|
||||
companion object {
|
||||
const val EXTRA_PACKAGE_NAME = "packageName"
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterial3ExpressiveApi::class)
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
|
||||
val packPackageName = intent.getStringExtra(EXTRA_PACKAGE_NAME).orEmpty()
|
||||
if (packPackageName.isEmpty()) {
|
||||
finish()
|
||||
return
|
||||
}
|
||||
|
||||
setContent {
|
||||
var packInfo by remember { mutableStateOf<Pair<String, Drawable>?>(null) }
|
||||
var resolved by remember { mutableStateOf(false) }
|
||||
|
||||
LaunchedEffect(packPackageName) {
|
||||
val result = withContext(Dispatchers.IO) {
|
||||
resolveIconPackInfo(packPackageName)
|
||||
}
|
||||
packInfo = result
|
||||
resolved = true
|
||||
if (result == null) finish()
|
||||
}
|
||||
|
||||
val info = packInfo
|
||||
if (resolved && info != null) {
|
||||
LawnchairTheme {
|
||||
EdgeToEdge()
|
||||
ApplyIconPackSheet(
|
||||
packName = info.first,
|
||||
packIcon = info.second,
|
||||
onConfirm = {
|
||||
PreferenceManager.getInstance(this@ApplyIconPackActivity)
|
||||
.iconPackPackage.set(packPackageName)
|
||||
finish()
|
||||
},
|
||||
onDismiss = { finish() },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun resolveIconPackInfo(packageName: String): Pair<String, Drawable>? {
|
||||
val pm = this.packageManager
|
||||
val resolveInfo = iconPackIntents
|
||||
.flatMap { pm.queryIntentActivities(it, 0) }
|
||||
.firstOrNull { it.activityInfo.packageName == packageName }
|
||||
?: return null
|
||||
val name = resolveInfo.loadLabel(pm).toString()
|
||||
val icon = CustomAdaptiveIconDrawable.wrapNonNull(resolveInfo.loadIcon(pm))
|
||||
return name to icon
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterial3ExpressiveApi::class)
|
||||
@Composable
|
||||
private fun ApplyIconPackSheet(
|
||||
packName: String,
|
||||
packIcon: Drawable,
|
||||
onConfirm: () -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
ModalBottomSheet(
|
||||
onDismissRequest = onDismiss,
|
||||
sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true),
|
||||
containerColor = MaterialTheme.colorScheme.surface,
|
||||
) {
|
||||
ModalBottomSheetContent(
|
||||
title = { Text(text = stringResource(id = R.string.apply_icon_pack_title)) },
|
||||
content = {
|
||||
PreferenceGroup {
|
||||
Item {
|
||||
PreferenceTemplate(
|
||||
title = { Text(text = packName) },
|
||||
startWidget = {
|
||||
Image(
|
||||
painter = rememberDrawablePainter(drawable = packIcon),
|
||||
contentDescription = packName,
|
||||
modifier = Modifier.size(36.dp),
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
buttons = {
|
||||
OutlinedButton(
|
||||
onClick = onDismiss,
|
||||
shapes = ButtonDefaults.shapes(),
|
||||
) {
|
||||
Text(text = stringResource(id = android.R.string.cancel))
|
||||
}
|
||||
Spacer(modifier = Modifier.requiredWidth(8.dp))
|
||||
Button(
|
||||
onClick = onConfirm,
|
||||
shapes = ButtonDefaults.shapes(),
|
||||
) {
|
||||
Text(text = stringResource(id = R.string.action_apply))
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
|
||||
private val iconPackIntents = listOf(
|
||||
internal val iconPackIntents = listOf(
|
||||
Intent("com.novalauncher.THEME"),
|
||||
Intent("org.adw.launcher.icons.ACTION_PICK_ICON"),
|
||||
Intent("com.dlto.atom.launcher.THEME"),
|
||||
|
||||
Reference in new issue
Block a user