Search in sources :

Example 1 with TableFetcher

use of com.frostwire.android.core.providers.TableFetcher in project frostwire by frostwire.

the class Librarian method getNumFiles.

/**
 * @param fileType the file type
 * @return the number of files registered in the providers
 */
public int getNumFiles(Context context, byte fileType) {
    TableFetcher fetcher = TableFetchers.getFetcher(fileType);
    Cursor c = null;
    int numFiles = 0;
    try {
        ContentResolver cr = context.getContentResolver();
        c = cr.query(fetcher.getContentUri(), new String[] { "count(" + BaseColumns._ID + ")" }, fetcher.where(), fetcher.whereArgs(), null);
        numFiles = c != null && c.moveToFirst() ? c.getInt(0) : 0;
    } catch (Throwable e) {
        Log.e(TAG, "Failed to get num of files", e);
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return numFiles;
}
Also used : TableFetcher(com.frostwire.android.core.providers.TableFetcher) Cursor(android.database.Cursor) ContentResolver(android.content.ContentResolver)

Example 2 with TableFetcher

use of com.frostwire.android.core.providers.TableFetcher in project frostwire by frostwire.

the class UIBittorrentDownload method deleteFilesFromContentResolver.

private void deleteFilesFromContentResolver(Context context, boolean deleteTorrent) {
    final List<TransferItem> items = getItems();
    final ContentResolver cr = context.getContentResolver();
    Librarian librarian = Librarian.instance();
    if (librarian == null) {
        return;
    }
    for (TransferItem item : items) {
        final List<FileDescriptor> fileDescriptors = librarian.getFiles(context, item.getFile().getAbsolutePath(), true);
        for (FileDescriptor fd : fileDescriptors) {
            File file = new File(fd.filePath);
            if (file.isFile()) {
                try {
                    TableFetcher fetcher = TableFetchers.getFetcher(fd.fileType);
                    if (fetcher != null) {
                        cr.delete(fetcher.getContentUri(), MediaStore.MediaColumns._ID + " = " + fd.id, null);
                    }
                } catch (Throwable e) {
                    LOG.error("Failed to delete file from media store. (" + fd.filePath + ")", e);
                }
            }
        }
    }
    if (deleteTorrent) {
        File torrent = dl.getTorrentFile();
        if (torrent != null) {
            final List<FileDescriptor> fds = librarian.getFiles(context, Constants.FILE_TYPE_TORRENTS, torrent.getAbsolutePath(), true);
            librarian.deleteFiles(context, Constants.FILE_TYPE_TORRENTS, fds);
        }
    }
}
Also used : TableFetcher(com.frostwire.android.core.providers.TableFetcher) Librarian(com.frostwire.android.gui.Librarian) TransferItem(com.frostwire.transfers.TransferItem) File(java.io.File) FileDescriptor(com.frostwire.android.core.FileDescriptor) ContentResolver(android.content.ContentResolver)

Example 3 with TableFetcher

use of com.frostwire.android.core.providers.TableFetcher in project frostwire by frostwire.

the class Librarian method deleteFiles.

/**
 * Deletes files.
 * If the fileType is audio it'll use MusicUtils.deleteTracks and
 * tell apollo to clean everything there, playslists, recents, etc.
 *
 * @param context
 * @param fileType
 * @param fds
 */
public void deleteFiles(final Context context, byte fileType, Collection<FileDescriptor> fds) {
    List<Integer> ids = new ArrayList<>(fds.size());
    final int audioMediaType = MediaType.getAudioMediaType().getId();
    if (fileType == audioMediaType) {
        ArrayList<Long> trackIdsToDelete = new ArrayList<>();
        for (FileDescriptor fd : fds) {
            // just in case, as we had similar checks in other code
            if (fd.fileType == audioMediaType) {
                trackIdsToDelete.add((long) fd.id);
                ids.add(fd.id);
            }
        }
        // wish I could do just trackIdsToDelete.toArray(new long[0]) ...
        long[] songsArray = new long[trackIdsToDelete.size()];
        int i = 0;
        for (Long l : trackIdsToDelete) {
            songsArray[i++] = l;
        }
        try {
            MusicUtils.deleteTracks(context, songsArray, false);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    } else {
        for (FileDescriptor fd : fds) {
            ids.add(fd.id);
        }
    }
    try {
        if (context != null) {
            ContentResolver cr = context.getContentResolver();
            TableFetcher fetcher = TableFetchers.getFetcher(fileType);
            cr.delete(fetcher.getContentUri(), MediaColumns._ID + " IN " + buildSet(ids), null);
        } else {
            Log.e(TAG, "Failed to delete files from media store, no context available");
        }
    } catch (Throwable e) {
        Log.e(TAG, "Failed to delete files from media store", e);
    }
    if (fileType == Constants.FILE_TYPE_TORRENTS) {
        FileSystem fs = Platforms.fileSystem();
        for (FileDescriptor fd : fds) {
            try {
                fs.delete(new File(fd.filePath));
            } catch (Throwable ignored) {
            }
        }
    }
    UIUtils.broadcastAction(context, Constants.ACTION_FILE_ADDED_OR_REMOVED, new UIUtils.IntentByteExtra(Constants.EXTRA_REFRESH_FILE_TYPE, fileType));
}
Also used : ArrayList(java.util.ArrayList) FileDescriptor(com.frostwire.android.core.FileDescriptor) ContentResolver(android.content.ContentResolver) FileSystem(com.frostwire.platform.FileSystem) TableFetcher(com.frostwire.android.core.providers.TableFetcher) UIUtils(com.frostwire.android.gui.util.UIUtils) File(java.io.File)

Example 4 with TableFetcher

use of com.frostwire.android.core.providers.TableFetcher in project frostwire by frostwire.

the class Librarian method syncMediaStore.

private void syncMediaStore(final Context context, byte fileType, Set<File> ignorableFiles) {
    TableFetcher fetcher = TableFetchers.getFetcher(fileType);
    if (fetcher == null) {
        return;
    }
    Cursor c = null;
    try {
        ContentResolver cr = context.getContentResolver();
        String where = MediaColumns.DATA + " LIKE ?";
        String[] whereArgs = new String[] { Platforms.data() + "%" };
        c = cr.query(fetcher.getContentUri(), new String[] { MediaColumns._ID, MediaColumns.DATA }, where, whereArgs, null);
        if (c == null) {
            return;
        }
        int idCol = c.getColumnIndex(MediaColumns._ID);
        int pathCol = c.getColumnIndex(MediaColumns.DATA);
        List<Integer> ids = new ArrayList<>(0);
        while (c.moveToNext()) {
            int id = Integer.valueOf(c.getString(idCol));
            String path = c.getString(pathCol);
            if (ignorableFiles.contains(new File(path))) {
                ids.add(id);
            }
        }
        cr.delete(fetcher.getContentUri(), MediaColumns._ID + " IN " + buildSet(ids), null);
    } catch (Throwable e) {
        Log.e(TAG, "General failure during sync of MediaStore", e);
    } finally {
        if (c != null) {
            c.close();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) TableFetcher(com.frostwire.android.core.providers.TableFetcher) Cursor(android.database.Cursor) File(java.io.File) ContentResolver(android.content.ContentResolver)

Example 5 with TableFetcher

use of com.frostwire.android.core.providers.TableFetcher in project frostwire by frostwire.

the class Librarian method renameFile.

public String renameFile(final Context context, FileDescriptor fd, String newFileName) {
    try {
        String filePath = fd.filePath;
        File oldFile = new File(filePath);
        String ext = FilenameUtils.getExtension(filePath);
        File newFile = new File(oldFile.getParentFile(), newFileName + '.' + ext);
        ContentResolver cr = context.getContentResolver();
        ContentValues values = new ContentValues();
        values.put(MediaColumns.DATA, newFile.getAbsolutePath());
        values.put(MediaColumns.DISPLAY_NAME, FilenameUtils.getBaseName(newFileName));
        values.put(MediaColumns.TITLE, FilenameUtils.getBaseName(newFileName));
        TableFetcher fetcher = TableFetchers.getFetcher(fd.fileType);
        cr.update(fetcher.getContentUri(), values, BaseColumns._ID + "=?", new String[] { String.valueOf(fd.id) });
        oldFile.renameTo(newFile);
        return newFile.getAbsolutePath();
    } catch (Throwable e) {
        Log.e(TAG, "Failed to rename file: " + fd, e);
    }
    return null;
}
Also used : ContentValues(android.content.ContentValues) TableFetcher(com.frostwire.android.core.providers.TableFetcher) File(java.io.File) ContentResolver(android.content.ContentResolver)

Aggregations

ContentResolver (android.content.ContentResolver)5 TableFetcher (com.frostwire.android.core.providers.TableFetcher)5 File (java.io.File)4 Cursor (android.database.Cursor)2 FileDescriptor (com.frostwire.android.core.FileDescriptor)2 ArrayList (java.util.ArrayList)2 ContentValues (android.content.ContentValues)1 Librarian (com.frostwire.android.gui.Librarian)1 UIUtils (com.frostwire.android.gui.util.UIUtils)1 FileSystem (com.frostwire.platform.FileSystem)1 TransferItem (com.frostwire.transfers.TransferItem)1