12d531653595344ed7e61c093dca308b9ba7ec185Svetoslav/*
22d531653595344ed7e61c093dca308b9ba7ec185Svetoslav * Copyright (C) 2013 The Android Open Source Project
32d531653595344ed7e61c093dca308b9ba7ec185Svetoslav *
42d531653595344ed7e61c093dca308b9ba7ec185Svetoslav * Licensed under the Apache License, Version 2.0 (the "License");
52d531653595344ed7e61c093dca308b9ba7ec185Svetoslav * you may not use this file except in compliance with the License.
62d531653595344ed7e61c093dca308b9ba7ec185Svetoslav * You may obtain a copy of the License at
72d531653595344ed7e61c093dca308b9ba7ec185Svetoslav *
82d531653595344ed7e61c093dca308b9ba7ec185Svetoslav *      http://www.apache.org/licenses/LICENSE-2.0
92d531653595344ed7e61c093dca308b9ba7ec185Svetoslav *
102d531653595344ed7e61c093dca308b9ba7ec185Svetoslav * Unless required by applicable law or agreed to in writing, software
112d531653595344ed7e61c093dca308b9ba7ec185Svetoslav * distributed under the License is distributed on an "AS IS" BASIS,
122d531653595344ed7e61c093dca308b9ba7ec185Svetoslav * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132d531653595344ed7e61c093dca308b9ba7ec185Svetoslav * See the License for the specific language governing permissions and
142d531653595344ed7e61c093dca308b9ba7ec185Svetoslav * limitations under the License.
152d531653595344ed7e61c093dca308b9ba7ec185Svetoslav */
162d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
172d531653595344ed7e61c093dca308b9ba7ec185Svetoslavpackage com.android.settings.print;
182d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
19bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.app.LoaderManager.LoaderCallbacks;
200ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onukiimport android.content.ActivityNotFoundException;
21bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.content.AsyncTaskLoader;
222d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.content.ComponentName;
232d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.content.Context;
242d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.content.Intent;
25bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.content.Loader;
262d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.content.pm.PackageManager;
270ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onukiimport android.graphics.drawable.Drawable;
282d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.net.Uri;
292d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.os.Bundle;
30bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.print.PrintJob;
31bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.print.PrintJobId;
32bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.print.PrintJobInfo;
33bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.print.PrintManager;
34bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.print.PrintManager.PrintJobStateChangeListener;
35d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmannimport android.print.PrintServicesLoader;
362d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.printservice.PrintServiceInfo;
37d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslavimport android.provider.SearchIndexableResource;
382d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.provider.Settings;
3901ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhangimport android.support.annotation.VisibleForTesting;
4039b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport android.support.v7.preference.Preference;
4139b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport android.support.v7.preference.PreferenceCategory;
422d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.text.TextUtils;
43bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.text.format.DateUtils;
44bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.util.Log;
4523176395a5413ad55eeaff780e60f36e7d70977ePhilip P. Moltmannimport android.view.LayoutInflater;
462d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.view.View;
470ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onukiimport android.view.View.OnClickListener;
48bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.view.ViewGroup;
490ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onukiimport android.widget.Button;
502d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.widget.TextView;
51738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann
52265d3c2a0c36251bf8a9f571d7239b6dd404d942Tamas Berghammerimport com.android.internal.logging.nano.MetricsProto.MetricsEvent;
532d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport com.android.settings.R;
54738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmannimport com.android.settings.dashboard.SummaryLoader;
55d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslavimport com.android.settings.search.BaseSearchIndexProvider;
567e920da6564032ddcc40db95925083ef59d19e58Fabrice Di Meglioimport com.android.settings.search.Indexable;
57d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslavimport com.android.settings.search.SearchIndexableRaw;
5810ad09b65824aa44a8cd1e541bb57f67190325a3Fan Zhangimport com.android.settings.utils.ProfileSettingsPreferenceFragment;
592d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
60bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport java.text.DateFormat;
61bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport java.util.ArrayList;
622d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport java.util.List;
632d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
642d531653595344ed7e61c093dca308b9ba7ec185Svetoslav/**
652d531653595344ed7e61c093dca308b9ba7ec185Svetoslav * Fragment with the top level print settings.
662d531653595344ed7e61c093dca308b9ba7ec185Svetoslav */
6732ccffc07c47d6e30b4198c74a8f2d2fbb8a799dTony Makpublic class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
6810ad09b65824aa44a8cd1e541bb57f67190325a3Fan Zhang        implements Indexable, OnClickListener {
690ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki    public static final String TAG = "PrintSettingsFragment";
70bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    private static final int LOADER_ID_PRINT_JOBS_LOADER = 1;
71d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann    private static final int LOADER_ID_PRINT_SERVICES = 2;
72bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
73bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    private static final String PRINT_JOBS_CATEGORY = "print_jobs_category";
74bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    private static final String PRINT_SERVICES_CATEGORY = "print_services_category";
75bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
76bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    static final String EXTRA_CHECKED = "EXTRA_CHECKED";
77bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    static final String EXTRA_TITLE = "EXTRA_TITLE";
78bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    static final String EXTRA_SERVICE_COMPONENT_NAME = "EXTRA_SERVICE_COMPONENT_NAME";
79bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
80bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    static final String EXTRA_PRINT_JOB_ID = "EXTRA_PRINT_JOB_ID";
812d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
820ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav    private static final String EXTRA_PRINT_SERVICE_COMPONENT_NAME =
830ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav            "EXTRA_PRINT_SERVICE_COMPONENT_NAME";
840ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav
85d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann    private static final int ORDER_LAST = Preference.DEFAULT_ORDER - 1;
862d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
87bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    private PreferenceCategory mActivePrintJobsCategory;
88bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    private PreferenceCategory mPrintServicesCategory;
89bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
90bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    private PrintJobsController mPrintJobsController;
91d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann    private PrintServicesController mPrintServicesController;
92d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann
930ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki    private Button mAddNewServiceButton;
94fe47a8dc89fe0393dae16b1c2152c901feab4dcbAlexandra Gherghina
95fe47a8dc89fe0393dae16b1c2152c901feab4dcbAlexandra Gherghina    @Override
966507613ebcd22e4691c2af92a5c161bd327db336Fan Zhang    public int getMetricsCategory() {
979d1bfd1e8de6e46137a9571507c03526880d6a46Chris Wren        return MetricsEvent.PRINT_SETTINGS;
988a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    }
998a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren
1008a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    @Override
10123acc2bb8af7da000d72a8c1d8c28a2792200348Jason Monk    protected int getHelpResource() {
10223acc2bb8af7da000d72a8c1d8c28a2792200348Jason Monk        return R.string.help_uri_printing;
10323acc2bb8af7da000d72a8c1d8c28a2792200348Jason Monk    }
10423acc2bb8af7da000d72a8c1d8c28a2792200348Jason Monk
10523acc2bb8af7da000d72a8c1d8c28a2792200348Jason Monk    @Override
10623176395a5413ad55eeaff780e60f36e7d70977ePhilip P. Moltmann    public View onCreateView(LayoutInflater inflater, ViewGroup container,
10723176395a5413ad55eeaff780e60f36e7d70977ePhilip P. Moltmann            Bundle savedInstanceState) {
10823176395a5413ad55eeaff780e60f36e7d70977ePhilip P. Moltmann        View root = super.onCreateView(inflater, container, savedInstanceState);
1092d531653595344ed7e61c093dca308b9ba7ec185Svetoslav        addPreferencesFromResource(R.xml.print_settings);
110bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
111bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        mActivePrintJobsCategory = (PreferenceCategory) findPreference(
112bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                PRINT_JOBS_CATEGORY);
113d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav        mPrintServicesCategory = (PreferenceCategory) findPreference(
114bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                PRINT_SERVICES_CATEGORY);
115bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        getPreferenceScreen().removePreference(mActivePrintJobsCategory);
116bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
117bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        mPrintJobsController = new PrintJobsController();
118eb01628ba897c116e434970309a4d5aff3fd03dbPhilip P. Moltmann        getLoaderManager().initLoader(LOADER_ID_PRINT_JOBS_LOADER, null, mPrintJobsController);
119d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann
120d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann        mPrintServicesController = new PrintServicesController();
121eb01628ba897c116e434970309a4d5aff3fd03dbPhilip P. Moltmann        getLoaderManager().initLoader(LOADER_ID_PRINT_SERVICES, null, mPrintServicesController);
12223176395a5413ad55eeaff780e60f36e7d70977ePhilip P. Moltmann
12323176395a5413ad55eeaff780e60f36e7d70977ePhilip P. Moltmann        return root;
1242d531653595344ed7e61c093dca308b9ba7ec185Svetoslav    }
1252d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
1262d531653595344ed7e61c093dca308b9ba7ec185Svetoslav    @Override
12799f0bd4aa6480f76fb7740ada8101bff84c1feb3Philip P. Moltmann    public void onStart() {
12899f0bd4aa6480f76fb7740ada8101bff84c1feb3Philip P. Moltmann        super.onStart();
1292d531653595344ed7e61c093dca308b9ba7ec185Svetoslav        setHasOptionsMenu(true);
130bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        startSubSettingsIfNeeded();
1312d531653595344ed7e61c093dca308b9ba7ec185Svetoslav    }
1322d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
1332d531653595344ed7e61c093dca308b9ba7ec185Svetoslav    @Override
13499f0bd4aa6480f76fb7740ada8101bff84c1feb3Philip P. Moltmann    public void onStop() {
13599f0bd4aa6480f76fb7740ada8101bff84c1feb3Philip P. Moltmann        super.onStop();
1362d531653595344ed7e61c093dca308b9ba7ec185Svetoslav    }
1372d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
1382d531653595344ed7e61c093dca308b9ba7ec185Svetoslav    @Override
139bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    public void onViewCreated(View view, Bundle savedInstanceState) {
140bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        super.onViewCreated(view, savedInstanceState);
141bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        ViewGroup contentRoot = (ViewGroup) getListView().getParent();
142bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        View emptyView = getActivity().getLayoutInflater().inflate(
143d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav                R.layout.empty_print_state, contentRoot, false);
144bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        TextView textView = (TextView) emptyView.findViewById(R.id.message);
145bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        textView.setText(R.string.print_no_services_installed);
1460ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki
1470ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        final Intent addNewServiceIntent = createAddNewServiceIntentOrNull();
1480ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        if (addNewServiceIntent != null) {
1490ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            mAddNewServiceButton = (Button) emptyView.findViewById(R.id.add_new_service);
1500ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            mAddNewServiceButton.setOnClickListener(this);
1510ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            // The empty is used elsewhere too so it's hidden by default.
1520ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            mAddNewServiceButton.setVisibility(View.VISIBLE);
1530ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        }
1540ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki
155bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        contentRoot.addView(emptyView);
15639b467482d1bf256a111c757e9b7621c6f523271Jason Monk        setEmptyView(emptyView);
15732ccffc07c47d6e30b4198c74a8f2d2fbb8a799dTony Mak    }
158fe47a8dc89fe0393dae16b1c2152c901feab4dcbAlexandra Gherghina
15932ccffc07c47d6e30b4198c74a8f2d2fbb8a799dTony Mak    @Override
16032ccffc07c47d6e30b4198c74a8f2d2fbb8a799dTony Mak    protected String getIntentActionString() {
16132ccffc07c47d6e30b4198c74a8f2d2fbb8a799dTony Mak        return Settings.ACTION_PRINT_SETTINGS;
162bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    }
163bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
164d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann    /**
165d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann     * Adds preferences for all print services to the {@value PRINT_SERVICES_CATEGORY} cathegory.
166d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann     */
167206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang    private final class PrintServicesController implements LoaderCallbacks<List<PrintServiceInfo>> {
168d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann        @Override
169d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann        public Loader<List<PrintServiceInfo>> onCreateLoader(int id, Bundle args) {
170d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            PrintManager printManager =
171d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                    (PrintManager) getContext().getSystemService(Context.PRINT_SERVICE);
172d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            if (printManager != null) {
173d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                return new PrintServicesLoader(printManager, getContext(),
174d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                        PrintManager.ALL_SERVICES);
175d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            } else {
176d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                return null;
177d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            }
178bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
1792d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
180d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann        @Override
181d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann        public void onLoadFinished(Loader<List<PrintServiceInfo>> loader,
182d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                List<PrintServiceInfo> services) {
183d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            if (services.isEmpty()) {
184d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                getPreferenceScreen().removePreference(mPrintServicesCategory);
185d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                return;
186d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            } else if (getPreferenceScreen().findPreference(PRINT_SERVICES_CATEGORY) == null) {
187d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                getPreferenceScreen().addPreference(mPrintServicesCategory);
188d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            }
1890ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki
190d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            mPrintServicesCategory.removeAll();
191d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            PackageManager pm = getActivity().getPackageManager();
19201ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang            final Context context = getPrefContext();
19301ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang            if (context == null) {
19401ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                Log.w(TAG, "No preference context, skip adding print services");
19501ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                return;
19601ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang            }
1972d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
19801ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang            for (PrintServiceInfo service : services) {
19901ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                Preference preference = new Preference(context);
2002d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
201d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                String title = service.getResolveInfo().loadLabel(pm).toString();
202d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                preference.setTitle(title);
2032d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
204d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                ComponentName componentName = service.getComponentName();
205d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                preference.setKey(componentName.flattenToString());
2062d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
207d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                preference.setFragment(PrintServiceSettingsFragment.class.getName());
208d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                preference.setPersistent(false);
2092d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
210d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                if (service.isEnabled()) {
211d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                    preference.setSummary(getString(R.string.print_feature_state_on));
212d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                } else {
213d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                    preference.setSummary(getString(R.string.print_feature_state_off));
214d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                }
2152d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
216d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                Drawable drawable = service.getResolveInfo().loadIcon(pm);
217d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                if (drawable != null) {
218d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                    preference.setIcon(drawable);
219d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                }
2202d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
221d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                Bundle extras = preference.getExtras();
222d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                extras.putBoolean(EXTRA_CHECKED, service.isEnabled());
223d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                extras.putString(EXTRA_TITLE, title);
224d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                extras.putString(EXTRA_SERVICE_COMPONENT_NAME, componentName.flattenToString());
2250ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki
226d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                mPrintServicesCategory.addPreference(preference);
2272d531653595344ed7e61c093dca308b9ba7ec185Svetoslav            }
2282d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
229d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            Preference addNewServicePreference = newAddServicePreferenceOrNull();
230d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            if (addNewServicePreference != null) {
231d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                mPrintServicesCategory.addPreference(addNewServicePreference);
2322d531653595344ed7e61c093dca308b9ba7ec185Svetoslav            }
2332d531653595344ed7e61c093dca308b9ba7ec185Svetoslav        }
2342d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
235d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann        @Override
236d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann        public void onLoaderReset(Loader<List<PrintServiceInfo>> loader) {
237bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            getPreferenceScreen().removePreference(mPrintServicesCategory);
2380ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        }
2390ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki    }
2400ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki
2410ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki    private Preference newAddServicePreferenceOrNull() {
2420ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        final Intent addNewServiceIntent = createAddNewServiceIntentOrNull();
2430ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        if (addNewServiceIntent == null) {
2440ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            return null;
2452d531653595344ed7e61c093dca308b9ba7ec185Svetoslav        }
24639b467482d1bf256a111c757e9b7621c6f523271Jason Monk        Preference preference = new Preference(getPrefContext());
2470ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        preference.setTitle(R.string.print_menu_item_add_service);
2480ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        preference.setIcon(R.drawable.ic_menu_add);
2490ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        preference.setOrder(ORDER_LAST);
2500ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        preference.setIntent(addNewServiceIntent);
2510ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        preference.setPersistent(false);
2520ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        return preference;
2530ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki    }
2540ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki
2550ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki    private Intent createAddNewServiceIntentOrNull() {
2560ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        final String searchUri = Settings.Secure.getString(getContentResolver(),
2570ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki                Settings.Secure.PRINT_SERVICE_SEARCH_URI);
2580ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        if (TextUtils.isEmpty(searchUri)) {
2590ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            return null;
2600ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        }
2610ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        return new Intent(Intent.ACTION_VIEW, Uri.parse(searchUri));
2622d531653595344ed7e61c093dca308b9ba7ec185Svetoslav    }
2632d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
264bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    private void startSubSettingsIfNeeded() {
2650ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav        if (getArguments() == null) {
2660ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav            return;
2670ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav        }
2680ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav        String componentName = getArguments().getString(EXTRA_PRINT_SERVICE_COMPONENT_NAME);
2690ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav        if (componentName != null) {
2700ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav            getArguments().remove(EXTRA_PRINT_SERVICE_COMPONENT_NAME);
2710ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav            Preference prereference = findPreference(componentName);
2720ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav            if (prereference != null) {
27339b467482d1bf256a111c757e9b7621c6f523271Jason Monk                prereference.performClick();
2740ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav            }
2750ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav        }
2760ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav    }
2770ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav
278fe47a8dc89fe0393dae16b1c2152c901feab4dcbAlexandra Gherghina    @Override
2790ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki    public void onClick(View v) {
2800ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        if (mAddNewServiceButton == v) {
2810ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            final Intent addNewServiceIntent = createAddNewServiceIntentOrNull();
2820ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            if (addNewServiceIntent != null) { // check again just in case.
2830ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki                try {
2840ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki                    startActivity(addNewServiceIntent);
2850ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki                } catch (ActivityNotFoundException e) {
2860ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki                    Log.w(TAG, "Unable to start activity", e);
2870ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki                }
2880ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            }
2890ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        }
2900ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki    }
2910ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki
292206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang    private final class PrintJobsController implements LoaderCallbacks<List<PrintJobInfo>> {
293bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
294bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
295bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        public Loader<List<PrintJobInfo>> onCreateLoader(int id, Bundle args) {
296bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (id == LOADER_ID_PRINT_JOBS_LOADER) {
297eb01628ba897c116e434970309a4d5aff3fd03dbPhilip P. Moltmann                return new PrintJobsLoader(getContext());
298bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
299bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            return null;
300bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
301bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
302bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
303bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        public void onLoadFinished(Loader<List<PrintJobInfo>> loader,
304bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                List<PrintJobInfo> printJobs) {
305bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (printJobs == null || printJobs.isEmpty()) {
306bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                getPreferenceScreen().removePreference(mActivePrintJobsCategory);
307bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            } else {
308bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                if (getPreferenceScreen().findPreference(PRINT_JOBS_CATEGORY) == null) {
309bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    getPreferenceScreen().addPreference(mActivePrintJobsCategory);
310bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                }
311bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
312bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                mActivePrintJobsCategory.removeAll();
31301ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                final Context context = getPrefContext();
31401ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                if (context == null) {
31501ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                    Log.w(TAG, "No preference context, skip adding print jobs");
31601ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                    return;
31701ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                }
318bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
31901ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                for (PrintJobInfo printJob : printJobs) {
32001ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                    Preference preference = new Preference(context);
321bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
322bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    preference.setPersistent(false);
323bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    preference.setFragment(PrintJobSettingsFragment.class.getName());
324bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    preference.setKey(printJob.getId().flattenToString());
325bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
326bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    switch (printJob.getState()) {
327bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                        case PrintJobInfo.STATE_QUEUED:
328206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                        case PrintJobInfo.STATE_STARTED:
32997e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                            if (!printJob.isCancelling()) {
33097e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                preference.setTitle(getString(
33197e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        R.string.print_printing_state_title_template,
33297e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        printJob.getLabel()));
33397e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                            } else {
33497e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                preference.setTitle(getString(
33597e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        R.string.print_cancelling_state_title_template,
33697e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        printJob.getLabel()));
33797e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                            }
338206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                            break;
339206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                        case PrintJobInfo.STATE_FAILED:
340bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                            preference.setTitle(getString(
341bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                                    R.string.print_failed_state_title_template,
342bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                                    printJob.getLabel()));
343206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                            break;
344206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                        case PrintJobInfo.STATE_BLOCKED:
34597e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                            if (!printJob.isCancelling()) {
34697e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                preference.setTitle(getString(
34797e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        R.string.print_blocked_state_title_template,
34897e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        printJob.getLabel()));
34997e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                            } else {
35097e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                preference.setTitle(getString(
35197e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        R.string.print_cancelling_state_title_template,
35297e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        printJob.getLabel()));
35397e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                            }
354206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                            break;
355bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    }
356bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
357bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    preference.setSummary(getString(R.string.print_job_summary,
358bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                            printJob.getPrinterName(), DateUtils.formatSameDayTime(
359bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                                    printJob.getCreationTime(), printJob.getCreationTime(),
360bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                                    DateFormat.SHORT, DateFormat.SHORT)));
361bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
362bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    switch (printJob.getState()) {
363bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                        case PrintJobInfo.STATE_QUEUED:
364206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                        case PrintJobInfo.STATE_STARTED:
365031e8b14322ec0a4ec3620de920fd5966290625cSvetoslav                            preference.setIcon(R.drawable.ic_print);
366206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                            break;
367bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                        case PrintJobInfo.STATE_FAILED:
368206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                        case PrintJobInfo.STATE_BLOCKED:
369031e8b14322ec0a4ec3620de920fd5966290625cSvetoslav                            preference.setIcon(R.drawable.ic_print_error);
370206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                            break;
371bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    }
372bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
373bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    Bundle extras = preference.getExtras();
374d9f156ddf072775c894db1ca3c3fe8989be4dbebSvetoslav                    extras.putString(EXTRA_PRINT_JOB_ID, printJob.getId().flattenToString());
375bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
376bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    mActivePrintJobsCategory.addPreference(preference);
377bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                }
378bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
379bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
380bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
381bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
382bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        public void onLoaderReset(Loader<List<PrintJobInfo>> loader) {
383bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            getPreferenceScreen().removePreference(mActivePrintJobsCategory);
384bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
385bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    }
386bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
387bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    private static final class PrintJobsLoader extends AsyncTaskLoader<List<PrintJobInfo>> {
388bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
389bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        private static final String LOG_TAG = "PrintJobsLoader";
390bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
391d9f156ddf072775c894db1ca3c3fe8989be4dbebSvetoslav        private static final boolean DEBUG = false;
392bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
393d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav        private List<PrintJobInfo> mPrintJobs = new ArrayList<PrintJobInfo>();
394bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
395bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        private final PrintManager mPrintManager;
396bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
397bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        private PrintJobStateChangeListener mPrintJobStateChangeListener;
398bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
399bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        public PrintJobsLoader(Context context) {
400bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            super(context);
401bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            mPrintManager = ((PrintManager) context.getSystemService(
402bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    Context.PRINT_SERVICE)).getGlobalPrintManagerForUser(
4036cfe7996541d608438fa7bec2cbad813ace93a4fSvetoslav                    context.getUserId());
404bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
405bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
406bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
407bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        public void deliverResult(List<PrintJobInfo> printJobs) {
408bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (isStarted()) {
409bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                super.deliverResult(printJobs);
410bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
411bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
412bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
413bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
414bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        protected void onStartLoading() {
415bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (DEBUG) {
416bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                Log.i(LOG_TAG, "onStartLoading()");
417bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
418bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            // If we already have a result, deliver it immediately.
419bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (!mPrintJobs.isEmpty()) {
420bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                deliverResult(new ArrayList<PrintJobInfo>(mPrintJobs));
421bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
422bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            // Start watching for changes.
423bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (mPrintJobStateChangeListener == null) {
424bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                mPrintJobStateChangeListener = new PrintJobStateChangeListener() {
425bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    @Override
426d9f156ddf072775c894db1ca3c3fe8989be4dbebSvetoslav                    public void onPrintJobStateChanged(PrintJobId printJobId) {
427bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                        onForceLoad();
428bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    }
429bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                };
430bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                mPrintManager.addPrintJobStateChangeListener(
431bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                        mPrintJobStateChangeListener);
432bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
433bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            // If the data changed or we have no data - load it now.
434bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (mPrintJobs.isEmpty()) {
435bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                onForceLoad();
436bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
437bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
438bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
439bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
440bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        protected void onStopLoading() {
441bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (DEBUG) {
442bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                Log.i(LOG_TAG, "onStopLoading()");
443bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
444bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            // Cancel the load in progress if possible.
445bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            onCancelLoad();
446bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
447bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
448bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
449bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        protected void onReset() {
450bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (DEBUG) {
451bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                Log.i(LOG_TAG, "onReset()");
452bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
453bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            // Stop loading.
454bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            onStopLoading();
455bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            // Clear the cached result.
456bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            mPrintJobs.clear();
457bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            // Stop watching for changes.
458bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (mPrintJobStateChangeListener != null) {
459bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                mPrintManager.removePrintJobStateChangeListener(
460bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                        mPrintJobStateChangeListener);
461bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                mPrintJobStateChangeListener = null;
462bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
463bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
464bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
465bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
466bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        public List<PrintJobInfo> loadInBackground() {
467bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            List<PrintJobInfo> printJobInfos = null;
468bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            List<PrintJob> printJobs = mPrintManager.getPrintJobs();
469bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            final int printJobCount = printJobs.size();
470bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            for (int i = 0; i < printJobCount; i++) {
471bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                PrintJobInfo printJob = printJobs.get(i).getInfo();
472bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                if (shouldShowToUser(printJob)) {
473bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    if (printJobInfos == null) {
474206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                        printJobInfos = new ArrayList<>();
475bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    }
476bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    printJobInfos.add(printJob);
477bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                }
478bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
479bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            return printJobInfos;
480bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
48143335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann    }
482bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
48343335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann    /**
48443335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann     * Should the print job the shown to the user in the settings app.
48543335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann     *
48643335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann     * @param printJob The print job in question.
48743335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann     * @return true iff the print job should be shown.
48843335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann     */
48943335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann    private static boolean shouldShowToUser(PrintJobInfo printJob) {
49043335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann        switch (printJob.getState()) {
49143335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann            case PrintJobInfo.STATE_QUEUED:
49243335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann            case PrintJobInfo.STATE_STARTED:
49343335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann            case PrintJobInfo.STATE_BLOCKED:
49443335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann            case PrintJobInfo.STATE_FAILED: {
49543335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann                return true;
496bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
497bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
49843335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann        return false;
499bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    }
500d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav
501738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann    /**
502738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann     * Provider for the print settings summary
503738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann     */
504206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang    @VisibleForTesting
505206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang    static class PrintSummaryProvider
506206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang            implements SummaryLoader.SummaryProvider, PrintJobStateChangeListener {
507738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann        private final Context mContext;
50801ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang        private final PrintManagerWrapper mPrintManager;
509738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann        private final SummaryLoader mSummaryLoader;
510738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann
511738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann        /**
512738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann         * Create a new {@link PrintSummaryProvider}.
513738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann         *
51401ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang         * @param context       The context this provider is for
515738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann         * @param summaryLoader The summary load using this provider
516738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann         */
51701ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang        PrintSummaryProvider(Context context, SummaryLoader summaryLoader,
51801ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                PrintManagerWrapper printManager) {
519738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann            mContext = context;
520738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann            mSummaryLoader = summaryLoader;
52101ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang            mPrintManager = printManager;
522738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann        }
523738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann
524738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann        @Override
525738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann        public void setListening(boolean isListening) {
52624264eb2508d54de473b00b934807d2a0881b0aaPhilip P. Moltmann            if (mPrintManager != null) {
52724264eb2508d54de473b00b934807d2a0881b0aaPhilip P. Moltmann                if (isListening) {
528206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                    mPrintManager.addPrintJobStateChanegListner(this);
529206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                    onPrintJobStateChanged(null);
530206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                } else {
531206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                    mPrintManager.removePrintJobStateChangeListener(this);
532206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                }
533206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang            }
534206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang        }
535206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang
536206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang        @Override
537206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang        public void onPrintJobStateChanged(PrintJobId printJobId) {
538206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang            final List<PrintJob> printJobs = mPrintManager.getPrintJobs();
539206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang
540206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang            int numActivePrintJobs = 0;
541206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang            if (printJobs != null) {
542206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                for (PrintJob job : printJobs) {
543206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                    if (shouldShowToUser(job.getInfo())) {
544206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                        numActivePrintJobs++;
54501ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                    }
54624264eb2508d54de473b00b934807d2a0881b0aaPhilip P. Moltmann                }
547738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann            }
548206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang
549206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang            if (numActivePrintJobs > 0) {
550206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                mSummaryLoader.setSummary(this, mContext.getResources().getQuantityString(
551206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                        R.plurals.print_jobs_summary, numActivePrintJobs, numActivePrintJobs));
552206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang            } else {
553206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                List<PrintServiceInfo> services =
554206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                        mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES);
555206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                if (services == null || services.isEmpty()) {
556206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                    mSummaryLoader.setSummary(this,
557206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                            mContext.getString(R.string.print_settings_summary_no_service));
558206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                } else {
559206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                    final int count = services.size();
560206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                    mSummaryLoader.setSummary(this,
561206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                            mContext.getResources().getQuantityString(
562206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                                    R.plurals.print_settings_summary, count, count));
563206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                }
564206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang            }
565738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann        }
566738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann
56701ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang        static class PrintManagerWrapper {
568738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann
56901ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang            private final PrintManager mPrintManager;
57001ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang
57101ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang            PrintManagerWrapper(Context context) {
57201ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                mPrintManager = ((PrintManager) context.getSystemService(Context.PRINT_SERVICE))
57301ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                        .getGlobalPrintManagerForUser(context.getUserId());
57443335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann            }
57543335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann
57601ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang            public List<PrintServiceInfo> getPrintServices(int selectionFlags) {
57701ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                return mPrintManager.getPrintServices(selectionFlags);
57801ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang            }
579206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang
580206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang            public void addPrintJobStateChanegListner(PrintJobStateChangeListener listener) {
581206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                mPrintManager.addPrintJobStateChangeListener(listener);
582206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang            }
583206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang
584206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang            public void removePrintJobStateChangeListener(PrintJobStateChangeListener listener) {
585206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                mPrintManager.removePrintJobStateChangeListener(listener);
586206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang            }
587206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang
588206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang            public List<PrintJob> getPrintJobs() {
589206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                return mPrintManager.getPrintJobs();
590206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang            }
591738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann        }
592738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann    }
593738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann
594738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann    /**
595738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann     * A factory for {@link PrintSummaryProvider providers} the settings app can use to read the
596738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann     * print summary.
597738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann     */
59801ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang    public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY =
59901ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang            (activity, summaryLoader) -> new PrintSummaryProvider(activity, summaryLoader,
60001ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                    new PrintSummaryProvider.PrintManagerWrapper(activity));
601738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann
602d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
603d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav            new BaseSearchIndexProvider() {
604d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav        @Override
605d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav        public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
606d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav            List<SearchIndexableRaw> indexables = new ArrayList<SearchIndexableRaw>();
607d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav
608d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav            PackageManager packageManager = context.getPackageManager();
609d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav            PrintManager printManager = (PrintManager) context.getSystemService(
610d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav                    Context.PRINT_SERVICE);
611d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav
6127476f5b904f16a0b330a99fc0f7d900a722554eaFabrice Di Meglio            String screenTitle = context.getResources().getString(R.string.print_settings);
613d729029b904d43c5c6d06f1d95004fe35445873dFabrice Di Meglio            SearchIndexableRaw data = new SearchIndexableRaw(context);
614d729029b904d43c5c6d06f1d95004fe35445873dFabrice Di Meglio            data.title = screenTitle;
615d729029b904d43c5c6d06f1d95004fe35445873dFabrice Di Meglio            data.screenTitle = screenTitle;
616d729029b904d43c5c6d06f1d95004fe35445873dFabrice Di Meglio            indexables.add(data);
617d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav
618d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            // Indexing all services, regardless if enabled. Please note that the index will not be
619d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            // updated until this function is called again
620d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            List<PrintServiceInfo> services =
621d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                    printManager.getPrintServices(PrintManager.ALL_SERVICES);
622c853a4282d0511d4a6886dfa95000c4dddff6a31Fabrice Di Meglio
6237833d183e7ddeaf08938b9db6d8714185e11901fPhilip P. Moltmann            if (services != null) {
6247833d183e7ddeaf08938b9db6d8714185e11901fPhilip P. Moltmann                final int serviceCount = services.size();
6257833d183e7ddeaf08938b9db6d8714185e11901fPhilip P. Moltmann                for (int i = 0; i < serviceCount; i++) {
6267833d183e7ddeaf08938b9db6d8714185e11901fPhilip P. Moltmann                    PrintServiceInfo service = services.get(i);
6277833d183e7ddeaf08938b9db6d8714185e11901fPhilip P. Moltmann
6287833d183e7ddeaf08938b9db6d8714185e11901fPhilip P. Moltmann                    ComponentName componentName = new ComponentName(
6297833d183e7ddeaf08938b9db6d8714185e11901fPhilip P. Moltmann                            service.getResolveInfo().serviceInfo.packageName,
6307833d183e7ddeaf08938b9db6d8714185e11901fPhilip P. Moltmann                            service.getResolveInfo().serviceInfo.name);
6317833d183e7ddeaf08938b9db6d8714185e11901fPhilip P. Moltmann
6327833d183e7ddeaf08938b9db6d8714185e11901fPhilip P. Moltmann                    data = new SearchIndexableRaw(context);
6337833d183e7ddeaf08938b9db6d8714185e11901fPhilip P. Moltmann                    data.key = componentName.flattenToString();
6347833d183e7ddeaf08938b9db6d8714185e11901fPhilip P. Moltmann                    data.title = service.getResolveInfo().loadLabel(packageManager).toString();
6357833d183e7ddeaf08938b9db6d8714185e11901fPhilip P. Moltmann                    data.screenTitle = screenTitle;
6367833d183e7ddeaf08938b9db6d8714185e11901fPhilip P. Moltmann                    indexables.add(data);
6377833d183e7ddeaf08938b9db6d8714185e11901fPhilip P. Moltmann                }
638d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav            }
639d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav
640d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav            return indexables;
641d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav        }
642d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav
643d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav        @Override
644d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav        public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
645d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav                boolean enabled) {
646d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav            List<SearchIndexableResource> indexables = new ArrayList<SearchIndexableResource>();
647d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav            SearchIndexableResource indexable = new SearchIndexableResource(context);
648d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav            indexable.xmlResId = R.xml.print_settings;
649d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav            indexables.add(indexable);
650d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav            return indexables;
651d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav        }
652d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav    };
653fe47a8dc89fe0393dae16b1c2152c901feab4dcbAlexandra Gherghina}
654