FileDocCategorySizeDatePackage
PrinterDiscoverySession.javaAPI DocAndroid 5.1 API10389Thu Mar 12 22:22:10 GMT 2015android.print

PrinterDiscoverySession

public final class PrinterDiscoverySession extends Object
hide

Fields Summary
private static final String
LOG_TAG
private static final int
MSG_PRINTERS_ADDED
private static final int
MSG_PRINTERS_REMOVED
private final LinkedHashMap
mPrinters
private final IPrintManager
mPrintManager
private final int
mUserId
private final android.os.Handler
mHandler
private IPrinterDiscoveryObserver
mObserver
private OnPrintersChangeListener
mListener
private boolean
mIsPrinterDiscoveryStarted
Constructors Summary
PrinterDiscoverySession(IPrintManager printManager, android.content.Context context, int userId)


        
          
    

          
        mPrintManager = printManager;
        mUserId = userId;
        mHandler = new SessionHandler(context.getMainLooper());
        mObserver = new PrinterDiscoveryObserver(this);
        try {
            mPrintManager.createPrinterDiscoverySession(mObserver, mUserId);
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error creating printer discovery session", re);
        }
    
Methods Summary
public final voiddestroy()

        if (isDestroyed()) {
            Log.w(LOG_TAG, "Ignoring destroy - session destroyed");
        }
        destroyNoCheck();
    
private voiddestroyNoCheck()

        stopPrinterDiscovery();
        try {
            mPrintManager.destroyPrinterDiscoverySession(mObserver, mUserId);
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error destroying printer discovery session", re);
        } finally {
            mObserver = null;
            mPrinters.clear();
        }
    
protected final voidfinalize()

        if (!isDestroyedNoCheck()) {
            Log.e(LOG_TAG, "Destroying leaked printer discovery session");
            destroyNoCheck();
        }
        super.finalize();
    
public final java.util.ListgetPrinters()

        if (isDestroyed()) {
            Log.w(LOG_TAG, "Ignoring get printers - session destroyed");
            return Collections.emptyList();
        }
        return new ArrayList<PrinterInfo>(mPrinters.values());
    
private voidhandlePrintersAdded(java.util.List addedPrinters)

        if (isDestroyed()) {
            return;
        }

        // No old printers - do not bother keeping their position.
        if (mPrinters.isEmpty()) {
            final int printerCount = addedPrinters.size();
            for (int i = 0; i < printerCount; i++) {
                PrinterInfo printer = addedPrinters.get(i);
                mPrinters.put(printer.getId(), printer);
            }
            notifyOnPrintersChanged();
            return;
        }

        // Add the printers to a map.
        ArrayMap<PrinterId, PrinterInfo> addedPrintersMap =
                new ArrayMap<PrinterId, PrinterInfo>();
        final int printerCount = addedPrinters.size();
        for (int i = 0; i < printerCount; i++) {
            PrinterInfo printer = addedPrinters.get(i);
            addedPrintersMap.put(printer.getId(), printer);
        }

        // Update printers we already have.
        for (PrinterId oldPrinterId : mPrinters.keySet()) {
            PrinterInfo updatedPrinter = addedPrintersMap.remove(oldPrinterId);
            if (updatedPrinter != null) {
                mPrinters.put(oldPrinterId, updatedPrinter);
            }
        }

        // Add the new printers, i.e. what is left.
        mPrinters.putAll(addedPrintersMap);

        // Announce the change.
        notifyOnPrintersChanged();
    
private voidhandlePrintersRemoved(java.util.List printerIds)

        if (isDestroyed()) {
            return;
        }
        boolean printersChanged = false;
        final int removedPrinterIdCount = printerIds.size();
        for (int i = 0; i < removedPrinterIdCount; i++) {
            PrinterId removedPrinterId = printerIds.get(i);
            if (mPrinters.remove(removedPrinterId) != null) {
                printersChanged = true;
            }
        }
        if (printersChanged) {
            notifyOnPrintersChanged();
        }
    
public final booleanisDestroyed()

        throwIfNotCalledOnMainThread();
        return isDestroyedNoCheck();
    
private booleanisDestroyedNoCheck()

        return (mObserver == null);
    
public final booleanisPrinterDiscoveryStarted()

        throwIfNotCalledOnMainThread();
        return mIsPrinterDiscoveryStarted;
    
private voidnotifyOnPrintersChanged()

        if (mListener != null) {
            mListener.onPrintersChanged();
        }
    
public final voidsetOnPrintersChangeListener(android.print.PrinterDiscoverySession$OnPrintersChangeListener listener)

        throwIfNotCalledOnMainThread();
        mListener = listener;
    
public final voidstartPrinterDiscovery(java.util.List priorityList)

        if (isDestroyed()) {
            Log.w(LOG_TAG, "Ignoring start printers discovery - session destroyed");
            return;
        }
        if (!mIsPrinterDiscoveryStarted) {
            mIsPrinterDiscoveryStarted = true;
            try {
                mPrintManager.startPrinterDiscovery(mObserver, priorityList, mUserId);
            } catch (RemoteException re) {
                Log.e(LOG_TAG, "Error starting printer discovery", re);
            }
        }
    
public final voidstartPrinterStateTracking(PrinterId printerId)

        if (isDestroyed()) {
            Log.w(LOG_TAG, "Ignoring start printer state tracking - session destroyed");
            return;
        }
        try {
            mPrintManager.startPrinterStateTracking(printerId, mUserId);
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error starting printer state tracking", re);
        }
    
public final voidstopPrinterDiscovery()

        if (isDestroyed()) {
            Log.w(LOG_TAG, "Ignoring stop printers discovery - session destroyed");
            return;
        }
        if (mIsPrinterDiscoveryStarted) {
            mIsPrinterDiscoveryStarted = false;
            try {
                mPrintManager.stopPrinterDiscovery(mObserver, mUserId);
            } catch (RemoteException re) {
                Log.e(LOG_TAG, "Error stopping printer discovery", re);
            }
        }
    
public final voidstopPrinterStateTracking(PrinterId printerId)

        if (isDestroyed()) {
            Log.w(LOG_TAG, "Ignoring stop printer state tracking - session destroyed");
            return;
        }
        try {
            mPrintManager.stopPrinterStateTracking(printerId, mUserId);
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error stopping printer state tracking", re);
        }
    
private static voidthrowIfNotCalledOnMainThread()

        if (!Looper.getMainLooper().isCurrentThread()) {
            throw new IllegalAccessError("must be called from the main thread");
        }
    
public final voidvalidatePrinters(java.util.List printerIds)

        if (isDestroyed()) {
            Log.w(LOG_TAG, "Ignoring validate printers - session destroyed");
            return;
        }
        try {
            mPrintManager.validatePrinters(printerIds, mUserId);
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error validating printers", re);
        }