Example #1
0
  /**
   * Insert new Google Drive files in the local database.
   *
   * @param driveFiles Collection of Google Drive files to insert.
   */
  private void insertNewDriveFiles(Collection<File> driveFiles) {
    Uri uri = getNotesUri(mAccount.name);

    Log.d(TAG, "Inserting new Drive files: " + driveFiles.size());

    for (File driveFile : driveFiles) {
      if (driveFile != null) {
        ContentValues values = new ContentValues();
        values.put(NotePad.Notes.COLUMN_NAME_ACCOUNT, mAccount.name);
        values.put(NotePad.Notes.COLUMN_NAME_FILE_ID, driveFile.getId());
        values.put(NotePad.Notes.COLUMN_NAME_TITLE, driveFile.getTitle());
        values.put(NotePad.Notes.COLUMN_NAME_NOTE, getFileContent(driveFile));
        values.put(NotePad.Notes.COLUMN_NAME_CREATE_DATE, driveFile.getCreatedDate().getValue());
        values.put(
            NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE, driveFile.getModifiedDate().getValue());
        try {
          mProvider.insert(uri, values);
        } catch (RemoteException e) {
          e.printStackTrace();
        }
      }
    }

    mContext.getContentResolver().notifyChange(uri, null, false);
  }
Example #2
0
  /** Insert all new local files in Google Drive. */
  private void insertNewLocalFiles() {
    Uri uri = getNotesUri(mAccount.name);
    try {
      Cursor cursor =
          mProvider.query(
              uri, PROJECTION, NotePad.Notes.COLUMN_NAME_FILE_ID + " is NULL", null, null);

      Log.d(TAG, "Inserting new local files: " + cursor.getCount());

      if (cursor.moveToFirst()) {
        do {
          Uri localFileUri = getNoteUri(mAccount.name, cursor.getString(COLUMN_INDEX_ID));

          if (cursor.getShort(COLUMN_INDEX_DELETED) != 0) {
            mProvider.delete(localFileUri, null, null);
          } else {
            File newFile = new File();

            newFile.setTitle(cursor.getString(COLUMN_INDEX_TITLE));
            newFile.setMimeType(TEXT_PLAIN);
            String content = cursor.getString(COLUMN_INDEX_NOTE);

            try {
              File insertedFile = null;

              if (content != null && content.length() > 0) {
                insertedFile =
                    mService
                        .files()
                        .insert(newFile, ByteArrayContent.fromString(TEXT_PLAIN, content))
                        .execute();
              } else {
                insertedFile = mService.files().insert(newFile).execute();
              }

              // Update the local file to add the file ID.
              ContentValues values = new ContentValues();
              values.put(
                  NotePad.Notes.COLUMN_NAME_MODIFICATION_DATE,
                  insertedFile.getModifiedDate().getValue());
              values.put(
                  NotePad.Notes.COLUMN_NAME_CREATE_DATE, insertedFile.getCreatedDate().getValue());
              values.put(NotePad.Notes.COLUMN_NAME_FILE_ID, insertedFile.getId());

              mProvider.update(localFileUri, values, null, null);
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        } while (cursor.moveToNext());
      }
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }