1 /*
2  * Copyright (C) 2021 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.app.smartspace;
17 
18 import android.annotation.IntRange;
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SuppressLint;
22 import android.annotation.SystemApi;
23 import android.content.Context;
24 import android.os.Bundle;
25 import android.os.Parcel;
26 import android.os.Parcelable;
27 
28 import java.util.Objects;
29 
30 /**
31  * A {@link SmartspaceConfig} instance is supposed to be created by a smartspace client for each
32  * UISurface. The client can specify some initialization conditions for the UISurface like its name,
33  * expected number of smartspace cards etc. The clients can also specify if they want periodic
34  * updates or their desired maximum refresh frequency.
35  *
36  * @hide
37  */
38 @SystemApi
39 public final class SmartspaceConfig implements Parcelable {
40 
41     /**
42      * The least number of smartspace targets expected to be predicted by the backend. The backend
43      * will always try to satisfy this threshold but it is not guaranteed to always meet it.
44      */
45     @IntRange(from = 0, to = 50)
46     private final int mSmartspaceTargetCount;
47 
48     /**
49      * A {@link mUiSurface} is the name of the surface which will be used to display the cards. A
50      * few examples are homescreen, lockscreen, aod etc.
51      */
52     @NonNull
53     private final String mUiSurface;
54 
55     /** Package name of the client. */
56     @NonNull
57     private String mPackageName;
58 
59     /**
60      * Send other client UI configurations in extras.
61      *
62      * This can include:
63      *
64      * - Desired maximum update frequency (For example 1 minute update frequency for AoD, 1 second
65      * update frequency for home screen etc).
66      * - Request to get periodic updates
67      * - Request to support multiple clients for the same UISurface.
68      */
69     @Nullable
70     private final Bundle mExtras;
71 
SmartspaceConfig(@onNull String uiSurface, int numPredictedTargets, @NonNull String packageName, @Nullable Bundle extras)72     private SmartspaceConfig(@NonNull String uiSurface, int numPredictedTargets,
73             @NonNull String packageName, @Nullable Bundle extras) {
74         mUiSurface = uiSurface;
75         mSmartspaceTargetCount = numPredictedTargets;
76         mPackageName = packageName;
77         mExtras = extras;
78     }
79 
SmartspaceConfig(Parcel parcel)80     private SmartspaceConfig(Parcel parcel) {
81         mUiSurface = parcel.readString();
82         mSmartspaceTargetCount = parcel.readInt();
83         mPackageName = parcel.readString();
84         mExtras = parcel.readBundle();
85     }
86 
87     /** Returns the package name of the prediction context. */
88     @NonNull
getPackageName()89     public String getPackageName() {
90         return mPackageName;
91     }
92 
93     /** Returns the number of smartspace targets requested by the user. */
94     @NonNull
getSmartspaceTargetCount()95     public int getSmartspaceTargetCount() {
96         return mSmartspaceTargetCount;
97     }
98 
99     /** Returns the UISurface requested by the client. */
100     @NonNull
getUiSurface()101     public String getUiSurface() {
102         return mUiSurface;
103     }
104 
105     @Nullable
106     @SuppressLint("NullableCollection")
getExtras()107     public Bundle getExtras() {
108         return mExtras;
109     }
110 
111     @Override
describeContents()112     public int describeContents() {
113         return 0;
114     }
115 
116     @Override
writeToParcel(@onNull Parcel dest, int flags)117     public void writeToParcel(@NonNull Parcel dest, int flags) {
118         dest.writeString(mUiSurface);
119         dest.writeInt(mSmartspaceTargetCount);
120         dest.writeString(mPackageName);
121         dest.writeBundle(mExtras);
122     }
123 
124     @Override
equals(Object o)125     public boolean equals(Object o) {
126         if (this == o) return true;
127         if (o == null || getClass() != o.getClass()) return false;
128         SmartspaceConfig that = (SmartspaceConfig) o;
129         return mSmartspaceTargetCount == that.mSmartspaceTargetCount
130                 && Objects.equals(mUiSurface, that.mUiSurface)
131                 && Objects.equals(mPackageName, that.mPackageName)
132                 && Objects.equals(mExtras, that.mExtras);
133     }
134 
135     @Override
hashCode()136     public int hashCode() {
137         return Objects.hash(mSmartspaceTargetCount, mUiSurface, mPackageName, mExtras);
138     }
139 
140     /**
141      * @see Creator
142      */
143     @NonNull
144     public static final Creator<SmartspaceConfig> CREATOR =
145             new Creator<SmartspaceConfig>() {
146                 public SmartspaceConfig createFromParcel(Parcel parcel) {
147                     return new SmartspaceConfig(parcel);
148                 }
149 
150                 public SmartspaceConfig[] newArray(int size) {
151                     return new SmartspaceConfig[size];
152                 }
153             };
154 
155     /**
156      * A builder for {@link SmartspaceConfig}.
157      *
158      * @hide
159      */
160     @SystemApi
161     public static final class Builder {
162         @NonNull
163         private int mSmartspaceTargetCount = 5; // Default count is 5
164         @NonNull
165         private final String mUiSurface;
166         @NonNull
167         private final String mPackageName;
168         @NonNull
169         private Bundle mExtras = Bundle.EMPTY;
170 
171         /**
172          * @param context   The {@link Context} which is used to fetch the package name.
173          * @param uiSurface the UI Surface name associated with this context.
174          * @hide
175          */
176         @SystemApi
Builder(@onNull Context context, @NonNull String uiSurface)177         public Builder(@NonNull Context context, @NonNull String uiSurface) {
178             mPackageName = context.getPackageName();
179             this.mUiSurface = uiSurface;
180         }
181 
182         /**
183          * Used to set the expected number of cards for this context.
184          */
185         @NonNull
setSmartspaceTargetCount( @ntRangefrom = 0, to = 50) int smartspaceTargetCount)186         public Builder setSmartspaceTargetCount(
187                 @IntRange(from = 0, to = 50) int smartspaceTargetCount) {
188             this.mSmartspaceTargetCount = smartspaceTargetCount;
189             return this;
190         }
191 
192         /**
193          * Used to send a bundle containing extras for the {@link SmartspaceConfig}.
194          */
195         @NonNull
setExtras(@uppressLintR) @onNull Bundle extras)196         public Builder setExtras(@SuppressLint("NullableCollection") @NonNull Bundle extras) {
197             this.mExtras = extras;
198             return this;
199         }
200 
201         /**
202          * Returns an instance of {@link SmartspaceConfig}.
203          */
204         @NonNull
build()205         public SmartspaceConfig build() {
206             return new SmartspaceConfig(mUiSurface, mSmartspaceTargetCount, mPackageName, mExtras);
207         }
208     }
209 }
210