Search in sources :

Example 1 with BaseFileObject

use of com.simplecity.amp_library.model.BaseFileObject in project Shuttle by timusus.

the class FileBrowser method loadDir.

/**
     * Loads the specified folder.
     *
     * @param directory The file object to points to the directory to load.
     * @return An {@link List<BaseFileObject>} object that holds the data of the specified directory.
     */
public List<BaseFileObject> loadDir(File directory) {
    mCurrentDir = directory;
    List<BaseFileObject> folderObjects = new ArrayList<>();
    List<BaseFileObject> fileObjects = new ArrayList<>();
    //Grab a list of all files/subdirs within the specified directory.
    File[] files = directory.listFiles(FileHelper.getAudioFilter());
    if (files != null) {
        for (File file : files) {
            BaseFileObject baseFileObject;
            if (file.isDirectory()) {
                baseFileObject = new FolderObject();
                baseFileObject.path = FileHelper.getPath(file);
                baseFileObject.name = file.getName();
                File[] listOfFiles = file.listFiles(FileHelper.getAudioFilter());
                if (listOfFiles != null && listOfFiles.length > 0) {
                    for (File listOfFile : listOfFiles) {
                        if (listOfFile.isDirectory()) {
                            ((FolderObject) baseFileObject).folderCount++;
                        } else {
                            ((FolderObject) baseFileObject).fileCount++;
                        }
                    }
                } else {
                    continue;
                }
                if (!folderObjects.contains(baseFileObject)) {
                    folderObjects.add(baseFileObject);
                }
            } else {
                baseFileObject = new FileObject();
                baseFileObject.path = FileHelper.getPath(file);
                baseFileObject.name = FileHelper.getName(file.getName());
                baseFileObject.size = file.length();
                ((FileObject) baseFileObject).extension = FileHelper.getExtension(file.getName());
                if (TextUtils.isEmpty(((FileObject) baseFileObject).extension)) {
                    continue;
                }
                ((FileObject) baseFileObject).tagInfo = new TagInfo(baseFileObject.path);
                if (!fileObjects.contains(baseFileObject)) {
                    fileObjects.add(baseFileObject);
                }
            }
        }
    }
    sortFileObjects(fileObjects);
    sortFolderObjects(folderObjects);
    if (!SettingsManager.getInstance().getFolderBrowserFilesAscending()) {
        Collections.reverse(fileObjects);
    }
    if (!SettingsManager.getInstance().getFolderBrowserFoldersAscending()) {
        Collections.reverse(folderObjects);
    }
    folderObjects.addAll(fileObjects);
    if (!FileHelper.isRootDirectory(mCurrentDir)) {
        FolderObject parentObject = new FolderObject();
        parentObject.fileType = FileType.PARENT;
        parentObject.name = FileHelper.PARENT_DIRECTORY;
        parentObject.path = FileHelper.getPath(mCurrentDir) + "/" + FileHelper.PARENT_DIRECTORY;
        folderObjects.add(0, parentObject);
    }
    return folderObjects;
}
Also used : BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) TagInfo(com.simplecity.amp_library.model.TagInfo) ArrayList(java.util.ArrayList) FolderObject(com.simplecity.amp_library.model.FolderObject) FileObject(com.simplecity.amp_library.model.FileObject) BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) File(java.io.File)

Example 2 with BaseFileObject

use of com.simplecity.amp_library.model.BaseFileObject in project Shuttle by timusus.

the class FileHelper method renameFile.

/**
     * Renames an {@link FileObject} to the passed in newName
     *
     * @param context        Context
     * @param baseFileObject the FileObject representation of the file to rename
     * @param newName        the new name of the file
     * @return
     */
public static boolean renameFile(Context context, BaseFileObject baseFileObject, String newName) {
    if (newName == null) {
        return false;
    }
    if (baseFileObject instanceof FileObject) {
        String ext = ((FileObject) baseFileObject).extension;
        if (ext == null) {
            ext = "";
        }
        newName = newName + "." + ext;
    }
    File file = new File(baseFileObject.path);
    File newFile = new File(baseFileObject.getParent(), newName);
    if (file.renameTo(newFile)) {
        baseFileObject.name = FileHelper.getName(newFile.getName());
        CustomMediaScanner.scanFiles(Collections.singletonList(file.getPath()), null);
        return true;
    }
    return false;
}
Also used : FileObject(com.simplecity.amp_library.model.FileObject) BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) File(java.io.File)

