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