• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 
17 package com.google.android.setupcompat.portal;
18 
19 import android.content.Intent;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 import androidx.annotation.DrawableRes;
23 import androidx.annotation.NonNull;
24 import androidx.annotation.StringRes;
25 import com.google.android.setupcompat.internal.Preconditions;
26 
27 /**
28  * A class that represents how a progress service to be registered to {@link
29  * com.google.android.setupcompat.portal.ISetupNotificationService}.
30  */
31 public class ProgressServiceComponent implements Parcelable {
32   private final String packageName;
33   private final String taskName;
34   private final boolean isSilent;
35   private final boolean autoRebind;
36   private final long timeoutForReRegister;
37   @StringRes private final int displayNameResId;
38   @DrawableRes private final int displayIconResId;
39   private final Intent serviceIntent;
40   private final Intent itemClickIntent;
41 
ProgressServiceComponent( String packageName, String taskName, boolean isSilent, boolean autoRebind, long timeoutForReRegister, @StringRes int displayNameResId, @DrawableRes int displayIconResId, Intent serviceIntent, Intent itemClickIntent)42   private ProgressServiceComponent(
43       String packageName,
44       String taskName,
45       boolean isSilent,
46       boolean autoRebind,
47       long timeoutForReRegister,
48       @StringRes int displayNameResId,
49       @DrawableRes int displayIconResId,
50       Intent serviceIntent,
51       Intent itemClickIntent) {
52     this.packageName = packageName;
53     this.taskName = taskName;
54     this.isSilent = isSilent;
55     this.autoRebind = autoRebind;
56     this.timeoutForReRegister = timeoutForReRegister;
57     this.displayNameResId = displayNameResId;
58     this.displayIconResId = displayIconResId;
59     this.serviceIntent = serviceIntent;
60     this.itemClickIntent = itemClickIntent;
61   }
62 
63   /** Returns a new instance of {@link Builder}. */
newBuilder()64   public static Builder newBuilder() {
65     return new ProgressServiceComponent.Builder();
66   }
67 
68   /** Returns the package name where the service exist. */
69   @NonNull
getPackageName()70   public String getPackageName() {
71     return packageName;
72   }
73 
74   /** Returns the service class name */
75   @NonNull
getTaskName()76   public String getTaskName() {
77     return taskName;
78   }
79 
80   /** Returns the whether the service is silent or not */
isSilent()81   public boolean isSilent() {
82     return isSilent;
83   }
84 
85   /** Auto rebind progress service while service connection disconnect. Default: true */
isAutoRebind()86   public boolean isAutoRebind() {
87     return autoRebind;
88   }
89 
90   /** The timeout period waiting for client register progress service again. */
getTimeoutForReRegister()91   public long getTimeoutForReRegister() {
92     return timeoutForReRegister;
93   }
94 
95   /** Returns the string resource id of display name. */
96   @StringRes
getDisplayName()97   public int getDisplayName() {
98     return displayNameResId;
99   }
100 
101   /** Returns the drawable resource id of display icon. */
102   @DrawableRes
getDisplayIcon()103   public int getDisplayIcon() {
104     return displayIconResId;
105   }
106 
107   /** Returns the Intent used to bind progress service. */
getServiceIntent()108   public Intent getServiceIntent() {
109     return serviceIntent;
110   }
111 
112   /** Returns the Intent to start the user interface while progress item click. */
getItemClickIntent()113   public Intent getItemClickIntent() {
114     return itemClickIntent;
115   }
116 
117   @Override
writeToParcel(Parcel dest, int flags)118   public void writeToParcel(Parcel dest, int flags) {
119     dest.writeString(getPackageName());
120     dest.writeString(getTaskName());
121     dest.writeInt(isSilent() ? 1 : 0);
122     dest.writeInt(getDisplayName());
123     dest.writeInt(getDisplayIcon());
124     dest.writeParcelable(getServiceIntent(), 0);
125     dest.writeParcelable(getItemClickIntent(), 0);
126     dest.writeInt(isAutoRebind() ? 1 : 0);
127     dest.writeLong(getTimeoutForReRegister());
128   }
129 
130   @Override
describeContents()131   public int describeContents() {
132     return 0;
133   }
134 
135   public static final Creator<ProgressServiceComponent> CREATOR =
136       new Creator<ProgressServiceComponent>() {
137         @Override
138         public ProgressServiceComponent createFromParcel(Parcel in) {
139           return ProgressServiceComponent.newBuilder()
140               .setPackageName(in.readString())
141               .setTaskName(in.readString())
142               .setSilentMode(in.readInt() == 1)
143               .setDisplayName(in.readInt())
144               .setDisplayIcon(in.readInt())
145               .setServiceIntent(in.readParcelable(Intent.class.getClassLoader()))
146               .setItemClickIntent(in.readParcelable(Intent.class.getClassLoader()))
147               .setAutoRebind(in.readInt() == 1)
148               .setTimeoutForReRegister(in.readLong())
149               .build();
150         }
151 
152         @Override
153         public ProgressServiceComponent[] newArray(int size) {
154           return new ProgressServiceComponent[size];
155         }
156       };
157 
158   /** Builder class for {@link ProgressServiceComponent} objects */
159   public static class Builder {
160     private String packageName;
161     private String taskName;
162     private boolean isSilent = false;
163     private boolean autoRebind = true;
164     private long timeoutForReRegister = 0L;
165     @StringRes private int displayNameResId;
166     @DrawableRes private int displayIconResId;
167     private Intent serviceIntent;
168     private Intent itemClickIntent;
169 
170     /** Sets the packages name which is the service exists */
setPackageName(@onNull String packageName)171     public Builder setPackageName(@NonNull String packageName) {
172       this.packageName = packageName;
173       return this;
174     }
175 
176     /** Sets a name to identify what task this progress is. */
setTaskName(@onNull String taskName)177     public Builder setTaskName(@NonNull String taskName) {
178       this.taskName = taskName;
179       return this;
180     }
181 
182     /** Sets the service as silent mode, it executes without UI on PortalActivity. */
setSilentMode(boolean isSilent)183     public Builder setSilentMode(boolean isSilent) {
184       this.isSilent = isSilent;
185       return this;
186     }
187 
188     /** Sets the service need auto rebind or not when service connection disconnected. */
setAutoRebind(boolean autoRebind)189     public Builder setAutoRebind(boolean autoRebind) {
190       this.autoRebind = autoRebind;
191       return this;
192     }
193 
194     /**
195      * Sets the timeout period waiting for the client register again, only works when auto-rebind
196      * disabled. When 0 is set, will read default configuration from SUW.
197      */
setTimeoutForReRegister(long timeoutForReRegister)198     public Builder setTimeoutForReRegister(long timeoutForReRegister) {
199       this.timeoutForReRegister = timeoutForReRegister;
200       return this;
201     }
202 
203     /** Sets the name which is displayed on PortalActivity */
setDisplayName(@tringRes int displayNameResId)204     public Builder setDisplayName(@StringRes int displayNameResId) {
205       this.displayNameResId = displayNameResId;
206       return this;
207     }
208 
209     /** Sets the icon which is display on PortalActivity */
setDisplayIcon(@rawableRes int displayIconResId)210     public Builder setDisplayIcon(@DrawableRes int displayIconResId) {
211       this.displayIconResId = displayIconResId;
212       return this;
213     }
214 
setServiceIntent(Intent serviceIntent)215     public Builder setServiceIntent(Intent serviceIntent) {
216       this.serviceIntent = serviceIntent;
217       return this;
218     }
219 
setItemClickIntent(Intent itemClickIntent)220     public Builder setItemClickIntent(Intent itemClickIntent) {
221       this.itemClickIntent = itemClickIntent;
222       return this;
223     }
224 
build()225     public ProgressServiceComponent build() {
226       Preconditions.checkNotNull(packageName, "packageName cannot be null.");
227       Preconditions.checkNotNull(taskName, "serviceClass cannot be null.");
228       Preconditions.checkNotNull(serviceIntent, "Service intent cannot be null.");
229       Preconditions.checkNotNull(itemClickIntent, "Item click intent cannot be null");
230       if (!isSilent) {
231         Preconditions.checkArgument(
232             displayNameResId != 0, "Invalidate resource id of display name");
233         Preconditions.checkArgument(
234             displayIconResId != 0, "Invalidate resource id of display icon");
235       }
236       return new ProgressServiceComponent(
237           packageName,
238           taskName,
239           isSilent,
240           autoRebind,
241           timeoutForReRegister,
242           displayNameResId,
243           displayIconResId,
244           serviceIntent,
245           itemClickIntent);
246     }
247 
Builder()248     private Builder() {}
249   }
250 }
251