From 79fc3b64c5b409b07f72a234ecb7fbcda51762f7 Mon Sep 17 00:00:00 2001 From: shadow578 <52449218+shadow578@users.noreply.github.com> Date: Sun, 1 Aug 2021 11:33:33 +0200 Subject: [PATCH] remove explore tab --- .../shadow578/music_dl/backup/BackupData.java | 4 +- .../music_dl/ui/main/MainActivity.java | 14 +- .../ui/ytmusic/YoutubeMusicFragment.java | 189 ------------------ .../ui/ytmusic/YoutubeMusicViewModel.java | 101 ---------- .../shadow578/music_dl/util/Payload.java | 83 -------- .../github/shadow578/music_dl/util/Url.java | 41 ---- .../main/res/drawable/ic_round_explore_24.xml | 10 - app/src/main/res/layout/fragment_explore.xml | 54 ----- .../main/res/menu/main_bottom_navigation.xml | 5 - app/src/main/res/values-de-rDE/strings.xml | 1 - app/src/main/res/values/strings.xml | 1 - 11 files changed, 4 insertions(+), 499 deletions(-) delete mode 100644 app/src/main/java/io/github/shadow578/music_dl/ui/ytmusic/YoutubeMusicFragment.java delete mode 100644 app/src/main/java/io/github/shadow578/music_dl/ui/ytmusic/YoutubeMusicViewModel.java delete mode 100644 app/src/main/java/io/github/shadow578/music_dl/util/Payload.java delete mode 100644 app/src/main/java/io/github/shadow578/music_dl/util/Url.java delete mode 100644 app/src/main/res/drawable/ic_round_explore_24.xml delete mode 100644 app/src/main/res/layout/fragment_explore.xml diff --git a/app/src/main/java/io/github/shadow578/music_dl/backup/BackupData.java b/app/src/main/java/io/github/shadow578/music_dl/backup/BackupData.java index 85fd145..58a6b81 100644 --- a/app/src/main/java/io/github/shadow578/music_dl/backup/BackupData.java +++ b/app/src/main/java/io/github/shadow578/music_dl/backup/BackupData.java @@ -16,13 +16,13 @@ public class BackupData { * the tracks in this backup */ @NonNull - public List tracks; + public final List tracks; /** * the time the backup was created */ @Nullable - public LocalDateTime backupTime; + public final LocalDateTime backupTime; public BackupData(@NonNull List tracks, @Nullable LocalDateTime backupTime) { this.tracks = tracks; diff --git a/app/src/main/java/io/github/shadow578/music_dl/ui/main/MainActivity.java b/app/src/main/java/io/github/shadow578/music_dl/ui/main/MainActivity.java index ccfb3a3..3912bb5 100644 --- a/app/src/main/java/io/github/shadow578/music_dl/ui/main/MainActivity.java +++ b/app/src/main/java/io/github/shadow578/music_dl/ui/main/MainActivity.java @@ -19,14 +19,12 @@ import io.github.shadow578.music_dl.ui.base.BaseActivity; import io.github.shadow578.music_dl.ui.more.MoreFragment; import io.github.shadow578.music_dl.ui.tracks.TracksFragment; -import io.github.shadow578.music_dl.ui.ytmusic.YoutubeMusicFragment; /** * the main activity */ public class MainActivity extends BaseActivity { - private final YoutubeMusicFragment exploreFragment = new YoutubeMusicFragment(); private final TracksFragment tracksFragment = new TracksFragment(); private final MoreFragment moreFragment = new MoreFragment(); @@ -35,7 +33,6 @@ public class MainActivity extends BaseActivity { */ private final List
sectionOrder = Arrays.asList( Section.Tracks, - Section.Explore, Section.More ); @@ -114,8 +111,6 @@ private Fragment getSectionFragment(@NonNull Section section) { default: case Tracks: return tracksFragment; - case Explore: - return exploreFragment; case More: return moreFragment; } @@ -128,17 +123,12 @@ public enum Section { /** * the tracks library fragment */ - Tracks(R.id.nav_tracks, false), - - /** - * the explore fragment - */ - Explore(R.id.nav_explore, false), + Tracks(R.id.nav_tracks, true), /** * the more / about fragment */ - More(R.id.nav_more, false); + More(R.id.nav_more, true); /** * the id of the menu item for this section (in bottom navigation) diff --git a/app/src/main/java/io/github/shadow578/music_dl/ui/ytmusic/YoutubeMusicFragment.java b/app/src/main/java/io/github/shadow578/music_dl/ui/ytmusic/YoutubeMusicFragment.java deleted file mode 100644 index 7b0d03a..0000000 --- a/app/src/main/java/io/github/shadow578/music_dl/ui/ytmusic/YoutubeMusicFragment.java +++ /dev/null @@ -1,189 +0,0 @@ -package io.github.shadow578.music_dl.ui.ytmusic; - -import android.annotation.SuppressLint; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.webkit.JavascriptInterface; -import android.webkit.WebView; -import android.webkit.WebViewClient; -import android.widget.Toast; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.lifecycle.ViewModelProvider; - -import java.util.Optional; - -import io.github.shadow578.music_dl.databinding.FragmentExploreBinding; -import io.github.shadow578.music_dl.ui.base.BaseFragment; -import io.github.shadow578.music_dl.util.Async; -import io.github.shadow578.music_dl.util.Payload; -import io.github.shadow578.music_dl.util.Url; -import io.github.shadow578.music_dl.util.Util; - -/** - * the yt music browsing activity - */ -public class YoutubeMusicFragment extends BaseFragment { - - /** - * the view binding instance - */ - private FragmentExploreBinding b; - - /** - * the view model instance - */ - private YoutubeMusicViewModel model; - - - @Nullable - @Override - public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - b = FragmentExploreBinding.inflate(inflater, container, false); - return b.getRoot(); - } - - @Override - public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - model = new ViewModelProvider(this).get(YoutubeMusicViewModel.class); - - // setup buttons - b.downloadTrack.setOnClickListener(v -> handleDownloadTrack()); - b.autoDownloadList.setOnCheckedChangeListener((v, checked) -> handleAutoDownload(checked)); - - // sync auto- download with model value - model.getAutoDownloadEnabled().observe(requireActivity(), autoDl -> { - b.autoDownloadList.setChecked(autoDl); - b.autoDownloadOverlay.setVisibility(autoDl ? View.VISIBLE : View.GONE); - }); - - // init webview - initWebView(); - - // navigate to yt music start page - b.webview.loadUrl(Url.YoutubeMusicMain.url()); - } - - /** - * initialize the webview - */ - @SuppressLint("SetJavaScriptEnabled") - private void initWebView() { - // enable remote debugging (from chrome://inspect) - WebView.setWebContentsDebuggingEnabled(true); - - // set the webview client to one we have events on - b.webview.setWebViewClient(new WebViewClient() { - @Override - public void onPageFinished(WebView view, String url) { - super.onPageFinished(view, url); - injectStaticPayload(); - } - }); - - // enable js and ... - b.webview.getSettings().setJavaScriptEnabled(true); - b.webview.getSettings().setDomStorageEnabled(true); - b.webview.getSettings().setAppCacheEnabled(true); - - // add javascript interface - b.webview.addJavascriptInterface(new JSInterface(), "JSI"); - } - - /** - * inject static javascript payloads - */ - private void injectStaticPayload() { - // monitor if the video changed - Payload.MonitorVideoChange.run(b.webview); - - // hide popups - Payload.HidePopups.runForever(b.webview); - } - - /** - * download the current track - */ - private void handleDownloadTrack() { - // extract id - final Optional id = Util.extractTrackId(b.webview.getUrl()); - - // show toast if could not find id or no track is playing - if (!id.isPresent() - || id.get().isEmpty() - || !model.isTrackPlaying()) { - Toast.makeText(requireContext(), "could not find track info! is a track playing?", Toast.LENGTH_SHORT).show(); - return; - } - - // call model download - model.downloadTrack(id.get()); - } - - /** - * start or stop auto- downloading the current playlist - * - * @param enable enable auto- download? - */ - private void handleAutoDownload(boolean enable) { - model.setAutoDownloadEnabled(enable); - - if (enable) { - autoDownloadNext(true); - } - } - - /** - * if in auto- download mode, go to the next track - * - * @param force disable check if auto- download is actually enabled - */ - private void autoDownloadNext(boolean force) { - // abort if not auto- downloading - if (!force && !Boolean.TRUE.equals(model.getAutoDownloadEnabled().getValue())) { - return; - } - - // download this track - handleDownloadTrack(); - - // go to the next track - Payload.NextTrack.runLater(b.webview, 1000); - } - - /** - * page javascript -> android bridge - */ - @SuppressWarnings("unused") - public class JSInterface { - - /** - * notification from the page js that the main video just changed - */ - @JavascriptInterface - public void videoChanged() { - // update the title - Async.runOnMain(() -> Payload.ExtractTrackTitle.run(b.webview)); - } - - /** - * report the video title - * - * @param title the video title. empty or null if a ad is playing - */ - @JavascriptInterface - public void reportTitle(@Nullable String title) { - Async.runOnMain(() -> { - // report new title - model.setCurrentTitle(title); - - // handle auto- downloading - autoDownloadNext(false); - }); - } - } -} \ No newline at end of file diff --git a/app/src/main/java/io/github/shadow578/music_dl/ui/ytmusic/YoutubeMusicViewModel.java b/app/src/main/java/io/github/shadow578/music_dl/ui/ytmusic/YoutubeMusicViewModel.java deleted file mode 100644 index 7796724..0000000 --- a/app/src/main/java/io/github/shadow578/music_dl/ui/ytmusic/YoutubeMusicViewModel.java +++ /dev/null @@ -1,101 +0,0 @@ -package io.github.shadow578.music_dl.ui.ytmusic; - -import android.app.Application; -import android.content.Intent; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.lifecycle.AndroidViewModel; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.MutableLiveData; - -import io.github.shadow578.music_dl.db.TracksDB; -import io.github.shadow578.music_dl.downloader.DownloaderService; -import io.github.shadow578.music_dl.ui.InsertTrackUIHelper; - -/** - * viewmodel for the {@link YoutubeMusicFragment} - */ -public class YoutubeMusicViewModel extends AndroidViewModel { - - /** - * the current title, null if no track is playing - */ - @Nullable - private String currentTitle = null; - - /** - * is auto- downloading enabled? - */ - private final MutableLiveData autoDownloadEnabled = new MutableLiveData<>(false); - - /** - * create the view model - * - * @param application the application reference - */ - public YoutubeMusicViewModel(@NonNull Application application) { - super(application); - TracksDB.init(getApplication()); - } - - /** - * start downloading a track - * - * @param id the track to download - */ - public void downloadTrack(@NonNull String id) { - // check if a track is playing - if (!isTrackPlaying()) { - return; - } - - // insert into database - InsertTrackUIHelper.insertTrack(getApplication(), id, currentTitle); - - // start downloader - maybeStartDownloaderService(); - } - - /** - * start the downloader service, if needed - */ - private void maybeStartDownloaderService() { - final Intent serviceIntent = new Intent(getApplication(), DownloaderService.class); - getApplication().startService(serviceIntent); - } - - /** - * set the auto- download mode enable - * - * @param enabled enable auto- download mode? - */ - public void setAutoDownloadEnabled(boolean enabled) { - autoDownloadEnabled.postValue(enabled); - } - - /** - * @return auto- download mode enable - */ - @NonNull - public LiveData getAutoDownloadEnabled() { - return autoDownloadEnabled; - } - - /** - * set the current track title - * - * @param title the title of the current track. null or empty if no track is playing - */ - public void setCurrentTitle(@Nullable String title) { - currentTitle = title; - } - - /** - * @return is there currently a track playing? - */ - @SuppressWarnings("BooleanMethodIsAlwaysInverted") - public boolean isTrackPlaying() { - return currentTitle != null && !currentTitle.isEmpty(); - } -} diff --git a/app/src/main/java/io/github/shadow578/music_dl/util/Payload.java b/app/src/main/java/io/github/shadow578/music_dl/util/Payload.java deleted file mode 100644 index f27f149..0000000 --- a/app/src/main/java/io/github/shadow578/music_dl/util/Payload.java +++ /dev/null @@ -1,83 +0,0 @@ -package io.github.shadow578.music_dl.util; - -import android.webkit.WebView; - -import androidx.annotation.NonNull; - -/** - * javascript payloads that can be injected into music.youtube.com - */ -public enum Payload { - - /** - * extract the title of the track. if a ad is currently playing, will result in a empty string. calls JSI.reportTitle() with the result - */ - ExtractTrackTitle("JSI.reportTitle(document.querySelectorAll('.title.ytmusic-player-bar')[0].innerText)"), - - /** - * go to the next track - */ - NextTrack("document.querySelectorAll('.next-button')[0].click()"), - - /** - * go to the previous track - */ - PreviousTrack("document.querySelectorAll('.previous-button')[0].click()"), - - /** - * hide popups (the 'buy youtube premium' ones - */ - HidePopups("document.querySelectorAll('.ytmusic-popup-container').forEach(function(v) { v.style.display=\"none\" })"), - - /** - * monitor if the main video changed. calls JSI.videoChanged() when the video changes - */ - MonitorVideoChange("document.querySelectorAll(\".html5-main-video\")[0].onplaying = function() { JSI.videoChanged() }"); - - /** - * the javascript expression of this payload - */ - @NonNull - private final String jsExpression; - - /** - * create a new JS payload - * - * @param jsExpression the javascript expression to execute - */ - Payload(@NonNull String jsExpression) { - this.jsExpression = jsExpression; - } - - /** - * run the payload in the webview - * - * @param webView the webview to run the payload in - */ - public void run(@NonNull WebView webView) { - webView.evaluateJavascript(jsExpression, null); - } - - - /** - * run the payload in the webview with a initial delay - * - * @param webView the webview to run the payload in - * @param delay the delay before running the payload, in milliseconds - */ - public void runLater(@NonNull WebView webView, int delay) { - if (delay <= 0) - throw new IllegalArgumentException("delay must be > 0 ms"); - - webView.evaluateJavascript("setTimeout(function() { " + jsExpression + " }, " + delay + ")", null); - } - - /** - * run the payload in the webview every x ms, forever - * - * @param webView the webview to run the payload in - */ - public void runForever(@NonNull WebView webView) { - webView.evaluateJavascript("setInterval(function() { " + jsExpression + " }, 100)", null); - } -} diff --git a/app/src/main/java/io/github/shadow578/music_dl/util/Url.java b/app/src/main/java/io/github/shadow578/music_dl/util/Url.java deleted file mode 100644 index e39696f..0000000 --- a/app/src/main/java/io/github/shadow578/music_dl/util/Url.java +++ /dev/null @@ -1,41 +0,0 @@ -package io.github.shadow578.music_dl.util; - -import androidx.annotation.NonNull; - -/** - * url constants - */ -public enum Url { - - /** - * the main youtube music page link - */ - YoutubeMusicMain("https://music.youtube.com"), - - /** - * the youtube music watch page link (without id) - */ - YoutubeMusicWatch(YoutubeMusicMain.url + "/watch?v="); - - /** - * the url of this value - */ - @NonNull - private final String url; - - /** - * create a new url value - * - * @param url the url - */ - Url(@NonNull String url) { - this.url = url; - } - - /** - * @return the url of this value - */ - public String url() { - return url; - } -} diff --git a/app/src/main/res/drawable/ic_round_explore_24.xml b/app/src/main/res/drawable/ic_round_explore_24.xml deleted file mode 100644 index 0ac168a..0000000 --- a/app/src/main/res/drawable/ic_round_explore_24.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - diff --git a/app/src/main/res/layout/fragment_explore.xml b/app/src/main/res/layout/fragment_explore.xml deleted file mode 100644 index a45591e..0000000 --- a/app/src/main/res/layout/fragment_explore.xml +++ /dev/null @@ -1,54 +0,0 @@ - - - - - - - - - - -