Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Music autocaching v2 #1591

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions app/src/main/java/com/vk/libvideo/VideoTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/ru/vtosters/hooks/other/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/ru/vtosters/lite/music/LastFMScrobbler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand All @@ -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,
Expand Down
53 changes: 42 additions & 11 deletions app/src/main/java/ru/vtosters/lite/ui/fragments/MusicFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String> items = Arrays.asList("Не кешировать", "Только свои", "Все");

ArrayAdapter<String> 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;
}
);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
Expand All @@ -355,7 +383,8 @@ private void logout(Context ctx) {
.show();
}

private static void cachedPlaylistsDialog(Context ctx) {
@SuppressLint("DefaultLocale")
private void cachedPlaylistsDialog(Context ctx) {
List<Playlist> playlists = PlaylistCacheDbDelegate.getAllPlaylists(ctx);
String[] playlistNames = new String[playlists.size()];

Expand All @@ -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();
Expand All @@ -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();
}

Expand Down
22 changes: 17 additions & 5 deletions app/src/main/java/ru/vtosters/lite/utils/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Loading