blob: 5ef93193c67c7f3817843e355bd91181604ed717 [file] [log] [blame]
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -07001/*
2 * Copyright (C) 2013 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.printservice;
18
Philip P. Moltmann8141bdf2015-12-21 17:03:05 -080019import android.annotation.NonNull;
Philip P. Moltmann7e018952017-04-02 14:34:09 -070020import android.annotation.SystemApi;
Svetoslav Ganov2dcedc122017-06-07 18:50:02 -070021import android.annotation.TestApi;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070022import android.content.ComponentName;
23import android.content.Context;
24import android.content.pm.PackageManager;
25import android.content.pm.PackageManager.NameNotFoundException;
26import android.content.pm.ResolveInfo;
27import android.content.res.Resources;
28import android.content.res.TypedArray;
29import android.content.res.XmlResourceParser;
30import android.os.Parcel;
31import android.os.Parcelable;
32import android.util.AttributeSet;
33import android.util.Log;
34import android.util.Xml;
35
36import org.xmlpull.v1.XmlPullParser;
37import org.xmlpull.v1.XmlPullParserException;
38
39import java.io.IOException;
40
41/**
42 * This class describes a {@link PrintService}. A print service knows
43 * how to communicate with one or more printers over one or more protocols
44 * and exposes printers for use by the applications via the platform print
45 * APIs.
46 *
47 * @see PrintService
48 * @see android.print.PrintManager
49 *
50 * @hide
51 */
Svetoslav Ganov2dcedc122017-06-07 18:50:02 -070052@TestApi
Philip P. Moltmann7e018952017-04-02 14:34:09 -070053@SystemApi
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070054public final class PrintServiceInfo implements Parcelable {
55
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070056 private static final String LOG_TAG = PrintServiceInfo.class.getSimpleName();
57
58 private static final String TAG_PRINT_SERVICE = "print-service";
59
60 private final String mId;
61
Philip P. Moltmann66c96592016-02-24 11:32:43 -080062 private boolean mIsEnabled;
63
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070064 private final ResolveInfo mResolveInfo;
65
66 private final String mSettingsActivityName;
67
68 private final String mAddPrintersActivityName;
69
Svetoslavb4fda132013-10-25 18:57:43 -070070 private final String mAdvancedPrintOptionsActivityName;
71
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070072 /**
73 * Creates a new instance.
74 *
75 * @hide
76 */
77 public PrintServiceInfo(Parcel parcel) {
78 mId = parcel.readString();
Philip P. Moltmann66c96592016-02-24 11:32:43 -080079 mIsEnabled = parcel.readByte() != 0;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070080 mResolveInfo = parcel.readParcelable(null);
81 mSettingsActivityName = parcel.readString();
82 mAddPrintersActivityName = parcel.readString();
Svetoslavb4fda132013-10-25 18:57:43 -070083 mAdvancedPrintOptionsActivityName = parcel.readString();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070084 }
85
86 /**
87 * Creates a new instance.
88 *
89 * @param resolveInfo The service resolve info.
90 * @param settingsActivityName Optional settings activity name.
91 * @param addPrintersActivityName Optional add printers activity name.
Svetoslavb4fda132013-10-25 18:57:43 -070092 * @param advancedPrintOptionsActivityName Optional advanced print options activity.
Philip P. Moltmann7e018952017-04-02 14:34:09 -070093 *
94 * @hide
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070095 */
96 public PrintServiceInfo(ResolveInfo resolveInfo, String settingsActivityName,
Svetoslavb4fda132013-10-25 18:57:43 -070097 String addPrintersActivityName, String advancedPrintOptionsActivityName) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070098 mId = new ComponentName(resolveInfo.serviceInfo.packageName,
99 resolveInfo.serviceInfo.name).flattenToString();
100 mResolveInfo = resolveInfo;
101 mSettingsActivityName = settingsActivityName;
102 mAddPrintersActivityName = addPrintersActivityName;
Svetoslavb4fda132013-10-25 18:57:43 -0700103 mAdvancedPrintOptionsActivityName = advancedPrintOptionsActivityName;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700104 }
105
106 /**
Philip P. Moltmann8141bdf2015-12-21 17:03:05 -0800107 * Return the component name for this print service.
108 *
109 * @return The component name for this print service.
110 */
111 public @NonNull ComponentName getComponentName() {
112 return new ComponentName(mResolveInfo.serviceInfo.packageName,
113 mResolveInfo.serviceInfo.name);
114 }
115
116 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700117 * Creates a new instance.
118 *
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700119 * @param context Context for accessing resources.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700120 * @param resolveInfo The service resolve info.
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800121 * @return The created instance.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700122 *
123 * @hide
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700124 */
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700125 public static PrintServiceInfo create(Context context, ResolveInfo resolveInfo) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700126 String settingsActivityName = null;
127 String addPrintersActivityName = null;
Svetoslavb4fda132013-10-25 18:57:43 -0700128 String advancedPrintOptionsActivityName = null;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700129
130 XmlResourceParser parser = null;
131 PackageManager packageManager = context.getPackageManager();
132 parser = resolveInfo.serviceInfo.loadXmlMetaData(packageManager,
133 PrintService.SERVICE_META_DATA);
134 if (parser != null) {
135 try {
136 int type = 0;
137 while (type != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
138 type = parser.next();
139 }
140
141 String nodeName = parser.getName();
142 if (!TAG_PRINT_SERVICE.equals(nodeName)) {
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700143 Log.e(LOG_TAG, "Ignoring meta-data that does not start with "
144 + TAG_PRINT_SERVICE + " tag");
145 } else {
146 Resources resources = packageManager.getResourcesForApplication(
147 resolveInfo.serviceInfo.applicationInfo);
148 AttributeSet allAttributes = Xml.asAttributeSet(parser);
149 TypedArray attributes = resources.obtainAttributes(allAttributes,
150 com.android.internal.R.styleable.PrintService);
151
152 settingsActivityName = attributes.getString(
153 com.android.internal.R.styleable.PrintService_settingsActivity);
154
155 addPrintersActivityName = attributes.getString(
156 com.android.internal.R.styleable.PrintService_addPrintersActivity);
157
Svetoslavb4fda132013-10-25 18:57:43 -0700158 advancedPrintOptionsActivityName = attributes.getString(com.android.internal
159 .R.styleable.PrintService_advancedPrintOptionsActivity);
160
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700161 attributes.recycle();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700162 }
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700163 } catch (IOException ioe) {
164 Log.w(LOG_TAG, "Error reading meta-data:" + ioe);
165 } catch (XmlPullParserException xppe) {
166 Log.w(LOG_TAG, "Error reading meta-data:" + xppe);
167 } catch (NameNotFoundException e) {
168 Log.e(LOG_TAG, "Unable to load resources for: "
169 + resolveInfo.serviceInfo.packageName);
170 } finally {
171 if (parser != null) {
172 parser.close();
173 }
174 }
175 }
176
Svetoslavb4fda132013-10-25 18:57:43 -0700177 return new PrintServiceInfo(resolveInfo, settingsActivityName,
178 addPrintersActivityName, advancedPrintOptionsActivityName);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700179 }
180
181 /**
182 * The accessibility service id.
183 * <p>
184 * <strong>Generated by the system.</strong>
185 * </p>
186 *
187 * @return The id.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700188 *
189 * @hide
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700190 */
191 public String getId() {
192 return mId;
193 }
194
195 /**
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800196 * If the service was enabled when it was read from the system.
197 *
198 * @return The id.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700199 *
200 * @hide
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800201 */
202 public boolean isEnabled() {
203 return mIsEnabled;
204 }
205
206 /**
207 * Mark a service as enabled or not
208 *
209 * @param isEnabled If the service should be marked as enabled.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700210 *
211 * @hide
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800212 */
213 public void setIsEnabled(boolean isEnabled) {
214 mIsEnabled = isEnabled;
215 }
216
217 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700218 * The service {@link ResolveInfo}.
219 *
220 * @return The info.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700221 *
222 * @hide
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700223 */
224 public ResolveInfo getResolveInfo() {
225 return mResolveInfo;
226 }
227
228 /**
229 * The settings activity name.
230 * <p>
231 * <strong>Statically set from
232 * {@link PrintService#SERVICE_META_DATA meta-data}.</strong>
233 * </p>
234 *
235 * @return The settings activity name.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700236 *
237 * @hide
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700238 */
239 public String getSettingsActivityName() {
240 return mSettingsActivityName;
241 }
242
243 /**
244 * The add printers activity name.
245 * <p>
246 * <strong>Statically set from
247 * {@link PrintService#SERVICE_META_DATA meta-data}.</strong>
248 * </p>
249 *
250 * @return The add printers activity name.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700251 *
252 * @hide
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700253 */
254 public String getAddPrintersActivityName() {
255 return mAddPrintersActivityName;
256 }
257
258 /**
Svetoslavb4fda132013-10-25 18:57:43 -0700259 * The advanced print options activity name.
260 * <p>
261 * <strong>Statically set from
262 * {@link PrintService#SERVICE_META_DATA meta-data}.</strong>
263 * </p>
264 *
265 * @return The advanced print options activity name.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700266 *
267 * @hide
Svetoslavb4fda132013-10-25 18:57:43 -0700268 */
269 public String getAdvancedOptionsActivityName() {
270 return mAdvancedPrintOptionsActivityName;
271 }
272
273 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700274 * {@inheritDoc}
275 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800276 @Override
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700277 public int describeContents() {
278 return 0;
279 }
280
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800281 @Override
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700282 public void writeToParcel(Parcel parcel, int flagz) {
283 parcel.writeString(mId);
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800284 parcel.writeByte((byte)(mIsEnabled ? 1 : 0));
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700285 parcel.writeParcelable(mResolveInfo, 0);
286 parcel.writeString(mSettingsActivityName);
287 parcel.writeString(mAddPrintersActivityName);
Svetoslavb4fda132013-10-25 18:57:43 -0700288 parcel.writeString(mAdvancedPrintOptionsActivityName);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700289 }
290
291 @Override
292 public int hashCode() {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700293 return 31 + ((mId == null) ? 0 : mId.hashCode());
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700294 }
295
296 @Override
297 public boolean equals(Object obj) {
298 if (this == obj) {
299 return true;
300 }
301 if (obj == null) {
302 return false;
303 }
304 if (getClass() != obj.getClass()) {
305 return false;
306 }
307 PrintServiceInfo other = (PrintServiceInfo) obj;
308 if (mId == null) {
309 if (other.mId != null) {
310 return false;
311 }
312 } else if (!mId.equals(other.mId)) {
313 return false;
314 }
315 return true;
316 }
317
318 @Override
319 public String toString() {
320 StringBuilder builder = new StringBuilder();
321 builder.append("PrintServiceInfo{");
Svetoslav Ganovb6699172013-09-07 22:42:47 -0700322 builder.append("id=").append(mId);
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800323 builder.append("isEnabled=").append(mIsEnabled);
Svetoslav Ganovb6699172013-09-07 22:42:47 -0700324 builder.append(", resolveInfo=").append(mResolveInfo);
325 builder.append(", settingsActivityName=").append(mSettingsActivityName);
326 builder.append(", addPrintersActivityName=").append(mAddPrintersActivityName);
Svetoslavb4fda132013-10-25 18:57:43 -0700327 builder.append(", advancedPrintOptionsActivityName=")
328 .append(mAdvancedPrintOptionsActivityName);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700329 builder.append("}");
330 return builder.toString();
331 }
332
333 public static final Parcelable.Creator<PrintServiceInfo> CREATOR =
334 new Parcelable.Creator<PrintServiceInfo>() {
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800335 @Override
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700336 public PrintServiceInfo createFromParcel(Parcel parcel) {
337 return new PrintServiceInfo(parcel);
338 }
339
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800340 @Override
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700341 public PrintServiceInfo[] newArray(int size) {
342 return new PrintServiceInfo[size];
343 }
344 };
345}