blob: 57f122923c69bfa40eee47a12c82bdccac26afe8 [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 Ganov4b9a4d12013-06-11 15:20:06 -070021import android.content.ComponentName;
22import android.content.Context;
23import android.content.pm.PackageManager;
24import android.content.pm.PackageManager.NameNotFoundException;
25import android.content.pm.ResolveInfo;
26import android.content.res.Resources;
27import android.content.res.TypedArray;
28import android.content.res.XmlResourceParser;
29import android.os.Parcel;
30import android.os.Parcelable;
31import android.util.AttributeSet;
32import android.util.Log;
33import android.util.Xml;
34
35import org.xmlpull.v1.XmlPullParser;
36import org.xmlpull.v1.XmlPullParserException;
37
38import java.io.IOException;
39
40/**
41 * This class describes a {@link PrintService}. A print service knows
42 * how to communicate with one or more printers over one or more protocols
43 * and exposes printers for use by the applications via the platform print
44 * APIs.
45 *
46 * @see PrintService
47 * @see android.print.PrintManager
48 *
49 * @hide
50 */
Philip P. Moltmann7e018952017-04-02 14:34:09 -070051@SystemApi
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070052public final class PrintServiceInfo implements Parcelable {
53
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070054 private static final String LOG_TAG = PrintServiceInfo.class.getSimpleName();
55
56 private static final String TAG_PRINT_SERVICE = "print-service";
57
58 private final String mId;
59
Philip P. Moltmann66c96592016-02-24 11:32:43 -080060 private boolean mIsEnabled;
61
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070062 private final ResolveInfo mResolveInfo;
63
64 private final String mSettingsActivityName;
65
66 private final String mAddPrintersActivityName;
67
Svetoslavb4fda132013-10-25 18:57:43 -070068 private final String mAdvancedPrintOptionsActivityName;
69
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070070 /**
71 * Creates a new instance.
72 *
73 * @hide
74 */
75 public PrintServiceInfo(Parcel parcel) {
76 mId = parcel.readString();
Philip P. Moltmann66c96592016-02-24 11:32:43 -080077 mIsEnabled = parcel.readByte() != 0;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070078 mResolveInfo = parcel.readParcelable(null);
79 mSettingsActivityName = parcel.readString();
80 mAddPrintersActivityName = parcel.readString();
Svetoslavb4fda132013-10-25 18:57:43 -070081 mAdvancedPrintOptionsActivityName = parcel.readString();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070082 }
83
84 /**
85 * Creates a new instance.
86 *
87 * @param resolveInfo The service resolve info.
88 * @param settingsActivityName Optional settings activity name.
89 * @param addPrintersActivityName Optional add printers activity name.
Svetoslavb4fda132013-10-25 18:57:43 -070090 * @param advancedPrintOptionsActivityName Optional advanced print options activity.
Philip P. Moltmann7e018952017-04-02 14:34:09 -070091 *
92 * @hide
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070093 */
94 public PrintServiceInfo(ResolveInfo resolveInfo, String settingsActivityName,
Svetoslavb4fda132013-10-25 18:57:43 -070095 String addPrintersActivityName, String advancedPrintOptionsActivityName) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070096 mId = new ComponentName(resolveInfo.serviceInfo.packageName,
97 resolveInfo.serviceInfo.name).flattenToString();
98 mResolveInfo = resolveInfo;
99 mSettingsActivityName = settingsActivityName;
100 mAddPrintersActivityName = addPrintersActivityName;
Svetoslavb4fda132013-10-25 18:57:43 -0700101 mAdvancedPrintOptionsActivityName = advancedPrintOptionsActivityName;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700102 }
103
104 /**
Philip P. Moltmann8141bdf2015-12-21 17:03:05 -0800105 * Return the component name for this print service.
106 *
107 * @return The component name for this print service.
108 */
109 public @NonNull ComponentName getComponentName() {
110 return new ComponentName(mResolveInfo.serviceInfo.packageName,
111 mResolveInfo.serviceInfo.name);
112 }
113
114 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700115 * Creates a new instance.
116 *
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700117 * @param context Context for accessing resources.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700118 * @param resolveInfo The service resolve info.
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800119 * @return The created instance.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700120 *
121 * @hide
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700122 */
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700123 public static PrintServiceInfo create(Context context, ResolveInfo resolveInfo) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700124 String settingsActivityName = null;
125 String addPrintersActivityName = null;
Svetoslavb4fda132013-10-25 18:57:43 -0700126 String advancedPrintOptionsActivityName = null;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700127
128 XmlResourceParser parser = null;
129 PackageManager packageManager = context.getPackageManager();
130 parser = resolveInfo.serviceInfo.loadXmlMetaData(packageManager,
131 PrintService.SERVICE_META_DATA);
132 if (parser != null) {
133 try {
134 int type = 0;
135 while (type != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
136 type = parser.next();
137 }
138
139 String nodeName = parser.getName();
140 if (!TAG_PRINT_SERVICE.equals(nodeName)) {
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700141 Log.e(LOG_TAG, "Ignoring meta-data that does not start with "
142 + TAG_PRINT_SERVICE + " tag");
143 } else {
144 Resources resources = packageManager.getResourcesForApplication(
145 resolveInfo.serviceInfo.applicationInfo);
146 AttributeSet allAttributes = Xml.asAttributeSet(parser);
147 TypedArray attributes = resources.obtainAttributes(allAttributes,
148 com.android.internal.R.styleable.PrintService);
149
150 settingsActivityName = attributes.getString(
151 com.android.internal.R.styleable.PrintService_settingsActivity);
152
153 addPrintersActivityName = attributes.getString(
154 com.android.internal.R.styleable.PrintService_addPrintersActivity);
155
Svetoslavb4fda132013-10-25 18:57:43 -0700156 advancedPrintOptionsActivityName = attributes.getString(com.android.internal
157 .R.styleable.PrintService_advancedPrintOptionsActivity);
158
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700159 attributes.recycle();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700160 }
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700161 } catch (IOException ioe) {
162 Log.w(LOG_TAG, "Error reading meta-data:" + ioe);
163 } catch (XmlPullParserException xppe) {
164 Log.w(LOG_TAG, "Error reading meta-data:" + xppe);
165 } catch (NameNotFoundException e) {
166 Log.e(LOG_TAG, "Unable to load resources for: "
167 + resolveInfo.serviceInfo.packageName);
168 } finally {
169 if (parser != null) {
170 parser.close();
171 }
172 }
173 }
174
Svetoslavb4fda132013-10-25 18:57:43 -0700175 return new PrintServiceInfo(resolveInfo, settingsActivityName,
176 addPrintersActivityName, advancedPrintOptionsActivityName);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700177 }
178
179 /**
180 * The accessibility service id.
181 * <p>
182 * <strong>Generated by the system.</strong>
183 * </p>
184 *
185 * @return The id.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700186 *
187 * @hide
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700188 */
189 public String getId() {
190 return mId;
191 }
192
193 /**
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800194 * If the service was enabled when it was read from the system.
195 *
196 * @return The id.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700197 *
198 * @hide
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800199 */
200 public boolean isEnabled() {
201 return mIsEnabled;
202 }
203
204 /**
205 * Mark a service as enabled or not
206 *
207 * @param isEnabled If the service should be marked as enabled.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700208 *
209 * @hide
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800210 */
211 public void setIsEnabled(boolean isEnabled) {
212 mIsEnabled = isEnabled;
213 }
214
215 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700216 * The service {@link ResolveInfo}.
217 *
218 * @return The info.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700219 *
220 * @hide
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700221 */
222 public ResolveInfo getResolveInfo() {
223 return mResolveInfo;
224 }
225
226 /**
227 * The settings activity name.
228 * <p>
229 * <strong>Statically set from
230 * {@link PrintService#SERVICE_META_DATA meta-data}.</strong>
231 * </p>
232 *
233 * @return The settings activity name.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700234 *
235 * @hide
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700236 */
237 public String getSettingsActivityName() {
238 return mSettingsActivityName;
239 }
240
241 /**
242 * The add printers activity name.
243 * <p>
244 * <strong>Statically set from
245 * {@link PrintService#SERVICE_META_DATA meta-data}.</strong>
246 * </p>
247 *
248 * @return The add printers activity name.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700249 *
250 * @hide
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700251 */
252 public String getAddPrintersActivityName() {
253 return mAddPrintersActivityName;
254 }
255
256 /**
Svetoslavb4fda132013-10-25 18:57:43 -0700257 * The advanced print options activity name.
258 * <p>
259 * <strong>Statically set from
260 * {@link PrintService#SERVICE_META_DATA meta-data}.</strong>
261 * </p>
262 *
263 * @return The advanced print options activity name.
Philip P. Moltmann7e018952017-04-02 14:34:09 -0700264 *
265 * @hide
Svetoslavb4fda132013-10-25 18:57:43 -0700266 */
267 public String getAdvancedOptionsActivityName() {
268 return mAdvancedPrintOptionsActivityName;
269 }
270
271 /**
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700272 * {@inheritDoc}
273 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800274 @Override
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700275 public int describeContents() {
276 return 0;
277 }
278
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800279 @Override
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700280 public void writeToParcel(Parcel parcel, int flagz) {
281 parcel.writeString(mId);
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800282 parcel.writeByte((byte)(mIsEnabled ? 1 : 0));
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700283 parcel.writeParcelable(mResolveInfo, 0);
284 parcel.writeString(mSettingsActivityName);
285 parcel.writeString(mAddPrintersActivityName);
Svetoslavb4fda132013-10-25 18:57:43 -0700286 parcel.writeString(mAdvancedPrintOptionsActivityName);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700287 }
288
289 @Override
290 public int hashCode() {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700291 return 31 + ((mId == null) ? 0 : mId.hashCode());
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700292 }
293
294 @Override
295 public boolean equals(Object obj) {
296 if (this == obj) {
297 return true;
298 }
299 if (obj == null) {
300 return false;
301 }
302 if (getClass() != obj.getClass()) {
303 return false;
304 }
305 PrintServiceInfo other = (PrintServiceInfo) obj;
306 if (mId == null) {
307 if (other.mId != null) {
308 return false;
309 }
310 } else if (!mId.equals(other.mId)) {
311 return false;
312 }
313 return true;
314 }
315
316 @Override
317 public String toString() {
318 StringBuilder builder = new StringBuilder();
319 builder.append("PrintServiceInfo{");
Svetoslav Ganovb6699172013-09-07 22:42:47 -0700320 builder.append("id=").append(mId);
Philip P. Moltmann66c96592016-02-24 11:32:43 -0800321 builder.append("isEnabled=").append(mIsEnabled);
Svetoslav Ganovb6699172013-09-07 22:42:47 -0700322 builder.append(", resolveInfo=").append(mResolveInfo);
323 builder.append(", settingsActivityName=").append(mSettingsActivityName);
324 builder.append(", addPrintersActivityName=").append(mAddPrintersActivityName);
Svetoslavb4fda132013-10-25 18:57:43 -0700325 builder.append(", advancedPrintOptionsActivityName=")
326 .append(mAdvancedPrintOptionsActivityName);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700327 builder.append("}");
328 return builder.toString();
329 }
330
331 public static final Parcelable.Creator<PrintServiceInfo> CREATOR =
332 new Parcelable.Creator<PrintServiceInfo>() {
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800333 @Override
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700334 public PrintServiceInfo createFromParcel(Parcel parcel) {
335 return new PrintServiceInfo(parcel);
336 }
337
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800338 @Override
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700339 public PrintServiceInfo[] newArray(int size) {
340 return new PrintServiceInfo[size];
341 }
342 };
343}