SAF is fixed
This commit is contained in:
parent
8789eeb854
commit
570a235836
25 changed files with 907 additions and 142 deletions
|
@ -0,0 +1,138 @@
|
|||
/*
|
||||
* Copyright (c) 2019 Hemanth Savarala.
|
||||
*
|
||||
* Licensed under the GNU General Public License v3
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
package code.name.monkey.retromusic.dialogs;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import code.name.monkey.retromusic.R;
|
||||
import code.name.monkey.retromusic.activities.saf.SAFGuideActivity;
|
||||
import code.name.monkey.retromusic.misc.DialogAsyncTask;
|
||||
import code.name.monkey.retromusic.model.Song;
|
||||
import code.name.monkey.retromusic.util.SAFUtil;
|
||||
|
||||
/**
|
||||
* Created by hemanths on 2019-07-31.
|
||||
*/
|
||||
public class DeleteSongsAsyncTask extends DialogAsyncTask<DeleteSongsAsyncTask.LoadingInfo, Integer, Void> {
|
||||
private WeakReference<DeleteSongsDialog> dialogReference;
|
||||
private WeakReference<FragmentActivity> activityWeakReference;
|
||||
|
||||
|
||||
public DeleteSongsAsyncTask(@NonNull DeleteSongsDialog dialog) {
|
||||
super(dialog.getActivity());
|
||||
this.dialogReference = new WeakReference<>(dialog);
|
||||
this.activityWeakReference = new WeakReference<>(dialog.getActivity());
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected Dialog createDialog(@NonNull Context context) {
|
||||
return new MaterialAlertDialogBuilder(context)
|
||||
.setTitle(R.string.deleting_songs)
|
||||
.setView(R.layout.loading)
|
||||
.setCancelable(false)
|
||||
.create();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected Void doInBackground(@NonNull LoadingInfo... loadingInfos) {
|
||||
try {
|
||||
LoadingInfo info = loadingInfos[0];
|
||||
DeleteSongsDialog dialog = this.dialogReference.get();
|
||||
FragmentActivity fragmentActivity = this.activityWeakReference.get();
|
||||
|
||||
if (dialog == null || fragmentActivity == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!info.isIntent) {
|
||||
if (!SAFUtil.isSAFRequiredForSongs(info.songs)) {
|
||||
dialog.deleteSongs(info.songs, null);
|
||||
} else {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
if (SAFUtil.isSDCardAccessGranted(fragmentActivity)) {
|
||||
dialog.deleteSongs(info.songs, null);
|
||||
} else {
|
||||
dialog.startActivityForResult(new Intent(fragmentActivity, SAFGuideActivity.class), SAFGuideActivity.REQUEST_CODE_SAF_GUIDE);
|
||||
}
|
||||
} else {
|
||||
Log.i("Hmm", "doInBackground: kitkat delete songs");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
switch (info.requestCode) {
|
||||
case SAFUtil.REQUEST_SAF_PICK_TREE:
|
||||
if (info.resultCode == Activity.RESULT_OK) {
|
||||
SAFUtil.saveTreeUri(fragmentActivity, info.intent);
|
||||
if (dialog.songsToRemove != null) {
|
||||
dialog.deleteSongs(dialog.songsToRemove, null);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SAFUtil.REQUEST_SAF_PICK_FILE:
|
||||
if (info.resultCode == Activity.RESULT_OK) {
|
||||
dialog.deleteSongs(Collections.singletonList(dialog.currentSong), Collections.singletonList(info.intent.getData()));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class LoadingInfo {
|
||||
public boolean isIntent;
|
||||
|
||||
public List<Song> songs;
|
||||
public List<Uri> safUris;
|
||||
|
||||
public int requestCode;
|
||||
public int resultCode;
|
||||
public Intent intent;
|
||||
|
||||
public LoadingInfo(List<Song> songs, List<Uri> safUris) {
|
||||
this.isIntent = false;
|
||||
this.songs = songs;
|
||||
this.safUris = safUris;
|
||||
}
|
||||
|
||||
public LoadingInfo(int requestCode, int resultCode, Intent intent) {
|
||||
this.isIntent = true;
|
||||
this.requestCode = requestCode;
|
||||
this.resultCode = resultCode;
|
||||
this.intent = intent;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -15,41 +15,85 @@
|
|||
package code.name.monkey.retromusic.dialogs
|
||||
|
||||
import android.app.Dialog
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.text.Html
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import code.name.monkey.retromusic.R
|
||||
import code.name.monkey.retromusic.activities.saf.SAFGuideActivity
|
||||
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
||||
import code.name.monkey.retromusic.model.Song
|
||||
import code.name.monkey.retromusic.util.MusicUtil
|
||||
import code.name.monkey.retromusic.util.SAFUtil
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import com.afollestad.materialdialogs.bottomsheets.BottomSheet
|
||||
|
||||
|
||||
class DeleteSongsDialog : DialogFragment() {
|
||||
@JvmField
|
||||
var currentSong: Song? = null
|
||||
@JvmField
|
||||
var songsToRemove: List<Song>? = null
|
||||
|
||||
private var deleteSongsAsyncTask: DeleteSongsAsyncTask? = null
|
||||
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
val songs = arguments!!.getParcelableArrayList<Song>("songs")
|
||||
val title: Int
|
||||
val content: CharSequence
|
||||
if (songs.size > 1) {
|
||||
title = R.string.delete_songs_title
|
||||
content = Html.fromHtml(getString(R.string.delete_x_songs, songs.size))
|
||||
} else {
|
||||
title = R.string.delete_song_title
|
||||
content = Html.fromHtml(getString(R.string.delete_song_x, songs.get(0).title))
|
||||
val songs: ArrayList<Song>? = arguments?.getParcelableArrayList("songs")
|
||||
var title = 0
|
||||
var content: CharSequence = ""
|
||||
if (songs != null) {
|
||||
if (songs.size > 1) {
|
||||
title = R.string.delete_songs_title
|
||||
content = Html.fromHtml(getString(R.string.delete_x_songs, songs.size))
|
||||
} else {
|
||||
title = R.string.delete_song_title
|
||||
content = Html.fromHtml(getString(R.string.delete_song_x, songs[0].title))
|
||||
}
|
||||
}
|
||||
return MaterialDialog(activity!!, BottomSheet()).show {
|
||||
|
||||
return MaterialDialog(requireActivity(), BottomSheet()).show {
|
||||
title(title)
|
||||
message(text = content)
|
||||
negativeButton(android.R.string.cancel)
|
||||
negativeButton(android.R.string.cancel) {
|
||||
dismiss()
|
||||
}
|
||||
noAutoDismiss()
|
||||
positiveButton(R.string.action_delete) {
|
||||
if (activity == null)
|
||||
return@positiveButton
|
||||
MusicUtil.deleteTracks(activity!!, songs);
|
||||
if (songs != null) {
|
||||
if ((songs.size == 1) && MusicPlayerRemote.isPlaying(songs[0])) {
|
||||
MusicPlayerRemote.playNextSong()
|
||||
}
|
||||
}
|
||||
|
||||
songsToRemove = songs
|
||||
deleteSongsAsyncTask = DeleteSongsAsyncTask(this@DeleteSongsDialog)
|
||||
deleteSongsAsyncTask?.execute(DeleteSongsAsyncTask.LoadingInfo(songs, null))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
when (requestCode) {
|
||||
SAFGuideActivity.REQUEST_CODE_SAF_GUIDE -> {
|
||||
SAFUtil.openTreePicker(this)
|
||||
}
|
||||
SAFUtil.REQUEST_SAF_PICK_TREE,
|
||||
SAFUtil.REQUEST_SAF_PICK_FILE -> {
|
||||
if (deleteSongsAsyncTask != null) {
|
||||
deleteSongsAsyncTask?.cancel(true)
|
||||
}
|
||||
deleteSongsAsyncTask = DeleteSongsAsyncTask(this)
|
||||
deleteSongsAsyncTask?.execute(DeleteSongsAsyncTask.LoadingInfo(requestCode, resultCode, data))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteSongs(songs: List<Song>, safUris: List<Uri>?) {
|
||||
MusicUtil.deleteTracks(activity!!, songs, safUris) { this.dismiss() }
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue