Home | History | Annotate | Download | only in print
      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 @NonNull 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      */
     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
     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      */
     65     private void queueNewResult() {
     66         Message m = mHandler.obtainMessage(0);
     67         m.obj = mPrintManager.getPrintServiceRecommendations();
     68         mHandler.sendMessage(m);
     69     }
     70 
     71     @Override
     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
     87     protected void onStopLoading() {
     88         if (mListener != null) {
     89             mPrintManager.removePrintServiceRecommendationsChangeListener(mListener);
     90             mListener = null;
     91         }
     92 
     93         mHandler.removeMessages(0);
     94     }
     95 
     96     @Override
     97     protected void onReset() {
     98         onStopLoading();
     99     }
    100 
    101     /**
    102      * Handler to sequentialize all the updates to the main thread.
    103      */
    104     private class MyHandler extends Handler {
    105         /**
    106          * Create a new handler on the main thread.
    107          */
    108         public MyHandler() {
    109             super(getContext().getMainLooper());
    110         }
    111 
    112         @Override
    113         public void handleMessage(Message msg) {
    114             if (isStarted()) {
    115                 deliverResult((List<RecommendationInfo>) msg.obj);
    116             }
    117         }
    118     }
    119 }
    120