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