Debian Codegraph @

sdk-common/src/main/java/com/android/ide/common/resources/configuration/DeviceConfigHelper.java

raw
1 /*
2 * Copyright (C) 2012 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.ide.common.resources.configuration;
18
19 import com.android.annotations.Nullable;
20 import com.android.resources.NightMode;
21 import com.android.resources.ScreenRound;
22 import com.android.resources.UiMode;
23 import com.android.sdklib.devices.Device;
24 import com.android.sdklib.devices.Hardware;
25 import com.android.sdklib.devices.Screen;
26 import com.android.sdklib.devices.State;
27
28 public class DeviceConfigHelper {
29 /**
30 * Returns a {@link FolderConfiguration} based on the given state
31 *
32 * @param state
33 * The {@link State} of the {@link Device} to base the
34 * {@link FolderConfiguration} on. Can be null.
35 * @return A {@link FolderConfiguration} based on the given {@link State}.
36 * If the given {@link State} is null, the result is also null;
37 */
38 @Nullable
39 public static FolderConfiguration getFolderConfig(@Nullable State state) {
40 if (state == null) {
41 return null;
42 }
43
44 Hardware hw = state.getHardware();
45
46 FolderConfiguration config = new FolderConfiguration();
47 config.createDefault();
48 Screen screen = hw.getScreen();
49 config.setDensityQualifier(new DensityQualifier(screen.getPixelDensity()));
50 config.setNavigationMethodQualifier(new NavigationMethodQualifier(hw.getNav()));
51 ScreenDimensionQualifier sdq;
52 if (screen.getXDimension() > screen.getYDimension()) {
53 sdq = new ScreenDimensionQualifier(screen.getXDimension(), screen.getYDimension());
54 } else {
55 sdq = new ScreenDimensionQualifier(screen.getYDimension(), screen.getXDimension());
56 }
57 config.setScreenDimensionQualifier(sdq);
58 config.setScreenRatioQualifier(new ScreenRatioQualifier(screen.getRatio()));
59 config.setScreenSizeQualifier(new ScreenSizeQualifier(screen.getSize()));
60 config.setTextInputMethodQualifier(new TextInputMethodQualifier(hw.getKeyboard()));
61 config.setTouchTypeQualifier(new TouchScreenQualifier(screen.getMechanism()));
62 ScreenRound screenRound = screen.getScreenRound();
63 if (screenRound == null) {
64 // The default is not round.
65 screenRound = ScreenRound.NOTROUND;
66 }
67 config.setScreenRoundQualifier(new ScreenRoundQualifier(screenRound));
68
69 config.setKeyboardStateQualifier(new KeyboardStateQualifier(state.getKeyState()));
70 config.setNavigationStateQualifier(new NavigationStateQualifier(state.getNavState()));
71 config.setScreenOrientationQualifier(
72 new ScreenOrientationQualifier(state.getOrientation()));
73
74 config.updateScreenWidthAndHeight();
75
76 // Setup some default qualifiers
77 config.setUiModeQualifier(new UiModeQualifier(UiMode.NORMAL));
78 config.setNightModeQualifier(new NightModeQualifier(NightMode.NOTNIGHT));
79 config.setCountryCodeQualifier(new CountryCodeQualifier());
80 config.setLocaleQualifier(new LocaleQualifier());
81 config.setLayoutDirectionQualifier(new LayoutDirectionQualifier());
82 config.setNetworkCodeQualifier(new NetworkCodeQualifier());
83 config.setVersionQualifier(new VersionQualifier());
84
85 return config;
86 }
87
88 /**
89 * Returns a {@link FolderConfiguration} based on the {@link State} given by
90 * the {@link Device} and the state name.
91 *
92 * @param d
93 * The {@link Device} to base the {@link FolderConfiguration} on.
94 * @param stateName
95 * The name of the state to base the {@link FolderConfiguration}
96 * on.
97 * @return The {@link FolderConfiguration} based on the determined
98 * {@link State}. If there is no {@link State} with the given state
99 * name for the given device, null is returned.
100 */
101 @Nullable
102 public static FolderConfiguration getFolderConfig(Device d, String stateName) {
103 return getFolderConfig(d.getState(stateName));
104 }
105
106 /**
107 * Returns a {@link FolderConfiguration} based on the default {@link State}
108 * for the given {@link Device}.
109 *
110 * @param d
111 * The {@link Device} to generate the {@link FolderConfiguration}
112 * from.
113 * @return A {@link FolderConfiguration} based on the default {@link State}
114 * for the given {@link Device}
115 */
116 public static FolderConfiguration getFolderConfig(Device d) {
117 return getFolderConfig(d.getDefaultState());
118 }
119 }