Example usage for android.content.res Configuration Configuration

List of usage examples for android.content.res Configuration Configuration

Introduction

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

Prototype

private Configuration(Parcel source) 

Source Link

Document

Construct this Configuration object, reading from the Parcel.

Usage

From source file:Main.java

static private void resetLocale(Resources res) {
    Configuration conf = new Configuration(res.getConfiguration());
    conf.locale = mCurLocale;/*from w w w . ja  v a2 s  .co m*/
    new Resources(res.getAssets(), res.getDisplayMetrics(), conf);
}

From source file:Main.java

private static void updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);/*from  www  . ja  v a 2 s.com*/

    Resources resources = context.getResources();

    Configuration configuration = new Configuration(resources.getConfiguration());

    configuration.locale = locale;

    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}

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   ww w  .ja v a  2 s  . co 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:com.pranavpandey.smallapp.SmallApp.java

@Override
protected void onCreate() {
    super.onCreate();

    mContext = getApplicationContext();/*from   ww  w  .j ava2  s.c  o m*/
    SmallTheme.initializeInstance(getContext());
    mConfig = new Configuration(getResources().getConfiguration());

    // Request runtime permissions if available.
    if (SmallUtils.isMarshmallow()) {
        final ArrayList<String> permissionsToGrant = new ArrayList<String>();
        if (getPermissions() != null) {
            for (int i = 0; i < getPermissions().length; i++) {
                if (ContextCompat.checkSelfPermission(mContext,
                        getPermissions()[i]) != PackageManager.PERMISSION_GRANTED) {
                    permissionsToGrant.add(getPermissions()[i]);
                }
            }
        }

        if (!permissionsToGrant.isEmpty()) {
            Intent intent = new Intent(mContext, PermissionDangerous.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(PermissionDangerous.PERMISSIONS,
                    permissionsToGrant.toArray(new String[permissionsToGrant.size()]));
            openDelayedActivity(intent);
        } else if (writeSystemSettings() && !Settings.System.canWrite(getContext())) {
            Intent intent = new Intent(mContext, PermissionWriteSystemSettings.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            openDelayedActivity(intent);
        }
    }

    setContentView(R.layout.sas_main);
    mRootView = (ViewGroup) findViewById(R.id.frame_container);

    if (getLayoutId() != 0) {
        View layoutView = LayoutInflater.from(SmallApp.this).inflate(getLayoutId(), null);
        mRootView.addView(layoutView);
    }
}

From source file:br.com.frs.foodrestrictions.FoodMessages.java

@Override
public void onItemSelected(AdapterView<?> adapterView, View v, int i, long l) {

    /* //TODO - Find a better way of doing it
     * I don't believe that this is the best approach to handle this problem but it is
     * the only way I found to do it so far. Do you have any better idea?
     * please help me here :D//w  w  w. j  a va2  s .c om
     */

    /* Getting the current resource  and config info */
    Resources rsc = v.getContext().getResources();
    Configuration config = new Configuration(rsc.getConfiguration());
    /* Saving the original locale before changing to the new one
     * just to show the texts
     */
    Locale orgLocale = config.locale;

    /* Changing the language to the one the user have selected based on the
     * Languages.xml file
     */
    switch (i) {
    /* English */
    case 0:
        config.locale = new Locale("en");
        break;
    /* Portuguese */
    case 1:
        config.locale = new Locale("pt");
        break;
    }

    /* Setting the new locale */
    rsc.updateConfiguration(config, rsc.getDisplayMetrics());
    refreshMessages(v);

    /* Updating the layout with the new selected language */

    /* Return to last locale to keep the app as it was before */
    config.locale = orgLocale;
    rsc.updateConfiguration(config, rsc.getDisplayMetrics());
}

From source file:com.pranavpandey.smallapp.SmallApp.java

@Override
protected boolean onSmallAppConfigurationChanged(Configuration newConfig) {
    int diff = newConfig.diff(mConfig);
    mConfig = new Configuration(getResources().getConfiguration());

    if ((diff & ActivityInfo.CONFIG_ORIENTATION | ActivityInfo.CONFIG_FONT_SCALE
            | ActivityInfo.CONFIG_SCREEN_SIZE | ActivityInfo.CONFIG_KEYBOARD) != 0) {
        return true;
    }// www.  j a  v a  2 s. c  o m
    return super.onSmallAppConfigurationChanged(newConfig);
}

From source file:com.jarklee.materialdatetimepicker.Utils.java

public static String getStringFromLocale(@NonNull Context context, @StringRes int strRes, Locale locale) {
    if (locale == null) {
        return context.getString(strRes);
    }//from   www .j  a v  a  2  s.  c om
    Resources standardResources = context.getResources();
    AssetManager assets = standardResources.getAssets();
    DisplayMetrics metrics = standardResources.getDisplayMetrics();
    Configuration config = new Configuration(standardResources.getConfiguration());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLocale(locale);
    } else {
        config.locale = locale;
    }
    Resources defaultResources = new Resources(assets, metrics, config);
    try {
        return defaultResources.getString(strRes);
    } catch (Exception e) {
        return context.getString(strRes);
    }
}

From source file:com.appsimobile.appsii.Appsi.java

@Override
public void onCreate() {
    Log.d(TAG, "onCreate() called with: " + "");
    super.onCreate();

    Log.d(TAG, "onCreate() create shared prefs");
    mPrefs = AppInjector.provideSharedPreferences();

    Context context = ThemingUtils.createContextThemeWrapper(this, mPrefs);

    Log.d(TAG, "onCreate() initializeDagger");
    initializeDagger(context);//  www .java  2s  .  c  o m

    mHotspotHelper.setCallback(this);

    Log.d(TAG, "onCreate() configure popup layer");
    mPopupLayer.setPopuplayerListener(this);

    mNotificationLikeOpener = new NotificationLikeOpener(this);
    Log.d(TAG, "onCreate() create configuration");
    mLastConfiguration = new Configuration(getResources().getConfiguration());

    Log.d(TAG, "onCreate() configure sidebar");
    mSidebar.setOnCancelCloseListener(this);
    mSidebar.setSidebarListener(this);

    Log.d(TAG, "onCreate() preferences");
    mSidebarPercentage = mPreferenceHelper.getSidebarWidth();

    int value = mPreferenceHelper.getSidebarDimLevel();
    updateDefaultDimColor(ThemingUtils.getPercentage(value));

    mPrefs.registerOnSharedPreferenceChangeListener(this);

    Log.d(TAG, "onCreate() create External events receiver");
    mExternalEventsReceiver = new ExternalEventsListener();
    mExternalEventsReceiver.register(this);

    Log.d(TAG, "onCreate() create Runnig receiver");
    mIsRunningBroadcastReceiver = new AppsiStatusBroadcastReceiver();
    mIsRunningBroadcastReceiver.register(this);

    Log.d(TAG, "onCreate() create status receiver");
    mAppStatusReceiver = AppStatusReceiver.register(this);

    Log.d(TAG, "onCreate() create local receiver");
    mLocalActionsReceiver = new LocalReceiver();
    mLocalActionsReceiver.register(this);

    Log.d(TAG, "onCreate() Handler");
    mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == MESSAGE_CLOSE_SIDEBAR) {
                onCloseSidebar();
            }
            return false;
        }
    });

    // listen for changes in the hotspots
    Log.d(TAG, "onCreate() registering contentObserver");
    getContentResolver().registerContentObserver(HomeContract.Hotspots.CONTENT_URI, true,
            mHotspotsContentObserver);

    // Now that appsi has been initialized, scan the hotspot config
    Log.d(TAG, "onCreate() Load hotspots");
    reloadHotspots();
    mCreated = true;
    Log.d(TAG, "onCreate() completed onCreate");
}

