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
This commit is contained in:
Philip P. Moltmann
2018-04-25 11:54:08 -07:00
parent c078e6b6fc
commit 8f65cc365f
7 changed files with 128 additions and 116 deletions

View File

@@ -119,14 +119,16 @@ 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.
try {
mCallbacks.onRecommendationsUpdated((List<RecommendationInfo>) msg.obj);
} catch (RemoteException | NullPointerException e) {
Log.e(LOG_TAG, "Could not update recommended services", e);
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;
}

View File

@@ -1,33 +0,0 @@
/*
* 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));
}
}

View File

@@ -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;

View File

@@ -1,34 +0,0 @@
/*
* 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.xerox;
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));
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}
@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));
}
}
}
}
});
NsdServiceInfo oldAddress = mPrinters.removePrinter(serviceInfo);
if (oldAddress != null) {
onChanged();
}
}
}
}

View File

@@ -0,0 +1,91 @@
/*
* 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.util;
import android.net.nsd.NsdServiceInfo;
import android.util.ArrayMap;
import java.net.InetAddress;
import java.util.ArrayList;
/**
* Map to store {@link NsdServiceInfo} belonging to printers. If two infos have the same
* {@link PrinterHashMap#getKey(NsdServiceInfo) key} they are considered the same.
*/
public class PrinterHashMap {
private final ArrayMap<String, NsdServiceInfo> mPrinters = new ArrayMap<>();
/**
* Key uniquely identifying a printer.
*
* @param serviceInfo The service info describing the printer
*
* @return The key
*/
public static String getKey(NsdServiceInfo serviceInfo) {
return serviceInfo.getServiceName();
}
/**
* Add a printer.
*
* @param device The service info of the printer
*
* @return The service info of the printer that was previously registered for the same key
*/
public NsdServiceInfo addPrinter(NsdServiceInfo device) {
return mPrinters.put(getKey(device), device);
}
/**
* Remove a printer.
*
* @param device The service info of the printer
*
* @return The service info of the printer that was previously registered for the same key
*/
public NsdServiceInfo removePrinter(NsdServiceInfo device) {
return mPrinters.remove(getKey(device));
}
/**
* @return the addresses of printers
*/
public ArrayList<InetAddress> getPrinterAddresses() {
int numPrinters = mPrinters.size();
ArrayList<InetAddress> printerAddressess = new ArrayList<>(numPrinters);
for (int i = 0; i < numPrinters; i++) {
printerAddressess.add(mPrinters.valueAt(i).getHost());
}
return printerAddressess;
}
/**
* Remove all printers
*/
public void clear() {
mPrinters.clear();
}
/**
* @return {@code} true iff the map is empty
*/
public boolean isEmpty() {
return mPrinters.isEmpty();
}
}