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
192edbaa7062aa1988fc1f8b60566c53eaa4f3f891Fan Zhangimport static com.android.settings.print.PrintSettingPreferenceController.shouldShowToUser;
202edbaa7062aa1988fc1f8b60566c53eaa4f3f891Fan Zhang
21bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.app.LoaderManager.LoaderCallbacks;
220ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onukiimport android.content.ActivityNotFoundException;
23bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.content.AsyncTaskLoader;
242d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.content.ComponentName;
252d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.content.Context;
262d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.content.Intent;
27bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.content.Loader;
282d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.content.pm.PackageManager;
29c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmannimport android.content.res.TypedArray;
300ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onukiimport android.graphics.drawable.Drawable;
312d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.net.Uri;
322d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.os.Bundle;
33bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.print.PrintJob;
34bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.print.PrintJobId;
35bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.print.PrintJobInfo;
36bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.print.PrintManager;
37bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.print.PrintManager.PrintJobStateChangeListener;
38d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmannimport android.print.PrintServicesLoader;
392d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.printservice.PrintServiceInfo;
40d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslavimport android.provider.SearchIndexableResource;
412d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.provider.Settings;
4239b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport android.support.v7.preference.Preference;
4339b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport android.support.v7.preference.PreferenceCategory;
442d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.text.TextUtils;
45bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.text.format.DateUtils;
46bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.util.Log;
4723176395a5413ad55eeaff780e60f36e7d70977ePhilip P. Moltmannimport android.view.LayoutInflater;
482d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.view.View;
490ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onukiimport android.view.View.OnClickListener;
50bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganovimport android.view.ViewGroup;
510ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onukiimport android.widget.Button;
522d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport android.widget.TextView;
53738e6db7b83f591a5883183255c9d1163d3b7f9aPhilip P. Moltmann
54265d3c2a0c36251bf8a9f571d7239b6dd404d942Tamas Berghammerimport com.android.internal.logging.nano.MetricsProto.MetricsEvent;
552d531653595344ed7e61c093dca308b9ba7ec185Svetoslavimport com.android.settings.R;
56d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslavimport com.android.settings.search.BaseSearchIndexProvider;
577e920da6564032ddcc40db95925083ef59d19e58Fabrice Di Meglioimport com.android.settings.search.Indexable;
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
101e0b0e9f9025038d72f8129606cc1f432afcb54e0Fan Zhang    public 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
134bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    public void onViewCreated(View view, Bundle savedInstanceState) {
135bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        super.onViewCreated(view, savedInstanceState);
136bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        ViewGroup contentRoot = (ViewGroup) getListView().getParent();
137bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        View emptyView = getActivity().getLayoutInflater().inflate(
138d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav                R.layout.empty_print_state, contentRoot, false);
139bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        TextView textView = (TextView) emptyView.findViewById(R.id.message);
140bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        textView.setText(R.string.print_no_services_installed);
1410ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki
1420ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        final Intent addNewServiceIntent = createAddNewServiceIntentOrNull();
1430ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        if (addNewServiceIntent != null) {
1440ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            mAddNewServiceButton = (Button) emptyView.findViewById(R.id.add_new_service);
1450ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            mAddNewServiceButton.setOnClickListener(this);
1460ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            // The empty is used elsewhere too so it's hidden by default.
1470ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            mAddNewServiceButton.setVisibility(View.VISIBLE);
1480ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        }
1490ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki
150bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        contentRoot.addView(emptyView);
15139b467482d1bf256a111c757e9b7621c6f523271Jason Monk        setEmptyView(emptyView);
15232ccffc07c47d6e30b4198c74a8f2d2fbb8a799dTony Mak    }
153fe47a8dc89fe0393dae16b1c2152c901feab4dcbAlexandra Gherghina
15432ccffc07c47d6e30b4198c74a8f2d2fbb8a799dTony Mak    @Override
15532ccffc07c47d6e30b4198c74a8f2d2fbb8a799dTony Mak    protected String getIntentActionString() {
15632ccffc07c47d6e30b4198c74a8f2d2fbb8a799dTony Mak        return Settings.ACTION_PRINT_SETTINGS;
157bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    }
158bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
159d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann    /**
160d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann     * Adds preferences for all print services to the {@value PRINT_SERVICES_CATEGORY} cathegory.
161d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann     */
162206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang    private final class PrintServicesController implements LoaderCallbacks<List<PrintServiceInfo>> {
163d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann        @Override
164d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann        public Loader<List<PrintServiceInfo>> onCreateLoader(int id, Bundle args) {
165d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            PrintManager printManager =
166d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                    (PrintManager) getContext().getSystemService(Context.PRINT_SERVICE);
167d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            if (printManager != null) {
168d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                return new PrintServicesLoader(printManager, getContext(),
169d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                        PrintManager.ALL_SERVICES);
170d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            } else {
171d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                return null;
172d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            }
173bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
1742d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
175d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann        @Override
176d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann        public void onLoadFinished(Loader<List<PrintServiceInfo>> loader,
177d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                List<PrintServiceInfo> services) {
178d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            if (services.isEmpty()) {
179d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                getPreferenceScreen().removePreference(mPrintServicesCategory);
180d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                return;
181d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            } else if (getPreferenceScreen().findPreference(PRINT_SERVICES_CATEGORY) == null) {
182d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                getPreferenceScreen().addPreference(mPrintServicesCategory);
183d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            }
1840ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki
185d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            mPrintServicesCategory.removeAll();
186d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            PackageManager pm = getActivity().getPackageManager();
18701ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang            final Context context = getPrefContext();
18801ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang            if (context == null) {
18901ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                Log.w(TAG, "No preference context, skip adding print services");
19001ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                return;
19101ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang            }
1922d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
19301ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang            for (PrintServiceInfo service : services) {
19401ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                Preference preference = new Preference(context);
1952d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
196d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                String title = service.getResolveInfo().loadLabel(pm).toString();
197d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                preference.setTitle(title);
1982d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
199d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                ComponentName componentName = service.getComponentName();
200d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                preference.setKey(componentName.flattenToString());
2012d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
202d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                preference.setFragment(PrintServiceSettingsFragment.class.getName());
203d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                preference.setPersistent(false);
2042d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
205d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                if (service.isEnabled()) {
206d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                    preference.setSummary(getString(R.string.print_feature_state_on));
207d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                } else {
208d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                    preference.setSummary(getString(R.string.print_feature_state_off));
209d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                }
2102d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
211d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                Drawable drawable = service.getResolveInfo().loadIcon(pm);
212d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                if (drawable != null) {
213d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                    preference.setIcon(drawable);
214d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                }
2152d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
216d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                Bundle extras = preference.getExtras();
217d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                extras.putBoolean(EXTRA_CHECKED, service.isEnabled());
218d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                extras.putString(EXTRA_TITLE, title);
219d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                extras.putString(EXTRA_SERVICE_COMPONENT_NAME, componentName.flattenToString());
2200ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki
221d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                mPrintServicesCategory.addPreference(preference);
2222d531653595344ed7e61c093dca308b9ba7ec185Svetoslav            }
2232d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
224d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            Preference addNewServicePreference = newAddServicePreferenceOrNull();
225d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann            if (addNewServicePreference != null) {
226d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann                mPrintServicesCategory.addPreference(addNewServicePreference);
2272d531653595344ed7e61c093dca308b9ba7ec185Svetoslav            }
2282d531653595344ed7e61c093dca308b9ba7ec185Svetoslav        }
2292d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
230d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann        @Override
231d9779db8c966347734c5791cf63b7dc2e466a0f4Philip P. Moltmann        public void onLoaderReset(Loader<List<PrintServiceInfo>> loader) {
232bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            getPreferenceScreen().removePreference(mPrintServicesCategory);
2330ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        }
2340ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki    }
2350ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki
2360ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki    private Preference newAddServicePreferenceOrNull() {
2370ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        final Intent addNewServiceIntent = createAddNewServiceIntentOrNull();
2380ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        if (addNewServiceIntent == null) {
2390ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            return null;
2402d531653595344ed7e61c093dca308b9ba7ec185Svetoslav        }
24139b467482d1bf256a111c757e9b7621c6f523271Jason Monk        Preference preference = new Preference(getPrefContext());
2420ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        preference.setTitle(R.string.print_menu_item_add_service);
2430ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        preference.setIcon(R.drawable.ic_menu_add);
2440ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        preference.setOrder(ORDER_LAST);
2450ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        preference.setIntent(addNewServiceIntent);
2460ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        preference.setPersistent(false);
2470ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        return preference;
2480ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki    }
2490ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki
2500ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki    private Intent createAddNewServiceIntentOrNull() {
2510ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        final String searchUri = Settings.Secure.getString(getContentResolver(),
2520ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki                Settings.Secure.PRINT_SERVICE_SEARCH_URI);
2530ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        if (TextUtils.isEmpty(searchUri)) {
2540ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            return null;
2550ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        }
2560ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        return new Intent(Intent.ACTION_VIEW, Uri.parse(searchUri));
2572d531653595344ed7e61c093dca308b9ba7ec185Svetoslav    }
2582d531653595344ed7e61c093dca308b9ba7ec185Svetoslav
259bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    private void startSubSettingsIfNeeded() {
2600ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav        if (getArguments() == null) {
2610ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav            return;
2620ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav        }
2630ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav        String componentName = getArguments().getString(EXTRA_PRINT_SERVICE_COMPONENT_NAME);
2640ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav        if (componentName != null) {
2650ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav            getArguments().remove(EXTRA_PRINT_SERVICE_COMPONENT_NAME);
2660ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav            Preference prereference = findPreference(componentName);
2670ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav            if (prereference != null) {
26839b467482d1bf256a111c757e9b7621c6f523271Jason Monk                prereference.performClick();
2690ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav            }
2700ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav        }
2710ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav    }
2720ad40283bb6b811c26874af3fccf25745f63de8dSvetoslav
273fe47a8dc89fe0393dae16b1c2152c901feab4dcbAlexandra Gherghina    @Override
2740ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki    public void onClick(View v) {
2750ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        if (mAddNewServiceButton == v) {
2760ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            final Intent addNewServiceIntent = createAddNewServiceIntentOrNull();
2770ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            if (addNewServiceIntent != null) { // check again just in case.
2780ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki                try {
2790ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki                    startActivity(addNewServiceIntent);
2800ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki                } catch (ActivityNotFoundException e) {
2810ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki                    Log.w(TAG, "Unable to start activity", e);
2820ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki                }
2830ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki            }
2840ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki        }
2850ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki    }
2860ec96fe64cea4d27e866fc2dc17b799cf9c265a4Makoto Onuki
287206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang    private final class PrintJobsController implements LoaderCallbacks<List<PrintJobInfo>> {
288bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
289bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
290bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        public Loader<List<PrintJobInfo>> onCreateLoader(int id, Bundle args) {
291bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (id == LOADER_ID_PRINT_JOBS_LOADER) {
292eb01628ba897c116e434970309a4d5aff3fd03dbPhilip P. Moltmann                return new PrintJobsLoader(getContext());
293bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
294bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            return null;
295bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
296bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
297bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
298bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        public void onLoadFinished(Loader<List<PrintJobInfo>> loader,
299bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                List<PrintJobInfo> printJobs) {
300bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (printJobs == null || printJobs.isEmpty()) {
301bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                getPreferenceScreen().removePreference(mActivePrintJobsCategory);
302bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            } else {
303bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                if (getPreferenceScreen().findPreference(PRINT_JOBS_CATEGORY) == null) {
304bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    getPreferenceScreen().addPreference(mActivePrintJobsCategory);
305bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                }
306bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
307bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                mActivePrintJobsCategory.removeAll();
30801ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                final Context context = getPrefContext();
30901ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                if (context == null) {
31001ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                    Log.w(TAG, "No preference context, skip adding print jobs");
31101ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                    return;
31201ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                }
313bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
31401ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                for (PrintJobInfo printJob : printJobs) {
31501ece62faa6bfc05b6a344a9069896b2f5097242Fan Zhang                    Preference preference = new Preference(context);
316bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
317bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    preference.setPersistent(false);
318bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    preference.setFragment(PrintJobSettingsFragment.class.getName());
319bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    preference.setKey(printJob.getId().flattenToString());
320bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
321bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    switch (printJob.getState()) {
322bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                        case PrintJobInfo.STATE_QUEUED:
323206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                        case PrintJobInfo.STATE_STARTED:
32497e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                            if (!printJob.isCancelling()) {
32597e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                preference.setTitle(getString(
32697e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        R.string.print_printing_state_title_template,
32797e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        printJob.getLabel()));
32897e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                            } else {
32997e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                preference.setTitle(getString(
33097e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        R.string.print_cancelling_state_title_template,
33197e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        printJob.getLabel()));
33297e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                            }
333206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                            break;
334206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                        case PrintJobInfo.STATE_FAILED:
335bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                            preference.setTitle(getString(
336bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                                    R.string.print_failed_state_title_template,
337bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                                    printJob.getLabel()));
338206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                            break;
339206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                        case PrintJobInfo.STATE_BLOCKED:
34097e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                            if (!printJob.isCancelling()) {
34197e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                preference.setTitle(getString(
34297e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        R.string.print_blocked_state_title_template,
34397e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        printJob.getLabel()));
34497e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                            } else {
34597e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                preference.setTitle(getString(
34697e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        R.string.print_cancelling_state_title_template,
34797e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                                        printJob.getLabel()));
34897e962cd36e1a2bbadc264ec9ce8627a7decf268Svetoslav                            }
349206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                            break;
350bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    }
351bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
352bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    preference.setSummary(getString(R.string.print_job_summary,
353bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                            printJob.getPrinterName(), DateUtils.formatSameDayTime(
354bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                                    printJob.getCreationTime(), printJob.getCreationTime(),
355bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                                    DateFormat.SHORT, DateFormat.SHORT)));
356bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
3572edbaa7062aa1988fc1f8b60566c53eaa4f3f891Fan Zhang                    TypedArray a = getActivity().obtainStyledAttributes(new int[] {
358c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann                            android.R.attr.colorControlNormal});
359c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann                    int tintColor = a.getColor(0, 0);
360c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann                    a.recycle();
361c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann
362bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    switch (printJob.getState()) {
363bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                        case PrintJobInfo.STATE_QUEUED:
364c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann                        case PrintJobInfo.STATE_STARTED: {
365c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann                            Drawable icon = getActivity().getDrawable(
366c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann                                    com.android.internal.R.drawable.ic_print);
367c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann                            icon.setTint(tintColor);
368c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann                            preference.setIcon(icon);
369206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                            break;
370c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann                        }
371c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann
372bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                        case PrintJobInfo.STATE_FAILED:
373c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann                        case PrintJobInfo.STATE_BLOCKED: {
374c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann                            Drawable icon = getActivity().getDrawable(
375c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann                                    com.android.internal.R.drawable.ic_print_error);
376c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann                            icon.setTint(tintColor);
377c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann                            preference.setIcon(icon);
378206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                            break;
379c16600066a68c85b03629d29e07f669a8a953be7Philip P. Moltmann                        }
380bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    }
381bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
382bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    Bundle extras = preference.getExtras();
383d9f156ddf072775c894db1ca3c3fe8989be4dbebSvetoslav                    extras.putString(EXTRA_PRINT_JOB_ID, printJob.getId().flattenToString());
384bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
385bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    mActivePrintJobsCategory.addPreference(preference);
386bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                }
387bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
388bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
389bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
390bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
391bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        public void onLoaderReset(Loader<List<PrintJobInfo>> loader) {
392bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            getPreferenceScreen().removePreference(mActivePrintJobsCategory);
393bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
394bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    }
395bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
396bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov    private static final class PrintJobsLoader extends AsyncTaskLoader<List<PrintJobInfo>> {
397bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
398bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        private static final String LOG_TAG = "PrintJobsLoader";
399bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
400d9f156ddf072775c894db1ca3c3fe8989be4dbebSvetoslav        private static final boolean DEBUG = false;
401bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
402d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav        private List<PrintJobInfo> mPrintJobs = new ArrayList<PrintJobInfo>();
403bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
404bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        private final PrintManager mPrintManager;
405bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
406bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        private PrintJobStateChangeListener mPrintJobStateChangeListener;
407bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
408bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        public PrintJobsLoader(Context context) {
409bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            super(context);
410bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            mPrintManager = ((PrintManager) context.getSystemService(
411bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    Context.PRINT_SERVICE)).getGlobalPrintManagerForUser(
4126cfe7996541d608438fa7bec2cbad813ace93a4fSvetoslav                    context.getUserId());
413bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
414bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
415bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
416bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        public void deliverResult(List<PrintJobInfo> printJobs) {
417bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (isStarted()) {
418bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                super.deliverResult(printJobs);
419bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
420bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
421bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
422bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
423bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        protected void onStartLoading() {
424bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (DEBUG) {
425bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                Log.i(LOG_TAG, "onStartLoading()");
426bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
427bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            // If we already have a result, deliver it immediately.
428bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (!mPrintJobs.isEmpty()) {
429bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                deliverResult(new ArrayList<PrintJobInfo>(mPrintJobs));
430bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
431bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            // Start watching for changes.
432bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (mPrintJobStateChangeListener == null) {
433bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                mPrintJobStateChangeListener = new PrintJobStateChangeListener() {
434bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    @Override
435d9f156ddf072775c894db1ca3c3fe8989be4dbebSvetoslav                    public void onPrintJobStateChanged(PrintJobId printJobId) {
436bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                        onForceLoad();
437bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    }
438bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                };
439bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                mPrintManager.addPrintJobStateChangeListener(
440bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                        mPrintJobStateChangeListener);
441bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
442bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            // If the data changed or we have no data - load it now.
443bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (mPrintJobs.isEmpty()) {
444bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                onForceLoad();
445bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
446bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
447bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
448bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
449bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        protected void onStopLoading() {
450bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (DEBUG) {
451bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                Log.i(LOG_TAG, "onStopLoading()");
452bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
453bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            // Cancel the load in progress if possible.
454bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            onCancelLoad();
455bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
456bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
457bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
458bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        protected void onReset() {
459bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (DEBUG) {
460bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                Log.i(LOG_TAG, "onReset()");
461bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
462bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            // Stop loading.
463bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            onStopLoading();
464bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            // Clear the cached result.
465bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            mPrintJobs.clear();
466bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            // Stop watching for changes.
467bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            if (mPrintJobStateChangeListener != null) {
468bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                mPrintManager.removePrintJobStateChangeListener(
469bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                        mPrintJobStateChangeListener);
470bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                mPrintJobStateChangeListener = null;
471bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
472bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
473bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
474bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        @Override
475bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        public List<PrintJobInfo> loadInBackground() {
476bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            List<PrintJobInfo> printJobInfos = null;
477bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            List<PrintJob> printJobs = mPrintManager.getPrintJobs();
478bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            final int printJobCount = printJobs.size();
479bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            for (int i = 0; i < printJobCount; i++) {
480bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                PrintJobInfo printJob = printJobs.get(i).getInfo();
481bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                if (shouldShowToUser(printJob)) {
482bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    if (printJobInfos == null) {
483206b774f79716ad6c02e5f5c5066d3eea7e72bf8Fan Zhang                        printJobInfos = new ArrayList<>();
484bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    }
485bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                    printJobInfos.add(printJob);
486bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov                }
487bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            }
488bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov            return printJobInfos;
489bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov        }
49043335444ef9089e3b68ad759ee28303cbb4bcc5aPhilip P. Moltmann    }
491bc2c3db8f923b9ee25345f5ebbad9f6c762a1e23Svetoslav Ganov
492d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
493d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav            new BaseSearchIndexProvider() {
494d78d44d32ae76e4eab843504114446dec3b22ed3Svetoslav
4952edbaa7062aa1988fc1f8b60566c53eaa4f3f891Fan Zhang                @Override
4962edbaa7062aa1988fc1f8b60566c53eaa4f3f891Fan Zhang                public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
4972edbaa7062aa1988fc1f8b60566c53eaa4f3f891Fan Zhang                        boolean enabled) {
4982edbaa7062aa1988fc1f8b60566c53eaa4f3f891Fan Zhang                    List<SearchIndexableResource> indexables = new ArrayList<>();
4992edbaa7062aa1988fc1f8b60566c53eaa4f3f891Fan Zhang                    SearchIndexableResource indexable = new SearchIndexableResource(context);
5002edbaa7062aa1988fc1f8b60566c53eaa4f3f891Fan Zhang                    indexable.xmlResId = R.xml.print_settings;
5012edbaa7062aa1988fc1f8b60566c53eaa4f3f891Fan Zhang                    indexables.add(indexable);
5022edbaa7062aa1988fc1f8b60566c53eaa4f3f891Fan Zhang                    return indexables;
5032edbaa7062aa1988fc1f8b60566c53eaa4f3f891Fan Zhang                }
5042edbaa7062aa1988fc1f8b60566c53eaa4f3f891Fan Zhang            };
505fe47a8dc89fe0393dae16b1c2152c901feab4dcbAlexandra Gherghina}
506