|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 The AxionAOSP Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | + * except in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | + * KIND, either express or implied. See the License for the specific language governing |
| 12 | + * permissions and limitations under the License. |
| 13 | + */ |
| 14 | +package com.android.settings.display; |
| 15 | + |
| 16 | +import android.app.ActivityManager; |
| 17 | +import android.content.ContentResolver; |
| 18 | +import android.content.Context; |
| 19 | +import android.database.ContentObserver; |
| 20 | +import android.net.Uri; |
| 21 | +import android.os.Handler; |
| 22 | +import android.os.Looper; |
| 23 | +import android.os.SystemProperties; |
| 24 | +import android.provider.Settings; |
| 25 | +import android.provider.Settings.Secure; |
| 26 | + |
| 27 | +import androidx.annotation.VisibleForTesting; |
| 28 | +import androidx.preference.Preference; |
| 29 | +import androidx.preference.PreferenceScreen; |
| 30 | + |
| 31 | +import com.android.settings.R; |
| 32 | +import com.android.settings.core.TogglePreferenceController; |
| 33 | +import com.android.settingslib.core.lifecycle.LifecycleObserver; |
| 34 | +import com.android.settingslib.core.lifecycle.events.OnStart; |
| 35 | +import com.android.settingslib.core.lifecycle.events.OnStop; |
| 36 | + |
| 37 | +public class HdrDisplayPreferenceController extends TogglePreferenceController |
| 38 | + implements LifecycleObserver, OnStart, OnStop { |
| 39 | + |
| 40 | + private static final String HDR_DISPLAY_SETTING = "hdr_display"; |
| 41 | + |
| 42 | + @VisibleForTesting |
| 43 | + ContentObserver mContentObserver; |
| 44 | + private Preference mPreference; |
| 45 | + |
| 46 | + public HdrDisplayPreferenceController(Context context, String key) { |
| 47 | + super(context, key); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public int getAvailabilityStatus() { |
| 52 | + return SystemProperties.getBoolean("ro.surface_flinger.has_HDR_display", false) |
| 53 | + ? AVAILABLE : UNSUPPORTED_ON_DEVICE; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean isChecked() { |
| 58 | + return Settings.Secure.getInt(mContext.getContentResolver(), HDR_DISPLAY_SETTING, 1) != 0; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean setChecked(boolean isChecked) { |
| 63 | + return Settings.Secure.putInt( |
| 64 | + mContext.getContentResolver(), |
| 65 | + HDR_DISPLAY_SETTING, |
| 66 | + isChecked ? 1 : 0 |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public int getSliceHighlightMenuRes() { |
| 72 | + return R.string.menu_key_display; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onStart() { |
| 77 | + if (!isAvailable()) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + final ContentResolver cr = mContext.getContentResolver(); |
| 82 | + mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) { |
| 83 | + @Override |
| 84 | + public void onChange(boolean selfChange, Uri uri) { |
| 85 | + super.onChange(selfChange, uri); |
| 86 | + if (HDR_DISPLAY_SETTING.equals(uri.getLastPathSegment())) { |
| 87 | + updateState(mPreference); |
| 88 | + } |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + cr.registerContentObserver(Settings.Secure.getUriFor(HDR_DISPLAY_SETTING), |
| 93 | + false, mContentObserver, ActivityManager.getCurrentUser()); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void onStop() { |
| 98 | + if (mContentObserver != null) { |
| 99 | + mContext.getContentResolver().unregisterContentObserver(mContentObserver); |
| 100 | + mContentObserver = null; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void displayPreference(PreferenceScreen screen) { |
| 106 | + super.displayPreference(screen); |
| 107 | + mPreference = screen.findPreference(getPreferenceKey()); |
| 108 | + } |
| 109 | +} |
0 commit comments