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