Search in sources :

Example 1 with BookJson

use of com.baker.abaker.model.BookJson in project baker-android by bakerframework.

the class GindActivity method postExecute.

/**
 * This will manage all the task post execute actions
 *
 * @param taskId the id of the task that concluded its work
 */
public void postExecute(final int taskId, String... results) {
    switch(taskId) {
        // The download of the shelf file has concluded
        case DOWNLOAD_SHELF_FILE:
            Log.d(this.getClass().getName(), "Shelf downloader finished.");
            shelfDownloader = null;
            // Get the results of the download
            String taskStatus = results[0];
            String filePath = results[1];
            if (taskStatus.equals("SUCCESS")) {
                // After the download is successful, we create a backup for the shelf.
                this.backupCachedShelf();
                this.readShelf(filePath);
            } else if ("DIRECTORY_NOT_FOUND".equals(taskStatus) && "".equals(filePath)) {
                Toast.makeText(this, this.getString(R.string.could_not_save_shelf), Toast.LENGTH_LONG).show();
                finish();
            } else {
                Log.d(this.getClass().toString(), "The shelf download failed, we will try to use a backup file.");
                this.useBackupShelf();
            }
            break;
        case REGISTRATION_TASK:
            if (results[0].equals("SUCCESS")) {
                this.registrationId = results[1];
                this.storeRegistrationId(this.getApplicationContext(), results[1]);
            } else {
                // Could not create registration ID for GCM services.
                Log.d(this.getClass().toString(), "Could not create registration ID for GCM services");
            }
            break;
        case CHECK_INTERNET_TASK:
            Log.d(this.getClass().toString(), "FINISHED TESTING INTERNET CONNECTION, CHECKING SHELF...");
            this.downloadShelf(results[0]);
            break;
        case BOOK_JSON_PARSE_TASK:
            try {
                BookJson bookJson = new BookJson();
                final String magazineName = results[0];
                final String rawJson = results[1];
                bookJson.setMagazineName(magazineName);
                bookJson.fromJson(rawJson);
                this.viewMagazine(bookJson);
            } catch (JSONException ex) {
                Log.e(this.getClass().getName(), "Error parsing the book.json", ex);
            } catch (ParseException ex) {
                Log.e(this.getClass().getName(), "Error parsing the book.json", ex);
            }
            break;
        case UNZIP_CUSTOM_STANDALONE:
            this.saveExtractionFinished(true);
            this.getValidIssuesFromSharedStorage();
            break;
    }
}
Also used : BookJson(com.baker.abaker.model.BookJson) JSONException(org.json.JSONException) ParseException(java.text.ParseException)

Example 2 with BookJson

use of com.baker.abaker.model.BookJson in project baker-android by bakerframework.

the class GindActivity method showAppUsage.

public void showAppUsage() {
    BookJson book = new BookJson();
    book.setMagazineName(this.getString(R.string.ut_directory));
    List<String> contents = new ArrayList<String>();
    String[] pages = this.getString(R.string.ut_pages).split(">");
    for (String page : pages) {
        page = page.trim();
        contents.add(page);
    }
    book.setContents(contents);
    book.setOrientation("portrait");
    Intent intent = new Intent(this, MagazineActivity.class);
    try {
        intent.putExtra(Configuration.BOOK_JSON_KEY, book.toJSON().toString());
        intent.putExtra(Configuration.MAGAZINE_NAME, book.getMagazineName());
        // intent.putExtra(Configuration.MAGAZINE_STANDALONE, true);
        intent.putExtra(Configuration.MAGAZINE_RETURN_TO_SHELF, true);
        intent.putExtra(Configuration.MAGAZINE_ENABLE_DOUBLE_TAP, false);
        intent.putExtra(Configuration.MAGAZINE_ENABLE_BACK_NEXT_BUTTONS, true);
        intent.putExtra(Configuration.MAGAZINE_ENABLE_TUTORIAL, true);
        startActivityForResult(intent, STANDALONE_MAGAZINE_ACTIVITY_FINISH);
    } catch (JSONException e) {
    // Nothing
    }
}
Also used : BookJson(com.baker.abaker.model.BookJson) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent)

Example 3 with BookJson

use of com.baker.abaker.model.BookJson in project baker-android by bakerframework.

the class MagazineThumb method postExecute.

/**
 * As an instance of GindMandator, this class should implement a method postExecute
 * to be executed when one of the tasks is done.
 *
 * @param taskId  the id of the task that ended.
 * @param results the results values.
 */
