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

Commit 8f65cc36 authored by Philip P. Moltmann's avatar Philip P. Moltmann
Browse files

Use PrinterHashMap in MDNSFilteredDiscovery

Before MDNSFilteredDiscovery used the ip-addresses to remove printers,
but this did not work as once the nsd service is lost, you cannot resolve
it anymore. Hence do the same thing as the other plugins, and remove a
lost service if the name matches.

Also do not try back when the callbacks are already gone.

Fixes: 70622095
Test: Removed Wifi when print service recommendations were shown and saw
      all recommendations (including samsung) to go away. Without this
      change the samsung service never realized when printers went away.

Change-Id: I7371ef00d626738741d39a2f3669497bfc9fd080
parent c078e6b6
Loading
Loading
Loading
Loading
+10 −8
Original line number Diff line number Diff line
@@ -119,15 +119,17 @@ public abstract class RecommendationService extends Service {
                    mCallbacks = null;
                    break;
                case MSG_UPDATE:
                    // Note that there might be a connection change in progress. In this case the
                    // message is handled as before the change. This is acceptable as the caller of
                    // the connection change has not guarantee when the connection change binder
                    // transaction is actually processed.
                    if (mCallbacks != null) {
                        // Note that there might be a connection change in progress. In this case
                        // the message is handled as before the change. This is acceptable as the
                        // caller of the connection change has not guarantee when the connection
                        // change binder transaction is actually processed.
                        try {
                            mCallbacks.onRecommendationsUpdated((List<RecommendationInfo>) msg.obj);
                        } catch (RemoteException | NullPointerException e) {
                            Log.e(LOG_TAG, "Could not update recommended services", e);
                        }
                    }
                    break;
            }
        }
+0 −33
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.printservice.recommendation.plugin.hp;

import android.net.nsd.NsdServiceInfo;

import java.util.HashMap;

final class PrinterHashMap extends HashMap<String, NsdServiceInfo> {
    public static String getKey(NsdServiceInfo serviceInfo) {
        return serviceInfo.getServiceName();
    }
    public NsdServiceInfo addPrinter(NsdServiceInfo device) {
        return put(getKey(device), device);
    }
    public NsdServiceInfo removePrinter(NsdServiceInfo device) {
        return remove(getKey(device));
    }
}
+2 −3
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.text.TextUtils;

import com.android.printservice.recommendation.R;
import com.android.printservice.recommendation.util.DiscoveryListenerMultiplexer;
import com.android.printservice.recommendation.util.PrinterHashMap;

import java.net.InetAddress;
import java.util.ArrayList;
@@ -183,9 +184,7 @@ public class ServiceListener implements ServiceResolveQueue.ResolveCallback {
        ArrayList<InetAddress> printerAddressess = new ArrayList<>();

        for (PrinterHashMap oneVendorPrinters : mVendorHashMap.values()) {
            for (NsdServiceInfo printer : oneVendorPrinters.values()) {
                printerAddressess.add(printer.getHost());
            }
            printerAddressess.addAll(oneVendorPrinters.getPrinterAddresses());
        }

        return printerAddressess;
+2 −6
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.text.TextUtils;

import com.android.printservice.recommendation.util.DiscoveryListenerMultiplexer;
import com.android.printservice.recommendation.util.NsdResolveQueue;
import com.android.printservice.recommendation.util.PrinterHashMap;

import java.net.InetAddress;
import java.util.ArrayList;
@@ -195,12 +196,7 @@ class ServiceResolver {
    }

    public ArrayList<InetAddress> getPrinters() {
        ArrayList<InetAddress> printerAddresses = new ArrayList<>();
        for (NsdServiceInfo printer : mPrinterHashMap.values()) {
            printerAddresses.add(printer.getHost());
        }

        return printerAddresses;
        return mPrinterHashMap.getPrinterAddresses();
    }

}
+23 −32
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.printservice.recommendation.util;
import android.content.Context;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.GuardedBy;
@@ -27,9 +28,8 @@ import androidx.core.util.Preconditions;

import com.android.printservice.recommendation.PrintServicePlugin;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
@@ -55,9 +55,9 @@ public class MDNSFilteredDiscovery implements NsdManager.DiscoveryListener {
        boolean matchesCriteria(NsdServiceInfo nsdServiceInfo);
    }

    /** Printer identifiers of the mPrinters found. */
    /** Printers found. */
    @GuardedBy("mLock")
    private final @NonNull HashSet<InetAddress> mPrinters;
    private final @NonNull PrinterHashMap mPrinters;

    /** Service types discovered by this plugin */
    private final @NonNull HashSet<String> mServiceTypes;
@@ -97,7 +97,7 @@ public class MDNSFilteredDiscovery implements NsdManager.DiscoveryListener {
        mPrinterFilter = Preconditions.checkNotNull(printerFilter, "printerFilter");

        mResolveQueue = NsdResolveQueue.getInstance();
        mPrinters = new HashSet<>();
        mPrinters = new PrinterHashMap();
    }

    /**
@@ -107,6 +107,12 @@ public class MDNSFilteredDiscovery implements NsdManager.DiscoveryListener {
        return (NsdManager) mContext.getSystemService(Context.NSD_SERVICE);
    }

    private void onChanged() {
        if (mCallback != null) {
            mCallback.onChanged(mPrinters.getPrinterAddresses());
        }
    }

    /**
     * Start the discovery.
     *
@@ -114,7 +120,8 @@ public class MDNSFilteredDiscovery implements NsdManager.DiscoveryListener {
     */
    public void start(@NonNull PrintServicePlugin.PrinterDiscoveryCallback callback) {
        mCallback = callback;
        mCallback.onChanged(new ArrayList<>(mPrinters));

        onChanged();

        for (String serviceType : mServiceTypes) {
            DiscoveryListenerMultiplexer.addListener(getNDSManager(), serviceType, this);
@@ -167,11 +174,12 @@ public class MDNSFilteredDiscovery implements NsdManager.DiscoveryListener {

                    @Override
                    public void onServiceResolved(NsdServiceInfo serviceInfo) {
                        if (mPrinterFilter.matchesCriteria(serviceInfo)) {
                        if (!TextUtils.isEmpty(PrinterHashMap.getKey(serviceInfo))
                                && mPrinterFilter.matchesCriteria(serviceInfo)) {
                            if (mCallback != null) {
                                boolean added = mPrinters.add(serviceInfo.getHost());
                                if (added) {
                                    mCallback.onChanged(new ArrayList<>(mPrinters));
                                NsdServiceInfo old = mPrinters.addPrinter(serviceInfo);
                                if (!Objects.equals(old, serviceInfo)) {
                                    onChanged();
                                }
                            }
                        }
@@ -181,26 +189,9 @@ public class MDNSFilteredDiscovery implements NsdManager.DiscoveryListener {

    @Override
    public void onServiceLost(NsdServiceInfo serviceInfo) {
        mResolveQueue.resolve(getNDSManager(), serviceInfo,
                new NsdManager.ResolveListener() {
                    @Override
                    public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
                        Log.w(LOG_TAG, "Service lost: Could not resolve " + serviceInfo + ": "
                                + errorCode);
        NsdServiceInfo oldAddress = mPrinters.removePrinter(serviceInfo);
        if (oldAddress != null) {
            onChanged();
        }

                    @Override
                    public void onServiceResolved(NsdServiceInfo serviceInfo) {
                        if (mPrinterFilter.matchesCriteria(serviceInfo)) {
                            if (mCallback != null) {
                                boolean removed = mPrinters.remove(serviceInfo.getHost());

                                if (removed) {
                                    mCallback.onChanged(new ArrayList<>(mPrinters));
                                }
                            }
                        }
                    }
                });
    }
}
Loading