public void onCreate(final Bundle savedInstanceState) {

    PrefsUtility.applyTheme(this);

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    super.onCreate(savedInstanceState);

    final Intent intent = getIntent();

    final String html = intent.getStringExtra("html");
    final String title = intent.getStringExtra("title");
    setTitle(title);

    if (html == null) {
      BugReportActivity.handleGlobalError(this, "No HTML");
    }

    webView = WebViewFragment.newInstanceHtml(html);

    setContentView(View.inflate(this, R.layout.main_single, null));

    getSupportFragmentManager().beginTransaction().add(R.id.main_single_frame, webView).commit();
  }
Example #2
0
  public synchronized void pruneCache() {

    try {

      final HashSet<Long> currentFiles = new HashSet<Long>(128);

      final File externalCacheDir = context.getExternalCacheDir();
      final File internalCacheDir = context.getCacheDir();

      if (externalCacheDir != null) {
        getCacheFileList(externalCacheDir, currentFiles);
      }

      if (internalCacheDir != null) {
        getCacheFileList(internalCacheDir, currentFiles);
      }

      final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
      final HashMap<Integer, Long> maxAge = PrefsUtility.pref_cache_maxage(context, prefs);

      final LinkedList<Long> filesToDelete = dbManager.getFilesToPrune(currentFiles, maxAge, 72);
      for (final long id : filesToDelete) {
        fileDeletionQueue.enqueue(id);
      }

    } catch (Throwable t) {
      BugReportActivity.handleGlobalError(context, t);
    }
  }
  public void onCreate(final Bundle savedInstanceState) {

    PrefsUtility.applyTheme(this);

    super.onCreate(savedInstanceState);

    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    OptionsMenuUtility.fixActionBar(this, getString(R.string.app_name));

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    final boolean solidblack =
        PrefsUtility.appearance_solidblack(this, sharedPreferences)
            && PrefsUtility.appearance_theme(this, sharedPreferences)
                == PrefsUtility.AppearanceTheme.NIGHT;

    // TODO load from savedInstanceState

    final View layout = getLayoutInflater().inflate(R.layout.main_single, null);
    if (solidblack) layout.setBackgroundColor(Color.BLACK);
    setContentView(layout);
    mPane = (FrameLayout) layout.findViewById(R.id.main_single_frame);

    RedditAccountManager.getInstance(this).addUpdateListener(this);

    if (getIntent() != null) {

      final Intent intent = getIntent();

      final ArrayList<String> urls = intent.getStringArrayListExtra("urls");

      for (final String url : urls) {
        final RedditURLParser.RedditURL redditURL =
            RedditURLParser.parseProbableCommentListing(Uri.parse(url));
        if (redditURL != null) {
          mUrls.add(redditURL);
        }
      }

      doRefresh(RefreshableFragment.COMMENTS, false);

    } else {
      throw new RuntimeException("Nothing to show! (should load from bundle)"); // TODO
    }
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    PrefsUtility.applyTheme(this);

    if (getActionBar() != null) {
      getActionBar().setHomeButtonEnabled(true);
      getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    final boolean solidblack =
        PrefsUtility.appearance_solidblack(this, sharedPreferences)
            && PrefsUtility.appearance_theme(this, sharedPreferences)
                == PrefsUtility.AppearanceTheme.NIGHT;

    if (solidblack) getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLACK));

    final Intent intent = getIntent();

    mUrl = intent.getDataString();

    if (mUrl == null) {
      finish();
      return;
    }

    final Matcher matchImgur = LinkHandler.imgurAlbumPattern.matcher(mUrl);
    final String albumId;

    if (matchImgur.find()) {
      albumId = matchImgur.group(2);
    } else {
      Log.e("AlbumListingActivity", "URL match failed");
      revertToWeb();
      return;
    }

    Log.i("AlbumListingActivity", "Loading URL " + mUrl + ", album id " + albumId);

    final ProgressBar progressBar =
        new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);

    final LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.addView(progressBar);

    ImgurAPI.getAlbumInfo(
        this,
        albumId,
        Constants.Priority.IMAGE_VIEW,
        0,
        new GetAlbumInfoListener() {

          @Override
          public void onFailure(
              final RequestFailureType type,
              final Throwable t,
              final StatusLine status,
              final String readableMessage) {
            Log.e("AlbumListingActivity", "getAlbumInfo call failed: " + type);

            if (status != null) Log.e("AlbumListingActivity", "status was: " + status.toString());
            if (t != null) Log.e("AlbumListingActivity", "exception was: ", t);

            // It might be a single image, not an album

            ImgurAPI.getImageInfo(
                AlbumListingActivity.this,
                albumId,
                Constants.Priority.IMAGE_VIEW,
                0,
                new GetImageInfoListener() {
                  @Override
                  public void onFailure(
                      final RequestFailureType type,
                      final Throwable t,
                      final StatusLine status,
                      final String readableMessage) {
                    Log.e("AlbumListingActivity", "Image info request also failed: " + type);
                    revertToWeb();
                  }

                  @Override
                  public void onSuccess(final ImgurAPI.ImageInfo info) {
                    Log.i("AlbumListingActivity", "Link was actually an image.");
                    LinkHandler.onLinkClicked(AlbumListingActivity.this, info.urlOriginal);
                    finish();
                  }

                  @Override
                  public void onNotAnImage() {
                    Log.i("AlbumListingActivity", "Not an image either");
                    revertToWeb();
                  }
                });
          }

          @Override
          public void onSuccess(final ImgurAPI.AlbumInfo info) {
            Log.i("AlbumListingActivity", "Got album, " + info.images.size() + " image(s)");

            AndroidApi.UI_THREAD_HANDLER.post(
                new Runnable() {
                  @Override
                  public void run() {

                    if (info.title != null && !info.title.trim().isEmpty()) {
                      OptionsMenuUtility.fixActionBar(
                          AlbumListingActivity.this, "Imgur album: " + info.title);
                    } else {
                      OptionsMenuUtility.fixActionBar(AlbumListingActivity.this, "Imgur album");
                    }

                    layout.removeAllViews();

                    final ListView listView = new ListView(AlbumListingActivity.this);
                    listView.setAdapter(new AlbumAdapter(info));
                    layout.addView(listView);

                    listView.setOnItemClickListener(
                        new AdapterView.OnItemClickListener() {
                          @Override
                          public void onItemClick(
                              final AdapterView<?> parent,
                              final View view,
                              final int position,
                              final long id) {

                            LinkHandler.onLinkClicked(
                                AlbumListingActivity.this, info.images.get(position).urlOriginal);
                          }
                        });
                  }
                });
          }
        });

    setContentView(layout);
  }
  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);
                          }
                        }
                      });
                }
              });
    }
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {

    PrefsUtility.applyTheme(this);
    super.onCreate(savedInstanceState);

    final RRThemeAttributes theme = new RRThemeAttributes(this);

    final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    editor = sharedPreferences.edit();
    final boolean solidblack =
        PrefsUtility.appearance_solidblack(this, sharedPreferences)
            && PrefsUtility.appearance_theme(this, sharedPreferences)
                == PrefsUtility.AppearanceTheme.NIGHT;

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    final String title;

    isModmail = getIntent() != null && getIntent().getBooleanExtra("modmail", false);
    onlyUnread = sharedPreferences.getBoolean("onlyUnread", false);

    if (!isModmail) {
      title = getString(R.string.mainmenu_inbox);
    } else {
      title = getString(R.string.mainmenu_modmail);
    }

    OptionsMenuUtility.fixActionBar(this, title);

    final LinearLayout outer = new LinearLayout(this);
    outer.setOrientation(android.widget.LinearLayout.VERTICAL);

    if (solidblack) {
      outer.setBackgroundColor(Color.BLACK);
    }

    loadingView = new LoadingView(this, getString(R.string.download_waiting), true, true);

    notifications = new LinearLayout(this);
    notifications.setOrientation(android.widget.LinearLayout.VERTICAL);
    notifications.addView(loadingView);

    final ListView lv = new ListView(this);

    lv.setSmoothScrollbarEnabled(false);
    lv.setVerticalFadingEdgeEnabled(false);

    lv.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            final Object item = lv.getAdapter().getItem(position);

            if (item != null && item instanceof RedditRenderableInboxItem) {
              ((RedditRenderableInboxItem) item).handleInboxClick(InboxListingActivity.this);
            }
          }
        });

    adapter = new InboxListingAdapter(this, theme);
    lv.setAdapter(adapter);

    registerForContextMenu(lv);

    outer.addView(notifications);
    outer.addView(lv);

    makeFirstRequest(this);

    setContentView(outer);
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {

    PrefsUtility.applyTheme(this);
    getSupportActionBar().setTitle(R.string.post_captcha_title);

    super.onCreate(savedInstanceState);

    final LoadingView loadingView = new LoadingView(this, R.string.download_waiting, true, true);
    setContentView(loadingView);

    final RedditAccount selectedAccount =
        RedditAccountManager.getInstance(this).getAccount(getIntent().getStringExtra("username"));

    final CacheManager cm = CacheManager.getInstance(this);

    RedditAPI.newCaptcha(
        cm,
        new APIResponseHandler.NewCaptchaResponseHandler(this) {
          @Override
          protected void onSuccess(final String captchaId) {

            final URI captchaUrl = Constants.Reddit.getUri("/captcha/" + captchaId);

            cm.makeRequest(
                new CacheRequest(
                    captchaUrl,
                    RedditAccountManager.getAnon(),
                    null,
                    Constants.Priority.CAPTCHA,
                    0,
                    CacheRequest.DownloadType.FORCE,
                    Constants.FileType.CAPTCHA,
                    false,
                    false,
                    true,
                    CaptchaActivity.this) {
                  @Override
                  protected void onCallbackException(Throwable t) {
                    BugReportActivity.handleGlobalError(CaptchaActivity.this, t);
                  }

                  @Override
                  protected void onDownloadNecessary() {}

                  @Override
                  protected void onDownloadStarted() {
                    loadingView.setIndeterminate(R.string.download_downloading);
                  }

                  @Override
                  protected void onFailure(
                      RequestFailureType type,
                      Throwable t,
                      StatusLine status,
                      String readableMessage) {
                    final RRError error =
                        General.getGeneralErrorForFailure(
                            CaptchaActivity.this, type, t, status, url.toString());
                    General.showResultDialog(CaptchaActivity.this, error);
                    finish();
                  }

                  @Override
                  protected void onProgress(long bytesRead, long totalBytes) {
                    loadingView.setProgress(
                        R.string.download_downloading,
                        (float) ((double) bytesRead / (double) totalBytes));
                  }

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

                    final Bitmap image;
                    try {
                      image = BitmapFactory.decodeStream(cacheFile.getInputStream());
                    } catch (IOException e) {
                      BugReportActivity.handleGlobalError(CaptchaActivity.this, e);
                      return;
                    }

                    new Handler(Looper.getMainLooper())
                        .post(
                            new Runnable() {
                              public void run() {

                                final LinearLayout ll = new LinearLayout(CaptchaActivity.this);
                                ll.setOrientation(LinearLayout.VERTICAL);

                                final ImageView captchaImg = new ImageView(CaptchaActivity.this);
                                ll.addView(captchaImg);
                                final LinearLayout.LayoutParams layoutParams =
                                    (LinearLayout.LayoutParams) captchaImg.getLayoutParams();
                                layoutParams.setMargins(20, 20, 20, 20);
                                layoutParams.height = General.dpToPixels(context, 100);
                                captchaImg.setScaleType(ImageView.ScaleType.FIT_CENTER);

                                final EditText captchaText = new EditText(CaptchaActivity.this);
                                ll.addView(captchaText);
                                ((LinearLayout.LayoutParams) captchaText.getLayoutParams())
                                    .setMargins(20, 0, 20, 20);
                                captchaText.setInputType(
                                    android.text.InputType.TYPE_CLASS_TEXT
                                        | android.text.InputType
                                            .TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                                        | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);

                                captchaImg.setImageBitmap(image);

                                final Button submitButton = new Button(CaptchaActivity.this);
                                submitButton.setText(R.string.post_captcha_submit_button);
                                ll.addView(submitButton);
                                ((LinearLayout.LayoutParams) submitButton.getLayoutParams())
                                    .setMargins(20, 0, 20, 20);
                                ((LinearLayout.LayoutParams) submitButton.getLayoutParams())
                                        .gravity =
                                    Gravity.RIGHT;
                                ((LinearLayout.LayoutParams) submitButton.getLayoutParams()).width =
                                    LinearLayout.LayoutParams.WRAP_CONTENT;

                                submitButton.setOnClickListener(
                                    new View.OnClickListener() {
                                      public void onClick(View v) {
                                        final Intent result = new Intent();
                                        result.putExtra("captchaId", captchaId);
                                        result.putExtra(
                                            "captchaText", captchaText.getText().toString());
                                        setResult(RESULT_OK, result);
                                        finish();
                                      }
                                    });

                                final ScrollView sv = new ScrollView(CaptchaActivity.this);
                                sv.addView(ll);
                                setContentView(sv);
                              }
                            });
                  }
                });
          }

          @Override
          protected void onCallbackException(Throwable t) {
            BugReportActivity.handleGlobalError(CaptchaActivity.this, t);
          }

          @Override
          protected void onFailure(
              RequestFailureType type, Throwable t, StatusLine status, String readableMessage) {
            final RRError error =
                General.getGeneralErrorForFailure(CaptchaActivity.this, type, t, status, null);
            General.showResultDialog(CaptchaActivity.this, error);
            finish();
          }

          @Override
          protected void onFailure(APIFailureType type) {
            final RRError error = General.getGeneralErrorForFailure(CaptchaActivity.this, type);
            General.showResultDialog(CaptchaActivity.this, error);
            finish();
          }
        },
        selectedAccount,
        this);
  }