mobile/android/base/MediaCastingBar.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko;
michael@0 6
michael@0 7 import org.mozilla.gecko.util.GeckoEventListener;
michael@0 8 import org.mozilla.gecko.util.ThreadUtils;
michael@0 9
michael@0 10 import org.json.JSONObject;
michael@0 11
michael@0 12 import android.content.Context;
michael@0 13 import android.text.TextUtils;
michael@0 14 import android.util.AttributeSet;
michael@0 15 import android.util.Log;
michael@0 16 import android.view.LayoutInflater;
michael@0 17 import android.view.View;
michael@0 18 import android.widget.ImageButton;
michael@0 19 import android.widget.RelativeLayout;
michael@0 20 import android.widget.TextView;
michael@0 21
michael@0 22 public class MediaCastingBar extends RelativeLayout implements View.OnClickListener, GeckoEventListener {
michael@0 23 private static final String LOGTAG = "MediaCastingBar";
michael@0 24
michael@0 25 private TextView mCastingTo;
michael@0 26 private ImageButton mMediaPlay;
michael@0 27 private ImageButton mMediaPause;
michael@0 28 private ImageButton mMediaStop;
michael@0 29
michael@0 30 private boolean mInflated = false;
michael@0 31
michael@0 32 public MediaCastingBar(Context context, AttributeSet attrs) {
michael@0 33 super(context, attrs);
michael@0 34
michael@0 35 GeckoAppShell.getEventDispatcher().registerEventListener("Casting:Started", this);
michael@0 36 GeckoAppShell.getEventDispatcher().registerEventListener("Casting:Stopped", this);
michael@0 37 }
michael@0 38
michael@0 39 public void inflateContent() {
michael@0 40 LayoutInflater inflater = LayoutInflater.from(getContext());
michael@0 41 View content = inflater.inflate(R.layout.media_casting, this);
michael@0 42
michael@0 43 mMediaPlay = (ImageButton) content.findViewById(R.id.media_play);
michael@0 44 mMediaPlay.setOnClickListener(this);
michael@0 45 mMediaPause = (ImageButton) content.findViewById(R.id.media_pause);
michael@0 46 mMediaPause.setOnClickListener(this);
michael@0 47 mMediaStop = (ImageButton) content.findViewById(R.id.media_stop);
michael@0 48 mMediaStop.setOnClickListener(this);
michael@0 49
michael@0 50 mCastingTo = (TextView) content.findViewById(R.id.media_casting_to);
michael@0 51
michael@0 52 // Capture clicks on the rest of the view to prevent them from
michael@0 53 // leaking into other views positioned below.
michael@0 54 content.setOnClickListener(this);
michael@0 55
michael@0 56 mInflated = true;
michael@0 57 }
michael@0 58
michael@0 59 public void show() {
michael@0 60 if (!mInflated)
michael@0 61 inflateContent();
michael@0 62
michael@0 63 setVisibility(VISIBLE);
michael@0 64 }
michael@0 65
michael@0 66 public void hide() {
michael@0 67 setVisibility(GONE);
michael@0 68 }
michael@0 69
michael@0 70 public void onDestroy() {
michael@0 71 GeckoAppShell.getEventDispatcher().unregisterEventListener("Casting:Started", this);
michael@0 72 GeckoAppShell.getEventDispatcher().unregisterEventListener("Casting:Stopped", this);
michael@0 73 }
michael@0 74
michael@0 75 // View.OnClickListener implementation
michael@0 76 @Override
michael@0 77 public void onClick(View v) {
michael@0 78 final int viewId = v.getId();
michael@0 79
michael@0 80 if (viewId == R.id.media_play) {
michael@0 81 GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Casting:Play", ""));
michael@0 82 mMediaPlay.setVisibility(GONE);
michael@0 83 mMediaPause.setVisibility(VISIBLE);
michael@0 84 } else if (viewId == R.id.media_pause) {
michael@0 85 GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Casting:Pause", ""));
michael@0 86 mMediaPause.setVisibility(GONE);
michael@0 87 mMediaPlay.setVisibility(VISIBLE);
michael@0 88 } else if (viewId == R.id.media_stop) {
michael@0 89 GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Casting:Stop", ""));
michael@0 90 }
michael@0 91 }
michael@0 92
michael@0 93 // GeckoEventListener implementation
michael@0 94 @Override
michael@0 95 public void handleMessage(final String event, final JSONObject message) {
michael@0 96 final String device = message.optString("device");
michael@0 97
michael@0 98 ThreadUtils.postToUiThread(new Runnable() {
michael@0 99 @Override
michael@0 100 public void run() {
michael@0 101 if (event.equals("Casting:Started")) {
michael@0 102 show();
michael@0 103 if (!TextUtils.isEmpty(device)) {
michael@0 104 mCastingTo.setText(device);
michael@0 105 } else {
michael@0 106 // Should not happen
michael@0 107 mCastingTo.setText("");
michael@0 108 Log.d(LOGTAG, "Device name is empty.");
michael@0 109 }
michael@0 110 mMediaPlay.setVisibility(GONE);
michael@0 111 mMediaPause.setVisibility(VISIBLE);
michael@0 112 } else if (event.equals("Casting:Stopped")) {
michael@0 113 hide();
michael@0 114 }
michael@0 115 }
michael@0 116 });
michael@0 117 }
michael@0 118 }

mercurial