From 9f06c87aa50871fe7945a8d5a7ec359622e36b97 Mon Sep 17 00:00:00 2001 From: Mudar Noufal Date: Mon, 13 Jan 2014 10:12:57 -0500 Subject: [PATCH] Fixed AlarmManager minor bugs --- .../PeaceOfMindBroadCastReceiver.java | 8 +- .../peaceofmind/ui/PeaceOfMindActivity.java | 10 +- .../peaceofmind/ui/VerticalSeekBar.java | 164 +++++++++--------- .../peaceofmind/utils/AlarmManagerHelper.java | 6 +- 4 files changed, 94 insertions(+), 94 deletions(-) diff --git a/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/receiver/PeaceOfMindBroadCastReceiver.java b/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/receiver/PeaceOfMindBroadCastReceiver.java index 3a2b47c..d42e6ba 100644 --- a/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/receiver/PeaceOfMindBroadCastReceiver.java +++ b/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/receiver/PeaceOfMindBroadCastReceiver.java @@ -149,7 +149,7 @@ private void updateDuration(Intent intent) { final long currentTimeRounded = TimeHelper.getRoundedCurrentTimeMillis(); mCurrentStats.mCurrentRun.mDuration = newDuration; - mCurrentStats.mCurrentRun.mTargetTime = currentTimeRounded + newDuration; + mCurrentStats.mCurrentRun.mTargetTime = mCurrentStats.mCurrentRun.mStartTime + newDuration; PeaceOfMindPrefs.saveToSharedPreferences(mCurrentStats, mSharedPreferences); @@ -159,7 +159,7 @@ private void updateDuration(Intent intent) { mContext.sendBroadcast(updateIntent); - AlarmManagerHelper.updateAlarm(mContext, currentTimeRounded + mCurrentStats.mCurrentRun.mDuration); + AlarmManagerHelper.updateAlarm(mContext, mCurrentStats.mCurrentRun.mTargetTime); } else { endPeaceOfMind(false); } @@ -243,8 +243,6 @@ private void endPeaceOfMind(boolean wasInterrupted) { setPeaceOfMindIconInNotificationBar(false, wasInterrupted); - if (wasInterrupted) { - AlarmManagerHelper.disableAlarm(mContext.getApplicationContext()); - } + AlarmManagerHelper.disableAlarm(mContext.getApplicationContext(), wasInterrupted); } } \ No newline at end of file diff --git a/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/ui/PeaceOfMindActivity.java b/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/ui/PeaceOfMindActivity.java index 5c125fa..92a3aef 100644 --- a/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/ui/PeaceOfMindActivity.java +++ b/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/ui/PeaceOfMindActivity.java @@ -84,7 +84,7 @@ public void run() { long currentTime = TimeHelper.getRoundedCurrentTimeMillis(); peaceOfMindTick(currentTime - currentStats.mCurrentRun.mStartTime, currentStats.mCurrentRun.mDuration); - if (currentStats.mCurrentRun.mTargetTime - currentTime > DateUtils.MINUTE_IN_MILLIS + Const.ALARM_INACCURACY) { + if (currentStats.mCurrentRun.mTargetTime - currentTime + Const.ALARM_INACCURACY > DateUtils.MINUTE_IN_MILLIS) { startTimer(); } } @@ -203,7 +203,7 @@ private void loadAvailableData() { final long currentTime = TimeHelper.getRoundedCurrentTimeMillis(); final float targetTimePercent = (float) currentStats.mCurrentRun.mDuration / (float) mMaxTime; - mVerticalSeekBar.setInvertedProgress((int) (targetTimePercent * mVerticalSeekBar.getHeight())); + mVerticalSeekBar.setInvertedProgress(targetTimePercent); updateTextForNewTime(currentTime - currentStats.mCurrentRun.mStartTime, currentStats.mCurrentRun.mDuration); updateTimeTextLabel(targetTimePercent * 100); @@ -458,7 +458,7 @@ public synchronized void peaceOfMindStarted(long duration) { updateTextForNewTime(0, duration); updateScreenBackgrounds(); - mVerticalSeekBar.setInvertedProgress((int) (targetTimePercent * mVerticalSeekBar.getHeight())); + mVerticalSeekBar.setInvertedProgress(targetTimePercent); startPeaceOfMindVideo(); startTimer(); @@ -520,7 +520,7 @@ public void run() { updateScreenBackgrounds(); mVerticalSeekBar.setThumb(mResources.getDrawable(R.drawable.seekbar_thumb_off)); mVerticalSeekBar.setThumbOffset(0); - mVerticalSeekBar.setInvertedProgress(0); + mVerticalSeekBar.setInvertedProgress(0f); updateTextForNewTime(0, 0); updateTimeTextLabel(0); @@ -589,7 +589,7 @@ private void displayHintOnFirstRun(SharedPreferences prefs) { @Override public boolean onError(MediaPlayer mp, int what, int extra) { - // Some devices cannot play MPEG-4, fallback using a lo-res H.264 + // Some devices cannot play MPEG-4, fallback using a lo-res H.264 Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.fp_start_pom_h264_video); mVideo.setVideoURI(uri); mVideo.start(); diff --git a/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/ui/VerticalSeekBar.java b/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/ui/VerticalSeekBar.java index 36bc75d..ee66ae0 100644 --- a/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/ui/VerticalSeekBar.java +++ b/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/ui/VerticalSeekBar.java @@ -23,87 +23,87 @@ public class VerticalSeekBar extends SeekBar { - private VerticalScrollListener _listener; - - public VerticalSeekBar(Context context) { - super(context); - } - - public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } - - public VerticalSeekBar(Context context, AttributeSet attrs) { - super(context, attrs); - } - - public void setPeaceListener(VerticalScrollListener listener) { - _listener = listener; - } - - public void onSizeChanged(int w, int h, int oldw, int oldh) { - super.onSizeChanged(h, w, oldh, oldw); - } - - @Override - protected synchronized void onMeasure(int widthMeasureSpec, - int heightMeasureSpec) { - super.onMeasure(heightMeasureSpec, widthMeasureSpec); - setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); - } - - protected void onDraw(Canvas c) { - c.rotate(90); - c.translate(0, -getWidth()); - - super.onDraw(c); - } - - @Override - public boolean onTouchEvent(MotionEvent event) { - if (!isEnabled()) { - return false; - } - - switch (event.getAction()) { - case MotionEvent.ACTION_DOWN: - updateScroll(event); - break; - case MotionEvent.ACTION_MOVE: - updateScroll(event); - break; - case MotionEvent.ACTION_UP: - updateScroll(event); - performScrollEnded(); - break; - - case MotionEvent.ACTION_CANCEL: - break; - } - return true; - } - - public void setInvertedProgress(int y){ - final int i = getMax() - (getMax() * y / getHeight()); - setProgress(100 - i); - onSizeChanged(getWidth(), getHeight(), 0, 0); - } - - private void updateScroll(MotionEvent event) { + private VerticalScrollListener _listener; + + public VerticalSeekBar(Context context) { + super(context); + } + + public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + public VerticalSeekBar(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public void setPeaceListener(VerticalScrollListener listener) { + _listener = listener; + } + + public void onSizeChanged(int w, int h, int oldw, int oldh) { + super.onSizeChanged(h, w, oldh, oldw); + } + + @Override + protected synchronized void onMeasure(int widthMeasureSpec, + int heightMeasureSpec) { + super.onMeasure(heightMeasureSpec, widthMeasureSpec); + setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); + } + + protected void onDraw(Canvas c) { + c.rotate(90); + c.translate(0, -getWidth()); + + super.onDraw(c); + } + + @Override + public boolean onTouchEvent(MotionEvent event) { + if (!isEnabled()) { + return false; + } + + switch (event.getAction()) { + case MotionEvent.ACTION_DOWN: + updateScroll(event); + break; + case MotionEvent.ACTION_MOVE: + updateScroll(event); + break; + case MotionEvent.ACTION_UP: + updateScroll(event); + performScrollEnded(); + break; + + case MotionEvent.ACTION_CANCEL: + break; + } + return true; + } + + public void setInvertedProgress(float y) { + final int progress = (int) Math.ceil(getMax() * y); + setProgress(progress); + onSizeChanged(getWidth(), getHeight(), 0, 0); + } + + private void updateScroll(MotionEvent event) { final int i = getMax() - (int) (getMax() * event.getY() / getHeight()); - setProgress(100 - i); - onSizeChanged(getWidth(), getHeight(), 0, 0); - - if (_listener != null) { - _listener.updateBarScroll(getProgress()); - } - } - - private void performScrollEnded() { - float progress = getProgress() / 100.0f; - - if (_listener != null) { - _listener.scrollEnded(progress); - } - } + setProgress(100 - i); + onSizeChanged(getWidth(), getHeight(), 0, 0); + + if (_listener != null) { + _listener.updateBarScroll(getProgress()); + } + } + + private void performScrollEnded() { + float progress = getProgress() / 100.0f; + + if (_listener != null) { + _listener.scrollEnded(progress); + } + } } \ No newline at end of file diff --git a/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/utils/AlarmManagerHelper.java b/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/utils/AlarmManagerHelper.java index 734e833..47f0f43 100644 --- a/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/utils/AlarmManagerHelper.java +++ b/FairphonePeaceOfMind/src/main/java/ca/mudar/fairphone/peaceofmind/utils/AlarmManagerHelper.java @@ -33,8 +33,10 @@ public static void enableAlarm(Context context, long target) { toggleWidgetRepeatingAlarm(context, target); } - public static void disableAlarm(Context context) { - toggleWakeupAlarm(context, -1l); + public static void disableAlarm(Context context, boolean wasInterrupted) { + if (wasInterrupted) { + toggleWakeupAlarm(context, -1l); + } toggleWidgetRepeatingAlarm(context, -1l); }