Added theme selection

This commit is contained in:
M66B 2016-01-02 16:38:24 +01:00
parent ec046b0e9c
commit 730844b3cc
11 changed files with 121 additions and 44 deletions

View File

@ -26,7 +26,7 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppThemeTeal">
<activity
android:name=".ActivityMain"

View File

@ -89,13 +89,13 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
return;
}
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
setTheme(prefs.getBoolean("dark_theme", false) ? R.style.AppThemeDark : R.style.AppTheme);
Util.setTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
running = true;
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean enabled = prefs.getBoolean("enabled", false);
boolean initialized = prefs.getBoolean("initialized", false);
@ -378,7 +378,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuSearch);
updateApplicationList(menuSearch.isActionViewExpanded() ? searchView.getQuery().toString() : null);
} else if ("dark_theme".equals(name))
} else if ("theme".equals(name) || "dark_theme".equals(name))
recreate();
}

View File

@ -52,9 +52,7 @@ public class ActivityPro extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "Create");
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
setTheme(prefs.getBoolean("dark_theme", false) ? R.style.AppThemeDark : R.style.AppTheme);
Util.setTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.pro);

View File

@ -34,6 +34,7 @@ import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.MultiSelectListPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
@ -83,11 +84,8 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
private static final Intent INTENT_VPN_SETTINGS = new Intent("android.net.vpn.SETTINGS");
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
setTheme(prefs.getBoolean("dark_theme", false) ? R.style.AppThemeDark : R.style.AppTheme);
Util.setTheme(this);
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new FragmentSettings()).commit();
getSupportActionBar().setTitle(R.string.menu_settings);
}
@ -127,6 +125,17 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
Preference pref_screen_delay = screen.findPreference("screen_delay");
pref_screen_delay.setTitle(getString(R.string.setting_delay, prefs.getString("screen_delay", "0")));
// Handle theme
Preference pref_screen_theme = screen.findPreference("theme");
String theme = prefs.getString("theme", "teal");
String[] themeNames = getResources().getStringArray(R.array.themeNames);
String[] themeValues = getResources().getStringArray(R.array.themeValues);
for (int i = 0; i < themeNames.length; i++)
if (theme.equals(themeValues[i])) {
pref_screen_theme.setTitle(getString(R.string.setting_theme, themeNames[i]));
break;
}
// Handle stats
EditTextPreference pref_stats_base = (EditTextPreference) screen.findPreference("stats_base");
EditTextPreference pref_stats_frequency = (EditTextPreference) screen.findPreference("stats_frequency");
@ -277,10 +286,10 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
@TargetApi(Build.VERSION_CODES.M)
public void onSharedPreferenceChanged(SharedPreferences prefs, String name) {
// Pro features
if ("dark_theme".equals(name)) {
if (prefs.getBoolean(name, false) && !IAB.isPurchased(ActivityPro.SKU_THEME, this)) {
prefs.edit().putBoolean(name, false).apply();
((SwitchPreference) getPreferenceScreen().findPreference(name)).setChecked(false);
if ("theme".equals(name)) {
if (!"teal".equals(prefs.getString(name, "teal")) && !IAB.isPurchased(ActivityPro.SKU_THEME, this)) {
prefs.edit().putString(name, "teal").apply();
((ListPreference) getPreferenceScreen().findPreference(name)).setValue("teal");
startActivity(new Intent(this, ActivityPro.class));
return;
}
@ -320,10 +329,11 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
else if ("screen_delay".equals(name))
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_delay, prefs.getString(name, "0")));
else if ("dark_theme".equals(name))
else if ("theme".equals(name) || "dark_theme".equals(name)) {
SinkholeService.setTheme(this);
recreate();
else if ("wifi_homes".equals(name)) {
} else if ("wifi_homes".equals(name)) {
MultiSelectListPreference pref_wifi_homes = (MultiSelectListPreference) getPreferenceScreen().findPreference(name);
Set<String> ssid = prefs.getStringSet(name, new HashSet<String>());
if (ssid.size() > 0)
@ -725,7 +735,7 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
else {
// Pro features
if (current == application) {
if ("dark_theme".equals(key)) {
if ("theme".equals(key)) {
if (!IAB.isPurchased(ActivityPro.SKU_THEME, context))
return;
} else if ("show_stats".equals(key)) {

View File

@ -116,8 +116,8 @@ public class Receiver extends BroadcastReceiver {
main.putExtra(ActivityMain.EXTRA_SEARCH, name);
PendingIntent pi = PendingIntent.getActivity(context, 999, main, PendingIntent.FLAG_UPDATE_CURRENT);
Util.setTheme(context);
TypedValue tv = new TypedValue();
context.setTheme(prefs.getBoolean("dark_theme", false) ? R.style.AppThemeDark : R.style.AppTheme);
context.getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_security_white_24dp)

View File

@ -105,7 +105,7 @@ public class SinkholeService extends VpnService {
private static final int MSG_STATS_STOP = 2;
private static final int MSG_STATS_UPDATE = 3;
public enum Command {start, reload, stop, stats, set}
public enum Command {start, reload, stop, stats, set, theme}
private static volatile PowerManager.WakeLock wlInstance = null;
@ -245,6 +245,10 @@ public class SinkholeService extends VpnService {
case set:
set(intent);
break;
case theme:
setTheme(intent);
break;
}
// Update main view
@ -500,6 +504,12 @@ public class SinkholeService extends VpnService {
}
}
private void setTheme(Intent intent) {
Util.setTheme(this);
stopForeground(true);
startForeground(NOTIFY_FOREGROUND, getForegroundNotification(0, 0));
}
private ParcelFileDescriptor startVPN() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
@ -800,11 +810,9 @@ public class SinkholeService extends VpnService {
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Create");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
setTheme(prefs.getBoolean("dark_theme", false) ? R.style.AppThemeDark : R.style.AppTheme);
Util.setTheme(this);
super.onCreate();
HandlerThread thread = new HandlerThread(getString(R.string.app_name) + " handler");
thread.start();
@ -1003,6 +1011,15 @@ public class SinkholeService extends VpnService {
context.startService(intent);
}
public static void setTheme(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (prefs.getBoolean("enabled", false)) {
Intent intent = new Intent(context, SinkholeService.class);
intent.putExtra(EXTRA_COMMAND, Command.theme);
context.startService(intent);
}
}
public static void reloadStats(String reason, Context context) {
Intent intent = new Intent(context, SinkholeService.class);
intent.putExtra(EXTRA_COMMAND, Command.stats);

View File

@ -311,6 +311,16 @@ public class Util {
}
}
public static void setTheme(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean dark = prefs.getBoolean("dark_theme", false);
String theme = prefs.getString("theme", "teal");
if (theme.equals("teal"))
context.setTheme(dark ? R.style.AppThemeTealDark : R.style.AppThemeTeal);
else if (theme.equals("blue"))
context.setTheme(dark ? R.style.AppThemeBlueDark : R.style.AppThemeBlue);
}
public static int dips2pixels(int dips, Context context) {
return Math.round(dips * context.getResources().getDisplayMetrics().density + 0.5f);
}

View File

@ -1,15 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Teal 500 -->
<color name="colorDefaultPrimary">#009688</color>
<color name="colorTealPrimary">#009688</color>
<!-- Teal 600-->
<color name="colorDefaultPrimaryDark">#00796B</color>
<color name="colorTealPrimaryDark">#00796B</color>
<!-- Deep orange A200 -->
<color name="colorDefaultAccent">#FF5722</color>
<color name="colorTealAccent">#FF5722</color>
<color name="colorDefaultOn">#009688</color>
<color name="colorDefaultOff">#FF5722</color>
<color name="colorTealOn">#009688</color>
<color name="colorTealOff">#FF5722</color>
<color name="colorSend">#FF5722</color>
<color name="colorReceive">#009688</color>
<color name="colorBluePrimary">#0066CC</color>
<color name="colorBluePrimaryDark">#003366</color>
<color name="colorBlueAccent">#FF3333</color>
<color name="colorBlueOn">#0066CC</color>
<color name="colorBlueOff">#FF3333</color>
<color name="colorSend">#FF0000</color>
<color name="colorReceive">#0000FF</color>
</resources>

View File

@ -34,6 +34,7 @@ These issues are caused by bugs in Android, or in the software provided by the m
<string name="setting_system">Manage system applications</string>
<string name="setting_auto">Auto enable after %1$s minutes</string>
<string name="setting_delay">Delay screen off %1$s minutes</string>
<string name="setting_theme">Theme: %1$s</string>
<string name="setting_dark">Use dark theme</string>
<string name="setting_wifi_home">Wi-Fi home networks: %1$s</string>
<string name="setting_metered">Handle metered Wi-Fi networks</string>
@ -107,5 +108,15 @@ Since NetGuard has no internet permission, you know your internet traffic is not
<string name="title_pro_challenge">Challenge</string>
<string name="title_pro_reponse">Response</string>
<string-array name="themeNames">
<item>Teal/Orange</item>
<item>Blue/Red</item>
</string-array>
<string-array name="themeValues" translatable="false">
<item>teal</item>
<item>blue</item>
</string-array>
<string name="fingerprint" translatable="false">ef46f813d2c8a064d72c936b9b96d1cccc989378</string>
</resources>

View File

@ -3,22 +3,42 @@
<attr name="colorOff" format="reference" />
<attr name="expander" format="reference" />
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorDefaultPrimary</item>
<item name="colorPrimaryDark">@color/colorDefaultPrimaryDark</item>
<item name="colorAccent">@color/colorDefaultAccent</item>
<item name="colorOn">@color/colorDefaultOn</item>
<item name="colorOff">@color/colorDefaultOff</item>
<style name="AppThemeTeal" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorTealPrimary</item>
<item name="colorPrimaryDark">@color/colorTealPrimaryDark</item>
<item name="colorAccent">@color/colorTealAccent</item>
<item name="colorOn">@color/colorTealOn</item>
<item name="colorOff">@color/colorTealOff</item>
<item name="expander">@drawable/expander_black</item>
<item name="android:windowDisablePreview">true</item>
</style>
<style name="AppThemeDark" parent="Theme.AppCompat">
<item name="colorPrimary">@color/colorDefaultPrimary</item>
<item name="colorPrimaryDark">@color/colorDefaultPrimaryDark</item>
<item name="colorAccent">@color/colorDefaultAccent</item>
<item name="colorOn">@color/colorDefaultOn</item>
<item name="colorOff">@color/colorDefaultOff</item>
<style name="AppThemeTealDark" parent="Theme.AppCompat">
<item name="colorPrimary">@color/colorTealPrimary</item>
<item name="colorPrimaryDark">@color/colorTealPrimaryDark</item>
<item name="colorAccent">@color/colorTealAccent</item>
<item name="colorOn">@color/colorTealOn</item>
<item name="colorOff">@color/colorTealOff</item>
<item name="expander">@drawable/expander_white</item>
<item name="android:windowDisablePreview">true</item>
</style>
<style name="AppThemeBlue" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorBluePrimary</item>
<item name="colorPrimaryDark">@color/colorBluePrimaryDark</item>
<item name="colorAccent">@color/colorBlueAccent</item>
<item name="colorOn">@color/colorBlueOn</item>
<item name="colorOff">@color/colorBlueOff</item>
<item name="expander">@drawable/expander_black</item>
<item name="android:windowDisablePreview">true</item>
</style>
<style name="AppThemeBlueDark" parent="Theme.AppCompat">
<item name="colorPrimary">@color/colorBluePrimary</item>
<item name="colorPrimaryDark">@color/colorBluePrimaryDark</item>
<item name="colorAccent">@color/colorBlueAccent</item>
<item name="colorOn">@color/colorBlueOn</item>
<item name="colorOff">@color/colorBlueOff</item>
<item name="expander">@drawable/expander_white</item>
<item name="android:windowDisablePreview">true</item>
</style>

View File

@ -42,6 +42,11 @@
android:inputType="number"
android:key="screen_delay"
android:summary="@string/summary_delay" />
<ListPreference
android:defaultValue="teal"
android:entries="@array/themeNames"
android:entryValues="@array/themeValues"
android:key="theme" />
<SwitchPreference
android:defaultValue="false"
android:key="dark_theme"