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