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

Commit a977acf1 authored by Sunny Shao's avatar Sunny Shao
Browse files

[Catalyst] Provide deep-link of Modes > Do Not Disturb

Test: manual
Bug: 408813103
Flag: com.android.settings.flags.device_state
Change-Id: Ie5bce1b32b5b4d2adb87f143515232cbc555ad59
parent c0ed9adc
Loading
Loading
Loading
Loading
+70 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.settings.notification.modes.devicestate

import android.annotation.FlaggedApi
import android.app.Flags as AppFlags
import android.content.Context
import android.content.Intent
import android.provider.Settings.EXTRA_AUTOMATIC_ZEN_RULE_ID
import com.android.settings.Settings.ModeSettingsActivity
import com.android.settings.contract.TAG_DEVICE_STATE_SCREEN
import com.android.settings.flags.Flags
import com.android.settingslib.metadata.PreferenceAvailabilityProvider
import com.android.settingslib.metadata.PreferenceMetadata
import com.android.settingslib.metadata.PreferenceSummaryProvider
import com.android.settingslib.metadata.PreferenceTitleProvider
import com.android.settingslib.metadata.ProvidePreferenceScreen
import com.android.settingslib.metadata.preferenceHierarchy
import com.android.settingslib.preference.PreferenceFragment
import com.android.settingslib.preference.PreferenceScreenCreator

@ProvidePreferenceScreen(ZenModeDndScreen.KEY)
class ZenModeDndScreen :
    PreferenceScreenCreator,
    PreferenceAvailabilityProvider,
    PreferenceTitleProvider,
    PreferenceSummaryProvider {
    override val key: String
        get() = KEY

    override fun tags(context: Context) = arrayOf(TAG_DEVICE_STATE_SCREEN)

    override fun isFlagEnabled(context: Context) = Flags.deviceState() && AppFlags.modesUi()

    override fun fragmentClass() = PreferenceFragment::class.java

    override fun isAvailable(context: Context) = AppFlags.modesUi() && context.hasDndMode()

    override fun getTitle(context: Context): CharSequence? = context.getDndMode()?.name

    override fun getSummary(context: Context): CharSequence? =
        context.getZenModeScreenSummary(context.getDndMode())

    @FlaggedApi(AppFlags.FLAG_MODES_UI)
    override fun getLaunchIntent(context: Context, metadata: PreferenceMetadata?): Intent? =
        if (AppFlags.modesUi())
            Intent(context, ModeSettingsActivity::class.java)
                .putExtra(EXTRA_AUTOMATIC_ZEN_RULE_ID, context.getDndMode()?.id)
        else null

    override fun getPreferenceHierarchy(context: Context) = preferenceHierarchy(context, this) {}

    companion object {
        const val KEY = "device_state_dnd_mode_screen"
    }
}
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.settings.notification.modes.devicestate

import android.content.Context
import android.service.notification.ZenModeConfig.MANUAL_RULE_ID
import com.android.settings.R
import com.android.settings.Utils.createAccessibleSequence
import com.android.settingslib.notification.modes.ZenMode
import com.android.settingslib.notification.modes.ZenModeDescriptions
import com.android.settingslib.notification.modes.ZenModesBackend

internal fun Context.getBackend() = ZenModesBackend.getInstance(this)

internal fun Context.hasDndMode() = (getBackend().getMode(MANUAL_RULE_ID) != null)

internal fun Context.getDndMode(): ZenMode? = getBackend().getMode(MANUAL_RULE_ID)

internal fun Context.getZenModeScreenSummary(zenMode: ZenMode?): String {
    if (zenMode == null) return ""
    val status = zenMode.status
    val descriptions = ZenModeDescriptions(this)
    val statusText = getStatusText(status, descriptions.getTriggerDescription(zenMode))
    val triggerDescriptionForA11y = descriptions.getTriggerDescriptionForAccessibility(zenMode)
    return if (triggerDescriptionForA11y != null) {
        createAccessibleSequence(statusText, getStatusText(status, triggerDescriptionForA11y))
            .toString()
    } else {
        statusText
    }
}

private fun Context.getStatusText(status: ZenMode.Status, triggerDescription: String?): String =
    when (status) {
        ZenMode.Status.ENABLED_AND_ACTIVE ->
            if (triggerDescription.isNullOrEmpty()) {
                getString(R.string.zen_mode_active_text)
            } else {
                getString(
                    R.string.zen_mode_format_status_and_trigger,
                    getString(R.string.zen_mode_active_text),
                    triggerDescription,
                )
            }
        ZenMode.Status.ENABLED -> triggerDescription ?: ""
        ZenMode.Status.DISABLED_BY_USER -> getString(R.string.zen_mode_disabled_by_user)
        ZenMode.Status.DISABLED_BY_OTHER -> getString(R.string.zen_mode_disabled_needs_setup)
    }