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.accessibility; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 22 import androidx.annotation.NonNull; 23 24 import com.android.settingslib.display.DisplayDensityUtils; 25 26 import java.util.Arrays; 27 import java.util.Collections; 28 import java.util.stream.Collectors; 29 30 /** 31 * Data class for storing the configurations related to the display size. 32 */ 33 public class DisplaySizeData extends PreviewSizeData<Integer> { 34 private final DisplayDensityUtils mDensity; 35 DisplaySizeData(Context context)36 DisplaySizeData(Context context) { 37 this(context, new DisplayDensityUtils(context)); 38 } 39 DisplaySizeData(@onNull Context context, @NonNull DisplayDensityUtils util)40 public DisplaySizeData(@NonNull Context context, @NonNull DisplayDensityUtils util) { 41 super(context); 42 43 mDensity = util; 44 final int initialIndex = mDensity.getCurrentIndex(); 45 if (initialIndex < 0) { 46 // Failed to obtain default density, which means we failed to 47 // connect to the window manager service. Just use the current 48 // density and don't let the user change anything. 49 final Resources resources = getContext().getResources(); 50 final int densityDpi = resources.getDisplayMetrics().densityDpi; 51 setDefaultValue(densityDpi); 52 setInitialIndex(0); 53 setValues(Collections.singletonList(densityDpi)); 54 } else { 55 setDefaultValue(mDensity.getDefaultDensity()); 56 setInitialIndex(initialIndex); 57 setValues(Arrays.stream(mDensity.getValues()).boxed() 58 .collect(Collectors.toList())); 59 } 60 } 61 62 @Override commit(int currentProgress)63 public void commit(int currentProgress) { 64 final int densityDpi = getValues().get(currentProgress); 65 if (densityDpi == getDefaultValue()) { 66 mDensity.clearForcedDisplayDensity(); 67 } else { 68 mDensity.setForcedDisplayDensity(currentProgress); 69 } 70 } 71 } 72