Example usage for android.content.res Resources updateConfiguration

List of usage examples for android.content.res Resources updateConfiguration

Introduction

In this page you can find the example usage for android.content.res Resources updateConfiguration.

Prototype

@Deprecated
public void updateConfiguration(Configuration config, DisplayMetrics metrics) 

Source Link

Document

Store the newly updated configuration.

Usage

From source file:Main.java

public static void changeAppLanguage(Resources resources, String lanAtr) {
    Configuration config = resources.getConfiguration();
    DisplayMetrics dm = resources.getDisplayMetrics();
    if (lanAtr.equals("zh-cn")) {
        config.locale = Locale.SIMPLIFIED_CHINESE;
    } else {/*from ww w .  ja va2s  . c o m*/
        config.locale = Locale.getDefault();
    }
    resources.updateConfiguration(config, dm);
}

From source file:Main.java

/**
 * Setting {@link Locale} for {@link Context}.
 *//*w  w w . j a v a 2 s . co m*/
public static Context applyLanguageForContext(Context context, Locale locale) {
    Resources resources = context.getResources();
    Configuration config = resources.getConfiguration();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        config.setLocale(locale);
        return context.createConfigurationContext(config);
    } else {
        config.locale = locale;
        resources.updateConfiguration(config, resources.getDisplayMetrics());
        return context;
    }
}

From source file:Main.java

public static void setLocale(Context paramContext, Locale paramLocale) {
    Resources localResources = paramContext.getResources();
    Configuration localConfiguration = localResources.getConfiguration();
    if (localConfiguration.locale.equals(paramLocale)) {

    } else {//  ww w.j a v  a  2s  .  c  o m
        DisplayMetrics localDisplayMetrics = localResources.getDisplayMetrics();
        localConfiguration.locale = paramLocale;
        localResources.updateConfiguration(localConfiguration, localDisplayMetrics);
        Resources.getSystem().updateConfiguration(localConfiguration, localDisplayMetrics);
    }
}

From source file:eu.faircode.netguard.ServiceJob.java

private static void submit(Rule rule, PersistableBundle bundle, Context context) {
    PackageManager pm = context.getPackageManager();
    JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);

    // Get english application label
    String label = null;//from w ww  .j a v  a2  s  .c  o m
    try {
        Configuration config = new Configuration();
        config.setLocale(new Locale("en"));
        Resources res = pm.getResourcesForApplication(rule.info.packageName);
        res.updateConfiguration(config, res.getDisplayMetrics());
        label = res.getString(rule.info.applicationInfo.labelRes);
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        CharSequence cs = rule.info.applicationInfo.loadLabel(pm);
        if (cs != null)
            label = cs.toString();
    }

    // Add application data
    bundle.putInt("uid", rule.info.applicationInfo.uid);
    bundle.putString("package", rule.info.packageName);
    bundle.putInt("version_code", rule.info.versionCode);
    bundle.putString("version_name", rule.info.versionName);
    bundle.putString("label", label);
    bundle.putInt("system", rule.system ? 1 : 0);
    try {
        bundle.putString("installer", pm.getInstallerPackageName(rule.info.packageName));
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        bundle.putString("installer", null);
    }

    // Cancel overlapping jobs
    for (JobInfo pending : scheduler.getAllPendingJobs()) {
        String type = pending.getExtras().getString("type");
        if (type != null && type.equals(bundle.getString("type"))) {
            if (type.equals("rule")) {
                int uid = pending.getExtras().getInt("uid");
                if (uid == bundle.getInt("uid")) {
                    Log.i(TAG, "Canceling id=" + pending.getId());
                    scheduler.cancel(pending.getId());
                }
            } else if (type.equals("host")) {
                int uid = pending.getExtras().getInt("uid");
                int version = pending.getExtras().getInt("version");
                int protocol = pending.getExtras().getInt("protocol");
                String daddr = pending.getExtras().getString("daddr");
                int dport = pending.getExtras().getInt("dport");
                if (uid == bundle.getInt("uid") && version == bundle.getInt("version")
                        && protocol == bundle.getInt("protocol") && daddr != null
                        && daddr.equals(bundle.getString("daddr")) && dport == bundle.getInt("dport")) {
                    Log.i(TAG, "Canceling id=" + pending.getId());
                    scheduler.cancel(pending.getId());
                }
            }
        }
    }

    // Schedule job
    ComponentName serviceName = new ComponentName(context, ServiceJob.class);
    JobInfo job = new JobInfo.Builder(++id, serviceName).setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
            .setMinimumLatency(Util.isDebuggable(context) ? 10 * 1000 : 60 * 1000).setExtras(bundle)
            .setPersisted(true).build();
    if (scheduler.schedule(job) == JobScheduler.RESULT_SUCCESS)
        Log.i(TAG, "Scheduled job=" + job.getId() + " success");
    else
        Log.e(TAG, "Scheduled job=" + job.getId() + " failed");
}

From source file:Main.java

/**
 * Get a dimension for the specified orientation.
 * This method may be heavy since it updates the {@code resources} twice.
 *//*from w ww.  jav  a  2  s. c  o m*/
public static float getDimensionForOrientation(Resources resources, int id, int orientation) {
    Configuration configuration = resources.getConfiguration();
    if (configuration.orientation == orientation) {
        return resources.getDimension(id);
    }

    Configuration originalConfiguration = new Configuration(resources.getConfiguration());
    try {
        configuration.orientation = orientation;
        resources.updateConfiguration(configuration, null);
        return resources.getDimension(id);
    } finally {
        resources.updateConfiguration(originalConfiguration, null);
    }
}

From source file:Main.java

public static void updateConfiguration(Context context) {
    Resources resources = context.getResources();
    Configuration conf = resources.getConfiguration();
    Locale current = Locale.getDefault();
    String currentLanguage = current.getDisplayLanguage();

    if (Locale.CHINESE.getDisplayLanguage().equals(currentLanguage)
            || Locale.ENGLISH.getDisplayLanguage().equals(currentLanguage)) {
        return;/*ww w  .j a  v a 2 s .c o m*/
    }

    conf.locale = Locale.ENGLISH;
    resources.updateConfiguration(conf, resources.getDisplayMetrics());
}

From source file:Main.java

public static void setLanguage(Context context, String language) {
    Resources resources = context.getResources();
    Configuration config = resources.getConfiguration();
    DisplayMetrics dm = resources.getDisplayMetrics();
    if (mLocaleMap == null) {
        config.locale = mDefaultLocale;//ww  w .  j a v a  2 s  .co m
    } else {
        if (mLocaleMap.containsKey(language)) {
            config.locale = mLocaleMap.get(language);
        } else {
            config.locale = mDefaultLocale;
        }
    }
    resources.updateConfiguration(config, dm);
}

From source file:me.zhang.bingo.Utility.java

public static void applyAppLanguage(Context context) {
    Locale myLocale = Utility.getChoosedLocale(context);
    Resources res = context.getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;/*from w  ww. j  a v  a 2s .co m*/
    res.updateConfiguration(conf, dm);
}

From source file:Main.java

public static void setLocale(String lang, Resources res) {
    Locale myLocale;/*from ww  w  .j  a  v a2 s  . c o  m*/
    if (lang.equalsIgnoreCase("zh-rTW")) {
        myLocale = Locale.TRADITIONAL_CHINESE;
    } else if (lang.equalsIgnoreCase("zh-rCN") || lang.equalsIgnoreCase("zh")) {
        myLocale = Locale.SIMPLIFIED_CHINESE;
    } else if (lang.equalsIgnoreCase("pt-rBR") || lang.equalsIgnoreCase("pt")) {
        myLocale = new Locale("pt", "BR");
    } else if (lang.equalsIgnoreCase("pt-rPT")) {
        myLocale = new Locale("pt", "PT");
    } else {
        myLocale = new Locale(lang);
    }
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
}

From source file:com.bilibili.magicasakura.utils.ThemeUtils.java

public static Resources updateNightMode(Resources resource, boolean on) {
    DisplayMetrics dm = resource.getDisplayMetrics();
    Configuration config = resource.getConfiguration();
    final int uiModeNightMaskOrigin = config.uiMode &= ~Configuration.UI_MODE_TYPE_MASK;
    final int uiModeNightMaskNew = on ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO;
    if (uiModeNightMaskOrigin != uiModeNightMaskNew) {
        config.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
        config.uiMode |= uiModeNightMaskNew;
        resource.updateConfiguration(config, dm);
    }//from w  w  w. j  a va2 s. com
    return resource;
}