NanoAppFilter.java revision 390704908004a9298944091c0f74a481fac1c4a8
1/*
2 * Copyright (C) 2016 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.hardware.location;
18
19
20import android.annotation.SystemApi;
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.util.Log;
24
25/**
26 * @hide
27 */
28@SystemApi
29public class NanoAppFilter {
30
31    private static final String TAG = "NanoAppFilter";
32
33    // The appId, can be set to APP_ID_ANY
34    private long mAppId;
35
36    // Version to filter apps
37    private int mAppVersion;
38
39    // filtering spec for version
40    private int mVersionRestrictionMask;
41
42    // If APP_ID is any, then a match is performef with the vendor mask
43    private long mAppIdVendorMask;
44
45    // Id of the context hub this instance is expected on
46    // TODO: Provide an API which will let us change this HubId.
47    private int mContextHubId = HUB_ANY;
48
49    /**
50     * Flag indicating any version. With this flag set, all versions shall match provided version.
51     */
52    public static final int FLAGS_VERSION_ANY = -1;
53    /**
54     * If this flag is set, only versions strictly greater than the version specified shall match.
55     */
56    public static final int FLAGS_VERSION_GREAT_THAN  = 2;
57    /**
58     * If this flag is set, only versions strictly less than the version specified shall match.
59     */
60    public static final int FLAGS_VERSION_LESS_THAN   = 4;
61    /**
62     * If this flag is set, only versions strictly equal to the
63     * version specified shall match.
64     */
65    public static final int FLAGS_VERSION_STRICTLY_EQUAL = 8;
66
67    /**
68     * If this flag is set, only versions strictly equal to the version specified shall match.
69     */
70    public static final int APP_ANY = -1;
71
72    /**
73     * If this flag is set, all vendors shall match.
74     */
75    public static final int VENDOR_ANY = -1;
76
77    /**
78     * If this flag is set, any hub shall match.
79     */
80    public static final int HUB_ANY = -1;
81
82    private NanoAppFilter(Parcel in) {
83        mAppId = in.readLong();
84        mAppVersion = in.readInt();
85        mVersionRestrictionMask = in.readInt();
86        mAppIdVendorMask = in.readInt();
87    }
88
89    public int describeContents() {
90        return 0;
91    }
92
93    public void writeToParcel(Parcel out, int flags) {
94
95        out.writeLong(mAppId);
96        out.writeInt(mAppVersion);
97        out.writeInt(mVersionRestrictionMask);
98        out.writeLong(mAppIdVendorMask);
99    }
100
101    /**
102     * Create a filter
103     *
104     * @param appId       application id
105     * @param appVersion  application version
106     * @param versionMask version
107     * @param vendorMask  vendor
108     */
109    public NanoAppFilter(long appId, int appVersion, int versionMask, long vendorMask) {
110        mAppId = appId;
111        mAppVersion = appVersion;
112        mVersionRestrictionMask = versionMask;
113        mAppIdVendorMask = vendorMask;
114    }
115
116    private boolean versionsMatch(int versionRestrictionMask, int expected, int actual){
117        // some refactoring of version restriction mask is needed, until then, return all
118        return true;
119    }
120    /**
121     * Test match method.
122     *
123     * @param info nano app instance info
124     *
125     * @return true if this is a match, false otherwise
126     */
127    public boolean testMatch(NanoAppInstanceInfo info) {
128        return (mContextHubId == HUB_ANY || info.getContexthubId() == mContextHubId) &&
129                (mAppId == APP_ANY || info.getAppId() == mAppId) &&
130                (versionsMatch(mVersionRestrictionMask, mAppVersion, info.getAppVersion()));
131    }
132
133    public static final Parcelable.Creator<NanoAppFilter> CREATOR
134            = new Parcelable.Creator<NanoAppFilter>() {
135        public NanoAppFilter createFromParcel(Parcel in) {
136            return new NanoAppFilter(in);
137        }
138
139        public NanoAppFilter[] newArray(int size) {
140            return new NanoAppFilter[size];
141        }
142    };
143}
144