Merge pull request #4 from AnotherDaniel/oledswitch

Oledswitch
This commit is contained in:
Daniel 2018-10-04 21:55:32 +02:00 committed by GitHub
commit a233d72b0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 38 additions and 21 deletions

View file

@ -78,7 +78,7 @@ public class NewFeedActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
((NewsReaderApplication) getApplication()).getAppComponent().injectActivity(this);
ThemeChooser.ChooseTheme(this);
ThemeChooser.chooseTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_feed);

View file

@ -97,7 +97,7 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
ThemeChooser.ChooseTheme(this);
ThemeChooser.chooseTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_detail);

View file

@ -148,7 +148,7 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements
@Override
protected void onCreate(Bundle savedInstanceState) {
ThemeChooser.ChooseTheme(this);
ThemeChooser.chooseTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newsreader);
@ -897,8 +897,7 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements
String newLayout = PreferenceManager.getDefaultSharedPreferences(NewsReaderListActivity.this).getString(SettingsActivity.SP_FEED_LIST_LAYOUT,"0");
if(ThemeChooser.getInstance(NewsReaderListActivity.this).themeRequiresRestartOfUI(NewsReaderListActivity.this) || !newLayout.equals(oldLayout)) {
finish();
startActivity(getIntent());
NewsReaderListActivity.this.recreate();
} else if(data.hasExtra(SettingsActivity.CACHE_CLEARED) && ownCloudSyncService != null) {
resetUiAndStartSync();
}

View file

@ -154,13 +154,8 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_newsreader_list, container, false);
if(!ThemeChooser.getInstance(getActivity()).isDarkTheme(getContext())) {
view.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.slider_listview_background_color));
}
ButterKnife.bind(this, view);
if(!Constants.IsNextCloud(getContext())) {
// Set ownCloud view
headerView.setBackgroundResource(R.drawable.left_drawer_header_background);

View file

@ -133,7 +133,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
if(isXLargeTablet(this)) {
setTheme(R.style.AppThemeSettings);
} else {
ThemeChooser.ChooseTheme(this);
ThemeChooser.chooseTheme(this);
}
super.onCreate(savedInstanceState);

View file

@ -27,12 +27,14 @@ import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatDelegate;
import android.util.Log;
import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.SettingsActivity;
public class ThemeChooser {
private static final String TAG = ThemeChooser.class.getCanonicalName();
private Integer mSelectedTheme;
private Boolean mOledMode;
private static ThemeChooser mInstance;
@ -44,25 +46,30 @@ public class ThemeChooser {
return mInstance;
}
public ThemeChooser(Context context) {
getSelectedTheme(context, false); // Init variable
private ThemeChooser(Context context) {
getSelectedTheme(context, false); // Init cache
isOledMode(context, false); // Init cache
}
public static void ChooseTheme(Activity act) {
public static void chooseTheme(Activity act) {
switch(getInstance(act).getSelectedTheme(act, false)) {
case 0: // Auto (Light / Dark)
Log.v(TAG, "Auto (Light / Dark)");
act.setTheme(R.style.AppTheme);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
break;
case 1: // Light Theme
Log.v(TAG, "Light");
act.setTheme(R.style.AppTheme);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
case 2: // Dark Theme
Log.v(TAG, "Dark");
act.setTheme(R.style.AppTheme);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
default:
Log.v(TAG, "Default");
// what would be a meaningful default? Auto mode?
break;
}
@ -77,23 +84,31 @@ public class ThemeChooser {
// Check if the currently loaded theme is different from the one set in the settings, or if OLED mode changed
public boolean themeRequiresRestartOfUI(Context context) {
return !mSelectedTheme.equals(getSelectedTheme(context, true)) ||
!mOledMode.equals(isOledMode(context, true));
boolean themeChanged = !mSelectedTheme.equals(getSelectedTheme(context, true));
boolean oledChanged = !mOledMode.equals(isOledMode(context, true));
return themeChanged || oledChanged;
}
public boolean isDarkTheme(Context context) {
switch(AppCompatDelegate.getDefaultNightMode()) {
case AppCompatDelegate.MODE_NIGHT_YES:
Log.v(TAG, "MODE_NIGHT_YES (Dark Theme)");
return true;
case AppCompatDelegate.MODE_NIGHT_AUTO:
//Log.v(TAG, "MODE_NIGHT_AUTO");
int nightModeFlags = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if(Configuration.UI_MODE_NIGHT_YES == nightModeFlags) {
Log.v(TAG, "MODE_NIGHT_AUTO (Dark Theme)");
return true;
}
// fallthrough is deliberate
Log.v(TAG, "MODE_NIGHT_AUTO (Light Theme)");
return false;
case AppCompatDelegate.MODE_NIGHT_NO:
// fallthrough is deliberate
Log.v(TAG, "MODE_NIGHT_NO (Light Theme)");
return false;
default:
Log.v(TAG, "Undefined Night-Mode");
return false;
}
}
@ -106,7 +121,7 @@ public class ThemeChooser {
return mOledMode;
}
public Integer getSelectedTheme(Context context, boolean forceReloadCache) {
public int getSelectedTheme(Context context, boolean forceReloadCache) {
if(mSelectedTheme == null || forceReloadCache) {
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
mSelectedTheme = Integer.parseInt(mPrefs.getString(SettingsActivity.SP_APP_THEME, "0"));

View file

@ -4,6 +4,13 @@
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppThemeOLED" parent="AppTheme.Base">
<!-- for settings activity -->
<item name="android:textColor">@android:color/white</item>
<item name="android:textColorSecondary">@color/material_grey_500</item>
<!-- for settings activity end-->
<item name="rssItemListBackground">@android:color/black</item>
<item name="news_detail_background_color">@android:color/black</item>
<item name="primaryTextColor">@android:color/white</item>

View file

@ -16,7 +16,8 @@
<SwitchPreference
android:key="cb_oled_mode"
android:title="@string/pref_oled_mode"
android:summary="@string/pref_oled_mode_summary"/>
android:summary="@string/pref_oled_mode_summary"
android:defaultValue="false" />
<ListPreference
@ -50,4 +51,4 @@
<intent android:action="android.settings.SYNC_SETTINGS" />
</Preference>
-->
</PreferenceScreen>
</PreferenceScreen>