Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] stop using updateConfiguration #315

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,8 +1,6 @@
package io.github.droidkaigi.confsched2017.util;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.text.TextUtils;
Expand All @@ -24,6 +22,7 @@
public class LocaleUtil {

private static final Locale DEFAULT_LANG = Locale.ENGLISH;

public static final List<Locale> SUPPORT_LANG = Arrays.asList(Locale.JAPANESE, Locale.ENGLISH);

private static final String TAG = LocaleUtil.class.getSimpleName();
Expand All @@ -34,19 +33,8 @@ public static void initLocale(Context context) {
setLocale(context, getCurrentLanguageId(context));
}

@SuppressWarnings("deprecation")
public static void setLocale(Context context, String languageId) {
Configuration config = new Configuration();
DefaultPrefs.get(context).putLanguageId(languageId);
Locale locale = new Locale(languageId);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale);
} else {
config.locale = locale;
}
// updateConfiguration, deprecated in API 25.
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}

public static String getCurrentLanguageId(Context context) {
Expand Down
Expand Up @@ -3,6 +3,8 @@
import com.tomoima.debot.Debot;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
Expand All @@ -13,12 +15,16 @@
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MenuItem;

import java.util.Locale;

import io.github.droidkaigi.confsched2017.MainApplication;
import io.github.droidkaigi.confsched2017.di.ActivityComponent;
import io.github.droidkaigi.confsched2017.di.ActivityModule;
import io.github.droidkaigi.confsched2017.util.LocaleUtil;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;

public abstract class BaseActivity extends AppCompatActivity {
Expand All @@ -28,6 +34,7 @@ public abstract class BaseActivity extends AppCompatActivity {
}

private ActivityComponent activityComponent;

private Debot debot;

@NonNull
Expand All @@ -41,7 +48,8 @@ public ActivityComponent getComponent() {

@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
String currentLanguageId = LocaleUtil.getCurrentLanguageId(this);
super.attachBaseContext(CalligraphyContextWrapper.wrap(setUpLocale(newBase, currentLanguageId)));
}

@Override
Expand Down Expand Up @@ -99,4 +107,28 @@ final void initBackToolbar(Toolbar toolbar) {
bar.setHomeButtonEnabled(true);
}
}

@SuppressWarnings("deprecation")
private Context setUpLocale(Context context, String language) {

if (TextUtils.isEmpty(language)) {
return context;
}

Configuration config = context.getResources().getConfiguration();

Locale locale = new Locale(language);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale);
} else {
config.locale = locale;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return context.createConfigurationContext(config);
} else {
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
return context;
}
}
}