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