Home | History | Annotate | Download | only in ui
      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 package com.android.messaging.ui;
     17 
     18 import android.animation.ObjectAnimator;
     19 import android.animation.TimeAnimator;
     20 import android.animation.TimeAnimator.TimeListener;
     21 import android.content.Context;
     22 import android.graphics.drawable.ClipDrawable;
     23 import android.graphics.drawable.Drawable;
     24 import android.os.SystemClock;
     25 import android.util.AttributeSet;
     26 import android.view.Gravity;
     27 import android.widget.ProgressBar;
     28 
     29 /**
     30  * Shows a styled progress bar that is synchronized with the playback state of an audio attachment.
     31  */
     32 public class AudioPlaybackProgressBar extends ProgressBar implements PlaybackStateView {
     33     private long mDurationInMillis;
     34     private final TimeAnimator mUpdateAnimator;
     35     private long mCumulativeTime = 0;
     36     private long mCurrentPlayStartTime = 0;
     37     private boolean mIncoming = false;
     38 
     39     public AudioPlaybackProgressBar(final Context context, final AttributeSet attrs) {
     40         super(context, attrs);
     41 
     42         mUpdateAnimator = new TimeAnimator();
     43         mUpdateAnimator.setRepeatCount(ObjectAnimator.INFINITE);
     44         mUpdateAnimator.setTimeListener(new TimeListener() {
     45             @Override
     46             public void onTimeUpdate(final TimeAnimator animation, final long totalTime,
     47                     final long deltaTime) {
     48                 int progress = 0;
     49                 if (mDurationInMillis > 0) {
     50                     progress = (int) (((mCumulativeTime + SystemClock.elapsedRealtime() -
     51                             mCurrentPlayStartTime) * 1.0f / mDurationInMillis) * 100);
     52                     progress = Math.max(Math.min(progress, 100), 0);
     53                 }
     54                 setProgress(progress);
     55             }
     56         });
     57         updateAppearance();
     58     }
     59 
     60     /**
     61      * Sets the duration of the audio that's being played, in milliseconds.
     62      */
     63     public void setDuration(final long durationInMillis) {
     64         mDurationInMillis = durationInMillis;
     65     }
     66 
     67     @Override
     68     public void restart() {
     69         reset();
     70         resume();
     71     }
     72 
     73     @Override
     74     public void reset() {
     75         stopUpdateTicks();
     76         setProgress(0);
     77         mCumulativeTime = 0;
     78         mCurrentPlayStartTime = 0;
     79     }
     80 
     81     @Override
     82     public void resume() {
     83         mCurrentPlayStartTime = SystemClock.elapsedRealtime();
     84         startUpdateTicks();
     85     }
     86 
     87     @Override
     88     public void pause() {
     89         mCumulativeTime += SystemClock.elapsedRealtime() - mCurrentPlayStartTime;
     90         stopUpdateTicks();
     91     }
     92 
     93     private void startUpdateTicks() {
     94         if (!mUpdateAnimator.isStarted()) {
     95             mUpdateAnimator.start();
     96         }
     97     }
     98 
     99     private void stopUpdateTicks() {
    100         if (mUpdateAnimator.isStarted()) {
    101             mUpdateAnimator.end();
    102         }
    103     }
    104 
    105     private void updateAppearance() {
    106         final Drawable drawable =
    107                 ConversationDrawables.get().getAudioProgressDrawable(mIncoming);
    108         final ClipDrawable clipDrawable = new ClipDrawable(drawable, Gravity.START,
    109                 ClipDrawable.HORIZONTAL);
    110         setProgressDrawable(clipDrawable);
    111         setBackground(ConversationDrawables.get()
    112                 .getAudioProgressBackgroundDrawable(mIncoming));
    113     }
    114 
    115     public void setVisualStyle(final boolean incoming) {
    116         if (mIncoming != incoming) {
    117             mIncoming = incoming;
    118             updateAppearance();
    119         }
    120     }
    121 }
    122