diff options
author | Svetoslav Ganov <svetoslavganov@google.com> | 2013-09-26 12:28:59 -0700 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2013-09-26 12:28:59 -0700 |
commit | af1066d917fd47c8108df98f8a534babd48d18cd (patch) | |
tree | 654142c5da25f11cd056e67f93fa9fa8a4474919 /services | |
parent | aa14d0e7fd36fa29557ef7c3980b86703f767760 (diff) | |
parent | 4609d9a9ebc17d804d31d5241968157439c92a57 (diff) | |
download | frameworks_base-af1066d917fd47c8108df98f8a534babd48d18cd.zip frameworks_base-af1066d917fd47c8108df98f8a534babd48d18cd.tar.gz frameworks_base-af1066d917fd47c8108df98f8a534babd48d18cd.tar.bz2 |
am 4609d9a9: am 5cab967b: Merge "Adding hidden APIs for observing the print jobs state." into klp-dev
* commit '4609d9a9ebc17d804d31d5241968157439c92a57':
Adding hidden APIs for observing the print jobs state.
Diffstat (limited to 'services')
3 files changed, 164 insertions, 0 deletions
diff --git a/services/java/com/android/server/print/PrintManagerService.java b/services/java/com/android/server/print/PrintManagerService.java index 5f8708a..625770c 100644 --- a/services/java/com/android/server/print/PrintManagerService.java +++ b/services/java/com/android/server/print/PrintManagerService.java @@ -32,9 +32,11 @@ import android.database.ContentObserver; import android.net.Uri; import android.os.Binder; import android.os.Process; +import android.os.RemoteException; import android.os.UserHandle; import android.print.IPrintClient; import android.print.IPrintDocumentAdapter; +import android.print.IPrintJobStateChangeListener; import android.print.IPrintManager; import android.print.IPrinterDiscoveryObserver; import android.print.PrintAttributes; @@ -300,6 +302,39 @@ public final class PrintManagerService extends IPrintManager.Stub { } @Override + public void addPrintJobStateChangeListener(IPrintJobStateChangeListener listener, + int appId, int userId) throws RemoteException { + final int resolvedUserId = resolveCallingUserEnforcingPermissions(userId); + final int resolvedAppId = resolveCallingAppEnforcingPermissions(appId); + final UserState userState; + synchronized (mLock) { + userState = getOrCreateUserStateLocked(resolvedUserId); + } + final long identity = Binder.clearCallingIdentity(); + try { + userState.addPrintJobStateChangeListener(listener, resolvedAppId); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + + @Override + public void removePrintJobStateChangeListener(IPrintJobStateChangeListener listener, + int userId) { + final int resolvedUserId = resolveCallingUserEnforcingPermissions(userId); + final UserState userState; + synchronized (mLock) { + userState = getOrCreateUserStateLocked(resolvedUserId); + } + final long identity = Binder.clearCallingIdentity(); + try { + userState.removePrintJobStateChangeListener(listener); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + + @Override public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { if (mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) { diff --git a/services/java/com/android/server/print/RemotePrintSpooler.java b/services/java/com/android/server/print/RemotePrintSpooler.java index 1bde6d7..f98a805 100644 --- a/services/java/com/android/server/print/RemotePrintSpooler.java +++ b/services/java/com/android/server/print/RemotePrintSpooler.java @@ -91,6 +91,7 @@ final class RemotePrintSpooler { public static interface PrintSpoolerCallbacks { public void onPrintJobQueued(PrintJobInfo printJob); public void onAllPrintJobsForServiceHandled(ComponentName printService); + public void onPrintJobStateChanged(PrintJobId printJobId, int appId); } public RemotePrintSpooler(Context context, int userId, @@ -345,6 +346,10 @@ final class RemotePrintSpooler { } } + private void onPrintJobStateChanged(PrintJobId printJobId, int appId) { + mCallbacks.onPrintJobStateChanged(printJobId, appId); + } + private IPrintSpooler getRemoteInstanceLazy() throws TimeoutException { synchronized (mLock) { if (mRemoteInstance != null) { @@ -618,5 +623,18 @@ final class RemotePrintSpooler { } } } + + @Override + public void onPrintJobStateChanged(PrintJobId printJobId, int appId) { + RemotePrintSpooler spooler = mWeakSpooler.get(); + if (spooler != null) { + final long identity = Binder.clearCallingIdentity(); + try { + spooler.onPrintJobStateChanged(printJobId, appId); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } } } diff --git a/services/java/com/android/server/print/UserState.java b/services/java/com/android/server/print/UserState.java index 8c21827..e5f5842 100644 --- a/services/java/com/android/server/print/UserState.java +++ b/services/java/com/android/server/print/UserState.java @@ -36,6 +36,7 @@ import android.os.RemoteException; import android.os.UserManager; import android.print.IPrintClient; import android.print.IPrintDocumentAdapter; +import android.print.IPrintJobStateChangeListener; import android.print.IPrinterDiscoveryObserver; import android.print.PrintAttributes; import android.print.PrintJobId; @@ -103,8 +104,12 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { private final RemotePrintSpooler mSpooler; + private final Handler mHandler; + private PrinterDiscoverySessionMediator mPrinterDiscoverySession; + private List<PrintJobStateChangeListenerRecord> mPrintJobStateChangeListenerRecords; + private boolean mDestroyed; public UserState(Context context, int userId, Object lock) { @@ -112,6 +117,7 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { mUserId = userId; mLock = lock; mSpooler = new RemotePrintSpooler(context, userId, this); + mHandler = new UserStateHandler(context.getMainLooper()); synchronized (mLock) { enableSystemPrintServicesOnFirstBootLocked(); } @@ -164,6 +170,7 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { printJob.setLabel(printJobName); printJob.setAttributes(attributes); printJob.setState(PrintJobInfo.STATE_CREATED); + printJob.setCopies(1); // Spin the spooler to add the job and show the config UI. new AsyncTask<Void, Void, Void>() { @@ -351,6 +358,51 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { } } + public void addPrintJobStateChangeListener(IPrintJobStateChangeListener listener, + int appId) throws RemoteException { + synchronized (mLock) { + throwIfDestroyedLocked(); + if (mPrintJobStateChangeListenerRecords == null) { + mPrintJobStateChangeListenerRecords = + new ArrayList<PrintJobStateChangeListenerRecord>(); + } + mPrintJobStateChangeListenerRecords.add( + new PrintJobStateChangeListenerRecord(listener, appId) { + @Override + public void onBinderDied() { + mPrintJobStateChangeListenerRecords.remove(this); + } + }); + } + } + + public void removePrintJobStateChangeListener(IPrintJobStateChangeListener listener) { + synchronized (mLock) { + throwIfDestroyedLocked(); + if (mPrintJobStateChangeListenerRecords == null) { + return; + } + final int recordCount = mPrintJobStateChangeListenerRecords.size(); + for (int i = 0; i < recordCount; i++) { + PrintJobStateChangeListenerRecord record = + mPrintJobStateChangeListenerRecords.get(i); + if (record.listener.asBinder().equals(listener.asBinder())) { + mPrintJobStateChangeListenerRecords.remove(i); + break; + } + } + if (mPrintJobStateChangeListenerRecords.isEmpty()) { + mPrintJobStateChangeListenerRecords = null; + } + } + } + + @Override + public void onPrintJobStateChanged(PrintJobId printJobId, int appId) { + mHandler.obtainMessage(UserStateHandler.MSG_DISPATCH_PRINT_JOB_STATE_CHANGED, + appId, 0, printJobId).sendToTarget(); + } + @Override public void onPrintersAdded(List<PrinterInfo> printers) { synchronized (mLock) { @@ -698,6 +750,65 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks { } } + private void handleDispatchPrintJobStateChanged(PrintJobId printJobId, int appId) { + final List<PrintJobStateChangeListenerRecord> records; + synchronized (mLock) { + if (mPrintJobStateChangeListenerRecords == null) { + return; + } + records = new ArrayList<PrintJobStateChangeListenerRecord>( + mPrintJobStateChangeListenerRecords); + } + final int recordCount = records.size(); + for (int i = 0; i < recordCount; i++) { + PrintJobStateChangeListenerRecord record = records.get(i); + if (record.appId == PrintManager.APP_ID_ANY + || record.appId == appId) + try { + record.listener.onPrintJobStateChanged(printJobId); + } catch (RemoteException re) { + Log.e(LOG_TAG, "Error notifying for print job state change", re); + } + } + } + + private final class UserStateHandler extends Handler { + public static final int MSG_DISPATCH_PRINT_JOB_STATE_CHANGED = 1; + + public UserStateHandler(Looper looper) { + super(looper, null, false); + } + + @Override + public void handleMessage(Message message) { + if (message.what == MSG_DISPATCH_PRINT_JOB_STATE_CHANGED) { + PrintJobId printJobId = (PrintJobId) message.obj; + final int appId = message.arg1; + handleDispatchPrintJobStateChanged(printJobId, appId); + } + } + } + + private abstract class PrintJobStateChangeListenerRecord implements DeathRecipient { + final IPrintJobStateChangeListener listener; + final int appId; + + public PrintJobStateChangeListenerRecord(IPrintJobStateChangeListener listener, + int appId) throws RemoteException { + this.listener = listener; + this.appId = appId; + listener.asBinder().linkToDeath(this, 0); + } + + @Override + public void binderDied() { + listener.asBinder().unlinkToDeath(this, 0); + onBinderDied(); + } + + public abstract void onBinderDied(); + } + private class PrinterDiscoverySessionMediator { private final ArrayMap<PrinterId, PrinterInfo> mPrinters = new ArrayMap<PrinterId, PrinterInfo>(); |