1/*
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 */
16package com.android.printservice.recommendation.plugin.xerox;
17
18import android.content.Context;
19import android.net.nsd.NsdManager;
20import android.annotation.NonNull;
21import com.android.printservice.recommendation.PrintServicePlugin;
22
23import com.android.printservice.recommendation.R;
24
25public class XeroxPrintServiceRecommendationPlugin implements PrintServicePlugin, ServiceResolver.Observer {
26
27    protected final Object mLock = new Object();
28    protected PrinterDiscoveryCallback mDiscoveryCallback = null;
29    protected final ServiceResolver mServiceResolver;
30    protected final NsdManager mNSDManager;
31    protected final VendorInfo mVendorInfo;
32    private final int mVendorStringID = R.string.plugin_vendor_xerox;
33    private final String PDL__PDF = "application/pdf";
34    private final String[] mServices = new String[]{"_ipp._tcp"};
35
36    public XeroxPrintServiceRecommendationPlugin(Context context) {
37        mNSDManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
38        mVendorInfo = new VendorInfo(context.getResources(), R.array.known_print_vendor_info_for_xerox);
39        mServiceResolver = new ServiceResolver(context, this, mVendorInfo, mServices, new String[]{PDL__PDF});
40    }
41
42    @Override
43    public int getName() {
44        return mVendorStringID;
45    }
46
47    @NonNull
48    @Override
49    public CharSequence getPackageName() {
50        return mVendorInfo.mPackageName;
51    }
52
53    @Override
54    public void start(@NonNull PrinterDiscoveryCallback callback) throws Exception {
55        synchronized (mLock) {
56            mDiscoveryCallback = callback;
57            mServiceResolver.start();
58        }
59    }
60
61    @Override
62    public void stop() throws Exception {
63        synchronized (mLock) {
64            mDiscoveryCallback = null;
65            mServiceResolver.stop();
66        }
67    }
68
69    @Override
70    public void dataSetChanged() {
71        synchronized (mLock) {
72            if (mDiscoveryCallback != null) mDiscoveryCallback.onChanged(getCount());
73        }
74    }
75
76    public int getCount() {
77        return mServiceResolver.getCount();
78    }
79}
80