Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
SRCT
whats-open-android
Commits
463057f8
Commit
463057f8
authored
Oct 04, 2018
by
Robert Hitt
Browse files
Merge branch '22themes/master/lundquism' into 'master'
22themes/master/lundquism Closes
#22
See merge request
!12
parents
594aea9f
a380373c
Pipeline
#3089
passed with stages
in 3 minutes and 14 seconds
Changes
8
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
app/src/main/java/srct/whatsopen/views/activities/BaseThemeActivity.java
0 → 100644
View file @
463057f8
package
srct.whatsopen.views.activities
;
import
android.content.SharedPreferences
;
import
android.content.pm.PackageManager
;
import
android.content.res.Resources
;
import
android.os.Bundle
;
import
android.preference.PreferenceManager
;
import
android.support.v7.app.AppCompatActivity
;
import
android.util.Log
;
import
android.view.ContextThemeWrapper
;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.Method
;
import
srct.whatsopen.R
;
public
abstract
class
BaseThemeActivity
extends
AppCompatActivity
{
private
static
SharedPreferences
prefs
;
/**
* This is overriden so the theme can be changed, but it only works statically because
* <a href="https://developer.android.com/reference/android/view/ContextThemeWrapper#setTheme(int)">setTheme(int)</a>
* only applies after the contentView is set.
* @param bundle This is just your classic bundle
*/
@Override
protected
void
onCreate
(
Bundle
bundle
)
{
prefs
=
PreferenceManager
.
getDefaultSharedPreferences
(
this
);
boolean
dark
=
prefs
.
getBoolean
(
"dark_theme"
,
false
);
// look at the udacity course. In this section you might want to reload,
// but this would mean every activity would have to set the right theme
Log
.
e
(
"BaseThemeActivity"
,
".onCreate Recreate called?"
);
if
(
dark
){
setTheme
(
R
.
style
.
darkTheme
);
Log
.
d
(
"BaseThemeActivity"
,
".onCreate() Theme is now dark"
);
}
else
{
setTheme
(
R
.
style
.
AppTheme
);
Log
.
e
(
"BaseThemeActivity"
,
".onCreate() Theme is now light"
);
}
super
.
onCreate
(
bundle
);
}
/**
* This is overriden so that the theme can be changed dynamically. onStart is called, but
* the themes are wrong, onStart will call recreate, which will destroy the app and re-create it.
* This is a dangerous practice as it could destroy volatile data,
* but we don't have any volatile data so who cares. It will set the theme correctly.
*/
@Override
protected
void
onStart
(){
boolean
dark
=
prefs
.
getBoolean
(
"dark_theme"
,
false
);
int
actTheme
=
getThemeID
();
if
(
actTheme
==
R
.
style
.
darkTheme
&&
!
dark
){
Log
.
d
(
"BaseThemeActivity"
,
".onStart calling recreate to set to light"
);
recreate
();
}
else
if
(
actTheme
==
R
.
style
.
AppTheme
&&
dark
){
Log
.
d
(
"BaseThemeActivity"
,
".onStart calling recreate to set to dark"
);
recreate
();
}
else
{
super
.
onStart
();
}
}
/**
gets the current theme's resid:
<a href="https://stackoverflow.com/questions/7267852/android-how-to-obtain-the-resource-id-of-the-current-theme">
getThemeResId()</a>
note, the top answer on this page returns the default theme's ID, not the actual theme.
The stackoverflow warned about using getThemeResId because it was designed to be private.
@return returns the int resid of the current theme
*/
private
int
getThemeID
(){
int
themeResId
=
0
;
String
TAG
=
"BaseThemeActivity"
;
try
{
Class
<?>
clazz
=
ContextThemeWrapper
.
class
;
Method
method
=
clazz
.
getMethod
(
"getThemeResId"
);
method
.
setAccessible
(
true
);
themeResId
=
(
Integer
)
method
.
invoke
(
this
);
}
catch
(
NoSuchMethodException
e
)
{
Log
.
e
(
TAG
,
".getThemeID Failed to get theme resource ID"
,
e
);
}
catch
(
IllegalAccessException
e
)
{
Log
.
e
(
TAG
,
".getThemeID Failed to get theme resource ID"
,
e
);
}
catch
(
IllegalArgumentException
e
)
{
Log
.
e
(
TAG
,
".getThemeID Failed to get theme resource ID"
,
e
);
}
catch
(
InvocationTargetException
e
)
{
Log
.
e
(
TAG
,
".getThemeID Failed to get theme resource ID"
,
e
);
}
return
themeResId
;
}
}
app/src/main/java/srct/whatsopen/views/activities/DetailActivity.java
View file @
463057f8
...
...
@@ -42,7 +42,7 @@ import srct.whatsopen.presenters.FacilityPresenter;
import
srct.whatsopen.views.fragments.NotificationDialogFragment
;
public
class
DetailActivity
extends
AppCompat
Activity
implements
FacilityView
,
public
class
DetailActivity
extends
BaseTheme
Activity
implements
FacilityView
,
NotificationDialogFragment
.
NotificationDialogListener
{
@BindView
(
R
.
id
.
open_status
)
...
...
app/src/main/java/srct/whatsopen/views/activities/MainActivity.java
View file @
463057f8
...
...
@@ -27,7 +27,7 @@ import srct.whatsopen.views.adapters.FacilityListAdapter;
import
srct.whatsopen.views.adapters.FacilityListFragmentPagerAdapter
;
import
srct.whatsopen.views.fragments.FacilityListFragment
;
public
class
MainActivity
extends
AppCompat
Activity
implements
MainView
{
public
class
MainActivity
extends
BaseTheme
Activity
implements
MainView
{
@BindView
(
R
.
id
.
progress_bar
)
ProgressBar
mProgressBar
;
...
...
app/src/main/java/srct/whatsopen/views/activities/SettingsActivity.java
View file @
463057f8
package
srct.whatsopen.views.activities
;
import
android.content.Intent
;
import
android.content.SharedPreferences
;
import
android.content.pm.ActivityInfo
;
import
android.content.res.Configuration
;
...
...
@@ -7,6 +8,7 @@ import android.os.Bundle;
import
android.support.v7.app.AppCompatActivity
;
import
android.support.v7.preference.PreferenceManager
;
import
android.support.v7.widget.Toolbar
;
import
android.util.Log
;
import
android.view.Menu
;
import
android.view.MenuItem
;
...
...
@@ -15,7 +17,7 @@ import srct.whatsopen.MyApplication;
import
srct.whatsopen.R
;
import
srct.whatsopen.views.fragments.SettingsFragment
;
public
class
SettingsActivity
extends
AppCompat
Activity
{
public
class
SettingsActivity
extends
BaseTheme
Activity
{
private
SharedPreferences
mSharedPreferences
;
...
...
@@ -72,6 +74,9 @@ public class SettingsActivity extends AppCompatActivity {
public
void
onSharedPreferenceChanged
(
SharedPreferences
prefs
,
String
key
)
{
if
(
key
.
equals
(
"turn_off_rotation_preference"
))
{
MyApplication
.
setRotation
(
SettingsActivity
.
this
);
}
else
if
(
key
.
equals
(
"dark_theme"
)){
Log
.
d
(
"SettingsActivity"
,
".onSharedPreferenceChanged: Button was clicked to reset Themes."
);
recreate
();
}
}
};
...
...
app/src/main/res/values-v21/styles.xml
View file @
463057f8
...
...
@@ -14,4 +14,18 @@
</style>
<!-- Dark theme when the dark button from the preferences settings screen is clicked,
the theme switches to this theme.-->
<style
name=
"darkTheme"
parent=
"Theme.AppCompat.NoActionBar"
>
<!-- Customize your theme here. -->
<item
name=
"colorPrimary"
>
@color/colorPrimary
</item>
<item
name=
"colorPrimaryDark"
>
@color/colorPrimaryDark
</item>
<item
name=
"colorAccent"
>
@color/colorAccent
</item>
<item
name=
"android:colorControlHighlight"
>
@color/tabButtonPressed
</item>
<item
name=
"android:homeAsUpIndicator"
>
@drawable/ic_arrow_back_white
</item>
<item
name=
"preferenceTheme"
>
@style/PreferenceThemeOverlay
</item>
<item
name=
"android:windowContentTransitions"
>
true
</item>
</style>
</resources>
\ No newline at end of file
app/src/main/res/values/strings.xml
View file @
463057f8
...
...
@@ -17,6 +17,7 @@
<string
name=
"title_turn_off_rotation"
>
Turn off rotation
</string>
<string
name=
"title_notification"
>
Notifications
</string>
<string
name=
"title_turn_off_vibrations"
>
Turn off vibrations
</string>
<string
name=
"title_dark_theme"
>
Dark theme
</string>
<string-array
name=
"entries_list_view_information_preference"
>
<item>
Both
</item>
<item>
Only for open Facilities
</item>
...
...
app/src/main/res/values/styles.xml
View file @
463057f8
...
...
@@ -11,4 +11,14 @@
<item
name=
"preferenceTheme"
>
@style/PreferenceThemeOverlay
</item>
</style>
<!-- Dark theme when the dark button from the preferences settings screen is clicked,
the theme switches to this theme.-->
<style
name=
"darkTheme"
parent=
"Theme.AppCompat.NoActionBar"
>
<!-- Customize your theme here. -->
<item
name=
"colorPrimary"
>
@color/colorPrimary
</item>
<item
name=
"colorPrimaryDark"
>
@color/colorPrimaryDark
</item>
<item
name=
"colorAccent"
>
@color/colorAccent
</item>
<item
name=
"android:homeAsUpIndicator"
>
@drawable/ic_arrow_back_white
</item>
<item
name=
"preferenceTheme"
>
@style/PreferenceThemeOverlay
</item>
</style>
</resources>
app/src/main/res/xml/settings.xml
View file @
463057f8
...
...
@@ -17,6 +17,12 @@
android:entries=
"@array/entries_list_view_default_tab_preference"
android:entryValues=
"@array/entryvalues_list_view_default_tab_preference"
/>
<SwitchPreference
android:defaultValue=
"false"
android:key=
"dark_theme"
android:title=
"@string/title_dark_theme"
android:entries=
"@array/entries_list_view_default_tab_preference"
android:entryValues=
"@array/entryvalues_list_view_default_tab_preference"
/>
</PreferenceCategory>
<PreferenceCategory
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment