From 899554a5718c33a0742475c0e7613e2707f768a9 Mon Sep 17 00:00:00 2001 From: Artur Skubei <41114720+gdlbo@users.noreply.github.com> Date: Tue, 26 Mar 2024 17:36:34 +0300 Subject: [PATCH] Music autocaching v2 (#1591) --- .../java/com/vk/libvideo/VideoTracker.java | 4 +- .../vk/music/stats/VKMusicStatsTracker.java | 3 +- .../ru/vtosters/hooks/other/Preferences.java | 4 +- .../vtosters/lite/music/LastFMScrobbler.java | 10 +++- .../lite/ui/fragments/MusicFragment.java | 53 +++++++++++++++---- .../java/ru/vtosters/lite/utils/Metrics.java | 22 ++++++-- 6 files changed, 72 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/vk/libvideo/VideoTracker.java b/app/src/main/java/com/vk/libvideo/VideoTracker.java index a7c24ba90..8ab1cf5c4 100644 --- a/app/src/main/java/com/vk/libvideo/VideoTracker.java +++ b/app/src/main/java/com/vk/libvideo/VideoTracker.java @@ -2,7 +2,6 @@ import android.os.Bundle; import android.text.TextUtils; -import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; @@ -11,7 +10,6 @@ import com.vk.media.player.PlayerTypes; import com.vk.navigation.NavigatorKeys; import com.vk.statistic.Statistic; -import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import ru.vtosters.hooks.other.Preferences; @@ -333,6 +331,6 @@ public void sendMetrics(String event) throws JSONException { jsonObject.put("position_sec", 0); jsonObject.put("cur_quality", "auto"); - Metrics.trackEvents(jsonObject); + Metrics.trackEvents(jsonObject, false); } } \ No newline at end of file diff --git a/app/src/main/java/com/vk/music/stats/VKMusicStatsTracker.java b/app/src/main/java/com/vk/music/stats/VKMusicStatsTracker.java index 496da1af6..423c7c7a8 100644 --- a/app/src/main/java/com/vk/music/stats/VKMusicStatsTracker.java +++ b/app/src/main/java/com/vk/music/stats/VKMusicStatsTracker.java @@ -11,7 +11,6 @@ import com.vk.navigation.NavigatorKeys; import com.vtosters.lite.data.Analytics; import kotlin.jvm.b.Functions2; -import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import ru.vtosters.hooks.other.Preferences; @@ -185,7 +184,7 @@ private void makeMetricsRequest(MusicPlaybackParams musicPlaybackParams, String } } - Metrics.trackEvents(event); + Metrics.trackEvents(event, true); } private Analytics.l a(MusicPlaybackParams musicPlaybackParams, String str) { diff --git a/app/src/main/java/ru/vtosters/hooks/other/Preferences.java b/app/src/main/java/ru/vtosters/hooks/other/Preferences.java index 4e826dc68..0e2a48134 100644 --- a/app/src/main/java/ru/vtosters/hooks/other/Preferences.java +++ b/app/src/main/java/ru/vtosters/hooks/other/Preferences.java @@ -100,8 +100,8 @@ public static boolean vkme() { return getBoolValue("vkme", false); } - public static boolean autocache() { - return getBoolValue("autocache", false); + public static int autocache() { + return Preferences.getPreferences().getInt("autocaching", 0); } public static boolean adsstories() { diff --git a/app/src/main/java/ru/vtosters/lite/music/LastFMScrobbler.java b/app/src/main/java/ru/vtosters/lite/music/LastFMScrobbler.java index 27738ca29..272c00cef 100644 --- a/app/src/main/java/ru/vtosters/lite/music/LastFMScrobbler.java +++ b/app/src/main/java/ru/vtosters/lite/music/LastFMScrobbler.java @@ -30,6 +30,7 @@ import ru.vtosters.lite.di.singleton.VtOkHttpClient; import ru.vtosters.lite.downloaders.AudioDownloader; import ru.vtosters.lite.music.cache.MusicCacheImpl; +import ru.vtosters.lite.utils.AccountManagerUtils; import ru.vtosters.lite.utils.AndroidUtils; import ru.vtosters.lite.utils.music.MusicTrackUtils; @@ -50,13 +51,14 @@ public static void grabMusicTrack(MusicTrack musictrack) { var access_key = !TextUtils.isEmpty(musictrack.J) ? musictrack.J : ""; var album_id = musictrack.I; var album = album_id != null ? album_id.getTitle() : null; + var isOwnTrack = musictrack.e == AccountManagerUtils.getUserId(); if (TextUtils.isEmpty(uid) || TextUtils.isEmpty(artist) || TextUtils.isEmpty(title) || duration == 0) { Log.d("Scrobbler", "grabTrackInfo: " + "Empty track, info: " + artist + " - " + title + " - " + duration + " - " + uid); return; } - if (Preferences.autocache() && !MusicCacheImpl.isCachedTrack(uid)) { + if (needToCache(isOwnTrack) && !MusicCacheImpl.isCachedTrack(uid)) { if (LibVKXClient.isIntegrationEnabled()) { LibVKXClient.getInstance().runOnService(service -> { try { @@ -73,6 +75,12 @@ public static void grabMusicTrack(MusicTrack musictrack) { scrobbleTrack(duration, artist, title, uid, album); } + private static boolean needToCache(boolean isOwnTrack) { + int autocache = Preferences.autocache(); + + return autocache == 2 || (autocache == 1 && isOwnTrack); + } + public static void scrobbleTrack( long duration, String artist, diff --git a/app/src/main/java/ru/vtosters/lite/ui/fragments/MusicFragment.java b/app/src/main/java/ru/vtosters/lite/ui/fragments/MusicFragment.java index 4b80ad625..cd05e0fbf 100644 --- a/app/src/main/java/ru/vtosters/lite/ui/fragments/MusicFragment.java +++ b/app/src/main/java/ru/vtosters/lite/ui/fragments/MusicFragment.java @@ -4,6 +4,7 @@ import android.content.Context; import android.os.Bundle; import android.view.ViewGroup; +import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.LinearLayout; import bruhcollective.itaysonlab.libvkx.client.LibVKXClient; @@ -20,6 +21,7 @@ import ru.vtosters.lite.utils.AndroidUtils; import ru.vtosters.lite.utils.LifecycleUtils; +import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -93,15 +95,31 @@ public void onCreate(Bundle bundle) { } ).setEnabled(!MusicCacheImpl.isEmpty()); - PreferenceFragmentUtils.addMaterialSwitchPreference( + PreferenceFragmentUtils.addPreference( getPreferenceScreen(), - "autocache", + "autocache_params", getString(com.vtosters.lite.R.string.autocache_title), - getString(com.vtosters.lite.R.string.autocache_summ), + getAutocacheSumm(), null, - false, - (preference, o) -> { - Preferences.getPreferences().edit().putBoolean("autocache", (boolean) o).apply(); + preference -> { + List items = Arrays.asList("Не кешировать", "Только свои", "Все"); + + ArrayAdapter adapter = new ArrayAdapter<>(requireContext(), android.R.layout.simple_list_item_1, items); + + int selectedItem = Preferences.getPreferences().getInt("autocaching", 0); + if (selectedItem >= 0 && selectedItem < items.size()) { + adapter.getItem(selectedItem); + } + + new VkAlertDialog.Builder(getActivity()) + .setAdapter(adapter, (dialog, which) -> { + adapter.getItem(which); + Preferences.getPreferences().edit().putInt("autocaching", which).apply(); + findPreference("autocache_params").setSummary(getAutocacheSumm()); + dialog.cancel(); + }) + .show(); + return true; } ); @@ -166,6 +184,7 @@ public void onCreate(Bundle bundle) { return; } Preferences.setMetadataSeparator(separator.getText().toString()); + findPreference("metadataSeparator").setSummary(separator.getText().toString()); }) .setNegativeButton("Отмена", (dialog, which) -> dialog.cancel()) .show(); @@ -334,6 +353,15 @@ private void lastfmAuth(Context ctx) { .show(); } + private static String getAutocacheSumm() { + int autocache = Preferences.autocache(); + return switch (autocache) { + case 1 -> "Только для своих"; + case 2 -> "Кешировать всё"; + default -> "Отключено"; + }; + } + public void updateLastFmPref() { if (LastFMScrobbler.isLoggedIn()) { findPreference("lastfm_auth").setSummary(getString(com.vtosters.lite.R.string.lastfm_authorized_as) + " " + LastFMScrobbler.getUserName()); @@ -355,7 +383,8 @@ private void logout(Context ctx) { .show(); } - private static void cachedPlaylistsDialog(Context ctx) { + @SuppressLint("DefaultLocale") + private void cachedPlaylistsDialog(Context ctx) { List playlists = PlaylistCacheDbDelegate.getAllPlaylists(ctx); String[] playlistNames = new String[playlists.size()]; @@ -370,6 +399,7 @@ private static void cachedPlaylistsDialog(Context ctx) { String playlistId = selectedPlaylist.v1(); PlaylistCacheDbDelegate.deletePlaylist(ctx, playlistId); AndroidUtils.sendToast("Плейлист удален"); + findPreference("cached_playlists").setSummary(String.format("Скачано плейлистов: %d", MusicCacheImpl.getPlaylists().size())); }); builder.show(); @@ -379,10 +409,11 @@ private void delcache(Context ctx) { new VkAlertDialog.Builder(ctx) .setTitle(com.vtosters.lite.R.string.warning) .setMessage(com.vtosters.lite.R.string.cached_tracks_remove_confirm) - .setPositiveButton(com.vtosters.lite.R.string.yes, - (dialog, which) -> executor.submit(MusicCacheImpl::clear)) - .setNeutralButton(com.vtosters.lite.R.string.no, - (dialog, which) -> dialog.cancel()) + .setPositiveButton(com.vtosters.lite.R.string.yes, (dialog, which) -> { + executor.submit(MusicCacheImpl::clear); + 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()) .show(); } diff --git a/app/src/main/java/ru/vtosters/lite/utils/Metrics.java b/app/src/main/java/ru/vtosters/lite/utils/Metrics.java index 5f17ed3a1..dbbd05159 100644 --- a/app/src/main/java/ru/vtosters/lite/utils/Metrics.java +++ b/app/src/main/java/ru/vtosters/lite/utils/Metrics.java @@ -24,13 +24,25 @@ public class Metrics { private static JSONArray events = new JSONArray(); - public static void trackEvents(JSONObject object) { - if (!NetworkUtils.isNetworkConnected()) { - events.put(object); - } else if (shouldSaveUserTraffic() || events.length() > 0) { - trackEventList(object); + public static void trackEvents(JSONObject object, boolean isMusic) { + if (NetworkUtils.isNetworkConnected()) { + handleNetworkConnected(object, isMusic); } else { + handleNetworkDisconnected(object, isMusic); + } + } + + private static void handleNetworkConnected(JSONObject object, boolean isMusic) { + if (isMusic || shouldSaveUserTraffic() || events.length() > 0) { trackEventsImmediately(object); + } else { + trackEventList(object); + } + } + + private static void handleNetworkDisconnected(JSONObject object, boolean isMusic) { + if (!isMusic) { + events.put(object); } }