1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.supportv7.media; 18 19 import com.example.android.supportv7.R; 20 21 import android.app.Dialog; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.graphics.Bitmap; 25 import android.os.Bundle; 26 import android.support.v7.app.MediaRouteControllerDialog; 27 import android.support.v7.media.MediaRouteSelector; 28 import android.support.v7.media.MediaRouter; 29 import android.util.Log; 30 import android.view.KeyEvent; 31 import android.view.MotionEvent; 32 import android.view.View; 33 import android.widget.ImageButton; 34 import android.widget.ImageView; 35 import android.widget.LinearLayout; 36 import android.widget.TextView; 37 38 /** 39 * This class serves as an example on how to customize the media router control 40 * dialog. It is derived from the standard MediaRouteControllerDialog with the 41 * following overrides: 42 * 43 * 1. Shows thumbnail/snapshot of the current item 44 * 45 * 2. For variable volume routes, only allow volume control via Volume Up/Down 46 * keys (to prevent accidental tapping on the volume adjust seekbar that sets 47 * volume to maximum) 48 * 49 * 3. Provides transport control buttons (play/pause, stop) 50 */ 51 public class SampleMediaRouteControllerDialog extends MediaRouteControllerDialog { 52 private static final String TAG = "SampleMediaRouteControllerDialog"; 53 private final SampleMediaRouterActivity mActivity; 54 private final SessionManager mSessionManager; 55 private final Player mPlayer; 56 private ImageButton mPauseResumeButton; 57 private ImageButton mStopButton; 58 private ImageView mThumbnail; 59 private TextView mTextView; 60 private LinearLayout mInfoLayout; 61 private LinearLayout mVolumeLayout; 62 63 public SampleMediaRouteControllerDialog(Context context, 64 SessionManager manager, Player player) { 65 super(context); 66 mActivity = (SampleMediaRouterActivity) context; 67 mSessionManager = manager; 68 mPlayer = player; 69 } 70 71 @Override 72 public View onCreateMediaControlView(Bundle savedInstanceState) { 73 // Thumbnail and Track info 74 View v = getLayoutInflater().inflate(R.layout.sample_media_controller, null); 75 mInfoLayout = (LinearLayout)v.findViewById(R.id.media_route_info); 76 mTextView = (TextView)v.findViewById(R.id.track_info); 77 mThumbnail = (ImageView)v.findViewById(R.id.snapshot); 78 79 // Transport controls 80 mPauseResumeButton = (ImageButton)v.findViewById(R.id.pause_resume_button); 81 mPauseResumeButton.setOnClickListener(new View.OnClickListener() { 82 @Override 83 public void onClick(View v) { 84 if (mActivity != null) { 85 mActivity.handleMediaKey(new KeyEvent(KeyEvent.ACTION_DOWN, 86 KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE)); 87 } 88 } 89 }); 90 91 mStopButton = (ImageButton)v.findViewById(R.id.stop_button); 92 mStopButton.setOnClickListener(new View.OnClickListener() { 93 @Override 94 public void onClick(View v) { 95 if (mActivity != null) { 96 mActivity.handleMediaKey(new KeyEvent(KeyEvent.ACTION_DOWN, 97 KeyEvent.KEYCODE_MEDIA_STOP)); 98 } 99 } 100 }); 101 102 // update session status (will callback to updateUi at the end) 103 mSessionManager.updateStatus(); 104 return v; 105 } 106 107 public void updateUi() { 108 String trackInfo = mPlayer.getDescription(); 109 Bitmap snapshot = mPlayer.getSnapshot(); 110 if (mPlayer.isRemotePlayback() && !trackInfo.isEmpty() && snapshot != null) { 111 mInfoLayout.setVisibility(View.VISIBLE); 112 mThumbnail.setImageBitmap(snapshot); 113 mTextView.setText(trackInfo); 114 } else { 115 mInfoLayout.setVisibility(View.GONE); 116 } 117 // show pause or resume icon depending on current state 118 mPauseResumeButton.setImageResource(mSessionManager.isPaused() ? 119 R.drawable.ic_media_play : R.drawable.ic_media_pause); 120 } 121 } 122