1 /* 2 * Copyright (C) 2017 The Android Open Source 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.content.Context; 17 import android.os.IBinder; 18 import android.os.Parcel; 19 import android.os.RemoteException; 20 import android.os.ServiceManager; 21 import android.util.Log; 22 23 import com.android.internal.annotations.VisibleForTesting; 24 import com.android.settings.core.PreferenceControllerMixin; 25 import com.android.settingslib.core.AbstractPreferenceController; 26 27 public class ColorModePreferenceController extends AbstractPreferenceController implements 28 PreferenceControllerMixin { 29 private static final String TAG = "ColorModePreference"; 30 private static final String KEY_COLOR_MODE = "color_mode"; 31 32 private static final int SURFACE_FLINGER_TRANSACTION_QUERY_WIDE_COLOR = 1024; 33 34 private final ConfigurationWrapper mConfigWrapper; 35 36 public ColorModePreferenceController(Context context) { 37 super(context); 38 mConfigWrapper = new ConfigurationWrapper(); 39 } 40 41 @Override 42 public String getPreferenceKey() { 43 return KEY_COLOR_MODE; 44 } 45 46 @Override 47 public boolean isAvailable() { 48 return mConfigWrapper.isScreenWideColorGamut(); 49 } 50 51 @VisibleForTesting 52 static class ConfigurationWrapper { 53 private final IBinder mSurfaceFlinger; 54 55 ConfigurationWrapper() { 56 mSurfaceFlinger = ServiceManager.getService("SurfaceFlinger"); 57 } 58 59 boolean isScreenWideColorGamut() { 60 if (mSurfaceFlinger != null) { 61 final Parcel data = Parcel.obtain(); 62 final Parcel reply = Parcel.obtain(); 63 data.writeInterfaceToken("android.ui.ISurfaceComposer"); 64 try { 65 mSurfaceFlinger.transact(SURFACE_FLINGER_TRANSACTION_QUERY_WIDE_COLOR, 66 data, reply, 0); 67 return reply.readBoolean(); 68 } catch (RemoteException ex) { 69 Log.e(TAG, "Failed to query wide color support", ex); 70 } finally { 71 data.recycle(); 72 reply.recycle(); 73 } 74 } 75 return false; 76 } 77 } 78 } 79