Java Code Examples for android.os.LocaleList#size()

The following examples show how to use android.os.LocaleList#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RNI18nModule.java    From imsdk-android with MIT License 6 votes vote down vote up
private WritableArray getLocaleList() {
  WritableArray array = Arguments.createArray();

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    LocaleList locales = getReactApplicationContext()
        .getResources().getConfiguration().getLocales();

    for (int i = 0; i < locales.size(); i++) {
      array.pushString(this.toLanguageTag(locales.get(i)));
    }
  } else {
    array.pushString(this.toLanguageTag(getReactApplicationContext()
        .getResources().getConfiguration().locale));
  }

  return array;
}
 
Example 2
Source File: LocaleManager.java    From ShizuruNotes with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = N)
private void setLocaleForApi24(Configuration config, Locale target) {
    Set<Locale> set = new LinkedHashSet<>();
    // bring the target locale to the front of the list
    set.add(target);

    LocaleList all = LocaleList.getDefault();
    for (int i = 0; i < all.size(); i++) {
        // append other locales supported by the user
        set.add(all.get(i));
    }

    Locale[] locales = set.toArray(new Locale[0]);
    config.setLocales(new LocaleList(locales));
}
 
Example 3
Source File: CompatUtils.java    From enterprise-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the primary locale from the specified {@link Configuration}.
 *
 * @param configuration The current {@link Configuration}.
 * @return The primary locale.
 */
public static Locale getPrimaryLocale(Configuration configuration) {
    if (Build.VERSION.SDK_INT >= 24) {
        final LocaleList locales = configuration.getLocales();
        if (locales.size() > 0) {
            return locales.get(0);
        }
    }
    //noinspection deprecation
    return configuration.locale;
}
 
Example 4
Source File: FormatUtils.java    From EnhancedScreenshotNotification with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
private static Locale chooseAvailableLocale(@NonNull LocaleList localeList) {
    for (int i = 0; i < localeList.size(); i++) {
        final Locale locale = localeList.get(i);
        if (sEditActionTextFormats.containsKey(locale)) {
            return locale;
        }
        for (Locale loc : sEditActionTextFormats.keySet()) {
            if (loc.getLanguage().equals(locale.getLanguage())) {
                return loc;
            }
        }
    }
    return Locale.ENGLISH;
}
 
Example 5
Source File: AlphabeticIndexCompat.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
AlphabeticIndexVN(Context context) {
    LocaleList locales = context.getResources().getConfiguration().getLocales();
    int localeCount = locales.size();

    Locale primaryLocale = localeCount == 0 ? Locale.ENGLISH : locales.get(0);
    AlphabeticIndex indexBuilder = new AlphabeticIndex(primaryLocale);
    for (int i = 1; i < localeCount; i++) {
        indexBuilder.addLabels(locales.get(i));
    }
    indexBuilder.addLabels(Locale.ENGLISH);

    mAlphabeticIndex = indexBuilder.buildImmutableIndex();
}
 
Example 6
Source File: LocaleUtils.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Converts LocaleList object to the comma separated BCP 47 compliant string format.
 *
 * @return a well-formed IETF BCP 47 language tag with language and country code that
 *         represents this locale list.
 */
@TargetApi(Build.VERSION_CODES.N)
public static String toLanguageTags(LocaleList localeList) {
    ArrayList<String> newLocaleList = new ArrayList<>();
    for (int i = 0; i < localeList.size(); i++) {
        Locale locale = getUpdatedLocaleForChromium(localeList.get(i));
        newLocaleList.add(toLanguageTag(locale));
    }
    return TextUtils.join(",", newLocaleList);
}
 
Example 7
Source File: SystemLocaleRetriever.java    From LocaleChanger with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.N)
private static List<Locale> mapToListOfLocales(LocaleList localeList) {
    List<Locale> locales = new ArrayList<>();
    for (int i = 0; i < localeList.size(); i++) {
        locales.add(localeList.get(i));
    }
    return locales;
}
 
Example 8
Source File: LocaleManager.java    From LanguageTest with MIT License 5 votes vote down vote up
@RequiresApi(api = N)
private void setLocaleForApi24(Configuration config, Locale target) {
    Set<Locale> set = new LinkedHashSet<>();
    // bring the target locale to the front of the list
    set.add(target);

    LocaleList all = LocaleList.getDefault();
    for (int i = 0; i < all.size(); i++) {
        // append other locales supported by the user
        set.add(all.get(i));
    }

    Locale[] locales = set.toArray(new Locale[0]);
    config.setLocales(new LocaleList(locales));
}
 
Example 9
Source File: LocaleUtils.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Converts LocaleList object to the comma separated BCP 47 compliant string format.
 *
 * @return a well-formed IETF BCP 47 language tag with language and country code that
 *         represents this locale list.
 */
@TargetApi(Build.VERSION_CODES.N)
public static String toLanguageTags(LocaleList localeList) {
    ArrayList<String> newLocaleList = new ArrayList<>();
    for (int i = 0; i < localeList.size(); i++) {
        Locale locale = getUpdatedLocaleForChromium(localeList.get(i));
        newLocaleList.add(toLanguageTag(locale));
    }
    return TextUtils.join(",", newLocaleList);
}
 
Example 10
Source File: DateTimePickerDialog.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private Locale getLocale() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList locales = getConfiguration().getLocales();
        if (locales.size() > 0) {
            return locales.get(0);
        }
    }
    return getConfiguration().locale;
}
 
