Make 'K9ActivityCommon' usable by all activities

This commit is contained in:
cketti 2019-05-22 13:21:50 +02:00
parent 8910fdf8c1
commit 6257efa007
5 changed files with 64 additions and 42 deletions

View file

@ -26,7 +26,7 @@ public abstract class K9Activity extends AppCompatActivity implements K9Activity
private static final String FRAGMENT_TAG_RATIONALE = "rationale";
private final K9ActivityCommon base = new K9ActivityCommon(this);
private final K9ActivityCommon base = new K9ActivityCommon(this, ThemeType.DEFAULT);
@Override

View file

@ -3,7 +3,6 @@ package com.fsck.k9.activity
import java.util.Locale
import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.text.TextUtils
import android.view.GestureDetector
@ -20,17 +19,26 @@ import com.fsck.k9.ui.R
* This class implements functionality common to most activities used in K-9 Mail.
*
* @see K9Activity
* @see K9ListActivity
* @see K9PreferenceActivity
*/
class K9ActivityCommon(private val activity: Activity) {
class K9ActivityCommon(
private val activity: Activity,
private val themeType: ThemeType
) {
private var gestureDetector: GestureDetector? = null
/**
* Call this before calling `super.onCreate(Bundle)`.
*/
fun preOnCreate() {
setLanguage(activity, K9.k9Language)
activity.setTheme(k9ThemeResourceId)
setLanguage(K9.k9Language)
val theme = when (themeType) {
ThemeType.DEFAULT -> k9ThemeResourceId
ThemeType.ACTION_BAR -> k9ActionBarThemeResourceId
ThemeType.DIALOG -> translucentDialogThemeResourceId
}
activity.setTheme(theme)
}
/**
@ -49,25 +57,24 @@ class K9ActivityCommon(private val activity: Activity) {
gestureDetector = GestureDetector(activity, SwipeGestureDetector(activity, listener))
}
companion object {
@JvmStatic
fun setLanguage(context: Context, language: String) {
val locale = if (TextUtils.isEmpty(language)) {
Resources.getSystem().configuration.locale
} else if (language.length == 5 && language[2] == '_') {
// language is in the form: en_US
Locale(language.substring(0, 2), language.substring(3))
} else {
Locale(language)
}
val resources = context.resources
val config = resources.configuration
config.locale = locale
resources.updateConfiguration(config, resources.displayMetrics)
private fun setLanguage(language: String) {
val locale = if (TextUtils.isEmpty(language)) {
Resources.getSystem().configuration.locale
} else if (language.length == 5 && language[2] == '_') {
// language is in the form: en_US
Locale(language.substring(0, 2), language.substring(3))
} else {
Locale(language)
}
val resources = activity.resources
val config = resources.configuration
config.locale = locale
resources.updateConfiguration(config, resources.displayMetrics)
}
companion object {
@JvmStatic
fun getK9ThemeResourceId(themeId: Theme): Int {
return if (themeId === Theme.LIGHT) R.style.Theme_K9_Light else R.style.Theme_K9_Dark
@ -83,6 +90,12 @@ class K9ActivityCommon(private val activity: Activity) {
@JvmStatic
val k9ThemeResourceId: Int
get() = getK9ThemeResourceId(K9.k9Theme)
private val translucentDialogThemeResourceId: Int
get() = if (k9ThemeResourceId == R.style.Theme_K9_Light)
R.style.Theme_K9_Dialog_Translucent_Light
else
R.style.Theme_K9_Dialog_Translucent_Dark
}
/**
@ -95,3 +108,9 @@ class K9ActivityCommon(private val activity: Activity) {
fun setupGestureDetector(listener: OnSwipeGestureListener)
}
}
enum class ThemeType {
DEFAULT,
ACTION_BAR,
DIALOG
}

View file

@ -1,24 +1,25 @@
package com.fsck.k9.activity;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.Lifecycle.State;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import androidx.annotation.NonNull;
import android.view.MenuItem;
import com.fsck.k9.K9;
public abstract class K9PreferenceActivity extends AppCompatPreferenceActivity implements LifecycleOwner {
private final K9ActivityCommon base = new K9ActivityCommon(this, ThemeType.ACTION_BAR);
private LifecycleRegistry lifecycleRegistry;
@Override
public void onCreate(Bundle icicle) {
K9ActivityCommon.setLanguage(this, K9.getK9Language());
setTheme(K9ActivityCommon.getK9ActionBarThemeResourceId());
base.preOnCreate();
super.onCreate(icicle);
lifecycleRegistry = new LifecycleRegistry(this);
lifecycleRegistry.markState(State.CREATED);

View file

@ -4,7 +4,6 @@ package com.fsck.k9.activity;
import java.util.Collections;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
@ -12,8 +11,8 @@ import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.fsck.k9.Account;
import com.fsck.k9.K9;
import com.fsck.k9.Preferences;
import com.fsck.k9.controller.MessageReference;
import com.fsck.k9.ui.R;
@ -24,12 +23,15 @@ import static com.fsck.k9.controller.MessageReferenceHelper.toMessageReferenceLi
import static com.fsck.k9.controller.MessageReferenceHelper.toMessageReferenceStringList;
public class NotificationDeleteConfirmation extends Activity {
public class NotificationDeleteConfirmation extends AppCompatActivity {
private final static String EXTRA_ACCOUNT_UUID = "accountUuid";
private final static String EXTRA_MESSAGE_REFERENCES = "messageReferences";
private final static int DIALOG_CONFIRM = 1;
private final K9ActivityCommon base = new K9ActivityCommon(this, ThemeType.DIALOG);
private Account account;
private List<MessageReference> messagesToDelete;
@ -51,11 +53,9 @@ public class NotificationDeleteConfirmation extends Activity {
@Override
public void onCreate(Bundle icicle) {
base.preOnCreate();
super.onCreate(icicle);
setTheme(K9.getK9Theme() == K9.Theme.LIGHT ?
R.style.Theme_K9_Dialog_Translucent_Light : R.style.Theme_K9_Dialog_Translucent_Dark);
extractExtras();
showDialog(DIALOG_CONFIRM);

View file

@ -27,8 +27,9 @@ import android.widget.ImageView;
import android.widget.ListAdapter;
import com.fsck.k9.Account;
import com.fsck.k9.K9;
import com.fsck.k9.Preferences;
import com.fsck.k9.activity.K9ActivityCommon;
import com.fsck.k9.activity.ThemeType;
import com.fsck.k9.ui.R;
import com.fsck.k9.ui.dialog.ApgDeprecationWarningDialog;
import org.openintents.openpgp.util.OpenPgpApi;
@ -52,6 +53,9 @@ public class OpenPgpAppSelectDialog extends FragmentActivity {
private static final Intent MARKET_INTENT_FALLBACK = new Intent(Intent.ACTION_VIEW, Uri.parse(
String.format("https://play.google.com/store/apps/details?id=%s", OPENKEYCHAIN_PACKAGE)));
private final K9ActivityCommon base = new K9ActivityCommon(this, ThemeType.DIALOG);
private boolean isStopped;
private Account account;
@ -63,13 +67,11 @@ public class OpenPgpAppSelectDialog extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
base.preOnCreate();
super.onCreate(savedInstanceState);
String accountUuid = getIntent().getStringExtra(EXTRA_ACCOUNT);
account = Preferences.getPreferences(this).getAccount(accountUuid);
setTheme(K9.getK9Theme() == K9.Theme.LIGHT ?
R.style.Theme_K9_Dialog_Translucent_Light : R.style.Theme_K9_Dialog_Translucent_Dark);
}
@Override