diff options
Diffstat (limited to 'app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java')
-rw-r--r-- | app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java | 260 |
1 files changed, 99 insertions, 161 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java b/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java index 17e564e2..b2d97247 100644 --- a/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java +++ b/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java @@ -23,14 +23,14 @@ import android.accounts.AccountManager; import android.app.Dialog; import android.content.ContentResolver; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.TypedArray; -import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; import android.provider.MediaStore; + +import androidx.annotation.NonNull; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import androidx.appcompat.app.AlertDialog; @@ -47,6 +47,7 @@ import com.sothree.slidinguppanel.SlidingUpPanelLayout; import java.io.File; import java.util.Date; import java.util.List; +import java.util.Objects; import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.MusicDirectory; @@ -85,10 +86,10 @@ import github.daneren2005.dsub.view.ChangeLog; * Created by Scott on 10/14/13. */ public class SubsonicFragmentActivity extends SubsonicActivity implements DownloadService.OnSongChangedListener { - private static String TAG = SubsonicFragmentActivity.class.getSimpleName(); + private static final String TAG = SubsonicFragmentActivity.class.getSimpleName(); private static boolean infoDialogDisplayed; private static boolean sessionInitialized = false; - private static long ALLOWED_SKEW = 30000L; + private static final long ALLOWED_SKEW = 30000L; private SlidingUpPanelLayout slideUpPanel; private SlidingUpPanelLayout.PanelSlideListener panelSlideListener; @@ -105,7 +106,6 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo private ImageButton startButton; private long lastBackPressTime = 0; private DownloadFile currentPlaying; - private PlayerState currentState; private ImageButton previousButton; private ImageButton nextButton; private ImageButton rewindButton; @@ -219,7 +219,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo currentFragment.stopActionMode(); // Disable custom view before switching - getSupportActionBar().setDisplayShowCustomEnabled(false); + Objects.requireNonNull(getSupportActionBar()).setDisplayShowCustomEnabled(false); getSupportActionBar().setDisplayShowTitleEnabled(true); bottomBar.setVisibility(View.GONE); @@ -250,12 +250,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo if(getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD)) { // Post this later so it actually runs - handler.postDelayed(new Runnable() { - @Override - public void run() { - openNowPlaying(); - } - }, 200); + handler.postDelayed(this::openNowPlaying, 200); getIntent().removeExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD); } @@ -277,98 +272,73 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo } rewindButton = (ImageButton) findViewById(R.id.download_rewind); - rewindButton.setOnClickListener(new View.OnClickListener() { + rewindButton.setOnClickListener(v -> new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { @Override - public void onClick(View v) { - new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { - @Override - protected Void doInBackground() throws Throwable { - if (getDownloadService() == null) { - return null; - } + protected Void doInBackground() { + if (getDownloadService() == null) { + return null; + } - getDownloadService().rewind(); - return null; - } - }.execute(); + getDownloadService().rewind(); + return null; } - }); + }.execute()); previousButton = (ImageButton) findViewById(R.id.download_previous); - previousButton.setOnClickListener(new View.OnClickListener() { + previousButton.setOnClickListener(v -> new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { @Override - public void onClick(View v) { - new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { - @Override - protected Void doInBackground() throws Throwable { - if(getDownloadService() == null) { - return null; - } + protected Void doInBackground() { + if(getDownloadService() == null) { + return null; + } - getDownloadService().previous(); - return null; - } - }.execute(); + getDownloadService().previous(); + return null; } - }); + }.execute()); startButton = (ImageButton) findViewById(R.id.download_start); - startButton.setOnClickListener(new View.OnClickListener() { + startButton.setOnClickListener(v -> new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { @Override - public void onClick(View v) { - new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { - @Override - protected Void doInBackground() throws Throwable { - PlayerState state = getDownloadService().getPlayerState(); - if(state == PlayerState.STARTED) { - getDownloadService().pause(); - } else if(state == PlayerState.IDLE) { - getDownloadService().play(); - } else { - getDownloadService().start(); - } + protected Void doInBackground() { + PlayerState state = getDownloadService().getPlayerState(); + if(state == PlayerState.STARTED) { + getDownloadService().pause(); + } else if(state == PlayerState.IDLE) { + getDownloadService().play(); + } else { + getDownloadService().start(); + } - return null; - } - }.execute(); + return null; } - }); + }.execute()); nextButton = (ImageButton) findViewById(R.id.download_next); - nextButton.setOnClickListener(new View.OnClickListener() { + nextButton.setOnClickListener(v -> new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { @Override - public void onClick(View v) { - new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { - @Override - protected Void doInBackground() throws Throwable { - if(getDownloadService() == null) { - return null; - } + protected Void doInBackground() { + if(getDownloadService() == null) { + return null; + } - getDownloadService().next(); - return null; - } - }.execute(); + getDownloadService().next(); + return null; } - }); + }.execute()); fastforwardButton = (ImageButton) findViewById(R.id.download_fastforward); - fastforwardButton.setOnClickListener(new View.OnClickListener() { + fastforwardButton.setOnClickListener(v -> new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { @Override - public void onClick(View v) { - new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { - @Override - protected Void doInBackground() throws Throwable { - if (getDownloadService() == null) { - return null; - } + protected Void doInBackground() { + if (getDownloadService() == null) { + return null; + } - getDownloadService().fastForward(); - return null; - } - }.execute(); + getDownloadService().fastForward(); + return null; } - }); + }.execute()); } @Override @@ -456,12 +426,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo UserUtil.seedCurrentUser(this); createAccount(); - runWhenServiceAvailable(new Runnable() { - @Override - public void run() { - getDownloadService().addOnSongChangedListener(SubsonicFragmentActivity.this, true); - } - }); + runWhenServiceAvailable(() -> getDownloadService().addOnSongChangedListener(SubsonicFragmentActivity.this, true)); } @Override @@ -474,7 +439,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo } @Override - public void onSaveInstanceState(Bundle savedInstanceState) { + public void onSaveInstanceState(@NonNull Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); savedInstanceState.putString(Constants.MAIN_NOW_PLAYING, nowPlayingFragment.getTag()); if(secondaryFragment != null) { @@ -598,7 +563,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo @Override public void setTitle(CharSequence title) { if(slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) { - getSupportActionBar().setTitle(title); + Objects.requireNonNull(getSupportActionBar()).setTitle(title); } else { super.setTitle(title); } @@ -687,19 +652,15 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo Updater updater = new Updater(ver); updater.checkUpdates(this); } - catch(Exception e) { - - } + catch(Exception ignored) {} } private void loadSession() { - if (Build.VERSION.SDK_INT >= 23) { - try { - KeyStoreUtil.loadKeyStore(); - } catch (Exception e) { - Log.w(TAG, "Error loading keystore"); - Log.w(TAG, Log.getStackTraceString(e)); - } + try { + KeyStoreUtil.loadKeyStore(); + } catch (Exception e) { + Log.w(TAG, "Error loading keystore"); + Log.w(TAG, Log.getStackTraceString(e)); } loadSettings(); @@ -741,41 +702,37 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo editor.putString(Constants.PREFERENCES_KEY_SERVER_NAME + 1, "Demo Server"); editor.putString(Constants.PREFERENCES_KEY_SERVER_URL + 1, "http://demo.subsonic.org"); editor.putString(Constants.PREFERENCES_KEY_USERNAME + 1, "guest"); - if (Build.VERSION.SDK_INT < 23) { - editor.putString(Constants.PREFERENCES_KEY_PASSWORD + 1, "guest"); - } else { - // Attempt to encrypt password - String encryptedDefaultPassword = KeyStoreUtil.encrypt("guest"); + // Attempt to encrypt password + String encryptedDefaultPassword = KeyStoreUtil.encrypt("guest"); - if (encryptedDefaultPassword != null) { - // If encryption succeeds, store encrypted password and flag password as encrypted - editor.putString(Constants.PREFERENCES_KEY_PASSWORD + 1, encryptedDefaultPassword); - editor.putBoolean(Constants.PREFERENCES_KEY_ENCRYPTED_PASSWORD + 1, true); - } else { - // Fall back to plaintext if Keystore is having issue - editor = editor.putString(Constants.PREFERENCES_KEY_PASSWORD + 1, "guest"); - editor.putBoolean(Constants.PREFERENCES_KEY_ENCRYPTED_PASSWORD + 1, false); - } + if (encryptedDefaultPassword != null) { + // If encryption succeeds, store encrypted password and flag password as encrypted + editor.putString(Constants.PREFERENCES_KEY_PASSWORD + 1, encryptedDefaultPassword); + editor.putBoolean(Constants.PREFERENCES_KEY_ENCRYPTED_PASSWORD + 1, true); + } else { + // Fall back to plaintext if Keystore is having issue + editor = editor.putString(Constants.PREFERENCES_KEY_PASSWORD + 1, "guest"); + editor.putBoolean(Constants.PREFERENCES_KEY_ENCRYPTED_PASSWORD + 1, false); } editor.putInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, 1); - editor.commit(); + editor.apply(); } if(!prefs.contains(Constants.PREFERENCES_KEY_SERVER_COUNT)) { SharedPreferences.Editor editor = prefs.edit(); editor.putInt(Constants.PREFERENCES_KEY_SERVER_COUNT, 1); - editor.commit(); + editor.apply(); } } private boolean resetCacheLocation(SharedPreferences prefs) { String newDirectory = FileUtil.getDefaultMusicDirectory(this).getPath(); String oldDirectory = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null); - if(newDirectory == null || (oldDirectory != null && newDirectory.equals(oldDirectory))) { + if(newDirectory.equals(oldDirectory)) { return false; } else { SharedPreferences.Editor editor = prefs.edit(); editor.putString(Constants.PREFERENCES_KEY_CACHE_LOCATION, newDirectory); - editor.commit(); + editor.apply(); return true; } } @@ -807,7 +764,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo private PlayerQueue playerQueue; @Override - protected Void doInBackground() throws Throwable { + protected Void doInBackground() { try { MusicService musicService = MusicServiceFactory.getMusicService(context); PlayerQueue remoteState = musicService.getPlayQueue(context, null); @@ -849,50 +806,35 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo builder.setIcon(android.R.drawable.ic_dialog_alert) .setTitle(R.string.common_confirm) .setMessage(message) - .setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() { + .setPositiveButton(R.string.common_ok, (dialogInterface, i) -> new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { @Override - public void onClick(DialogInterface dialogInterface, int i) { - new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { - @Override - protected Void doInBackground() throws Throwable { - DownloadService downloadService = getDownloadService(); - downloadService.clear(); - downloadService.download(remoteState.songs, false, false, false, false, remoteState.currentPlayingIndex, remoteState.currentPlayingPosition); - return null; - } - }.execute(); + protected Void doInBackground() { + DownloadService downloadService = getDownloadService(); + downloadService.clear(); + downloadService.download(remoteState.songs, false, false, false, false, remoteState.currentPlayingIndex, remoteState.currentPlayingPosition); + return null; } - }) - .setNeutralButton(R.string.common_cancel, new DialogInterface.OnClickListener() { + }.execute()) + .setNeutralButton(R.string.common_cancel, (dialogInterface, i) -> new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { @Override - public void onClick(DialogInterface dialogInterface, int i) { - new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { - @Override - protected Void doInBackground() throws Throwable { - DownloadService downloadService = getDownloadService(); - downloadService.serializeQueue(false); - return null; - } - }.execute(); + protected Void doInBackground() { + DownloadService downloadService = getDownloadService(); + downloadService.serializeQueue(false); + return null; } - }) - .setNegativeButton(R.string.common_never, new DialogInterface.OnClickListener() { + }.execute()) + .setNegativeButton(R.string.common_never, (dialogInterface, i) -> new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { @Override - public void onClick(DialogInterface dialogInterface, int i) { - new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) { - @Override - protected Void doInBackground() throws Throwable { - DownloadService downloadService = getDownloadService(); - downloadService.serializeQueue(false); - - SharedPreferences.Editor editor = Util.getPreferences(SubsonicFragmentActivity.this).edit(); - editor.putBoolean(Constants.PREFERENCES_KEY_RESUME_PLAY_QUEUE_NEVER, true); - editor.commit(); - return null; - } - }.execute(); + protected Void doInBackground() { + DownloadService downloadService = getDownloadService(); + downloadService.serializeQueue(false); + + SharedPreferences.Editor editor = Util.getPreferences(SubsonicFragmentActivity.this).edit(); + editor.putBoolean(Constants.PREFERENCES_KEY_RESUME_PLAY_QUEUE_NEVER, true); + editor.apply(); + return null; } - }); + }.execute()); builder.create().show(); } @@ -902,7 +844,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo new SilentBackgroundTask<Void>(this) { @Override - protected Void doInBackground() throws Throwable { + protected Void doInBackground() { AccountManager accountManager = (AccountManager) context.getSystemService(ACCOUNT_SERVICE); Account account = new Account(Constants.SYNC_ACCOUNT_NAME, Constants.SYNC_ACCOUNT_TYPE); accountManager.addAccountExplicitly(account, null, null); @@ -941,10 +883,6 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo } } - public Toolbar getActiveToolbar() { - return slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED ? nowPlayingToolbar : mainToolbar; - } - @Override public void onSongChanged(DownloadFile currentPlaying, int currentPlayingIndex, boolean shouldFastForward) { this.currentPlaying = currentPlaying; |