Пример #1
0
  @Override
  public void onCreate(final Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    url = General.uriFromString(getArguments().getString("url"));

    if (getArguments().containsKey("current")) {
      current = UUID.fromString(getArguments().getString("current"));
    } else {
      current = null;
    }

    type = SessionChangeListener.SessionChangeType.valueOf(getArguments().getString("type"));
  }
  public void reset(final int listPosition, final ImageInfo info) {

    mImageInfo = info;

    if (info.title == null || info.title.trim().isEmpty()) {
      mTitle.setText("Image " + (listPosition + 1));
    } else {
      mTitle.setText((listPosition + 1) + ". " + info.title.trim());
    }

    String subtitle = "";

    if (info.type != null) {
      subtitle += info.type;
    }

    if (info.width != null && info.height != null) {

      if (!subtitle.isEmpty()) subtitle += ", ";

      subtitle += info.width + "x" + info.height;
    }

    if (info.size != null) {

      if (!subtitle.isEmpty()) subtitle += ", ";

      long size = info.size;

      if (size < 512 * 1024) {
        subtitle += String.format("%.1f kB", (float) size / 1024);
      } else {
        subtitle += String.format("%.1f MB", (float) size / (1024 * 1024));
      }
    }

    if (subtitle.isEmpty()) {
      mSubtitle.setVisibility(GONE);
    } else {
      mSubtitle.setVisibility(VISIBLE);
    }

    mSubtitle.setText(subtitle);

    mThumbnail.setImageBitmap(null);

    final boolean isConnectionWifi = General.isConnectionWifi(getContext());

    final PrefsUtility.AppearanceThumbnailsShow thumbnailsPref =
        PrefsUtility.appearance_thumbnails_show(
            getContext(), PreferenceManager.getDefaultSharedPreferences(getContext()));

    final boolean downloadThumbnails =
        thumbnailsPref == PrefsUtility.AppearanceThumbnailsShow.ALWAYS
            || (thumbnailsPref == PrefsUtility.AppearanceThumbnailsShow.WIFIONLY
                && isConnectionWifi);

    if (!downloadThumbnails) {
      mThumbnail.setVisibility(GONE);

    } else {

      mThumbnail.setVisibility(VISIBLE);

      CacheManager.getInstance(getContext())
          .makeRequest(
              new CacheRequest(
                  General.uriFromString(info.urlBigSquare),
                  RedditAccountManager.getAnon(),
                  null,
                  Constants.Priority.THUMBNAIL,
                  listPosition,
                  CacheRequest.DownloadType.IF_NECESSARY,
                  Constants.FileType.THUMBNAIL,
                  CacheRequest.DownloadQueueType.IMMEDIATE,
                  false,
                  false,
                  getContext()) {
                @Override
                protected void onCallbackException(final Throwable t) {
                  Log.e("ImgurAlbumListEntryView", "Error in album thumbnail fetch callback", t);
                }

                @Override
                protected void onDownloadNecessary() {}

                @Override
                protected void onDownloadStarted() {}

                @Override
                protected void onFailure(
                    final RequestFailureType type,
                    final Throwable t,
                    final Integer status,
                    final String readableMessage) {
                  Log.e("ImgurAlbumListEntryView", "Failed to fetch thumbnail " + url.toString());
                }

                @Override
                protected void onProgress(
                    final boolean authorizationInProgress,
                    final long bytesRead,
                    final long totalBytes) {}

                @Override
                protected void onSuccess(
                    final CacheManager.ReadableCacheFile cacheFile,
                    final long timestamp,
                    final UUID session,
                    final boolean fromCache,
                    final String mimetype) {

                  if (mImageInfo != info) return;

                  // TODO post message rather than runnable
                  AndroidApi.UI_THREAD_HANDLER.post(
                      new Runnable() {
                        @Override
                        public void run() {
                          try {
                            mThumbnail.setImageURI(cacheFile.getUri());
                          } catch (IOException e) {
                            throw new RuntimeException(e);
                          }
                        }
                      });
                }
              });
    }
  }