diff options
Diffstat (limited to 'src/com/android/deskclock/stopwatch/StopwatchFragment.java')
-rw-r--r-- | src/com/android/deskclock/stopwatch/StopwatchFragment.java | 113 |
1 files changed, 97 insertions, 16 deletions
diff --git a/src/com/android/deskclock/stopwatch/StopwatchFragment.java b/src/com/android/deskclock/stopwatch/StopwatchFragment.java index b751cc048..6a8aa5140 100644 --- a/src/com/android/deskclock/stopwatch/StopwatchFragment.java +++ b/src/com/android/deskclock/stopwatch/StopwatchFragment.java @@ -9,7 +9,10 @@ import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.graphics.drawable.Drawable; import android.os.Bundle; +import android.os.PowerManager; +import android.os.PowerManager.WakeLock; import android.preference.PreferenceManager; +import android.text.format.DateUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -17,7 +20,6 @@ import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; -import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ListPopupWindow; @@ -28,7 +30,6 @@ import android.widget.TextView; import com.android.deskclock.CircleButtonsLinearLayout; import com.android.deskclock.CircleTimerView; import com.android.deskclock.DeskClock; -import com.android.deskclock.DeskClock.OnTapListener; import com.android.deskclock.DeskClockFragment; import com.android.deskclock.Log; import com.android.deskclock.R; @@ -38,10 +39,10 @@ import com.android.deskclock.timer.CountingTimerView; import java.util.ArrayList; import java.util.List; -/** - * TODO: Insert description here. (generated by isaackatz) - */ -public class StopwatchFragment extends DeskClockFragment implements OnSharedPreferenceChangeListener{ +public class StopwatchFragment extends DeskClockFragment + implements OnSharedPreferenceChangeListener { + + private static final String TAG = "StopwatchFragment"; int mState = Stopwatches.STOPWATCH_RESET; // Stopwatch views that are accessed by the activity @@ -52,6 +53,7 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref private ListView mLapsList; private ImageButton mShareButton; private ListPopupWindow mSharePopup; + private WakeLock mWakeLock; // Used for calculating the time from the start taking into account the pause times long mStartTime = 0; @@ -78,10 +80,26 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref ArrayList<Lap> mLaps = new ArrayList<Lap>(); private final LayoutInflater mInflater; private final int mBackgroundColor; + private final String[] mFormats; + private final String[] mLapFormatSet; + // Size of this array must match the size of formats + private final long[] mThresholds = { + 10 * DateUtils.MINUTE_IN_MILLIS, // < 10 minutes + DateUtils.HOUR_IN_MILLIS, // < 1 hour + 10 * DateUtils.HOUR_IN_MILLIS, // < 10 hours + 100 * DateUtils.HOUR_IN_MILLIS, // < 100 hours + 1000 * DateUtils.HOUR_IN_MILLIS // < 1000 hours + }; + private int mLapIndex = 0; + private int mTotalIndex = 0; + private String mLapFormat; public LapsListAdapter(Context context) { mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mBackgroundColor = getResources().getColor(R.color.blackish); + mFormats = context.getResources().getStringArray(R.array.stopwatch_format_set); + mLapFormatSet = context.getResources().getStringArray(R.array.sw_lap_number_set); + updateLapFormat(); } @Override @@ -103,10 +121,11 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref TextView count = (TextView)lapInfo.findViewById(R.id.lap_number); TextView lapTime = (TextView)lapInfo.findViewById(R.id.lap_time); TextView toalTime = (TextView)lapInfo.findViewById(R.id.lap_total); - lapTime.setText(Stopwatches.getTimeText(mLaps.get(position).mLapTime)); - toalTime.setText(Stopwatches.getTimeText(mLaps.get(position).mTotalTime)); - count.setText(getString(R.string.sw_notification_lap_number, mLaps.size() - position) - .toUpperCase()); + lapTime.setText(Stopwatches.formatTimeText(mLaps.get(position).mLapTime, + mFormats[mLapIndex])); + toalTime.setText(Stopwatches.formatTimeText(mLaps.get(position).mTotalTime, + mFormats[mTotalIndex])); + count.setText(String.format(mLapFormat, mLaps.size() - position).toUpperCase()); lapInfo.setBackgroundColor(mBackgroundColor); return lapInfo; @@ -125,13 +144,38 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref return mLaps.get(position); } + private void updateLapFormat() { + // Note Stopwatches.MAX_LAPS < 100 + mLapFormat = mLapFormatSet[mLaps.size() < 10 ? 0 : 1]; + } + + private void resetTimeFormats() { + mLapIndex = mTotalIndex = 0; + } + + public boolean updateTimeFormats(Lap lap) { + boolean formatChanged = false; + while (mLapIndex + 1 < mThresholds.length && lap.mLapTime >= mThresholds[mLapIndex]) { + mLapIndex++; + formatChanged = true; + } + while (mTotalIndex + 1 < mThresholds.length && + lap.mTotalTime >= mThresholds[mTotalIndex]) { + mTotalIndex++; + formatChanged = true; + } + return formatChanged; + } + public void addLap(Lap l) { mLaps.add(0, l); - notifyDataSetChanged(); + // for efficiency caller also calls notifyDataSetChanged() } public void clearLaps() { mLaps.clear(); + updateLapFormat(); + resetTimeFormats(); notifyDataSetChanged(); } @@ -163,7 +207,9 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref for (int i = size -1; i >= 0; i --) { totalTime += laps[i]; mLaps.get(i).mTotalTime = totalTime; + updateTimeFormats(mLaps.get(i)); } + updateLapFormat(); notifyDataSetChanged(); } } @@ -185,7 +231,6 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref Intent intent = new Intent(context, StopwatchService.class); intent.putExtra(Stopwatches.MESSAGE_TIME, time); intent.putExtra(Stopwatches.SHOW_NOTIF, false); - buttonClicked(true); switch (mState) { case Stopwatches.STOPWATCH_RUNNING: // do stop @@ -194,6 +239,7 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref doStop(); intent.setAction(Stopwatches.STOP_STOPWATCH); context.startService(intent); + releaseWakeLock(); break; case Stopwatches.STOPWATCH_RESET: case Stopwatches.STOPWATCH_STOPPED: @@ -201,6 +247,7 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref doStart(time); intent.setAction(Stopwatches.START_STOPWATCH); context.startService(intent); + acquireWakeLock(); break; default: Log.wtf("Illegal state " + mState @@ -224,7 +271,6 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref Intent intent = new Intent(context, StopwatchService.class); intent.putExtra(Stopwatches.MESSAGE_TIME, time); intent.putExtra(Stopwatches.SHOW_NOTIF, false); - buttonClicked(true); switch (mState) { case Stopwatches.STOPWATCH_RUNNING: // Save lap time @@ -238,6 +284,7 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref doReset(); intent.setAction(Stopwatches.RESET_STOPWATCH); context.startService(intent); + releaseWakeLock(); break; default: Log.wtf("Illegal state " + mState @@ -300,12 +347,13 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref setButtons(mState); mTimeText.setTime(mAccumulatedTime, true, true); if (mState == Stopwatches.STOPWATCH_RUNNING) { + acquireWakeLock(); startUpdateThread(); } else if (mState == Stopwatches.STOPWATCH_STOPPED && mAccumulatedTime != 0) { mTimeText.blinkTimeStr(true); } showLaps(); - + ((DeskClock)getActivity()).registerPageChangedListener(this); super.onResume(); } @@ -325,10 +373,20 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref mSharePopup.dismiss(); mSharePopup = null; } - + ((DeskClock)getActivity()).unregisterPageChangedListener(this); + releaseWakeLock(); super.onPause(); } + @Override + public void onPageChanged(int page) { + if (page == DeskClock.STOPWATCH_TAB_INDEX && mState == Stopwatches.STOPWATCH_RUNNING) { + acquireWakeLock(); + } else { + releaseWakeLock(); + } + } + private void doStop() { stopUpdateThread(); mTime.pauseIntervalAnimation(); @@ -555,15 +613,18 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref long curTime = time - mStartTime + mAccumulatedTime; if (size == 0) { // Always show the ending lap and a new one - mLapsAdapter.addLap(new Lap(curTime, curTime)); + Lap firstLap = new Lap(curTime, curTime); + mLapsAdapter.addLap(firstLap); mLapsAdapter.addLap(new Lap(0, curTime)); mTime.setIntervalTime(curTime); + mLapsAdapter.updateTimeFormats(firstLap); } else { long lapTime = curTime - ((Lap) mLapsAdapter.getItem(1)).mTotalTime; ((Lap)mLapsAdapter.getItem(0)).mLapTime = lapTime; ((Lap)mLapsAdapter.getItem(0)).mTotalTime = curTime; mLapsAdapter.addLap(new Lap(0, 0)); mTime.setMarkerTime(lapTime); + mLapsAdapter.updateLapFormat(); // mTime.setIntervalTime(lapTime * 10); } mLapsAdapter.notifyDataSetChanged(); @@ -743,4 +804,24 @@ public class StopwatchFragment extends DeskClockFragment implements OnSharedPref } } } + + // Used to keeps screen on when stopwatch is running. + + private void acquireWakeLock() { + if (mWakeLock == null) { + final PowerManager pm = + (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE); + mWakeLock = pm.newWakeLock( + PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG); + mWakeLock.setReferenceCounted(false); + } + mWakeLock.acquire(); + } + + private void releaseWakeLock() { + if (mWakeLock != null && mWakeLock.isHeld()) { + mWakeLock.release(); + } + } + } |