blob: 9d15f3054ee26ba580378542a51e45bc6769bba6 [file] [log] [blame]
Philip P. Moltmanna26b7752016-05-23 10:33:57 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.printservice.recommendation.plugin.samsung;
18
19import android.content.Context;
20import android.net.nsd.NsdManager;
21import android.net.nsd.NsdServiceInfo;
22import android.annotation.NonNull;
23import android.text.TextUtils;
24import com.android.printservice.recommendation.PrintServicePlugin;
25
26public abstract class ServiceRecommendationPlugin implements PrintServicePlugin, ServiceListener.Observer {
27
28 protected static final String PDL_ATTRIBUTE = "pdl";
29
30 protected final Object mLock = new Object();
31 protected PrinterDiscoveryCallback mCallback = null;
32 protected final ServiceListener mListener;
33 protected final NsdManager mNSDManager;
34 protected final VendorInfo mVendorInfo;
35 private final int mVendorStringID;
36
37 protected ServiceRecommendationPlugin(Context context, int vendorStringID, VendorInfo vendorInfo, String[] services) {
38 mNSDManager = (NsdManager)context.getSystemService(Context.NSD_SERVICE);
39 mVendorStringID = vendorStringID;
40 mVendorInfo = vendorInfo;
41 mListener = new ServiceListener(context, this, services);
42 }
43
44 @Override
45 public int getName() {
46 return mVendorStringID;
47 }
48
49 @NonNull
50 @Override
51 public CharSequence getPackageName() {
52 return mVendorInfo.mPackageName;
53 }
54
55 @Override
56 public void start(@NonNull PrinterDiscoveryCallback callback) throws Exception {
57 synchronized (mLock) {
58 mCallback = callback;
59 }
60 mListener.start();
61 }
62
63 @Override
64 public void stop() throws Exception {
65 synchronized (mLock) {
66 mCallback = null;
67 }
68 mListener.stop();
69 }
70
71 @Override
72 public void dataSetChanged() {
73 synchronized (mLock) {
74 if (mCallback != null) mCallback.onChanged(getCount());
75 }
76 }
77
78 @Override
79 public boolean matchesCriteria(String vendor, NsdServiceInfo nsdServiceInfo) {
80 return TextUtils.equals(vendor, mVendorInfo.mVendorID);
81 }
82
83 public int getCount() {
84 return mListener.getCount().second;
85 }
86}