Merge branch 'nitehu-api28' into api28

This commit is contained in:
Óscar García Amor 2020-06-18 10:10:57 +02:00
commit a8c7b6765e
8 changed files with 34 additions and 34 deletions

View File

@ -25,7 +25,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/Theme.AppCompat"
android:name=".app.UApp"
android:label="@string/common.appname">
android:label="@string/common.appname"
android:usesCleartextTraffic="true">
<activity
android:name=".activity.MainActivity"
android:configChanges="orientation|keyboardHidden"

View File

@ -265,7 +265,7 @@ public class MainActivity extends SubsonicTabActivity
if (!preferences.contains(Constants.PREFERENCES_KEY_CACHE_LOCATION))
{
final SharedPreferences.Editor editor = preferences.edit();
editor.putString(Constants.PREFERENCES_KEY_CACHE_LOCATION, FileUtil.getDefaultMusicDirectory().getPath());
editor.putString(Constants.PREFERENCES_KEY_CACHE_LOCATION, FileUtil.getDefaultMusicDirectory(this).getPath());
editor.commit();
}
}

View File

@ -347,7 +347,7 @@ public class SettingsFragment extends PreferenceFragment
}
private void setHideMedia(boolean hide) {
File nomediaDir = new File(FileUtil.getUltraSonicDirectory(), ".nomedia");
File nomediaDir = new File(FileUtil.getUltraSonicDirectory(getActivity()), ".nomedia");
if (hide && !nomediaDir.exists()) {
if (!nomediaDir.mkdir()) {
Log.w(LOG_TAG, "Failed to create " + nomediaDir);
@ -385,7 +385,8 @@ public class SettingsFragment extends PreferenceFragment
PermissionUtil.handlePermissionFailed(getActivity(), new PermissionUtil.PermissionRequestFinishedCallback() {
@Override
public void onPermissionRequestFinished() {
String currentPath = settings.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, FileUtil.getDefaultMusicDirectory().getPath());
String currentPath = settings.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION,
FileUtil.getDefaultMusicDirectory(getActivity()).getPath());
cacheLocation.setSummary(currentPath);
cacheLocation.setText(currentPath);
}

View File

@ -528,7 +528,7 @@ public class OfflineMusicService extends RESTMusicService
public List<Playlist> getPlaylists(boolean refresh, Context context, ProgressListener progressListener) throws Exception
{
List<Playlist> playlists = new ArrayList<Playlist>();
File root = FileUtil.getPlaylistDirectory();
File root = FileUtil.getPlaylistDirectory(context);
String lastServer = null;
boolean removeServer = true;
for (File folder : FileUtil.listFiles(root))
@ -601,7 +601,7 @@ public class OfflineMusicService extends RESTMusicService
name = name.substring(id.length() + 2);
}
File playlistFile = FileUtil.getPlaylistFile(id, name);
File playlistFile = FileUtil.getPlaylistFile(context, id, name);
reader = new FileReader(playlistFile);
buffer = new BufferedReader(reader);
@ -632,7 +632,7 @@ public class OfflineMusicService extends RESTMusicService
@Override
public void createPlaylist(String id, String name, List<MusicDirectory.Entry> entries, Context context, ProgressListener progressListener) throws Exception
{
File playlistFile = FileUtil.getPlaylistFile(Util.getServerName(context), name);
File playlistFile = FileUtil.getPlaylistFile(context, Util.getServerName(context), name);
FileWriter fw = new FileWriter(playlistFile);
BufferedWriter bw = new BufferedWriter(fw);
try

View File

@ -388,7 +388,7 @@ public class RESTMusicService implements MusicService {
private void savePlaylist(String name,
Context context,
MusicDirectory playlist) throws IOException {
File playlistFile = FileUtil.getPlaylistFile(Util.getServerName(context), name);
File playlistFile = FileUtil.getPlaylistFile(context, Util.getServerName(context), name);
FileWriter fw = new FileWriter(playlistFile);
BufferedWriter bw = new BufferedWriter(fw);
try {

View File

@ -307,11 +307,11 @@ public class CacheCleaner
{
Thread.currentThread().setName("BackgroundPlaylistsCleanup");
String server = Util.getServerName(context);
SortedSet<File> playlistFiles = FileUtil.listFiles(FileUtil.getPlaylistDirectory(server));
SortedSet<File> playlistFiles = FileUtil.listFiles(FileUtil.getPlaylistDirectory(context, server));
List<Playlist> playlists = params[0];
for (Playlist playlist : playlists)
{
playlistFiles.remove(FileUtil.getPlaylistFile(server, playlist.getName()));
playlistFiles.remove(FileUtil.getPlaylistFile(context, server, playlist.getName()));
}
for (File playlist : playlistFiles)

View File

@ -26,7 +26,6 @@ import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import org.moire.ultrasonic.activity.MainActivity;
import org.moire.ultrasonic.activity.SubsonicTabActivity;
import org.moire.ultrasonic.domain.MusicDirectory;
@ -40,7 +39,6 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Pattern;
@ -56,7 +54,6 @@ public class FileUtil
private static final List<String> MUSIC_FILE_EXTENSIONS = Arrays.asList("mp3", "ogg", "aac", "flac", "m4a", "wav", "wma");
private static final List<String> VIDEO_FILE_EXTENSIONS = Arrays.asList("flv", "mp4", "m4v", "wmv", "avi", "mov", "mpg", "mkv");
private static final List<String> PLAYLIST_FILE_EXTENSIONS = Collections.singletonList("m3u");
private static final File DEFAULT_MUSIC_DIR = createDirectory("music");
private static final Pattern TITLE_WITH_TRACK = Pattern.compile("^\\d\\d-.*");
public static File getSongFile(Context context, MusicDirectory.Entry song)
@ -86,22 +83,22 @@ public class FileUtil
return new File(dir, fileName.toString());
}
public static File getPlaylistFile(String server, String name)
public static File getPlaylistFile(Context context, String server, String name)
{
File playlistDir = getPlaylistDirectory(server);
File playlistDir = getPlaylistDirectory(context, server);
return new File(playlistDir, String.format("%s.m3u", fileSystemSafe(name)));
}
public static File getPlaylistDirectory()
public static File getPlaylistDirectory(Context context)
{
File playlistDir = new File(getUltraSonicDirectory(), "playlists");
File playlistDir = new File(getUltraSonicDirectory(context), "playlists");
ensureDirectoryExistsAndIsReadWritable(playlistDir);
return playlistDir;
}
public static File getPlaylistDirectory(String server)
public static File getPlaylistDirectory(Context context, String server)
{
File playlistDir = new File(getPlaylistDirectory(), server);
File playlistDir = new File(getPlaylistDirectory(context), server);
ensureDirectoryExistsAndIsReadWritable(playlistDir);
return playlistDir;
}
@ -304,7 +301,7 @@ public class FileUtil
public static File getAlbumArtDirectory(Context context)
{
File albumArtDir = new File(getUltraSonicDirectory(), "artwork");
File albumArtDir = new File(getUltraSonicDirectory(context), "artwork");
ensureDirectoryExistsAndIsReadWritable(albumArtDir);
ensureDirectoryExistsAndIsReadWritable(new File(albumArtDir, ".nomedia"));
return albumArtDir;
@ -352,9 +349,9 @@ public class FileUtil
}
}
private static File createDirectory(String name)
private static File getOrCreateDirectory(Context context, String name)
{
File dir = new File(getUltraSonicDirectory(), name);
File dir = new File(getUltraSonicDirectory(context), name);
if (!dir.exists() && !dir.mkdirs())
{
@ -364,29 +361,30 @@ public class FileUtil
return dir;
}
public static File getUltraSonicDirectory()
public static File getUltraSonicDirectory(Context context)
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
return new File(Environment.getExternalStorageDirectory(), "Android/data/org.moire.ultrasonic");
// After Android M, the location of the files must be queried differently. GetExternalFilesDir will always return a directory which Ultrasonic can access without any extra privileges.
return MainActivity.getInstance().getExternalFilesDir(null);
return context.getExternalFilesDir(null);
}
public static File getDefaultMusicDirectory()
public static File getDefaultMusicDirectory(Context context)
{
return DEFAULT_MUSIC_DIR;
return getOrCreateDirectory(context, "music");
}
public static File getMusicDirectory(Context context)
{
String path = Util.getPreferences(context).getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, DEFAULT_MUSIC_DIR.getPath());
File defaultMusicDirectory = getDefaultMusicDirectory(context);
String path = Util.getPreferences(context).getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, defaultMusicDirectory.getPath());
File dir = new File(path);
boolean hasAccess = ensureDirectoryExistsAndIsReadWritable(dir);
if (hasAccess == false) PermissionUtil.handlePermissionFailed(context, null);
return hasAccess ? dir : DEFAULT_MUSIC_DIR;
return hasAccess ? dir : defaultMusicDirectory;
}
public static boolean ensureDirectoryExistsAndIsReadWritable(File dir)

View File

@ -48,8 +48,8 @@ public class PermissionUtil {
*/
public static void handlePermissionFailed(final Context context, final PermissionRequestFinishedCallback callback) {
String currentCachePath = Util.getPreferences(context).getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, FileUtil.getDefaultMusicDirectory().getPath());
String defaultCachePath = FileUtil.getDefaultMusicDirectory().getPath();
String currentCachePath = Util.getPreferences(context).getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, FileUtil.getDefaultMusicDirectory(context).getPath());
String defaultCachePath = FileUtil.getDefaultMusicDirectory(context).getPath();
// Ultrasonic can do nothing about this error when the Music Directory is already set to the default.
if (currentCachePath.compareTo(defaultCachePath) == 0) return;
@ -60,10 +60,10 @@ public class PermissionUtil {
if ((PermissionChecker.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PERMISSION_DENIED) ||
(PermissionChecker.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) == PERMISSION_DENIED)) {
// While we request permission, the Music Directory is temporarily reset to its default location
setCacheLocation(context, FileUtil.getDefaultMusicDirectory().getPath());
setCacheLocation(context, FileUtil.getDefaultMusicDirectory(context).getPath());
requestPermission(mainContext, currentCachePath, callback);
} else {
setCacheLocation(context, FileUtil.getDefaultMusicDirectory().getPath());
setCacheLocation(context, FileUtil.getDefaultMusicDirectory(context).getPath());
showWarning(mainContext, context.getString(R.string.permissions_message_box_title), context.getString(R.string.permissions_access_error), null);
callback.onPermissionRequestFinished();
}
@ -98,7 +98,7 @@ public class PermissionUtil {
}
Log.i(TAG, String.format("At least one permission is missing to use directory %s ", cacheLocation));
setCacheLocation(context, FileUtil.getDefaultMusicDirectory().getPath());
setCacheLocation(context, FileUtil.getDefaultMusicDirectory(context).getPath());
showWarning(context, context.getString(R.string.permissions_message_box_title),
context.getString(R.string.permissions_permission_missing), null);
if (callback != null) callback.onPermissionRequestFinished();
@ -135,7 +135,7 @@ public class PermissionUtil {
builder.setNegativeButton(context.getString(R.string.common_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setCacheLocation(context, FileUtil.getDefaultMusicDirectory().getPath());
setCacheLocation(context, FileUtil.getDefaultMusicDirectory(context).getPath());
dialog.cancel();
}
});