2013-04-02 08:01:15 +02:00
|
|
|
/*
|
|
|
|
This file is part of Subsonic.
|
|
|
|
|
|
|
|
Subsonic is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Subsonic is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2009 (C) Sindre Mehus
|
|
|
|
*/
|
|
|
|
|
2015-07-26 18:15:07 +02:00
|
|
|
package org.moire.ultrasonic.activity;
|
2013-04-02 08:01:15 +02:00
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.preference.PreferenceManager;
|
2013-12-04 07:36:02 +01:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
2013-04-02 08:01:15 +02:00
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.TextView;
|
2013-12-04 07:36:02 +01:00
|
|
|
|
2015-07-26 18:15:07 +02:00
|
|
|
import org.moire.ultrasonic.R;
|
2020-09-18 09:37:19 +02:00
|
|
|
import org.moire.ultrasonic.data.ActiveServerProvider;
|
2020-06-25 14:33:44 +02:00
|
|
|
import org.moire.ultrasonic.service.MediaPlayerLifecycleSupport;
|
2018-03-18 19:15:22 +01:00
|
|
|
import org.moire.ultrasonic.service.MusicService;
|
|
|
|
import org.moire.ultrasonic.service.MusicServiceFactory;
|
2015-07-26 18:15:07 +02:00
|
|
|
import org.moire.ultrasonic.util.Constants;
|
|
|
|
import org.moire.ultrasonic.util.FileUtil;
|
|
|
|
import org.moire.ultrasonic.util.MergeAdapter;
|
2018-03-18 19:15:22 +01:00
|
|
|
import org.moire.ultrasonic.util.TabActivityBackgroundTask;
|
2015-07-26 18:15:07 +02:00
|
|
|
import org.moire.ultrasonic.util.Util;
|
2013-05-17 09:00:20 +02:00
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
import java.util.Collections;
|
|
|
|
|
2020-06-22 18:35:58 +02:00
|
|
|
import kotlin.Lazy;
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
import static java.util.Arrays.asList;
|
2020-09-18 09:37:19 +02:00
|
|
|
import static org.koin.android.viewmodel.compat.ViewModelCompat.viewModel;
|
|
|
|
import static org.koin.java.KoinJavaComponent.inject;
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
public class MainActivity extends SubsonicTabActivity
|
|
|
|
{
|
|
|
|
private static boolean infoDialogDisplayed;
|
|
|
|
private static boolean shouldUseId3;
|
2020-09-18 09:37:19 +02:00
|
|
|
private static int lastActiveServer;
|
2013-12-04 07:36:02 +01:00
|
|
|
|
2020-06-25 14:33:44 +02:00
|
|
|
private Lazy<MediaPlayerLifecycleSupport> lifecycleSupport = inject(MediaPlayerLifecycleSupport.class);
|
2020-09-18 09:37:19 +02:00
|
|
|
private Lazy<ActiveServerProvider> activeServerProvider = inject(ActiveServerProvider.class);
|
|
|
|
private Lazy<ServerSettingsModel> serverSettingsModel = viewModel(this, ServerSettingsModel.class);
|
2020-06-22 18:35:58 +02:00
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
/**
|
|
|
|
* Called when the activity is first created.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onCreate(final Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
if (getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_EXIT))
|
|
|
|
{
|
2013-12-31 06:06:30 +01:00
|
|
|
setResult(Constants.RESULT_CLOSE_ALL);
|
2014-01-23 05:57:19 +01:00
|
|
|
|
2020-06-25 14:33:44 +02:00
|
|
|
if (getMediaPlayerController() != null)
|
2014-01-23 05:57:19 +01:00
|
|
|
{
|
2020-06-25 14:33:44 +02:00
|
|
|
getMediaPlayerController().stopJukeboxService();
|
2014-01-23 05:57:19 +01:00
|
|
|
}
|
2014-01-22 09:58:38 +01:00
|
|
|
|
|
|
|
if (getImageLoader() != null)
|
|
|
|
{
|
|
|
|
getImageLoader().stopImageLoader();
|
|
|
|
}
|
|
|
|
|
2013-12-31 06:06:30 +01:00
|
|
|
finish();
|
2013-12-04 07:36:02 +01:00
|
|
|
exit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setContentView(R.layout.main);
|
|
|
|
|
|
|
|
loadSettings();
|
|
|
|
|
|
|
|
final View buttons = LayoutInflater.from(this).inflate(R.layout.main_buttons, null);
|
|
|
|
final View serverButton = buttons.findViewById(R.id.main_select_server);
|
2020-09-18 09:37:19 +02:00
|
|
|
final TextView serverTextView = serverButton.findViewById(R.id.main_select_server_2);
|
2013-12-04 07:36:02 +01:00
|
|
|
final View musicTitle = buttons.findViewById(R.id.main_music);
|
|
|
|
final View artistsButton = buttons.findViewById(R.id.main_artists_button);
|
|
|
|
final View albumsButton = buttons.findViewById(R.id.main_albums_button);
|
|
|
|
final View genresButton = buttons.findViewById(R.id.main_genres_button);
|
|
|
|
final View videosTitle = buttons.findViewById(R.id.main_videos_title);
|
|
|
|
final View songsTitle = buttons.findViewById(R.id.main_songs);
|
|
|
|
final View randomSongsButton = buttons.findViewById(R.id.main_songs_button);
|
|
|
|
final View songsStarredButton = buttons.findViewById(R.id.main_songs_starred);
|
|
|
|
final View albumsTitle = buttons.findViewById(R.id.main_albums);
|
|
|
|
final View albumsNewestButton = buttons.findViewById(R.id.main_albums_newest);
|
|
|
|
final View albumsRandomButton = buttons.findViewById(R.id.main_albums_random);
|
|
|
|
final View albumsHighestButton = buttons.findViewById(R.id.main_albums_highest);
|
|
|
|
final View albumsStarredButton = buttons.findViewById(R.id.main_albums_starred);
|
|
|
|
final View albumsRecentButton = buttons.findViewById(R.id.main_albums_recent);
|
|
|
|
final View albumsFrequentButton = buttons.findViewById(R.id.main_albums_frequent);
|
|
|
|
final View albumsAlphaByNameButton = buttons.findViewById(R.id.main_albums_alphaByName);
|
|
|
|
final View albumsAlphaByArtistButton = buttons.findViewById(R.id.main_albums_alphaByArtist);
|
|
|
|
final View videosButton = buttons.findViewById(R.id.main_videos);
|
|
|
|
|
2020-09-18 09:37:19 +02:00
|
|
|
lastActiveServer = ActiveServerProvider.Companion.getActiveServerId(this);
|
|
|
|
String name = activeServerProvider.getValue().getActiveServer().getName();
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
serverTextView.setText(name);
|
|
|
|
|
2020-09-18 09:37:19 +02:00
|
|
|
final ListView list = findViewById(R.id.main_list);
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
final MergeAdapter adapter = new MergeAdapter();
|
|
|
|
adapter.addViews(Collections.singletonList(serverButton), true);
|
|
|
|
|
2020-09-18 09:37:19 +02:00
|
|
|
if (!ActiveServerProvider.Companion.isOffline(this))
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
|
|
|
adapter.addView(musicTitle, false);
|
|
|
|
adapter.addViews(asList(artistsButton, albumsButton, genresButton), true);
|
|
|
|
adapter.addView(songsTitle, false);
|
|
|
|
adapter.addViews(asList(randomSongsButton, songsStarredButton), true);
|
|
|
|
adapter.addView(albumsTitle, false);
|
|
|
|
|
|
|
|
if (Util.getShouldUseId3Tags(MainActivity.this))
|
|
|
|
{
|
|
|
|
shouldUseId3 = true;
|
|
|
|
adapter.addViews(asList(albumsNewestButton, albumsRecentButton, albumsFrequentButton, albumsRandomButton, albumsStarredButton, albumsAlphaByNameButton, albumsAlphaByArtistButton), true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shouldUseId3 = false;
|
|
|
|
adapter.addViews(asList(albumsNewestButton, albumsRecentButton, albumsFrequentButton, albumsHighestButton, albumsRandomButton, albumsStarredButton, albumsAlphaByNameButton, albumsAlphaByArtistButton), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter.addView(videosTitle, false);
|
|
|
|
adapter.addViews(Collections.singletonList(videosButton), true);
|
2018-03-18 19:15:22 +01:00
|
|
|
|
|
|
|
if (Util.isNetworkConnected(this)) {
|
|
|
|
new PingTask(this, false).execute();
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
list.setAdapter(adapter);
|
|
|
|
|
|
|
|
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
|
|
|
|
{
|
|
|
|
if (view == serverButton)
|
|
|
|
{
|
2020-09-18 09:37:19 +02:00
|
|
|
showServers();
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
else if (view == albumsNewestButton)
|
|
|
|
{
|
|
|
|
showAlbumList("newest", R.string.main_albums_newest);
|
|
|
|
}
|
|
|
|
else if (view == albumsRandomButton)
|
|
|
|
{
|
|
|
|
showAlbumList("random", R.string.main_albums_random);
|
|
|
|
}
|
|
|
|
else if (view == albumsHighestButton)
|
|
|
|
{
|
|
|
|
showAlbumList("highest", R.string.main_albums_highest);
|
|
|
|
}
|
|
|
|
else if (view == albumsRecentButton)
|
|
|
|
{
|
|
|
|
showAlbumList("recent", R.string.main_albums_recent);
|
|
|
|
}
|
|
|
|
else if (view == albumsFrequentButton)
|
|
|
|
{
|
|
|
|
showAlbumList("frequent", R.string.main_albums_frequent);
|
|
|
|
}
|
|
|
|
else if (view == albumsStarredButton)
|
|
|
|
{
|
|
|
|
showAlbumList(Constants.STARRED, R.string.main_albums_starred);
|
|
|
|
}
|
|
|
|
else if (view == albumsAlphaByNameButton)
|
|
|
|
{
|
|
|
|
showAlbumList(Constants.ALPHABETICAL_BY_NAME, R.string.main_albums_alphaByName);
|
|
|
|
}
|
|
|
|
else if (view == albumsAlphaByArtistButton)
|
|
|
|
{
|
|
|
|
showAlbumList("alphabeticalByArtist", R.string.main_albums_alphaByArtist);
|
|
|
|
}
|
|
|
|
else if (view == songsStarredButton)
|
|
|
|
{
|
|
|
|
showStarredSongs();
|
|
|
|
}
|
|
|
|
else if (view == artistsButton)
|
|
|
|
{
|
|
|
|
showArtists();
|
|
|
|
}
|
|
|
|
else if (view == albumsButton)
|
|
|
|
{
|
|
|
|
showAlbumList(Constants.ALPHABETICAL_BY_NAME, R.string.main_albums_title);
|
|
|
|
}
|
|
|
|
else if (view == randomSongsButton)
|
|
|
|
{
|
|
|
|
showRandomSongs();
|
|
|
|
}
|
|
|
|
else if (view == genresButton)
|
|
|
|
{
|
|
|
|
showGenres();
|
|
|
|
}
|
|
|
|
else if (view == videosButton)
|
|
|
|
{
|
|
|
|
showVideos();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
final View homeMenuItem = findViewById(R.id.menu_home);
|
|
|
|
menuDrawer.setActiveView(homeMenuItem);
|
|
|
|
|
|
|
|
setActionBarTitle(R.string.common_appname);
|
|
|
|
setTitle(R.string.common_appname);
|
|
|
|
|
|
|
|
// Remember the current theme.
|
|
|
|
theme = Util.getTheme(this);
|
|
|
|
|
2020-09-18 09:37:19 +02:00
|
|
|
boolean shouldShowDialog = Util.shouldShowWelcomeScreen(this);
|
|
|
|
// This will convert the server settings from the Preferences to the DB
|
|
|
|
if (shouldShowDialog) serverSettingsModel.getValue().getServerList();
|
2013-12-04 07:36:02 +01:00
|
|
|
showInfoDialog(shouldShowDialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadSettings()
|
|
|
|
{
|
|
|
|
PreferenceManager.setDefaultValues(this, R.xml.settings, false);
|
|
|
|
final SharedPreferences preferences = Util.getPreferences(this);
|
|
|
|
|
|
|
|
if (!preferences.contains(Constants.PREFERENCES_KEY_CACHE_LOCATION))
|
|
|
|
{
|
|
|
|
final SharedPreferences.Editor editor = preferences.edit();
|
2020-06-17 18:14:34 +02:00
|
|
|
editor.putString(Constants.PREFERENCES_KEY_CACHE_LOCATION, FileUtil.getDefaultMusicDirectory(this).getPath());
|
2013-12-04 07:36:02 +01:00
|
|
|
editor.commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onResume()
|
|
|
|
{
|
|
|
|
super.onResume();
|
2020-09-18 09:37:19 +02:00
|
|
|
boolean shouldRestart = false;
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
boolean id3 = Util.getShouldUseId3Tags(MainActivity.this);
|
2020-09-18 09:37:19 +02:00
|
|
|
int currentActiveServer = ActiveServerProvider.Companion.getActiveServerId(MainActivity.this);
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
if (id3 != shouldUseId3)
|
|
|
|
{
|
|
|
|
shouldUseId3 = id3;
|
2020-09-18 09:37:19 +02:00
|
|
|
shouldRestart = true;
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
2020-09-18 09:37:19 +02:00
|
|
|
if (currentActiveServer != lastActiveServer)
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
2020-09-18 09:37:19 +02:00
|
|
|
lastActiveServer = currentActiveServer;
|
|
|
|
shouldRestart = true;
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
2020-09-18 09:37:19 +02:00
|
|
|
if (shouldRestart) restart();
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-09-18 09:37:19 +02:00
|
|
|
public boolean onCreateOptionsMenu(final Menu menu)
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
2020-09-18 09:37:19 +02:00
|
|
|
final MenuInflater menuInflater = getMenuInflater();
|
|
|
|
menuInflater.inflate(R.menu.main, menu);
|
|
|
|
super.onCreateOptionsMenu(menu);
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(final MenuItem item)
|
|
|
|
{
|
|
|
|
switch (item.getItemId())
|
|
|
|
{
|
|
|
|
case android.R.id.home:
|
|
|
|
menuDrawer.toggleMenu();
|
|
|
|
return true;
|
|
|
|
case R.id.main_shuffle:
|
|
|
|
final Intent intent1 = new Intent(this, DownloadActivity.class);
|
|
|
|
intent1.putExtra(Constants.INTENT_EXTRA_NAME_SHUFFLE, true);
|
2013-12-31 06:06:30 +01:00
|
|
|
startActivityForResultWithoutTransition(this, intent1);
|
2013-12-04 07:36:02 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void exit()
|
|
|
|
{
|
2020-06-22 18:35:58 +02:00
|
|
|
lifecycleSupport.getValue().onDestroy();
|
2020-07-02 19:01:33 +02:00
|
|
|
Util.unregisterMediaButtonEventReceiver(this, false);
|
2013-12-04 07:36:02 +01:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showInfoDialog(final boolean show)
|
|
|
|
{
|
|
|
|
if (!infoDialogDisplayed)
|
|
|
|
{
|
|
|
|
infoDialogDisplayed = true;
|
|
|
|
|
2020-09-18 09:37:19 +02:00
|
|
|
if (show)
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
2020-09-18 09:37:19 +02:00
|
|
|
Util.showWelcomeDialog(this, this, R.string.main_welcome_title, R.string.main_welcome_text_new);
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showAlbumList(final String type, final int title)
|
|
|
|
{
|
|
|
|
final Intent intent = new Intent(this, SelectAlbumActivity.class);
|
|
|
|
intent.putExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE, type);
|
|
|
|
intent.putExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TITLE, title);
|
|
|
|
intent.putExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, Util.getMaxAlbums(this));
|
|
|
|
intent.putExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0);
|
2013-12-31 06:06:30 +01:00
|
|
|
startActivityForResultWithoutTransition(this, intent);
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void showStarredSongs()
|
|
|
|
{
|
|
|
|
final Intent intent = new Intent(this, SelectAlbumActivity.class);
|
|
|
|
intent.putExtra(Constants.INTENT_EXTRA_NAME_STARRED, 1);
|
2013-12-31 06:06:30 +01:00
|
|
|
startActivityForResultWithoutTransition(this, intent);
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void showRandomSongs()
|
|
|
|
{
|
|
|
|
final Intent intent = new Intent(this, SelectAlbumActivity.class);
|
|
|
|
intent.putExtra(Constants.INTENT_EXTRA_NAME_RANDOM, 1);
|
|
|
|
intent.putExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, Util.getMaxSongs(this));
|
2013-12-31 06:06:30 +01:00
|
|
|
startActivityForResultWithoutTransition(this, intent);
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void showArtists()
|
|
|
|
{
|
|
|
|
final Intent intent = new Intent(this, SelectArtistActivity.class);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
intent.putExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TITLE, getResources().getString(R.string.main_artists_title));
|
2013-12-31 06:06:30 +01:00
|
|
|
startActivityForResultWithoutTransition(this, intent);
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void showGenres()
|
|
|
|
{
|
|
|
|
final Intent intent = new Intent(this, SelectGenreActivity.class);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
2013-12-31 06:06:30 +01:00
|
|
|
startActivityForResultWithoutTransition(this, intent);
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void showVideos()
|
|
|
|
{
|
|
|
|
final Intent intent = new Intent(this, SelectAlbumActivity.class);
|
|
|
|
intent.putExtra(Constants.INTENT_EXTRA_NAME_VIDEOS, 1);
|
2013-12-31 06:06:30 +01:00
|
|
|
startActivityForResultWithoutTransition(this, intent);
|
2013-04-02 08:01:15 +02:00
|
|
|
}
|
2018-03-18 19:15:22 +01:00
|
|
|
|
2020-09-18 09:37:19 +02:00
|
|
|
private void showServers()
|
|
|
|
{
|
|
|
|
final Intent intent = new Intent(this, ServerSelectorActivity.class);
|
|
|
|
startActivityForResult(intent, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-18 19:15:22 +01:00
|
|
|
* Temporary task to make a ping to server to get it supported api version.
|
|
|
|
*/
|
|
|
|
private static class PingTask extends TabActivityBackgroundTask<Void> {
|
|
|
|
PingTask(SubsonicTabActivity activity, boolean changeProgress) {
|
|
|
|
super(activity, changeProgress);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground() throws Throwable {
|
|
|
|
final MusicService service = MusicServiceFactory.getMusicService(getActivity());
|
|
|
|
service.ping(getActivity(), null);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void done(Void result) {}
|
|
|
|
}
|
2012-02-26 21:25:13 +01:00
|
|
|
}
|