PrintServicesLoader.java revision 9dcb86a48d73f399fb1b5c020005d76d350eeac2
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.print;
18
19import android.annotation.NonNull;
20import android.content.Context;
21import android.content.Loader;
22import android.os.Handler;
23import android.os.Message;
24import android.printservice.PrintServiceInfo;
25import com.android.internal.util.Preconditions;
26
27import java.util.List;
28
29/**
30 * Loader for the list of print services. Can be parametrized to select a subset.
31 *
32 * @hide
33 */
34public class PrintServicesLoader extends Loader<List<PrintServiceInfo>> {
35    /** What type of services to load. */
36    private final int mSelectionFlags;
37
38    /** The print manager to be used by this object */
39    private final @NonNull PrintManager mPrintManager;
40
41    /** Handler to sequentialize the delivery of the results to the main thread */
42    private Handler mHandler;
43
44    /** Listens for updates to the data from the platform */
45    private PrintManager.PrintServicesChangeListener mListener;
46
47    /**
48     * Create a new PrintServicesLoader.
49     *
50     * @param printManager   The print manager supplying the data
51     * @param context        Context of the using object
52     * @param selectionFlags What type of services to load.
53     */
54    public PrintServicesLoader(@NonNull PrintManager printManager, @NonNull Context context,
55            int selectionFlags) {
56        super(Preconditions.checkNotNull(context));
57        mPrintManager = Preconditions.checkNotNull(printManager);
58        mSelectionFlags = Preconditions.checkFlagsArgument(selectionFlags,
59                PrintManager.ALL_SERVICES);
60    }
61
62    @Override
63    protected void onForceLoad() {
64        queueNewResult();
65    }
66
67    /**
68     * Read the print services and queue it to be delivered on the main thread.
69     */
70    private void queueNewResult() {
71        Message m = mHandler.obtainMessage(0);
72        m.obj = mPrintManager.getPrintServices(mSelectionFlags);
73        mHandler.sendMessage(m);
74    }
75
76    @Override
77    protected void onStartLoading() {
78        mHandler = new MyHandler();
79        mListener = new PrintManager.PrintServicesChangeListener() {
80            @Override public void onPrintServicesChanged() {
81                queueNewResult();
82            }
83        };
84
85        mPrintManager.addPrintServicesChangeListener(mListener);
86
87        // Immediately deliver a result
88        deliverResult(mPrintManager.getPrintServices(mSelectionFlags));
89    }
90
91    @Override
92    protected void onStopLoading() {
93        if (mListener != null) {
94            mPrintManager.removePrintServicesChangeListener(mListener);
95            mListener = null;
96        }
97
98        if (mHandler != null) {
99            mHandler.removeMessages(0);
100            mHandler = null;
101        }
102    }
103
104    @Override
105    protected void onReset() {
106        onStopLoading();
107    }
108
109    /**
110     * Handler to sequentialize all the updates to the main thread.
111     */
112    private class MyHandler extends Handler {
113        /**
114         * Create a new handler on the main thread.
115         */
116        public MyHandler() {
117            super(getContext().getMainLooper());
118        }
119
120        @Override
121        public void handleMessage(Message msg) {
122            super.handleMessage(msg);
123
124            if (isStarted()) {
125                deliverResult((List<PrintServiceInfo>) msg.obj);
126            }
127        }
128    }
129}
130