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

Unverified Commit 5ec610e2 authored by Jesse Chan's avatar Jesse Chan Committed by Michael Bestas
Browse files

Settings: support black theme for dark mode



Co-authored-by: default avatarDmitrii <bankersenator@gmail.com>
Co-authored-by: default avatarMichael Bestas <mkbestas@lineageos.org>
Change-Id: I12451c0b72f73b08b885e8103bbe3e74a0c4e19f
parent 7bbfc5a7
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -32,6 +32,10 @@
    <string name="backup_transport_setting_label">Change backup provider</string>
    <string name="backup_transport_title">Select backup provider</string>

    <!-- Black theme for dark mode -->
    <string name="berry_black_theme_title">Pure black</string>
    <string name="berry_black_theme_summary">Pure black background for dark theme</string>

    <!-- Device Info screen. LineageOS legal. -->
    <string name="lineagelicense_title">LineageOS legal</string>

+58 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) The LineageOS 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.display.darkmode

import android.content.Context
import androidx.preference.SwitchPreferenceCompat
import com.android.settings.R
import com.android.settingslib.metadata.BooleanValuePreference
import com.android.settingslib.metadata.PreferenceMetadata
import com.android.settingslib.preference.PreferenceBinding
import lineageos.providers.LineageSettings

class DarkModeBlackThemePreference(context: Context, private val darkModeStorage: DarkModeStorage) :
    PreferenceMetadata, BooleanValuePreference, PreferenceBinding {

    private val blackThemeStorage = DarkModeBlackThemeStorage(context)

    override val key: String = LineageSettings.Secure.BERRY_BLACK_THEME

    override val title: Int = R.string.berry_black_theme_title

    override val summary: Int = R.string.berry_black_theme_summary

    override fun storage(context: Context) = blackThemeStorage

    override fun isEnabled(context: Context): Boolean {
        return darkModeStorage.getBoolean(DarkModeMainSwitchPreference.KEY) ?: false
    }

    override fun createWidget(context: Context) =
        SwitchPreferenceCompat(context).apply { isPersistent = false }

    fun isAvailable(context: Context): Boolean {
        return try {
            context.packageManager.getPackageInfo(
                "org.lineageos.overlay.customization.blacktheme",
                0,
            )
            true
        } catch (e: Exception) {
            false
        }
    }
}
+75 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) The LineageOS 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.display.darkmode

import android.content.Context
import android.database.ContentObserver
import android.os.Handler
import android.os.Looper
import com.android.settingslib.datastore.AbstractKeyedDataObservable
import com.android.settingslib.datastore.KeyValueStore
import com.android.settingslib.datastore.Permissions
import com.android.settingslib.metadata.PreferenceChangeReason
import lineageos.providers.LineageSettings

@Suppress("UNCHECKED_CAST")
class DarkModeBlackThemeStorage(private val context: Context) :
    AbstractKeyedDataObservable<String>(), KeyValueStore {

    private val settingsObserver =
        object : ContentObserver(Handler(Looper.getMainLooper())) {
            override fun onChange(selfChange: Boolean) {
                notifyChange(LineageSettings.Secure.BERRY_BLACK_THEME, PreferenceChangeReason.VALUE)
            }
        }

    override fun contains(key: String) = key == LineageSettings.Secure.BERRY_BLACK_THEME

    override fun <T : Any> getValue(key: String, valueType: Class<T>): T? {
        val value = LineageSettings.Secure.getInt(context.contentResolver, key, 0) != 0
        return value as T
    }

    override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
        val boolValue = value as? Boolean ?: false
        LineageSettings.Secure.putInt(
            context.contentResolver,
            LineageSettings.Secure.BERRY_BLACK_THEME,
            if (boolValue) 1 else 0,
        )
        // Notify immediately for UI responsiveness
        notifyChange(key, PreferenceChangeReason.VALUE)
    }

    override fun onFirstObserverAdded() {
        context.contentResolver.registerContentObserver(
            LineageSettings.Secure.getUriFor(LineageSettings.Secure.BERRY_BLACK_THEME),
            false,
            settingsObserver,
        )
    }

    override fun onLastObserverRemoved() {
        context.contentResolver.unregisterContentObserver(settingsObserver)
    }

    companion object {
        fun getReadPermissions() = Permissions.EMPTY

        fun getWritePermissions() = Permissions.allOf("lineageos.permission.WRITE_SECURE_SETTINGS")
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -112,6 +112,7 @@ abstract class BaseDarkModeScreen(context: Context) :
                    +ExpandedDarkModeSelectorPreference(modeStorage)
                }
            }
            +DarkModeBlackThemePreference(context, darkModeStorage)
            +PreferenceCategory("display_category", R.string.dark_theme_timing_category) += {
                val uiModeManager = context.getSystemService(UiModeManager::class.java)
                +DarkModeSchedulePreference(uiModeManager!!, BedtimeSettings(context))