145948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott/*
245948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott * Copyright (C) 2010 The Android Open Source Project
345948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott *
445948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott * Licensed under the Apache License, Version 2.0 (the "License");
545948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott * you may not use this file except in compliance with the License.
645948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott * You may obtain a copy of the License at
745948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott *
845948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott *      http://www.apache.org/licenses/LICENSE-2.0
945948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott *
1045948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott * Unless required by applicable law or agreed to in writing, software
1145948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott * distributed under the License is distributed on an "AS IS" BASIS,
1245948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1345948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott * See the License for the specific language governing permissions and
1445948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott * limitations under the License.
1545948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott */
1645948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott
1745948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scottpackage android.service.urlrenderer;
1845948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott
1945948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scottimport android.os.IBinder;
2045948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scottimport android.os.ParcelFileDescriptor;
2145948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scottimport android.os.RemoteException;
2245948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott
2345948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scottimport java.util.List;
2445948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott
2545948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott/**
2645948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott * TODO(phanna): Document this class.
2745948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott * {@hide} while developing
2845948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott */
2945948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scottpublic final class UrlRenderer {
3045948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott    /**
3145948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott     * Interface for clients to receive the result of calls to
3245948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott     * {@link UrlRenderer#render}.
3345948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott     * {@hide} while developing
3445948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott     */
3545948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott    public interface Callback {
3645948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott        /**
3745948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott         * Calls to {@link render} will result in multiple invokations of this
3845948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott         * method for each url.  A null result means that there was a server
3945948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott         * error or a problem rendering the url.
4045948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott         * @param url  The url that has been rendered.
4145948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott         * @param result  A ParcelFileDescriptor containing the encoded image
4245948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott         *                data. The client is responsible for closing the stream
4345948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott         *                to free resources.  A null result indicates a failure
4445948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott         *                to render.
4545948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott         */
4645948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott        public void complete(String url, ParcelFileDescriptor result);
4745948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott    }
4845948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott
4945948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott    private IUrlRendererService mService;
5045948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott
5145948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott    /**
5245948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott     * Create a new UrlRenderer to remotely render urls.
5345948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott     * @param service  An IBinder service usually obtained through
5445948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott     *                 {@link ServiceConnection#onServiceConnected}
5545948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott     */
5645948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott    public UrlRenderer(IBinder service) {
5745948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott        mService = IUrlRendererService.Stub.asInterface(service);
5845948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott    }
5945948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott
6045948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott    private static class InternalCallback extends IUrlRendererCallback.Stub {
6145948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott        private final Callback mCallback;
6245948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott        InternalCallback(Callback cb) {
6345948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott            mCallback = cb;
6445948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott        }
6545948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott
6645948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott        public void complete(String url, ParcelFileDescriptor result) {
6745948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott            mCallback.complete(url, result);
6845948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott        }
6945948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott    }
7045948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott
7145948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott    /**
7245948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott     * Render the list of <var>urls</var> and invoke the <var>callback</var>
7345948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott     * for each result.
7445948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott     * @param urls  A List of urls to render.
7545948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott     * @param width  The desired width of the result.
7645948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott     * @param height  The desired height of the result.
7745948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott     * @param callback  An instance of {@link Callback} invoked for each url.
7845948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott     */
7945948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott    public void render(List<String> urls, int width, int height,
8045948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott            Callback callback) {
8145948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott        if (mService != null) {
8245948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott            try {
8345948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott                mService.render(urls, width, height,
8445948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott                        new InternalCallback(callback));
8545948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott            } catch (RemoteException ex) {
8645948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott            }
8745948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott        }
8845948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott    }
8945948fd407da525e6c8721ba75cfc8b356fc7e0fPatrick Scott}
90