Search in sources :

Example 1 with PrinterId

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

the class PrinterDiscoverySession method addPrinters.

/**
     * Adds discovered printers. Adding an already added printer updates it.
     * Removed printers can be added again. You can call this method multiple
     * times during the life of this session. Duplicates will be ignored.
     * <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 printers The printers to add.
     *
     * @see #removePrinters(List)
     * @see #getPrinters()
     * @see #isDestroyed()
     */
public final void addPrinters(@NonNull List<PrinterInfo> printers) {
    PrintService.throwIfNotCalledOnMainThread();
    // If the session is destroyed - nothing do to.
    if (mIsDestroyed) {
        Log.w(LOG_TAG, "Not adding printers - session destroyed.");
        return;
    }
    if (mIsDiscoveryStarted) {
        // If during discovery, add the new printers and send them.
        List<PrinterInfo> addedPrinters = null;
        final int addedPrinterCount = printers.size();
        for (int i = 0; i < addedPrinterCount; i++) {
            PrinterInfo addedPrinter = printers.get(i);
            PrinterInfo oldPrinter = mPrinters.put(addedPrinter.getId(), addedPrinter);
            if (oldPrinter == null || !oldPrinter.equals(addedPrinter)) {
                if (addedPrinters == null) {
                    addedPrinters = new ArrayList<PrinterInfo>();
                }
                addedPrinters.add(addedPrinter);
            }
        }
        // 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);
            }
        }
    } else {
        // Remember the last sent printers if needed.
        if (mLastSentPrinters == null) {
            mLastSentPrinters = new ArrayMap<PrinterId, PrinterInfo>(mPrinters);
        }
        // Update the printers.
        final int addedPrinterCount = printers.size();
        for (int i = 0; i < addedPrinterCount; i++) {
            PrinterInfo addedPrinter = printers.get(i);
            if (mPrinters.get(addedPrinter.getId()) == null) {
                mPrinters.put(addedPrinter.getId(), addedPrinter);
            }
        }
    }
}
Also used : PrinterId(android.print.PrinterId) PrinterInfo(android.print.PrinterInfo) RemoteException(android.os.RemoteException)

Example 2 with PrinterId

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

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 3 with PrinterId

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

the class FusedPrintersProvider method computeAndDeliverResult.

/**
     * Compute the printers, order them appropriately and deliver the printers to the clients. We
     * prefer printers that have been previously used (favorites) and printers that have been used
     * previously close to the current location (near printers).
     *
     * @param discoveredPrinters All printers currently discovered by the print discovery session.
     * @param favoritePrinters The ordered list of printers. The earlier in the list, the more
     *            preferred.
     */
private void computeAndDeliverResult(Map<PrinterId, PrinterInfo> discoveredPrinters, List<Pair<PrinterInfo, Location>> favoritePrinters) {
    List<PrinterInfo> printers = new ArrayList<>();
    // Store the printerIds that have already been added. We cannot compare the printerInfos in
    // "printers" as they might have been taken from discoveredPrinters and the printerInfo does
    // not equals() anymore
    HashSet<PrinterId> alreadyAddedPrinter = new HashSet<>(MAX_FAVORITE_PRINTER_COUNT);
    Location location = getCurrentLocation();
    // Add the favorite printers that have last been used close to the current location
    final int favoritePrinterCount = favoritePrinters.size();
    if (location != null) {
        for (int i = 0; i < favoritePrinterCount; i++) {
            // Only add a certain amount of favorite printers
            if (printers.size() == MAX_FAVORITE_PRINTER_COUNT) {
                break;
            }
            PrinterInfo favoritePrinter = favoritePrinters.get(i).first;
            Location printerLocation = favoritePrinters.get(i).second;
            if (printerLocation != null && !alreadyAddedPrinter.contains(favoritePrinter.getId())) {
                if (printerLocation.distanceTo(location) <= MAX_PRINTER_DISTANCE) {
                    updateAndAddPrinter(printers, favoritePrinter, discoveredPrinters);
                    alreadyAddedPrinter.add(favoritePrinter.getId());
                }
            }
        }
    }
    // Add the other favorite printers
    for (int i = 0; i < favoritePrinterCount; i++) {
        // Only add a certain amount of favorite printers
        if (printers.size() == MAX_FAVORITE_PRINTER_COUNT) {
            break;
        }
        PrinterInfo favoritePrinter = favoritePrinters.get(i).first;
        if (!alreadyAddedPrinter.contains(favoritePrinter.getId())) {
            updateAndAddPrinter(printers, favoritePrinter, discoveredPrinters);
            alreadyAddedPrinter.add(favoritePrinter.getId());
        }
    }
    // Add other updated printers. Printers that have already been added have been removed from
    // discoveredPrinters in the calls to updateAndAddPrinter
    final int printerCount = mPrinters.size();
    for (int i = 0; i < printerCount; i++) {
        PrinterInfo printer = mPrinters.get(i);
        PrinterInfo updatedPrinter = discoveredPrinters.remove(printer.getId());
        if (updatedPrinter != null) {
            printers.add(updatedPrinter);
        }
    }
    // Add the new printers, i.e. what is left.
    printers.addAll(discoveredPrinters.values());
    // Update the list of printers.
    mPrinters.clear();
    mPrinters.addAll(printers);
    if (isStarted()) {
        // If stated deliver the new printers.
        deliverResult(printers);
    } else {
        // Otherwise, take a note for the change.
        onContentChanged();
    }
}
Also used : ArrayList(java.util.ArrayList) PrinterId(android.print.PrinterId) PrinterInfo(android.print.PrinterInfo) HashSet(java.util.HashSet) Location(android.location.Location)

