Android Open Source - unbounce-android Locale Helper






From Project

Back to project page unbounce-android.

License

The source code is released under:

MIT License

If you think the Android project unbounce-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ryansteckler.nlpunbounce.helpers;
// www . ja v  a2  s  . co m
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;

import java.util.Locale;

/**
 * Created by rsteckler on 10/13/14.
 */
public class LocaleHelper {
    public static int sForceEnglish = -1;
    private static String sLocale = null;

    /**
     * Set the locale of the Activity, and restart it by creating a new Activity of the same type.
     */

    private LocaleHelper() {
    }

    public static void forceEnglish(Activity activity) {
        sForceEnglish = 1;
        activity.finish();
        activity.startActivity(new Intent(activity, activity.getClass()));
    }

    public static void revertToSystem(Activity activity) {
        sForceEnglish = 0;
        activity.finish();
        activity.startActivity(new Intent(activity, activity.getClass()));
    }

    /**
     * Set the theme of the activity, according to the configuration.
     */
    public static int onActivityCreateSetLocale(Activity activity) {
        if (sForceEnglish == -1) {
            // Load from prefs
            SharedPreferences prefs = activity.getSharedPreferences("com.ryansteckler.nlpunbounce" + "_preferences", Context.MODE_WORLD_READABLE);
            sForceEnglish = prefs.getBoolean("force_english", false) ? 1 : 0;
        }

        if (sForceEnglish == 1) {
            Locale locale = new Locale("en");
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            activity.getBaseContext().getResources().updateConfiguration(config, activity.getBaseContext().getResources().getDisplayMetrics());
        } else {
            Locale defaultLocale = activity.getResources().getSystem().getConfiguration().locale;
            Locale.setDefault(defaultLocale);
            Configuration config = new Configuration();
            config.locale = defaultLocale;
            activity.getBaseContext().getResources().updateConfiguration(config, activity.getBaseContext().getResources().getDisplayMetrics());
        }
        return sForceEnglish;
    }

    public static int onActivityResumeVerifyLocale(Activity activity, int curForceEnglish) {
        if (curForceEnglish != sForceEnglish) {
            if (sForceEnglish == 1) {
                forceEnglish(activity);
            } else {
                revertToSystem(activity);
            }
        }

        return sForceEnglish;
    }

    public static int getForceEnglish() {
        return sForceEnglish;
    }

    public static String getFormattedTime(java.lang.Object... paramTimes) {
        StringBuilder sbuf = new StringBuilder();
        int offset = 0;
        if (5 == paramTimes.length) {
            offset = 2;
        } else if (4 == paramTimes.length) {
            offset = 1;
        }
        for (int i = 0; i < paramTimes.length; i++) {
            if (i < (paramTimes.length - offset)) {
                if ((long) paramTimes[i] > 0) {
                    sbuf.append(String.format("%02d", paramTimes[i]));
                    sbuf.append(":");
                } else {
                    continue;
                }
            } else {
                sbuf.append(String.format("%02d", paramTimes[i]));
                sbuf.append(":");
            }
        }
        sbuf.deleteCharAt(sbuf.lastIndexOf(":"));
        return sbuf.toString();
    }
}




Java Source Code List

com.example.rsteckler.myapplication.ApplicationTest.java
com.ryansteckler.inappbilling.Base64DecoderException.java
com.ryansteckler.inappbilling.Base64.java
com.ryansteckler.inappbilling.IabException.java
com.ryansteckler.inappbilling.IabHelper.java
com.ryansteckler.inappbilling.IabResult.java
com.ryansteckler.inappbilling.Inventory.java
com.ryansteckler.inappbilling.Purchase.java
com.ryansteckler.inappbilling.Security.java
com.ryansteckler.inappbilling.SkuDetails.java
com.ryansteckler.nlpunbounce.ActivityReceiver.java
com.ryansteckler.nlpunbounce.AlarmDetailFragment.java
com.ryansteckler.nlpunbounce.AlarmsFragment.java
com.ryansteckler.nlpunbounce.BaseDetailFragment.java
com.ryansteckler.nlpunbounce.ExpandingLayout.java
com.ryansteckler.nlpunbounce.HomeFragment.java
com.ryansteckler.nlpunbounce.MaterialSettingsActivity.java
com.ryansteckler.nlpunbounce.NavigationDrawerFragment.java
com.ryansteckler.nlpunbounce.ServiceDetailFragment.java
com.ryansteckler.nlpunbounce.ServicesFragment.java
com.ryansteckler.nlpunbounce.SettingsActivity.java
com.ryansteckler.nlpunbounce.WakelockDetailFragment.java
com.ryansteckler.nlpunbounce.WakelocksFragment.java
com.ryansteckler.nlpunbounce.XposedReceiver.java
com.ryansteckler.nlpunbounce.adapters.AlarmsAdapter.java
com.ryansteckler.nlpunbounce.adapters.BaseAdapter.java
com.ryansteckler.nlpunbounce.adapters.ServicesAdapter.java
com.ryansteckler.nlpunbounce.adapters.WakelocksAdapter.java
com.ryansteckler.nlpunbounce.helpers.DownloadHelper.java
com.ryansteckler.nlpunbounce.helpers.LocaleHelper.java
com.ryansteckler.nlpunbounce.helpers.LogHelper.java
com.ryansteckler.nlpunbounce.helpers.NetworkHelper.java
com.ryansteckler.nlpunbounce.helpers.RootHelper.java
com.ryansteckler.nlpunbounce.helpers.SettingsHelper.java
com.ryansteckler.nlpunbounce.helpers.SortWakeLocks.java
com.ryansteckler.nlpunbounce.helpers.ThemeHelper.java
com.ryansteckler.nlpunbounce.helpers.UidNameResolver.java
com.ryansteckler.nlpunbounce.hooks.Wakelocks.java
com.ryansteckler.nlpunbounce.models.AlarmStats.java
com.ryansteckler.nlpunbounce.models.BaseStatsWrapper.java
com.ryansteckler.nlpunbounce.models.BaseStats.java
com.ryansteckler.nlpunbounce.models.EventLookup.java
com.ryansteckler.nlpunbounce.models.InterimEvent.java
com.ryansteckler.nlpunbounce.models.ServiceStats.java
com.ryansteckler.nlpunbounce.models.UnbounceStatsCollection.java
com.ryansteckler.nlpunbounce.models.WakelockStats.java
com.ryansteckler.nlpunbounce.tasker.TaskerActivity.java
com.ryansteckler.nlpunbounce.tasker.TaskerReceiver.java
com.ryansteckler.nlpunbounce.tasker.TaskerWhichFragment.java