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