Example 3 with BaseFileObject

use of com.simplecity.amp_library.model.BaseFileObject in project Shuttle by timusus.

the class FolderFragment method changeDir.

public void changeDir(File newDir) {
    final String path = FileHelper.getPath(newDir);
    if (TextUtils.isEmpty(path)) {
        return;
    }
    ShuttleUtils.execute(new AsyncTask<Void, Void, List<BaseFileObject>>() {

        private final File file = new File(path);

        @Override
        protected List<BaseFileObject> doInBackground(Void... params) {
            return mFileBrowser.loadDir(file);
        }

        @Override
        protected void onPostExecute(List<BaseFileObject> fileObjects) {
            super.onPostExecute(fileObjects);
            List<AdaptableItem> items = Stream.of(fileObjects).map(baseFileObject -> {
                FolderView folderView = new FolderView(baseFileObject);
                folderView.setChecked(mShowCheckboxes);
                return folderView;
            }).collect(Collectors.toList());
            mCurrentDir = path;
            if (mShowBreadcrumbsInList) {
                BreadcrumbsView breadcrumbsView = new BreadcrumbsView(mCurrentDir);
                breadcrumbsView.setBreadcrumbsPath(mCurrentDir);
                items.add(0, breadcrumbsView);
            }
            if (mAdapter != null) {
                mAdapter.setItems(items);
            }
            if (mBreadcrumb != null) {
                mBreadcrumb.changeBreadcrumbPath(mCurrentDir);
            }
            if (mAdapter != null) {
                changeBreadcrumbPath();
            }
        }
    });
}
Also used : BreadcrumbsView(com.simplecity.amp_library.ui.modelviews.BreadcrumbsView) FolderView(com.simplecity.amp_library.ui.modelviews.FolderView) BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File)

Example 4 with BaseFileObject

use of com.simplecity.amp_library.model.BaseFileObject in project Shuttle by timusus.

the class FolderFragment method onOverflowClick.

