Пример #1
0
 @Override
 public Uri insert(Uri uri, ContentValues values) {
   final SQLiteDatabase db = dbHelper.getWritableDatabase();
   final int match = uriMatcher.match(uri);
   Uri returnUri;
   switch (match) {
     case QUEUE:
       {
         long _id = db.replace(AlexandriaContract.QueueEntry.TABLE_NAME, null, values);
         if (_id > 0) {
           returnUri = AlexandriaContract.QueueEntry.CONTENT_URI;
         } else {
           throw new android.database.SQLException("Failed to insert row into " + uri);
         }
         break;
       }
     case BOOK:
       {
         long _id = db.insert(AlexandriaContract.BookEntry.TABLE_NAME, null, values);
         if (_id > 0) {
           db.delete(
               AlexandriaContract.QueueEntry.TABLE_NAME,
               AlexandriaContract.QueueEntry._ID + " = '" + Long.toString(_id) + "'",
               null);
           returnUri = AlexandriaContract.BookEntry.buildBookUri(_id);
         } else {
           throw new android.database.SQLException("Failed to insert row into " + uri);
         }
         getContext()
             .getContentResolver()
             .notifyChange(AlexandriaContract.BookEntry.buildFullBookUri(_id), null);
         getContext()
             .getContentResolver()
             .notifyChange(AlexandriaContract.BookEntry.CONTENT_URI, null);
         break;
       }
     case AUTHOR:
       {
         long _id = db.insert(AlexandriaContract.AuthorEntry.TABLE_NAME, null, values);
         if (_id > 0)
           returnUri = AlexandriaContract.AuthorEntry.buildAuthorUri(values.getAsLong("_id"));
         else throw new android.database.SQLException("Failed to insert row into " + uri);
         break;
       }
     case CATEGORY:
       {
         long _id = db.insert(AlexandriaContract.CategoryEntry.TABLE_NAME, null, values);
         if (_id > 0)
           returnUri = AlexandriaContract.CategoryEntry.buildCategoryUri(values.getAsLong("_id"));
         else throw new android.database.SQLException("Failed to insert row into " + uri);
         break;
       }
     default:
       throw new UnsupportedOperationException("Unknown uri: " + uri);
   }
   return returnUri;
 }
Пример #2
0
 @Override
 public android.support.v4.content.Loader<Cursor> onCreateLoader(int id, Bundle args) {
   return new CursorLoader(
       getActivity(),
       AlexandriaContract.BookEntry.buildFullBookUri(Long.parseLong(ean)),
       null,
       null,
       null,
       null);
 }
Пример #3
0
  /** Handle action Foo in the provided background thread with the provided parameters. */
  private void deleteBook(String ean) {
    if (!TextUtils.isEmpty(ean)) {
      long bookId = -1;
      try {
        bookId = Long.parseLong(ean);
      } catch (NumberFormatException e) {
        // No valid bookId, abort
        return;
      }

      getContentResolver()
          .delete(AlexandriaContract.BookEntry.buildBookUri(Long.parseLong(ean)), null, null);
    }
  }
Пример #4
0
 @Override
 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
   if (eanEditText.getText().length() == 0) {
     return null;
   }
   String eanStr = eanEditText.getText().toString();
   if (eanStr.length() == 10 && !eanStr.startsWith("978")) {
     eanStr = "978" + eanStr;
   }
   return new CursorLoader(
       getActivity(),
       AlexandriaContract.BookEntry.buildFullBookUri(Long.parseLong(eanStr)),
       null,
       null,
       null,
       null);
 }
