1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-08-13 17:03:48 -04:00
k-9/src/com/fsck/k9/activity/K9ActivityCommon.java
m0viefreak a74d57cb71 MessageView: Theme fix when global and message view theme differ
- Since the split-view change, MessageView is only a fragment, so we
  can't call setTheme() anymore so easily.
  Instead, use a ContextThemeWrapper and use that to inflate the
  layout. This way the message header and attachment view
  are styled correctly.

- The HTC WebView fix in SingleMessageView was returning the wrong
  background color, when message view theme and global theme differ,
  because it always used the global theme to retrieve it.
  Fix: Specifically put the light/dark values in the themes.xml,
  and get them using getContext().getTheme().resolveAttribute().
  getContext() will use the ContextThemeWrapper from above, so
  even if the global and message view themes differ, it aleays
  returns the correct one.
  The getThemeBackgroundColor() method added to the K9ActivityMagic
  interface in 309eeb72ac is now not
  needed anymore, and was removed.
2013-02-04 19:30:40 +01:00

116 lines
3.6 KiB
Java

package com.fsck.k9.activity;
import java.util.Locale;
import com.fsck.k9.K9;
import com.fsck.k9.activity.misc.SwipeGestureDetector;
import com.fsck.k9.activity.misc.SwipeGestureDetector.OnSwipeGestureListener;
import com.fsck.k9.helper.StringUtils;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.view.GestureDetector;
import android.view.MotionEvent;
/**
* This class implements functionality common to most activities used in K-9 Mail.
*
* @see K9Activity
* @see K9ListActivity
* @see K9FragmentActivity
*/
public class K9ActivityCommon {
/**
* Creates a new instance of {@link K9ActivityCommon} bound to the specified activity.
*
* @param activity
* The {@link Activity} the returned {@code K9ActivityCommon} instance will be bound to.
*
* @return The {@link K9ActivityCommon} instance that will provide the base functionality of the
* "K9" activities.
*/
public static K9ActivityCommon newInstance(Activity activity) {
return new K9ActivityCommon(activity);
}
public static void setLanguage(Context context, String language) {
Locale locale;
if (StringUtils.isNullOrEmpty(language)) {
locale = Locale.getDefault();
} else if (language.length() == 5 && language.charAt(2) == '_') {
// language is in the form: en_US
locale = new Locale(language.substring(0, 2), language.substring(3));
} else {
locale = new Locale(language);
}
Configuration config = new Configuration();
config.locale = locale;
Resources resources = context.getResources();
resources.updateConfiguration(config, resources.getDisplayMetrics());
}
/**
* Base activities need to implement this interface.
*
* <p>The implementing class simply has to call through to the implementation of these methods
* in {@link K9ActivityCommon}.</p>
*/
public interface K9ActivityMagic {
void setupGestureDetector(OnSwipeGestureListener listener);
}
private Activity mActivity;
private GestureDetector mGestureDetector;
private K9ActivityCommon(Activity activity) {
mActivity = activity;
setLanguage(mActivity, K9.getK9Language());
mActivity.setTheme(K9.getK9ThemeResourceId());
}
/**
* Call this before calling {@code super.dispatchTouchEvent(MotionEvent)}.
*/
public void preDispatchTouchEvent(MotionEvent event) {
if (mGestureDetector != null) {
mGestureDetector.onTouchEvent(event);
}
}
/**
* Get the background color of the theme used for this activity.
*
* @return The background color of the current theme.
*/
public int getThemeBackgroundColor() {
TypedArray array = mActivity.getTheme().obtainStyledAttributes(
new int[] { android.R.attr.colorBackground });
int backgroundColor = array.getColor(0, 0xFF00FF);
array.recycle();
return backgroundColor;
}
/**
* Call this if you wish to use the swipe gesture detector.
*
* @param listener
* A listener that will be notified if a left to right or right to left swipe has been
* detected.
*/
public void setupGestureDetector(OnSwipeGestureListener listener) {
mGestureDetector = new GestureDetector(mActivity,
new SwipeGestureDetector(mActivity, listener));
}
}