Search in sources :

Example 11 with Playlist

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

the class DetailPresenter method playlistSelected.

void playlistSelected(Context context, MenuItem item, Runnable insertCallback) {
    songsProvider.getSongs().observeOn(AndroidSchedulers.mainThread()).subscribe(songs -> {
        Playlist playlist = (Playlist) item.getIntent().getSerializableExtra(PlaylistUtils.ARG_PLAYLIST);
        PlaylistUtils.addToPlaylist(context, playlist, songs, insertCallback);
    });
}
Also used : Playlist(com.simplecity.amp_library.model.Playlist)

Example 12 with Playlist

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

the class PlaylistUtils method createPlaylist.

@Nullable
public static Playlist createPlaylist(Context context, String name) {
    Playlist playlist = null;
    long id = -1;
    if (!TextUtils.isEmpty(name)) {
        Query query = new Query.Builder().uri(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI).projection(new String[] { MediaStore.Audio.PlaylistsColumns.NAME }).selection(MediaStore.Audio.PlaylistsColumns.NAME + " = '" + name + "'").build();
        final Cursor cursor = SqlUtils.createQuery(context, query);
        if (cursor != null) {
            try {
                int count = cursor.getCount();
                if (count <= 0) {
                    final ContentValues values = new ContentValues(1);
                    values.put(MediaStore.Audio.PlaylistsColumns.NAME, name);
                    // Catch NPE occurring on Amazon devices.
                    try {
                        final Uri uri = context.getContentResolver().insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values);
                        if (uri != null) {
                            id = Long.parseLong(uri.getLastPathSegment());
                        }
                    } catch (NullPointerException e) {
                        Crashlytics.log("Failed to create playlist: " + e.getMessage());
                    }
                }
            } finally {
                cursor.close();
            }
        }
    }
    if (id != -1) {
        playlist = new Playlist(Playlist.Type.USER_CREATED, id, name, true, false, true, true, true);
    } else {
        Crashlytics.log(String.format("Failed to create playlist. Name: %s, id: %d", name, id));
        DataManager.getInstance().getPlaylistsRelay().first(Collections.emptyList()).subscribe(playlists -> Crashlytics.log("Existing playlists: " + playlists), throwable -> {
        });
    }
    return playlist;
}
Also used : ContentValues(android.content.ContentValues) Playlist(com.simplecity.amp_library.model.Playlist) Query(com.simplecity.amp_library.model.Query) SpannableStringBuilder(android.text.SpannableStringBuilder) Cursor(android.database.Cursor) Uri(android.net.Uri) SuppressLint(android.annotation.SuppressLint) Nullable(android.support.annotation.Nullable)

Example 13 with Playlist

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

the class PlaylistUtils method addFileObjectsToPlaylist.

@SuppressLint("CheckResult")
public static void addFileObjectsToPlaylist(Context context, Playlist playlist, List<BaseFileObject> fileObjects, Runnable insertCallback) {
    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, insertCallback);
    }, error -> LogUtils.logException(TAG, "Error getting songs for file object", error));
}
Also used : R(com.simplecity.amp_library.R) Spannable(android.text.Spannable) Completable(io.reactivex.Completable) Uri(android.net.Uri) PlayCountTable(com.simplecity.amp_library.sql.providers.PlayCountTable) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Song(com.simplecity.amp_library.model.Song) Optional(com.annimon.stream.Optional) CheckBox(android.widget.CheckBox) ContentResolver(android.content.ContentResolver) MediaStore(android.provider.MediaStore) View(android.view.View) Schedulers(io.reactivex.schedulers.Schedulers) Log(android.util.Log) Playlist(com.simplecity.amp_library.model.Playlist) SubMenu(android.view.SubMenu) Query(com.simplecity.amp_library.model.Query) List(java.util.List) TextView(android.widget.TextView) ContentValues(android.content.ContentValues) Nullable(android.support.annotation.Nullable) 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) Single(io.reactivex.Single) Editable(android.text.Editable) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint) SpannableStringBuilder(android.text.SpannableStringBuilder) Toast(android.widget.Toast) BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) Observable(io.reactivex.Observable) UnsafeConsumer(com.simplecity.amp_library.rx.UnsafeConsumer) Cursor(android.database.Cursor) SqlBriteUtils(com.simplecity.amp_library.sql.sqlbrite.SqlBriteUtils) 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) TimeUnit(java.util.concurrent.TimeUnit) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) Pair(android.support.v4.util.Pair) Crashlytics(com.crashlytics.android.Crashlytics) Collections(java.util.Collections) EditText(android.widget.EditText) ContentUris(android.content.ContentUris) ProgressDialog(android.app.ProgressDialog) SuppressLint(android.annotation.SuppressLint)

Example 14 with Playlist

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

the class PlaylistUtils method createFavoritePlaylist.