Example 4 with PrinterId

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

the class PrintSpoolerService method getPrintJobInfos.

public List<PrintJobInfo> getPrintJobInfos(ComponentName componentName, int state, int appId) {
    List<PrintJobInfo> foundPrintJobs = null;
    synchronized (mLock) {
        final int printJobCount = mPrintJobs.size();
        for (int i = 0; i < printJobCount; i++) {
            PrintJobInfo printJob = mPrintJobs.get(i);
            PrinterId printerId = printJob.getPrinterId();
            final boolean sameComponent = (componentName == null || (printerId != null && componentName.equals(printerId.getServiceName())));
            final boolean sameAppId = appId == PrintManager.APP_ID_ANY || printJob.getAppId() == appId;
            final boolean sameState = (state == printJob.getState()) || (state == PrintJobInfo.STATE_ANY) || (state == PrintJobInfo.STATE_ANY_VISIBLE_TO_CLIENTS && isStateVisibleToUser(printJob.getState())) || (state == PrintJobInfo.STATE_ANY_ACTIVE && isActiveState(printJob.getState())) || (state == PrintJobInfo.STATE_ANY_SCHEDULED && isScheduledState(printJob.getState()));
            if (sameComponent && sameAppId && sameState) {
                if (foundPrintJobs == null) {
                    foundPrintJobs = new ArrayList<>();
                }
                foundPrintJobs.add(printJob);
            }
        }
    }
    return foundPrintJobs;
}
Also used : PrintJobInfo(android.print.PrintJobInfo) PrinterId(android.print.PrinterId)

Example 5 with PrinterId

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

the class FusedPrintersProvider method loadInternal.

private void loadInternal() {
    if (mDiscoverySession == null) {
        PrintManager printManager = (PrintManager) getContext().getSystemService(Context.PRINT_SERVICE);
        mDiscoverySession = printManager.createPrinterDiscoverySession();
        mPersistenceManager.readPrinterHistory();
    } else if (mPersistenceManager.isHistoryChanged()) {
        mPersistenceManager.readPrinterHistory();
    }
    if (mPersistenceManager.isReadHistoryCompleted() && !mDiscoverySession.isPrinterDiscoveryStarted()) {
        mDiscoverySession.setOnPrintersChangeListener(new OnPrintersChangeListener() {

            @Override
            public void onPrintersChanged() {
                if (DEBUG) {
                    Log.i(LOG_TAG, "onPrintersChanged() count:" + mDiscoverySession.getPrinters().size() + " " + FusedPrintersProvider.this.hashCode());
                }
                updatePrinters(mDiscoverySession.getPrinters(), mFavoritePrinters, getCurrentLocation());
            }
        });
        final int favoriteCount = mFavoritePrinters.size();
        List<PrinterId> printerIds = new ArrayList<>(favoriteCount);
        for (int i = 0; i < favoriteCount; i++) {
            printerIds.add(mFavoritePrinters.get(i).first.getId());
        }
        mDiscoverySession.startPrinterDiscovery(printerIds);
        List<PrinterInfo> printers = mDiscoverySession.getPrinters();
        updatePrinters(printers, mFavoritePrinters, getCurrentLocation());
    }
}
Also used : OnPrintersChangeListener(android.print.PrinterDiscoverySession.OnPrintersChangeListener) ArrayList(java.util.ArrayList) PrinterId(android.print.PrinterId) PrintManager(android.print.PrintManager) 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