1 /* 2 * Copyright (C) 2022 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 17 package com.android.settings.display; 18 19 import android.content.Context; 20 import android.hardware.display.DisplayManager; 21 import android.view.Display; 22 23 import androidx.annotation.VisibleForTesting; 24 25 import com.android.settings.R; 26 import com.android.settings.core.BasePreferenceController; 27 28 /** Controller that switch the screen resolution. */ 29 public class ScreenResolutionController extends BasePreferenceController { 30 31 static final int FHD_WIDTH = 1080; 32 static final int QHD_WIDTH = 1440; 33 34 private Display mDisplay; 35 ScreenResolutionController(Context context, String key)36 public ScreenResolutionController(Context context, String key) { 37 super(context, key); 38 39 mDisplay = 40 mContext.getSystemService(DisplayManager.class).getDisplay(Display.DEFAULT_DISPLAY); 41 } 42 43 /** Check if the width is supported by the display. */ isSupportedMode(int width)44 private boolean isSupportedMode(int width) { 45 for (Display.Mode mode : getSupportedModes()) { 46 if (mode.getPhysicalWidth() == width) return true; 47 } 48 return false; 49 } 50 51 /** Return true if the device contains two (or more) resolutions. */ checkSupportedResolutions()52 protected boolean checkSupportedResolutions() { 53 return isSupportedMode(FHD_WIDTH) && isSupportedMode(QHD_WIDTH); 54 } 55 56 @Override getAvailabilityStatus()57 public int getAvailabilityStatus() { 58 return (checkSupportedResolutions()) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 59 } 60 61 @Override getSummary()62 public CharSequence getSummary() { 63 String summary = null; 64 switch (getDisplayWidth()) { 65 case FHD_WIDTH: 66 summary = mContext.getString(R.string.screen_resolution_summary_high); 67 break; 68 case QHD_WIDTH: 69 summary = mContext.getString(R.string.screen_resolution_summary_highest); 70 break; 71 default: 72 summary = mContext.getString(R.string.screen_resolution_title); 73 } 74 75 return summary; 76 } 77 78 @VisibleForTesting getDisplayWidth()79 public int getDisplayWidth() { 80 return mDisplay.getMode().getPhysicalWidth(); 81 } 82 83 @VisibleForTesting getSupportedModes()84 public Display.Mode[] getSupportedModes() { 85 return mDisplay.getSupportedModes(); 86 } 87 } 88