blob: 43dd1b67de6624d68f4e19b0ad81ab788dcd142d [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
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.pm.PackageManager;
22import android.content.pm.PackageManager.NameNotFoundException;
23import android.content.pm.ResolveInfo;
24import android.content.res.Resources;
25import android.content.res.TypedArray;
26import android.content.res.XmlResourceParser;
27import android.os.Parcel;
28import android.os.Parcelable;
29import android.util.AttributeSet;
30import android.util.Log;
31import android.util.Xml;
32
33import org.xmlpull.v1.XmlPullParser;
34import org.xmlpull.v1.XmlPullParserException;
35
36import java.io.IOException;
37
38/**
39 * This class describes a {@link PrintService}. A print service knows
40 * how to communicate with one or more printers over one or more protocols
41 * and exposes printers for use by the applications via the platform print
42 * APIs.
43 *
44 * @see PrintService
45 * @see android.print.PrintManager
46 *
47 * @hide
48 */
49public final class PrintServiceInfo implements Parcelable {
50
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070051 private static final String LOG_TAG = PrintServiceInfo.class.getSimpleName();
52
53 private static final String TAG_PRINT_SERVICE = "print-service";
54
55 private final String mId;
56
57 private final ResolveInfo mResolveInfo;
58
59 private final String mSettingsActivityName;
60
61 private final String mAddPrintersActivityName;
62
63 /**
64 * Creates a new instance.
65 *
66 * @hide
67 */
68 public PrintServiceInfo(Parcel parcel) {
69 mId = parcel.readString();
70 mResolveInfo = parcel.readParcelable(null);
71 mSettingsActivityName = parcel.readString();
72 mAddPrintersActivityName = parcel.readString();
73 }
74
75 /**
76 * Creates a new instance.
77 *
78 * @param resolveInfo The service resolve info.
79 * @param settingsActivityName Optional settings activity name.
80 * @param addPrintersActivityName Optional add printers activity name.
81 */
82 public PrintServiceInfo(ResolveInfo resolveInfo, String settingsActivityName,
83 String addPrintersActivityName) {
84 mId = new ComponentName(resolveInfo.serviceInfo.packageName,
85 resolveInfo.serviceInfo.name).flattenToString();
86 mResolveInfo = resolveInfo;
87 mSettingsActivityName = settingsActivityName;
88 mAddPrintersActivityName = addPrintersActivityName;
89 }
90
91 /**
92 * Creates a new instance.
93 *
94 * @param resolveInfo The service resolve info.
95 * @param context Context for accessing resources.
96 * @throws XmlPullParserException If a XML parsing error occurs.
97 * @throws IOException If a I/O error occurs.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070098 */
99 public static PrintServiceInfo create(ResolveInfo resolveInfo, Context context) {
100 String settingsActivityName = null;
101 String addPrintersActivityName = null;
102
103 XmlResourceParser parser = null;
104 PackageManager packageManager = context.getPackageManager();
105 parser = resolveInfo.serviceInfo.loadXmlMetaData(packageManager,
106 PrintService.SERVICE_META_DATA);
107 if (parser != null) {
108 try {
109 int type = 0;
110 while (type != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
111 type = parser.next();
112 }
113
114 String nodeName = parser.getName();
115 if (!TAG_PRINT_SERVICE.equals(nodeName)) {
116 throw new XmlPullParserException(
Svetoslav Ganova0027152013-06-25 14:59:53 -0700117 "Meta-data does not start with " + TAG_PRINT_SERVICE + " tag");
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700118 }
119
120 Resources resources = packageManager.getResourcesForApplication(
121 resolveInfo.serviceInfo.applicationInfo);
122 AttributeSet allAttributes = Xml.asAttributeSet(parser);
123 TypedArray attributes = resources.obtainAttributes(allAttributes,
124 com.android.internal.R.styleable.PrintService);
125
126 settingsActivityName = attributes.getString(
127 com.android.internal.R.styleable.PrintService_settingsActivity);
128
129 addPrintersActivityName = attributes.getString(
130 com.android.internal.R.styleable.PrintService_addPrintersActivity);
131
132 attributes.recycle();
133 } catch (IOException ioe) {
134 Log.w(LOG_TAG, "Error reading meta-data:" + ioe);
135 } catch (XmlPullParserException xppe) {
136 Log.w(LOG_TAG, "Error reading meta-data:" + xppe);
137 } catch (NameNotFoundException e) {
138 Log.e(LOG_TAG, "Unable to load resources for: "
139 + resolveInfo.serviceInfo.packageName);
140 } finally {
141 if (parser != null) {
142 parser.close();
143 }
144 }
145 }
146
147 return new PrintServiceInfo(resolveInfo, settingsActivityName, addPrintersActivityName);
148 }
149
150 /**
151 * The accessibility service id.
152 * <p>
153 * <strong>Generated by the system.</strong>
154 * </p>
155 *
156 * @return The id.
157 */
158 public String getId() {
159 return mId;
160 }
161
162 /**
163 * The service {@link ResolveInfo}.
164 *
165 * @return The info.
166 */
167 public ResolveInfo getResolveInfo() {
168 return mResolveInfo;
169 }
170
171 /**
172 * The settings activity name.
173 * <p>
174 * <strong>Statically set from
175 * {@link PrintService#SERVICE_META_DATA meta-data}.</strong>
176 * </p>
177 *
178 * @return The settings activity name.
179 */
180 public String getSettingsActivityName() {
181 return mSettingsActivityName;
182 }
183
184 /**
185 * The add printers activity name.
186 * <p>
187 * <strong>Statically set from
188 * {@link PrintService#SERVICE_META_DATA meta-data}.</strong>
189 * </p>
190 *
191 * @return The add printers activity name.
192 */
193 public String getAddPrintersActivityName() {
194 return mAddPrintersActivityName;
195 }
196
197 /**
198 * {@inheritDoc}
199 */
200 public int describeContents() {
201 return 0;
202 }
203
204 public void writeToParcel(Parcel parcel, int flagz) {
205 parcel.writeString(mId);
206 parcel.writeParcelable(mResolveInfo, 0);
207 parcel.writeString(mSettingsActivityName);
208 parcel.writeString(mAddPrintersActivityName);
209 }
210
211 @Override
212 public int hashCode() {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700213 return 31 + ((mId == null) ? 0 : mId.hashCode());
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700214 }
215
216 @Override
217 public boolean equals(Object obj) {
218 if (this == obj) {
219 return true;
220 }
221 if (obj == null) {
222 return false;
223 }
224 if (getClass() != obj.getClass()) {
225 return false;
226 }
227 PrintServiceInfo other = (PrintServiceInfo) obj;
228 if (mId == null) {
229 if (other.mId != null) {
230 return false;
231 }
232 } else if (!mId.equals(other.mId)) {
233 return false;
234 }
235 return true;
236 }
237
238 @Override
239 public String toString() {
240 StringBuilder builder = new StringBuilder();
241 builder.append("PrintServiceInfo{");
242 builder.append("id:").append(mId).append(", ");
243 builder.append("resolveInfo:").append(mResolveInfo).append(", ");
Svetoslav Ganova0027152013-06-25 14:59:53 -0700244 builder.append("settingsActivityName:").append(mSettingsActivityName);
245 builder.append("addPrintersActivityName:").append(mAddPrintersActivityName);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700246 builder.append("}");
247 return builder.toString();
248 }
249
250 public static final Parcelable.Creator<PrintServiceInfo> CREATOR =
251 new Parcelable.Creator<PrintServiceInfo>() {
252 public PrintServiceInfo createFromParcel(Parcel parcel) {
253 return new PrintServiceInfo(parcel);
254 }
255
256 public PrintServiceInfo[] newArray(int size) {
257 return new PrintServiceInfo[size];
258 }
259 };
260}