1 /* 2 * Copyright (C) 2018 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.dialer.voicemail.settings; 18 19 import android.Manifest; 20 import android.app.Activity; 21 import android.content.Intent; 22 import android.content.pm.PackageManager; 23 import android.media.MediaPlayer; 24 import android.os.Bundle; 25 import android.support.annotation.NonNull; 26 import android.support.v4.app.ActivityCompat; 27 import android.view.View; 28 import android.view.View.OnClickListener; 29 import android.widget.ImageButton; 30 import android.widget.TextView; 31 import com.android.dialer.common.LogUtil; 32 import com.android.dialer.widget.DialerToolbar; 33 import java.io.IOException; 34 import java.util.Locale; 35 36 /** Activity to display current voicemail greeting and allow user to navigate to record a new one */ 37 public class CurrentVoicemailGreetingActivity extends Activity { 38 public static final String VOICEMAIL_GREETING_FILEPATH_KEY = "canonVoicemailGreetingFilePathKey"; 39 40 private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200; 41 42 private boolean permissionToRecordAccepted = false; 43 44 private ImageButton changeGreetingButton; 45 private ImageButton playButton; 46 47 private DialerToolbar currentVoicemailGreetingDialerToolbar; 48 49 private int greetingDuration = -1; 50 51 private MediaPlayer mediaPlayer; 52 53 private TextView playbackProgressLabel; 54 private View playbackDisplay; 55 56 private String voicemailGreetingFilePath = ""; 57 58 @Override onCreate(Bundle savedInstanceState)59 protected void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 setContentView(R.layout.activity_current_voicemail_greeting); 62 63 playbackDisplay = findViewById(R.id.current_voicemail_greeting_recording_display); 64 playbackProgressLabel = (TextView) findViewById(R.id.playback_progress_text_view); 65 currentVoicemailGreetingDialerToolbar = (DialerToolbar) findViewById(R.id.toolbar); 66 67 currentVoicemailGreetingDialerToolbar.setTitle( 68 R.string.voicemail_change_greeting_preference_title); 69 70 changeGreetingButton = (ImageButton) findViewById(R.id.change_greeting_button); 71 changeGreetingButton.setOnClickListener( 72 new OnClickListener() { 73 @Override 74 public void onClick(View v) { 75 // TODO(sabowitz): Implement this in CL child beta01. 76 } 77 }); 78 79 playButton = (ImageButton) findViewById(R.id.play_button); 80 playButton.setOnClickListener( 81 new OnClickListener() { 82 @Override 83 public void onClick(View v) { 84 // TODO(sabowitz): Finish implementing this in CL child beta02. 85 } 86 }); 87 88 displayCurrentVoicemailGreetingStatus(); 89 } 90 91 @Override onStart()92 public void onStart() { 93 ActivityCompat.requestPermissions( 94 this, new String[] {Manifest.permission.RECORD_AUDIO}, REQUEST_RECORD_AUDIO_PERMISSION); 95 96 if (isGreetingRecorded()) { 97 mediaPlayer = new MediaPlayer(); 98 try { 99 mediaPlayer.setDataSource(voicemailGreetingFilePath); 100 mediaPlayer.prepare(); 101 } catch (IOException e) { 102 LogUtil.e("CurrentVoicemailGreetingActivity.onStart", "mediaPlayer setup failed."); 103 } 104 } 105 super.onStart(); 106 } 107 108 @Override onPause()109 public void onPause() { 110 if (isGreetingRecorded()) { 111 if (mediaPlayer.isPlaying()) { 112 mediaPlayer.release(); 113 mediaPlayer = null; 114 } 115 } 116 super.onPause(); 117 } 118 119 @Override onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)120 public void onRequestPermissionsResult( 121 int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 122 super.onRequestPermissionsResult(requestCode, permissions, grantResults); 123 124 if (requestCode == REQUEST_RECORD_AUDIO_PERMISSION) { 125 permissionToRecordAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED; 126 } 127 if (!permissionToRecordAccepted) { 128 LogUtil.w( 129 "CurrentVoicemailGreetingActivity.onRequestPermissionsResult", 130 "permissionToRecordAccepted = false."); 131 // TODO(sabowitz): Implement error dialog logic in a child CL. 132 } 133 } 134 isGreetingRecorded()135 private boolean isGreetingRecorded() { 136 Intent intent = getIntent(); 137 if (intent.hasExtra(VOICEMAIL_GREETING_FILEPATH_KEY)) { 138 String filePathProxy = intent.getStringExtra(VOICEMAIL_GREETING_FILEPATH_KEY); 139 if (filePathProxy == null || filePathProxy.length() == 0) { 140 return false; 141 } 142 if (mediaPlayer == null) { 143 mediaPlayer = new MediaPlayer(); 144 } 145 try { 146 mediaPlayer.setDataSource(filePathProxy); 147 int durationProxy = mediaPlayer.getDuration(); 148 greetingDuration = durationProxy; 149 voicemailGreetingFilePath = filePathProxy; 150 mediaPlayer = null; 151 return true; 152 } catch (IOException e) { 153 LogUtil.e("CurrentVoicemailGreetingActivity.isGreetingRecorded", "bad filepath."); 154 mediaPlayer = null; 155 return false; 156 } 157 } 158 return false; 159 } 160 displayCurrentVoicemailGreetingStatus()161 private void displayCurrentVoicemailGreetingStatus() { 162 if (isGreetingRecorded()) { 163 String durationLabel = String.format(Locale.US, "00:%d", greetingDuration); 164 playbackProgressLabel.setText(durationLabel); 165 } else { 166 playbackDisplay.setVisibility(View.GONE); 167 } 168 } 169 } 170