| 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.recommendation.RecommendationInfo; |
| 25 |
|
| 26 |
import com.android.internal.util.Preconditions; |
| 27 |
|
| 28 |
import java.util.List; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class PrintServiceRecommendationsLoader extends Loader<List<RecommendationInfo>> { |
| 36 |
|
| 37 |
private final @NonNull PrintManager mPrintManager; |
| 38 |
|
| 39 |
|
| 40 |
private final @NonNull Handler mHandler; |
| 41 |
|
| 42 |
|
| 43 |
private PrintManager.PrintServiceRecommendationsChangeListener mListener; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 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 |
| 59 |
protected void onForceLoad() { |
| 60 |
queueNewResult(); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private void queueNewResult() { |
| 67 |
Message m = mHandler.obtainMessage(0); |
| 68 |
m.obj = mPrintManager.getPrintServiceRecommendations(); |
| 69 |
mHandler.sendMessage(m); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 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 |
|
| 84 |
deliverResult(mPrintManager.getPrintServiceRecommendations()); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 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 |
| 98 |
protected void onReset() { |
| 99 |
onStopLoading(); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private class MyHandler extends Handler { |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public MyHandler() { |
| 110 |
super(getContext().getMainLooper()); |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public void handleMessage(Message msg) { |
| 115 |
if (isStarted()) { |
| 116 |
deliverResult((List<RecommendationInfo>) msg.obj); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |