Commit 5e469aa1 authored by rachelzhang's avatar rachelzhang
Browse files

Fix delete/reset timer button disappearing

Bug: 17910859
Change-Id: I4ccd92602bc909db6c265f3d8bee27bf2e524cb2
parent d0a9d268
......@@ -269,6 +269,12 @@ public class AlarmClockFragment extends DeskClockFragment implements
public void onResume() {
super.onResume();
final DeskClock activity = (DeskClock) getActivity();
if (activity.getSelectedTab() == DeskClock.ALARM_TAB_INDEX) {
setFabAppearance();
setLeftRightButtonAppearance();
}
if (mAdapter != null) {
mAdapter.notifyDataSetChanged();
}
......@@ -1442,18 +1448,24 @@ public class AlarmClockFragment extends DeskClockFragment implements
}
@Override
public void setFabAppearance(ImageButton fab) {
if (!isAdded()) {
public void setFabAppearance() {
final DeskClock activity = (DeskClock) getActivity();
if (mFab == null || activity.getSelectedTab() != DeskClock.ALARM_TAB_INDEX) {
return;
}
fab.setVisibility(View.VISIBLE);
fab.setImageResource(R.drawable.ic_fab_plus);
fab.setContentDescription(getString(R.string.button_alarms));
mFab.setVisibility(View.VISIBLE);
mFab.setImageResource(R.drawable.ic_fab_plus);
mFab.setContentDescription(getString(R.string.button_alarms));
}
@Override
public void setLeftRightButtonAppearance(ImageButton left, ImageButton right) {
left.setVisibility(View.INVISIBLE);
right.setVisibility(View.INVISIBLE);
public void setLeftRightButtonAppearance() {
final DeskClock activity = (DeskClock) getActivity();
if (mLeftButton == null || mRightButton == null ||
activity.getSelectedTab() != DeskClock.ALARM_TAB_INDEX) {
return;
}
mLeftButton.setVisibility(View.INVISIBLE);
mRightButton.setVisibility(View.INVISIBLE);
}
}
......@@ -209,11 +209,17 @@ public class ClockFragment extends DeskClockFragment implements OnSharedPreferen
@Override
public void onResume() {
super.onResume();
final DeskClock activity = (DeskClock) getActivity();
if (activity.getSelectedTab() == DeskClock.CLOCK_TAB_INDEX) {
setFabAppearance();
setLeftRightButtonAppearance();
}
mPrefs.registerOnSharedPreferenceChangeListener(this);
mDateFormat = getString(R.string.abbrev_wday_month_day_no_year);
mDateFormatForAccessibility = getString(R.string.full_wday_month_day_no_year);
Activity activity = getActivity();
Utils.setQuarterHourUpdater(mHandler, mQuarterHourUpdater);
// Besides monitoring when quarter-hour changes, monitor other actions that
// effect clock time
......@@ -277,18 +283,24 @@ public class ClockFragment extends DeskClockFragment implements OnSharedPreferen
}
@Override
public void setFabAppearance(ImageButton fab) {
if (!isAdded()) {
public void setFabAppearance() {
final DeskClock activity = (DeskClock) getActivity();
if (mFab == null || activity.getSelectedTab() != DeskClock.CLOCK_TAB_INDEX) {
return;
}
fab.setVisibility(View.VISIBLE);
fab.setImageResource(R.drawable.ic_globe);
fab.setContentDescription(getString(R.string.button_cities));
mFab.setVisibility(View.VISIBLE);
mFab.setImageResource(R.drawable.ic_globe);
mFab.setContentDescription(getString(R.string.button_cities));
}
@Override
public void setLeftRightButtonAppearance(ImageButton left, ImageButton right) {
left.setVisibility(View.INVISIBLE);
right.setVisibility(View.INVISIBLE);
public void setLeftRightButtonAppearance() {
final DeskClock activity = (DeskClock) getActivity();
if (mLeftButton == null || mRightButton == null ||
activity.getSelectedTab() != DeskClock.CLOCK_TAB_INDEX) {
return;
}
mLeftButton.setVisibility(View.INVISIBLE);
mRightButton.setVisibility(View.INVISIBLE);
}
}
......@@ -477,9 +477,8 @@ public class DeskClock extends Activity implements LabelDialogFragment.TimerLabe
TabInfo info = mTabs.get(getRtlPosition(position));
fragment = Fragment.instantiate(mContext, info.clss.getName(), info.args);
if (fragment instanceof TimerFragment) {
((TimerFragment) fragment).setFabAppearance(mFab);
((TimerFragment) fragment).setLeftRightButtonAppearance(mLeftButton,
mRightButton);
((TimerFragment) fragment).setFabAppearance();
((TimerFragment) fragment).setLeftRightButtonAppearance();
}
}
return fragment;
......@@ -543,10 +542,12 @@ public class DeskClock extends Activity implements LabelDialogFragment.TimerLabe
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
TabInfo info = (TabInfo) tab.getTag();
int position = info.getPosition();
final TabInfo info = (TabInfo) tab.getTag();
final int position = info.getPosition();
final int rtlSafePosition = getRtlPosition(position);
mSelectedTab = position;
if (mIsFirstLaunch && getRtlPosition(position) == CLOCK_TAB_INDEX) {
if (mIsFirstLaunch && isClockTab(rtlSafePosition)) {
mLeftButton.setVisibility(View.INVISIBLE);
mRightButton.setVisibility(View.INVISIBLE);
mFab.setVisibility(View.VISIBLE);
......@@ -554,11 +555,11 @@ public class DeskClock extends Activity implements LabelDialogFragment.TimerLabe
mFab.setContentDescription(getString(R.string.button_cities));
mIsFirstLaunch = false;
} else {
DeskClockFragment f = (DeskClockFragment) getItem(getRtlPosition(position));
f.setFabAppearance(mFab);
f.setLeftRightButtonAppearance(mLeftButton, mRightButton);
DeskClockFragment f = (DeskClockFragment) getItem(rtlSafePosition);
f.setFabAppearance();
f.setLeftRightButtonAppearance();
}
mPager.setCurrentItem(getRtlPosition(position));
mPager.setCurrentItem(rtlSafePosition);
}
@Override
......@@ -566,6 +567,11 @@ public class DeskClock extends Activity implements LabelDialogFragment.TimerLabe
// Do nothing
}
private boolean isClockTab(int rtlSafePosition) {
final int clockTabIndex = isRtl() ? RTL_CLOCK_TAB_INDEX : CLOCK_TAB_INDEX;
return rtlSafePosition == clockTabIndex;
}
public void notifySelectedPage(int page) {
notifyPageChanged(page);
}
......@@ -712,4 +718,16 @@ public class DeskClock extends Activity implements LabelDialogFragment.TimerLabe
}
return position;
}
public ImageButton getFab() {
return mFab;
}
public ImageButton getLeftButton() {
return mLeftButton;
}
public ImageButton getRightButton() {
return mRightButton;
}
}
......@@ -16,6 +16,7 @@
package com.android.deskclock;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.widget.PopupMenuCompat;
......@@ -26,6 +27,10 @@ import android.widget.PopupMenu;
public class DeskClockFragment extends Fragment {
protected ImageButton mFab;
protected ImageButton mLeftButton;
protected ImageButton mRightButton;
public void onPageChanged(int page) {
// Do nothing here , only in derived classes
}
......@@ -34,11 +39,23 @@ public class DeskClockFragment extends Fragment {
// Do nothing here , only in derived classes
}
public void setFabAppearance(ImageButton fab) {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final Activity activity = getActivity();
if (activity instanceof DeskClock) {
final DeskClock deskClockActivity = (DeskClock) activity;
mFab = deskClockActivity.getFab();
mLeftButton = deskClockActivity.getLeftButton();
mRightButton = deskClockActivity.getRightButton();
}
}
public void setFabAppearance() {
// Do nothing here , only in derived classes
}
public void setLeftRightButtonAppearance(ImageButton left, ImageButton right) {
public void setLeftRightButtonAppearance() {
// Do nothing here , only in derived classes
}
......
......@@ -43,9 +43,6 @@ public class StopwatchFragment extends DeskClockFragment
int mState = Stopwatches.STOPWATCH_RESET;
// Stopwatch views that are accessed by the activity
private ImageButton mFab;
private ImageButton mLeftButton;
private ImageButton mRightButton;
private CircleTimerView mTime;
private CountingTimerView mTimeText;
private ListView mLapsList;
......@@ -399,7 +396,8 @@ public class StopwatchFragment extends DeskClockFragment
mTime.readFromSharedPref(prefs, "sw");
mTime.postInvalidate();
setButtons(mState);
setFabAppearance();
setLeftRightButtonAppearance();
mTimeText.setTime(mAccumulatedTime, true, true);
if (mState == Stopwatches.STOPWATCH_RUNNING) {
acquireWakeLock();
......@@ -458,8 +456,9 @@ public class StopwatchFragment extends DeskClockFragment
mTimeText.setTime(mAccumulatedTime, true, true);
mTimeText.blinkTimeStr(true);
updateCurrentLap(mAccumulatedTime);
setButtons(Stopwatches.STOPWATCH_STOPPED);
mState = Stopwatches.STOPWATCH_STOPPED;
setFabAppearance();
setLeftRightButtonAppearance();
}
private void doStart(long time) {
......@@ -470,14 +469,16 @@ public class StopwatchFragment extends DeskClockFragment
if (mTime.isAnimating()) {
mTime.startIntervalAnimation();
}
setButtons(Stopwatches.STOPWATCH_RUNNING);
mState = Stopwatches.STOPWATCH_RUNNING;
setFabAppearance();
setLeftRightButtonAppearance();
}
private void doLap() {
if (DEBUG) LogUtils.v("StopwatchFragment.doLap");
showLaps();
setButtons(Stopwatches.STOPWATCH_RUNNING);
setFabAppearance();
setLeftRightButtonAppearance();
}
private void doReset() {
......@@ -493,15 +494,9 @@ public class StopwatchFragment extends DeskClockFragment
mTime.reset();
mTimeText.setTime(mAccumulatedTime, true, true);
mTimeText.blinkTimeStr(false);
setButtons(Stopwatches.STOPWATCH_RESET);
mState = Stopwatches.STOPWATCH_RESET;
}
private void showShareButton(boolean show) {
if (mRightButton != null) {
mRightButton.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
mRightButton.setEnabled(show);
}
setFabAppearance();
setLeftRightButtonAppearance();
}
private void shareResults() {
......@@ -542,80 +537,10 @@ public class StopwatchFragment extends DeskClockFragment
return output;
}
/***
* Update the buttons on the stopwatch according to the watch's state
*/
private void setButtons(int state) {
final Activity activity = getActivity();
if (!(activity instanceof DeskClock)) {
return;
}
final DeskClock deskClockActivity = (DeskClock) activity;
if (mFab == null || deskClockActivity.getSelectedTab() != DeskClock
.STOPWATCH_TAB_INDEX) {
return;
}
switch (state) {
case Stopwatches.STOPWATCH_RESET:
setButton(mLeftButton, R.string.sw_lap_button, R.drawable.ic_lap, false,
View.INVISIBLE);
changeFab(R.drawable.ic_fab_play);
showShareButton(false);
break;
case Stopwatches.STOPWATCH_RUNNING:
setButton(mLeftButton, R.string.sw_lap_button, R.drawable.ic_lap,
!reachedMaxLaps(), View.VISIBLE);
changeFab(R.drawable.ic_fab_pause);
showShareButton(false);
break;
case Stopwatches.STOPWATCH_STOPPED:
setButton(mLeftButton, R.string.sw_reset_button, R.drawable.ic_reset, true,
View.VISIBLE);
changeFab(R.drawable.ic_fab_play);
showShareButton(true);
break;
default:
break;
}
}
private void changeFab(int id) {
if (getActivity() instanceof DeskClock) {
if (mFab != null &&
((DeskClock) getActivity()).getSelectedTab() == DeskClock.STOPWATCH_TAB_INDEX) {
if (id == R.drawable.ic_fab_play) {
mFab.setContentDescription(getString(R.string.sw_start_button));
} else if (id == R.drawable.ic_fab_pause){
mFab.setContentDescription(getString(R.string.sw_stop_button));
}
mFab.setImageResource(id);
mFab.setVisibility(View.VISIBLE);
}
}
}
private boolean reachedMaxLaps() {
return mLapsAdapter.getCount() >= Stopwatches.MAX_LAPS;
}
/***
* Set a single button with the string and states provided.
* @param b - Button view to update
* @param text - Text in button
* @param enabled - enable/disables the button
* @param visibility - Show/hide the button
*/
private void setButton(
ImageButton b, int text, int drawableId, boolean enabled, int visibility) {
if (b == null) {
return;
}
b.setContentDescription(getActivity().getResources().getString(text));
b.setImageResource(drawableId);
b.setVisibility(visibility);
b.setEnabled(enabled);
}
/***
* Handle action when user presses the lap button
* @param time - in hundredth of a second
......@@ -873,12 +798,11 @@ public class StopwatchFragment extends DeskClockFragment
}
@Override
public void setFabAppearance(ImageButton fab) {
if (!isAdded()) {
public void setFabAppearance() {
final DeskClock activity = (DeskClock) getActivity();
if (mFab == null || activity.getSelectedTab() != DeskClock.STOPWATCH_TAB_INDEX) {
return;
}
mFab = fab;
if (mState == Stopwatches.STOPWATCH_RUNNING) {
mFab.setImageResource(R.drawable.ic_fab_pause);
mFab.setContentDescription(getString(R.string.sw_stop_button));
......@@ -890,27 +814,37 @@ public class StopwatchFragment extends DeskClockFragment
}
@Override
public void setLeftRightButtonAppearance(ImageButton left, ImageButton right) {
if (!isAdded()) {
public void setLeftRightButtonAppearance() {
final DeskClock activity = (DeskClock) getActivity();
if (mLeftButton == null || mRightButton == null ||
activity.getSelectedTab() != DeskClock.STOPWATCH_TAB_INDEX) {
return;
}
mLeftButton = left;
mRightButton = right;
mLeftButton.setVisibility(mState == Stopwatches.STOPWATCH_RESET ? View.INVISIBLE :
View.VISIBLE);
mRightButton.setVisibility(mState == Stopwatches.STOPWATCH_RESET ||
mState == Stopwatches.STOPWATCH_RUNNING ? View.INVISIBLE : View.VISIBLE);
if (mState == Stopwatches.STOPWATCH_RUNNING) {
mLeftButton.setImageResource(R.drawable.ic_lap);
mLeftButton.setContentDescription(getString(R.string.sw_lap_button));
} else {
mLeftButton.setImageResource(R.drawable.ic_reset);
mLeftButton.setContentDescription(getString(R.string.sw_reset_button));
}
mRightButton.setImageResource(R.drawable.ic_share);
mRightButton.setContentDescription(getString(R.string.sw_share_button));
switch (mState) {
case Stopwatches.STOPWATCH_RESET:
mLeftButton.setImageResource(R.drawable.ic_lap);
mLeftButton.setContentDescription(getString(R.string.sw_lap_button));
mLeftButton.setEnabled(false);
mLeftButton.setVisibility(View.INVISIBLE);
mRightButton.setVisibility(View.INVISIBLE);
break;
case Stopwatches.STOPWATCH_RUNNING:
mLeftButton.setImageResource(R.drawable.ic_lap);
mLeftButton.setContentDescription(getString(R.string.sw_lap_button));
mLeftButton.setEnabled(!reachedMaxLaps());
mLeftButton.setVisibility(View.VISIBLE);
mRightButton.setVisibility(View.INVISIBLE);
break;
case Stopwatches.STOPWATCH_STOPPED:
mLeftButton.setImageResource(R.drawable.ic_reset);
mLeftButton.setContentDescription(getString(R.string.sw_reset_button));
mLeftButton.setEnabled(true);
mLeftButton.setVisibility(View.VISIBLE);
mRightButton.setVisibility(View.VISIBLE);
break;
}
}
}
......@@ -67,9 +67,6 @@ public class TimerFragment extends DeskClockFragment implements OnSharedPreferen
private TimerSetupView mSetupView;
private VerticalViewPager mViewPager;
private TimerFragmentAdapter mAdapter;
private ImageButton mFab;
private ImageButton mLeftButton;
private ImageButton mRightButton;
private ImageButton mCancel;
private ViewGroup mContentView;
private View mTimerView;
......@@ -303,8 +300,8 @@ public class TimerFragment extends DeskClockFragment implements OnSharedPreferen
mTimerView.setVisibility(View.VISIBLE);
mSetupView.setVisibility(View.GONE);
mLastView = mTimerView;
setLeftRightButtonAppearance(mLeftButton, mRightButton);
setFabAppearance(mFab);
setLeftRightButtonAppearance();
setFabAppearance();
startClockTicks();
}
......@@ -319,8 +316,8 @@ public class TimerFragment extends DeskClockFragment implements OnSharedPreferen
mSetupView.updateDeleteButtonAndDivider();
mSetupView.registerStartButton(mFab);
mLastView = mSetupView;
setLeftRightButtonAppearance(mLeftButton, mRightButton);
setFabAppearance(mFab);
setLeftRightButtonAppearance();
setFabAppearance();
stopClockTicks();
}
......@@ -512,54 +509,42 @@ public class TimerFragment extends DeskClockFragment implements OnSharedPreferen
}
@Override
public void setFabAppearance(ImageButton fab) {
if (!isAdded()) {
public void setFabAppearance() {
final DeskClock activity = (DeskClock) getActivity();
if (mFab == null) {
return;
}
mFab = fab;
if (mFab != null) {
if (atTimerTab()) {
if (mLastView == mTimerView) {
setTimerViewFabIcon(getCurrentTimer());
} else if (mSetupView != null) {
mSetupView.registerStartButton(mFab);
mFab.setImageResource(R.drawable.ic_fab_play);
mFab.setContentDescription(getString(R.string.timer_start));
}
} else {
mFab.setVisibility(View.VISIBLE);
}
if (activity.getSelectedTab() != DeskClock.TIMER_TAB_INDEX) {
mFab.setVisibility(View.VISIBLE);
return;
}
}
private boolean atTimerTab() {
if (getActivity() instanceof DeskClock) {
final DeskClock deskClockActivity = (DeskClock) getActivity();
return deskClockActivity.getSelectedTab() == DeskClock.TIMER_TAB_INDEX;
} else {
return false;
if (mLastView == mTimerView) {
setTimerViewFabIcon(getCurrentTimer());
} else if (mSetupView != null) {
mSetupView.registerStartButton(mFab);
mFab.setImageResource(R.drawable.ic_fab_play);
mFab.setContentDescription(getString(R.string.timer_start));
}
}
@Override
public void setLeftRightButtonAppearance(ImageButton left, ImageButton right) {
if (!isAdded()) {
public void setLeftRightButtonAppearance() {
final DeskClock activity = (DeskClock) getActivity();
if (mLeftButton == null || mRightButton == null ||
activity.getSelectedTab() != DeskClock.TIMER_TAB_INDEX) {
return;
}
mLeftButton = left;
mRightButton = right;
if (mLeftButton != null && mRightButton != null && atTimerTab()) {
mLeftButton.setEnabled(true);
mRightButton.setEnabled(true);
mLeftButton.setVisibility(mLastView != mTimerView ? View.GONE : View.VISIBLE);
mRightButton.setVisibility(mLastView != mTimerView ? View.GONE : View.VISIBLE);
mLeftButton.setImageResource(R.drawable.ic_delete);
mLeftButton.setContentDescription(getString(R.string.timer_delete));
mRightButton.setImageResource(R.drawable.ic_add_timer);
mRightButton.setContentDescription(getString(R.string.timer_add_timer));
}
mLeftButton.setEnabled(true);
mRightButton.setEnabled(true);
mLeftButton.setVisibility(mLastView != mTimerView ? View.GONE : View.VISIBLE);
mRightButton.setVisibility(mLastView != mTimerView ? View.GONE : View.VISIBLE);
mLeftButton.setImageResource(R.drawable.ic_delete);
mLeftButton.setContentDescription(getString(R.string.timer_delete));
mRightButton.setImageResource(R.drawable.ic_add_timer);
mRightButton.setContentDescription(getString(R.string.timer_add_timer));
}
@Override
......@@ -610,7 +595,7 @@ public class TimerFragment extends DeskClockFragment implements OnSharedPreferen
// When deleting a negative timer (hidden fab), since deleting will not trigger
// onResume(), in order to ensure the fab showing correctly, we need to manually
// set fab appearance here.
setFabAppearance(mFab);
setFabAppearance();
}
private void highlightPageIndicator(int position) {
......@@ -691,7 +676,7 @@ public class TimerFragment extends DeskClockFragment implements OnSharedPreferen
} else {
highlightPageIndicator(0);
}
setFabAppearance(mFab);
setFabAppearance();
return;
}
}
......@@ -738,7 +723,7 @@ public class TimerFragment extends DeskClockFragment implements OnSharedPreferen
break;
}
// This will change status of the timer, so update fab
setFabAppearance(mFab);
setFabAppearance();
}
private void cancelTimerNotification(int timerId) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment