Search in sources :

Example 26 with PrinterId

use of android.print.PrinterId in project android_frameworks_base by AOSPA.

the class PrinterDiscoverySession method removePrinters.

/**
     * Removes added printers. Removing an already removed or never added
     * printer has no effect. Removed printers can be added again. You can
     * call this method multiple times during the lifetime of this session.
     * <p>
     * <strong>Note: </strong> Calls to this method after the session is
     * destroyed, that is after the {@link #onDestroy()} callback, will be ignored.
     * </p>
     *
     * @param printerIds The ids of the removed printers.
     *
     * @see #addPrinters(List)
     * @see #getPrinters()
     * @see #isDestroyed()
     */
public final void removePrinters(@NonNull List<PrinterId> printerIds) {
    PrintService.throwIfNotCalledOnMainThread();
    // If the session is destroyed - nothing do to.
    if (mIsDestroyed) {
        Log.w(LOG_TAG, "Not removing printers - session destroyed.");
        return;
    }
    if (mIsDiscoveryStarted) {
        // If during discovery, remove existing printers and send them.
        List<PrinterId> removedPrinterIds = new ArrayList<PrinterId>();
        final int removedPrinterIdCount = printerIds.size();
        for (int i = 0; i < removedPrinterIdCount; i++) {
            PrinterId removedPrinterId = printerIds.get(i);
            if (mPrinters.remove(removedPrinterId) != null) {
                removedPrinterIds.add(removedPrinterId);
            }
        }
        // Send the removed printers, if such.
        if (!removedPrinterIds.isEmpty()) {
            try {
                mObserver.onPrintersRemoved(new ParceledListSlice<PrinterId>(removedPrinterIds));
            } catch (RemoteException re) {
                Log.e(LOG_TAG, "Error sending removed printers", re);
            }
        }
    } else {
        // Remember the last sent printers if needed.
        if (mLastSentPrinters == null) {
            mLastSentPrinters = new ArrayMap<PrinterId, PrinterInfo>(mPrinters);
        }
        // Update the printers.
        final int removedPrinterIdCount = printerIds.size();
        for (int i = 0; i < removedPrinterIdCount; i++) {
            PrinterId removedPrinterId = printerIds.get(i);
            mPrinters.remove(removedPrinterId);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) PrinterId(android.print.PrinterId) RemoteException(android.os.RemoteException) PrinterInfo(android.print.PrinterInfo)

Example 27 with PrinterId

use of android.print.PrinterId in project android_frameworks_base by AOSPA.

the class PrinterDiscoverySession method sendOutOfDiscoveryPeriodPrinterChanges.

private void sendOutOfDiscoveryPeriodPrinterChanges() {
    // Noting changed since the last discovery period - nothing to do.
    if (mLastSentPrinters == null || mLastSentPrinters.isEmpty()) {
        mLastSentPrinters = null;
        return;
    }
    // Determine the added printers.
    List<PrinterInfo> addedPrinters = null;
    for (PrinterInfo printer : mPrinters.values()) {
        PrinterInfo sentPrinter = mLastSentPrinters.get(printer.getId());
        if (sentPrinter == null || !sentPrinter.equals(printer)) {
            if (addedPrinters == null) {
                addedPrinters = new ArrayList<PrinterInfo>();
            }
            addedPrinters.add(printer);
        }
    }
    // Send the added printers, if such.
    if (addedPrinters != null) {
        try {
            mObserver.onPrintersAdded(new ParceledListSlice<PrinterInfo>(addedPrinters));
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error sending added printers", re);
        }
    }
    // Determine the removed printers.
    List<PrinterId> removedPrinterIds = null;
    for (PrinterInfo sentPrinter : mLastSentPrinters.values()) {
        if (!mPrinters.containsKey(sentPrinter.getId())) {
            if (removedPrinterIds == null) {
                removedPrinterIds = new ArrayList<PrinterId>();
            }
            removedPrinterIds.add(sentPrinter.getId());
        }
    }
    // Send the removed printers, if such.
    if (removedPrinterIds != null) {
        try {
            mObserver.onPrintersRemoved(new ParceledListSlice<PrinterId>(removedPrinterIds));
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error sending removed printers", re);
        }
    }
    mLastSentPrinters = null;
}
Also used : PrinterId(android.print.PrinterId) PrinterInfo(android.print.PrinterInfo) RemoteException(android.os.RemoteException)

Example 28 with PrinterId

use of android.print.PrinterId in project android_frameworks_base by DirtyUnicorns.

the class PrintService method generatePrinterId.

/**
     * Generates a global printer id given the printer's locally unique one.
     *
     * @param localId A locally unique id in the context of your print service.
     * @return Global printer id.
     */
@NonNull
public final PrinterId generatePrinterId(String localId) {
    throwIfNotCalledOnMainThread();
    localId = Preconditions.checkNotNull(localId, "localId cannot be null");
    return new PrinterId(new ComponentName(getPackageName(), getClass().getName()), localId);
}
Also used : ComponentName(android.content.ComponentName) PrinterId(android.print.PrinterId) NonNull(android.annotation.NonNull)

Example 29 with PrinterId

use of android.print.PrinterId in project android_frameworks_base by DirtyUnicorns.

the class PrinterDiscoverySession method sendOutOfDiscoveryPeriodPrinterChanges.

private void sendOutOfDiscoveryPeriodPrinterChanges() {
    // Noting changed since the last discovery period - nothing to do.
    if (mLastSentPrinters == null || mLastSentPrinters.isEmpty()) {
        mLastSentPrinters = null;
        return;
    }
    // Determine the added printers.
    List<PrinterInfo> addedPrinters = null;
    for (PrinterInfo printer : mPrinters.values()) {
        PrinterInfo sentPrinter = mLastSentPrinters.get(printer.getId());
        if (sentPrinter == null || !sentPrinter.equals(printer)) {
            if (addedPrinters == null) {
                addedPrinters = new ArrayList<PrinterInfo>();
            }
            addedPrinters.add(printer);
        }
    }
    // Send the added printers, if such.
    if (addedPrinters != null) {
        try {
            mObserver.onPrintersAdded(new ParceledListSlice<PrinterInfo>(addedPrinters));
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error sending added printers", re);
        }
    }
    // Determine the removed printers.
    List<PrinterId> removedPrinterIds = null;
    for (PrinterInfo sentPrinter : mLastSentPrinters.values()) {
        if (!mPrinters.containsKey(sentPrinter.getId())) {
            if (removedPrinterIds == null) {
                removedPrinterIds = new ArrayList<PrinterId>();
            }
            removedPrinterIds.add(sentPrinter.getId());
        }
    }
    // Send the removed printers, if such.
    if (removedPrinterIds != null) {
        try {
            mObserver.onPrintersRemoved(new ParceledListSlice<PrinterId>(removedPrinterIds));
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error sending removed printers", re);
        }
    }
    mLastSentPrinters = null;
}
Also used : PrinterId(android.print.PrinterId) PrinterInfo(android.print.PrinterInfo) RemoteException(android.os.RemoteException)

Example 30 with PrinterId

use of android.print.PrinterId in project android_frameworks_base by DirtyUnicorns.

the class PrinterDiscoverySession method removePrinters.

/**
     * Removes added printers. Removing an already removed or never added
     * printer has no effect. Removed printers can be added again. You can
     * call this method multiple times during the lifetime of this session.
     * <p>
     * <strong>Note: </strong> Calls to this method after the session is
     * destroyed, that is after the {@link #onDestroy()} callback, will be ignored.
     * </p>
     *
     * @param printerIds The ids of the removed printers.
     *
     * @see #addPrinters(List)
     * @see #getPrinters()
     * @see #isDestroyed()
     */
public final void removePrinters(@NonNull List<PrinterId> printerIds) {
    PrintService.throwIfNotCalledOnMainThread();
    // If the session is destroyed - nothing do to.
    if (mIsDestroyed) {
        Log.w(LOG_TAG, "Not removing printers - session destroyed.");
        return;
    }
    if (mIsDiscoveryStarted) {
        // If during discovery, remove existing printers and send them.
        List<PrinterId> removedPrinterIds = new ArrayList<PrinterId>();
        final int removedPrinterIdCount = printerIds.size();
        for (int i = 0; i < removedPrinterIdCount; i++) {
            PrinterId removedPrinterId = printerIds.get(i);
            if (mPrinters.remove(removedPrinterId) != null) {
                removedPrinterIds.add(removedPrinterId);
            }
        }
        // Send the removed printers, if such.
        if (!removedPrinterIds.isEmpty()) {
            try {
                mObserver.onPrintersRemoved(new ParceledListSlice<PrinterId>(removedPrinterIds));
            } catch (RemoteException re) {
                Log.e(LOG_TAG, "Error sending removed printers", re);
            }
        }
    } else {
        // Remember the last sent printers if needed.
        if (mLastSentPrinters == null) {
            mLastSentPrinters = new ArrayMap<PrinterId, PrinterInfo>(mPrinters);
        }
        // Update the printers.
        final int removedPrinterIdCount = printerIds.size();
        for (int i = 0; i < removedPrinterIdCount; i++) {
            PrinterId removedPrinterId = printerIds.get(i);
            mPrinters.remove(removedPrinterId);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) PrinterId(android.print.PrinterId) RemoteException(android.os.RemoteException) PrinterInfo(android.print.PrinterInfo)

Aggregations

PrinterId (android.print.PrinterId)49 PrinterInfo (android.print.PrinterInfo)30 RemoteException (android.os.RemoteException)15 ArrayList (java.util.ArrayList)15 ComponentName (android.content.ComponentName)14 PrintJobInfo (android.print.PrintJobInfo)14 NonNull (android.annotation.NonNull)5 Location (android.location.Location)5 PrintManager (android.print.PrintManager)5 OnPrintersChangeListener (android.print.PrinterDiscoverySession.OnPrintersChangeListener)5 HashSet (java.util.HashSet)5 LinkedHashMap (java.util.LinkedHashMap)5