mobile/android/base/MediaCastingBar.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     5 package org.mozilla.gecko;
     7 import org.mozilla.gecko.util.GeckoEventListener;
     8 import org.mozilla.gecko.util.ThreadUtils;
    10 import org.json.JSONObject;
    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;
    22 public class MediaCastingBar extends RelativeLayout implements View.OnClickListener, GeckoEventListener  {
    23     private static final String LOGTAG = "MediaCastingBar";
    25     private TextView mCastingTo;
    26     private ImageButton mMediaPlay;
    27     private ImageButton mMediaPause;
    28     private ImageButton mMediaStop;
    30     private boolean mInflated = false;
    32     public MediaCastingBar(Context context, AttributeSet attrs) {
    33         super(context, attrs);
    35         GeckoAppShell.getEventDispatcher().registerEventListener("Casting:Started", this);
    36         GeckoAppShell.getEventDispatcher().registerEventListener("Casting:Stopped", this);
    37     }
    39     public void inflateContent() {
    40         LayoutInflater inflater = LayoutInflater.from(getContext());
    41         View content = inflater.inflate(R.layout.media_casting, this);
    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);
    50         mCastingTo = (TextView) content.findViewById(R.id.media_casting_to);
    52         // Capture clicks on the rest of the view to prevent them from
    53         // leaking into other views positioned below.
    54         content.setOnClickListener(this);
    56         mInflated = true;
    57     }
    59     public void show() {
    60         if (!mInflated)
    61             inflateContent();
    63         setVisibility(VISIBLE);
    64     }
    66     public void hide() {
    67         setVisibility(GONE);
    68     }
    70     public void onDestroy() {
    71         GeckoAppShell.getEventDispatcher().unregisterEventListener("Casting:Started", this);
    72         GeckoAppShell.getEventDispatcher().unregisterEventListener("Casting:Stopped", this);
    73     }
    75     // View.OnClickListener implementation
    76     @Override
    77     public void onClick(View v) {
    78         final int viewId = v.getId();
    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     }
    93     // GeckoEventListener implementation
    94     @Override
    95     public void handleMessage(final String event, final JSONObject message) {
    96         final String device = message.optString("device");
    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 }

mercurial