1 /* 2 * Copyright (C) 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.messaging.ui.mediapicker; 18 19 import android.Manifest; 20 import android.content.pm.PackageManager; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 25 import com.android.messaging.R; 26 import com.android.messaging.datamodel.data.MessagePartData; 27 import com.android.messaging.util.OsUtil; 28 29 /** 30 * Chooser which allows the user to record audio 31 */ 32 class AudioMediaChooser extends MediaChooser implements 33 AudioRecordView.HostInterface { 34 private View mEnabledView; 35 private View mMissingPermissionView; 36 AudioMediaChooser(final MediaPicker mediaPicker)37 AudioMediaChooser(final MediaPicker mediaPicker) { 38 super(mediaPicker); 39 } 40 41 @Override getSupportedMediaTypes()42 public int getSupportedMediaTypes() { 43 return MediaPicker.MEDIA_TYPE_AUDIO; 44 } 45 46 @Override getIconResource()47 public int getIconResource() { 48 return R.drawable.ic_audio_light; 49 } 50 51 @Override getIconDescriptionResource()52 public int getIconDescriptionResource() { 53 return R.string.mediapicker_audioChooserDescription; 54 } 55 56 @Override onAudioRecorded(final MessagePartData item)57 public void onAudioRecorded(final MessagePartData item) { 58 mMediaPicker.dispatchItemsSelected(item, true); 59 } 60 61 @Override setThemeColor(final int color)62 public void setThemeColor(final int color) { 63 if (mView != null) { 64 ((AudioRecordView) mView).setThemeColor(color); 65 } 66 } 67 68 @Override createView(final ViewGroup container)69 protected View createView(final ViewGroup container) { 70 final LayoutInflater inflater = getLayoutInflater(); 71 final AudioRecordView view = (AudioRecordView) inflater.inflate( 72 R.layout.mediapicker_audio_chooser, 73 container /* root */, 74 false /* attachToRoot */); 75 view.setHostInterface(this); 76 view.setThemeColor(mMediaPicker.getConversationThemeColor()); 77 mEnabledView = view.findViewById(R.id.mediapicker_enabled); 78 mMissingPermissionView = view.findViewById(R.id.missing_permission_view); 79 return view; 80 } 81 82 @Override getActionBarTitleResId()83 int getActionBarTitleResId() { 84 return R.string.mediapicker_audio_title; 85 } 86 87 @Override isHandlingTouch()88 public boolean isHandlingTouch() { 89 // Whenever the user is in the process of recording audio, we want to allow the user 90 // to move the finger within the panel without interpreting that as dragging the media 91 // picker panel. 92 return ((AudioRecordView) mView).shouldHandleTouch(); 93 } 94 95 @Override stopTouchHandling()96 public void stopTouchHandling() { 97 ((AudioRecordView) mView).stopTouchHandling(); 98 } 99 100 @Override onPause()101 public void onPause() { 102 super.onPause(); 103 if (mView != null) { 104 ((AudioRecordView) mView).onPause(); 105 } 106 } 107 108 @Override setSelected(final boolean selected)109 protected void setSelected(final boolean selected) { 110 super.setSelected(selected); 111 if (selected && !OsUtil.hasRecordAudioPermission()) { 112 requestRecordAudioPermission(); 113 } 114 } 115 requestRecordAudioPermission()116 private void requestRecordAudioPermission() { 117 mMediaPicker.requestPermissions(new String[] { Manifest.permission.RECORD_AUDIO }, 118 MediaPicker.RECORD_AUDIO_PERMISSION_REQUEST_CODE); 119 } 120 121 @Override onRequestPermissionsResult( final int requestCode, final String permissions[], final int[] grantResults)122 protected void onRequestPermissionsResult( 123 final int requestCode, final String permissions[], final int[] grantResults) { 124 if (requestCode == MediaPicker.RECORD_AUDIO_PERMISSION_REQUEST_CODE) { 125 final boolean permissionGranted = grantResults[0] == PackageManager.PERMISSION_GRANTED; 126 mEnabledView.setVisibility(permissionGranted ? View.VISIBLE : View.GONE); 127 mMissingPermissionView.setVisibility(permissionGranted ? View.GONE : View.VISIBLE); 128 } 129 } 130 } 131