Merge "Use PooledLambda in print code"

This commit is contained in:
TreeHugger Robot
2018-02-23 21:21:20 +00:00
committed by Android (Google) Code Review
5 changed files with 156 additions and 295 deletions

View File

@@ -683,6 +683,23 @@ public class Handler {
return enqueueMessage(queue, msg, 0);
}
/**
* Executes the message synchronously if called on the same thread this handler corresponds to,
* or {@link #sendMessage pushes it to the queue} otherwise
*
* @return Returns true if the message was successfully ran or placed in to the
* message queue. Returns false on failure, usually because the
* looper processing the message queue is exiting.
* @hide
*/
public final boolean executeOrSendMessage(Message msg) {
if (mLooper == Looper.myLooper()) {
dispatchMessage(msg);
return true;
}
return sendMessage(msg);
}
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
msg.target = this;
if (mAsynchronous) {

View File

@@ -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;
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_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 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_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 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_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 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_CHECK_ALL_PRINTJOBS_HANDLED: {
checkAllPrintJobsHandled();
} break;
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

View File

@@ -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();
}
}
}

View File

@@ -18,6 +18,7 @@ package com.android.server.print;
import static com.android.internal.print.DumpUtils.writePrinterId;
import static com.android.internal.util.dump.DumpUtils.writeComponentName;
import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage;
import android.annotation.FloatRange;
import android.annotation.NonNull;
@@ -33,8 +34,6 @@ import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.IBinder.DeathRecipient;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.os.UserHandle;
@@ -86,8 +85,6 @@ final class RemotePrintService implements DeathRecipient {
private final RemotePrintServiceClient mPrintServiceClient;
private final Handler mHandler;
private IPrintService mPrintService;
private boolean mBinding;
@@ -128,7 +125,6 @@ final class RemotePrintService implements DeathRecipient {
mIntent = new Intent().setComponent(mComponentName);
mUserId = userId;
mSpooler = spooler;
mHandler = new MyHandler(context.getMainLooper());
mPrintServiceClient = new RemotePrintServiceClient(this);
}
@@ -137,7 +133,8 @@ final class RemotePrintService implements DeathRecipient {
}
public void destroy() {
mHandler.sendEmptyMessage(MyHandler.MSG_DESTROY);
Handler.getMain().sendMessage(obtainMessage(
RemotePrintService::handleDestroy, this));
}
private void handleDestroy() {
@@ -163,7 +160,8 @@ final class RemotePrintService implements DeathRecipient {
@Override
public void binderDied() {
mHandler.sendEmptyMessage(MyHandler.MSG_BINDER_DIED);
Handler.getMain().sendMessage(obtainMessage(
RemotePrintService::handleBinderDied, this));
}
private void handleBinderDied() {
@@ -177,7 +175,8 @@ final class RemotePrintService implements DeathRecipient {
}
public void onAllPrintJobsHandled() {
mHandler.sendEmptyMessage(MyHandler.MSG_ON_ALL_PRINT_JOBS_HANDLED);
Handler.getMain().sendMessage(obtainMessage(
RemotePrintService::handleOnAllPrintJobsHandled, this));
}
private void handleOnAllPrintJobsHandled() {
@@ -209,8 +208,8 @@ final class RemotePrintService implements DeathRecipient {
}
public void onRequestCancelPrintJob(PrintJobInfo printJob) {
mHandler.obtainMessage(MyHandler.MSG_ON_REQUEST_CANCEL_PRINT_JOB,
printJob).sendToTarget();
Handler.getMain().sendMessage(obtainMessage(
RemotePrintService::handleRequestCancelPrintJob, this, printJob));
}
private void handleRequestCancelPrintJob(final PrintJobInfo printJob) {
@@ -235,8 +234,8 @@ final class RemotePrintService implements DeathRecipient {
}
public void onPrintJobQueued(PrintJobInfo printJob) {
mHandler.obtainMessage(MyHandler.MSG_ON_PRINT_JOB_QUEUED,
printJob).sendToTarget();
Handler.getMain().sendMessage(obtainMessage(
RemotePrintService::handleOnPrintJobQueued, this, printJob));
}
private void handleOnPrintJobQueued(final PrintJobInfo printJob) {
@@ -262,7 +261,8 @@ final class RemotePrintService implements DeathRecipient {
}
public void createPrinterDiscoverySession() {
mHandler.sendEmptyMessage(MyHandler.MSG_CREATE_PRINTER_DISCOVERY_SESSION);
Handler.getMain().sendMessage(obtainMessage(
RemotePrintService::handleCreatePrinterDiscoverySession, this));
}
private void handleCreatePrinterDiscoverySession() {
@@ -288,7 +288,8 @@ final class RemotePrintService implements DeathRecipient {
}
public void destroyPrinterDiscoverySession() {
mHandler.sendEmptyMessage(MyHandler.MSG_DESTROY_PRINTER_DISCOVERY_SESSION);
Handler.getMain().sendMessage(obtainMessage(
RemotePrintService::handleDestroyPrinterDiscoverySession, this));
}
private void handleDestroyPrinterDiscoverySession() {
@@ -325,8 +326,8 @@ final class RemotePrintService implements DeathRecipient {
}
public void startPrinterDiscovery(List<PrinterId> priorityList) {
mHandler.obtainMessage(MyHandler.MSG_START_PRINTER_DISCOVERY,
priorityList).sendToTarget();
Handler.getMain().sendMessage(obtainMessage(
RemotePrintService::handleStartPrinterDiscovery, this, priorityList));
}
private void handleStartPrinterDiscovery(final List<PrinterId> priorityList) {
@@ -356,7 +357,8 @@ final class RemotePrintService implements DeathRecipient {
}
public void stopPrinterDiscovery() {
mHandler.sendEmptyMessage(MyHandler.MSG_STOP_PRINTER_DISCOVERY);
Handler.getMain().sendMessage(obtainMessage(
RemotePrintService::handleStopPrinterDiscovery, this));
}
private void handleStopPrinterDiscovery() {
@@ -387,8 +389,8 @@ final class RemotePrintService implements DeathRecipient {
}
public void validatePrinters(List<PrinterId> printerIds) {
mHandler.obtainMessage(MyHandler.MSG_VALIDATE_PRINTERS,
printerIds).sendToTarget();
Handler.getMain().sendMessage(obtainMessage(
RemotePrintService::handleValidatePrinters, this, printerIds));
}
private void handleValidatePrinters(final List<PrinterId> printerIds) {
@@ -413,8 +415,8 @@ final class RemotePrintService implements DeathRecipient {
}
public void startPrinterStateTracking(@NonNull PrinterId printerId) {
mHandler.obtainMessage(MyHandler.MSG_START_PRINTER_STATE_TRACKING,
printerId).sendToTarget();
Handler.getMain().sendMessage(obtainMessage(
RemotePrintService::handleStartPrinterStateTracking, this, printerId));
}
/**
@@ -424,8 +426,8 @@ final class RemotePrintService implements DeathRecipient {
* @see android.print.PrinterInfo.Builder#setHasCustomPrinterIcon
*/
public void requestCustomPrinterIcon(@NonNull PrinterId printerId) {
mHandler.obtainMessage(MyHandler.MSG_REQUEST_CUSTOM_PRINTER_ICON,
printerId).sendToTarget();
Handler.getMain().sendMessage(obtainMessage(
RemotePrintService::handleRequestCustomPrinterIcon, this, printerId));
}
/**
@@ -481,8 +483,8 @@ final class RemotePrintService implements DeathRecipient {
}
public void stopPrinterStateTracking(PrinterId printerId) {
mHandler.obtainMessage(MyHandler.MSG_STOP_PRINTER_STATE_TRACKING,
printerId).sendToTarget();
Handler.getMain().sendMessage(obtainMessage(
RemotePrintService::handleStopPrinterStateTracking, this, printerId));
}
private void handleStopPrinterStateTracking(final PrinterId printerId) {
@@ -672,96 +674,6 @@ final class RemotePrintService implements DeathRecipient {
}
}
private final class MyHandler extends Handler {
public static final int MSG_CREATE_PRINTER_DISCOVERY_SESSION = 1;
public static final int MSG_DESTROY_PRINTER_DISCOVERY_SESSION = 2;
public static final int MSG_START_PRINTER_DISCOVERY = 3;
public static final int MSG_STOP_PRINTER_DISCOVERY = 4;
public static final int MSG_VALIDATE_PRINTERS = 5;
public static final int MSG_START_PRINTER_STATE_TRACKING = 6;
public static final int MSG_STOP_PRINTER_STATE_TRACKING = 7;
public static final int MSG_ON_ALL_PRINT_JOBS_HANDLED = 8;
public static final int MSG_ON_REQUEST_CANCEL_PRINT_JOB = 9;
public static final int MSG_ON_PRINT_JOB_QUEUED = 10;
public static final int MSG_DESTROY = 11;
public static final int MSG_BINDER_DIED = 12;
public static final int MSG_REQUEST_CUSTOM_PRINTER_ICON = 13;
public MyHandler(Looper looper) {
super(looper, null, false);
}
@Override
@SuppressWarnings("unchecked")
public void handleMessage(Message message) {
if (mDestroyed) {
Slog.w(LOG_TAG, "Not handling " + message + " as service for " + mComponentName
+ " is already destroyed");
return;
}
switch (message.what) {
case MSG_CREATE_PRINTER_DISCOVERY_SESSION: {
handleCreatePrinterDiscoverySession();
} break;
case MSG_DESTROY_PRINTER_DISCOVERY_SESSION: {
handleDestroyPrinterDiscoverySession();
} break;
case MSG_START_PRINTER_DISCOVERY: {
List<PrinterId> priorityList = (ArrayList<PrinterId>) message.obj;
handleStartPrinterDiscovery(priorityList);
} break;
case MSG_STOP_PRINTER_DISCOVERY: {
handleStopPrinterDiscovery();
} break;
case MSG_VALIDATE_PRINTERS: {
List<PrinterId> printerIds = (List<PrinterId>) message.obj;
handleValidatePrinters(printerIds);
} break;
case MSG_START_PRINTER_STATE_TRACKING: {
PrinterId printerId = (PrinterId) message.obj;
handleStartPrinterStateTracking(printerId);
} break;
case MSG_STOP_PRINTER_STATE_TRACKING: {
PrinterId printerId = (PrinterId) message.obj;
handleStopPrinterStateTracking(printerId);
} break;
case MSG_ON_ALL_PRINT_JOBS_HANDLED: {
handleOnAllPrintJobsHandled();
} break;
case MSG_ON_REQUEST_CANCEL_PRINT_JOB: {
PrintJobInfo printJob = (PrintJobInfo) message.obj;
handleRequestCancelPrintJob(printJob);
} break;
case MSG_ON_PRINT_JOB_QUEUED: {
PrintJobInfo printJob = (PrintJobInfo) message.obj;
handleOnPrintJobQueued(printJob);
} break;
case MSG_DESTROY: {
handleDestroy();
} break;
case MSG_BINDER_DIED: {
handleBinderDied();
} break;
case MSG_REQUEST_CUSTOM_PRINTER_ICON: {
PrinterId printerId = (PrinterId) message.obj;
handleRequestCustomPrinterIcon(printerId);
} break;
}
}
}
private static final class RemotePrintServiceClient extends IPrintServiceClient.Stub {
private final WeakReference<RemotePrintService> mWeakService;

View File

@@ -45,7 +45,6 @@ import android.os.IBinder;
import android.os.IBinder.DeathRecipient;
import android.os.IInterface;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.os.UserHandle;
@@ -81,6 +80,7 @@ import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.os.BackgroundThread;
import com.android.internal.util.dump.DualDumpOutputStream;
import com.android.internal.util.function.pooled.PooledLambda;
import com.android.server.print.RemotePrintService.PrintServiceCallbacks;
import com.android.server.print.RemotePrintServiceRecommendationService
.RemotePrintServiceRecommendationServiceCallbacks;
@@ -93,6 +93,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.IntSupplier;
/**
* Represents the print state for a user.
@@ -134,8 +135,6 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks,
private final RemotePrintSpooler mSpooler;
private final Handler mHandler;
private PrinterDiscoverySessionMediator mPrinterDiscoverySession;
private List<PrintJobStateChangeListenerRecord> mPrintJobStateChangeListenerRecords;
@@ -161,7 +160,6 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks,
mUserId = userId;
mLock = lock;
mSpooler = new RemotePrintSpooler(context, userId, lowPriority, this);
mHandler = new UserStateHandler(context.getMainLooper());
synchronized (mLock) {
readInstalledPrintServicesLocked();
@@ -172,9 +170,7 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks,
// Some print services might have gotten installed before the User State came up
prunePrintServices();
synchronized (mLock) {
onConfigurationChangedLocked();
}
onConfigurationChanged();
}
public void increasePriority() {
@@ -695,18 +691,22 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks,
@Override
public void onPrintJobStateChanged(PrintJobInfo printJob) {
mPrintJobForAppCache.onPrintJobStateChanged(printJob);
mHandler.obtainMessage(UserStateHandler.MSG_DISPATCH_PRINT_JOB_STATE_CHANGED,
printJob.getAppId(), 0, printJob.getId()).sendToTarget();
Handler.getMain().sendMessage(obtainMessage(
UserState::handleDispatchPrintJobStateChanged,
this, printJob.getId(),
PooledLambda.obtainSupplier(printJob.getAppId()).recycleOnUse()));
}
public void onPrintServicesChanged() {
mHandler.obtainMessage(UserStateHandler.MSG_DISPATCH_PRINT_SERVICES_CHANGED).sendToTarget();
Handler.getMain().sendMessage(obtainMessage(
UserState::handleDispatchPrintServicesChanged, this));
}
@Override
public void onPrintServiceRecommendationsUpdated(List<RecommendationInfo> recommendations) {
mHandler.obtainMessage(UserStateHandler.MSG_DISPATCH_PRINT_SERVICES_RECOMMENDATIONS_UPDATED,
0, 0, recommendations).sendToTarget();
Handler.getMain().sendMessage(obtainMessage(
UserState::handleDispatchPrintServiceRecommendationsUpdated,
this, recommendations));
}
@Override
@@ -771,8 +771,8 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks,
mActiveServices.remove(service.getComponentName());
// The service might need to be restarted if it died because of an update
mHandler.sendMessageDelayed(
mHandler.obtainMessage(UserStateHandler.MSG_CHECK_CONFIG_CHANGED),
Handler.getMain().sendMessageDelayed(obtainMessage(
UserState::onConfigurationChanged, this),
SERVICE_RESTART_DELAY_MILLIS);
// No session - nothing to do.
@@ -1112,24 +1112,26 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks,
}
}
private void handleDispatchPrintJobStateChanged(PrintJobId printJobId, int appId) {
private void handleDispatchPrintJobStateChanged(
PrintJobId printJobId, IntSupplier appIdSupplier) {
int appId = appIdSupplier.getAsInt();
final List<PrintJobStateChangeListenerRecord> records;
synchronized (mLock) {
if (mPrintJobStateChangeListenerRecords == null) {
return;
}
records = new ArrayList<PrintJobStateChangeListenerRecord>(
mPrintJobStateChangeListenerRecords);
records = new ArrayList<>(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);
|| record.appId == appId) {
try {
record.listener.onPrintJobStateChanged(printJobId);
} catch (RemoteException re) {
Log.e(LOG_TAG, "Error notifying for print job state change", re);
}
}
}
}
@@ -1177,38 +1179,9 @@ final class UserState implements PrintSpoolerCallbacks, PrintServiceCallbacks,
}
}
private final class UserStateHandler extends Handler {
public static final int MSG_DISPATCH_PRINT_JOB_STATE_CHANGED = 1;
public static final int MSG_DISPATCH_PRINT_SERVICES_CHANGED = 2;
public static final int MSG_DISPATCH_PRINT_SERVICES_RECOMMENDATIONS_UPDATED = 3;
public static final int MSG_CHECK_CONFIG_CHANGED = 4;
public UserStateHandler(Looper looper) {
super(looper, null, false);
}
@Override
public void handleMessage(Message message) {
switch (message.what) {
case MSG_DISPATCH_PRINT_JOB_STATE_CHANGED:
PrintJobId printJobId = (PrintJobId) message.obj;
final int appId = message.arg1;
handleDispatchPrintJobStateChanged(printJobId, appId);
break;
case MSG_DISPATCH_PRINT_SERVICES_CHANGED:
handleDispatchPrintServicesChanged();
break;
case MSG_DISPATCH_PRINT_SERVICES_RECOMMENDATIONS_UPDATED:
handleDispatchPrintServiceRecommendationsUpdated(
(List<RecommendationInfo>) message.obj);
break;
case MSG_CHECK_CONFIG_CHANGED:
synchronized (mLock) {
onConfigurationChangedLocked();
}
default:
// not reached
}
private void onConfigurationChanged() {
synchronized (mLock) {
onConfigurationChangedLocked();
}
}