163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann/*
263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * Copyright (C) 2016 The Android Open Source Project
363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann *
463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * Licensed under the Apache License, Version 2.0 (the "License");
563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * you may not use this file except in compliance with the License.
663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * You may obtain a copy of the License at
763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann *
863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann *      http://www.apache.org/licenses/LICENSE-2.0
963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann *
1063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * Unless required by applicable law or agreed to in writing, software
1163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * distributed under the License is distributed on an "AS IS" BASIS,
1263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * See the License for the specific language governing permissions and
1463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann * limitations under the License.
1563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann */
1663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmannpackage com.android.printservice.recommendation.plugin.xerox;
1763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
1863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmannimport android.content.Context;
1963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmannimport android.net.nsd.NsdManager;
2063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmannimport android.annotation.NonNull;
2163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmannimport com.android.printservice.recommendation.PrintServicePlugin;
2263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
2363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmannimport com.android.printservice.recommendation.R;
2463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
2563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmannpublic class XeroxPrintServiceRecommendationPlugin implements PrintServicePlugin, ServiceResolver.Observer {
2663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
2763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    protected final Object mLock = new Object();
2863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    protected PrinterDiscoveryCallback mDiscoveryCallback = null;
2963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    protected final ServiceResolver mServiceResolver;
3063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    protected final NsdManager mNSDManager;
3163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    protected final VendorInfo mVendorInfo;
3263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    private final int mVendorStringID = R.string.plugin_vendor_xerox;
3363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    private final String PDL__PDF = "application/pdf";
3463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    private final String[] mServices = new String[]{"_ipp._tcp"};
3563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
3663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public XeroxPrintServiceRecommendationPlugin(Context context) {
3763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        mNSDManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
3863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        mVendorInfo = new VendorInfo(context.getResources(), R.array.known_print_vendor_info_for_xerox);
3963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        mServiceResolver = new ServiceResolver(context, this, mVendorInfo, mServices, new String[]{PDL__PDF});
4063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    }
4163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
4263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    @Override
4363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public int getName() {
4463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        return mVendorStringID;
4563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    }
4663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
4763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    @NonNull
4863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    @Override
4963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public CharSequence getPackageName() {
5063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        return mVendorInfo.mPackageName;
5163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    }
5263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
5363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    @Override
5463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public void start(@NonNull PrinterDiscoveryCallback callback) throws Exception {
5563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        synchronized (mLock) {
5663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann            mDiscoveryCallback = callback;
5763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann            mServiceResolver.start();
5863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        }
5963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    }
6063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
6163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    @Override
6263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public void stop() throws Exception {
6363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        synchronized (mLock) {
6463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann            mDiscoveryCallback = null;
6563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann            mServiceResolver.stop();
6663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        }
6763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    }
6863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
6963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    @Override
7063498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public void dataSetChanged() {
7163498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        synchronized (mLock) {
7263498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann            if (mDiscoveryCallback != null) mDiscoveryCallback.onChanged(getCount());
7363498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        }
7463498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    }
7563498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann
7663498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    public int getCount() {
7763498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann        return mServiceResolver.getCount();
7863498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann    }
7963498afff8079a5b70d763688d634ecdf7086f72Philip P. Moltmann}
80