1. Added notifications for a queued print job, for a started print job,
for ongoing canceling a print job, and for a failed print job. The
notifications for queued and started state have a cancel action. The
notification for failed print job has a cancel and a restart action.
2. Propagating failure message from the print service to the notifications.
3. PrintJobConfigActivity was not setting the initial value for the
print job copies and was not updating the UI immediately after creation.
4. Refactored PrintJobConfigActivity to avoid using the hack to avoid
reaction for item selection change in a spinner for an event that
happened before the callback was registered.
5. Removed the label attribute from PrinterInfo and now PrinterId is
composed of the printer name and the service component name. This
is nice since for restarting print jobs we do not need to store
information about the printer except the printer id which is
already part of the PrintJobInfo's data. Also the printer name
is not expected to change anyway.
6. Allowing cancellation of a queued print job. Also no print job is
cancelled without asking the managing print service to do that.
Before we were immediately canceling print jobs in queued state
but it was possible for a buggy print service to not set the
print job state to started before starting to do expensive work
that will not be canceled.
7. PrintServiceInfo was throwing an exception the the meta-data
XML for the print service was not well-formed which would crash
the system process. Now we just ignore not well-formed meta-data.
8. Removed unused permissions from the PrintSpooler's manifest.
Change-Id: Iba2dd14b487f56e137b90d1da17c3033422ab5e6
534 lines
18 KiB
Java
534 lines
18 KiB
Java
/*
|
|
* Copyright (C) 2013 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package com.android.server.print;
|
|
|
|
import android.content.ComponentName;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.ServiceConnection;
|
|
import android.os.Binder;
|
|
import android.os.Build;
|
|
import android.os.Handler;
|
|
import android.os.IBinder;
|
|
import android.os.Looper;
|
|
import android.os.Message;
|
|
import android.os.ParcelFileDescriptor;
|
|
import android.os.RemoteException;
|
|
import android.os.UserHandle;
|
|
import android.os.IBinder.DeathRecipient;
|
|
import android.print.IPrinterDiscoveryObserver;
|
|
import android.print.PrintJobInfo;
|
|
import android.print.PrintManager;
|
|
import android.print.PrinterId;
|
|
import android.print.PrinterInfo;
|
|
import android.printservice.IPrintService;
|
|
import android.printservice.IPrintServiceClient;
|
|
import android.util.Slog;
|
|
|
|
import java.lang.ref.WeakReference;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* This class represents a remote print service. It abstracts away the binding
|
|
* and unbinding from the remote implementation. Clients can call methods of
|
|
* this class without worrying about when and how to bind/unbind.
|
|
*/
|
|
final class RemotePrintService implements DeathRecipient {
|
|
|
|
private static final String LOG_TAG = "RemotePrintService";
|
|
|
|
private static final boolean DEBUG = true && Build.IS_DEBUGGABLE;
|
|
|
|
private final Context mContext;
|
|
|
|
private final ComponentName mComponentName;
|
|
|
|
private final Intent mIntent;
|
|
|
|
private final RemotePrintSpooler mSpooler;
|
|
|
|
private final int mUserId;
|
|
|
|
private final List<Runnable> mPendingCommands = new ArrayList<Runnable>();
|
|
|
|
private final ServiceConnection mServiceConnection = new RemoteServiceConneciton();
|
|
|
|
private final RemotePrintServiceClient mPrintServiceClient;
|
|
|
|
private final Handler mHandler;
|
|
|
|
private IPrintService mPrintService;
|
|
|
|
private boolean mBinding;
|
|
|
|
private boolean mDestroyed;
|
|
|
|
public RemotePrintService(Context context, ComponentName componentName, int userId,
|
|
RemotePrintSpooler spooler) {
|
|
mContext = context;
|
|
mComponentName = componentName;
|
|
mIntent = new Intent().setComponent(mComponentName);
|
|
mUserId = userId;
|
|
mSpooler = spooler;
|
|
mHandler = new MyHandler(context.getMainLooper());
|
|
mPrintServiceClient = new RemotePrintServiceClient(this);
|
|
}
|
|
|
|
public void destroy() {
|
|
mHandler.sendEmptyMessage(MyHandler.MSG_DESTROY);
|
|
}
|
|
|
|
private void handleDestroy() {
|
|
throwIfDestroyed();
|
|
ensureUnbound();
|
|
mDestroyed = true;
|
|
}
|
|
|
|
public void onAllPrintJobsHandled() {
|
|
mHandler.sendEmptyMessage(MyHandler.MSG_ON_ALL_PRINT_JOBS_HANDLED);
|
|
}
|
|
|
|
@Override
|
|
public void binderDied() {
|
|
mHandler.sendEmptyMessage(MyHandler.MSG_BINDER_DIED);
|
|
}
|
|
|
|
private void handleBinderDied() {
|
|
ensureUnbound();
|
|
}
|
|
|
|
private void handleOnAllPrintJobsHandled() {
|
|
throwIfDestroyed();
|
|
if (isBound()) {
|
|
if (DEBUG) {
|
|
Slog.i(LOG_TAG, "[user: " + mUserId + "] handleOnAllPrintJobsHandled()");
|
|
}
|
|
// If bound and all the work is completed, then unbind.
|
|
ensureUnbound();
|
|
}
|
|
}
|
|
|
|
public void onRequestCancelPrintJob(PrintJobInfo printJob) {
|
|
mHandler.obtainMessage(MyHandler.MSG_ON_REQUEST_CANCEL_PRINT_JOB,
|
|
printJob).sendToTarget();
|
|
}
|
|
|
|
private void handleOnRequestCancelPrintJob(final PrintJobInfo printJob) {
|
|
throwIfDestroyed();
|
|
// If we are not bound, then we have no print jobs to handle
|
|
// which means that there are no print jobs to be cancelled.
|
|
if (isBound()) {
|
|
if (DEBUG) {
|
|
Slog.i(LOG_TAG, "[user: " + mUserId + "] handleOnRequestCancelPrintJob()");
|
|
}
|
|
try {
|
|
mPrintService.onRequestCancelPrintJob(printJob);
|
|
} catch (RemoteException re) {
|
|
Slog.e(LOG_TAG, "Error canceling pring job.", re);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void onPrintJobQueued(PrintJobInfo printJob) {
|
|
mHandler.obtainMessage(MyHandler.MSG_ON_PRINT_JOB_QUEUED,
|
|
printJob).sendToTarget();
|
|
}
|
|
|
|
private void handleOnPrintJobQueued(final PrintJobInfo printJob) {
|
|
throwIfDestroyed();
|
|
if (!isBound()) {
|
|
ensureBound();
|
|
mPendingCommands.add(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
handleOnPrintJobQueued(printJob);
|
|
}
|
|
});
|
|
} else {
|
|
if (DEBUG) {
|
|
Slog.i(LOG_TAG, "[user: " + mUserId + "] handleOnPrintJobQueued()");
|
|
}
|
|
try {
|
|
mPrintService.onPrintJobQueued(printJob);
|
|
} catch (RemoteException re) {
|
|
Slog.e(LOG_TAG, "Error announcing queued pring job.", re);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void onStartPrinterDiscovery(IPrinterDiscoveryObserver observer) {
|
|
mHandler.obtainMessage(MyHandler.MSG_ON_START_PRINTER_DISCOVERY, observer).sendToTarget();
|
|
}
|
|
|
|
private void handleOnStartPrinterDiscovery(final IPrinterDiscoveryObserver observer) {
|
|
throwIfDestroyed();
|
|
if (!isBound()) {
|
|
ensureBound();
|
|
mPendingCommands.add(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
handleOnStartPrinterDiscovery(observer);
|
|
}
|
|
});
|
|
} else {
|
|
if (DEBUG) {
|
|
Slog.i(LOG_TAG, "[user: " + mUserId + "] onStartPrinterDiscovery()");
|
|
}
|
|
try {
|
|
mPrintService.onStartPrinterDiscovery(observer);
|
|
} catch (RemoteException re) {
|
|
Slog.e(LOG_TAG, "Error announcing start printer dicovery.", re);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void onStopPrinterDiscovery() {
|
|
mHandler.sendEmptyMessage(MyHandler.MSG_ON_STOP_PRINTER_DISCOVERY);
|
|
}
|
|
|
|
private void handleStopPrinterDiscovery() {
|
|
throwIfDestroyed();
|
|
if (!isBound()) {
|
|
ensureBound();
|
|
mPendingCommands.add(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
handleStopPrinterDiscovery();
|
|
}
|
|
});
|
|
} else {
|
|
if (DEBUG) {
|
|
Slog.i(LOG_TAG, "[user: " + mUserId + "] onStopPrinterDiscovery()");
|
|
}
|
|
try {
|
|
mPrintService.onStopPrinterDiscovery();
|
|
} catch (RemoteException re) {
|
|
Slog.e(LOG_TAG, "Error announcing stop printer dicovery.", re);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void onRequestUpdatePrinters(List<PrinterId> printerIds) {
|
|
mHandler.obtainMessage(MyHandler.MSG_ON_REQUEST_UPDATE_PRINTERS,
|
|
printerIds).sendToTarget();
|
|
}
|
|
|
|
private void handleReqeustUpdatePritners(final List<PrinterId> printerIds) {
|
|
throwIfDestroyed();
|
|
if (!isBound()) {
|
|
ensureBound();
|
|
mPendingCommands.add(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
handleReqeustUpdatePritners(printerIds);
|
|
}
|
|
});
|
|
} else {
|
|
if (DEBUG) {
|
|
Slog.i(LOG_TAG, "[user: " + mUserId + "] handleReqeustUpdatePritners()");
|
|
}
|
|
try {
|
|
mPrintService.onRequestUpdatePrinters(printerIds);
|
|
} catch (RemoteException re) {
|
|
Slog.e(LOG_TAG, "Error requesting to update printers.", re);
|
|
}
|
|
}
|
|
}
|
|
|
|
private boolean isBound() {
|
|
return mPrintService != null;
|
|
}
|
|
|
|
private void ensureBound() {
|
|
if (isBound() || mBinding) {
|
|
return;
|
|
}
|
|
if (DEBUG) {
|
|
Slog.i(LOG_TAG, "[user: " + mUserId + "] ensureBound()");
|
|
}
|
|
mBinding = true;
|
|
mContext.bindServiceAsUser(mIntent, mServiceConnection,
|
|
Context.BIND_AUTO_CREATE, new UserHandle(mUserId));
|
|
}
|
|
|
|
private void ensureUnbound() {
|
|
if (!isBound() && !mBinding) {
|
|
return;
|
|
}
|
|
if (DEBUG) {
|
|
Slog.i(LOG_TAG, "[user: " + mUserId + "] ensureUnbound()");
|
|
}
|
|
mBinding = false;
|
|
mPendingCommands.clear();
|
|
if (isBound()) {
|
|
try {
|
|
mPrintService.setClient(null);
|
|
} catch (RemoteException re) {
|
|
/* ignore */
|
|
}
|
|
mPrintService.asBinder().unlinkToDeath(this, 0);
|
|
mPrintService = null;
|
|
mContext.unbindService(mServiceConnection);
|
|
}
|
|
}
|
|
|
|
private void throwIfDestroyed() {
|
|
if (mDestroyed) {
|
|
throw new IllegalStateException("Cannot interact with a destroyed service");
|
|
}
|
|
}
|
|
|
|
private class RemoteServiceConneciton implements ServiceConnection {
|
|
@Override
|
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
|
if (mDestroyed || !mBinding) {
|
|
return;
|
|
}
|
|
mBinding = false;
|
|
mPrintService = IPrintService.Stub.asInterface(service);
|
|
try {
|
|
service.linkToDeath(RemotePrintService.this, 0);
|
|
} catch (RemoteException re) {
|
|
handleBinderDied();
|
|
return;
|
|
}
|
|
try {
|
|
mPrintService.setClient(mPrintServiceClient);
|
|
} catch (RemoteException re) {
|
|
Slog.e(LOG_TAG, "Error setting client for: " + service, re);
|
|
handleBinderDied();
|
|
return;
|
|
}
|
|
final int pendingCommandCount = mPendingCommands.size();
|
|
for (int i = 0; i < pendingCommandCount; i++) {
|
|
Runnable pendingCommand = mPendingCommands.get(i);
|
|
pendingCommand.run();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onServiceDisconnected(ComponentName name) {
|
|
mBinding = true;
|
|
}
|
|
}
|
|
|
|
private final class MyHandler extends Handler {
|
|
public static final int MSG_ON_ALL_PRINT_JOBS_HANDLED = 1;
|
|
public static final int MSG_ON_REQUEST_CANCEL_PRINT_JOB = 2;
|
|
public static final int MSG_ON_PRINT_JOB_QUEUED = 3;
|
|
public static final int MSG_ON_START_PRINTER_DISCOVERY = 4;
|
|
public static final int MSG_ON_STOP_PRINTER_DISCOVERY = 5;
|
|
public static final int MSG_ON_REQUEST_UPDATE_PRINTERS = 6;
|
|
public static final int MSG_DESTROY = 7;
|
|
public static final int MSG_BINDER_DIED = 8;
|
|
|
|
public MyHandler(Looper looper) {
|
|
super(looper, null, false);
|
|
}
|
|
|
|
@Override
|
|
@SuppressWarnings("unchecked")
|
|
public void handleMessage(Message message) {
|
|
switch (message.what) {
|
|
case MSG_ON_ALL_PRINT_JOBS_HANDLED: {
|
|
handleOnAllPrintJobsHandled();
|
|
} break;
|
|
|
|
case MSG_ON_REQUEST_CANCEL_PRINT_JOB: {
|
|
PrintJobInfo printJob = (PrintJobInfo) message.obj;
|
|
handleOnRequestCancelPrintJob(printJob);
|
|
} break;
|
|
|
|
case MSG_ON_PRINT_JOB_QUEUED: {
|
|
PrintJobInfo printJob = (PrintJobInfo) message.obj;
|
|
handleOnPrintJobQueued(printJob);
|
|
} break;
|
|
|
|
case MSG_ON_START_PRINTER_DISCOVERY: {
|
|
IPrinterDiscoveryObserver observer = (IPrinterDiscoveryObserver) message.obj;
|
|
handleOnStartPrinterDiscovery(new SecurePrinterDiscoveryObserver(
|
|
mComponentName, observer));
|
|
} break;
|
|
|
|
case MSG_ON_REQUEST_UPDATE_PRINTERS: {
|
|
List<PrinterId> printerIds = (List<PrinterId>) message.obj;
|
|
handleReqeustUpdatePritners(printerIds);
|
|
} break;
|
|
|
|
case MSG_ON_STOP_PRINTER_DISCOVERY: {
|
|
handleStopPrinterDiscovery();
|
|
} break;
|
|
|
|
case MSG_DESTROY: {
|
|
handleDestroy();
|
|
} break;
|
|
|
|
case MSG_BINDER_DIED: {
|
|
handleBinderDied();
|
|
} break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private static final class RemotePrintServiceClient extends IPrintServiceClient.Stub {
|
|
private final WeakReference<RemotePrintService> mWeakService;
|
|
|
|
public RemotePrintServiceClient(RemotePrintService service) {
|
|
mWeakService = new WeakReference<RemotePrintService>(service);
|
|
}
|
|
|
|
@Override
|
|
public List<PrintJobInfo> getPrintJobInfos() {
|
|
RemotePrintService service = mWeakService.get();
|
|
if (service != null) {
|
|
final long identity = Binder.clearCallingIdentity();
|
|
try {
|
|
return service.mSpooler.getPrintJobInfos(service.mComponentName,
|
|
PrintJobInfo.STATE_ANY_VISIBLE_TO_CLIENTS, PrintManager.APP_ID_ANY);
|
|
} finally {
|
|
Binder.restoreCallingIdentity(identity);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public PrintJobInfo getPrintJobInfo(int printJobId) {
|
|
RemotePrintService service = mWeakService.get();
|
|
if (service != null) {
|
|
final long identity = Binder.clearCallingIdentity();
|
|
try {
|
|
return service.mSpooler.getPrintJobInfo(printJobId,
|
|
PrintManager.APP_ID_ANY);
|
|
} finally {
|
|
Binder.restoreCallingIdentity(identity);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public boolean setPrintJobState(int printJobId, int state, CharSequence error) {
|
|
RemotePrintService service = mWeakService.get();
|
|
if (service != null) {
|
|
final long identity = Binder.clearCallingIdentity();
|
|
try {
|
|
return service.mSpooler.setPrintJobState(printJobId, state, error);
|
|
} finally {
|
|
Binder.restoreCallingIdentity(identity);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean setPrintJobTag(int printJobId, String tag) {
|
|
RemotePrintService service = mWeakService.get();
|
|
if (service != null) {
|
|
final long identity = Binder.clearCallingIdentity();
|
|
try {
|
|
return service.mSpooler.setPrintJobTag(printJobId, tag);
|
|
} finally {
|
|
Binder.restoreCallingIdentity(identity);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public void writePrintJobData(ParcelFileDescriptor fd, int printJobId) {
|
|
RemotePrintService service = mWeakService.get();
|
|
if (service != null) {
|
|
final long identity = Binder.clearCallingIdentity();
|
|
try {
|
|
service.mSpooler.writePrintJobData(fd, printJobId);
|
|
} finally {
|
|
Binder.restoreCallingIdentity(identity);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static final class SecurePrinterDiscoveryObserver
|
|
extends IPrinterDiscoveryObserver.Stub {
|
|
private final ComponentName mComponentName;
|
|
|
|
private final IPrinterDiscoveryObserver mDecoratedObsever;
|
|
|
|
public SecurePrinterDiscoveryObserver(ComponentName componentName,
|
|
IPrinterDiscoveryObserver observer) {
|
|
mComponentName = componentName;
|
|
mDecoratedObsever = observer;
|
|
}
|
|
|
|
@Override
|
|
public void onPrintersAdded(List<PrinterInfo> printers) {
|
|
throwIfPrinterIdsForPrinterInfoTampered(printers);
|
|
try {
|
|
mDecoratedObsever.onPrintersAdded(printers);
|
|
} catch (RemoteException re) {
|
|
Slog.e(LOG_TAG, "Error delegating to onPrintersAdded", re);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onPrintersUpdated(List<PrinterInfo> printers) {
|
|
throwIfPrinterIdsForPrinterInfoTampered(printers);
|
|
try {
|
|
mDecoratedObsever.onPrintersUpdated(printers);
|
|
} catch (RemoteException re) {
|
|
Slog.e(LOG_TAG, "Error delegating to onPrintersUpdated.", re);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onPrintersRemoved(List<PrinterId> printerIds) {
|
|
throwIfPrinterIdsTampered(printerIds);
|
|
try {
|
|
mDecoratedObsever.onPrintersRemoved(printerIds);
|
|
} catch (RemoteException re) {
|
|
Slog.e(LOG_TAG, "Error delegating to onPrintersRemoved", re);
|
|
}
|
|
}
|
|
|
|
private void throwIfPrinterIdsForPrinterInfoTampered(
|
|
List<PrinterInfo> printerInfos) {
|
|
final int printerInfoCount = printerInfos.size();
|
|
for (int i = 0; i < printerInfoCount; i++) {
|
|
PrinterId printerId = printerInfos.get(i).getId();
|
|
throwIfPrinterIdTampered(printerId);
|
|
}
|
|
}
|
|
|
|
private void throwIfPrinterIdsTampered(List<PrinterId> printerIds) {
|
|
final int printerIdCount = printerIds.size();
|
|
for (int i = 0; i < printerIdCount; i++) {
|
|
PrinterId printerId = printerIds.get(i);
|
|
throwIfPrinterIdTampered(printerId);
|
|
}
|
|
}
|
|
|
|
private void throwIfPrinterIdTampered(PrinterId printerId) {
|
|
if (printerId == null || printerId.getServiceName() == null
|
|
|| !printerId.getServiceName().equals(mComponentName)) {
|
|
throw new IllegalArgumentException("Invalid printer id: " + printerId);
|
|
}
|
|
}
|
|
}
|
|
}
|