| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.printservice; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.SystemApi; |
| 22 |
import android.content.ComponentName; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.pm.PackageManager; |
| 25 |
import android.content.pm.PackageManager.NameNotFoundException; |
| 26 |
import android.content.pm.ResolveInfo; |
| 27 |
import android.content.res.Resources; |
| 28 |
import android.content.res.TypedArray; |
| 29 |
import android.content.res.XmlResourceParser; |
| 30 |
import android.os.Parcel; |
| 31 |
import android.os.Parcelable; |
| 32 |
import android.util.AttributeSet; |
| 33 |
import android.util.Log; |
| 34 |
import android.util.Xml; |
| 35 |
|
| 36 |
import org.xmlpull.v1.XmlPullParser; |
| 37 |
import org.xmlpull.v1.XmlPullParserException; |
| 38 |
|
| 39 |
import java.io.IOException; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
@SystemApi |
| 53 |
public final class PrintServiceInfo implements Parcelable { |
| 54 |
|
| 55 |
private static final String LOG_TAG = PrintServiceInfo.class.getSimpleName(); |
| 56 |
|
| 57 |
private static final String TAG_PRINT_SERVICE = "print-service"; |
| 58 |
|
| 59 |
private final String mId; |
| 60 |
|
| 61 |
private boolean mIsEnabled; |
| 62 |
|
| 63 |
private final ResolveInfo mResolveInfo; |
| 64 |
|
| 65 |
private final String mSettingsActivityName; |
| 66 |
|
| 67 |
private final String mAddPrintersActivityName; |
| 68 |
|
| 69 |
private final String mAdvancedPrintOptionsActivityName; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public PrintServiceInfo(Parcel parcel) { |
| 77 |
mId = parcel.readString(); |
| 78 |
mIsEnabled = parcel.readByte() != 0; |
| 79 |
mResolveInfo = parcel.readParcelable(null, android.content.pm.ResolveInfo.class); |
| 80 |
mSettingsActivityName = parcel.readString(); |
| 81 |
mAddPrintersActivityName = parcel.readString(); |
| 82 |
mAdvancedPrintOptionsActivityName = parcel.readString(); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public PrintServiceInfo(ResolveInfo resolveInfo, String settingsActivityName, |
| 96 |
String addPrintersActivityName, String advancedPrintOptionsActivityName) { |
| 97 |
mId = new ComponentName(resolveInfo.serviceInfo.packageName, |
| 98 |
resolveInfo.serviceInfo.name).flattenToString(); |
| 99 |
mResolveInfo = resolveInfo; |
| 100 |
mSettingsActivityName = settingsActivityName; |
| 101 |
mAddPrintersActivityName = addPrintersActivityName; |
| 102 |
mAdvancedPrintOptionsActivityName = advancedPrintOptionsActivityName; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public @NonNull ComponentName getComponentName() { |
| 111 |
return new ComponentName(mResolveInfo.serviceInfo.packageName, |
| 112 |
mResolveInfo.serviceInfo.name); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public static PrintServiceInfo create(Context context, ResolveInfo resolveInfo) { |
| 125 |
String settingsActivityName = null; |
| 126 |
String addPrintersActivityName = null; |
| 127 |
String advancedPrintOptionsActivityName = null; |
| 128 |
|
| 129 |
XmlResourceParser parser = null; |
| 130 |
PackageManager packageManager = context.getPackageManager(); |
| 131 |
parser = resolveInfo.serviceInfo.loadXmlMetaData(packageManager, |
| 132 |
PrintService.SERVICE_META_DATA); |
| 133 |
if (parser != null) { |
| 134 |
try { |
| 135 |
int type = 0; |
| 136 |
while (type != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) { |
| 137 |
type = parser.next(); |
| 138 |
} |
| 139 |
|
| 140 |
String nodeName = parser.getName(); |
| 141 |
if (!TAG_PRINT_SERVICE.equals(nodeName)) { |
| 142 |
Log.e(LOG_TAG, "Ignoring meta-data that does not start with " |
| 143 |
+ TAG_PRINT_SERVICE + " tag"); |
| 144 |
} else { |
| 145 |
Resources resources = packageManager.getResourcesForApplication( |
| 146 |
resolveInfo.serviceInfo.applicationInfo); |
| 147 |
AttributeSet allAttributes = Xml.asAttributeSet(parser); |
| 148 |
TypedArray attributes = resources.obtainAttributes(allAttributes, |
| 149 |
com.android.internal.R.styleable.PrintService); |
| 150 |
|
| 151 |
settingsActivityName = attributes.getString( |
| 152 |
com.android.internal.R.styleable.PrintService_settingsActivity); |
| 153 |
|
| 154 |
addPrintersActivityName = attributes.getString( |
| 155 |
com.android.internal.R.styleable.PrintService_addPrintersActivity); |
| 156 |
|
| 157 |
advancedPrintOptionsActivityName = attributes.getString(com.android.internal |
| 158 |
.R.styleable.PrintService_advancedPrintOptionsActivity); |
| 159 |
|
| 160 |
attributes.recycle(); |
| 161 |
} |
| 162 |
} catch (IOException ioe) { |
| 163 |
Log.w(LOG_TAG, "Error reading meta-data:" + ioe); |
| 164 |
} catch (XmlPullParserException xppe) { |
| 165 |
Log.w(LOG_TAG, "Error reading meta-data:" + xppe); |
| 166 |
} catch (NameNotFoundException e) { |
| 167 |
Log.e(LOG_TAG, "Unable to load resources for: " |
| 168 |
+ resolveInfo.serviceInfo.packageName); |
| 169 |
} finally { |
| 170 |
if (parser != null) { |
| 171 |
parser.close(); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return new PrintServiceInfo(resolveInfo, settingsActivityName, |
| 177 |
addPrintersActivityName, advancedPrintOptionsActivityName); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public String getId() { |
| 191 |
return mId; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public boolean isEnabled() { |
| 202 |
return mIsEnabled; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public void setIsEnabled(boolean isEnabled) { |
| 213 |
mIsEnabled = isEnabled; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public ResolveInfo getResolveInfo() { |
| 224 |
return mResolveInfo; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public String getSettingsActivityName() { |
| 239 |
return mSettingsActivityName; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public String getAddPrintersActivityName() { |
| 254 |
return mAddPrintersActivityName; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public String getAdvancedOptionsActivityName() { |
| 269 |
return mAdvancedPrintOptionsActivityName; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
@Override |
| 276 |
public int describeContents() { |
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
@Override |
| 281 |
public void writeToParcel(Parcel parcel, int flagz) { |
| 282 |
parcel.writeString(mId); |
| 283 |
parcel.writeByte((byte)(mIsEnabled ? 1 : 0)); |
| 284 |
parcel.writeParcelable(mResolveInfo, 0); |
| 285 |
parcel.writeString(mSettingsActivityName); |
| 286 |
parcel.writeString(mAddPrintersActivityName); |
| 287 |
parcel.writeString(mAdvancedPrintOptionsActivityName); |
| 288 |
} |
| 289 |
|
| 290 |
@Override |
| 291 |
public int hashCode() { |
| 292 |
return 31 + ((mId == null) ? 0 : mId.hashCode()); |
| 293 |
} |
| 294 |
|
| 295 |
@Override |
| 296 |
public boolean equals(@Nullable Object obj) { |
| 297 |
if (this == obj) { |
| 298 |
return true; |
| 299 |
} |
| 300 |
if (obj == null) { |
| 301 |
return false; |
| 302 |
} |
| 303 |
if (getClass() != obj.getClass()) { |
| 304 |
return false; |
| 305 |
} |
| 306 |
PrintServiceInfo other = (PrintServiceInfo) obj; |
| 307 |
if (mId == null) { |
| 308 |
if (other.mId != null) { |
| 309 |
return false; |
| 310 |
} |
| 311 |
} else if (!mId.equals(other.mId)) { |
| 312 |
return false; |
| 313 |
} |
| 314 |
return true; |
| 315 |
} |
| 316 |
|
| 317 |
@NonNull |
| 318 |
@Override |
| 319 |
public String toString() { |
| 320 |
StringBuilder builder = new StringBuilder(); |
| 321 |
builder.append("PrintServiceInfo{"); |
| 322 |
builder.append("id=").append(mId); |
| 323 |
builder.append("isEnabled=").append(mIsEnabled); |
| 324 |
builder.append(", resolveInfo=").append(mResolveInfo); |
| 325 |
builder.append(", settingsActivityName=").append(mSettingsActivityName); |
| 326 |
builder.append(", addPrintersActivityName=").append(mAddPrintersActivityName); |
| 327 |
builder.append(", advancedPrintOptionsActivityName=") |
| 328 |
.append(mAdvancedPrintOptionsActivityName); |
| 329 |
builder.append("}"); |
| 330 |
return builder.toString(); |
| 331 |
} |
| 332 |
|
| 333 |
public static final @android.annotation.NonNull Parcelable.Creator<PrintServiceInfo> CREATOR = |
| 334 |
new Parcelable.Creator<PrintServiceInfo>() { |
| 335 |
@Override |
| 336 |
public PrintServiceInfo createFromParcel(Parcel parcel) { |
| 337 |
return new PrintServiceInfo(parcel); |
| 338 |
} |
| 339 |
|
| 340 |
@Override |
| 341 |
public PrintServiceInfo[] newArray(int size) { |
| 342 |
return new PrintServiceInfo[size]; |
| 343 |
} |
| 344 |
}; |
| 345 |
} |