Example 11
Source File: CompatUtils.java    From android-NfcProvisioning with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the primary locale from the specified {@link Configuration}.
 *
 * @param configuration The current {@link Configuration}.
 * @return The primary locale.
 */
public static Locale getPrimaryLocale(Configuration configuration) {
    if (Build.VERSION.SDK_INT >= 24) {
        final LocaleList locales = configuration.getLocales();
        if (locales.size() > 0) {
            return locales.get(0);
        }
    }
    //noinspection deprecation
    return configuration.locale;
}
 
Example 12
Source File: LocaleManager.java    From Dashchan with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.N)
private ArrayList<Locale> list(Configuration configuration) {
	ArrayList<Locale> arrayList = new ArrayList<>();
	if (C.API_NOUGAT) {
		LocaleList localeList = configuration.getLocales();
		for (int i = 0; i < localeList.size(); i++) {
			arrayList.add(localeList.get(i));
		}
	} else {
		arrayList.add(configuration.locale);
	}
	return arrayList;
}
 
Example 13
Source File: Configuration.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 *
 * @hide
 */
public static String localesToResourceQualifier(LocaleList locs) {
    final StringBuilder sb = new StringBuilder();
    for (int i = 0; i < locs.size(); i++) {
        final Locale loc = locs.get(i);
        final int l = loc.getLanguage().length();
        if (l == 0) {
            continue;
        }
        final int s = loc.getScript().length();
        final int c = loc.getCountry().length();
        final int v = loc.getVariant().length();
        // We ignore locale extensions, since they are not supported by AAPT

        if (sb.length() != 0) {
            sb.append(",");
        }
        if (l == 2 && s == 0 && (c == 0 || c == 2) && v == 0) {
            // Traditional locale format: xx or xx-rYY
            sb.append(loc.getLanguage());
            if (c == 2) {
                sb.append("-r").append(loc.getCountry());
            }
        } else {
            sb.append("b+");
            sb.append(loc.getLanguage());
            if (s != 0) {
                sb.append("+");
                sb.append(loc.getScript());
            }
            if (c != 0) {
                sb.append("+");
                sb.append(loc.getCountry());
            }
            if (v != 0) {
                sb.append("+");
                sb.append(loc.getVariant());
            }
        }
    }
    return sb.toString();
}
 
Example 14
Source File: SpannableUtils.java    From talkback with Apache License 2.0 4 votes vote down vote up
/**
 * Logs the type, position and args of spans which attach to given text, but only if log priority
 * is equal to Log.VERBOSE. Format is {type 'spanned text' extra-data} {type 'other text'
 * extra-data} ..."
 *
 * @param text Text to be logged
 */
public static String spansToStringForLogging(CharSequence text) {
  if (!LogUtils.shouldLog(Log.VERBOSE)) {
    return null;
  }

  if (isEmptyOrNotSpannableStringType(text)) {
    return null;
  }

  Spanned spanned = (Spanned) text;
  ParcelableSpan[] spans = spanned.getSpans(0, text.length(), ParcelableSpan.class);
  if (spans.length == 0) {
    return null;
  }

  StringBuilder stringBuilder = new StringBuilder();
  for (ParcelableSpan span : spans) {
    stringBuilder.append("{");
    // Span type.
    stringBuilder.append(span.getClass().getSimpleName());

    // Span text.
    int start = spanned.getSpanStart(span);
    int end = spanned.getSpanEnd(span);
    if (start < 0 || end < 0 || start == end) {
      stringBuilder.append(" invalid index:[");
      stringBuilder.append(start);
      stringBuilder.append(",");
      stringBuilder.append(end);
      stringBuilder.append("]}");
      continue;
    } else {
      stringBuilder.append(" '");
      stringBuilder.append(spanned, start, end);
      stringBuilder.append("'");
    }

    // Extra data.
    if (span instanceof LocaleSpan) {
      LocaleSpan localeSpan = (LocaleSpan) span;
      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
        Locale locale = localeSpan.getLocale();
        if (locale != null) {
          stringBuilder.append(" locale=");
          stringBuilder.append(locale);
        }
      } else {
        LocaleList localeList = localeSpan.getLocales();
        int size = localeList.size();
        if (size > 0) {
          stringBuilder.append(" locale=[");
          for (int i = 0; i < size - 1; i++) {
            stringBuilder.append(localeList.get(i));
            stringBuilder.append(",");
          }
          stringBuilder.append(localeList.get(size - 1));
          stringBuilder.append("]");
        }
      }

    } else if (span instanceof TtsSpan) {
      TtsSpan ttsSpan = (TtsSpan) span;
      stringBuilder.append(" ttsType=");
      stringBuilder.append(ttsSpan.getType());
      PersistableBundle bundle = ttsSpan.getArgs();
      Set<String> keys = bundle.keySet();
      if (!keys.isEmpty()) {
        for (String key : keys) {
          stringBuilder.append(" ");
          stringBuilder.append(key);
          stringBuilder.append("=");
          stringBuilder.append(bundle.get(key));
        }
      }
    } else if (span instanceof URLSpan) {
      URLSpan urlSpan = (URLSpan) span;
      stringBuilder.append(" url=");
      stringBuilder.append(urlSpan.getURL());
    }
    stringBuilder.append("}");
  }
  return stringBuilder.toString();
}