Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 953c4143 authored by Svetoslav Ganov's avatar Svetoslav Ganov
Browse files

Missed signal in FusedPrintersLoader.

1. In the FusedPrintersLoader we start observing the printers on
   the device and if they change we send the result. If however,
   the printers are already loaded in our session (because it
   joined an ongoing printer discovery) and the pritners do not
   change, the loader never sends its result. Now we are registring
   the callback only after historical printers are loaded. We
   also immediately check after starting discovery whether the
   there are printers in the discovery session and if so deliver
   them.

2. Improved logging in the FusedPrintersLoader.

bug:10940712

Change-Id: Ieb9b897d64780742125b29309462dea3eda170a6
parent a53b8541
Loading
Loading
Loading
Loading
+40 −21
Original line number Diff line number Diff line
@@ -161,33 +161,48 @@ public class FusedPrintersProvider extends Loader<List<PrinterInfo>> {
            PrintManager printManager = (PrintManager) getContext()
                    .getSystemService(Context.PRINT_SERVICE);
            mDiscoverySession = printManager.createPrinterDiscoverySession();
            mPersistenceManager.readPrinterHistory();
        }
        if (mPersistenceManager.isReadHistoryCompleted()
                && !mDiscoverySession.isPrinterDiscoveryStarted()) {
            mDiscoverySession.setOnPrintersChangeListener(new OnPrintersChangeListener() {
                @Override
                public void onPrintersChanged() {
                    ArrayMap<PrinterId, PrinterInfo> printersMap =
                            new ArrayMap<PrinterId, PrinterInfo>();
                    List<PrinterInfo> printers = mDiscoverySession.getPrinters();
                    final int printerCount = printers.size();
                    for (int i = 0; i < printerCount; i++) {
                        PrinterInfo printer = printers.get(i);
                        printersMap.put(printer.getId(), printer);
                    if (DEBUG) {
                        Log.i(LOG_TAG, "onPrintersChanged() count:"
                                + mDiscoverySession.getPrinters().size()
                                + " " + FusedPrintersProvider.this.hashCode());
                    }
                    computeAndDeliverResult(printersMap);
                    updatePrinters(mDiscoverySession.getPrinters());
                }
            });
            mPersistenceManager.readPrinterHistory();
        }
        if (mPersistenceManager.isReadHistoryCompleted()
                && !mDiscoverySession.isPrinterDiscoveryStarted()) {
            final int favoriteCount = mFavoritePrinters.size();
            List<PrinterId> printerIds = new ArrayList<PrinterId>(favoriteCount);
            for (int i = 0; i < favoriteCount; i++) {
                printerIds.add(mFavoritePrinters.get(i).getId());
            }
            mDiscoverySession.startPrinterDisovery(printerIds);
            List<PrinterInfo> printers = mDiscoverySession.getPrinters();
            if (!printers.isEmpty()) {
                updatePrinters(printers);
            }
        }
    }

    private void updatePrinters(List<PrinterInfo> printers) {
        if (mPrinters.equals(printers)) {
            return;
        }
        ArrayMap<PrinterId, PrinterInfo> printersMap =
                new ArrayMap<PrinterId, PrinterInfo>();
        final int printerCount = printers.size();
        for (int i = 0; i < printerCount; i++) {
            PrinterInfo printer = printers.get(i);
            printersMap.put(printer.getId(), printer);
        }
        computeAndDeliverResult(printersMap);
    }

    @Override
    protected boolean onCancelLoad() {
        if (DEBUG) {
@@ -288,7 +303,8 @@ public class FusedPrintersProvider extends Loader<List<PrinterInfo>> {

        public void readPrinterHistory() {
            if (DEBUG) {
                Log.i(LOG_TAG, "read history started");
                Log.i(LOG_TAG, "read history started "
                        + FusedPrintersProvider.this.hashCode());
            }
            mReadHistoryInProgress = true;
            mReadTask = new ReadTask();
@@ -393,7 +409,10 @@ public class FusedPrintersProvider extends Loader<List<PrinterInfo>> {
                try {
                    in = mStatePersistFile.openRead();
                } catch (FileNotFoundException fnfe) {
                    Log.i(LOG_TAG, "No existing printer history.");
                    if (DEBUG) {
                        Log.i(LOG_TAG, "No existing printer history "
                                + FusedPrintersProvider.this.hashCode());
                    }
                    return new ArrayList<PrinterInfo>();
                }
                try {