diff --git a/app/src/main/java/ru/vtosters/lite/hooks/JsonInjectors.java b/app/src/main/java/ru/vtosters/lite/hooks/JsonInjectors.java index 208a7566e..053ad9640 100644 --- a/app/src/main/java/ru/vtosters/lite/hooks/JsonInjectors.java +++ b/app/src/main/java/ru/vtosters/lite/hooks/JsonInjectors.java @@ -247,7 +247,7 @@ public class JsonInjectors { public static JSONObject musiclink(JSONObject json) { var links = json.optJSONArray("links"); - if (links != null) { + if (links != null && !getBoolValue("useOldAppVer", false)) { if (links.optJSONObject(0).optString("url").contains("?section=recent")) { json.remove("links"); @@ -276,6 +276,69 @@ public class JsonInjectors { } } } + + return catalogInjector(json); + } + + public static JSONObject catalogInjector(JSONObject json) { + + try { + var section = json.optJSONObject("section"); + var useOldAppVer = getBoolValue("useOldAppVer", false); + var isUsersCatalog = section.optString("url").contains("https://vk.com/audios" + getUserId() + "?section=" + (useOldAppVer ? "all" : "general")); + + if (isUsersCatalog && CacheDatabaseDelegate.hasTracks() && !LibVKXClient.isIntegrationEnabled()) { + try { + json.optJSONArray("playlists").put(getPlaylist()); + } catch (Exception e) { + Log.d("catalogInjector", e.toString()); + } + + var blocks = section.getJSONArray("blocks"); + + if (!useOldAppVer) { + var newBlocks = new JSONArray(); + + newBlocks + .put(getCatalogHeader()) + .put(getCatalogPlaylist()) + .put(getCatalogSeparator()); + + Log.d("catalogInjector", "added cache catalog playlist"); + + for (int i = 0; i < blocks.length(); i++) { + newBlocks.put(blocks.optJSONObject(i)); + } + + section.put("blocks", newBlocks); + } else { + for (int i = 0; i < blocks.length(); i++) { + var j = blocks.optJSONObject(i); + var type = j.optString("data_type"); + + if (type.equals("music_playlists") && j.has("playlists_ids")) { + var newarr = new JSONArray(); + var playlists_ids = j.optJSONArray("playlists_ids"); + + newarr.put(getUserId() + "_-1"); + + for (int n = 0; n < playlists_ids.length(); n++) { + newarr.put(playlists_ids.optString(n)); + } + + Log.d("catalogInjector", "added to pl ids"); + + j.put("playlists_ids", newarr); + break; + } + } + } + } + + } catch (JSONException e) { + Log.d("catalogInjector", e.toString()); + } + return json; } @@ -401,32 +464,45 @@ public class JsonInjectors { } if (oldItems != null && oldItems.optJSONObject(0).optString("url").contains("general")) { - json.optJSONArray("playlists").put(getPlaylist()); - var blocks = oldItems.optJSONObject(0).optJSONArray("blocks"); - if (blocks != null) { - for (int i = 0; i < blocks.length(); i++) { - var block = blocks.optJSONObject(i); - var layout = block.optJSONObject("layout"); - if (layout != null) { - var name = layout.optString("name"); - if (name.equals("header_extended")) { - if (layout.has("top_title")) blocks.remove(i); - try { - layout.put("name", "header"); - } catch (JSONException e) { - e.printStackTrace(); + + if (!getBoolValue("useOldAppVer", false)) { + if (blocks != null) { + for (int i = 0; i < blocks.length(); i++) { + var block = blocks.optJSONObject(i); + var layout = block.optJSONObject("layout"); + if (layout != null) { + var name = layout.optString("name"); + if (name.equals("header_extended")) { + if (layout.has("top_title")) blocks.remove(i); + try { + layout.put("name", "header"); + } catch (JSONException e) { + e.printStackTrace(); + } } } } } - } + if (podcastcatalog()) { + var podcasts = fetchCatalogPodcast(); + if (podcasts != null) { + var catalogarr = podcasts.optJSONObject("catalog").optJSONArray("sections").optJSONObject(0); - if (podcastcatalog()) { - var podcasts = fetchCatalogPodcast(); - if (podcasts != null) { - var catalogarr = podcasts.optJSONObject("catalog").optJSONArray("sections").optJSONObject(0); + var title = catalogarr.optString("title"); + var id = catalogarr.optString("id"); + var url = catalogarr.optString("url"); + + if (dev()) Log.d("VKMusic", "Added " + title + " in music sections"); + + oldItems.put(new JSONObject().put("id", id).put("title", title).put("url", url)); + } + } + + var playlists = fetchCatalogId("https://vk.com/audio?section=my_playlists"); + if (playlists != null) { + var catalogarr = playlists.optJSONObject("catalog").optJSONArray("sections").optJSONObject(0); var title = catalogarr.optString("title"); var id = catalogarr.optString("id"); @@ -436,100 +512,67 @@ public class JsonInjectors { oldItems.put(new JSONObject().put("id", id).put("title", title).put("url", url)); } - } - var playlists = fetchCatalogId("https://vk.com/audio?section=my_playlists"); - if (playlists != null) { - var catalogarr = playlists.optJSONObject("catalog").optJSONArray("sections").optJSONObject(0); + var albums = fetchCatalogId("https://vk.com/audio?section=albums"); + if (albums != null) { + var catalogarr = albums.optJSONObject("catalog").optJSONArray("sections").optJSONObject(0); - var title = catalogarr.optString("title"); - var id = catalogarr.optString("id"); - var url = catalogarr.optString("url"); + var title = catalogarr.optString("title"); + var id = catalogarr.optString("id"); + var url = catalogarr.optString("url"); - if (dev()) Log.d("VKMusic", "Added " + title + " in music sections"); + if (dev()) Log.d("VKMusic", "Added " + title + " in music sections"); - oldItems.put(new JSONObject().put("id", id).put("title", title).put("url", url)); - } + oldItems.put(new JSONObject().put("id", id).put("title", title).put("url", url)); + } - var albums = fetchCatalogId("https://vk.com/audio?section=albums"); - if (albums != null) { - var catalogarr = albums.optJSONObject("catalog").optJSONArray("sections").optJSONObject(0); + var recent = fetchCatalogId("https://vk.com/audio?section=recent"); + if (recent != null) { + var catalogarr = recent.optJSONObject("catalog").optJSONArray("sections").optJSONObject(0); - var title = catalogarr.optString("title"); - var id = catalogarr.optString("id"); - var url = catalogarr.optString("url"); + var title = catalogarr.optString("title"); + var id = catalogarr.optString("id"); + var url = catalogarr.optString("url"); - if (dev()) Log.d("VKMusic", "Added " + title + " in music sections"); + if (dev()) Log.d("VKMusic", "Added " + title + " in music sections"); - oldItems.put(new JSONObject().put("id", id).put("title", title).put("url", url)); - } + oldItems.put(new JSONObject().put("id", id).put("title", title).put("url", url)); + } - var recent = fetchCatalogId("https://vk.com/audio?section=recent"); - if (recent != null) { - var catalogarr = recent.optJSONObject("catalog").optJSONArray("sections").optJSONObject(0); + for (int i = 0; i < oldItems.length(); i++) { + var item = oldItems.optJSONObject(i); + var title = item.optString("title"); + var id = item.optString("id"); + var url = item.optString("url"); + var value = AndroidUtils.getPrefsValue("musicdefcatalog"); - var title = catalogarr.optString("title"); - var id = catalogarr.optString("id"); - var url = catalogarr.optString("url"); + Log.d("VKMusic", "title: " + title + " id: " + id + " url: " + url + " value: " + value); - if (dev()) Log.d("VKMusic", "Added " + title + " in music sections"); - - oldItems.put(new JSONObject().put("id", id).put("title", title).put("url", url)); - } - - for (int i = 0; i < oldItems.length(); i++) { - var item = oldItems.optJSONObject(i); - var title = item.optString("title"); - var id = item.optString("id"); - var url = item.optString("url"); - var value = AndroidUtils.getPrefsValue("musicdefcatalog"); - - Log.d("VKMusic", "title: " + title + " id: " + id + " url: " + url + " value: " + value); - - if (url.contains(value) && !value.isEmpty()) { - catalog.put("default_section", id); - if (dev()) Log.d("VKMusic", "Added " + title + " as default music section"); + if (url.contains(value) && !value.isEmpty()) { + catalog.put("default_section", id); + if (dev()) Log.d("VKMusic", "Added " + title + " as default music section"); + } } } if (CacheDatabaseDelegate.hasTracks() && !LibVKXClient.isIntegrationEnabled()) { // inj in playlist list - if (getBoolValue("useOldAppVer", false)) { - for (int i = 0; i < blocks.length(); i++) { - var j = blocks.optJSONObject(i); - var type = j.optString("data_type"); + json.optJSONArray("playlists").put(getPlaylist()); - if (type.equals("music_playlists") && j.has("playlists_ids")) { - var newarr = new JSONArray(); - var playlists_ids = j.optJSONArray("playlists_ids"); - - newarr.put(getUserId() + "_-1"); - - for (int n = 0; n < playlists_ids.length(); n++) { - newarr.put(playlists_ids.optString(n)); - } - - j.put("playlists_ids", newarr); - break; - } - } - - } else { + if (!getBoolValue("useOldAppVer", false)) { var newBlocks = new JSONArray(); - if (blocks != null) { - newBlocks - .put(getCatalogHeader()) - .put(getCatalogPlaylist()) - .put(getCatalogSeparator()); + newBlocks + .put(getCatalogHeader()) + .put(getCatalogPlaylist()) + .put(getCatalogSeparator()); - Log.d("VKMusic", "added cache catalog playlist"); + Log.d("VKMusic", "added cache catalog playlist"); - for (int i = 0; i < blocks.length(); i++) { - newBlocks.put(blocks.optJSONObject(i)); - } - - oldItems.optJSONObject(0).put("blocks", newBlocks); + for (int i = 0; i < blocks.length(); i++) { + newBlocks.put(blocks.optJSONObject(i)); } + + oldItems.optJSONObject(0).put("blocks", newBlocks); } } } diff --git a/app/src/main/java/ru/vtosters/lite/music/cache/injectors/TracklistInjector.java b/app/src/main/java/ru/vtosters/lite/music/cache/injectors/TracklistInjector.java index 3e673120b..88fb830af 100644 --- a/app/src/main/java/ru/vtosters/lite/music/cache/injectors/TracklistInjector.java +++ b/app/src/main/java/ru/vtosters/lite/music/cache/injectors/TracklistInjector.java @@ -1,6 +1,8 @@ package ru.vtosters.lite.music.cache.injectors; import static ru.vtosters.lite.music.cache.CacheDatabaseDelegate.hasTracks; +import static ru.vtosters.lite.music.cache.helpers.PlaylistHelper.getPlaylist; +import static ru.vtosters.lite.utils.AccountManagerUtils.getUserId; import android.util.Log; @@ -20,9 +22,8 @@ import java.util.UUID; import bruhcollective.itaysonlab.libvkx.client.LibVKXClient; import io.reactivex.Observable; import io.reactivex.ObservableOnSubscribe; -import ru.vtosters.lite.music.cache.helpers.PlaylistHelper; +import ru.vtosters.lite.music.cache.CacheDatabaseDelegate; import ru.vtosters.lite.music.cache.helpers.TracklistHelper; -import ru.vtosters.lite.utils.AccountManagerUtils; import ru.vtosters.lite.utils.AndroidUtils; import ru.vtosters.lite.utils.NetworkUtils; @@ -76,76 +77,40 @@ public class TracklistInjector { public static void injectIntoExistingCatalog(JSONObject catalogNode) { Log.d("TracklistInjector", "injectIntoExistingCatalog"); - if (!hasTracks()) return; try { - var sectionsNode = catalogNode.getJSONObject("catalog") - .getJSONArray("sections"); + var catalog = catalogNode.getJSONObject("catalog"); + var sectionsNode = catalog.getJSONArray("sections"); var firstSection = sectionsNode.getJSONObject(0); - if (!firstSection.getString("url").equals("https://vk.com/audios" + AccountManagerUtils.getUserId() + "?section=all")) - return; - var foundPlaylistBlock = false; - var suitableIndex = 0; + if (!firstSection.getString("url").equals("https://vk.com/audios" + getUserId() + "?section=all")) + return; var blocks = firstSection.getJSONArray("blocks"); - for (int i = 0; i < blocks.length(); i++) { - var block = blocks.getJSONObject(i); - var dataType = block.getString("data_type"); - if ("music_audios".equals(dataType)) { - suitableIndex = i - 1; - continue; + + if (CacheDatabaseDelegate.hasTracks() && !LibVKXClient.isIntegrationEnabled()) { // inj in playlist list + catalogNode.optJSONArray("playlists").put(getPlaylist()); + + for (int i = 0; i < blocks.length(); i++) { + var j = blocks.optJSONObject(i); + var type = j.optString("data_type"); + + if (type.equals("music_playlists") && j.has("playlists_ids")) { + var newarr = new JSONArray(); + var playlists_ids = j.optJSONArray("playlists_ids"); + + newarr.put(getUserId() + "_-1"); + + for (int n = 0; n < playlists_ids.length(); n++) { + newarr.put(playlists_ids.optString(n)); + } + + j.put("playlists_ids", newarr); + break; + } } - - if (!"music_playlists".equals(dataType)) - continue; - - var playlistIds = block.getJSONArray("playlists_ids"); - var newPlaylistIds = new JSONArray() - .put(AccountManagerUtils.getUserId() + "_-1"); - - for (int j = 0; j < playlistIds.length(); j++) - newPlaylistIds.put(playlistIds.get(j)); - - block.put("playlists_ids", newPlaylistIds); - - // need test - blocks.remove(i); - blocks.put(i, block); - - foundPlaylistBlock = true; } - - if (foundPlaylistBlock) { - var playlists = catalogNode.getJSONArray("playlists") - .put(0, PlaylistHelper.createCachedPlaylistMetadata().J()); - catalogNode.put("playlists", playlists); - return; - } - - var ids = new JSONArray() - .put(0, AccountManagerUtils.getUserId() + "_-1"); - - var playlistsBlock = new JSONObject() - .put("id", getRandomId()) - .put("data_type", "music_playlists") - .put("url", "synth:cache:playlists") - .put("playlists_ids", ids); - - var layout = new JSONObject() - .put("name", "large_slider") - .put("owner_id", AccountManagerUtils.getUserId()); - - playlistsBlock.put("layout", layout); - - blocks.put(suitableIndex, playlistsBlock); - - firstSection.put("blocks", blocks); - - var playlists = new JSONArray() - .put(0, PlaylistHelper.createCachedPlaylistMetadata().J()); - catalogNode.put("playlists", playlists); - } catch (JSONException e) { - e.printStackTrace(); + } catch (Exception e) { + Log.d("TracklistInjector", e.toString()); } } @@ -175,7 +140,7 @@ public class TracklistInjector { var layout = new JSONObject() .put("name", "list") - .put("owner_id", AccountManagerUtils.getUserId()); + .put("owner_id", getUserId()); audiosBlock.put("layout", layout);