@Override
 public List<String> defaultLanguageCodes() {
   final TreeSet<String> set = new TreeSet<String>();
   set.add(Locale.getDefault().getLanguage());
   final TelephonyManager manager =
       (TelephonyManager) myApplication.getSystemService(Context.TELEPHONY_SERVICE);
   if (manager != null) {
     final String country0 = manager.getSimCountryIso().toLowerCase();
     final String country1 = manager.getNetworkCountryIso().toLowerCase();
     for (Locale locale : Locale.getAvailableLocales()) {
       final String country = locale.getCountry().toLowerCase();
       if (country != null
           && country.length() > 0
           && (country.equals(country0) || country.equals(country1))) {
         set.add(locale.getLanguage());
       }
     }
     if ("ru".equals(country0) || "ru".equals(country1)) {
       set.add("ru");
     } else if ("by".equals(country0) || "by".equals(country1)) {
       set.add("ru");
     } else if ("ua".equals(country0) || "ua".equals(country1)) {
       set.add("ru");
     }
   }
   set.add("multi");
   return new ArrayList<String>(set);
 }
Exemple #2
0
  public static void initApplication(Application app) {
    LayoutInflater inflater =
        (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (inflater instanceof BootInflater) {
      // already inited
      return;
    }
    systemInflater = inflater;
    Class<?> cCtxImpl = app.getBaseContext().getClass();
    if ("android.app.ContextImpl".equals(cCtxImpl.getName())) {
      try {
        ClassLoader cl = cCtxImpl.getClassLoader();
        Class<?> cStaticFetcher = cl.loadClass("android.app.ContextImpl$StaticServiceFetcher");
        Class<?> cFetcherContainer = null;
        for (int i = 1; i < 50; i++) {
          String cn = "android.app.ContextImpl$" + i;
          try {
            Class<?> c = cl.loadClass(cn);
            if (cStaticFetcher.isAssignableFrom(c)) {
              cFetcherContainer = c;
              break;
            }
          } catch (Exception e) {
          }
        }
        Constructor<?> cFetcherConstructor = cFetcherContainer.getDeclaredConstructor();
        cFetcherConstructor.setAccessible(true);
        Object fetcher = cFetcherConstructor.newInstance();
        Field f = cStaticFetcher.getDeclaredField("mCachedInstance");
        f.setAccessible(true);
        f.set(fetcher, new BootInflater(app));
        f = cCtxImpl.getDeclaredField("SYSTEM_SERVICE_MAP");
        f.setAccessible(true);
        HashMap<String, Object> map = (HashMap<String, Object>) f.get(null);
        map.put(Context.LAYOUT_INFLATER_SERVICE, fetcher);
      } catch (Exception e) {
        throw new RuntimeException("unable to initialize application for BootInflater");
      }
    } else {
      throw new RuntimeException(
          "application base context class " + cCtxImpl.getName() + " is not expected");
    }

    if (!(app.getSystemService(Context.LAYOUT_INFLATER_SERVICE) instanceof BootInflater)) {
      throw new RuntimeException("unable to initialize application for BootInflater");
    }
  }
Exemple #3
0
  /**
   * Fix for https://code.google.com/p/android/issues/detail?id=171190 .
   *
   * <p>When a view that has focus gets detached, we wait for the main thread to be idle and then
   * check if the InputMethodManager is leaking a view. If yes, we tell it that the decor view got
   * focus, which is what happens if you press home and come back from recent apps. This replaces
   * the reference to the detached view with a reference to the decor view.
   *
   * <p>Should be called from {@link Activity#onCreate(android.os.Bundle)} )}.
   */
  @TargetApi(Build.VERSION_CODES.KITKAT)
  public static void fixFocusedViewLeak(Application application) {

    // Don't know about other versions yet.
    if (SDK_INT < 19 || SDK_INT > 23) {
      return;
    }

    final InputMethodManager inputMethodManager =
        (InputMethodManager) application.getSystemService(INPUT_METHOD_SERVICE);

    final Field mServedViewField;
    final Field mHField;
    final Method finishInputLockedMethod;
    final Method focusInMethod;
    try {
      mServedViewField = InputMethodManager.class.getDeclaredField("mServedView");
      mServedViewField.setAccessible(true);
      mHField = InputMethodManager.class.getDeclaredField("mServedView");
      mHField.setAccessible(true);
      finishInputLockedMethod = InputMethodManager.class.getDeclaredMethod("finishInputLocked");
      finishInputLockedMethod.setAccessible(true);
      focusInMethod = InputMethodManager.class.getDeclaredMethod("focusIn", View.class);
      focusInMethod.setAccessible(true);
    } catch (NoSuchMethodException | NoSuchFieldException unexpected) {
      Log.e("IMMLeaks", "Unexpected reflection exception", unexpected);
      return;
    }

    application.registerActivityLifecycleCallbacks(
        new LifecycleCallbacksAdapter() {
          @Override
          public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            ReferenceCleaner cleaner =
                new ReferenceCleaner(
                    inputMethodManager, mHField, mServedViewField, finishInputLockedMethod);
            View rootView = activity.getWindow().getDecorView().getRootView();
            ViewTreeObserver viewTreeObserver = rootView.getViewTreeObserver();
            viewTreeObserver.addOnGlobalFocusChangeListener(cleaner);
          }
        });
  }
Exemple #4
0
  /**
   * Send a status bar notification.
   *
   * <p>The action triggered when the notification is selected is to start the {@link
   * CrashReportDialog} Activity.
   *
   * @param reportFileName Name of the report file to send.
   */
  private void notifySendReport(String reportFileName) {
    // This notification can't be set to AUTO_CANCEL because after a crash,
    // clicking on it restarts the application and this triggers a check
    // for pending reports which issues the notification back.
    // Notification cancellation is done in the dialog activity displayed
    // on notification click.
    final NotificationManager notificationManager =
        (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    final ReportsCrashes conf = ACRA.getConfig();

    // Default notification icon is the warning symbol
    final int icon = conf.resNotifIcon();

    final CharSequence tickerText = mContext.getText(conf.resNotifTickerText());
    final long when = System.currentTimeMillis();
    final Notification notification = new Notification(icon, tickerText, when);

    final CharSequence contentTitle = mContext.getText(conf.resNotifTitle());
    final CharSequence contentText = mContext.getText(conf.resNotifText());

    final Intent notificationIntent = new Intent(mContext, CrashReportDialog.class);
    Log.d(LOG_TAG, "Creating Notification for " + reportFileName);
    notificationIntent.putExtra(ACRAConstants.EXTRA_REPORT_FILE_NAME, reportFileName);
    final PendingIntent contentIntent =
        PendingIntent.getActivity(
            mContext,
            mNotificationCounter++,
            notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setLatestEventInfo(mContext, contentTitle, contentText, contentIntent);

    final Intent deleteIntent = new Intent(mContext, CrashReportDialog.class);
    deleteIntent.putExtra(ACRAConstants.EXTRA_FORCE_CANCEL, true);
    notification.deleteIntent = PendingIntent.getActivity(mContext, -1, deleteIntent, 0);

    // Send new notification
    notificationManager.notify(ACRAConstants.NOTIF_CRASH_ID, notification);
  }
Exemple #5
0
 public static void init(Application app) {
   application = new WeakReference<Application>(app);
   TelephonyManager tm = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
   // 获取本机ID
   deviceId = tm.getDeviceId();
   // 初始化Volley的请求队列.
   requestQueue = Volley.newRequestQueue(app);
   // 是否有相机
   hasCamera = app.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
   APP_BASE_EXTERNAL_STORAGE_PATH =
       EXTERNAL_STORAGE_PATH
           + File.separator
           + application.get().getPackageName()
           + File.separator;
   File base = new File(APP_BASE_EXTERNAL_STORAGE_PATH);
   if (base.exists() && !base.isDirectory()) {
     base.delete();
   }
   if (!base.exists()) {
     base.mkdir();
   }
 }
  /**
   * Constructor
   *
   * @param ctx
   */
  public HttpAccess(Application app, String userAgent) {
    // Using android http client
    http = getHttpClient();
    ((AndroidHttpClient) http).enableCurlLogging("HttpAccess", Log.DEBUG);

    // Set user agent
    this.userAgent = userAgent;

    // Only null in tests
    if (app == null) {
      conMgr = null;
    } else {
      // Keep reference to app
      this.app = app;

      // Registering receiver to handle network changes
      app.registerReceiver(this, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

      // Getting connectivity manager and current network type
      conMgr = (ConnectivityManager) app.getSystemService(Context.CONNECTIVITY_SERVICE);
      currentNetworkSubType = conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getSubtype();
    }
  }
 @Provides
 @Singleton
 ConnectivityManager provideConnectivityManager() {
   return (ConnectivityManager) application.getSystemService(Context.CONNECTIVITY_SERVICE);
 }
 @Provides
 public NotificationManager provideNotificationManager(Application application) {
   return (NotificationManager) application.getSystemService(Context.NOTIFICATION_SERVICE);
 }