1/*
2 * Copyright (C) 2009 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.pm;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.util.proto.ProtoOutputStream;
22
23/**
24 * Definition of a single optional hardware or software feature of an Android
25 * device.
26 * <p>
27 * This object is used to represent both features supported by a device and
28 * features requested by an app. Apps can request that certain features be
29 * available as a prerequisite to being installed through the
30 * {@code uses-feature} tag in their manifests.
31 * <p>
32 * Starting in {@link android.os.Build.VERSION_CODES#N}, features can have a
33 * version, which must always be backwards compatible. That is, a device
34 * claiming to support version 3 of a specific feature must support apps
35 * requesting version 1 of that feature.
36 */
37public class FeatureInfo implements Parcelable {
38    /**
39     * The name of this feature, for example "android.hardware.camera".  If
40     * this is null, then this is an OpenGL ES version feature as described
41     * in {@link #reqGlEsVersion}.
42     */
43    public String name;
44
45    /**
46     * If this object represents a feature supported by a device, this is the
47     * maximum version of this feature supported by the device. The device
48     * implicitly supports all older versions of this feature.
49     * <p>
50     * If this object represents a feature requested by an app, this is the
51     * minimum version of the feature required by the app.
52     * <p>
53     * When a feature version is undefined by a device, it's assumed to be
54     * version 0.
55     */
56    public int version;
57
58    /**
59     * Default value for {@link #reqGlEsVersion};
60     */
61    public static final int GL_ES_VERSION_UNDEFINED = 0;
62
63    /**
64     * The GLES version used by an application. The upper order 16 bits represent the
65     * major version and the lower order 16 bits the minor version.  Only valid
66     * if {@link #name} is null.
67     */
68    public int reqGlEsVersion;
69
70    /**
71     * Set on {@link #flags} if this feature has been required by the application.
72     */
73    public static final int FLAG_REQUIRED = 0x0001;
74
75    /**
76     * Additional flags.  May be zero or more of {@link #FLAG_REQUIRED}.
77     */
78    public int flags;
79
80    public FeatureInfo() {
81    }
82
83    public FeatureInfo(FeatureInfo orig) {
84        name = orig.name;
85        version = orig.version;
86        reqGlEsVersion = orig.reqGlEsVersion;
87        flags = orig.flags;
88    }
89
90    @Override
91    public String toString() {
92        if (name != null) {
93            return "FeatureInfo{"
94                    + Integer.toHexString(System.identityHashCode(this))
95                    + " " + name + " v=" + version + " fl=0x" + Integer.toHexString(flags) + "}";
96        } else {
97            return "FeatureInfo{"
98                    + Integer.toHexString(System.identityHashCode(this))
99                    + " glEsVers=" + getGlEsVersion()
100                    + " fl=0x" + Integer.toHexString(flags) + "}";
101        }
102    }
103
104    @Override
105    public int describeContents() {
106        return 0;
107    }
108
109    @Override
110    public void writeToParcel(Parcel dest, int parcelableFlags) {
111        dest.writeString(name);
112        dest.writeInt(version);
113        dest.writeInt(reqGlEsVersion);
114        dest.writeInt(flags);
115    }
116
117    /** @hide */
118    public void writeToProto(ProtoOutputStream proto, long fieldId) {
119        long token = proto.start(fieldId);
120        if (name != null) {
121            proto.write(FeatureInfoProto.NAME, name);
122        }
123        proto.write(FeatureInfoProto.VERSION, version);
124        proto.write(FeatureInfoProto.GLES_VERSION, getGlEsVersion());
125        proto.write(FeatureInfoProto.FLAGS, flags);
126        proto.end(token);
127    }
128
129    public static final Creator<FeatureInfo> CREATOR = new Creator<FeatureInfo>() {
130        @Override
131        public FeatureInfo createFromParcel(Parcel source) {
132            return new FeatureInfo(source);
133        }
134        @Override
135        public FeatureInfo[] newArray(int size) {
136            return new FeatureInfo[size];
137        }
138    };
139
140    private FeatureInfo(Parcel source) {
141        name = source.readString();
142        version = source.readInt();
143        reqGlEsVersion = source.readInt();
144        flags = source.readInt();
145    }
146
147    /**
148     * This method extracts the major and minor version of reqGLEsVersion attribute
149     * and returns it as a string. Say reqGlEsVersion value of 0x00010002 is returned
150     * as 1.2
151     * @return String representation of the reqGlEsVersion attribute
152     */
153    public String getGlEsVersion() {
154        int major = ((reqGlEsVersion & 0xffff0000) >> 16);
155        int minor = reqGlEsVersion & 0x0000ffff;
156        return String.valueOf(major)+"."+String.valueOf(minor);
157    }
158}
159