session-android/src/org/thoughtcrime/securesms/util/DynamicTheme.java

58 lines
2.0 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.util;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.preference.PreferenceManager;
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
import org.thoughtcrime.securesms.ConversationActivity;
2013-06-23 04:44:12 +00:00
import org.thoughtcrime.securesms.ConversationListActivity;
import org.thoughtcrime.securesms.R;
public class DynamicTheme {
private int currentTheme;
public void onCreate(Activity activity) {
currentTheme = getSelectedTheme(activity);
activity.setTheme(currentTheme);
}
public void onResume(Activity activity) {
if (currentTheme != getSelectedTheme(activity)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
activity.recreate();
} else {
Intent intent = activity.getIntent();
activity.finish();
OverridePendingTransition.invoke(activity);
activity.startActivity(intent);
OverridePendingTransition.invoke(activity);
}
}
}
2013-06-23 04:44:12 +00:00
private static int getSelectedTheme(Activity activity) {
String theme = TextSecurePreferences.getTheme(activity);
if (theme.equals("light")) {
2013-06-23 04:44:12 +00:00
if (activity instanceof ConversationListActivity) return R.style.TextSecure_LightTheme_NavigationDrawer;
else if (activity instanceof ConversationActivity) return R.style.TextSecure_LightTheme_ConversationActivity;
2013-06-23 04:44:12 +00:00
else return R.style.TextSecure_LightTheme;
} else if (theme.equals("dark")) {
2013-06-23 04:44:12 +00:00
if (activity instanceof ConversationListActivity) return R.style.TextSecure_DarkTheme_NavigationDrawer;
else if (activity instanceof ConversationActivity) return R.style.TextSecure_DarkTheme_ConversationActivity;
2013-06-23 04:44:12 +00:00
else return R.style.TextSecure_DarkTheme;
}
return R.style.TextSecure_LightTheme;
}
private static final class OverridePendingTransition {
static void invoke(Activity activity) {
activity.overridePendingTransition(0, 0);
}
}
}