Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c4233acb authored by Charlotte Lu's avatar Charlotte Lu Committed by Android (Google) Code Review
Browse files

Merge "Change into SpaBaseDialogActivity" into main

parents 2c4c9168 7cb16ee4
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -74,8 +74,7 @@
            android:exported="false">
        </provider>
        <activity
            android:name="com.android.settingslib.spa.gallery.SpaDialogActivity"
            android:excludeFromRecents="true"
            android:name="com.android.settingslib.spa.gallery.GalleryDialogActivity"
            android:exported="true"
            android:theme="@style/Theme.SpaLib.Dialog">
        </activity>
+45 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * 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 com.android.settingslib.spa.gallery

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import com.android.settingslib.spa.SpaBaseDialogActivity
import com.android.settingslib.spa.widget.dialog.AlertDialogButton
import com.android.settingslib.spa.widget.dialog.SettingsAlertDialogWithIcon

class GalleryDialogActivity : SpaBaseDialogActivity() {
    @Composable
    override fun Content() {
        SettingsAlertDialogWithIcon(
            onDismissRequest = { finish() },
            confirmButton = AlertDialogButton("confirm") { finish() },
            dismissButton = AlertDialogButton("dismiss") { finish() },
            title = "title",
            text = {
                Text(
                    "text",
                    modifier = Modifier.fillMaxWidth(),
                    textAlign = TextAlign.Center
                )
            }
        )
    }
}
+46 −0
Original line number Diff line number Diff line
@@ -14,34 +14,17 @@
 * limitations under the License.
 */

package com.android.settingslib.spa.gallery
package com.android.settingslib.spa

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.WarningAmber
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
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.graphics.vector.ImageVector
import com.android.settingslib.spa.framework.common.LogCategory
import com.android.settingslib.spa.framework.common.SpaEnvironmentFactory
import com.android.settingslib.spa.framework.theme.SettingsTheme
import com.android.settingslib.spa.widget.dialog.getDialogWidth


class SpaDialogActivity : ComponentActivity() {
abstract class SpaBaseDialogActivity : ComponentActivity() {
    private val spaEnvironment get() = SpaEnvironmentFactory.instance

    override fun onCreate(savedInstanceState: Bundle?) {
@@ -55,77 +38,9 @@ class SpaDialogActivity : ComponentActivity() {
    }

    @Composable
    fun Content() {
        var openAlertDialog by remember { mutableStateOf(false) }
        AlertDialog(openAlertDialog)
        LaunchedEffect(key1 = Unit) {
            openAlertDialog = true
        }
    }

    @Composable
    fun AlertDialog(openAlertDialog: Boolean) {
        when {
            openAlertDialog -> {
                AlertDialogExample(
                    onDismissRequest = { finish() },
                    onConfirmation = { finish() },
                    dialogTitle = intent.getStringExtra(DIALOG_TITLE) ?: DIALOG_TITLE,
                    dialogText = intent.getStringExtra(DIALOG_TEXT) ?: DIALOG_TEXT,
                    icon = Icons.Default.WarningAmber
                )
            }
        }
    }

    @Composable
    fun AlertDialogExample(
        onDismissRequest: () -> Unit,
        onConfirmation: () -> Unit,
        dialogTitle: String,
        dialogText: String,
        icon: ImageVector,
    ) {
        AlertDialog(
            modifier = Modifier.width(getDialogWidth()),
            icon = {
                Icon(icon, contentDescription = null)
            },
            title = {
                Text(text = dialogTitle)
            },
            text = {
                Text(text = dialogText)
            },
            onDismissRequest = {
                onDismissRequest()
            },
            dismissButton = {
                OutlinedButton(
                    onClick = {
                        onDismissRequest()
                    }
                ) {
                    Text(intent.getStringExtra(DISMISS_TEXT) ?: DISMISS_TEXT)
                }
            },
            confirmButton = {
                Button(
                    onClick = {
                        onConfirmation()
                    },
                ) {
                    Text(intent.getStringExtra(CONFIRM_TEXT) ?: CONFIRM_TEXT)
                }
            }
        )
    }
    abstract fun Content()

    companion object {
        private const val TAG = "SpaDialogActivity"
        private const val DIALOG_TITLE = "dialogTitle"
        private const val DIALOG_TEXT = "dialogText"
        private const val CONFIRM_TEXT = "confirmText"
        private const val DISMISS_TEXT = "dismissText"
        private const val TAG = "SpaBaseDialogActivity"
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -102,8 +102,8 @@ private fun AlertDialogPresenter.SettingsAlertDialog(
fun getDialogWidth(): Dp {
    val configuration = LocalConfiguration.current
    return configuration.screenWidthDp.dp * when (configuration.orientation) {
        Configuration.ORIENTATION_LANDSCAPE -> 0.6f
        else -> 0.8f
        Configuration.ORIENTATION_LANDSCAPE -> 0.65f
        else -> 0.85f
    }
}

+88 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * 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 com.android.settingslib.spa.widget.dialog

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.WarningAmber
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.window.DialogProperties

@Composable
fun SettingsAlertDialogWithIcon(
    onDismissRequest: () -> Unit,
    confirmButton: AlertDialogButton?,
    dismissButton: AlertDialogButton?,
    title: String?,
    text: @Composable (() -> Unit)?,
) {
    AlertDialog(
        onDismissRequest = onDismissRequest,
        icon = { Icon(Icons.Default.WarningAmber, contentDescription = null) },
        modifier = Modifier.width(getDialogWidth()),
        confirmButton = {
            confirmButton?.let {
                Button(
                    onClick = {
                        it.onClick()
                    },
                ) {
                    Text(it.text)
                }
            }
        },
        dismissButton = dismissButton?.let {
            {
                OutlinedButton(
                    onClick = {
                        it.onClick()
                    },
                ) {
                    Text(it.text)
                }
            }
        },
        title = title?.let {
            {
                Text(
                    it,
                    modifier = Modifier.fillMaxWidth(),
                    textAlign = TextAlign.Center
                )
            }
        },
        text = text?.let {
            {
                Column(Modifier.verticalScroll(rememberScrollState())) {
                    text()
                }
            }
        },
        properties = DialogProperties(usePlatformDefaultWidth = false),
    )
}
 No newline at end of file
Loading