1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.settings.theme 17 18 import android.app.UiModeManager 19 import android.app.UiModeManager.ContrastUtils.CONTRAST_LEVEL_HIGH 20 import android.app.UiModeManager.ContrastUtils.CONTRAST_LEVEL_MEDIUM 21 import android.app.UiModeManager.ContrastUtils.toContrastLevel 22 import android.content.Context 23 import android.content.Intent 24 import android.os.UserHandle 25 import android.text.TextUtils 26 import androidx.preference.Preference 27 import com.android.internal.annotations.VisibleForTesting 28 import com.android.settings.R 29 import com.android.settings.core.BasePreferenceController 30 31 /** 32 * Controller that opens the contrast dialog and updates the text describing the current contrast 33 */ 34 class ContrastPreferenceController( 35 private val context: Context, 36 private val uiModeManager: UiModeManager) : BasePreferenceController(context, KEY) { 37 38 companion object { 39 @VisibleForTesting 40 const val KEY = "contrast_preference" 41 } 42 getAvailabilityStatusnull43 override fun getAvailabilityStatus(): Int { 44 return AVAILABLE 45 } 46 handlePreferenceTreeClicknull47 override fun handlePreferenceTreeClick(preference: Preference): Boolean { 48 if (TextUtils.equals(preference.key, preferenceKey)) { 49 val intent = Intent(Intent.ACTION_SHOW_CONTRAST_DIALOG) 50 context.startActivityAsUser(intent, UserHandle(UserHandle.USER_CURRENT)) 51 return true 52 } 53 return false 54 } 55 getSummarynull56 override fun getSummary(): CharSequence = getSummary(toContrastLevel(uiModeManager.contrast)) 57 58 @VisibleForTesting 59 fun getSummary(contrast: Int): String { 60 return when (contrast) { 61 CONTRAST_LEVEL_HIGH -> context.getString(R.string.contrast_high) 62 CONTRAST_LEVEL_MEDIUM -> context.getString(R.string.contrast_medium) 63 else -> context.getString(R.string.contrast_standard) 64 } 65 } 66 }