summaryrefslogtreecommitdiffstats
path: root/core/java/android/printservice/PrinterDiscoverySession.java
blob: 92dc0dd4a05fdba30ff866f83f4317b19c474a80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.printservice;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.print.IPrinterDiscoverySessionController;
import android.print.IPrinterDiscoverySessionObserver;
import android.print.PrinterId;
import android.print.PrinterInfo;
import android.util.Log;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * This class encapsulates the interaction between a print service and the
 * system during printer discovery. During printer discovery you are responsible
 * for adding discovered printers, removing already added printers that
 * disappeared, and updating already added printers.
 * <p>
 * The opening of the session is announced by a call to {@link
 * PrinterDiscoverySession#onOpen(List)} at which point you should start printer
 * discovery. The closing of the session is announced by a call to {@link
 * PrinterDiscoverySession#onClose()} at which point you should stop printer
 * discovery. Discovered printers are added by invoking {@link
 * PrinterDiscoverySession#addPrinters(List)}. Added printers that disappeared
 * are removed by invoking {@link PrinterDiscoverySession#removePrinters(List)}.
 * Added printers whose properties or capabilities changed are updated through
 * a call to {@link PrinterDiscoverySession#updatePrinters(List)}.
 * </p>
 * <p>
 * The system will make a call to
 * {@link PrinterDiscoverySession#onRequestPrinterUpdate(PrinterId)} if you
 * need to update a given printer. It is possible that you add a printer without
 * specifying its capabilities. This enables you to avoid querying all
 * discovered printers for their capabilities, rather querying the capabilities
 * of a printer only if necessary. For example, the system will require that you
 * update a printer if it gets selected by the user. If you did not report the
 * printer capabilities when adding it, you must do so after the system requests
 * a printer update. Otherwise, the printer will be ignored.
 * </p>
 * <p>
 * During printer discovery all printers that are known to your print service
 * have to be added. The system does not retain any printers from previous
 * sessions.
 * </p>
 */
public abstract class PrinterDiscoverySession {
    private static final String LOG_TAG = "PrinterDiscoverySession";

    private static int sIdCounter = 0;

    private final Object mLock = new Object();

    private final Handler mHandler;

    private final int mId;

    private IPrinterDiscoverySessionController mController;

    private IPrinterDiscoverySessionObserver mObserver;

    /**
     * Constructor.
     *
     * @param context A context instance.
     */
    public PrinterDiscoverySession(Context context) {
        mId = sIdCounter++;
        mHandler = new SessionHandler(context.getMainLooper());
        mController = new PrinterDiscoverySessionController(this);
    }

    void setObserver(IPrinterDiscoverySessionObserver observer) {
        synchronized (mLock) {
            mObserver = observer;
            try {
                mObserver.setController(mController);
            } catch (RemoteException re) {
                Log.e(LOG_TAG, "Error setting session controller", re);
            }
        }
    }

    int getId() {
        return mId;
    }

    /**
     * Adds discovered printers. Adding an already added printer has no effect.
     * Removed printers can be added again. You can call this method multiple
     * times during printer discovery.
     * <p>
     * <strong>Note: </strong> Calls to this method before the session is opened,
     * i.e. before the {@link #onOpen(List)} call, and after the session is closed,
     * i.e. after the call to {@link #onClose()}, will be ignored.
     * </p>
     *
     * @param printers The printers to add.
     *
     * @see #removePrinters(List)
     * @see #updatePrinters(List)
     */
    public final void addPrinters(List<PrinterInfo> printers) {
        final IPrinterDiscoverySessionObserver observer;
        synchronized (mLock) {
            observer = mObserver;
        }
        if (observer != null) {
            try {
                observer.onPrintersAdded(printers);
            } catch (RemoteException re) {
                Log.e(LOG_TAG, "Error adding printers", re);
            }
        } else {
            Log.w(LOG_TAG, "Printer discovery session not open not adding printers.");
        }
    }

    /**
     * Removes added printers. Removing an already removed or never added
     * printer has no effect. Removed printers can be added again. You
     * can call this method multiple times during printer discovery.
     * <p>
     * <strong>Note: </strong> Calls to this method before the session is opened,
     * i.e. before the {@link #onOpen(List)} call, and after the session is closed,
     * i.e. after the call to {@link #onClose()}, will be ignored.
     * </p>
     *
     * @param printerIds The ids of the removed printers.
     *
     * @see #addPrinters(List)
     * @see #updatePrinters(List)
     */
    public final void removePrinters(List<PrinterId> printerIds) {
        final IPrinterDiscoverySessionObserver observer;
        synchronized (mLock) {
            observer = mObserver;
        }
        if (observer != null) {
            try {
                observer.onPrintersRemoved(printerIds);
            } catch (RemoteException re) {
                Log.e(LOG_TAG, "Error removing printers", re);
            }
        } else {
            Log.w(LOG_TAG, "Printer discovery session not open not removing printers.");
        }
    }

    /**
     * Updates added printers. Updating a printer that was not added or that
     * was removed has no effect. You can call this method multiple times
     * during printer discovery.
     * <p>
     * <strong>Note: </strong> Calls to this method before the session is opened,
     * i.e. before the {@link #onOpen(List)} call, and after the session is closed,
     * i.e. after the call to {@link #onClose()}, will be ignored.
     * </p>
     *
     * @param printers The printers to update.
     *
     * @see #addPrinters(List)
     * @see #removePrinters(List)
     */
    public final void updatePrinters(List<PrinterInfo> printers) {
        final IPrinterDiscoverySessionObserver observer;
        synchronized (mLock) {
            observer = mObserver;
        }
        if (observer != null) {
            try {
                observer.onPrintersUpdated(printers);
            } catch (RemoteException re) {
                Log.e(LOG_TAG, "Error updating printers", re);
            }
        } else {
            Log.w(LOG_TAG, "Printer discovery session not open not updating printers.");
        }
    }

    /**
     * Callback notifying you that the session is open and you should start
     * printer discovery. Discovered printers should be added via calling
     * {@link #addPrinters(List)}. Added printers that disappeared should be
     * removed via calling {@link #removePrinters(List)}. Added printers whose
     * properties or capabilities changes should be updated via calling {@link
     * #updatePrinters(List)}. When the session is closed you will receive a
     * call to {@link #onClose()}.
     * <p>
     * During printer discovery all printers that are known to your print
     * service have to be added. The system does not retain any printers from
     * previous sessions.
     * </p>
     * <p>
     * <strong>Note: </strong>You are also given a list of printers whose
     * availability has to be checked first. For example, these printers could
     * be the user's favorite ones, therefore they have to be verified first.
     * </p>
     *
     * @see #onClose()
     * @see #addPrinters(List)
     * @see #removePrinters(List)
     * @see #updatePrinters(List)
     */
    public abstract void onOpen(List<PrinterId> priorityList);

    /**
     * Callback notifying you that the session is closed and you should stop
     * printer discovery. After the session is closed any call to the methods
     * of this instance will be ignored. Once the session is closed
     * it will never be opened again.
     */
    public abstract void onClose();

    /**
     * Requests that you update a printer. You are responsible for updating
     * the printer by also reporting its capabilities via calling {@link
     * #updatePrinters(List)}.
     * <p>
     * <strong>Note: </strong> A printer can be initially added without its
     * capabilities to avoid polling printers that the user will not select.
     * However, after this method is called you are expected to update the
     * printer <strong>including</strong> its capabilities. Otherwise, the
     * printer will be ignored.
     * <p>
     * A scenario when you may be requested to update a printer is if the user
     * selects it and the system has to present print options UI based on the
     * printer's capabilities.
     * </p>
     *
     * @param printerId The printer id.
     *
     * @see #updatePrinters(List)
     * @see PrinterInfo.Builder#setCapabilities(PrinterCapabilitiesInfo)
     *      PrinterInfo.Builder.setCapabilities(PrinterCapabilitiesInfo)
     */
    public abstract void onRequestPrinterUpdate(PrinterId printerId);

    void close() {
        synchronized (mLock) {
            mController = null;
            mObserver = null;
        }
    }

    private final class SessionHandler extends Handler {
        public static final int MSG_OPEN = 1;
        public static final int MSG_CLOSE = 2;
        public static final int MSG_REQUEST_PRINTER_UPDATE = 3;

        public SessionHandler(Looper looper) {
            super(looper, null, true);
        }

        @Override
        @SuppressWarnings("unchecked")
        public void handleMessage(Message message) {
            switch (message.what) {
                case MSG_OPEN: {
                    List<PrinterId> priorityList = (List<PrinterId>) message.obj;
                    onOpen(priorityList);
                } break;

                case MSG_CLOSE: {
                    onClose();
                    close();
                } break;

                case MSG_REQUEST_PRINTER_UPDATE: {
                    PrinterId printerId = (PrinterId) message.obj;
                    onRequestPrinterUpdate(printerId);
                } break;
            }
        }
    }

    private static final class PrinterDiscoverySessionController extends
            IPrinterDiscoverySessionController.Stub {
        private final WeakReference<PrinterDiscoverySession> mWeakSession;

        public PrinterDiscoverySessionController(PrinterDiscoverySession session) {
            mWeakSession = new WeakReference<PrinterDiscoverySession>(session);
        }

        @Override
        public void open(List<PrinterId> priorityList) {
            PrinterDiscoverySession session = mWeakSession.get();
            if (session != null) {
                session.mHandler.obtainMessage(SessionHandler.MSG_OPEN,
                        priorityList).sendToTarget();
            }
        }

        @Override
        public void close() {
            PrinterDiscoverySession session = mWeakSession.get();
            if (session != null) {
                session.mHandler.sendEmptyMessage(SessionHandler.MSG_CLOSE);
            }
        }

        @Override
        public void requestPrinterUpdate(PrinterId printerId) {
            PrinterDiscoverySession session = mWeakSession.get();
            if (session != null) {
                session.mHandler.obtainMessage(
                        SessionHandler.MSG_REQUEST_PRINTER_UPDATE,
                        printerId).sendToTarget();
            }
        }
    };
}