Пример #5
0
  /** Handle action fetchBook in the provided background thread with the provided parameters. */
  private void fetchBook(String ean) {
    if (ean == null || ean.length() != 13) {
      return;
    }

    Cursor bookEntry =
        getContentResolver()
            .query(
                AlexandriaContract.BookEntry.buildBookUri(Long.parseLong(ean)),
                null, // leaving "columns" null just returns all the columns.
                null, // cols for "where" clause
                null, // values for "where" clause
                null // sort order
                );

    if (bookEntry.getCount() > 0) {
      bookEntry.close();
      return;
    }

    bookEntry.close();

    HttpURLConnection urlConnection = null;
    BufferedReader reader = null;
    String bookJsonString = null;

    try {
      final String FORECAST_BASE_URL = "https://www.googleapis.com/books/v1/volumes?";
      final String QUERY_PARAM = "q";

      final String ISBN_PARAM = "isbn:" + ean;

      Uri builtUri =
          Uri.parse(FORECAST_BASE_URL)
              .buildUpon()
              .appendQueryParameter(QUERY_PARAM, ISBN_PARAM)
              .build();

      URL url = new URL(builtUri.toString());

      urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setRequestMethod("GET");
      urlConnection.connect();

      InputStream inputStream = urlConnection.getInputStream();
      StringBuffer buffer = new StringBuffer();
      if (inputStream == null) {
        return;
      }

      reader = new BufferedReader(new InputStreamReader(inputStream));
      String line;
      while ((line = reader.readLine()) != null) {
        buffer.append(line);
        buffer.append("\n");
      }

      if (buffer.length() == 0) {
        return;
      }
      bookJsonString = buffer.toString();
    } catch (Exception e) {
      Log.e(LOG_TAG, "Error ", e);
    } finally {
      if (urlConnection != null) {
        urlConnection.disconnect();
      }
      if (reader != null) {
        try {
          reader.close();
        } catch (final IOException e) {
          Log.e(LOG_TAG, "Error closing stream", e);
        }
      }
    }

    final String ITEMS = "items";

    final String VOLUME_INFO = "volumeInfo";

    final String TITLE = "title";
    final String SUBTITLE = "subtitle";
    final String AUTHORS = "authors";
    final String DESC = "description";
    final String CATEGORIES = "categories";
    final String IMG_URL_PATH = "imageLinks";
    final String IMG_URL = "thumbnail";

    try {
      JSONObject bookJson = new JSONObject(bookJsonString);
      JSONArray bookArray;
      if (bookJson.has(ITEMS)) {
        bookArray = bookJson.getJSONArray(ITEMS);
      } else {
        Intent messageIntent = new Intent(MainActivity.MESSAGE_EVENT);
        messageIntent.putExtra(
            MainActivity.MESSAGE_KEY, getResources().getString(R.string.not_found));
        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(messageIntent);
        return;
      }

      JSONObject bookInfo = ((JSONObject) bookArray.get(0)).getJSONObject(VOLUME_INFO);

      String title = bookInfo.getString(TITLE);

      String subtitle = "";
      if (bookInfo.has(SUBTITLE)) {
        subtitle = bookInfo.getString(SUBTITLE);
      }

      String desc = "";
      if (bookInfo.has(DESC)) {
        desc = bookInfo.getString(DESC);
      }

      String imgUrl = "";
      if (bookInfo.has(IMG_URL_PATH) && bookInfo.getJSONObject(IMG_URL_PATH).has(IMG_URL)) {
        imgUrl = bookInfo.getJSONObject(IMG_URL_PATH).getString(IMG_URL);
      }

      writeBackBook(ean, title, subtitle, desc, imgUrl);

      if (bookInfo.has(AUTHORS)) {
        writeBackAuthors(ean, bookInfo.getJSONArray(AUTHORS));
      }
      if (bookInfo.has(CATEGORIES)) {
        writeBackCategories(ean, bookInfo.getJSONArray(CATEGORIES));
      }

    } catch (JSONException e) {
      Log.e(LOG_TAG, "Error ", e);
    }
  }
Пример #6
0
 /** Handle action Foo in the provided background thread with the provided parameters. */
 private void deleteBook(String ean) {
   if (ean != null) {
     getContentResolver()
         .delete(AlexandriaContract.BookEntry.buildBookUri(Long.parseLong(ean)), null, null);
   }
 }