package com.limelight.utils; import android.app.Activity; import android.app.AlertDialog; import android.app.UiModeManager; import android.content.Context; import android.content.DialogInterface; import android.content.res.Configuration; import android.view.View; import com.limelight.R; import com.limelight.preferences.PreferenceConfiguration; import java.util.Locale; public class UiHelper { // Values from https://developer.android.com/training/tv/start/layouts.html private static final int TV_VERTICAL_PADDING_DP = 27; private static final int TV_HORIZONTAL_PADDING_DP = 48; public static void setLocale(Activity activity) { String locale = PreferenceConfiguration.readPreferences(activity).language; if (!locale.equals(PreferenceConfiguration.DEFAULT_LANGUAGE)) { Configuration config = new Configuration(activity.getResources().getConfiguration()); // Some locales include both language and country which must be separated // before calling the Locale constructor. if (locale.contains("-")) { config.locale = new Locale(locale.substring(0, locale.indexOf('-')), locale.substring(locale.indexOf('-') + 1)); } else { config.locale = new Locale(locale); } activity.getResources().updateConfiguration(config, activity.getResources().getDisplayMetrics()); } } public static void notifyNewRootView(Activity activity) { View rootView = activity.findViewById(android.R.id.content); UiModeManager modeMgr = (UiModeManager) activity.getSystemService(Context.UI_MODE_SERVICE); if (modeMgr.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) { // Increase view padding on TVs float scale = activity.getResources().getDisplayMetrics().density; int verticalPaddingPixels = (int) (TV_VERTICAL_PADDING_DP*scale + 0.5f); int horizontalPaddingPixels = (int) (TV_HORIZONTAL_PADDING_DP*scale + 0.5f); rootView.setPadding(horizontalPaddingPixels, verticalPaddingPixels, horizontalPaddingPixels, verticalPaddingPixels); } } public static void displayQuitConfirmationDialog(Activity parent, final Runnable onYes, final Runnable onNo) { DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which){ case DialogInterface.BUTTON_POSITIVE: if (onYes != null) { onYes.run(); } break; case DialogInterface.BUTTON_NEGATIVE: if (onNo != null) { onNo.run(); } break; } } }; AlertDialog.Builder builder = new AlertDialog.Builder(parent); builder.setMessage(parent.getResources().getString(R.string.applist_quit_confirmation)) .setPositiveButton(parent.getResources().getString(R.string.yes), dialogClickListener) .setNegativeButton(parent.getResources().getString(R.string.no), dialogClickListener) .show(); } }