summaryrefslogtreecommitdiff
path: root/services/print
diff options
context:
space:
mode:
authorTreeHugger Robot <treehugger-gerrit@google.com>2018-02-23 21:21:20 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2018-02-23 21:21:20 +0000
commit1dcd64578992a650f3134be2178694ce0b6ee9d8 (patch)
tree23dc811cb5d33f5250472bfe86cf228abe0cae06 /services/print
parent7509bb3f97feb1d15d4472b7112e6485ea453ee0 (diff)
parent9f35ca996432e960b77eb194975e2086d7c18aff (diff)
Merge "Use PooledLambda in print code"
Diffstat (limited to 'services/print')
-rw-r--r--services/print/java/com/android/server/print/RemotePrintService.java142
-rw-r--r--services/print/java/com/android/server/print/UserState.java81
2 files changed, 54 insertions, 169 deletions
diff --git a/services/print/java/com/android/server/print/RemotePrintService.java b/services/print/java/com/android/server/print/RemotePrintService.java
index f72d8eee7b9b..d4cbe7b1cf66 100644
--- a/services/print/java/com/android/server/print/RemotePrintService.java
+++ b/services/print/java/com/android/server/print/RemotePrintService.java
@@ -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;
diff --git a/services/print/java/com/android/server/print/UserState.java b/services/print/java/com/android/server/print/UserState.java
index 3c09cb193af3..62185d782cbc 100644
--- a/services/print/java/com/android/server/print/UserState.java
+++ b/services/print/java/com/android/server/print/UserState.java
@@ -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();
}
}