@Override
public void postExecute(final int taskId, String... results) {
    // Hide progress
    findViewById(R.id.txtProgress).setVisibility(View.GONE);
    findViewById(R.id.progress_ui).setVisibility(View.GONE);
    // TODO: Handle failures.
    switch(taskId) {
        case MAGAZINE_DOWNLOAD_TASK:
            this.packDownloader = null;
            // When the download task ended successfully we will start unzipping the file.
            Log.d(this.getClass().getName(), "DownloaderTask result: " + results[0]);
            if (results[0].equals("SUCCESS")) {
                // Here we register the DOWNLOAD ISSUE event on Google Analytics
                if (this.activity.getResources().getBoolean(R.bool.ga_enable) && this.activity.getResources().getBoolean(R.bool.ga_register_issue_download_event)) {
                    ((ABakerApp) this.activity.getApplication()).sendEvent(this.activity.getString(R.string.issues_category), this.activity.getString(R.string.issue_download), this.magazine.getName());
                }
                startUnzip(results[1], this.magazine.getName());
            } else if (results[0].equals("ERROR")) {
                this.resetUI();
            }
            break;
        case THUMB_DOWNLOAD_TASK:
            // If the thumbnail download ended successfully we will render the cover.
            if (results[0].equals("SUCCESS")) {
                Log.d(this.getClass().getName(), "Cover download for " + this.magazine.getName() + " finished.");
                this.renderCover();
                thumbnailDownloaderHandler.sendEmptyMessage(1);
            }
            break;
        case UNZIP_MAGAZINE_TASK:
            // If the Unzip tasks ended successfully we will update the UI to let the user
            // start reading the issue.
            this.unzipperTask = null;
            if (results[0].equals("SUCCESS")) {
                this.enableReadArchiveActions();
                this.sendDownloadReport();
            } else {
                this.resetUI();
                Toast.makeText(this.getContext(), "Could not extract the package. Possibly corrupted.", Toast.LENGTH_LONG).show();
            }
            break;
        case MAGAZINE_DELETE_TASK:
            // the user download the issue again.
            if (results[0].equals("SUCCESS")) {
                // Here we register the DELETE ISSUE event on Google Analytics
                if (this.activity.getResources().getBoolean(R.bool.ga_enable) && this.activity.getResources().getBoolean(R.bool.ga_register_issue_delete_event)) {
                    ((ABakerApp) this.activity.getApplication()).sendEvent(this.activity.getString(R.string.issues_category), this.activity.getString(R.string.issue_delete), this.magazine.getName());
                }
                this.enableDownloadAction();
            }
            break;
        case POST_DOWNLOAD_TASK:
            if (!results[0].equals("ERROR")) {
            }
            break;
        case BOOK_JSON_PARSE_TASK:
            try {
                BookJson bookJson = new BookJson();
                final String magazineName = results[0];
                final String rawJson = results[1];
                bookJson.setMagazineName(magazineName);
                bookJson.fromJson(rawJson);
                setBookJson(bookJson);
            } catch (JSONException ex) {
                Log.e(this.getClass().getName(), "Error parsing the book.json", ex);
            } catch (ParseException ex) {
                Log.e(this.getClass().getName(), "Error parsing the book.json", ex);
            }
            break;
        case CHECK_INTERNET_TASK:
            if (results[0].equals("TRUE")) {
                if (TASK == TASKS_AFTER_CHECK_INTERNET.DOWNLOAD_ISSUE) {
                    this.startPackageDownload();
                } else {
                    this.readOnline();
                }
            } else {
                if (TASK == TASKS_AFTER_CHECK_INTERNET.DOWNLOAD_ISSUE) {
                    Log.e(this.getClass().toString(), "No Internet access to download the issue.");
                    Toast.makeText(this.getContext(), this.getContext().getString(R.string.cannot_download_issue), Toast.LENGTH_LONG).show();
                } else {
                    Log.e(this.getClass().toString(), "Cannot view issue online. No internet access.");
                    Toast.makeText(this.getContext(), this.getContext().getString(R.string.cannot_view_online), Toast.LENGTH_LONG).show();
                }
            }
            break;
    }
}
Also used : BookJson(com.baker.abaker.model.BookJson) JSONException(org.json.JSONException) ParseException(java.text.ParseException) ABakerApp(com.baker.abaker.ABakerApp)

Example 4 with BookJson

use of com.baker.abaker.model.BookJson in project baker-android by bakerframework.

the class BookJsonParserTask method doInBackground.

