From ab468c69c860c5101c7d992a85eaa77e760d5c17 Mon Sep 17 00:00:00 2001 From: Jonathan Caryl Date: Sat, 16 Sep 2017 08:47:03 +0100 Subject: [PATCH 1/2] Extract night mode code so it works on CheeseDetailActivity too --- app/build.gradle | 12 ++-- .../designlibdemo/CheeseDetailActivity.java | 17 ++++- .../android/designlibdemo/MainActivity.java | 58 ++++------------ .../android/designlibdemo/NightModeUtils.java | 69 +++++++++++++++++++ build.gradle | 10 ++- gradle/wrapper/gradle-wrapper.properties | 4 +- 6 files changed, 115 insertions(+), 55 deletions(-) create mode 100644 app/src/main/java/com/support/android/designlibdemo/NightModeUtils.java diff --git a/app/build.gradle b/app/build.gradle index 117b50a..d3d54ee 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,10 +20,14 @@ android { } } +repositories { + jcenter() +} + dependencies { - implementation 'com.android.support:design:26.0.0-beta2' - implementation 'com.android.support:cardview-v7:26.0.0-beta2' + compile "com.android.support:design:$supportLibVersion" + compile "com.android.support:cardview-v7:$supportLibVersion" - implementation 'com.github.bumptech.glide:glide:3.7.0' - implementation 'de.hdodenhof:circleimageview:1.3.0' + compile 'com.github.bumptech.glide:glide:3.6.0' + compile 'de.hdodenhof:circleimageview:1.3.0' } 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(); + } + } + +} diff --git a/build.gradle b/build.gradle index 6dbef84..9369b43 100644 --- a/build.gradle +++ b/build.gradle @@ -2,17 +2,21 @@ buildscript { repositories { - maven { url 'https://maven.google.com' } + google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.0.0-alpha6' + classpath 'com.android.tools.build:gradle:3.0.0-beta6' } } +ext { + supportLibVersion = '26.0.2' +} + allprojects { repositories { - maven { url 'https://maven.google.com' } + google() jcenter() } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 081868d..46abfad 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Fri Jun 09 10:31:39 BST 2017 +#Sat Sep 16 08:45:03 BST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-milestone-1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip From 6bf9617c2a44ccef638a635c640acb7f86a9382c Mon Sep 17 00:00:00 2001 From: Jonathan Caryl Date: Sat, 16 Sep 2017 08:52:14 +0100 Subject: [PATCH 2/2] Remove casts we no longer need --- .../android/designlibdemo/CheeseDetailActivity.java | 7 +++---- .../android/designlibdemo/CheeseListFragment.java | 4 ++-- .../support/android/designlibdemo/MainActivity.java | 12 ++++++------ .../android/designlibdemo/NightModeUtils.java | 8 ++------ 4 files changed, 13 insertions(+), 18 deletions(-) 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 4169f8c..fa9eb99 100644 --- a/app/src/main/java/com/support/android/designlibdemo/CheeseDetailActivity.java +++ b/app/src/main/java/com/support/android/designlibdemo/CheeseDetailActivity.java @@ -40,12 +40,11 @@ public void onCreate(Bundle savedInstanceState) { Intent intent = getIntent(); final String cheeseName = intent.getStringExtra(EXTRA_NAME); - final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + final Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); - CollapsingToolbarLayout collapsingToolbar = - (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar); + CollapsingToolbarLayout collapsingToolbar = findViewById(R.id.collapsing_toolbar); collapsingToolbar.setTitle(cheeseName); loadBackdrop(); @@ -54,7 +53,7 @@ public void onCreate(Bundle savedInstanceState) { } private void loadBackdrop() { - final ImageView imageView = (ImageView) findViewById(R.id.backdrop); + final ImageView imageView = findViewById(R.id.backdrop); Glide.with(this).load(Cheeses.getRandomCheeseDrawable()).centerCrop().into(imageView); } diff --git a/app/src/main/java/com/support/android/designlibdemo/CheeseListFragment.java b/app/src/main/java/com/support/android/designlibdemo/CheeseListFragment.java index fdf9452..ca4a518 100644 --- a/app/src/main/java/com/support/android/designlibdemo/CheeseListFragment.java +++ b/app/src/main/java/com/support/android/designlibdemo/CheeseListFragment.java @@ -80,8 +80,8 @@ public static class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View view) { super(view); mView = view; - mImageView = (ImageView) view.findViewById(R.id.avatar); - mTextView = (TextView) view.findViewById(android.R.id.text1); + mImageView = view.findViewById(R.id.avatar); + mTextView = view.findViewById(android.R.id.text1); } @Override 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 79cb9f1..3c04598 100644 --- a/app/src/main/java/com/support/android/designlibdemo/MainActivity.java +++ b/app/src/main/java/com/support/android/designlibdemo/MainActivity.java @@ -50,26 +50,26 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); final ActionBar ab = getSupportActionBar(); ab.setHomeAsUpIndicator(R.drawable.ic_menu); ab.setDisplayHomeAsUpEnabled(true); - mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); + mDrawerLayout = findViewById(R.id.drawer_layout); - NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); + NavigationView navigationView = findViewById(R.id.nav_view); if (navigationView != null) { setupDrawerContent(navigationView); } - ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); + ViewPager viewPager = findViewById(R.id.viewpager); if (viewPager != null) { setupViewPager(viewPager); } - FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); + FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -78,7 +78,7 @@ public void onClick(View view) { } }); - TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); + TabLayout tabLayout = findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); nightModeUtils = NightModeUtils.from(this); diff --git a/app/src/main/java/com/support/android/designlibdemo/NightModeUtils.java b/app/src/main/java/com/support/android/designlibdemo/NightModeUtils.java index 7347708..99aece2 100644 --- a/app/src/main/java/com/support/android/designlibdemo/NightModeUtils.java +++ b/app/src/main/java/com/support/android/designlibdemo/NightModeUtils.java @@ -1,7 +1,6 @@ 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; @@ -38,7 +37,7 @@ boolean checkNightMode(@NonNull Menu menu) { return true; } - public boolean onNightModeItemSelected(@NonNull MenuItem item) { + boolean onNightModeItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.menu_night_mode_system: setNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); @@ -61,9 +60,6 @@ public boolean onNightModeItemSelected(@NonNull MenuItem item) { private void setNightMode(@AppCompatDelegate.NightMode int nightMode) { AppCompatDelegate.setDefaultNightMode(nightMode); - if (Build.VERSION.SDK_INT >= 11) { - activity.recreate(); - } + activity.recreate(); } - }