Compare commits

...
Author SHA1 Message Date
gdlbo 27dd93efa3 many fixes 2024-03-30 05:47:39 +03:00
gdlbo f4c7cdce8b init
??
2024-03-30 04:56:38 +03:00
9 changed files with 143 additions and 25 deletions
@@ -246,7 +246,7 @@ public class TracklistInjector {
.put(getCatalogPlaylist()); .put(getCatalogPlaylist());
} }
if (!MusicCacheImpl.getAllOwnTracks().isEmpty()) { if (!PlaylistCacheDbDelegate.isPlaylistEmpty(AndroidUtils.getGlobalContext(), AccountManagerUtils.getUserId() + "_-1")) {
if (MusicCacheImpl.hasPlaylist()) blocks.put(getCatalogSeparator()); if (MusicCacheImpl.hasPlaylist()) blocks.put(getCatalogSeparator());
blocks.put(getCatalogHeader(AndroidUtils.getString(R.string.cached_tracks_title))) blocks.put(getCatalogHeader(AndroidUtils.getString(R.string.cached_tracks_title)))
.put(getShuffleButton()) .put(getShuffleButton())
@@ -59,12 +59,12 @@ public class MusicCacheImpl {
} }
public static boolean hasPlaylist() { public static boolean hasPlaylist() {
return PlaylistCacheDbDelegate.getPlaylistsCount(AndroidUtils.getGlobalContext()) != 0; // has playlists or not return !PlaylistCacheDbDelegate.isPlaylistsDbEmpty(AndroidUtils.getGlobalContext()); // has playlists or not
} }
public static long getTracksCount() { public static long getTracksCount() {
return !LibVKXClient.isIntegrationEnabled() return !LibVKXClient.isIntegrationEnabled()
? MusicCacheDbDelegate.getTracksCount(AndroidUtils.getGlobalContext()) ? PlaylistCacheDbDelegate.getTracksCountInPlaylist(AndroidUtils.getGlobalContext(),AccountManagerUtils.getUserId() + "_-1")
: LibVKXClient.getInstance().runOnServiceSync( : LibVKXClient.getInstance().runOnServiceSync(
new LibVKXClientImpl.LibVKXActionGeneric<Long>() { new LibVKXClientImpl.LibVKXActionGeneric<Long>() {
@Override @Override
@@ -89,7 +89,7 @@ public class MusicCacheImpl {
} }
public static boolean isEmpty() { public static boolean isEmpty() {
return !LibVKXClient.isIntegrationEnabled() && getTracksCount() == 0L; return !LibVKXClient.isIntegrationEnabled() && MusicCacheDbDelegate.isEmpty(AndroidUtils.getGlobalContext());
} }
public static void clear() { public static void clear() {
@@ -174,6 +174,15 @@ public class MusicCacheDb extends SQLiteOpenHelper implements AutoCloseable { //
return DatabaseUtils.queryNumEntries(getReadableDatabase(), Constants.TABLE_NAME); return DatabaseUtils.queryNumEntries(getReadableDatabase(), Constants.TABLE_NAME);
} }
public boolean isDatabaseEmpty() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT EXISTS(SELECT 1 FROM " + Constants.TABLE_NAME + " LIMIT 1)", null);
cursor.moveToFirst();
boolean isEmpty = cursor.getInt(0) == 0;
cursor.close();
return isEmpty;
}
public MusicTrack getTrackById(String trackId) { public MusicTrack getTrackById(String trackId) {
SQLiteDatabase db = getReadableDatabase(); SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(Constants.TABLE_NAME, null, Constants.COLUMN_TRACK_ID + " = ?", new String[]{trackId}, null, null, null); Cursor cursor = db.query(Constants.TABLE_NAME, null, Constants.COLUMN_TRACK_ID + " = ?", new String[]{trackId}, null, null, null);
@@ -53,8 +53,7 @@ public class PlaylistCacheDb extends SQLiteOpenHelper implements AutoCloseable {
Boolean.parseBoolean(cur.getString(cur.getColumnIndex(Constants.COLUMN_IS_EXPLICIT))), Boolean.parseBoolean(cur.getString(cur.getColumnIndex(Constants.COLUMN_IS_EXPLICIT))),
cur.getString(cur.getColumnIndex(Constants.COLUMN_TITLE)), cur.getString(cur.getColumnIndex(Constants.COLUMN_TITLE)),
cur.getString(cur.getColumnIndex(Constants.COLUMN_DESCRIPTION)), cur.getString(cur.getColumnIndex(Constants.COLUMN_DESCRIPTION)),
photo, photo);
0);
Log.d("Playlist", "generated " + Playlist.U.a(playlist).v1()); Log.d("Playlist", "generated " + Playlist.U.a(playlist).v1());
@@ -170,6 +169,50 @@ public class PlaylistCacheDb extends SQLiteOpenHelper implements AutoCloseable {
return playlists; return playlists;
} }
public long getTracksCountInPlaylist(String playlistId) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(
Constants.TABLE_PLAYLIST_TRACKS,
new String[]{Constants.COLUMN_TRACK_ID},
Constants.COLUMN_PLAYLIST_ID + " = ?",
new String[]{playlistId},
null, null, null
);
long count = cursor.getCount();
cursor.close();
return count;
}
public List<String> getAllPlaylistIds() {
SQLiteDatabase db = getReadableDatabase();
List<String> playlistIds = new ArrayList<>();
Cursor cursor = db.query(
Constants.TABLE_NAME,
new String[]{Constants.COLUMN_ID, Constants.COLUMN_OWNER_ID},
null, null, null, null, null
);
if (cursor.moveToFirst()) {
do {
String playlistId = cursor.getString(cursor.getColumnIndexOrThrow(Constants.COLUMN_OWNER_ID)) + "_" + cursor.getString(cursor.getColumnIndexOrThrow(Constants.COLUMN_ID));
playlistIds.add(playlistId);
} while (cursor.moveToNext());
}
cursor.close();
return playlistIds;
}
public boolean isPlaylistsDbEmpty() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT EXISTS(SELECT 1 FROM " + Constants.TABLE_NAME + " LIMIT 1)", null);
cursor.moveToFirst();
boolean isEmpty = cursor.getInt(0) == 0;
cursor.close();
return isEmpty;
}
public void addTrackToPlaylist(String playlistId, String trackId) { public void addTrackToPlaylist(String playlistId, String trackId) {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(Constants.COLUMN_PLAYLIST_ID, playlistId); values.put(Constants.COLUMN_PLAYLIST_ID, playlistId);
@@ -181,6 +224,15 @@ public class PlaylistCacheDb extends SQLiteOpenHelper implements AutoCloseable {
getWritableDatabase().delete(Constants.TABLE_PLAYLIST_TRACKS, Constants.COLUMN_PLAYLIST_ID + " = ? AND " + Constants.COLUMN_TRACK_ID + " = ?", new String[]{playlistId, trackId}); getWritableDatabase().delete(Constants.TABLE_PLAYLIST_TRACKS, Constants.COLUMN_PLAYLIST_ID + " = ? AND " + Constants.COLUMN_TRACK_ID + " = ?", new String[]{playlistId, trackId});
} }
public boolean isPlaylistEmpty(String playlistId) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT EXISTS(SELECT 1 FROM " + Constants.TABLE_PLAYLIST_TRACKS + " WHERE " + Constants.COLUMN_PLAYLIST_ID + " = ? LIMIT 1)", new String[]{playlistId});
cursor.moveToFirst();
boolean isEmpty = cursor.getInt(0) == 0;
cursor.close();
return isEmpty;
}
public List<MusicTrack> getTracksInPlaylist(String playlistId) { public List<MusicTrack> getTracksInPlaylist(String playlistId) {
SQLiteDatabase db = getReadableDatabase(); SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query( Cursor cursor = db.query(
@@ -55,6 +55,12 @@ public class MusicCacheDbDelegate {
} }
} }
public static boolean isEmpty(Context context) {
try (var db = connectToDb(context)) {
return db.isDatabaseEmpty();
}
}
public static boolean isCachedTrack(Context context, String trackId) { public static boolean isCachedTrack(Context context, String trackId) {
try (var db = connectToDb(context)) { try (var db = connectToDb(context)) {
return db.isCachedTrack(trackId); return db.isCachedTrack(trackId);
@@ -62,6 +62,31 @@ public class PlaylistCacheDbDelegate {
} }
} }
public static long getTracksCountInPlaylist(Context context, String playlistId) {
try (var db = connectToDb(context)) {
return db.getTracksCountInPlaylist(playlistId);
}
}
public static boolean isPlaylistsDbEmpty(Context context) {
try (var db = connectToDb(context)) {
return db.isPlaylistsDbEmpty();
}
}
public static void removeAllPlaylists(Context context) {
List<String> playlistIds = getAllPlaylistIds(context);
for (String playlistId : playlistIds) {
deletePlaylist(context, playlistId);
}
}
public static List<String> getAllPlaylistIds(Context context) {
try (var db = connectToDb(context)) {
return db.getAllPlaylistIds();
}
}
public static boolean isCachedPlaylist(Context context, String id) { public static boolean isCachedPlaylist(Context context, String id) {
try (var db = connectToDb(context)) { try (var db = connectToDb(context)) {
String[] parts = id.split("_"); String[] parts = id.split("_");
@@ -112,6 +137,12 @@ public class PlaylistCacheDbDelegate {
} }
} }
public static boolean isPlaylistEmpty(Context context, String playlistId) {
try (var db = connectToDb(context)) {
return db.isPlaylistEmpty(playlistId);
}
}
public static void drop(Context context) { public static void drop(Context context) {
context.deleteDatabase(PlaylistCacheDb.Constants.DB_NAME); context.deleteDatabase(PlaylistCacheDb.Constants.DB_NAME);
} }
@@ -7,6 +7,7 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import ru.vtosters.lite.music.cache.MusicCacheImpl; import ru.vtosters.lite.music.cache.MusicCacheImpl;
import ru.vtosters.lite.music.cache.delegate.PlaylistCacheDbDelegate;
import ru.vtosters.lite.utils.AccountManagerUtils; import ru.vtosters.lite.utils.AccountManagerUtils;
import ru.vtosters.lite.utils.AndroidUtils; import ru.vtosters.lite.utils.AndroidUtils;
import ru.vtosters.lite.utils.NetworkUtils; import ru.vtosters.lite.utils.NetworkUtils;
@@ -25,19 +26,19 @@ public class PlaylistHelper {
public static JSONArray addCachedPlaylists(JSONArray jsonArray, Boolean noPlaylists) { public static JSONArray addCachedPlaylists(JSONArray jsonArray, Boolean noPlaylists) {
try { try {
if (noPlaylists) { if (noPlaylists) {
jsonArray.put(new JSONArray().put(getCachedSongsPlaylist())); jsonArray.put(new JSONArray());
} else { }
if (!PlaylistCacheDbDelegate.isPlaylistEmpty(AndroidUtils.getGlobalContext(), AccountManagerUtils.getUserId() + "_-1")) {
jsonArray.put(getCachedSongsPlaylist()); jsonArray.put(getCachedSongsPlaylist());
} }
for (Playlist playlist : MusicCacheImpl.getPlaylists()) { if (MusicCacheImpl.hasPlaylist()) {
if (noPlaylists) { for (Playlist playlist : MusicCacheImpl.getPlaylists()) {
jsonArray.put(new JSONArray().put(generatePlaylist(playlist.a, playlist.b, playlist.C, playlist.g, playlist.B, PlaylistUtils.getThumb(playlist), playlist.O))); jsonArray.put(generatePlaylist(playlist.a, playlist.b, playlist.C, playlist.g, playlist.B, PlaylistUtils.getThumb(playlist)));
} else {
jsonArray.put(generatePlaylist(playlist.a, playlist.b, playlist.C, playlist.g, playlist.B, PlaylistUtils.getThumb(playlist), playlist.O));
}
Log.d("PlaylistHelper", "Playlist cache added: " + playlist.a + " " + playlist.b + " " + playlist.C + " " + playlist.g + " " + playlist.B); Log.d("PlaylistHelper", "Playlist cache added: " + playlist.a + " " + playlist.b + " " + playlist.C + " " + playlist.g + " " + playlist.B);
}
} }
} catch (JSONException e) { } catch (JSONException e) {
e.fillInStackTrace(); e.fillInStackTrace();
@@ -45,7 +46,7 @@ public class PlaylistHelper {
return jsonArray; return jsonArray;
} }
public static JSONObject generatePlaylist(int id, int owner_id, boolean is_explicit, String title, String description, JSONObject photo, int count) throws JSONException { public static JSONObject generatePlaylist(int id, int owner_id, boolean is_explicit, String title, String description, JSONObject photo) throws JSONException {
JSONObject playlist = new JSONObject() JSONObject playlist = new JSONObject()
.put("id", id) .put("id", id)
.put("owner_id", owner_id) .put("owner_id", owner_id)
@@ -64,7 +65,7 @@ public class PlaylistHelper {
.put("subtitle", "") .put("subtitle", "")
.put("meta", new JSONObject() .put("meta", new JSONObject()
.put("view", "compact")) .put("view", "compact"))
.put("count", count); .put("count", 0);
if (photo != null) { if (photo != null) {
playlist.put("photo", photo); playlist.put("photo", photo);
@@ -101,18 +102,18 @@ public class PlaylistHelper {
.put("height", 600)) .put("height", 600))
.put("meta", new JSONObject() .put("meta", new JSONObject()
.put("view", "compact")) .put("view", "compact"))
.put("count", MusicCacheImpl.getAllOwnTracks().size()); .put("count", 0);
} }
public static JSONArray getCachedPlaylistsIds() { public static JSONArray getCachedPlaylistsIds() {
JSONArray arr = new JSONArray(); JSONArray arr = new JSONArray();
if (NetworkUtils.isNetworkConnected() && !MusicCacheImpl.getAllOwnTracks().isEmpty()) { if (NetworkUtils.isNetworkConnected() && !PlaylistCacheDbDelegate.isPlaylistEmpty(AndroidUtils.getGlobalContext(), AccountManagerUtils.getUserId() + "_-1")) {
arr.put(AccountManagerUtils.getUserId() + "_-1"); arr.put(AccountManagerUtils.getUserId() + "_-1");
} }
for (var playlist : MusicCacheImpl.getPlaylists()) { for (String playlist : PlaylistCacheDbDelegate.getAllPlaylistIds(AndroidUtils.getGlobalContext())) {
arr.put(playlist.v1()); arr.put(playlist);
} }
return arr; return arr;
@@ -28,7 +28,7 @@ public class TracklistHelper {
boolean doNotinvertOrder = Preferences.getBoolValue("invertCachedTracks", false); boolean doNotinvertOrder = Preferences.getBoolValue("invertCachedTracks", false);
if (!doNotinvertOrder) { if (doNotinvertOrder) {
Collections.reverse(tracks); Collections.reverse(tracks);
} }
@@ -20,6 +20,7 @@ import ru.vtosters.lite.music.LastFMScrobbler;
import ru.vtosters.lite.music.cache.MusicCacheImpl; import ru.vtosters.lite.music.cache.MusicCacheImpl;
import ru.vtosters.lite.music.cache.delegate.PlaylistCacheDbDelegate; import ru.vtosters.lite.music.cache.delegate.PlaylistCacheDbDelegate;
import ru.vtosters.lite.ui.PreferenceFragmentUtils; import ru.vtosters.lite.ui.PreferenceFragmentUtils;
import ru.vtosters.lite.utils.AccountManagerUtils;
import ru.vtosters.lite.utils.AndroidUtils; import ru.vtosters.lite.utils.AndroidUtils;
import ru.vtosters.lite.utils.LifecycleUtils; import ru.vtosters.lite.utils.LifecycleUtils;
@@ -55,7 +56,7 @@ public class MusicFragment extends TrackedMaterialPreferenceToolbarFragment {
if (MusicCacheImpl.isEmpty()) { if (MusicCacheImpl.isEmpty()) {
AndroidUtils.sendToast(requireContext().getString(com.vtosters.lite.R.string.no_cache_error)); AndroidUtils.sendToast(requireContext().getString(com.vtosters.lite.R.string.no_cache_error));
} else { } else {
delcache(requireContext()); delcache(requireContext(), false);
} }
return true; return true;
} }
@@ -73,6 +74,18 @@ public class MusicFragment extends TrackedMaterialPreferenceToolbarFragment {
return true; return true;
} }
); );
PreferenceFragmentUtils.addPreference(
getPreferenceScreen(),
"cached_playlists",
"Удалить все скачанные плейлисты",
null,
null,
preference -> {
delcache(requireContext(), true);
return true;
}
);
} }
PreferenceFragmentUtils.addPreference( PreferenceFragmentUtils.addPreference(
@@ -443,12 +456,18 @@ public class MusicFragment extends TrackedMaterialPreferenceToolbarFragment {
builder.show(); builder.show();
} }
private void delcache(Context ctx) { private void delcache(Context ctx, boolean isPlaylists) {
new VkAlertDialog.Builder(ctx) new VkAlertDialog.Builder(ctx)
.setTitle(com.vtosters.lite.R.string.warning) .setTitle(com.vtosters.lite.R.string.warning)
.setMessage(com.vtosters.lite.R.string.cached_tracks_remove_confirm) .setMessage(com.vtosters.lite.R.string.cached_tracks_remove_confirm)
.setPositiveButton(com.vtosters.lite.R.string.yes, (dialog, which) -> { .setPositiveButton(com.vtosters.lite.R.string.yes, (dialog, which) -> {
executor.submit(MusicCacheImpl::clear); executor.submit(() -> {
if (isPlaylists) {
PlaylistCacheDbDelegate.removeAllPlaylists(ctx);
} else {
PlaylistCacheDbDelegate.deletePlaylist(ctx, AccountManagerUtils.getUserId() + "_-1");
}
});
findPreference("cached_tracks").setSummary(String.format(requireContext().getString(com.vtosters.lite.R.string.cached_tracks_counter), MusicCacheImpl.getTracksCount())); findPreference("cached_tracks").setSummary(String.format(requireContext().getString(com.vtosters.lite.R.string.cached_tracks_counter), MusicCacheImpl.getTracksCount()));
}) })
.setNeutralButton(com.vtosters.lite.R.string.no, (dialog, which) -> dialog.cancel()) .setNeutralButton(com.vtosters.lite.R.string.no, (dialog, which) -> dialog.cancel())