1 /*
2  * Copyright 2017 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 package android.hardware.location;
17 
18 import android.annotation.NonNull;
19 import android.annotation.SystemApi;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.List;
27 
28 /**
29  * A class describing the nanoapp state information resulting from a query to a Context Hub
30  * through {@link ContextHubManager.queryNanoApps(ContextHubInfo)}. It contains metadata about
31  * the nanoapp running on a Context Hub.
32  *
33  * See "struct chreNanoappInfo" in the CHRE API (system/chre/chre_api) for additional details.
34  *
35  * @hide
36  */
37 @SystemApi
38 public final class NanoAppState implements Parcelable {
39     private long mNanoAppId;
40     private int mNanoAppVersion;
41     private boolean mIsEnabled;
42     private List<String> mNanoAppPermissions = new ArrayList<String>();
43     private List<NanoAppRpcService> mNanoAppRpcServiceList =
44                 new ArrayList<NanoAppRpcService>();
45 
46     /**
47      * @param nanoAppId The unique ID of this nanoapp, see {#getNanoAppId()}.
48      * @param appVersion The software version of this nanoapp, see {#getNanoAppVersion()}.
49      * @param enabled True if the nanoapp is enabled and running on the Context Hub.
50      */
NanoAppState(long nanoAppId, int appVersion, boolean enabled)51     public NanoAppState(long nanoAppId, int appVersion, boolean enabled) {
52         mNanoAppId = nanoAppId;
53         mNanoAppVersion = appVersion;
54         mIsEnabled = enabled;
55     }
56 
57     /**
58      * @param nanoAppId The unique ID of this nanoapp, see {#getNanoAppId()}.
59      * @param appVersion The software version of this nanoapp, see {#getNanoAppVersion()}.
60      * @param enabled True if the nanoapp is enabled and running on the Context Hub.
61      * @param nanoAppPermissions The list of permissions required to communicate with this
62      *   nanoapp.
63      */
NanoAppState(long nanoAppId, int appVersion, boolean enabled, @NonNull List<String> nanoAppPermissions)64     public NanoAppState(long nanoAppId, int appVersion, boolean enabled,
65                         @NonNull List<String> nanoAppPermissions) {
66         mNanoAppId = nanoAppId;
67         mNanoAppVersion = appVersion;
68         mIsEnabled = enabled;
69         mNanoAppPermissions = Collections.unmodifiableList(nanoAppPermissions);
70     }
71 
72     /**
73      * @param nanoAppId The unique ID of this nanoapp, see {#getNanoAppId()}.
74      * @param appVersion The software version of this nanoapp, see {#getNanoAppVersion()}.
75      * @param enabled True if the nanoapp is enabled and running on the Context Hub.
76      * @param nanoAppPermissions The list of permissions required to communicate with this
77      *   nanoapp.
78      * @param nanoAppRpcServiceList The list of RPC services published by this nanoapp, see
79      *   {@link NanoAppRpcService} for additional details.
80      */
NanoAppState(long nanoAppId, int appVersion, boolean enabled, @NonNull List<String> nanoAppPermissions, @NonNull List<NanoAppRpcService> nanoAppRpcServiceList)81     public NanoAppState(long nanoAppId, int appVersion, boolean enabled,
82                         @NonNull List<String> nanoAppPermissions,
83                         @NonNull List<NanoAppRpcService> nanoAppRpcServiceList) {
84         mNanoAppId = nanoAppId;
85         mNanoAppVersion = appVersion;
86         mIsEnabled = enabled;
87         mNanoAppPermissions = Collections.unmodifiableList(nanoAppPermissions);
88         mNanoAppRpcServiceList = Collections.unmodifiableList(nanoAppRpcServiceList);
89     }
90 
91     /**
92      * @return the unique ID of this nanoapp, which must never change once released on Android.
93      */
getNanoAppId()94     public long getNanoAppId() {
95         return mNanoAppId;
96     }
97 
98     /**
99      * The software version of this service, which follows the sematic
100      * versioning scheme (see semver.org). It follows the format
101      * major.minor.patch, where major and minor versions take up one byte
102      * each, and the patch version takes up the final 2 (lower) bytes.
103      * I.e. the version is encoded as 0xMMmmpppp, where MM, mm, pppp are
104      * the major, minor, patch versions, respectively.
105      *
106      * @return the app version
107      */
getNanoAppVersion()108     public long getNanoAppVersion() {
109         return mNanoAppVersion;
110     }
111 
112     /**
113      * @return {@code true} if the app is enabled at the Context Hub, {@code false} otherwise
114      */
isEnabled()115     public boolean isEnabled() {
116         return mIsEnabled;
117     }
118 
119     /**
120      * @return A read-only list of Android permissions that are all required to communicate with
121      *   this nanoapp.
122      */
getNanoAppPermissions()123     public @NonNull List<String> getNanoAppPermissions() {
124         return mNanoAppPermissions;
125     }
126 
127     /**
128      * @return A read-only list of RPC services supported by this nanoapp.
129      */
getRpcServices()130     public @NonNull List<NanoAppRpcService> getRpcServices() {
131         return mNanoAppRpcServiceList;
132     }
133 
NanoAppState(Parcel in)134     private NanoAppState(Parcel in) {
135         mNanoAppId = in.readLong();
136         mNanoAppVersion = in.readInt();
137         mIsEnabled = (in.readInt() == 1);
138         mNanoAppPermissions = new ArrayList<String>();
139         in.readStringList(mNanoAppPermissions);
140         mNanoAppRpcServiceList = Collections.unmodifiableList(
141                     Arrays.asList(in.readParcelableArray(
142                     NanoAppRpcService.class.getClassLoader(), NanoAppRpcService.class)));
143     }
144 
145     @Override
describeContents()146     public int describeContents() {
147         return 0;
148     }
149 
150     @Override
writeToParcel(Parcel out, int flags)151     public void writeToParcel(Parcel out, int flags) {
152         out.writeLong(mNanoAppId);
153         out.writeInt(mNanoAppVersion);
154         out.writeInt(mIsEnabled ? 1 : 0);
155         out.writeStringList(mNanoAppPermissions);
156         out.writeParcelableArray(mNanoAppRpcServiceList.toArray(new NanoAppRpcService[0]), 0);
157     }
158 
159     public static final @android.annotation.NonNull Creator<NanoAppState> CREATOR =
160             new Creator<NanoAppState>() {
161                 @Override
162                 public NanoAppState createFromParcel(Parcel in) {
163                     return new NanoAppState(in);
164                 }
165 
166                 @Override
167                 public NanoAppState[] newArray(int size) {
168                     return new NanoAppState[size];
169                 }
170             };
171 }
172