diff options
author | Jeff Brown <jeffbrown@google.com> | 2012-09-07 16:00:12 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-09-07 16:00:13 -0700 |
commit | 7017e48380ab0c1be033594bb2a9331898ad5be8 (patch) | |
tree | 1e24e747937706deb96ac1770f71a9c4cb6ebc65 /media/java | |
parent | cd620591b764cd999f18878985444fba01d5b710 (diff) | |
parent | cbad976b2a36a0895ca94510d5208a86f66cf596 (diff) | |
download | frameworks_base-7017e48380ab0c1be033594bb2a9331898ad5be8.zip frameworks_base-7017e48380ab0c1be033594bb2a9331898ad5be8.tar.gz frameworks_base-7017e48380ab0c1be033594bb2a9331898ad5be8.tar.bz2 |
Merge "Add support for Wifi display." into jb-mr1-dev
Diffstat (limited to 'media/java')
-rw-r--r-- | media/java/android/media/RemoteDisplay.java | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/media/java/android/media/RemoteDisplay.java b/media/java/android/media/RemoteDisplay.java new file mode 100644 index 0000000..b463d26 --- /dev/null +++ b/media/java/android/media/RemoteDisplay.java @@ -0,0 +1,153 @@ +/* + * Copyright (C) 2012 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.media; + +import dalvik.system.CloseGuard; + +import android.os.Handler; +import android.view.Surface; + +/** + * Listens for Wifi remote display connections managed by the media server. + * + * @hide + */ +public final class RemoteDisplay { + /* these constants must be kept in sync with IRemoteDisplayClient.h */ + + public static final int DISPLAY_FLAG_SECURE = 1 << 0; + + public static final int DISPLAY_ERROR_UNKOWN = 1; + public static final int DISPLAY_ERROR_CONNECTION_DROPPED = 2; + + private final CloseGuard mGuard = CloseGuard.get(); + private final Listener mListener; + private final Handler mHandler; + + private int mPtr; + + private native int nativeListen(String iface); + private native void nativeDispose(int ptr); + + private RemoteDisplay(Listener listener, Handler handler) { + mListener = listener; + mHandler = handler; + } + + @Override + protected void finalize() throws Throwable { + try { + dispose(true); + } finally { + super.finalize(); + } + } + + /** + * Starts listening for displays to be connected on the specified interface. + * + * @param iface The interface address and port in the form "x.x.x.x:y". + * @param listener The listener to invoke when displays are connected or disconnected. + * @param handler The handler on which to invoke the listener. + */ + public static RemoteDisplay listen(String iface, Listener listener, Handler handler) { + if (iface == null) { + throw new IllegalArgumentException("iface must not be null"); + } + if (listener == null) { + throw new IllegalArgumentException("listener must not be null"); + } + if (handler == null) { + throw new IllegalArgumentException("handler must not be null"); + } + + RemoteDisplay display = new RemoteDisplay(listener, handler); + display.startListening(iface); + return display; + } + + /** + * Disconnects the remote display and stops listening for new connections. + */ + public void dispose() { + dispose(false); + } + + private void dispose(boolean finalized) { + if (mPtr != 0) { + if (mGuard != null) { + if (finalized) { + mGuard.warnIfOpen(); + } else { + mGuard.close(); + } + } + + nativeDispose(mPtr); + mPtr = 0; + } + } + + private void startListening(String iface) { + mPtr = nativeListen(iface); + if (mPtr == 0) { + throw new IllegalStateException("Could not start listening for " + + "remote display connection on \"" + iface + "\""); + } + mGuard.open("dispose"); + } + + // Called from native. + private void notifyDisplayConnected(final Surface surface, + final int width, final int height, final int flags) { + mHandler.post(new Runnable() { + @Override + public void run() { + mListener.onDisplayConnected(surface, width, height, flags); + } + }); + } + + // Called from native. + private void notifyDisplayDisconnected() { + mHandler.post(new Runnable() { + @Override + public void run() { + mListener.onDisplayDisconnected(); + } + }); + } + + // Called from native. + private void notifyDisplayError(final int error) { + mHandler.post(new Runnable() { + @Override + public void run() { + mListener.onDisplayError(error); + } + }); + } + + /** + * Listener invoked when the remote display connection changes state. + */ + public interface Listener { + void onDisplayConnected(Surface surface, int width, int height, int flags); + void onDisplayDisconnected(); + void onDisplayError(int error); + } +} |