@Override
protected BookJson doInBackground(String... params) {
    BookJson result = null;
    String rawJson = "";
    boolean online = false;
    try {
        if ("ONLINE".equals(params[0])) {
            online = true;
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String separator = "/";
            String url = this.magazine.getLiveUrl() + separator + this.context.getString(R.string.book);
            Log.d(this.getClass().toString(), "Will parse the BookJson from the Live URL: " + url);
            HttpGet httpGet = new HttpGet(url);
            HttpResponse response = httpClient.execute(httpGet);
            if (null != response) {
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200) {
                    rawJson = EntityUtils.toString(response.getEntity());
                    Log.d(this.getClass().toString(), "Get request for book.json succeeded: " + rawJson);
                } else {
                    Log.e(this.getClass().toString(), "Bad response when obtaining the book.json: " + statusCode);
                }
            } else {
                Log.e(this.getClass().toString(), "The response is NULL when obtaining the book.json.");
            }
        } else if ("STANDALONE".equals(params[0])) {
            AssetManager assetManager = this.context.getAssets();
            String books;
            String bookJsonPath = this.magazine.getName().concat(File.separator).concat("book.json");
            BufferedReader reader;
            if (this.isFromAssets()) {
                Log.d(this.getClass().getName(), "Will parse the BookJson from the assets directory.");
                books = this.context.getString(R.string.sa_books_directory);
                bookJsonPath = books.concat(File.separator).concat(bookJsonPath);
                reader = new BufferedReader(new InputStreamReader(assetManager.open(bookJsonPath)));
            } else {
                Log.d(this.getClass().getName(), "Will parse the BookJson from the magazines directory.");
                books = Configuration.getMagazinesDirectory(this.context);
                bookJsonPath = books.concat(File.separator).concat(bookJsonPath);
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(bookJsonPath)));
            }
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            rawJson = sb.toString();
            Log.d(this.getClass().toString(), "Book.json read from file: " + rawJson);
            reader.close();
        } else {
            Log.d(this.getClass().getName(), "Will parse the BookJson from the file system.");
            String workingDir = this.magazinesDirectory + File.separator;
            File book = new File(workingDir + params[0] + File.separator + this.context.getString(R.string.book));
            FileInputStream input = new FileInputStream(book);
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            rawJson = sb.toString();
            Log.d(this.getClass().toString(), "Book.json read from file: " + rawJson);
            input.close();
        }
        boolean valid = this.validateJson(rawJson);
        if (valid) {
            Log.d(this.getClass().toString(), "Book.json is valid.");
            result = new BookJson();
            if (online && this.magazine.getLiveUrl() != null) {
                result.setLiveUrl(this.magazine.getLiveUrl());
            } else {
                result.setLiveUrl(null);
            }
            result.fromJson(rawJson);
            result.setMagazineName(this.magazine.getName());
        } else {
            Log.d(this.getClass().toString(), "Book.json is NOT valid.");
        }
    } catch (Exception ex) {
        Log.e(this.getClass().getName(), "Error parsing the book.json.", ex);
        result = null;
    }
    return result;
}
Also used : BookJson(com.baker.abaker.model.BookJson) AssetManager(android.content.res.AssetManager) InputStreamReader(java.io.InputStreamReader) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) FileInputStream(java.io.FileInputStream) JSONException(org.json.JSONException) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 5 with BookJson

use of com.baker.abaker.model.BookJson in project baker-android by bakerframework.

the class MagazineActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // We would like to keep the screen on while reading the magazine
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    // Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    // Remove notification bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.html_pager);
    Intent intent = getIntent();
    try {
        STANDALONE_MODE = intent.getBooleanExtra(Configuration.MAGAZINE_STANDALONE, false);
        RETURN_TO_SHELF = intent.getBooleanExtra(Configuration.MAGAZINE_RETURN_TO_SHELF, true);
        ENABLE_DOUBLE_TAP = intent.getBooleanExtra(Configuration.MAGAZINE_ENABLE_DOUBLE_TAP, true);
        ENABLE_BACK_NEXT_BUTTONS = intent.getBooleanExtra(Configuration.MAGAZINE_ENABLE_BACK_NEXT_BUTTONS, false);
        ENABLE_TUTORIAL = intent.getBooleanExtra(Configuration.MAGAZINE_ENABLE_TUTORIAL, false);
        Log.d(this.getClass().getName(), "Will run in standalone mode: " + STANDALONE_MODE);
        if (!RETURN_TO_SHELF) {
            setResult(GindActivity.STANDALONE_MAGAZINE_ACTIVITY_FINISH);
        } else {
            setResult(0);
        }
        jsonBook = new BookJson();
        jsonBook.setMagazineName(intent.getStringExtra(Configuration.MAGAZINE_NAME));
        Log.d(this.getClass().toString(), "THE RAW BOOK.JSON IS: " + intent.getStringExtra(Configuration.BOOK_JSON_KEY));
        jsonBook.fromJson(intent.getStringExtra(Configuration.BOOK_JSON_KEY));
        this.setOrientation(jsonBook.getOrientation());
        this.setPagerView(jsonBook);
        this.setEnableDoubleTap(ENABLE_DOUBLE_TAP);
        this.setEnableBackNextButton(ENABLE_BACK_NEXT_BUTTONS);
        detectFirstOrLastPage();
        gestureDetector = new GestureDetectorCompat(this, new MyGestureListener());
    } catch (Exception ex) {
        ex.printStackTrace();
        Toast.makeText(this, "Not valid book.json found!", Toast.LENGTH_LONG).show();
    }
    resources = getResources();
}
Also used : BookJson(com.baker.abaker.model.BookJson) Intent(android.content.Intent) GestureDetectorCompat(android.support.v4.view.GestureDetectorCompat) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

BookJson (com.baker.abaker.model.BookJson)5 JSONException (org.json.JSONException)4 Intent (android.content.Intent)2 ParseException (java.text.ParseException)2 PendingIntent (android.app.PendingIntent)1 AssetManager (android.content.res.AssetManager)1 GestureDetectorCompat (android.support.v4.view.GestureDetectorCompat)1 ABakerApp (com.baker.abaker.ABakerApp)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 HttpResponse (org.apache.http.HttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)1