@Nullable
public static Optional<Playlist> createFavoritePlaylist() {
    Playlist playlist = PlaylistUtils.createPlaylist(ShuttleApplication.getInstance(), ShuttleApplication.getInstance().getString(R.string.fav_title));
    if (playlist != null) {
        playlist.canDelete = false;
        playlist.canRename = false;
        playlist.type = Playlist.Type.FAVORITES;
    }
    return Optional.ofNullable(playlist);
}
Also used : Playlist(com.simplecity.amp_library.model.Playlist) Nullable(android.support.annotation.Nullable)

Example 15 with Playlist

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

the class PlaylistUtils method renamePlaylistDialog.

public static void renamePlaylistDialog(final Context context, final Playlist playlist) {
    @SuppressLint("InflateParams") View customView = LayoutInflater.from(context).inflate(R.layout.dialog_playlist, null);
    final EditText editText = customView.findViewById(R.id.editText);
    editText.setText(playlist.name);
    MaterialDialog.Builder builder = DialogUtils.getBuilder(context).title(R.string.create_playlist_create_text_prompt).customView(customView, false).positiveText(R.string.save).onPositive((materialDialog, dialogAction) -> {
        String name = editText.getText().toString();
        if (name.length() > 0) {
            ContentResolver resolver = context.getContentResolver();
            ContentValues values = new ContentValues(1);
            values.put(MediaStore.Audio.Playlists.NAME, name);
            resolver.update(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values, MediaStore.Audio.Playlists._ID + "=?", new String[] { Long.valueOf(playlist.id).toString() });
            playlist.name = name;
            Toast.makeText(context, R.string.playlist_renamed_message, Toast.LENGTH_SHORT).show();
        }
    }).negativeText(R.string.cancel);
    final MaterialDialog dialog = builder.build();
    TextWatcher textWatcher = new TextWatcher() {

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // check if playlist with current name exists already, and warn the user if so.
            setSaveButton(dialog, playlist, editText.getText().toString());
        }

        public void afterTextChanged(Editable s) {
        }
    };
    editText.addTextChangedListener(textWatcher);
    dialog.show();
}
Also used : EditText(android.widget.EditText) R(com.simplecity.amp_library.R) Spannable(android.text.Spannable) Completable(io.reactivex.Completable) Uri(android.net.Uri) PlayCountTable(com.simplecity.amp_library.sql.providers.PlayCountTable) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Song(com.simplecity.amp_library.model.Song) Optional(com.annimon.stream.Optional) CheckBox(android.widget.CheckBox) ContentResolver(android.content.ContentResolver) MediaStore(android.provider.MediaStore) View(android.view.View) Schedulers(io.reactivex.schedulers.Schedulers) Log(android.util.Log) Playlist(com.simplecity.amp_library.model.Playlist) SubMenu(android.view.SubMenu) Query(com.simplecity.amp_library.model.Query) List(java.util.List) TextView(android.widget.TextView) ContentValues(android.content.ContentValues) Nullable(android.support.annotation.Nullable) 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) Single(io.reactivex.Single) Editable(android.text.Editable) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint) SpannableStringBuilder(android.text.SpannableStringBuilder) Toast(android.widget.Toast) BaseFileObject(com.simplecity.amp_library.model.BaseFileObject) Observable(io.reactivex.Observable) UnsafeConsumer(com.simplecity.amp_library.rx.UnsafeConsumer) Cursor(android.database.Cursor) SqlBriteUtils(com.simplecity.amp_library.sql.sqlbrite.SqlBriteUtils) 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) TimeUnit(java.util.concurrent.TimeUnit) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) Pair(android.support.v4.util.Pair) Crashlytics(com.crashlytics.android.Crashlytics) Collections(java.util.Collections) EditText(android.widget.EditText) ContentUris(android.content.ContentUris) ContentValues(android.content.ContentValues) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) SuppressLint(android.annotation.SuppressLint) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) View(android.view.View) TextView(android.widget.TextView) ContentResolver(android.content.ContentResolver)

Aggregations

Playlist (com.simplecity.amp_library.model.Playlist)35 Intent (android.content.Intent)22 View (android.view.View)19 List (java.util.List)19 Uri (android.net.Uri)17 R (com.simplecity.amp_library.R)17 Song (com.simplecity.amp_library.model.Song)17 Context (android.content.Context)16 LayoutInflater (android.view.LayoutInflater)16 Toast (android.widget.Toast)16 SubMenu (android.view.SubMenu)15 Stream (com.annimon.stream.Stream)15 ContentUris (android.content.ContentUris)14 MediaStore (android.provider.MediaStore)14 TextView (android.widget.TextView)14 ArrayList (java.util.ArrayList)14 Query (com.simplecity.amp_library.model.Query)12 TimeUnit (java.util.concurrent.TimeUnit)12 NonNull (android.support.annotation.NonNull)11 Collections (java.util.Collections)11