1 /* 2 * Copyright 2015, 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.android.cts.verifier.managedprovisioning; 18 19 import android.app.Dialog; 20 import android.app.DialogFragment; 21 import android.content.DialogInterface; 22 import android.graphics.Bitmap; 23 import android.net.Uri; 24 import android.media.MediaPlayer; 25 import android.media.MediaPlayer.OnPreparedListener; 26 import android.os.Bundle; 27 import android.util.Log; 28 import android.view.View; 29 import android.view.View.OnClickListener; 30 import android.widget.Button; 31 import android.widget.ImageView; 32 import android.widget.Toast; 33 import android.widget.VideoView; 34 35 import com.android.cts.verifier.R; 36 37 import java.io.IOException; 38 39 /** 40 * This dialog shows/plays an image, video or audio uri. 41 */ 42 public class ByodPresentMediaDialog extends DialogFragment { 43 static final String TAG = "ByodPresentMediaDialog"; 44 45 private static final String KEY_VIDEO_URI = "video"; 46 private static final String KEY_IMAGE_URI = "image"; 47 private static final String KEY_AUDIO_URI = "audio"; 48 49 /** 50 * Get a dialogFragment showing an image. 51 */ newImageInstance(Uri uri)52 public static ByodPresentMediaDialog newImageInstance(Uri uri) { 53 ByodPresentMediaDialog dialog = new ByodPresentMediaDialog(); 54 Bundle args = new Bundle(); 55 args.putParcelable(KEY_IMAGE_URI, uri); 56 dialog.setArguments(args); 57 return dialog; 58 } 59 60 /** 61 * Get a dialogFragment playing a video. 62 */ newVideoInstance(Uri uri)63 public static ByodPresentMediaDialog newVideoInstance(Uri uri) { 64 ByodPresentMediaDialog dialog = new ByodPresentMediaDialog(); 65 Bundle args = new Bundle(); 66 args.putParcelable(KEY_VIDEO_URI, uri); 67 dialog.setArguments(args); 68 return dialog; 69 } 70 71 /** 72 * Get a dialogFragment playing audio. 73 */ newAudioInstance(Uri uri)74 public static ByodPresentMediaDialog newAudioInstance(Uri uri) { 75 ByodPresentMediaDialog dialog = new ByodPresentMediaDialog(); 76 Bundle args = new Bundle(); 77 args.putParcelable(KEY_AUDIO_URI, uri); 78 dialog.setArguments(args); 79 return dialog; 80 } 81 82 @Override onCreateDialog(Bundle savedInstanceState)83 public Dialog onCreateDialog(Bundle savedInstanceState) { 84 final Dialog dialog = new Dialog(getActivity()); 85 dialog.setContentView(R.layout.byod_present_media); 86 87 Button dismissButton = (Button) dialog.findViewById(R.id.dismissButton); 88 dismissButton.setOnClickListener(new View.OnClickListener() { 89 public void onClick(View v) { 90 dismiss(); 91 ((DialogCallback) getActivity()).onDialogClose(); 92 } 93 }); 94 95 Bundle arguments = getArguments(); 96 97 // Initially all video and image specific UI is invisible. 98 if (arguments.containsKey(KEY_VIDEO_URI)) { 99 // Show video UI. 100 dialog.setTitle(getString(R.string.provisioning_byod_verify_video_title)); 101 102 Uri uri = (Uri) getArguments().getParcelable(KEY_VIDEO_URI); 103 final VideoView videoView = (VideoView) dialog.findViewById(R.id.videoView); 104 videoView.setVisibility(View.VISIBLE); 105 videoView.setVideoURI(uri); 106 107 Button playButton = (Button) dialog.findViewById(R.id.playButton); 108 playButton.setVisibility(View.VISIBLE); 109 playButton.setOnClickListener(new View.OnClickListener() { 110 public void onClick(View v) { 111 videoView.start(); 112 } 113 }); 114 } else if (arguments.containsKey(KEY_IMAGE_URI)) { 115 // Show image UI. 116 dialog.setTitle(getString(R.string.provisioning_byod_verify_image_title)); 117 118 Uri uri = (Uri) getArguments().getParcelable(KEY_IMAGE_URI); 119 ImageView imageView = (ImageView) dialog.findViewById(R.id.imageView); 120 imageView.setVisibility(View.VISIBLE); 121 imageView.setImageURI(uri); 122 } else if (arguments.containsKey(KEY_AUDIO_URI)) { 123 // Show audio playback UI. 124 dialog.setTitle(getString(R.string.provisioning_byod_verify_audio_title)); 125 126 Uri uri = (Uri) getArguments().getParcelable(KEY_AUDIO_URI); 127 final MediaPlayer mediaPlayer = new MediaPlayer(); 128 final Button playButton = (Button) dialog.findViewById(R.id.playButton); 129 playButton.setVisibility(View.VISIBLE); 130 playButton.setEnabled(false); 131 132 try { 133 mediaPlayer.setDataSource(getActivity(), uri); 134 mediaPlayer.prepare(); 135 } catch (IllegalArgumentException|SecurityException|IllegalStateException 136 |IOException e) { 137 Log.e(TAG, "Cannot play given audio with media player.", e); 138 Toast.makeText(getActivity(), R.string.provisioning_byod_capture_media_error, 139 Toast.LENGTH_SHORT).show(); 140 getActivity().finish(); 141 } 142 143 mediaPlayer.setOnPreparedListener(new OnPreparedListener() { 144 @Override 145 public void onPrepared(MediaPlayer mp) { 146 playButton.setEnabled(true); 147 playButton.setOnClickListener(new View.OnClickListener() { 148 public void onClick(View v) { 149 mediaPlayer.start(); 150 } 151 }); 152 } 153 }); 154 } 155 156 return dialog; 157 } 158 159 @Override onCancel(DialogInterface dialog)160 public void onCancel(DialogInterface dialog) { 161 ((DialogCallback) getActivity()).onDialogClose(); 162 } 163 164 public interface DialogCallback { onDialogClose()165 public abstract void onDialogClose(); 166 } 167 } 168