UrlRenderer.java revision 45948fd407da525e6c8721ba75cfc8b356fc7e0f
1/*
2 * Copyright (C) 2010 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.service.urlrenderer;
18
19import android.os.IBinder;
20import android.os.ParcelFileDescriptor;
21import android.os.RemoteException;
22
23import java.util.List;
24
25/**
26 * TODO(phanna): Document this class.
27 * {@hide} while developing
28 */
29public final class UrlRenderer {
30    /**
31     * Interface for clients to receive the result of calls to
32     * {@link UrlRenderer#render}.
33     * {@hide} while developing
34     */
35    public interface Callback {
36        /**
37         * Calls to {@link render} will result in multiple invokations of this
38         * method for each url.  A null result means that there was a server
39         * error or a problem rendering the url.
40         * @param url  The url that has been rendered.
41         * @param result  A ParcelFileDescriptor containing the encoded image
42         *                data. The client is responsible for closing the stream
43         *                to free resources.  A null result indicates a failure
44         *                to render.
45         */
46        public void complete(String url, ParcelFileDescriptor result);
47    }
48
49    private IUrlRendererService mService;
50
51    /**
52     * Create a new UrlRenderer to remotely render urls.
53     * @param service  An IBinder service usually obtained through
54     *                 {@link ServiceConnection#onServiceConnected}
55     */
56    public UrlRenderer(IBinder service) {
57        mService = IUrlRendererService.Stub.asInterface(service);
58    }
59
60    private static class InternalCallback extends IUrlRendererCallback.Stub {
61        private final Callback mCallback;
62        InternalCallback(Callback cb) {
63            mCallback = cb;
64        }
65
66        public void complete(String url, ParcelFileDescriptor result) {
67            mCallback.complete(url, result);
68        }
69    }
70
71    /**
72     * Render the list of <var>urls</var> and invoke the <var>callback</var>
73     * for each result.
74     * @param urls  A List of urls to render.
75     * @param width  The desired width of the result.
76     * @param height  The desired height of the result.
77     * @param callback  An instance of {@link Callback} invoked for each url.
78     */
79    public void render(List<String> urls, int width, int height,
80            Callback callback) {
81        if (mService != null) {
82            try {
83                mService.render(urls, width, height,
84                        new InternalCallback(callback));
85            } catch (RemoteException ex) {
86            }
87        }
88    }
89}
90