From 398a87565441d38da1e66610521ab4b6c277befc Mon Sep 17 00:00:00 2001 From: Phillip Hsu Date: Fri, 16 Sep 2016 19:34:06 -0700 Subject: [PATCH] Change content intent of stopwatch notification to launch MainActivity and scroll to stopwatch page --- .../com/philliphsu/clock2/MainActivity.java | 25 ++++++++++++++++--- .../StopwatchNotificationService.java | 6 ++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/philliphsu/clock2/MainActivity.java b/app/src/main/java/com/philliphsu/clock2/MainActivity.java index 00a2c7a..a151a9e 100644 --- a/app/src/main/java/com/philliphsu/clock2/MainActivity.java +++ b/app/src/main/java/com/philliphsu/clock2/MainActivity.java @@ -29,6 +29,11 @@ import butterknife.Bind; public class MainActivity extends BaseActivity { private static final String TAG = "MainActivity"; + public static final int PAGE_ALARMS = 0; + public static final int PAGE_TIMERS = 1; + public static final int PAGE_STOPWATCH = 2; + public static final String EXTRA_SHOW_PAGE = "com.philliphsu.clock2.extra.SHOW_PAGE"; + /** * The {@link android.support.v4.view.PagerAdapter} that will provide * fragments for each of the sections. We use a @@ -230,6 +235,20 @@ public class MainActivity extends BaseActivity { }); mAddItemDrawable = ContextCompat.getDrawable(this, R.drawable.ic_add_24dp); + + final int initialPage = getIntent().getIntExtra(EXTRA_SHOW_PAGE, -1); + if (initialPage > 0/*0 is already the default page*/ && initialPage <= PAGE_STOPWATCH) { + // Run this only after the ViewPager is finished drawing + mViewPager.post(new Runnable() { + @Override + public void run() { + // TOneverDO: smoothScroll == false, or else the onPageScrolled callback won't + // be called for the intermediate pages that are responsible for translating + // the FAB + mViewPager.setCurrentItem(initialPage, true/*smoothScroll*/); + } + }); + } } @Override @@ -387,11 +406,11 @@ public class MainActivity extends BaseActivity { public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. switch (position) { - case 0: + case PAGE_ALARMS: return AlarmsFragment.newInstance(1); - case 1: + case PAGE_TIMERS: return new TimersFragment(); - case 2: + case PAGE_STOPWATCH: return new StopwatchFragment(); default: return PlaceholderFragment.newInstance(position + 1); diff --git a/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchNotificationService.java b/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchNotificationService.java index 9d23cfa..6173088 100644 --- a/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchNotificationService.java +++ b/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchNotificationService.java @@ -86,7 +86,11 @@ public class StopwatchNotificationService extends ChronometerNotificationService @Override protected PendingIntent getContentIntent() { Intent intent = new Intent(this, MainActivity.class); - intent.putExtra(null/*TODO:MainActivity.EXTRA_SHOW_PAGE*/, 2/*TODO:MainActivity.INDEX_STOPWATCH*/); + // http://stackoverflow.com/a/3128418/5055032 + // "For some unspecified reason, extras will be delivered only if you've set some action" + // This ONLY applies to PendingIntents... + intent.setAction("foo"/*dummy action*/); + intent.putExtra(MainActivity.EXTRA_SHOW_PAGE, MainActivity.PAGE_STOPWATCH); return PendingIntent.getActivity(this, 0, intent, 0); }