diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2018-02-23 21:21:20 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2018-02-23 21:21:20 +0000 |
commit | 1dcd64578992a650f3134be2178694ce0b6ee9d8 (patch) | |
tree | 23dc811cb5d33f5250472bfe86cf228abe0cae06 /packages/PrintSpooler/src | |
parent | 7509bb3f97feb1d15d4472b7112e6485ea453ee0 (diff) | |
parent | 9f35ca996432e960b77eb194975e2086d7c18aff (diff) |
Merge "Use PooledLambda in print code"
Diffstat (limited to 'packages/PrintSpooler/src')
-rw-r--r-- | packages/PrintSpooler/src/com/android/printspooler/model/PrintSpoolerService.java | 162 | ||||
-rw-r--r-- | packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java | 49 |
2 files changed, 85 insertions, 126 deletions
diff --git a/packages/PrintSpooler/src/com/android/printspooler/model/PrintSpoolerService.java b/packages/PrintSpooler/src/com/android/printspooler/model/PrintSpoolerService.java index 53e8813c7363..eba5edbf6d68 100644 --- a/packages/PrintSpooler/src/com/android/printspooler/model/PrintSpoolerService.java +++ b/packages/PrintSpooler/src/com/android/printspooler/model/PrintSpoolerService.java @@ -31,6 +31,7 @@ import android.graphics.drawable.Icon; import android.os.AsyncTask; import android.os.Binder; import android.os.Bundle; +import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.ParcelFileDescriptor; @@ -58,11 +59,11 @@ import android.util.Xml; import android.util.proto.ProtoOutputStream; import com.android.internal.logging.MetricsLogger; -import com.android.internal.os.HandlerCaller; import com.android.internal.util.FastXmlSerializer; import com.android.internal.util.IndentingPrintWriter; import com.android.internal.util.Preconditions; import com.android.internal.util.dump.DualDumpOutputStream; +import com.android.internal.util.function.pooled.PooledLambda; import com.android.printspooler.R; import com.android.printspooler.util.ApprovedPrintServices; @@ -116,8 +117,6 @@ public final class PrintSpoolerService extends Service { private IPrintSpoolerClient mClient; - private HandlerCaller mHandlerCaller; - private PersistenceManager mPersistanceManager; private NotificationController mNotificationController; @@ -134,8 +133,6 @@ public final class PrintSpoolerService extends Service { @Override public void onCreate() { super.onCreate(); - mHandlerCaller = new HandlerCaller(this, getMainLooper(), - new HandlerCallerCallback(), false); mPersistanceManager = new PersistenceManager(); mNotificationController = new NotificationController(PrintSpoolerService.this); @@ -230,93 +227,73 @@ public final class PrintSpoolerService extends Service { } private void sendOnPrintJobQueued(PrintJobInfo printJob) { - Message message = mHandlerCaller.obtainMessageO( - HandlerCallerCallback.MSG_ON_PRINT_JOB_QUEUED, printJob); - mHandlerCaller.executeOrSendMessage(message); + Message message = PooledLambda.obtainMessage( + PrintSpoolerService::onPrintJobQueued, this, printJob); + Handler.getMain().executeOrSendMessage(message); } private void sendOnAllPrintJobsForServiceHandled(ComponentName service) { - Message message = mHandlerCaller.obtainMessageO( - HandlerCallerCallback.MSG_ON_ALL_PRINT_JOBS_FOR_SERIVICE_HANDLED, service); - mHandlerCaller.executeOrSendMessage(message); + Message message = PooledLambda.obtainMessage( + PrintSpoolerService::onAllPrintJobsForServiceHandled, this, service); + Handler.getMain().executeOrSendMessage(message); } private void sendOnAllPrintJobsHandled() { - Message message = mHandlerCaller.obtainMessage( - HandlerCallerCallback.MSG_ON_ALL_PRINT_JOBS_HANDLED); - mHandlerCaller.executeOrSendMessage(message); + Message message = PooledLambda.obtainMessage( + PrintSpoolerService::onAllPrintJobsHandled, this); + Handler.getMain().executeOrSendMessage(message); } - private final class HandlerCallerCallback implements HandlerCaller.Callback { - public static final int MSG_SET_CLIENT = 1; - public static final int MSG_ON_PRINT_JOB_QUEUED = 2; - public static final int MSG_ON_ALL_PRINT_JOBS_FOR_SERIVICE_HANDLED = 3; - public static final int MSG_ON_ALL_PRINT_JOBS_HANDLED = 4; - public static final int MSG_CHECK_ALL_PRINTJOBS_HANDLED = 5; - public static final int MSG_ON_PRINT_JOB_STATE_CHANGED = 6; - - @Override - public void executeMessage(Message message) { - switch (message.what) { - case MSG_SET_CLIENT: { - synchronized (mLock) { - mClient = (IPrintSpoolerClient) message.obj; - if (mClient != null) { - Message msg = mHandlerCaller.obtainMessage( - HandlerCallerCallback.MSG_CHECK_ALL_PRINTJOBS_HANDLED); - mHandlerCaller.sendMessageDelayed(msg, - CHECK_ALL_PRINTJOBS_HANDLED_DELAY); - } - } - } break; - case MSG_ON_PRINT_JOB_QUEUED: { - PrintJobInfo printJob = (PrintJobInfo) message.obj; - if (mClient != null) { - try { - mClient.onPrintJobQueued(printJob); - } catch (RemoteException re) { - Slog.e(LOG_TAG, "Error notify for a queued print job.", re); - } - } - } break; + private void onPrintJobStateChanged(PrintJobInfo printJob) { + if (mClient != null) { + try { + mClient.onPrintJobStateChanged(printJob); + } catch (RemoteException re) { + Slog.e(LOG_TAG, "Error notify for print job state change.", re); + } + } + } - case MSG_ON_ALL_PRINT_JOBS_FOR_SERIVICE_HANDLED: { - ComponentName service = (ComponentName) message.obj; - if (mClient != null) { - try { - mClient.onAllPrintJobsForServiceHandled(service); - } catch (RemoteException re) { - Slog.e(LOG_TAG, "Error notify for all print jobs per service" - + " handled.", re); - } - } - } break; + private void onAllPrintJobsHandled() { + if (mClient != null) { + try { + mClient.onAllPrintJobsHandled(); + } catch (RemoteException re) { + Slog.e(LOG_TAG, "Error notify for all print job handled.", re); + } + } + } - case MSG_ON_ALL_PRINT_JOBS_HANDLED: { - if (mClient != null) { - try { - mClient.onAllPrintJobsHandled(); - } catch (RemoteException re) { - Slog.e(LOG_TAG, "Error notify for all print job handled.", re); - } - } - } break; + private void onAllPrintJobsForServiceHandled(ComponentName service) { + if (mClient != null) { + try { + mClient.onAllPrintJobsForServiceHandled(service); + } catch (RemoteException re) { + Slog.e(LOG_TAG, "Error notify for all print jobs per service" + + " handled.", re); + } + } + } - case MSG_CHECK_ALL_PRINTJOBS_HANDLED: { - checkAllPrintJobsHandled(); - } break; + private void onPrintJobQueued(PrintJobInfo printJob) { + if (mClient != null) { + try { + mClient.onPrintJobQueued(printJob); + } catch (RemoteException re) { + Slog.e(LOG_TAG, "Error notify for a queued print job.", re); + } + } + } - case MSG_ON_PRINT_JOB_STATE_CHANGED: { - if (mClient != null) { - PrintJobInfo printJob = (PrintJobInfo) message.obj; - try { - mClient.onPrintJobStateChanged(printJob); - } catch (RemoteException re) { - Slog.e(LOG_TAG, "Error notify for print job state change.", re); - } - } - } break; + private void setClient(IPrintSpoolerClient client) { + synchronized (mLock) { + mClient = client; + if (mClient != null) { + Message msg = PooledLambda.obtainMessage( + PrintSpoolerService::checkAllPrintJobsHandled, this); + Handler.getMain().sendMessageDelayed(msg, + CHECK_ALL_PRINTJOBS_HANDLED_DELAY); } } } @@ -379,10 +356,9 @@ public final class PrintSpoolerService extends Service { addPrintJobLocked(printJob); setPrintJobState(printJob.getId(), PrintJobInfo.STATE_CREATED, null); - Message message = mHandlerCaller.obtainMessageO( - HandlerCallerCallback.MSG_ON_PRINT_JOB_STATE_CHANGED, - printJob); - mHandlerCaller.executeOrSendMessage(message); + Message message = PooledLambda.obtainMessage( + PrintSpoolerService::onPrintJobStateChanged, this, printJob); + Handler.getMain().executeOrSendMessage(message); } } @@ -546,10 +522,9 @@ public final class PrintSpoolerService extends Service { * @param printJob The updated print job. */ private void notifyPrintJobUpdated(PrintJobInfo printJob) { - Message message = mHandlerCaller.obtainMessageO( - HandlerCallerCallback.MSG_ON_PRINT_JOB_STATE_CHANGED, - printJob); - mHandlerCaller.executeOrSendMessage(message); + Message message = PooledLambda.obtainMessage( + PrintSpoolerService::onPrintJobStateChanged, this, printJob); + Handler.getMain().executeOrSendMessage(message); mNotificationController.onUpdateNotifications(mPrintJobs); } @@ -742,10 +717,9 @@ public final class PrintSpoolerService extends Service { } mNotificationController.onUpdateNotifications(mPrintJobs); - Message message = mHandlerCaller.obtainMessageO( - HandlerCallerCallback.MSG_ON_PRINT_JOB_STATE_CHANGED, - printJob); - mHandlerCaller.executeOrSendMessage(message); + Message message = PooledLambda.obtainMessage( + PrintSpoolerService::onPrintJobStateChanged, this, printJob); + Handler.getMain().executeOrSendMessage(message); } } } @@ -1472,9 +1446,9 @@ public final class PrintSpoolerService extends Service { @Override public void setClient(IPrintSpoolerClient client) { - Message message = mHandlerCaller.obtainMessageO( - HandlerCallerCallback.MSG_SET_CLIENT, client); - mHandlerCaller.executeOrSendMessage(message); + Message message = PooledLambda.obtainMessage( + PrintSpoolerService::setClient, PrintSpoolerService.this, client); + Handler.getMain().executeOrSendMessage(message); } @Override diff --git a/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java b/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java index b6a003de0eb9..164454690d46 100644 --- a/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java +++ b/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java @@ -16,7 +16,6 @@ package com.android.printspooler.model; -import android.annotation.NonNull; import android.content.ContentResolver; import android.content.Context; import android.net.Uri; @@ -39,6 +38,7 @@ import android.print.PrintDocumentAdapter; import android.print.PrintDocumentInfo; import android.util.Log; +import com.android.internal.util.function.pooled.PooledLambda; import com.android.printspooler.R; import com.android.printspooler.util.PageRangeUtils; @@ -549,6 +549,9 @@ public final class RemotePrintDocument { } private static abstract class AsyncCommand implements Runnable { + /** Message indicated the desire to {@link #forceCancel} a command */ + static final int MSG_FORCE_CANCEL = 0; + private static final int STATE_PENDING = 0; private static final int STATE_RUNNING = 1; private static final int STATE_COMPLETED = 2; @@ -574,7 +577,7 @@ public final class RemotePrintDocument { public AsyncCommand(Looper looper, IPrintDocumentAdapter adapter, RemotePrintDocumentInfo document, CommandDoneCallback doneCallback) { - mHandler = new AsyncCommandHandler(looper); + mHandler = new Handler(looper); mAdapter = adapter; mDocument = document; mDoneCallback = doneCallback; @@ -594,12 +597,12 @@ public final class RemotePrintDocument { */ protected void removeForceCancel() { if (DEBUG) { - if (mHandler.hasMessages(AsyncCommandHandler.MSG_FORCE_CANCEL)) { + if (mHandler.hasMessages(MSG_FORCE_CANCEL)) { Log.i(LOG_TAG, "[FORCE CANCEL] Removed"); } } - mHandler.removeMessages(AsyncCommandHandler.MSG_FORCE_CANCEL); + mHandler.removeMessages(MSG_FORCE_CANCEL); } /** @@ -628,7 +631,8 @@ public final class RemotePrintDocument { Log.i(LOG_TAG, "[FORCE CANCEL] queued"); } mHandler.sendMessageDelayed( - mHandler.obtainMessage(AsyncCommandHandler.MSG_FORCE_CANCEL), + PooledLambda.obtainMessage(AsyncCommand::forceCancel, this) + .setWhat(MSG_FORCE_CANCEL), FORCE_CANCEL_TIMEOUT); } @@ -698,34 +702,15 @@ public final class RemotePrintDocument { return mError; } - /** - * Handler for the async command. - */ - private class AsyncCommandHandler extends Handler { - /** Message indicated the desire for to force cancel a command */ - final static int MSG_FORCE_CANCEL = 0; - - AsyncCommandHandler(@NonNull Looper looper) { - super(looper); - } - - @Override - public void handleMessage(Message msg) { - switch (msg.what) { - case MSG_FORCE_CANCEL: - if (isCanceling()) { - if (DEBUG) { - Log.i(LOG_TAG, "[FORCE CANCEL] executed"); - } - failed("Command did not respond to cancellation in " - + FORCE_CANCEL_TIMEOUT + " ms"); - - mDoneCallback.onDone(); - } - break; - default: - // not reached; + private void forceCancel() { + if (isCanceling()) { + if (DEBUG) { + Log.i(LOG_TAG, "[FORCE CANCEL] executed"); } + failed("Command did not respond to cancellation in " + + FORCE_CANCEL_TIMEOUT + " ms"); + + mDoneCallback.onDone(); } } } |