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.mediapicker; 17 18 import android.content.Context; 19 import android.os.SystemClock; 20 import android.util.AttributeSet; 21 import android.widget.Chronometer; 22 23 import com.android.messaging.ui.PlaybackStateView; 24 25 /** 26 * A pausable Chronometer implementation. The default Chronometer in Android only stops the UI 27 * from updating when you call stop(), but doesn't actually pause it. This implementation adds an 28 * additional timestamp that tracks the timespan for the pause and compensate for that. 29 */ 30 public class PausableChronometer extends Chronometer implements PlaybackStateView { 31 // Keeps track of how far long the Chronometer has been tracking when it's paused. We'd like 32 // to start from this time the next time it's resumed. 33 private long mTimeWhenPaused = 0; 34 PausableChronometer(final Context context, final AttributeSet attrs)35 public PausableChronometer(final Context context, final AttributeSet attrs) { 36 super(context, attrs); 37 } 38 39 /** 40 * Reset the timer and start counting from zero. 41 */ 42 @Override restart()43 public void restart() { 44 reset(); 45 start(); 46 } 47 48 /** 49 * Reset the timer to zero, but don't start it. 50 */ 51 @Override reset()52 public void reset() { 53 stop(); 54 setBase(SystemClock.elapsedRealtime()); 55 mTimeWhenPaused = 0; 56 } 57 58 /** 59 * Resume the timer after a previous pause. 60 */ 61 @Override resume()62 public void resume() { 63 setBase(SystemClock.elapsedRealtime() - mTimeWhenPaused); 64 start(); 65 } 66 67 /** 68 * Pause the timer. 69 */ 70 @Override pause()71 public void pause() { 72 stop(); 73 mTimeWhenPaused = SystemClock.elapsedRealtime() - getBase(); 74 } 75 } 76