示例#1
0
 /**
  * If the download failed to complete then delete the content associated with it, as it cannot
  * otherwise be deleted (there's no resource record for it).
  */
 private void deleteDownloadSideEffect(Uri downloadUri) {
   Log.v("deleteDownloadSideEffect(uri=" + downloadUri + ")");
   Cursor cursor =
       query(
           downloadUri,
           new String[] {Downloads._ID, Downloads.DIRECTORY, Downloads.FILENAME, Downloads.STATUS},
           null,
           null,
           null);
   if (!cursor.moveToFirst()) {
     // This is also called when deleting a Resource and there the
     // download could have been manually removed by the user, so this is
     // to be expected common.
     cursor.close();
     return;
   }
   int statusInt = cursor.getInt(cursor.getColumnIndex(Downloads.STATUS));
   Downloads.Status status = Downloads.Status.fromInt(statusInt);
   if (status == Downloads.Status.FAILED
       || status == Downloads.Status.CANCELLED
       || status == Downloads.Status.PAUSED) {
     String filename = cursor.getString(cursor.getColumnIndex(Downloads.FILENAME));
     String dir = cursor.getString(cursor.getColumnIndex(Downloads.DIRECTORY));
     deleteVideo(directorySwitch(dir), filename);
   }
   cursor.close();
 }
示例#2
0
  @Override
  public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    List<String> pseg = uri.getPathSegments();
    if (pseg.size() < 2) {
      throw new FileNotFoundException("invalid uri error " + uri);
    }

    File path = getFileFromUri(uri);

    Log.v("openFile(uri=" + uri + ", file=" + path + ")");
    int imode = 0;
    if (mode.contains("w")) {
      imode |= ParcelFileDescriptor.MODE_WRITE_ONLY;
      if (!path.exists()) {
        try {
          path.createNewFile();
        } catch (IOException e) {
          e.printStackTrace();
          throw new FileNotFoundException("Error creating " + uri);
        }
      } else {
        throw new FileNotFoundException("File with name " + path + " already exists");
      }
    } else if (mode.contains("r")) {
      if (!path.exists()) {
        throw new FileNotFoundException("File not found " + uri);
      }
    }

    if (mode.contains("r")) imode |= ParcelFileDescriptor.MODE_READ_ONLY;
    if (mode.contains("+")) imode |= ParcelFileDescriptor.MODE_APPEND;

    return ParcelFileDescriptor.open(path, imode);
  }
示例#3
0
  /** {@inheritDoc} */
  @Override
  public int delete(Uri uri, String selection, String[] selectionArgs) {
    Log.v("delete(uri=" + uri + ")");

    final int match = getUriMatcher().match(uri);
    switch (match) {
      case RESOURCES_ID:
        deleteResourceSideEffect(uri);
        break;
      case DOWNLOADS_ID:
        deleteDownloadSideEffect(uri);
        break;
      default:
        break;
    }

    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    final SelectionBuilder builder = buildSimpleSelection(uri);
    int retVal;
    synchronized (mutex) {
      retVal = builder.where(selection, selectionArgs).delete(db);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return retVal;
  }
示例#4
0
  /** {@inheritDoc} */
  @Override
  public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    Log.v("update(uri=" + uri + ")");

    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    final SelectionBuilder builder = buildSimpleSelection(uri);
    int rowsChanged;
    synchronized (mutex) {
      rowsChanged = builder.where(selection, selectionArgs).update(db, values);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return rowsChanged;
  }
示例#5
0
  @Override
  public Cursor query(
      Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    Log.v("query(uri=" + uri + ", proj=" + Arrays.toString(projection) + ")");

    final SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    final int match = getUriMatcher().match(uri);

    final SelectionBuilder builder = buildExpandedSelection(uri, match);
    Cursor query;
    synchronized (mutex) {
      query = builder.where(selection, selectionArgs).query(db, projection, sortOrder);
    }
    query.setNotificationUri(getContext().getContentResolver(), uri);
    return query;
  }
示例#6
0
  private void deleteResourceSideEffect(Uri resourceUri) {
    Log.v("deleteResourceSideEffect(uri=" + resourceUri + ")");

    // delete download row correspondong to this content
    Cursor cursor =
        query(
            resourceUri,
            new String[] {
              Resources._ID,
              Resources.DOWNLOAD_ID,
              Resources.DIRECTORY,
              Resources.FILENAME,
              Resources.CONTENT
            },
            null,
            null,
            null);
    if (!cursor.moveToFirst()) {
      Log.e("deleteResourceSideEffect: cursor empty error");
      return;
    }
    String downloadId = cursor.getString(cursor.getColumnIndex(Resources.DOWNLOAD_ID));
    Uri downloadUri = Downloads.buildDownloadUri(downloadId);
    delete(downloadUri, null, null);
    getContext().getContentResolver().notifyChange(downloadUri, null);

    // delete video file associated with resource
    String filename = cursor.getString(cursor.getColumnIndex(Resources.FILENAME));
    String dir = cursor.getString(cursor.getColumnIndex(Resources.DIRECTORY));
    deleteVideo(directorySwitch(dir), filename);

    if (VizUtils.isPublicDir(dir)) {
      removeFromMediaStore(VizUtils.getPublicVideoFilename(filename));
    }

    deleteThumbnail(VizContract.PATH_THUMBNAILS, filename + THUMBNAIL_EXT);

    cursor.close();
  }
示例#7
0
 private void deleteThumbnail(String dir, String filename) {
   Log.v("deleteThumbnail(dir=" + dir + ", filename=" + filename + ")");
   File file = new File(getContext().getExternalFilesDir(dir), filename);
   file.delete();
 }
示例#8
0
 private void deleteVideo(File dir, String filename) {
   Log.v("deleteThumbnail(dir=" + dir.toString() + ", filename=" + filename + ")");
   File file = new File(dir, filename);
   file.delete();
 }
示例#9
0
  /** {@inheritDoc} */
  @Override
  public Uri insert(Uri uri, ContentValues values) {
    Log.v("insert(uri=" + uri + ", values=[" + values.toString() + "])");

    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    final int match = getUriMatcher().match(uri);
    long id = 0;

    switch (match) {
      case FAVORITES:
        {
          synchronized (mutex) {
            id = db.insertOrThrow(Tables.FAVORITES, null, values);
          }
          getContext().getContentResolver().notifyChange(uri, null);
          return Favorites.buildFavoriteUri(String.valueOf(id));
        }
      case RESOURCES:
      case RESOURCES_ID:
        {
          try {
            addDefaultThumbnail(values);
            calculateDuration(values);
          } catch (Exception e) {
            Log.w("Exception thrown creating thumbnail");
            e.printStackTrace();
          }
          try {
            informMediaScanner(values);
          } catch (IOException e) {
          }
          values.put(Resources.TIMESTAMP, String.valueOf(System.currentTimeMillis()));
          synchronized (mutex) {
            id = db.insertOrThrow(Tables.RESOURCES, null, values);
          }
          Uri resourceUri = Resources.buildResourceUri(String.valueOf(id));

          try {
            createThumbnail(values);
            getContext().getContentResolver().update(resourceUri, values, null, null);
          } catch (IOException e) {
            Log.w("Error creating thumbnail");
          }

          getContext().getContentResolver().notifyChange(uri, null);
          return resourceUri;
        }
      case DOWNLOADS:
        {
          values.put(Downloads.STATUS, Downloads.Status.INPROGRESS.valueOf());
          values.put(Downloads.TIMESTAMP, String.valueOf(System.currentTimeMillis()));
          values.put(Downloads.MAX_PROGRESS, Downloads.PROGRESS_MAX_NUM);
          synchronized (mutex) {
            id = db.insertOrThrow(Tables.DOWNLOADS, null, values);
          }
          getContext().getContentResolver().notifyChange(uri, null);
          return Downloads.buildDownloadUri(String.valueOf(id));
        }

      default:
        {
          throw new UnsupportedOperationException("Unknown uri(" + match + "): " + uri);
        }
    }
  }