private void readFromSharedPref(SharedPreferences prefs) { mStartTime = prefs.getLong(Stopwatches.PREF_START_TIME, 0); mAccumulatedTime = prefs.getLong(Stopwatches.PREF_ACCUM_TIME, 0); mState = prefs.getInt(Stopwatches.PREF_STATE, Stopwatches.STOPWATCH_RESET); int numLaps = prefs.getInt(Stopwatches.PREF_LAP_NUM, Stopwatches.STOPWATCH_RESET); if (mLapsAdapter != null) { long[] oldLaps = mLapsAdapter.getLapTimes(); if (oldLaps == null || oldLaps.length < numLaps) { long[] laps = new long[numLaps]; long prevLapElapsedTime = 0; for (int lap_i = 0; lap_i < numLaps; lap_i++) { String key = Stopwatches.PREF_LAP_TIME + Integer.toString(lap_i + 1); long lap = prefs.getLong(key, 0); laps[numLaps - lap_i - 1] = lap - prevLapElapsedTime; prevLapElapsedTime = lap; } mLapsAdapter.setLapTimes(laps); } } if (prefs.getBoolean(Stopwatches.PREF_UPDATE_CIRCLE, true)) { if (mState == Stopwatches.STOPWATCH_STOPPED) { doStop(); } else if (mState == Stopwatches.STOPWATCH_RUNNING) { doStart(mStartTime); } else if (mState == Stopwatches.STOPWATCH_RESET) { doReset(); } } }
/** * * Handle action when user presses the lap button * * @param time - in hundredth of a second */ private void addLapTime(long time) { // The total elapsed time final long curTime = time - mStartTime + mAccumulatedTime; int size = mLapsAdapter.getCount(); if (size == 0) { // Create and add the first lap Lap firstLap = new Lap(curTime, curTime); mLapsAdapter.addLap(firstLap); // Create the first active lap mLapsAdapter.addLap(new Lap(0, curTime)); // Update the interval on the clock and check the lap and total time formatting mTime.setIntervalTime(curTime); mLapsAdapter.updateTimeFormats(firstLap); } else { // Finish active lap final long lapTime = curTime - mLapsAdapter.getItem(1).mTotalTime; mLapsAdapter.getItem(0).mLapTime = lapTime; mLapsAdapter.getItem(0).mTotalTime = curTime; // Create a new active lap mLapsAdapter.addLap(new Lap(0, curTime)); // Update marker on clock and check that formatting for the lap number mTime.setMarkerTime(lapTime); mLapsAdapter.updateLapFormat(); } // Repaint the laps list mLapsAdapter.notifyDataSetChanged(); // Start lap animation starting from the second lap mTime.stopIntervalAnimation(); if (!reachedMaxLaps()) { mTime.startIntervalAnimation(); } }
private void writeToSharedPref(SharedPreferences prefs) { SharedPreferences.Editor editor = prefs.edit(); editor.putLong(Stopwatches.PREF_START_TIME, mStartTime); editor.putLong(Stopwatches.PREF_ACCUM_TIME, mAccumulatedTime); editor.putInt(Stopwatches.PREF_STATE, mState); if (mLapsAdapter != null) { long[] laps = mLapsAdapter.getLapTimes(); if (laps != null) { editor.putInt(Stopwatches.PREF_LAP_NUM, laps.length); for (int i = 0; i < laps.length; i++) { String key = Stopwatches.PREF_LAP_TIME + Integer.toString(laps.length - i); editor.putLong(key, laps[i]); } } } if (mState == Stopwatches.STOPWATCH_RUNNING) { editor.putLong(Stopwatches.NOTIF_CLOCK_BASE, mStartTime - mAccumulatedTime); editor.putLong(Stopwatches.NOTIF_CLOCK_ELAPSED, -1); editor.putBoolean(Stopwatches.NOTIF_CLOCK_RUNNING, true); } else if (mState == Stopwatches.STOPWATCH_STOPPED) { editor.putLong(Stopwatches.NOTIF_CLOCK_ELAPSED, mAccumulatedTime); editor.putLong(Stopwatches.NOTIF_CLOCK_BASE, -1); editor.putBoolean(Stopwatches.NOTIF_CLOCK_RUNNING, false); } else if (mState == Stopwatches.STOPWATCH_RESET) { editor.remove(Stopwatches.NOTIF_CLOCK_BASE); editor.remove(Stopwatches.NOTIF_CLOCK_RUNNING); editor.remove(Stopwatches.NOTIF_CLOCK_ELAPSED); } editor.putBoolean(Stopwatches.PREF_UPDATE_CIRCLE, false); editor.apply(); }
private void updateCurrentLap(long totalTime) { // There are either 0, 2 or more Laps in the list See {@link #addLapTime} if (mLapsAdapter.getCount() > 0) { Lap curLap = mLapsAdapter.getItem(0); curLap.mLapTime = totalTime - mLapsAdapter.getItem(1).mTotalTime; curLap.mTotalTime = totalTime; // If this lap has caused a change in the format for total and/or lap time, all of // the rows need a fresh print. The simplest way to refresh all of the rows is // calling notifyDataSetChanged. if (mLapsAdapter.updateTimeFormats(curLap)) { mLapsAdapter.notifyDataSetChanged(); } else { curLap.updateView(); } } }
/** Show or hide the laps-list */ private void showLaps() { if (DEBUG) Log.v( LOG_TAG, String.format("StopwatchFragment.showLaps: count=%d", mLapsAdapter.getCount())); boolean lapsVisible = mLapsAdapter.getCount() > 0; // Layout change animations will start upon the first add/hide view. Temporarily disable // the layout transition animation for the spacers, make the changes, then re-enable // the animation for the add/hide laps-list if (mSpacersUsed) { int spacersVisibility = lapsVisible ? View.GONE : View.VISIBLE; ViewGroup rootView = (ViewGroup) getView(); if (rootView != null) { rootView.setLayoutTransition(null); if (mStartSpace != null) { mStartSpace.setVisibility(spacersVisibility); } if (mEndSpace != null) { mEndSpace.setVisibility(spacersVisibility); } rootView.setLayoutTransition(mLayoutTransition); } } if (lapsVisible) { // There are laps - show the laps-list // No delay for the CircleButtonsLayout changes - start immediately so that the // circle has shifted before the laps-list starts appearing. mCircleLayoutTransition.setStartDelay(LayoutTransition.CHANGING, 0); mLapsList.setVisibility(View.VISIBLE); } else { // There are no laps - hide the laps list // Delay the CircleButtonsLayout animation until after the laps-list disappears long startDelay = mLayoutTransition.getStartDelay(LayoutTransition.DISAPPEARING) + mLayoutTransition.getDuration(LayoutTransition.DISAPPEARING); mCircleLayoutTransition.setStartDelay(LayoutTransition.CHANGING, startDelay); mLapsList.setVisibility(View.GONE); } }
@Override public void run() { long curTime = Utils.getTimeNow(); long totalTime = mAccumulatedTime + (curTime - mStartTime); if (mTime != null) { mTimeText.setTime(totalTime, true, true); } if (mLapsAdapter.getCount() > 0) { updateCurrentLap(totalTime); } mTime.postDelayed(mTimeUpdateThread, 10); }
private Intent getShareIntent() { Intent intent = new Intent(android.content.Intent.ACTION_SEND); intent.setType("text/plain"); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); intent.putExtra( Intent.EXTRA_SUBJECT, Stopwatches.getShareTitle(getActivity().getApplicationContext())); intent.putExtra( Intent.EXTRA_TEXT, Stopwatches.buildShareResults( getActivity().getApplicationContext(), mTimeText.getTimeString(), getLapShareTimes(mLapsAdapter.getLapTimes()))); return intent; }
private void doReset() { if (DEBUG) Log.v(LOG_TAG, "StopwatchFragment.doReset"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); Utils.clearSwSharedPref(prefs); mTime.clearSharedPref(prefs, "sw"); mAccumulatedTime = 0; mLapsAdapter.clearLaps(); showLaps(); mTime.stopIntervalAnimation(); mTime.reset(); mTimeText.setTime(mAccumulatedTime, true, true); mTimeText.blinkTimeStr(false); setButtons(Stopwatches.STOPWATCH_RESET); mState = Stopwatches.STOPWATCH_RESET; }
/** * Make the final display setup. * * <p>If the fragment is starting with an existing list of laps, shows the laps list and if the * spacers around the clock exist, hide them. If there are not laps at the start, hide the laps * list and show the clock spacers if they exist. */ @Override public void onStart() { super.onStart(); boolean lapsVisible = mLapsAdapter.getCount() > 0; mLapsList.setVisibility(lapsVisible ? View.VISIBLE : View.GONE); if (mSpacersUsed) { int spacersVisibility = lapsVisible ? View.GONE : View.VISIBLE; if (mStartSpace != null) { mStartSpace.setVisibility(spacersVisibility); } if (mEndSpace != null) { mEndSpace.setVisibility(spacersVisibility); } } ((ViewGroup) getView()).setLayoutTransition(mLayoutTransition); mCircleLayout.setLayoutTransition(mCircleLayoutTransition); }
private boolean reachedMaxLaps() { return mLapsAdapter.getCount() >= Stopwatches.MAX_LAPS; }