CompatibilityInfo.java revision 5c1e00b14d2ef10ec76abf3e951fa8003a67f558
1/*
2 * Copyright (C) 2006 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
17package android.content.res;
18
19import android.content.pm.ApplicationInfo;
20import android.util.DisplayMetrics;
21import android.view.Gravity;
22
23/**
24 * CompatibilityInfo class keeps the information about compatibility mode that the application is
25 * running under.
26 *
27 *  {@hide}
28 */
29public class CompatibilityInfo {
30    /** default compatibility info object for compatible applications */
31    public static final CompatibilityInfo DEFAULT_COMPATIBILITY_INFO = new CompatibilityInfo();
32
33    /**
34     * The default width of the screen in portrait mode.
35     */
36    public static final int DEFAULT_PORTRAIT_WIDTH = 320;
37
38    /**
39     * The default height of the screen in portrait mode.
40     */
41    public static final int DEFAULT_PORTRAIT_HEIGHT = 480;
42
43    /**
44     * Application's scale.
45     */
46    public final float mApplicationScale;
47
48    /**
49     * Application's inverted scale.
50     */
51    public final float mApplicationInvertedScale;
52
53    /**
54     * A boolean flag to indicates that the application can expand over the original size.
55     * The flag is set to true if
56     * 1) Application declares its expandable in manifest file using <expandable /> or
57     * 2) The screen size is same as (320 x 480) * density.
58     */
59    public boolean mExpandable;
60
61    /**
62     * A expandable flag in the configuration.
63     */
64    public final boolean mConfiguredExpandable;
65
66    /**
67     * A boolean flag to tell if the application needs scaling (when mApplicationScale != 1.0f)
68     */
69    public final boolean mScalingRequired;
70
71    public CompatibilityInfo(ApplicationInfo appInfo) {
72        mExpandable = mConfiguredExpandable =
73            (appInfo.flags & ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS) != 0;
74
75        float packageDensityScale = -1.0f;
76        if (appInfo.supportsDensities != null) {
77            int minDiff = Integer.MAX_VALUE;
78            for (int density : appInfo.supportsDensities) {
79                if (density == ApplicationInfo.ANY_DENSITY) {
80                    packageDensityScale = 1.0f;
81                    break;
82                }
83                int tmpDiff = Math.abs(DisplayMetrics.DEVICE_DENSITY - density);
84                if (tmpDiff == 0) {
85                    packageDensityScale = 1.0f;
86                    break;
87                }
88                // prefer higher density (appScale>1.0), unless that's only option.
89                if (tmpDiff < minDiff && packageDensityScale < 1.0f) {
90                    packageDensityScale = DisplayMetrics.DEVICE_DENSITY / (float) density;
91                    minDiff = tmpDiff;
92                }
93            }
94        }
95        if (packageDensityScale > 0.0f) {
96            mApplicationScale = packageDensityScale;
97        } else {
98            mApplicationScale = DisplayMetrics.DEVICE_DENSITY / (float) DisplayMetrics.DEFAULT_DENSITY;
99        }
100        mApplicationInvertedScale = 1.0f / mApplicationScale;
101        mScalingRequired = mApplicationScale != 1.0f;
102    }
103
104    private CompatibilityInfo() {
105        mApplicationScale = mApplicationInvertedScale = 1.0f;
106        mExpandable = mConfiguredExpandable = true;
107        mScalingRequired = false;
108    }
109
110    @Override
111    public String toString() {
112        return "CompatibilityInfo{scale=" + mApplicationScale +
113                ", expandable=" + mExpandable + "}";
114    }
115}
116