From source file:com.appsimobile.appsii.Appsi.java

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    int diff = mLastConfiguration.diff(newConfig);

    // handle layout-direction change

    boolean directionChanged = (diff
            & ActivityInfo.CONFIG_LAYOUT_DIRECTION) == ActivityInfo.CONFIG_LAYOUT_DIRECTION;
    boolean localeChanged = (diff & ActivityInfo.CONFIG_LOCALE) == ActivityInfo.CONFIG_LOCALE;
    boolean orientationChanged = newConfig.orientation != mLastConfiguration.orientation;

    mLastConfiguration = new Configuration(newConfig);

    if (directionChanged || localeChanged) {
        Log.i("Appsi", "restarting because of direction or locale change");
        restartAppsiService();/*  w  w  w  .j  a  v  a  2 s.  c o  m*/
        return;
    }

    if (orientationChanged) {
        mHotspotHelper.onOrientationChanged();
    }
}

From source file:im.vector.VectorApp.java

/**
 * Init the application locale from the saved one
 *//*from w w  w.j av  a2 s.  com*/
private static void initApplicationLocale() {
    Context context = VectorApp.getInstance();
    Locale locale = getApplicationLocale();
    float fontScale = getFontScaleValue();
    String theme = ThemeUtils.getApplicationTheme(context);

    Locale.setDefault(locale);
    Configuration config = new Configuration(context.getResources().getConfiguration());
    config.locale = locale;
    config.fontScale = fontScale;
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());

    // init the theme
    ThemeUtils.setApplicationTheme(context, theme);

    // init the known locales in background
    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            getApplicationLocales(VectorApp.getInstance());
            return null;
        }
    };

    // should never crash
    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}