| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class PrintServicesLoader extends Loader<List<PrintServiceInfo>> { |
| 36 |
|
| 37 |
private final int mSelectionFlags; |
| 38 |
|
| 39 |
|
| 40 |
private final @NonNull PrintManager mPrintManager; |
| 41 |
|
| 42 |
|
| 43 |
private final @NonNull Handler mHandler; |
| 44 |
|
| 45 |
|
| 46 |
private PrintManager.PrintServicesChangeListener mListener; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 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 |
| 65 |
protected void onForceLoad() { |
| 66 |
queueNewResult(); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private void queueNewResult() { |
| 73 |
Message m = mHandler.obtainMessage(0); |
| 74 |
m.obj = mPrintManager.getPrintServices(mSelectionFlags); |
| 75 |
mHandler.sendMessage(m); |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 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 |
|
| 89 |
deliverResult(mPrintManager.getPrintServices(mSelectionFlags)); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 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 |
| 103 |
protected void onReset() { |
| 104 |
onStopLoading(); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
private class MyHandler extends Handler { |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public MyHandler() { |
| 115 |
super(getContext().getMainLooper()); |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public void handleMessage(Message msg) { |
| 120 |
if (isStarted()) { |
| 121 |
deliverResult((List<PrintServiceInfo>) msg.obj); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |