Example #1
0
  /**
   * go grab the remote emoticons
   *
   * @param delay - start grabbing after this many milliseconds
   */
  public void fetchEmoticons(
      final int delay, final boolean forceFetch, final DisplayMetrics displayMetrics) {

    _dm = displayMetrics;

    if (!forceFetch) {
      synchronized (this) {
        if (_isRunning) {
          MLog.d(TAG, "RemoteEmoticonMgr.fetchEmoticons()  already issued fetch");
          return;
        }
        _isRunning = true;
      }
    }
    _isReady = false;
    try {
      Thread.sleep(delay);
    } catch (final InterruptedException e) {
    }

    new Thread() {
      public void run() {
        initBlocking();
      }
    }.start();
  }
Example #2
0
  private void initBlocking() {
    try {
      final HashMap<String, String> code2Id = new HashMap<String, String>(20);
      final HashMap<Integer, Drawable> id2File = new HashMap<Integer, Drawable>(20);

      MLog.d(TAG, "RemoteEmoticonMgr.initBlocking()..");
      final JSONArray emoticons = WebService.instance.getEmoticons();
      for (int i = 0; i < emoticons.length(); i++) {
        final JSONObject emo = emoticons.getJSONObject(i);
        final String code = emo.getString("code");
        final String link = emo.getString("link");
        final String descr = emo.optString("descr", "");
        final int id = emo.getInt("id");

        final File localImage =
            new File(GmobApp.DIR_EXTRA_PICS + '/' + StringUtils.getImageFilenameFromURL(link));
        if (descr.contains("remove from cache") && localImage.exists()) {
          localImage.delete();
        }
        /*
         * we chose Globals.DIR_EXTRA_PICS because its cached files
         * last 12 days as opposed to DIR_MEDIA which lasts only 12 hours
         * false param = don't return the drawable object, we just
         * want to know by it finishing that it exists on the file system
         */
        MLog.d(TAG, "RemoteEmoticonMgr.initBlocking() about to grab " + emo.toString());
        if (!localImage.exists()) {
          HttpUtils.saveHttpFile(link, localImage.getPath());
        }

        final BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inDither = false;
        opts.inDensity =
            DisplayMetrics.DENSITY_MEDIUM; // this can be dangerous to assume but watever
        opts.inScaled = true;
        opts.inTargetDensity = _dm.densityDpi;

        final Bitmap b = BitmapFactory.decodeFile(localImage.getPath(), opts);
        // final Bitmap b2 = ImageUtils.scale(b, (int)(b.getWidth() * 1.5f)+10, (int)(b.getHeight()
        // * 1.5f)+10);
        // b.recycle();

        final Drawable d = new BitmapDrawable(b);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        code2Id.put(code, "<img src=\"" + (256 + id) + "\" />");
        id2File.put(256 + id, d);
      }
      final String[] codes = new String[code2Id.size()];
      int j = 0;
      for (final String c : code2Id.keySet()) {
        codes[j++] = c;
      }
      _id2File = id2File;
      _code2Id = code2Id;
      _codes = codes;
      _isReady = true;
      _isRunning = false;
    } catch (final Exception e) {
      MLog.d(TAG, "", e);
      _failedToLoad = true;
      _isRunning = false;
    }
  }