Android Open Source - zxing-android-portrait Locale Manager






From Project

Back to project page zxing-android-portrait.

License

The source code is released under:

Apache License

If you think the Android project zxing-android-portrait 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

/*
 * Copyright (C) 2008 ZXing authors/*from  w ww .  j ava 2s  .c  o m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.zxing.client.android;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;

/**
 * Handles any locale-specific logic for the client.
 *
 * @author Sean Owen
 */
public final class LocaleManager {

  private static final String DEFAULT_TLD = "com";
  private static final String DEFAULT_COUNTRY = "US";
  private static final String DEFAULT_LANGUAGE = "en";

  /**
   * Locales (well, countries) where Google web search is available.
   * These should be kept in sync with our translations.
   */
  private static final Map<String,String> GOOGLE_COUNTRY_TLD;
  static {
    GOOGLE_COUNTRY_TLD = new HashMap<>();
    GOOGLE_COUNTRY_TLD.put("AR", "com.ar"); // ARGENTINA
    GOOGLE_COUNTRY_TLD.put("AU", "com.au"); // AUSTRALIA
    GOOGLE_COUNTRY_TLD.put("BR", "com.br"); // BRAZIL
    GOOGLE_COUNTRY_TLD.put("BG", "bg"); // BULGARIA
    GOOGLE_COUNTRY_TLD.put(Locale.CANADA.getCountry(), "ca");
    GOOGLE_COUNTRY_TLD.put(Locale.CHINA.getCountry(), "cn");
    GOOGLE_COUNTRY_TLD.put("CZ", "cz"); // CZECH REPUBLIC
    GOOGLE_COUNTRY_TLD.put("DK", "dk"); // DENMARK
    GOOGLE_COUNTRY_TLD.put("FI", "fi"); // FINLAND
    GOOGLE_COUNTRY_TLD.put(Locale.FRANCE.getCountry(), "fr");
    GOOGLE_COUNTRY_TLD.put(Locale.GERMANY.getCountry(), "de");
    GOOGLE_COUNTRY_TLD.put("GR", "gr"); // GREECE
    GOOGLE_COUNTRY_TLD.put("HU", "hu"); // HUNGARY
    GOOGLE_COUNTRY_TLD.put("ID", "co.id"); // INDONESIA
    GOOGLE_COUNTRY_TLD.put("IL", "co.il"); // ISRAEL
    GOOGLE_COUNTRY_TLD.put(Locale.ITALY.getCountry(), "it");
    GOOGLE_COUNTRY_TLD.put(Locale.JAPAN.getCountry(), "co.jp");
    GOOGLE_COUNTRY_TLD.put(Locale.KOREA.getCountry(), "co.kr");
    GOOGLE_COUNTRY_TLD.put("NL", "nl"); // NETHERLANDS
    GOOGLE_COUNTRY_TLD.put("PL", "pl"); // POLAND
    GOOGLE_COUNTRY_TLD.put("PT", "pt"); // PORTUGAL
    GOOGLE_COUNTRY_TLD.put("RO", "ro"); // ROMANIA    
    GOOGLE_COUNTRY_TLD.put("RU", "ru"); // RUSSIA
    GOOGLE_COUNTRY_TLD.put("SK", "sk"); // SLOVAK REPUBLIC
    GOOGLE_COUNTRY_TLD.put("SI", "si"); // SLOVENIA
    GOOGLE_COUNTRY_TLD.put("ES", "es"); // SPAIN
    GOOGLE_COUNTRY_TLD.put("SE", "se"); // SWEDEN
    GOOGLE_COUNTRY_TLD.put("CH", "ch"); // SWITZERLAND    
    GOOGLE_COUNTRY_TLD.put(Locale.TAIWAN.getCountry(), "tw");
    GOOGLE_COUNTRY_TLD.put("TR", "com.tr"); // TURKEY
    GOOGLE_COUNTRY_TLD.put(Locale.UK.getCountry(), "co.uk");
    GOOGLE_COUNTRY_TLD.put(Locale.US.getCountry(), "com");
  }

  /**
   * Google Product Search for mobile is available in fewer countries than web search. See here:
   * http://support.google.com/merchants/bin/answer.py?hl=en-GB&answer=160619
   */
  private static final Map<String,String> GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD;
  static {
    GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD = new HashMap<>();
    GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put("AU", "com.au"); // AUSTRALIA
    //GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.CHINA.getCountry(), "cn");
    GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.FRANCE.getCountry(), "fr");
    GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.GERMANY.getCountry(), "de");
    GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.ITALY.getCountry(), "it");
    GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.JAPAN.getCountry(), "co.jp");
    GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put("NL", "nl"); // NETHERLANDS
    GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put("ES", "es"); // SPAIN
    GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put("CH", "ch"); // SWITZERLAND
    GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.UK.getCountry(), "co.uk");
    GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.US.getCountry(), "com");
  }

  /**
   * Book search is offered everywhere that web search is available.
   */
  private static final Map<String,String> GOOGLE_BOOK_SEARCH_COUNTRY_TLD = GOOGLE_COUNTRY_TLD;

  private static final Collection<String> TRANSLATED_HELP_ASSET_LANGUAGES =
      Arrays.asList("de", "en", "es", "fr", "it", "ja", "ko", "nl", "pt", "ru", "zh-rCN", "zh-rTW", "zh-rHK");

  private LocaleManager() {}

  /**
   * @param context application's {@link Context}
   * @return country-specific TLD suffix appropriate for the current default locale
   *  (e.g. "co.uk" for the United Kingdom)
   */
  public static String getCountryTLD(Context context) {
    return doGetTLD(GOOGLE_COUNTRY_TLD, context);
  }

  /**
   * The same as above, but specifically for Google Product Search.
   *
   * @param context application's {@link Context}
   * @return The top-level domain to use.
   */
  public static String getProductSearchCountryTLD(Context context) {
    return doGetTLD(GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD, context);
  }

  /**
   * The same as above, but specifically for Google Book Search.
   *
   * @param context application's {@link Context}
   * @return The top-level domain to use.
   */
  public static String getBookSearchCountryTLD(Context context) {
    return doGetTLD(GOOGLE_BOOK_SEARCH_COUNTRY_TLD, context);
  }

  /**
   * Does a given URL point to Google Book Search, regardless of domain.
   *
   * @param url The address to check.
   * @return True if this is a Book Search URL.
   */
  public static boolean isBookSearchUrl(String url) {
    return url.startsWith("http://google.com/books") || url.startsWith("http://books.google.");
  }

  private static String getSystemCountry() {
    Locale locale = Locale.getDefault();
    return locale == null ? DEFAULT_COUNTRY : locale.getCountry();
  }

  private static String getSystemLanguage() {
    Locale locale = Locale.getDefault();
    if (locale == null) {
      return DEFAULT_LANGUAGE;
    }
    String language = locale.getLanguage();
    // Special case Chinese
    if (Locale.SIMPLIFIED_CHINESE.getLanguage().equals(language)) {
      return language + "-r" + getSystemCountry();
    }
    return language;
  }

  public static String getTranslatedAssetLanguage() {
    String language = getSystemLanguage();
    return TRANSLATED_HELP_ASSET_LANGUAGES.contains(language) ? language : DEFAULT_LANGUAGE;
  }

  private static String doGetTLD(Map<String,String> map, Context context) {
    String tld = map.get(getCountry(context));
    return tld == null ? DEFAULT_TLD : tld;
  }

  public static String getCountry(Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String countryOverride = prefs.getString(PreferencesActivity.KEY_SEARCH_COUNTRY, "-");
    if (countryOverride != null && !countryOverride.isEmpty() && !"-".equals(countryOverride)) {
      return countryOverride;
    }
    return getSystemCountry();
  }

}




Java Source Code List

com.google.zxing.client.android.AmbientLightManager.java
com.google.zxing.client.android.BeepManager.java
com.google.zxing.client.android.CaptureActivityHandler.java
com.google.zxing.client.android.CaptureActivity.java
com.google.zxing.client.android.Contents.java
com.google.zxing.client.android.DecodeFormatManager.java
com.google.zxing.client.android.DecodeHandler.java
com.google.zxing.client.android.DecodeHintManager.java
com.google.zxing.client.android.DecodeThread.java
com.google.zxing.client.android.FinishListener.java
com.google.zxing.client.android.HelpActivity.java
com.google.zxing.client.android.HttpHelper.java
com.google.zxing.client.android.InactivityTimer.java
com.google.zxing.client.android.IntentSource.java
com.google.zxing.client.android.Intents.java
com.google.zxing.client.android.LocaleManager.java
com.google.zxing.client.android.PreferencesActivity.java
com.google.zxing.client.android.PreferencesFragment.java
com.google.zxing.client.android.ScanFromWebPageManager.java
com.google.zxing.client.android.ViewfinderResultPointCallback.java
com.google.zxing.client.android.ViewfinderView.java
com.google.zxing.client.android.book.BrowseBookListener.java
com.google.zxing.client.android.book.SearchBookContentsActivity.java
com.google.zxing.client.android.book.SearchBookContentsAdapter.java
com.google.zxing.client.android.book.SearchBookContentsListItem.java
com.google.zxing.client.android.book.SearchBookContentsResult.java
com.google.zxing.client.android.camera.AutoFocusManager.java
com.google.zxing.client.android.camera.CameraConfigurationManager.java
com.google.zxing.client.android.camera.CameraManager.java
com.google.zxing.client.android.camera.FrontLightMode.java
com.google.zxing.client.android.camera.PreviewCallback.java
com.google.zxing.client.android.camera.metering.MeteringInterface.java
com.google.zxing.client.android.camera.open.OpenCameraInterface.java
com.google.zxing.client.android.clipboard.ClipboardInterface.java
com.google.zxing.client.android.encode.ContactEncoder.java
com.google.zxing.client.android.encode.EncodeActivity.java
com.google.zxing.client.android.encode.Formatter.java
com.google.zxing.client.android.encode.MECARDContactEncoder.java
com.google.zxing.client.android.encode.QRCodeEncoder.java
com.google.zxing.client.android.encode.VCardContactEncoder.java
com.google.zxing.client.android.encode.VCardFieldFormatter.java
com.google.zxing.client.android.encode.VCardTelDisplayFormatter.java
com.google.zxing.client.android.history.DBHelper.java
com.google.zxing.client.android.history.HistoryActivity.java
com.google.zxing.client.android.history.HistoryItemAdapter.java
com.google.zxing.client.android.history.HistoryItem.java
com.google.zxing.client.android.history.HistoryManager.java
com.google.zxing.client.android.result.AddressBookResultHandler.java
com.google.zxing.client.android.result.CalendarResultHandler.java
com.google.zxing.client.android.result.EmailAddressResultHandler.java
com.google.zxing.client.android.result.GeoResultHandler.java
com.google.zxing.client.android.result.ISBNResultHandler.java
com.google.zxing.client.android.result.ProductResultHandler.java
com.google.zxing.client.android.result.ResultButtonListener.java
com.google.zxing.client.android.result.ResultHandlerFactory.java
com.google.zxing.client.android.result.ResultHandler.java
com.google.zxing.client.android.result.SMSResultHandler.java
com.google.zxing.client.android.result.TelResultHandler.java
com.google.zxing.client.android.result.TextResultHandler.java
com.google.zxing.client.android.result.URIResultHandler.java
com.google.zxing.client.android.result.WifiResultHandler.java
com.google.zxing.client.android.result.supplement.BookResultInfoRetriever.java
com.google.zxing.client.android.result.supplement.ProductResultInfoRetriever.java
com.google.zxing.client.android.result.supplement.SupplementalInfoRetriever.java
com.google.zxing.client.android.result.supplement.TitleRetriever.java
com.google.zxing.client.android.result.supplement.URIResultInfoRetriever.java
com.google.zxing.client.android.share.AppInfo.java
com.google.zxing.client.android.share.AppPickerActivity.java
com.google.zxing.client.android.share.BookmarkAdapter.java
com.google.zxing.client.android.share.BookmarkPickerActivity.java
com.google.zxing.client.android.share.LoadPackagesAsyncTask.java
com.google.zxing.client.android.share.ShareActivity.java
com.google.zxing.client.android.wifi.NetworkType.java
com.google.zxing.client.android.wifi.WifiConfigManager.java