1/*
2 * Copyright (C) 2015 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.printservice;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.graphics.drawable.Icon;
22import android.os.RemoteException;
23import android.print.PrinterId;
24import android.util.Log;
25
26
27/**
28 * Callback for {@link PrinterDiscoverySession#onRequestCustomPrinterIcon}.
29 */
30public final class CustomPrinterIconCallback {
31    /** The printer the call back is for */
32    private final @NonNull PrinterId mPrinterId;
33    private final @NonNull IPrintServiceClient mObserver;
34    private static final String LOG_TAG = "CustomPrinterIconCB";
35
36    /**
37     * Create a callback class to be used once a icon is loaded
38     *
39     * @param printerId The printer the icon should be loaded for
40     * @param observer The observer that needs to be notified about the update.
41     */
42    CustomPrinterIconCallback(@NonNull PrinterId printerId, @NonNull IPrintServiceClient observer) {
43        mPrinterId = printerId;
44        mObserver = observer;
45    }
46
47    /**
48     * Provide a new icon for a printer. Can be called more than once to update the icon.
49     *
50     * @param icon The new icon for the printer or null to unset the current icon
51     * @return true iff the icon could be updated
52     */
53    public boolean onCustomPrinterIconLoaded(@Nullable Icon icon) {
54        try {
55            mObserver.onCustomPrinterIconLoaded(mPrinterId, icon);
56        } catch (RemoteException e) {
57            Log.e(LOG_TAG , "Could not update icon", e);
58            return false;
59        }
60
61        return true;
62    }
63}
64