Search in sources :

Example 1 with MagazineThumb

use of com.baker.abaker.views.MagazineThumb in project baker-android by bakerframework.

the class GindActivity method createThumbnails.

public void createThumbnails(final JSONArray jsonArray) {
    Log.d(this.getClass().getName(), "Shelf json contains " + jsonArray.length() + " elements.");
    JSONObject json;
    try {
        this.setContentView(R.layout.activity_gind);
        loadHeader();
        loadBackground();
        if (this.getActionBar() != null) {
            this.getActionBar().show();
            // Modify the action bar to use a custom layout to center the title.
            this.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            this.getActionBar().setCustomView(R.layout.custom_actionbar);
        }
        flowLayout = (FlowLayout) findViewById(R.id.thumbsContainer);
        int length = jsonArray.length();
        for (int i = 0; i < length; i++) {
            json = new JSONObject(jsonArray.getString(i));
            Log.i(this.getClass().getName(), "Parsing JSON object " + json);
            LinearLayout inner = new LinearLayout(this);
            inner.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1));
            inner.setGravity(Gravity.CENTER_HORIZONTAL);
            // Building magazine data
            Date date = sdfInput.parse(json.getString("date"));
            String dateString = sdfOutput.format(date);
            int size = 0;
            if (json.has("size"))
                size = json.getInt("size");
            String encoding = "UTF-8";
            Magazine magazine = new Magazine();
            magazine.setName(new String(json.getString("name").getBytes(encoding), encoding));
            magazine.setTitle(new String(json.getString("title").getBytes(encoding), encoding));
            magazine.setInfo(new String(json.getString("info").getBytes(encoding), encoding));
            magazine.setDate(dateString);
            magazine.setSize(size);
            magazine.setCover(new String(json.getString("cover").getBytes(encoding), encoding));
            magazine.setUrl(new String(json.getString("url").getBytes(encoding), encoding));
            magazine.setStandalone(STANDALONE_MODE);
            if (json.has("liveUrl")) {
                String liveUrl = new String(json.getString("liveUrl").getBytes(encoding), encoding);
                liveUrl = liveUrl.replace("/" + this.getString(R.string.book), "");
                while (liveUrl.endsWith("/")) {
                    liveUrl = liveUrl.substring(0, liveUrl.length() - 1);
                }
                magazine.setLiveUrl(liveUrl);
                Log.d(this.getClass().toString(), "The liveUrl for the magazine " + magazine.getName() + " will be " + liveUrl);
            }
            // Starting the ThumbLayout
            MagazineThumb thumb = new MagazineThumb(this, magazine, thumbnailDownloaderHandler);
            thumb.setId(i + i);
            thumb.init(this, null);
            thumbnailIds.add(thumb.getId());
            if (this.magazineExists(magazine.getName())) {
                thumb.enableReadArchiveActions();
            } else if (STANDALONE_MODE) {
                thumb.enableReadButton();
            }
            // Add layout
            flowLayout.addView(thumb);
        }
        // Start downloading the thumbnails.
        this.downloadNextThumbnail();
        isLoading = false;
    } catch (Exception e) {
        Log.e(this.getClass().getName(), "Error loading the shelf elements.", e);
        //TODO: Notify the user about the issue.
        e.printStackTrace();
    }
}
Also used : MagazineThumb(com.baker.abaker.views.MagazineThumb) JSONObject(org.json.JSONObject) LinearLayout(android.widget.LinearLayout) Date(java.util.Date) Magazine(com.baker.abaker.model.Magazine) JSONException(org.json.JSONException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 2 with MagazineThumb

use of com.baker.abaker.views.MagazineThumb in project baker-android by bakerframework.

the class GindActivity method startDownloadByName.

private void startDownloadByName(final String name) {
    for (int i = 0; i < flowLayout.getChildCount(); i++) {
        MagazineThumb thumb = (MagazineThumb) flowLayout.getChildAt(i);
        if (thumb.getMagazine().getName().equals(name)) {
            Log.d("Automatically starting download of ", thumb.getMagazine().getName());
            thumb.startPackageDownload();
        }
    }
}
Also used : MagazineThumb(com.baker.abaker.views.MagazineThumb)

Example 3 with MagazineThumb

use of com.baker.abaker.views.MagazineThumb in project baker-android by bakerframework.

the class GindActivity method onBackPressed.

@Override
public void onBackPressed() {
    if (!this.isLoading) {
        boolean downloading = false;
        final ArrayList<Integer> downloadingThumbs = new ArrayList<Integer>();
        for (int i = 0; i < flowLayout.getChildCount(); i++) {
            MagazineThumb thumb = (MagazineThumb) flowLayout.getChildAt(i);
            if (thumb.isDownloading()) {
                downloadingThumbs.add(i);
                downloading = true;
            }
        }
        if (downloading) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(this.getString(R.string.exit)).setMessage(this.getString(R.string.closing_app)).setPositiveButton(this.getString(R.string.yes), new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    GindActivity.this.terminateDownloads(downloadingThumbs);
                    GindActivity.super.onBackPressed();
                }
            }).setNegativeButton(this.getString(R.string.no), null).show();
        } else {
            super.onBackPressed();
        }
    } else {
        super.onBackPressed();
    }
}
Also used : AlertDialog(android.app.AlertDialog) MagazineThumb(com.baker.abaker.views.MagazineThumb) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList)

Example 4 with MagazineThumb

use of com.baker.abaker.views.MagazineThumb in project baker-android by bakerframework.

the class GindActivity method startDownloadLastContent.

private void startDownloadLastContent(final JSONArray jsonArray) {
    try {
        ArrayList<Date> list = new ArrayList<Date>();
        JSONObject jsonObject;
        Date date;
        for (int i = 0; i < jsonArray.length(); i++) {
            jsonObject = new JSONObject(jsonArray.getString(i));
            String rawDate = jsonObject.getString("date");
            date = sdfInput.parse(rawDate);
            list.add(date);
        }
        Collections.sort(list, new Comparator<Date>() {

            @Override
            public int compare(Date s, Date s2) {
                return s2.compareTo(s);
            }
        });
        for (int i = 0; i < flowLayout.getChildCount(); i++) {
            MagazineThumb thumb = (MagazineThumb) flowLayout.getChildAt(i);
            String dateString = sdfOutput.format(list.get(0));
            if (thumb.getMagazine().getDate().equals(dateString)) {
                if (!this.magazineExists(thumb.getMagazine().getName())) {
                    Log.d(this.getClass().toString(), "Automatically starting download of " + thumb.getMagazine().getName());
                    thumb.startPackageDownload();
                } else {
                    Log.d(this.getClass().toString(), "The magazine with name '" + thumb.getMagazine().getName() + "' already exists.");
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : MagazineThumb(com.baker.abaker.views.MagazineThumb) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) Date(java.util.Date) JSONException(org.json.JSONException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 5 with MagazineThumb

use of com.baker.abaker.views.MagazineThumb in project baker-android by bakerframework.

the class GindActivity method terminateDownloads.

private void terminateDownloads(final ArrayList<Integer> downloadingThumbs) {
    for (Integer id : downloadingThumbs) {
        MagazineThumb thumb = (MagazineThumb) flowLayout.getChildAt(id);
        thumb.getPackDownloader().cancelDownload();
    }
}
Also used : MagazineThumb(com.baker.abaker.views.MagazineThumb)

Aggregations

MagazineThumb (com.baker.abaker.views.MagazineThumb)7 IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 LinearLayout (android.widget.LinearLayout)1 Magazine (com.baker.abaker.model.Magazine)1