From 22269b35dabfa7da7bfb06e179766385cf323869 Mon Sep 17 00:00:00 2001 From: Jonathan Caryl Date: Fri, 24 Mar 2017 11:34:26 +0000 Subject: [PATCH] Extract night mode code so it works on CheeseDetailActivity too --- .../designlibdemo/CheeseDetailActivity.java | 17 ++++- .../android/designlibdemo/MainActivity.java | 58 ++++------------ .../android/designlibdemo/NightModeUtils.java | 69 +++++++++++++++++++ 3 files changed, 98 insertions(+), 46 deletions(-) create mode 100644 app/src/main/java/com/support/android/designlibdemo/NightModeUtils.java diff --git a/app/src/main/java/com/support/android/designlibdemo/CheeseDetailActivity.java b/app/src/main/java/com/support/android/designlibdemo/CheeseDetailActivity.java index cf1b30f..4169f8c 100644 --- a/app/src/main/java/com/support/android/designlibdemo/CheeseDetailActivity.java +++ b/app/src/main/java/com/support/android/designlibdemo/CheeseDetailActivity.java @@ -27,11 +27,10 @@ import com.bumptech.glide.Glide; -import java.util.Random; - public class CheeseDetailActivity extends AppCompatActivity { public static final String EXTRA_NAME = "cheese_name"; + private NightModeUtils nightModeUtils; @Override public void onCreate(Bundle savedInstanceState) { @@ -50,6 +49,8 @@ public void onCreate(Bundle savedInstanceState) { collapsingToolbar.setTitle(cheeseName); loadBackdrop(); + + nightModeUtils = NightModeUtils.from(this); } private void loadBackdrop() { @@ -62,4 +63,16 @@ public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.sample_actions, menu); return true; } + + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + nightModeUtils.checkNightMode(menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + return nightModeUtils.onNightModeItemSelected(item) || super.onOptionsItemSelected(item); + } + } diff --git a/app/src/main/java/com/support/android/designlibdemo/MainActivity.java b/app/src/main/java/com/support/android/designlibdemo/MainActivity.java index 3e86b60..79cb9f1 100644 --- a/app/src/main/java/com/support/android/designlibdemo/MainActivity.java +++ b/app/src/main/java/com/support/android/designlibdemo/MainActivity.java @@ -16,7 +16,6 @@ package com.support.android.designlibdemo; -import android.os.Build; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; @@ -30,14 +29,10 @@ import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; -import android.support.v7.app.AppCompatDelegate; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; -import android.widget.Toast; import java.util.ArrayList; import java.util.List; @@ -48,6 +43,7 @@ public class MainActivity extends AppCompatActivity { private DrawerLayout mDrawerLayout; + private NightModeUtils nightModeUtils; @Override protected void onCreate(Bundle savedInstanceState) { @@ -84,6 +80,8 @@ public void onClick(View view) { TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); + + nightModeUtils = NightModeUtils.from(this); } @Override @@ -94,50 +92,22 @@ public boolean onCreateOptionsMenu(Menu menu) { @Override public boolean onPrepareOptionsMenu(Menu menu) { - switch (AppCompatDelegate.getDefaultNightMode()) { - case AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM: - menu.findItem(R.id.menu_night_mode_system).setChecked(true); - break; - case AppCompatDelegate.MODE_NIGHT_AUTO: - menu.findItem(R.id.menu_night_mode_auto).setChecked(true); - break; - case AppCompatDelegate.MODE_NIGHT_YES: - menu.findItem(R.id.menu_night_mode_night).setChecked(true); - break; - case AppCompatDelegate.MODE_NIGHT_NO: - menu.findItem(R.id.menu_night_mode_day).setChecked(true); - break; - } + nightModeUtils.checkNightMode(menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - mDrawerLayout.openDrawer(GravityCompat.START); - return true; - case R.id.menu_night_mode_system: - setNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); - break; - case R.id.menu_night_mode_day: - setNightMode(AppCompatDelegate.MODE_NIGHT_NO); - break; - case R.id.menu_night_mode_night: - setNightMode(AppCompatDelegate.MODE_NIGHT_YES); - break; - case R.id.menu_night_mode_auto: - setNightMode(AppCompatDelegate.MODE_NIGHT_AUTO); - break; - } - return super.onOptionsItemSelected(item); - } - - private void setNightMode(@AppCompatDelegate.NightMode int nightMode) { - AppCompatDelegate.setDefaultNightMode(nightMode); - - if (Build.VERSION.SDK_INT >= 11) { - recreate(); + if (nightModeUtils.onNightModeItemSelected(item)) { + return true; + } else { + switch (item.getItemId()) { + case android.R.id.home: + mDrawerLayout.openDrawer(GravityCompat.START); + return true; + default: + return super.onOptionsItemSelected(item); + } } } diff --git a/app/src/main/java/com/support/android/designlibdemo/NightModeUtils.java b/app/src/main/java/com/support/android/designlibdemo/NightModeUtils.java new file mode 100644 index 0000000..7347708 --- /dev/null +++ b/app/src/main/java/com/support/android/designlibdemo/NightModeUtils.java @@ -0,0 +1,69 @@ +package com.support.android.designlibdemo; + +import android.app.Activity; +import android.os.Build; +import android.support.annotation.NonNull; +import android.support.v7.app.AppCompatDelegate; +import android.view.Menu; +import android.view.MenuItem; + +class NightModeUtils { + private Activity activity; + + private NightModeUtils(@NonNull Activity activity) { + this.activity = activity; + } + + static NightModeUtils from(@NonNull Activity activity) { + return new NightModeUtils(activity); + } + + boolean checkNightMode(@NonNull Menu menu) { + switch (AppCompatDelegate.getDefaultNightMode()) { + case AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM: + menu.findItem(R.id.menu_night_mode_system).setChecked(true); + break; + case AppCompatDelegate.MODE_NIGHT_AUTO: + menu.findItem(R.id.menu_night_mode_auto).setChecked(true); + break; + case AppCompatDelegate.MODE_NIGHT_YES: + menu.findItem(R.id.menu_night_mode_night).setChecked(true); + break; + case AppCompatDelegate.MODE_NIGHT_NO: + menu.findItem(R.id.menu_night_mode_day).setChecked(true); + break; + default: + return false; + } + return true; + } + + public boolean onNightModeItemSelected(@NonNull MenuItem item) { + switch (item.getItemId()) { + case R.id.menu_night_mode_system: + setNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); + break; + case R.id.menu_night_mode_day: + setNightMode(AppCompatDelegate.MODE_NIGHT_NO); + break; + case R.id.menu_night_mode_night: + setNightMode(AppCompatDelegate.MODE_NIGHT_YES); + break; + case R.id.menu_night_mode_auto: + setNightMode(AppCompatDelegate.MODE_NIGHT_AUTO); + break; + default: + return false; + } + return true; + } + + private void setNightMode(@AppCompatDelegate.NightMode int nightMode) { + AppCompatDelegate.setDefaultNightMode(nightMode); + + if (Build.VERSION.SDK_INT >= 11) { + activity.recreate(); + } + } + +}