diff options
Diffstat (limited to 'src/com/android/deskclock/DeskClock.java')
-rw-r--r-- | src/com/android/deskclock/DeskClock.java | 55 |
1 files changed, 49 insertions, 6 deletions
diff --git a/src/com/android/deskclock/DeskClock.java b/src/com/android/deskclock/DeskClock.java index b084dccb0..18b70b07c 100644 --- a/src/com/android/deskclock/DeskClock.java +++ b/src/com/android/deskclock/DeskClock.java @@ -20,6 +20,7 @@ import android.app.ActionBar; import android.app.ActionBar.Tab; import android.app.Activity; import android.app.Fragment; +import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.ActivityNotFoundException; import android.content.Context; @@ -28,8 +29,6 @@ import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.graphics.drawable.TransitionDrawable; import android.os.Bundle; -import android.os.Handler; -import android.os.Message; import android.preference.PreferenceManager; import android.support.v13.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; @@ -56,6 +55,7 @@ import com.android.deskclock.timer.Timers; import com.android.deskclock.worldclock.CitiesActivity; import java.util.ArrayList; +import java.util.HashSet; import java.util.TimeZone; /** @@ -155,6 +155,7 @@ public class DeskClock extends Activity implements LabelDialogFragment.TimerLabe mStopwatchTab.setContentDescription(R.string.menu_stopwatch); mTabsAdapter.addTab(mStopwatchTab, StopwatchFragment.class,STOPWATCH_TAB_INDEX); mActionBar.setSelectedNavigationItem(selectedIndex); + mTabsAdapter.notifySelectedPage(selectedIndex); } } @@ -303,10 +304,19 @@ public class DeskClock extends Activity implements LabelDialogFragment.TimerLabe Log.v(LOG_TAG, "Setting home time zone to " + homeTimeZone); } - public boolean isClockTab() { - return mViewPager.getCurrentItem() == CLOCK_TAB_INDEX; + public void registerPageChangedListener(DeskClockFragment frag) { + if (mTabsAdapter != null) { + mTabsAdapter.registerPageChangedListener(frag); + } + } + + public void unregisterPageChangedListener(DeskClockFragment frag) { + if (mTabsAdapter != null) { + mTabsAdapter.unregisterPageChangedListener(frag); + } } + /*** * Adapter for wrapping together the ActionBar's tab with the ViewPager */ @@ -335,6 +345,8 @@ public class DeskClock extends Activity implements LabelDialogFragment.TimerLabe ActionBar mMainActionBar; Context mContext; ViewPager mPager; + // Used for doing callbacks to fragments. + HashSet<String> mFragmentTags = new HashSet<String>(); public TabsAdapter(Activity activity, ViewPager pager) { super(activity.getFragmentManager()); @@ -375,6 +387,7 @@ public class DeskClock extends Activity implements LabelDialogFragment.TimerLabe @Override public void onPageSelected(int position) { mMainActionBar.setSelectedNavigationItem(position); + notifyPageChanged(position); } @Override @@ -399,14 +412,44 @@ public class DeskClock extends Activity implements LabelDialogFragment.TimerLabe // Do nothing } + + public void notifySelectedPage(int page) { + notifyPageChanged(page); + } + + private void notifyPageChanged(int newPage) { + for (String tag : mFragmentTags) { + final FragmentManager fm = getFragmentManager(); + DeskClockFragment f = (DeskClockFragment) fm.findFragmentByTag(tag); + if (f != null) { + f.onPageChanged(newPage); + } + } + } + + public void registerPageChangedListener(DeskClockFragment frag) { + String tag = frag.getTag(); + if (mFragmentTags.contains(tag)) { + Log.wtf(LOG_TAG, "Trying to add an existing fragment " + tag); + } else { + mFragmentTags.add(frag.getTag()); + } + // Since registering a listener by the fragment is done sometimes after the page + // was already changed, make sure the fragment gets the current page + frag.onPageChanged(mMainActionBar.getSelectedNavigationIndex()); + } + + public void unregisterPageChangedListener(DeskClockFragment frag) { + mFragmentTags.remove(frag.getTag()); + } } public static abstract class OnTapListener implements OnTouchListener { private float mLastTouchX; private float mLastTouchY; private long mLastTouchTime; - private TextView mMakePressedTextView; - private int mPressedColor, mGrayColor; + private final TextView mMakePressedTextView; + private final int mPressedColor, mGrayColor; private final float MAX_MOVEMENT_ALLOWED = 20; private final long MAX_TIME_ALLOWED = 500; |