1 /* 2 * Copyright (C) 2008 Esmertec AG. 3 * Copyright (C) 2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.mms.ui; 19 20 import com.android.mms.R; 21 22 import android.content.Context; 23 import android.graphics.Bitmap; 24 import android.graphics.BitmapFactory; 25 import android.media.MediaPlayer; 26 import android.net.Uri; 27 import android.text.method.HideReturnsTransformationMethod; 28 import android.util.AttributeSet; 29 import android.util.Config; 30 import android.util.Log; 31 import android.view.LayoutInflater; 32 import android.view.View; 33 import android.widget.AbsoluteLayout; 34 import android.widget.ImageView; 35 import android.widget.ScrollView; 36 import android.widget.TextView; 37 import android.widget.VideoView; 38 39 import java.io.IOException; 40 import java.util.Map; 41 42 /** 43 * A basic view to show the contents of a slide. 44 */ 45 public class SlideView extends AbsoluteLayout implements 46 AdaptableSlideViewInterface { 47 private static final String TAG = "SlideView"; 48 private static final boolean DEBUG = false; 49 private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV; 50 // FIXME: Need getHeight from mAudioInfoView instead of constant AUDIO_INFO_HEIGHT. 51 private static final int AUDIO_INFO_HEIGHT = 82; 52 53 private View mAudioInfoView; 54 private ImageView mImageView; 55 private VideoView mVideoView; 56 private ScrollView mScrollText; 57 private TextView mTextView; 58 private OnSizeChangedListener mSizeChangedListener; 59 private MediaPlayer mAudioPlayer; 60 private boolean mIsPrepared; 61 private boolean mStartWhenPrepared; 62 private int mSeekWhenPrepared; 63 private boolean mStopWhenPrepared; 64 MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() { 65 public void onPrepared(MediaPlayer mp) { 66 mIsPrepared = true; 67 if (mSeekWhenPrepared > 0) { 68 mAudioPlayer.seekTo(mSeekWhenPrepared); 69 mSeekWhenPrepared = 0; 70 } 71 if (mStartWhenPrepared) { 72 mAudioPlayer.start(); 73 mStartWhenPrepared = false; 74 displayAudioInfo(); 75 } 76 if (mStopWhenPrepared) { 77 mAudioPlayer.stop(); 78 mAudioPlayer.release(); 79 mAudioPlayer = null; 80 mStopWhenPrepared = false; 81 hideAudioInfo(); 82 } 83 } 84 }; 85 SlideView(Context context)86 public SlideView(Context context) { 87 super(context); 88 } 89 SlideView(Context context, AttributeSet attrs)90 public SlideView(Context context, AttributeSet attrs) { 91 super(context, attrs); 92 } 93 setImage(String name, Bitmap bitmap)94 public void setImage(String name, Bitmap bitmap) { 95 if (mImageView == null) { 96 mImageView = new ImageView(mContext); 97 addView(mImageView, new LayoutParams( 98 0, 0, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 99 if (DEBUG) { 100 mImageView.setBackgroundColor(0xFFFF0000); 101 } 102 } 103 104 if (null == bitmap) { 105 bitmap = BitmapFactory.decodeResource(getResources(), 106 R.drawable.ic_missing_thumbnail_picture); 107 } 108 mImageView.setImageBitmap(bitmap); 109 } 110 setImageRegion(int left, int top, int width, int height)111 public void setImageRegion(int left, int top, int width, int height) { 112 if (mImageView != null) { 113 mImageView.setLayoutParams(new LayoutParams(width, height, left, top)); 114 } 115 } 116 setImageRegionFit(String fit)117 public void setImageRegionFit(String fit) { 118 // TODO Auto-generated method stub 119 } 120 setVideo(String name, Uri video)121 public void setVideo(String name, Uri video) { 122 if (mVideoView == null) { 123 mVideoView = new VideoView(mContext); 124 addView(mVideoView, new LayoutParams( 125 0, 0, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 126 if (DEBUG) { 127 mVideoView.setBackgroundColor(0xFFFF0000); 128 } 129 } 130 131 if (LOCAL_LOGV) { 132 Log.v(TAG, "Changing video source to " + video); 133 } 134 mVideoView.setVideoURI(video); 135 } 136 initAudioInfoView(String name)137 private void initAudioInfoView(String name) { 138 LayoutInflater factory = LayoutInflater.from(getContext()); 139 mAudioInfoView = factory.inflate(R.layout.playing_audio_info, null); 140 int height = mAudioInfoView.getHeight(); 141 TextView audioName = (TextView) mAudioInfoView.findViewById(R.id.name); 142 audioName.setText(name); 143 addView(mAudioInfoView, new LayoutParams( 144 LayoutParams.FILL_PARENT, AUDIO_INFO_HEIGHT, 145 0, getHeight() - AUDIO_INFO_HEIGHT)); 146 if (DEBUG) { 147 mAudioInfoView.setBackgroundColor(0xFFFF0000); 148 } 149 150 mAudioInfoView.setVisibility(View.GONE); 151 } 152 displayAudioInfo()153 private void displayAudioInfo() { 154 if (null != mAudioInfoView) { 155 mAudioInfoView.setVisibility(View.VISIBLE); 156 } 157 } 158 hideAudioInfo()159 private void hideAudioInfo() { 160 if (null != mAudioInfoView) { 161 mAudioInfoView.setVisibility(View.GONE); 162 } 163 } 164 setAudio(Uri audio, String name, Map<String, ?> extras)165 public void setAudio(Uri audio, String name, Map<String, ?> extras) { 166 if (audio == null) { 167 throw new IllegalArgumentException("Audio URI may not be null."); 168 } 169 170 if (LOCAL_LOGV) { 171 Log.v(TAG, "Changing audio source to " + audio); 172 } 173 174 if (mAudioPlayer != null) { 175 mAudioPlayer.reset(); 176 mAudioPlayer.release(); 177 mAudioPlayer = null; 178 } 179 mIsPrepared = false; 180 181 try { 182 mAudioPlayer = new MediaPlayer(); 183 mAudioPlayer.setOnPreparedListener(mPreparedListener); 184 mAudioPlayer.setDataSource(mContext, audio); 185 mAudioPlayer.prepareAsync(); 186 } catch (IOException e) { 187 Log.e(TAG, "Unexpected IOException.", e); 188 mAudioPlayer.release(); 189 mAudioPlayer = null; 190 } 191 initAudioInfoView(name); 192 } 193 setText(String name, String text)194 public void setText(String name, String text) { 195 if (null == mScrollText) { 196 mScrollText = new ScrollView(mContext); 197 mScrollText.setScrollBarStyle(SCROLLBARS_OUTSIDE_INSET); 198 addView(mScrollText, new LayoutParams( 199 0, 0, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 200 if (DEBUG) { 201 mScrollText.setBackgroundColor(0xFF00FF00); 202 } 203 } 204 205 if (null == mTextView) { 206 mTextView = new TextView(mContext); 207 mTextView.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); 208 mScrollText.addView(mTextView); 209 } 210 211 mScrollText.requestFocus(); 212 mTextView.setText(text); 213 } 214 setTextRegion(int left, int top, int width, int height)215 public void setTextRegion(int left, int top, int width, int height) { 216 if (mScrollText != null) { 217 mScrollText.setLayoutParams(new LayoutParams(width, height, left, top)); 218 } 219 } 220 setVideoRegion(int left, int top, int width, int height)221 public void setVideoRegion(int left, int top, int width, int height) { 222 if (mVideoView != null) { 223 mVideoView.setLayoutParams(new LayoutParams(width, height, left, top)); 224 } 225 } 226 setImageVisibility(boolean visible)227 public void setImageVisibility(boolean visible) { 228 if (mImageView != null) { 229 mImageView.setVisibility(visible ? View.VISIBLE : View.INVISIBLE); 230 } 231 } 232 setTextVisibility(boolean visible)233 public void setTextVisibility(boolean visible) { 234 if (mScrollText != null) { 235 mScrollText.setVisibility(visible ? View.VISIBLE : View.INVISIBLE); 236 } 237 } 238 setVideoVisibility(boolean visible)239 public void setVideoVisibility(boolean visible) { 240 if (mVideoView != null) { 241 mVideoView.setVisibility(visible ? View.VISIBLE : View.INVISIBLE); 242 } 243 } 244 startAudio()245 public void startAudio() { 246 if ((mAudioPlayer != null) && mIsPrepared) { 247 mAudioPlayer.start(); 248 mStartWhenPrepared = false; 249 displayAudioInfo(); 250 } else { 251 mStartWhenPrepared = true; 252 } 253 } 254 stopAudio()255 public void stopAudio() { 256 if ((mAudioPlayer != null) && mIsPrepared) { 257 mAudioPlayer.stop(); 258 mAudioPlayer.release(); 259 mAudioPlayer = null; 260 hideAudioInfo(); 261 } else { 262 mStopWhenPrepared = true; 263 } 264 } 265 pauseAudio()266 public void pauseAudio() { 267 if ((mAudioPlayer != null) && mIsPrepared) { 268 if (mAudioPlayer.isPlaying()) { 269 mAudioPlayer.pause(); 270 } 271 } 272 mStartWhenPrepared = false; 273 } 274 seekAudio(int seekTo)275 public void seekAudio(int seekTo) { 276 if ((mAudioPlayer != null) && mIsPrepared) { 277 mAudioPlayer.seekTo(seekTo); 278 } else { 279 mSeekWhenPrepared = seekTo; 280 } 281 } 282 startVideo()283 public void startVideo() { 284 if (mVideoView != null) { 285 if (LOCAL_LOGV) { 286 Log.v(TAG, "Starting video playback."); 287 } 288 mVideoView.start(); 289 } 290 } 291 stopVideo()292 public void stopVideo() { 293 if ((mVideoView != null)) { 294 if (LOCAL_LOGV) { 295 Log.v(TAG, "Stopping video playback."); 296 } 297 mVideoView.stopPlayback(); 298 } 299 } 300 pauseVideo()301 public void pauseVideo() { 302 if (mVideoView != null) { 303 if (LOCAL_LOGV) { 304 Log.v(TAG, "Pausing video playback."); 305 } 306 mVideoView.pause(); 307 } 308 } 309 seekVideo(int seekTo)310 public void seekVideo(int seekTo) { 311 if (mVideoView != null) { 312 if (seekTo > 0) { 313 if (LOCAL_LOGV) { 314 Log.v(TAG, "Seeking video playback to " + seekTo); 315 } 316 mVideoView.seekTo(seekTo); 317 } 318 } 319 } 320 reset()321 public void reset() { 322 if (null != mScrollText) { 323 mScrollText.setVisibility(View.GONE); 324 } 325 326 if (null != mImageView) { 327 mImageView.setVisibility(View.GONE); 328 } 329 330 if (null != mAudioPlayer) { 331 stopAudio(); 332 } 333 334 if (null != mVideoView) { 335 stopVideo(); 336 mVideoView.setVisibility(View.GONE); 337 } 338 } 339 setVisibility(boolean visible)340 public void setVisibility(boolean visible) { 341 // TODO Auto-generated method stub 342 } 343 344 @Override onSizeChanged(int w, int h, int oldw, int oldh)345 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 346 super.onSizeChanged(w, h, oldw, oldh); 347 348 if (mSizeChangedListener != null) { 349 if (LOCAL_LOGV) { 350 Log.v(TAG, "new size=" + w + "x" + h); 351 } 352 mSizeChangedListener.onSizeChanged(w, h - AUDIO_INFO_HEIGHT); 353 } 354 } 355 setOnSizeChangedListener(OnSizeChangedListener l)356 public void setOnSizeChangedListener(OnSizeChangedListener l) { 357 mSizeChangedListener = l; 358 } 359 } 360