mobile/android/base/MediaCastingBar.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/MediaCastingBar.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,118 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko;
     1.9 +
    1.10 +import org.mozilla.gecko.util.GeckoEventListener;
    1.11 +import org.mozilla.gecko.util.ThreadUtils;
    1.12 +
    1.13 +import org.json.JSONObject;
    1.14 +
    1.15 +import android.content.Context;
    1.16 +import android.text.TextUtils;
    1.17 +import android.util.AttributeSet;
    1.18 +import android.util.Log;
    1.19 +import android.view.LayoutInflater;
    1.20 +import android.view.View;
    1.21 +import android.widget.ImageButton;
    1.22 +import android.widget.RelativeLayout;
    1.23 +import android.widget.TextView;
    1.24 +
    1.25 +public class MediaCastingBar extends RelativeLayout implements View.OnClickListener, GeckoEventListener  {
    1.26 +    private static final String LOGTAG = "MediaCastingBar";
    1.27 +
    1.28 +    private TextView mCastingTo;
    1.29 +    private ImageButton mMediaPlay;
    1.30 +    private ImageButton mMediaPause;
    1.31 +    private ImageButton mMediaStop;
    1.32 +
    1.33 +    private boolean mInflated = false;
    1.34 +
    1.35 +    public MediaCastingBar(Context context, AttributeSet attrs) {
    1.36 +        super(context, attrs);
    1.37 +
    1.38 +        GeckoAppShell.getEventDispatcher().registerEventListener("Casting:Started", this);
    1.39 +        GeckoAppShell.getEventDispatcher().registerEventListener("Casting:Stopped", this);
    1.40 +    }
    1.41 +
    1.42 +    public void inflateContent() {
    1.43 +        LayoutInflater inflater = LayoutInflater.from(getContext());
    1.44 +        View content = inflater.inflate(R.layout.media_casting, this);
    1.45 +
    1.46 +        mMediaPlay = (ImageButton) content.findViewById(R.id.media_play);
    1.47 +        mMediaPlay.setOnClickListener(this);
    1.48 +        mMediaPause = (ImageButton) content.findViewById(R.id.media_pause);
    1.49 +        mMediaPause.setOnClickListener(this);
    1.50 +        mMediaStop = (ImageButton) content.findViewById(R.id.media_stop);
    1.51 +        mMediaStop.setOnClickListener(this);
    1.52 +
    1.53 +        mCastingTo = (TextView) content.findViewById(R.id.media_casting_to);
    1.54 +
    1.55 +        // Capture clicks on the rest of the view to prevent them from
    1.56 +        // leaking into other views positioned below.
    1.57 +        content.setOnClickListener(this);
    1.58 +
    1.59 +        mInflated = true;
    1.60 +    }
    1.61 +
    1.62 +    public void show() {
    1.63 +        if (!mInflated)
    1.64 +            inflateContent();
    1.65 +
    1.66 +        setVisibility(VISIBLE);
    1.67 +    }
    1.68 +
    1.69 +    public void hide() {
    1.70 +        setVisibility(GONE);
    1.71 +    }
    1.72 +
    1.73 +    public void onDestroy() {
    1.74 +        GeckoAppShell.getEventDispatcher().unregisterEventListener("Casting:Started", this);
    1.75 +        GeckoAppShell.getEventDispatcher().unregisterEventListener("Casting:Stopped", this);
    1.76 +    }
    1.77 +
    1.78 +    // View.OnClickListener implementation
    1.79 +    @Override
    1.80 +    public void onClick(View v) {
    1.81 +        final int viewId = v.getId();
    1.82 +
    1.83 +        if (viewId == R.id.media_play) {
    1.84 +            GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Casting:Play", ""));
    1.85 +            mMediaPlay.setVisibility(GONE);
    1.86 +            mMediaPause.setVisibility(VISIBLE);
    1.87 +        } else if (viewId == R.id.media_pause) {
    1.88 +            GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Casting:Pause", ""));
    1.89 +            mMediaPause.setVisibility(GONE);
    1.90 +            mMediaPlay.setVisibility(VISIBLE);
    1.91 +        } else if (viewId == R.id.media_stop) {
    1.92 +            GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Casting:Stop", ""));
    1.93 +        }
    1.94 +    }
    1.95 +
    1.96 +    // GeckoEventListener implementation
    1.97 +    @Override
    1.98 +    public void handleMessage(final String event, final JSONObject message) {
    1.99 +        final String device = message.optString("device");
   1.100 +
   1.101 +        ThreadUtils.postToUiThread(new Runnable() {
   1.102 +            @Override
   1.103 +            public void run() {
   1.104 +                if (event.equals("Casting:Started")) {
   1.105 +                    show();
   1.106 +                    if (!TextUtils.isEmpty(device)) {
   1.107 +                        mCastingTo.setText(device);
   1.108 +                    } else {
   1.109 +                        // Should not happen
   1.110 +                        mCastingTo.setText("");
   1.111 +                        Log.d(LOGTAG, "Device name is empty.");
   1.112 +                    }
   1.113 +                    mMediaPlay.setVisibility(GONE);
   1.114 +                    mMediaPause.setVisibility(VISIBLE);
   1.115 +                } else if (event.equals("Casting:Stopped")) {
   1.116 +                    hide();
   1.117 +                }
   1.118 +            }
   1.119 +        });
   1.120 +    }
   1.121 +}

mercurial