@Override
public void onOverflowClick(View v, int position, BaseFileObject fileObject) {
    PopupMenu menu = new PopupMenu(getActivity(), v);
    if (fileObject.fileType == FileType.FILE) {
        //Play this song next
        menu.getMenu().add(FRAGMENT_GROUPID, PLAY_NEXT, 4, R.string.play_next);
        //Tag editor
        if (ShuttleUtils.isUpgraded()) {
            menu.getMenu().add(FRAGMENT_GROUPID, TAGGER, 5, R.string.edit_tags);
        }
        //Set this song as the ringtone
        menu.getMenu().add(FRAGMENT_GROUPID, USE_AS_RINGTONE, 6, R.string.ringtone_menu);
        if (FileHelper.canReadWrite(new File(fileObject.path))) {
            //Rename File
            menu.getMenu().add(FRAGMENT_GROUPID, RENAME, 7, R.string.rename_file);
            //Delete File
            menu.getMenu().add(FRAGMENT_GROUPID, DELETE_ITEM, 8, R.string.delete_item);
        }
        menu.getMenu().add(FRAGMENT_GROUPID, VIEW_INFO, 9, R.string.song_info);
    } else {
        //Play all files in this dir
        menu.getMenu().add(FRAGMENT_GROUPID, PLAY_SELECTION, 0, R.string.play_selection);
        //Set this directory as initial directory
        menu.getMenu().add(FRAGMENT_GROUPID, SET_INITIAL_DIR, 4, R.string.set_initial_dir);
        if (FileHelper.canReadWrite(new File(fileObject.path))) {
            //Rename dir
            menu.getMenu().add(FRAGMENT_GROUPID, RENAME, 5, R.string.rename_folder);
            //Delete dir
            menu.getMenu().add(FRAGMENT_GROUPID, DELETE_ITEM, 6, R.string.delete_item);
        }
    }
    //Bring up the add to playlist menu
    SubMenu sub = menu.getMenu().addSubMenu(FRAGMENT_GROUPID, ADD_TO_PLAYLIST, 2, R.string.add_to_playlist);
    PlaylistUtils.makePlaylistMenu(getActivity(), sub, FRAGMENT_GROUPID);
    //Add to queue
    menu.getMenu().add(FRAGMENT_GROUPID, QUEUE, 3, R.string.add_to_queue);
    menu.getMenu().add(FRAGMENT_GROUPID, RESCAN, 4, R.string.scan_file);
    menu.setOnMenuItemClickListener(item -> {
        switch(item.getItemId()) {
            case TAGGER:
                FileHelper.getSong(new File(fileObject.path)).subscribeOn(Schedulers.io()).subscribe(song -> TaggerDialog.newInstance(song).show(getFragmentManager()));
                return true;
            case QUEUE:
                FileHelper.getSongList(new File(fileObject.path), true, false).observeOn(AndroidSchedulers.mainThread()).subscribe(songs -> MusicUtils.addToQueue(getActivity(), songs));
                return true;
            case DELETE_ITEM:
                MaterialDialog.Builder builder = DialogUtils.getBuilder(getActivity()).title(R.string.delete_item).icon(DrawableUtils.getBlackDrawable(getActivity(), R.drawable.ic_dialog_alert));
                if (fileObject.fileType == FileType.FILE) {
                    builder.content(String.format(getResources().getString(R.string.delete_file_confirmation_dialog), fileObject.name));
                } else {
                    builder.content(String.format(getResources().getString(R.string.delete_folder_confirmation_dialog), fileObject.path));
                }
                builder.positiveText(R.string.button_ok).onPositive((materialDialog, dialogAction) -> {
                    if (FileHelper.deleteFile(new File(fileObject.path))) {
                        mAdapter.removeItem(position);
                        CustomMediaScanner.scanFiles(Collections.singletonList(fileObject.path), null);
                    } else {
                        Toast.makeText(getActivity(), fileObject.fileType == FileType.FOLDER ? R.string.delete_folder_failed : R.string.delete_file_failed, Toast.LENGTH_LONG).show();
                    }
                });
                builder.negativeText(R.string.cancel).show();
                return true;
            case RENAME:
                View customView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_rename, null);
                final CustomEditText editText = (CustomEditText) customView.findViewById(R.id.editText);
                ThemeUtils.themeEditText(editText);
                editText.setText(fileObject.name);
                builder = DialogUtils.getBuilder(getActivity());
                if (fileObject.fileType == FileType.FILE) {
                    builder.title(R.string.rename_file);
                } else {
                    builder.title(R.string.rename_folder);
                }
                builder.customView(customView, false);
                builder.positiveText(R.string.save).onPositive((materialDialog, dialogAction) -> {
                    if (editText.getText() != null) {
                        if (FileHelper.renameFile(getActivity(), fileObject, editText.getText().toString())) {
                            mAdapter.notifyDataSetChanged();
                        } else {
                            Toast.makeText(getActivity(), fileObject.fileType == FileType.FOLDER ? R.string.rename_folder_failed : R.string.rename_file_failed, Toast.LENGTH_LONG).show();
                        }
                    }
                });
                builder.negativeText(R.string.cancel).show();
                return true;
            case USE_AS_RINGTONE:
                FileHelper.getSong(new File(fileObject.path)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(song -> ShuttleUtils.setRingtone(getContext(), song));
                return true;
            case PLAY_NEXT:
                FileHelper.getSongList(new File(fileObject.path), false, false).observeOn(AndroidSchedulers.mainThread()).subscribe(songs -> MusicUtils.playNext(getActivity(), songs));
                return true;
            case PLAY_SELECTION:
                final ProgressDialog progressDialog = ProgressDialog.show(getActivity(), "", getString(R.string.gathering_songs), false);
                FileHelper.getSongList(new File(fileObject.path), true, fileObject.fileType == FileType.FILE).observeOn(AndroidSchedulers.mainThread()).subscribe(songs -> {
                    MusicUtils.playAll(songs, 0, () -> {
                        final String message = getContext().getString(R.string.emptyplaylist);
                        Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
                    });
                    if (isAdded() && progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                });
                return true;
            case NEW_PLAYLIST:
                List<BaseFileObject> fileObjects = new ArrayList<>();
                fileObjects.add(fileObject);
                PlaylistUtils.createFileObjectPlaylistDialog(getActivity(), fileObjects);
                return true;
            case PLAYLIST_SELECTED:
                final Playlist playlist = (Playlist) item.getIntent().getSerializableExtra(ShuttleUtils.ARG_PLAYLIST);
                FileHelper.getSongList(new File(fileObject.path), true, false).observeOn(AndroidSchedulers.mainThread()).subscribe(songs -> PlaylistUtils.addToPlaylist(getContext(), playlist, songs));
                return true;
            case SET_INITIAL_DIR:
                SettingsManager.getInstance().setFolderBrowserInitialDir(fileObject.path);
                Toast.makeText(getActivity(), fileObject.path + getResources().getString(R.string.initial_dir_set_message), Toast.LENGTH_SHORT).show();
                return true;
            case RESCAN:
                if (fileObject instanceof FolderObject) {
                    View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_progress, null);
                    TextView pathsTextView = (TextView) view.findViewById(R.id.paths);
                    pathsTextView.setText(fileObject.path);
                    ProgressBar indeterminateProgress = (ProgressBar) view.findViewById(R.id.indeterminateProgress);
                    DrawableCompat.setTint(DrawableCompat.wrap(indeterminateProgress.getIndeterminateDrawable()), ColorUtils.getAccentColor());
                    ProgressBar horizontalProgress = (ProgressBar) view.findViewById(R.id.horizontalProgress);
                    DrawableCompat.setTint(DrawableCompat.wrap(horizontalProgress.getProgressDrawable()), ColorUtils.getAccentColor());
                    MaterialDialog dialog = DialogUtils.getBuilder(getContext()).title(R.string.scanning).customView(view, false).negativeText(R.string.close).show();
                    FileHelper.getSongList(new File(fileObject.path), true, false).map(songs -> Stream.of(songs).map(song -> song.path).collect(Collectors.toList())).observeOn(AndroidSchedulers.mainThread()).subscribe(paths -> {
                        ViewUtils.fadeOut(indeterminateProgress, null);
                        ViewUtils.fadeIn(horizontalProgress, null);
                        horizontalProgress.setMax(paths.size());
                        CustomMediaScanner.scanFiles(paths, new CustomMediaScanner.ScanCompletionListener() {

                            @Override
                            public void onPathScanned(String path) {
                                horizontalProgress.setProgress(horizontalProgress.getProgress() + 1);
                                pathsTextView.setText(path);
                            }

                            @Override
                            public void onScanCompleted() {
                                if (isAdded() && dialog.isShowing()) {
                                    dialog.dismiss();
                                }
                            }
                        });
                    });
                } else {
                    CustomMediaScanner.scanFiles(Collections.singletonList(fileObject.path), new CustomMediaScanner.ScanCompletionListener() {

                        @Override
                        public void onPathScanned(String path) {
                        }

                        @Override
                        public void onScanCompleted() {
                            Toast.makeText(getContext(), R.string.scan_complete, Toast.LENGTH_LONG).show();
                        }
                    });
                }
                return true;
            case VIEW_INFO:
                DialogUtils.showFileInfoDialog(getActivity(), (FileObject) fileObject);
                break;
        }
        return false;
    });
    menu.show();
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) CustomEditText(com.simplecity.amp_library.ui.views.CustomEditText) ArrayList(java.util.ArrayList) SubMenu(android.view.SubMenu) ProgressDialog(android.app.ProgressDialog) View(android.view.View) TextView(android.widget.TextView) FolderView(com.simplecity.amp_library.ui.modelviews.FolderView) RecyclerView(android.support.v7.widget.RecyclerView) BreadcrumbsView(com.simplecity.amp_library.ui.modelviews.BreadcrumbsView) Playlist(com.simplecity.amp_library.model.Playlist) BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) TextView(android.widget.TextView) FolderObject(com.simplecity.amp_library.model.FolderObject) File(java.io.File) ProgressBar(android.widget.ProgressBar) PopupMenu(android.support.v7.widget.PopupMenu) CustomMediaScanner(com.simplecity.amp_library.utils.CustomMediaScanner)

