public void updateFavorites() {
   ArrayList<String> titleFav = new ArrayList<>();
   ArrayList<String> imgFav = new ArrayList<>();
   for (int i = 0; i < mTitles.size(); i++) {
     if (prefHelper.checkWhatIfFav(mTitles.size() - i)) {
       titleFav.add(mTitles.get(i));
       if (!fullOffline) {
         imgFav.add(mImgs.get(i));
       }
     }
   }
   adapter = new RVAdapter(titleFav, imgFav);
   SlideInBottomAnimationAdapter slideAdapter = new SlideInBottomAnimationAdapter(adapter);
   slideAdapter.setInterpolator(new DecelerateInterpolator());
   rv.setAdapter(slideAdapter);
 }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.recycler_layout, container, false);
    ButterKnife.bind(this, v);
    setHasOptionsMenu(true);
    prefHelper = ((MainActivity) getActivity()).getPrefHelper();

    instance = this;
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    rv.setLayoutManager(llm);
    rv.setHasFixedSize(false);

    fullOffline = prefHelper.fullOfflineWhatIf();
    new DisplayOverview().execute();

    return v;
  }
Пример #3
0
  @Override
  protected void onHandleIntent(Intent intent) {
    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_notification)
            .setProgress(100, 0, false)
            .setOngoing(true)
            .setContentTitle(getResources().getString(R.string.loading_offline_whatif))
            .setAutoCancel(true);

    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());

    PrefHelper prefHelper = new PrefHelper(getApplicationContext());
    File sdCard = prefHelper.getOfflinePath();
    File dir = new File(sdCard.getAbsolutePath() + OFFLINE_WHATIF_OVERVIEW_PATH);
    OkHttpClient client = new OkHttpClient();
    Document doc;
    if (!dir.exists()) dir.mkdirs();
    // download overview
    if (!BuildConfig.DEBUG) {
      try {
        doc =
            Jsoup.connect("https://what-if.xkcd.com/archive/")
                .userAgent(
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.19 Safari/537.36")
                .get();
        StringBuilder sb = new StringBuilder();
        Elements titles = doc.select("h1");
        prefHelper.setNewestWhatif(titles.size());

        sb.append(titles.first().text());
        titles.remove(0);
        for (Element title : titles) {
          sb.append("&&");
          sb.append(title.text());
        }
        prefHelper.setWhatIfTitles(sb.toString());

        Elements img = doc.select("img.archive-image");
        int count = 1;
        for (Element image : img) {
          String url = image.absUrl("src");
          try {
            Request request = new Request.Builder().url(url).build();
            Response response = client.newCall(request).execute();
            File file = new File(dir, String.valueOf(count) + ".png");
            BufferedSink sink = Okio.buffer(Okio.sink(file));
            sink.writeAll(response.body().source());
            sink.close();
            response.body().close();
          } catch (Exception e) {
            e.printStackTrace();
          }
          int p = (int) (count / ((float) img.size()) * 100);
          mBuilder.setProgress(100, p, false);
          mNotificationManager.notify(1, mBuilder.build());
          count++;
        }
        if (prefHelper.getNewestWhatIf() == 0) prefHelper.setNewestWhatif(count - 1);
      } catch (IOException e) {
        e.printStackTrace();
      }

      // download html
      int size = prefHelper.getNewestWhatIf();
      for (int i = 1; i <= size; i++) {
        try {
          doc = Jsoup.connect("https://what-if.xkcd.com/" + String.valueOf(i)).get();
          dir = new File(sdCard.getAbsolutePath() + OFFLINE_WHATIF_PATH + String.valueOf(i));
          dir.mkdirs();
          File file = new File(dir, String.valueOf(i) + ".html");
          BufferedWriter writer = new BufferedWriter(new FileWriter(file));
          writer.write(doc.outerHtml());
          writer.close();
          // download images
          int count = 1;
          for (Element e : doc.select(".illustration")) {
            try {
              String url = "http://what-if.xkcd.com" + e.attr("src");
              Request request = new Request.Builder().url(url).build();
              Response response = client.newCall(request).execute();
              dir = new File(sdCard.getAbsolutePath() + OFFLINE_WHATIF_PATH + String.valueOf(i));
              if (!dir.exists()) dir.mkdirs();
              file = new File(dir, String.valueOf(count) + ".png");
              BufferedSink sink = Okio.buffer(Okio.sink(file));
              sink.writeAll(response.body().source());
              sink.close();
              response.body().close();
              count++;
            } catch (Exception e2) {
              Log.e("article" + i, e2.getMessage());
            }
          }
          int p = (int) (i / ((float) size) * 100);
          mBuilder.setProgress(100, p, false);
          mBuilder.setContentText(i + "/" + size);
          mNotificationManager.notify(1, mBuilder.build());
        } catch (Exception e) {
          Log.e("article" + i, e.getMessage());
        }
      }
    }
    prefHelper.setSunbeamLoaded();

    Intent restart = new Intent("de.tap.easy_xkcd.ACTION_COMIC");
    restart.putExtra("number", prefHelper.getLastComic());
    PendingIntent pendingIntent =
        PendingIntent.getActivity(this, 1, restart, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder
        .setContentIntent(pendingIntent)
        .setContentText(getResources().getString(R.string.not_restart));
    mNotificationManager.notify(1, mBuilder.build());
  }