From ffe0739990cf46d71ff71866baee767209926305 Mon Sep 17 00:00:00 2001 From: Android Dialer Date: Thu, 21 Jun 2018 18:19:18 -0700 Subject: Set up framework of methods for RecordButton class Added setTracks() method to update RecordButton progress Added getBasePaint() method and initialized track paints Added onSizeChanged method Added main logic for onDraw() method Added onClick method for recording activity Added basic state transition logic for both classes Created drawables for RecordButton.java Added dimensions file for RecordButton.java - trackWidth and centerIconRadius Test: "No Tests" PiperOrigin-RevId: 201621063 Change-Id: I9a8d9bc275da5198f41c7a12d84ed26d90141e85 --- .../dialer/voicemail/settings/RecordButton.java | 140 ++++++++++++++++++++- .../settings/RecordVoicemailGreetingActivity.java | 72 ++++++++++- .../res/drawable/start_playback_drawable.xml | 29 +++++ .../res/drawable/start_recording_drawable.xml | 29 +++++ .../res/drawable/stop_playback_drawable.xml | 31 +++++ .../res/drawable/stop_recording_drawable.xml | 31 +++++ .../settings/res/values/dimens_record_button.xml | 20 +++ 7 files changed, 349 insertions(+), 3 deletions(-) create mode 100644 java/com/android/dialer/voicemail/settings/res/drawable/start_playback_drawable.xml create mode 100644 java/com/android/dialer/voicemail/settings/res/drawable/start_recording_drawable.xml create mode 100644 java/com/android/dialer/voicemail/settings/res/drawable/stop_playback_drawable.xml create mode 100644 java/com/android/dialer/voicemail/settings/res/drawable/stop_recording_drawable.xml create mode 100644 java/com/android/dialer/voicemail/settings/res/values/dimens_record_button.xml (limited to 'java') diff --git a/java/com/android/dialer/voicemail/settings/RecordButton.java b/java/com/android/dialer/voicemail/settings/RecordButton.java index f144576d4..d62637053 100644 --- a/java/com/android/dialer/voicemail/settings/RecordButton.java +++ b/java/com/android/dialer/voicemail/settings/RecordButton.java @@ -17,21 +17,157 @@ package com.android.dialer.voicemail.settings; import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Rect; +import android.graphics.RectF; +import android.graphics.drawable.Drawable; +import android.support.v4.content.ContextCompat; import android.util.AttributeSet; import android.widget.Button; +import com.android.dialer.voicemail.settings.RecordVoicemailGreetingActivity.ButtonState; /** Custom Button View for Dialer voicemail greeting recording */ public class RecordButton extends Button { + private final float trackWidth = getResources().getDimensionPixelSize(R.dimen.track_width); + private final int centerIconRadius = + getResources().getDimensionPixelSize(R.dimen.center_icon_radius); + private final int secondaryTrackAlpha = 64; + + private float mainTrackFraction; + private float secondaryTrackFraction; + + private Rect centerIconRect; + private RectF bodyRect; + + private Drawable readyDrawable; + private Drawable recordingDrawable; + private Drawable recordedDrawable; + private Drawable playingDrawable; + private Drawable currentCenterDrawable; + + private Paint mainTrackPaint; + private Paint secondaryTrackPaint; + public RecordButton(Context context) { super(context); + init(); } public RecordButton(Context context, AttributeSet attrs) { super(context, attrs); + init(); + } + + public RecordButton(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(); + } + + /** + * Updates bounds for main and secondary tracks and the size of the center Drawable based on View + * resizing + */ + @Override + protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) { + // Canvas.drawArc() method draws from center of stroke, so the trackWidth must be accounted for + float viewRadius = Math.min(width, height) / 2f - trackWidth; + float centerX = width / 2f; + float centerY = viewRadius + trackWidth / 2f; + + bodyRect = + new RectF( + centerX - viewRadius, centerY - viewRadius, centerX + viewRadius, centerY + viewRadius); + + centerIconRect = + new Rect( + (int) centerX - centerIconRadius, + (int) centerY - centerIconRadius, + (int) centerX + centerIconRadius, + (int) centerY + centerIconRadius); + } + + private void init() { + readyDrawable = ContextCompat.getDrawable(getContext(), R.drawable.start_recording_drawable); + recordingDrawable = ContextCompat.getDrawable(getContext(), R.drawable.stop_recording_drawable); + recordedDrawable = ContextCompat.getDrawable(getContext(), R.drawable.start_playback_drawable); + playingDrawable = ContextCompat.getDrawable(getContext(), R.drawable.stop_playback_drawable); + + mainTrackPaint = getBasePaint(R.color.dialer_call_green); + secondaryTrackPaint = getBasePaint(R.color.dialer_call_green); + secondaryTrackPaint.setAlpha(secondaryTrackAlpha); + + setState(RecordVoicemailGreetingActivity.RECORD_GREETING_INIT); + } + + /** Returns Paint with base attributes for drawing the main and secondary tracks */ + private Paint getBasePaint(int id) { + Paint paint = new Paint(); + paint.setAntiAlias(true); + paint.setStrokeWidth(trackWidth); + paint.setStrokeCap(Paint.Cap.ROUND); + paint.setStyle(Paint.Style.STROKE); + paint.setColor(ContextCompat.getColor(getContext(), id)); + return paint; + } + + /** Sets the fraction value of progress tracks, this will trigger a redraw of the button. */ + public void setTracks(float mainTrackFraction, float secondaryTrackFraction) { + this.mainTrackFraction = mainTrackFraction; + this.secondaryTrackFraction = secondaryTrackFraction; + invalidate(); + } + + /** + * Sets internal state of RecordButton. This will also trigger UI refresh of the button to reflect + * the new state. + */ + public void setState(@ButtonState int state) { + switch (state) { + case RecordVoicemailGreetingActivity.RECORD_GREETING_INIT: + mainTrackPaint = getBasePaint(R.color.dialer_call_green); + secondaryTrackPaint = getBasePaint(R.color.dialer_call_green); + secondaryTrackPaint.setAlpha(secondaryTrackAlpha); + currentCenterDrawable = readyDrawable; + break; + case RecordVoicemailGreetingActivity.RECORD_GREETING_PLAYING_BACK: + mainTrackPaint = getBasePaint(R.color.google_blue_500); + secondaryTrackPaint = getBasePaint(R.color.google_blue_50); + currentCenterDrawable = playingDrawable; + break; + case RecordVoicemailGreetingActivity.RECORD_GREETING_RECORDED: + mainTrackPaint = getBasePaint(R.color.google_blue_500); + secondaryTrackPaint = getBasePaint(R.color.google_blue_50); + currentCenterDrawable = recordedDrawable; + break; + case RecordVoicemailGreetingActivity.RECORD_GREETING_RECORDING: + mainTrackPaint = getBasePaint(R.color.dialer_red); + secondaryTrackPaint = getBasePaint(R.color.dialer_red); + secondaryTrackPaint.setAlpha(secondaryTrackAlpha); + currentCenterDrawable = recordingDrawable; + break; + default: + throw new RuntimeException("Invalid button state"); + } + refreshDrawableState(); + invalidate(); } - public RecordButton(Context context, AttributeSet attrs, int defStyleAttrs) { - super(context, attrs, defStyleAttrs); + /** + * Handles drawing the main and secondary track arcs and the center Drawable image based on track + * fractions and the Button's current state + */ + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + canvas.drawArc(bodyRect, -90, secondaryTrackFraction * 360, false, secondaryTrackPaint); + canvas.drawArc(bodyRect, -90, mainTrackFraction * 360, false, mainTrackPaint); + + // TODO(marquelle) - Add pulse + + currentCenterDrawable.setBounds(centerIconRect); + currentCenterDrawable.draw(canvas); } } diff --git a/java/com/android/dialer/voicemail/settings/RecordVoicemailGreetingActivity.java b/java/com/android/dialer/voicemail/settings/RecordVoicemailGreetingActivity.java index 750636faa..17f8282c5 100644 --- a/java/com/android/dialer/voicemail/settings/RecordVoicemailGreetingActivity.java +++ b/java/com/android/dialer/voicemail/settings/RecordVoicemailGreetingActivity.java @@ -18,13 +18,83 @@ package com.android.dialer.voicemail.settings; import android.app.Activity; import android.os.Bundle; +import android.support.annotation.IntDef; +import android.view.View; +import android.view.View.OnClickListener; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; /** Activity for recording a new voicemail greeting */ -public class RecordVoicemailGreetingActivity extends Activity { +public class RecordVoicemailGreetingActivity extends Activity implements OnClickListener { + + /** Possible states of RecordButton and RecordVoicemailGreetingActivity */ + @Retention(RetentionPolicy.SOURCE) + @IntDef({ + RECORD_GREETING_INIT, + RECORD_GREETING_RECORDED, + RECORD_GREETING_RECORDED, + RECORD_GREETING_PLAYING_BACK + }) + public @interface ButtonState {} + + public static final int RECORD_GREETING_INIT = 1; + public static final int RECORD_GREETING_RECORDING = 2; + public static final int RECORD_GREETING_RECORDED = 3; + public static final int RECORD_GREETING_PLAYING_BACK = 4; + public static final int MAX_GREETING_DURATION_MS = 45000; + + private int currentState; + private int duration; + private RecordButton recordButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_record_voicemail_greeting); + + duration = 0; + setState(RECORD_GREETING_INIT); + } + + @Override + public void onClick(View v) { + if (v == recordButton) { + switch (currentState) { + case RECORD_GREETING_INIT: + setState(RECORD_GREETING_RECORDING); + break; + case RECORD_GREETING_RECORDED: + setState(RECORD_GREETING_PLAYING_BACK); + break; + case RECORD_GREETING_RECORDING: + case RECORD_GREETING_PLAYING_BACK: + setState(RECORD_GREETING_RECORDED); + break; + default: + break; + } + } + } + + private void setState(@ButtonState int state) { + currentState = state; + + switch (state) { + case RECORD_GREETING_INIT: + recordButton.setState(state); + recordButton.setTracks(0, 0); + break; + case RECORD_GREETING_PLAYING_BACK: + case RECORD_GREETING_RECORDED: + recordButton.setState(state); + recordButton.setTracks(0, (float) duration / MAX_GREETING_DURATION_MS); + break; + case RECORD_GREETING_RECORDING: + recordButton.setState(state); + recordButton.setTracks(0, 1f); + break; + default: + break; + } } } diff --git a/java/com/android/dialer/voicemail/settings/res/drawable/start_playback_drawable.xml b/java/com/android/dialer/voicemail/settings/res/drawable/start_playback_drawable.xml new file mode 100644 index 000000000..2a7ec4a7c --- /dev/null +++ b/java/com/android/dialer/voicemail/settings/res/drawable/start_playback_drawable.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + diff --git a/java/com/android/dialer/voicemail/settings/res/drawable/start_recording_drawable.xml b/java/com/android/dialer/voicemail/settings/res/drawable/start_recording_drawable.xml new file mode 100644 index 000000000..42d27fc2a --- /dev/null +++ b/java/com/android/dialer/voicemail/settings/res/drawable/start_recording_drawable.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + diff --git a/java/com/android/dialer/voicemail/settings/res/drawable/stop_playback_drawable.xml b/java/com/android/dialer/voicemail/settings/res/drawable/stop_playback_drawable.xml new file mode 100644 index 000000000..24bf90b40 --- /dev/null +++ b/java/com/android/dialer/voicemail/settings/res/drawable/stop_playback_drawable.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + diff --git a/java/com/android/dialer/voicemail/settings/res/drawable/stop_recording_drawable.xml b/java/com/android/dialer/voicemail/settings/res/drawable/stop_recording_drawable.xml new file mode 100644 index 000000000..cff539d54 --- /dev/null +++ b/java/com/android/dialer/voicemail/settings/res/drawable/stop_recording_drawable.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + diff --git a/java/com/android/dialer/voicemail/settings/res/values/dimens_record_button.xml b/java/com/android/dialer/voicemail/settings/res/values/dimens_record_button.xml new file mode 100644 index 000000000..a45f987c2 --- /dev/null +++ b/java/com/android/dialer/voicemail/settings/res/values/dimens_record_button.xml @@ -0,0 +1,20 @@ + + + + + 15dp + 48dp + -- cgit v1.2.3