Example 5 with BaseFileObject

use of com.simplecity.amp_library.model.BaseFileObject in project Shuttle by timusus.

the class PlaylistUtils method addFileObjectsToPlaylist.

public static void addFileObjectsToPlaylist(Context context, Playlist playlist, List<BaseFileObject> fileObjects) {
    ProgressDialog progressDialog = ProgressDialog.show(context, "", context.getString(R.string.gathering_songs), false);
    long folderCount = Stream.of(fileObjects).filter(value -> value.fileType == FileType.FOLDER).count();
    if (folderCount > 0) {
        progressDialog.show();
    }
    ShuttleUtils.getSongsForFileObjects(fileObjects).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(songs -> {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        addToPlaylist(context, playlist, songs);
    });
}
Also used : R(com.simplecity.amp_library.R) Spannable(android.text.Spannable) Uri(android.net.Uri) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) LocalBroadcastManager(android.support.v4.content.LocalBroadcastManager) PlayCountTable(com.simplecity.amp_library.sql.providers.PlayCountTable) Song(com.simplecity.amp_library.model.Song) CheckBox(android.widget.CheckBox) ContentResolver(android.content.ContentResolver) MediaStore(android.provider.MediaStore) Schedulers(rx.schedulers.Schedulers) View(android.view.View) Log(android.util.Log) Playlist(com.simplecity.amp_library.model.Playlist) BaseColumns(android.provider.BaseColumns) SubMenu(android.view.SubMenu) Query(com.simplecity.amp_library.model.Query) List(java.util.List) TextView(android.widget.TextView) ContentValues(android.content.ContentValues) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) SqlUtils(com.simplecity.amp_library.sql.SqlUtils) TextWatcher(android.text.TextWatcher) FileType(com.simplecity.amp_library.interfaces.FileType) Context(android.content.Context) Stream(com.annimon.stream.Stream) Environment(android.os.Environment) Dialog(android.app.Dialog) Intent(android.content.Intent) NonNull(android.support.annotation.NonNull) Editable(android.text.Editable) Observable(rx.Observable) SpannableStringBuilder(android.text.SpannableStringBuilder) Func1(rx.functions.Func1) Toast(android.widget.Toast) BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) Cursor(android.database.Cursor) SqlBriteUtils(com.simplecity.amp_library.sql.sqlbrite.SqlBriteUtils) Collectors(com.annimon.stream.Collectors) LayoutInflater(android.view.LayoutInflater) StyleSpan(android.text.style.StyleSpan) FileWriter(java.io.FileWriter) ProgressDialog(android.app.ProgressDialog) TextUtils(android.text.TextUtils) DialogAction(com.afollestad.materialdialogs.DialogAction) IOException(java.io.IOException) WorkerThread(android.support.annotation.WorkerThread) File(java.io.File) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) MusicService(com.simplecity.amp_library.playback.MusicService) Crashlytics(com.crashlytics.android.Crashlytics) CustomEditText(com.simplecity.amp_library.ui.views.CustomEditText) EditText(android.widget.EditText) ContentUris(android.content.ContentUris) ProgressDialog(android.app.ProgressDialog)

Aggregations

BaseFileObject (com.simplecity.amp_library.model.BaseFileObject)5 File (java.io.File)5 ProgressDialog (android.app.ProgressDialog)2 SubMenu (android.view.SubMenu)2 View (android.view.View)2 TextView (android.widget.TextView)2 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)2 FileObject (com.simplecity.amp_library.model.FileObject)2 Playlist (com.simplecity.amp_library.model.Playlist)2 CustomEditText (com.simplecity.amp_library.ui.views.CustomEditText)2 ArrayList (java.util.ArrayList)2 Dialog (android.app.Dialog)1 ContentResolver (android.content.ContentResolver)1 ContentUris (android.content.ContentUris)1 ContentValues (android.content.ContentValues)1 Context (android.content.Context)1 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 Environment (android.os.Environment)1