OSDN Git Service

Merge "Change suggestion window text color based on user feedback." into nyc-dev
[android-x86/frameworks-base.git] / core / java / android / print / PrintServicesLoader.java
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
17 package android.print;
18
19 import android.annotation.NonNull;
20 import android.content.Context;
21 import android.content.Loader;
22 import android.os.Handler;
23 import android.os.Message;
24 import android.printservice.PrintServiceInfo;
25
26 import java.util.List;
27
28 /**
29  * Loader for the list of print services. Can be parametrized to select a subset.
30  *
31  * @hide
32  */
33 public class PrintServicesLoader extends Loader<List<PrintServiceInfo>> {
34     /** What type of services to load. */
35     private final int mSelectionFlags;
36
37     /** The print manager to be used by this object */
38     private final @NonNull PrintManager mPrintManager;
39
40     /** Handler to sequentialize the delivery of the results to the main thread */
41     private Handler mHandler;
42
43     /** Listens for updates to the data from the platform */
44     private PrintManager.PrintServicesChangeListener mListener;
45
46     /**
47      * Create a new PrintServicesLoader.
48      *
49      * @param selectionFlags What type of services to load.
50      */
51     public PrintServicesLoader(@NonNull PrintManager printManager, @NonNull Context context,
52             int selectionFlags) {
53         super(context);
54         mPrintManager = printManager;
55         mSelectionFlags = selectionFlags;
56     }
57
58     @Override
59     protected void onForceLoad() {
60         queueNewResult();
61     }
62
63     /**
64      * Read the print services and queue it to be delivered on the main thread.
65      */
66     private void queueNewResult() {
67         Message m = mHandler.obtainMessage(0);
68         m.obj = mPrintManager.getPrintServices(mSelectionFlags);
69         mHandler.sendMessage(m);
70     }
71
72     @Override
73     protected void onStartLoading() {
74         mHandler = new MyHandler();
75         mListener = new PrintManager.PrintServicesChangeListener() {
76             @Override public void onPrintServicesChanged() {
77                 queueNewResult();
78             }
79         };
80
81         mPrintManager.addPrintServicesChangeListener(mListener);
82
83         // Immediately deliver a result
84         deliverResult(mPrintManager.getPrintServices(mSelectionFlags));
85     }
86
87     @Override
88     protected void onStopLoading() {
89         if (mListener != null) {
90             mPrintManager.removePrintServicesChangeListener(mListener);
91             mListener = null;
92         }
93
94         if (mHandler != null) {
95             mHandler.removeMessages(0);
96             mHandler = null;
97         }
98     }
99
100     @Override
101     protected void onReset() {
102         onStopLoading();
103     }
104
105     /**
106      * Handler to sequentialize all the updates to the main thread.
107      */
108     private class MyHandler extends Handler {
109         /**
110          * Create a new handler on the main thread.
111          */
112         public MyHandler() {
113             super(getContext().getMainLooper());
114         }
115
116         @Override
117         public void handleMessage(Message msg) {
118             super.handleMessage(msg);
119
120             if (isStarted()) {
121                 deliverResult((List<PrintServiceInfo>) msg.obj);
122             }
123         }
124     }
125 }