michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import org.mozilla.gecko.util.GeckoEventListener; michael@0: import org.mozilla.gecko.util.ThreadUtils; michael@0: michael@0: import org.json.JSONObject; michael@0: michael@0: import android.content.Context; michael@0: import android.text.TextUtils; michael@0: import android.util.AttributeSet; michael@0: import android.util.Log; michael@0: import android.view.LayoutInflater; michael@0: import android.view.View; michael@0: import android.widget.ImageButton; michael@0: import android.widget.RelativeLayout; michael@0: import android.widget.TextView; michael@0: michael@0: public class MediaCastingBar extends RelativeLayout implements View.OnClickListener, GeckoEventListener { michael@0: private static final String LOGTAG = "MediaCastingBar"; michael@0: michael@0: private TextView mCastingTo; michael@0: private ImageButton mMediaPlay; michael@0: private ImageButton mMediaPause; michael@0: private ImageButton mMediaStop; michael@0: michael@0: private boolean mInflated = false; michael@0: michael@0: public MediaCastingBar(Context context, AttributeSet attrs) { michael@0: super(context, attrs); michael@0: michael@0: GeckoAppShell.getEventDispatcher().registerEventListener("Casting:Started", this); michael@0: GeckoAppShell.getEventDispatcher().registerEventListener("Casting:Stopped", this); michael@0: } michael@0: michael@0: public void inflateContent() { michael@0: LayoutInflater inflater = LayoutInflater.from(getContext()); michael@0: View content = inflater.inflate(R.layout.media_casting, this); michael@0: michael@0: mMediaPlay = (ImageButton) content.findViewById(R.id.media_play); michael@0: mMediaPlay.setOnClickListener(this); michael@0: mMediaPause = (ImageButton) content.findViewById(R.id.media_pause); michael@0: mMediaPause.setOnClickListener(this); michael@0: mMediaStop = (ImageButton) content.findViewById(R.id.media_stop); michael@0: mMediaStop.setOnClickListener(this); michael@0: michael@0: mCastingTo = (TextView) content.findViewById(R.id.media_casting_to); michael@0: michael@0: // Capture clicks on the rest of the view to prevent them from michael@0: // leaking into other views positioned below. michael@0: content.setOnClickListener(this); michael@0: michael@0: mInflated = true; michael@0: } michael@0: michael@0: public void show() { michael@0: if (!mInflated) michael@0: inflateContent(); michael@0: michael@0: setVisibility(VISIBLE); michael@0: } michael@0: michael@0: public void hide() { michael@0: setVisibility(GONE); michael@0: } michael@0: michael@0: public void onDestroy() { michael@0: GeckoAppShell.getEventDispatcher().unregisterEventListener("Casting:Started", this); michael@0: GeckoAppShell.getEventDispatcher().unregisterEventListener("Casting:Stopped", this); michael@0: } michael@0: michael@0: // View.OnClickListener implementation michael@0: @Override michael@0: public void onClick(View v) { michael@0: final int viewId = v.getId(); michael@0: michael@0: if (viewId == R.id.media_play) { michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Casting:Play", "")); michael@0: mMediaPlay.setVisibility(GONE); michael@0: mMediaPause.setVisibility(VISIBLE); michael@0: } else if (viewId == R.id.media_pause) { michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Casting:Pause", "")); michael@0: mMediaPause.setVisibility(GONE); michael@0: mMediaPlay.setVisibility(VISIBLE); michael@0: } else if (viewId == R.id.media_stop) { michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Casting:Stop", "")); michael@0: } michael@0: } michael@0: michael@0: // GeckoEventListener implementation michael@0: @Override michael@0: public void handleMessage(final String event, final JSONObject message) { michael@0: final String device = message.optString("device"); michael@0: michael@0: ThreadUtils.postToUiThread(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: if (event.equals("Casting:Started")) { michael@0: show(); michael@0: if (!TextUtils.isEmpty(device)) { michael@0: mCastingTo.setText(device); michael@0: } else { michael@0: // Should not happen michael@0: mCastingTo.setText(""); michael@0: Log.d(LOGTAG, "Device name is empty."); michael@0: } michael@0: mMediaPlay.setVisibility(GONE); michael@0: mMediaPause.setVisibility(VISIBLE); michael@0: } else if (event.equals("Casting:Stopped")) { michael@0: hide(); michael@0: } michael@0: } michael@0: }); michael@0: } michael@0: }