The great whitespace cleanup

This commit is contained in:
Andrew Rabert 2018-03-24 15:25:12 -04:00
parent 75ad0a0239
commit a72f978001
256 changed files with 20257 additions and 20257 deletions

View File

@ -1,16 +1,16 @@
/*
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 2014 (C) Scott Jackson
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 2014 (C) Scott Jackson
*/
package net.nullsum.audinaut.activity;
@ -46,200 +46,200 @@ import net.nullsum.audinaut.util.LoadingTask;
import net.nullsum.audinaut.util.Util;
public class EditPlayActionActivity extends SubsonicActivity {
private CheckBox shuffleCheckbox;
private CheckBox startYearCheckbox;
private EditText startYearBox;
private CheckBox endYearCheckbox;
private EditText endYearBox;
private Button genreButton;
private Spinner offlineSpinner;
private CheckBox shuffleCheckbox;
private CheckBox startYearCheckbox;
private EditText startYearBox;
private CheckBox endYearCheckbox;
private EditText endYearBox;
private Button genreButton;
private Spinner offlineSpinner;
private String doNothing;
private String doNothing;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.tasker_start_playing_title);
setContentView(R.layout.edit_play_action);
final Activity context = this;
doNothing = context.getResources().getString(R.string.tasker_edit_do_nothing);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.tasker_start_playing_title);
setContentView(R.layout.edit_play_action);
final Activity context = this;
doNothing = context.getResources().getString(R.string.tasker_edit_do_nothing);
shuffleCheckbox = (CheckBox) findViewById(R.id.edit_shuffle_checkbox);
shuffleCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
startYearCheckbox.setEnabled(isChecked);
endYearCheckbox.setEnabled(isChecked);
genreButton.setEnabled(isChecked);
}
});
shuffleCheckbox = (CheckBox) findViewById(R.id.edit_shuffle_checkbox);
shuffleCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
startYearCheckbox.setEnabled(isChecked);
endYearCheckbox.setEnabled(isChecked);
genreButton.setEnabled(isChecked);
}
});
startYearCheckbox = (CheckBox) findViewById(R.id.edit_start_year_checkbox);
startYearBox = (EditText) findViewById(R.id.edit_start_year);
// Disable/enable number box if checked
startYearCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
startYearBox.setEnabled(isChecked);
}
});
startYearCheckbox = (CheckBox) findViewById(R.id.edit_start_year_checkbox);
startYearBox = (EditText) findViewById(R.id.edit_start_year);
// Disable/enable number box if checked
startYearCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
startYearBox.setEnabled(isChecked);
}
});
endYearCheckbox = (CheckBox) findViewById(R.id.edit_end_year_checkbox);
endYearBox = (EditText) findViewById(R.id.edit_end_year);
endYearCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
endYearBox.setEnabled(isChecked);
}
});
endYearCheckbox = (CheckBox) findViewById(R.id.edit_end_year_checkbox);
endYearBox = (EditText) findViewById(R.id.edit_end_year);
endYearCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
endYearBox.setEnabled(isChecked);
}
});
genreButton = (Button) findViewById(R.id.edit_genre_spinner);
genreButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new LoadingTask<List<Genre>>(context, true) {
@Override
protected List<Genre> doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(context);
return musicService.getGenres(false, context, this);
}
genreButton = (Button) findViewById(R.id.edit_genre_spinner);
genreButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new LoadingTask<List<Genre>>(context, true) {
@Override
protected List<Genre> doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(context);
return musicService.getGenres(false, context, this);
}
@Override
protected void done(final List<Genre> genres) {
List<String> names = new ArrayList<String>();
String blank = context.getResources().getString(R.string.select_genre_blank);
names.add(doNothing);
names.add(blank);
for(Genre genre: genres) {
names.add(genre.getName());
}
final List<String> finalNames = names;
@Override
protected void done(final List<Genre> genres) {
List<String> names = new ArrayList<String>();
String blank = context.getResources().getString(R.string.select_genre_blank);
names.add(doNothing);
names.add(blank);
for(Genre genre: genres) {
names.add(genre.getName());
}
final List<String> finalNames = names;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.shuffle_pick_genre)
.setItems(names.toArray(new CharSequence[names.size()]), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(which == 1) {
genreButton.setText("");
} else {
genreButton.setText(finalNames.get(which));
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.shuffle_pick_genre)
.setItems(names.toArray(new CharSequence[names.size()]), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(which == 1) {
genreButton.setText("");
} else {
genreButton.setText(finalNames.get(which));
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
@Override
protected void error(Throwable error) {
String msg;
if (error instanceof OfflineException) {
msg = getErrorMessage(error);
} else {
msg = context.getResources().getString(R.string.playlist_error) + " " + getErrorMessage(error);
}
@Override
protected void error(Throwable error) {
String msg;
if (error instanceof OfflineException) {
msg = getErrorMessage(error);
} else {
msg = context.getResources().getString(R.string.playlist_error) + " " + getErrorMessage(error);
}
Util.toast(context, msg, false);
}
}.execute();
}
});
genreButton.setText(doNothing);
Util.toast(context, msg, false);
}
}.execute();
}
});
genreButton.setText(doNothing);
offlineSpinner = (Spinner) findViewById(R.id.edit_offline_spinner);
ArrayAdapter<CharSequence> offlineAdapter = ArrayAdapter.createFromResource(this, R.array.editServerOptions, android.R.layout.simple_spinner_item);
offlineAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
offlineSpinner.setAdapter(offlineAdapter);
offlineSpinner = (Spinner) findViewById(R.id.edit_offline_spinner);
ArrayAdapter<CharSequence> offlineAdapter = ArrayAdapter.createFromResource(this, R.array.editServerOptions, android.R.layout.simple_spinner_item);
offlineAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
offlineSpinner.setAdapter(offlineAdapter);
// Setup default for everything
Bundle extras = getIntent().getBundleExtra(Constants.TASKER_EXTRA_BUNDLE);
if(extras != null) {
if(extras.getBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE)) {
shuffleCheckbox.setChecked(true);
}
// Setup default for everything
Bundle extras = getIntent().getBundleExtra(Constants.TASKER_EXTRA_BUNDLE);
if(extras != null) {
if(extras.getBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE)) {
shuffleCheckbox.setChecked(true);
}
String startYear = extras.getString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, null);
if(startYear != null) {
startYearCheckbox.setEnabled(true);
startYearBox.setText(startYear);
}
String endYear = extras.getString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, null);
if(endYear != null) {
endYearCheckbox.setEnabled(true);
endYearBox.setText(endYear);
}
String startYear = extras.getString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, null);
if(startYear != null) {
startYearCheckbox.setEnabled(true);
startYearBox.setText(startYear);
}
String endYear = extras.getString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, null);
if(endYear != null) {
endYearCheckbox.setEnabled(true);
endYearBox.setText(endYear);
}
String genre = extras.getString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, doNothing);
if(genre != null) {
genreButton.setText(genre);
}
String genre = extras.getString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, doNothing);
if(genre != null) {
genreButton.setText(genre);
}
int offline = extras.getInt(Constants.PREFERENCES_KEY_OFFLINE, 0);
if(offline != 0) {
offlineSpinner.setSelection(offline);
}
}
int offline = extras.getInt(Constants.PREFERENCES_KEY_OFFLINE, 0);
if(offline != 0) {
offlineSpinner.setSelection(offline);
}
}
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.tasker_configuration, menu);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.tasker_configuration, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home) {
cancel();
return true;
} else if(item.getItemId() == R.id.menu_accept) {
accept();
return true;
} else if(item.getItemId() == R.id.menu_cancel) {
cancel();
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home) {
cancel();
return true;
} else if(item.getItemId() == R.id.menu_accept) {
accept();
return true;
} else if(item.getItemId() == R.id.menu_cancel) {
cancel();
return true;
}
return false;
}
return false;
}
private void accept() {
Intent intent = new Intent();
private void accept() {
Intent intent = new Intent();
String blurb = getResources().getString(shuffleCheckbox.isChecked() ? R.string.tasker_start_playing_shuffled : R.string.tasker_start_playing);
intent.putExtra("com.twofortyfouram.locale.intent.extra.BLURB", blurb);
String blurb = getResources().getString(shuffleCheckbox.isChecked() ? R.string.tasker_start_playing_shuffled : R.string.tasker_start_playing);
intent.putExtra("com.twofortyfouram.locale.intent.extra.BLURB", blurb);
// Get settings user specified
Bundle data = new Bundle();
boolean shuffle = shuffleCheckbox.isChecked();
data.putBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE, shuffle);
if(shuffle) {
if(startYearCheckbox.isChecked()) {
data.putString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, startYearBox.getText().toString());
}
if(endYearCheckbox.isChecked()) {
data.putString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, endYearBox.getText().toString());
}
String genre = genreButton.getText().toString();
if(!genre.equals(doNothing)) {
data.putString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, genre);
}
}
// Get settings user specified
Bundle data = new Bundle();
boolean shuffle = shuffleCheckbox.isChecked();
data.putBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE, shuffle);
if(shuffle) {
if(startYearCheckbox.isChecked()) {
data.putString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, startYearBox.getText().toString());
}
if(endYearCheckbox.isChecked()) {
data.putString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, endYearBox.getText().toString());
}
String genre = genreButton.getText().toString();
if(!genre.equals(doNothing)) {
data.putString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, genre);
}
}
int offline = offlineSpinner.getSelectedItemPosition();
if(offline != 0) {
data.putInt(Constants.PREFERENCES_KEY_OFFLINE, offline);
}
int offline = offlineSpinner.getSelectedItemPosition();
if(offline != 0) {
data.putInt(Constants.PREFERENCES_KEY_OFFLINE, offline);
}
intent.putExtra(Constants.TASKER_EXTRA_BUNDLE, data);
intent.putExtra(Constants.TASKER_EXTRA_BUNDLE, data);
setResult(Activity.RESULT_OK, intent);
finish();
}
private void cancel() {
setResult(Activity.RESULT_CANCELED);
finish();
}
setResult(Activity.RESULT_OK, intent);
finish();
}
private void cancel() {
setResult(Activity.RESULT_CANCELED);
finish();
}
}

View File

@ -38,48 +38,48 @@ import net.nullsum.audinaut.provider.AudinautSearchProvider;
*/
public class QueryReceiverActivity extends Activity {
private static final String TAG = QueryReceiverActivity.class.getSimpleName();
private static final String TAG = QueryReceiverActivity.class.getSimpleName();
@Override
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
doSearch();
} else if(Intent.ACTION_VIEW.equals(intent.getAction())) {
showResult(intent.getDataString(), intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
}
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
doSearch();
} else if(Intent.ACTION_VIEW.equals(intent.getAction())) {
showResult(intent.getDataString(), intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
}
finish();
Util.disablePendingTransition(this);
}
private void doSearch() {
String query = getIntent().getStringExtra(SearchManager.QUERY);
if (query != null) {
Intent intent = new Intent(QueryReceiverActivity.this, SubsonicFragmentActivity.class);
intent.putExtra(Constants.INTENT_EXTRA_NAME_QUERY, query);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Util.startActivityWithoutTransition(QueryReceiverActivity.this, intent);
}
}
private void showResult(String albumId, String name) {
if (albumId != null) {
Intent intent = new Intent(this, SubsonicFragmentActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(Constants.INTENT_EXTRA_VIEW_ALBUM, true);
if(albumId.indexOf("ar-") == 0) {
intent.putExtra(Constants.INTENT_EXTRA_NAME_ARTIST, true);
albumId = albumId.replace("ar-", "");
} else if(albumId.indexOf("so-") == 0) {
intent.putExtra(Constants.INTENT_EXTRA_SEARCH_SONG, name);
albumId = albumId.replace("so-", "");
}
intent.putExtra(Constants.INTENT_EXTRA_NAME_ID, albumId);
if (name != null) {
intent.putExtra(Constants.INTENT_EXTRA_NAME_NAME, name);
}
Util.startActivityWithoutTransition(this, intent);
}
}
private void doSearch() {
String query = getIntent().getStringExtra(SearchManager.QUERY);
if (query != null) {
Intent intent = new Intent(QueryReceiverActivity.this, SubsonicFragmentActivity.class);
intent.putExtra(Constants.INTENT_EXTRA_NAME_QUERY, query);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Util.startActivityWithoutTransition(QueryReceiverActivity.this, intent);
}
}
private void showResult(String albumId, String name) {
if (albumId != null) {
Intent intent = new Intent(this, SubsonicFragmentActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(Constants.INTENT_EXTRA_VIEW_ALBUM, true);
if(albumId.indexOf("ar-") == 0) {
intent.putExtra(Constants.INTENT_EXTRA_NAME_ARTIST, true);
albumId = albumId.replace("ar-", "");
} else if(albumId.indexOf("so-") == 0) {
intent.putExtra(Constants.INTENT_EXTRA_SEARCH_SONG, name);
albumId = albumId.replace("so-", "");
}
intent.putExtra(Constants.INTENT_EXTRA_NAME_ID, albumId);
if (name != null) {
intent.putExtra(Constants.INTENT_EXTRA_NAME_NAME, name);
}
Util.startActivityWithoutTransition(this, intent);
}
}
}

View File

@ -28,29 +28,29 @@ import net.nullsum.audinaut.fragments.SettingsFragment;
import net.nullsum.audinaut.util.Constants;
public class SettingsActivity extends SubsonicActivity {
private static final String TAG = SettingsActivity.class.getSimpleName();
private PreferenceCompatFragment fragment;
private static final String TAG = SettingsActivity.class.getSimpleName();
private PreferenceCompatFragment fragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lastSelectedPosition = R.id.drawer_settings;
setContentView(R.layout.settings_activity);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lastSelectedPosition = R.id.drawer_settings;
setContentView(R.layout.settings_activity);
if (savedInstanceState == null) {
fragment = new SettingsFragment();
Bundle args = new Bundle();
args.putInt(Constants.INTENT_EXTRA_FRAGMENT_TYPE, R.xml.settings);
if (savedInstanceState == null) {
fragment = new SettingsFragment();
Bundle args = new Bundle();
args.putInt(Constants.INTENT_EXTRA_FRAGMENT_TYPE, R.xml.settings);
fragment.setArguments(args);
fragment.setRetainInstance(true);
fragment.setArguments(args);
fragment.setRetainInstance(true);
currentFragment = fragment;
currentFragment.setPrimaryFragment(true);
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, currentFragment, currentFragment.getSupportTag() + "").commit();
}
currentFragment = fragment;
currentFragment.setPrimaryFragment(true);
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, currentFragment, currentFragment.getSupportTag() + "").commit();
}
Toolbar mainToolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(mainToolbar);
}
Toolbar mainToolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(mainToolbar);
}
}

View File

@ -40,7 +40,7 @@ import net.nullsum.audinaut.provider.AudinautSearchProvider;
* @author Sindre Mehus
*/
public class VoiceQueryReceiverActivity extends Activity {
private static final String TAG = VoiceQueryReceiverActivity.class.getSimpleName();
private static final String TAG = VoiceQueryReceiverActivity.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
@ -53,7 +53,7 @@ public class VoiceQueryReceiverActivity extends Activity {
intent.putExtra(Constants.INTENT_EXTRA_NAME_QUERY, query);
intent.putExtra(Constants.INTENT_EXTRA_NAME_AUTOPLAY, true);
intent.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, getIntent().getStringExtra(MediaStore.EXTRA_MEDIA_FOCUS));
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Util.startActivityWithoutTransition(VoiceQueryReceiverActivity.this, intent);
}
finish();

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.adapter;
@ -24,21 +24,21 @@ import net.nullsum.audinaut.util.ImageLoader;
import net.nullsum.audinaut.view.FastScroller;
public class AlphabeticalAlbumAdapter extends EntryInfiniteGridAdapter implements FastScroller.BubbleTextGetter {
public AlphabeticalAlbumAdapter(Context context, List<MusicDirectory.Entry> entries, ImageLoader imageLoader, boolean largeCell) {
super(context, entries, imageLoader, largeCell);
}
public AlphabeticalAlbumAdapter(Context context, List<MusicDirectory.Entry> entries, ImageLoader imageLoader, boolean largeCell) {
super(context, entries, imageLoader, largeCell);
}
@Override
public String getTextToShowInBubble(int position) {
// Make sure that we are not trying to get an item for the loading placeholder
if(position >= sections.get(0).size()) {
if(sections.get(0).size() > 0) {
return getTextToShowInBubble(position - 1);
} else {
return "*";
}
} else {
return getNameIndex(getItemForPosition(position).getAlbum());
}
}
@Override
public String getTextToShowInBubble(int position) {
// Make sure that we are not trying to get an item for the loading placeholder
if(position >= sections.get(0).size()) {
if(sections.get(0).size() > 0) {
return getTextToShowInBubble(position - 1);
} else {
return "*";
}
} else {
return getNameIndex(getItemForPosition(position).getAlbum());
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.adapter;
@ -38,125 +38,125 @@ import net.nullsum.audinaut.view.SongView;
import net.nullsum.audinaut.view.UpdateView;
public class ArtistAdapter extends SectionAdapter<Serializable> implements FastScroller.BubbleTextGetter {
public static int VIEW_TYPE_SONG = 3;
public static int VIEW_TYPE_ARTIST = 4;
public static int VIEW_TYPE_SONG = 3;
public static int VIEW_TYPE_ARTIST = 4;
private List<MusicFolder> musicFolders;
private OnMusicFolderChanged onMusicFolderChanged;
private List<MusicFolder> musicFolders;
private OnMusicFolderChanged onMusicFolderChanged;
public ArtistAdapter(Context context, List<Serializable> artists, OnItemClickedListener listener) {
this(context, artists, null, listener, null);
}
public ArtistAdapter(Context context, List<Serializable> artists, OnItemClickedListener listener) {
this(context, artists, null, listener, null);
}
public ArtistAdapter(Context context, List<Serializable> artists, List<MusicFolder> musicFolders, OnItemClickedListener onItemClickedListener, OnMusicFolderChanged onMusicFolderChanged) {
super(context, artists);
this.musicFolders = musicFolders;
this.onItemClickedListener = onItemClickedListener;
this.onMusicFolderChanged = onMusicFolderChanged;
public ArtistAdapter(Context context, List<Serializable> artists, List<MusicFolder> musicFolders, OnItemClickedListener onItemClickedListener, OnMusicFolderChanged onMusicFolderChanged) {
super(context, artists);
this.musicFolders = musicFolders;
this.onItemClickedListener = onItemClickedListener;
this.onMusicFolderChanged = onMusicFolderChanged;
if(musicFolders != null) {
this.singleSectionHeader = true;
}
}
if(musicFolders != null) {
this.singleSectionHeader = true;
}
}
@Override
public UpdateView.UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) {
final View header = LayoutInflater.from(context).inflate(R.layout.select_artist_header, parent, false);
header.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(context, header.findViewById(R.id.select_artist_folder_2));
@Override
public UpdateView.UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) {
final View header = LayoutInflater.from(context).inflate(R.layout.select_artist_header, parent, false);
header.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(context, header.findViewById(R.id.select_artist_folder_2));
popup.getMenu().add(R.string.select_artist_all_folders);
for (MusicFolder musicFolder : musicFolders) {
popup.getMenu().add(musicFolder.getName());
}
popup.getMenu().add(R.string.select_artist_all_folders);
for (MusicFolder musicFolder : musicFolders) {
popup.getMenu().add(musicFolder.getName());
}
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
for (MusicFolder musicFolder : musicFolders) {
if(item.getTitle().equals(musicFolder.getName())) {
if(onMusicFolderChanged != null) {
onMusicFolderChanged.onMusicFolderChanged(musicFolder);
}
return true;
}
}
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
for (MusicFolder musicFolder : musicFolders) {
if(item.getTitle().equals(musicFolder.getName())) {
if(onMusicFolderChanged != null) {
onMusicFolderChanged.onMusicFolderChanged(musicFolder);
}
return true;
}
}
if(onMusicFolderChanged != null) {
onMusicFolderChanged.onMusicFolderChanged(null);
}
return true;
}
});
popup.show();
}
});
if(onMusicFolderChanged != null) {
onMusicFolderChanged.onMusicFolderChanged(null);
}
return true;
}
});
popup.show();
}
});
return new UpdateView.UpdateViewHolder(header, false);
}
@Override
public void onBindHeaderHolder(UpdateView.UpdateViewHolder holder, String header, int sectionIndex) {
TextView folderName = (TextView) holder.getView().findViewById(R.id.select_artist_folder_2);
return new UpdateView.UpdateViewHolder(header, false);
}
@Override
public void onBindHeaderHolder(UpdateView.UpdateViewHolder holder, String header, int sectionIndex) {
TextView folderName = (TextView) holder.getView().findViewById(R.id.select_artist_folder_2);
String musicFolderId = Util.getSelectedMusicFolderId(context);
if(musicFolderId != null) {
for (MusicFolder musicFolder : musicFolders) {
if (musicFolder.getId().equals(musicFolderId)) {
folderName.setText(musicFolder.getName());
break;
}
}
} else {
folderName.setText(R.string.select_artist_all_folders);
}
}
String musicFolderId = Util.getSelectedMusicFolderId(context);
if(musicFolderId != null) {
for (MusicFolder musicFolder : musicFolders) {
if (musicFolder.getId().equals(musicFolderId)) {
folderName.setText(musicFolder.getName());
break;
}
}
} else {
folderName.setText(R.string.select_artist_all_folders);
}
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
UpdateView updateView = null;
if(viewType == VIEW_TYPE_ARTIST) {
updateView = new ArtistView(context);
} else if(viewType == VIEW_TYPE_SONG) {
updateView = new SongView(context);
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
UpdateView updateView = null;
if(viewType == VIEW_TYPE_ARTIST) {
updateView = new ArtistView(context);
} else if(viewType == VIEW_TYPE_SONG) {
updateView = new SongView(context);
}
return new UpdateView.UpdateViewHolder(updateView);
}
return new UpdateView.UpdateViewHolder(updateView);
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Serializable item, int viewType) {
UpdateView view = holder.getUpdateView();
if(viewType == VIEW_TYPE_ARTIST) {
view.setObject(item);
} else if(viewType == VIEW_TYPE_SONG) {
SongView songView = (SongView) view;
Entry entry = (Entry) item;
songView.setObject(entry, checkable);
}
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Serializable item, int viewType) {
UpdateView view = holder.getUpdateView();
if(viewType == VIEW_TYPE_ARTIST) {
view.setObject(item);
} else if(viewType == VIEW_TYPE_SONG) {
SongView songView = (SongView) view;
Entry entry = (Entry) item;
songView.setObject(entry, checkable);
}
}
@Override
public int getItemViewType(Serializable item) {
if(item instanceof Artist) {
return VIEW_TYPE_ARTIST;
} else {
return VIEW_TYPE_SONG;
}
}
@Override
public int getItemViewType(Serializable item) {
if(item instanceof Artist) {
return VIEW_TYPE_ARTIST;
} else {
return VIEW_TYPE_SONG;
}
}
@Override
public String getTextToShowInBubble(int position) {
Object item = getItemForPosition(position);
if(item instanceof Artist) {
return getNameIndex(((Artist) item).getName(), true);
} else {
return null;
}
}
@Override
public String getTextToShowInBubble(int position) {
Object item = getItemForPosition(position);
if(item instanceof Artist) {
return getNameIndex(((Artist) item).getName(), true);
} else {
return null;
}
}
public interface OnMusicFolderChanged {
void onMusicFolderChanged(MusicFolder musicFolder);
}
public interface OnMusicFolderChanged {
void onMusicFolderChanged(MusicFolder musicFolder);
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.adapter;
@ -24,25 +24,25 @@ import net.nullsum.audinaut.view.BasicListView;
import net.nullsum.audinaut.view.UpdateView;
public class BasicListAdapter extends SectionAdapter<String> {
public static int VIEW_TYPE_LINE = 1;
public static int VIEW_TYPE_LINE = 1;
public BasicListAdapter(Context context, List<String> strings, OnItemClickedListener listener) {
super(context, strings);
this.onItemClickedListener = listener;
}
public BasicListAdapter(Context context, List<String> strings, OnItemClickedListener listener) {
super(context, strings);
this.onItemClickedListener = listener;
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
return new UpdateView.UpdateViewHolder(new BasicListView(context));
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
return new UpdateView.UpdateViewHolder(new BasicListView(context));
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, String item, int viewType) {
holder.getUpdateView().setObject(item);
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, String item, int viewType) {
holder.getUpdateView().setObject(item);
}
@Override
public int getItemViewType(String item) {
return VIEW_TYPE_LINE;
}
@Override
public int getItemViewType(String item) {
return VIEW_TYPE_LINE;
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.adapter;
@ -30,33 +30,33 @@ import java.util.List;
import net.nullsum.audinaut.R;
public class DetailsAdapter extends ArrayAdapter<String> {
private List<String> headers;
private List<String> details;
private List<String> headers;
private List<String> details;
public DetailsAdapter(Context context, int layout, List<String> headers, List<String> details) {
super(context, layout, headers);
public DetailsAdapter(Context context, int layout, List<String> headers, List<String> details) {
super(context, layout, headers);
this.headers = headers;
this.details = details;
}
this.headers = headers;
this.details = details;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View view;
if(convertView == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.details_item, null);
} else {
view = convertView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View view;
if(convertView == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.details_item, null);
} else {
view = convertView;
}
TextView nameView = (TextView) view.findViewById(R.id.detail_name);
TextView detailsView = (TextView) view.findViewById(R.id.detail_value);
TextView nameView = (TextView) view.findViewById(R.id.detail_name);
TextView detailsView = (TextView) view.findViewById(R.id.detail_value);
nameView.setText(headers.get(position));
nameView.setText(headers.get(position));
detailsView.setText(details.get(position));
Linkify.addLinks(detailsView, Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES);
detailsView.setText(details.get(position));
Linkify.addLinks(detailsView, Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES);
return view;
}
return view;
}
}

View File

@ -1,16 +1,16 @@
/*
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 2014 (C) Scott Jackson
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 2014 (C) Scott Jackson
*/
package net.nullsum.audinaut.adapter;
@ -33,42 +33,42 @@ import net.nullsum.audinaut.view.SongView;
import net.nullsum.audinaut.view.UpdateView;
public class DownloadFileAdapter extends SectionAdapter<DownloadFile> implements FastScroller.BubbleTextGetter {
public static int VIEW_TYPE_DOWNLOAD_FILE = 1;
public static int VIEW_TYPE_DOWNLOAD_FILE = 1;
public DownloadFileAdapter(Context context, List<DownloadFile> entries, OnItemClickedListener onItemClickedListener) {
super(context, entries);
this.onItemClickedListener = onItemClickedListener;
this.checkable = true;
}
public DownloadFileAdapter(Context context, List<DownloadFile> entries, OnItemClickedListener onItemClickedListener) {
super(context, entries);
this.onItemClickedListener = onItemClickedListener;
this.checkable = true;
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
return new UpdateView.UpdateViewHolder(new SongView(context));
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
return new UpdateView.UpdateViewHolder(new SongView(context));
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, DownloadFile item, int viewType) {
SongView songView = (SongView) holder.getUpdateView();
songView.setObject(item.getSong(), Util.isBatchMode(context));
songView.setDownloadFile(item);
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, DownloadFile item, int viewType) {
SongView songView = (SongView) holder.getUpdateView();
songView.setObject(item.getSong(), Util.isBatchMode(context));
songView.setDownloadFile(item);
}
@Override
public int getItemViewType(DownloadFile item) {
return VIEW_TYPE_DOWNLOAD_FILE;
}
@Override
public int getItemViewType(DownloadFile item) {
return VIEW_TYPE_DOWNLOAD_FILE;
}
@Override
public String getTextToShowInBubble(int position) {
return null;
}
@Override
public String getTextToShowInBubble(int position) {
return null;
}
@Override
public void onCreateActionModeMenu(Menu menu, MenuInflater menuInflater) {
if(Util.isOffline(context)) {
menuInflater.inflate(R.menu.multiselect_nowplaying_offline, menu);
} else {
menuInflater.inflate(R.menu.multiselect_nowplaying, menu);
}
}
@Override
public void onCreateActionModeMenu(Menu menu, MenuInflater menuInflater) {
if(Util.isOffline(context)) {
menuInflater.inflate(R.menu.multiselect_nowplaying_offline, menu);
} else {
menuInflater.inflate(R.menu.multiselect_nowplaying, menu);
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.adapter;
@ -35,122 +35,122 @@ import net.nullsum.audinaut.view.UpdateView;
import net.nullsum.audinaut.view.UpdateView.UpdateViewHolder;
public class EntryGridAdapter extends SectionAdapter<Entry> {
private static String TAG = EntryGridAdapter.class.getSimpleName();
private static String TAG = EntryGridAdapter.class.getSimpleName();
public static int VIEW_TYPE_ALBUM_CELL = 1;
public static int VIEW_TYPE_ALBUM_LINE = 2;
public static int VIEW_TYPE_SONG = 3;
public static int VIEW_TYPE_ALBUM_CELL = 1;
public static int VIEW_TYPE_ALBUM_LINE = 2;
public static int VIEW_TYPE_SONG = 3;
private ImageLoader imageLoader;
private boolean largeAlbums;
private boolean showArtist = false;
private boolean showAlbum = false;
private boolean removeFromPlaylist = false;
private View header;
private ImageLoader imageLoader;
private boolean largeAlbums;
private boolean showArtist = false;
private boolean showAlbum = false;
private boolean removeFromPlaylist = false;
private View header;
public EntryGridAdapter(Context context, List<Entry> entries, ImageLoader imageLoader, boolean largeCell) {
super(context, entries);
this.imageLoader = imageLoader;
this.largeAlbums = largeCell;
public EntryGridAdapter(Context context, List<Entry> entries, ImageLoader imageLoader, boolean largeCell) {
super(context, entries);
this.imageLoader = imageLoader;
this.largeAlbums = largeCell;
// Always show artist if they aren't all the same
String artist = null;
for(MusicDirectory.Entry entry: entries) {
if(artist == null) {
artist = entry.getArtist();
}
// Always show artist if they aren't all the same
String artist = null;
for(MusicDirectory.Entry entry: entries) {
if(artist == null) {
artist = entry.getArtist();
}
if(artist != null && !artist.equals(entry.getArtist())) {
showArtist = true;
}
}
checkable = true;
}
if(artist != null && !artist.equals(entry.getArtist())) {
showArtist = true;
}
}
checkable = true;
}
@Override
public UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
UpdateView updateView = null;
if(viewType == VIEW_TYPE_ALBUM_LINE || viewType == VIEW_TYPE_ALBUM_CELL) {
updateView = new AlbumView(context, viewType == VIEW_TYPE_ALBUM_CELL);
} else if(viewType == VIEW_TYPE_SONG) {
updateView = new SongView(context);
}
@Override
public UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
UpdateView updateView = null;
if(viewType == VIEW_TYPE_ALBUM_LINE || viewType == VIEW_TYPE_ALBUM_CELL) {
updateView = new AlbumView(context, viewType == VIEW_TYPE_ALBUM_CELL);
} else if(viewType == VIEW_TYPE_SONG) {
updateView = new SongView(context);
}
return new UpdateViewHolder(updateView);
}
return new UpdateViewHolder(updateView);
}
@Override
public void onBindViewHolder(UpdateViewHolder holder, Entry entry, int viewType) {
UpdateView view = holder.getUpdateView();
if(viewType == VIEW_TYPE_ALBUM_CELL || viewType == VIEW_TYPE_ALBUM_LINE) {
AlbumView albumView = (AlbumView) view;
albumView.setShowArtist(showArtist);
albumView.setObject(entry, imageLoader);
} else if(viewType == VIEW_TYPE_SONG) {
SongView songView = (SongView) view;
songView.setShowAlbum(showAlbum);
songView.setObject(entry, checkable);
}
}
@Override
public void onBindViewHolder(UpdateViewHolder holder, Entry entry, int viewType) {
UpdateView view = holder.getUpdateView();
if(viewType == VIEW_TYPE_ALBUM_CELL || viewType == VIEW_TYPE_ALBUM_LINE) {
AlbumView albumView = (AlbumView) view;
albumView.setShowArtist(showArtist);
albumView.setObject(entry, imageLoader);
} else if(viewType == VIEW_TYPE_SONG) {
SongView songView = (SongView) view;
songView.setShowAlbum(showAlbum);
songView.setObject(entry, checkable);
}
}
public UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) {
return new UpdateViewHolder(header, false);
}
public void onBindHeaderHolder(UpdateViewHolder holder, String header, int sectionIndex) {
public UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) {
return new UpdateViewHolder(header, false);
}
public void onBindHeaderHolder(UpdateViewHolder holder, String header, int sectionIndex) {
}
}
@Override
public int getItemViewType(Entry entry) {
if(entry.isDirectory()) {
if (largeAlbums) {
return VIEW_TYPE_ALBUM_CELL;
} else {
return VIEW_TYPE_ALBUM_LINE;
}
} else {
return VIEW_TYPE_SONG;
}
}
@Override
public int getItemViewType(Entry entry) {
if(entry.isDirectory()) {
if (largeAlbums) {
return VIEW_TYPE_ALBUM_CELL;
} else {
return VIEW_TYPE_ALBUM_LINE;
}
} else {
return VIEW_TYPE_SONG;
}
}
public void setHeader(View header) {
this.header = header;
this.singleSectionHeader = true;
}
public View getHeader() {
return header;
}
public void setHeader(View header) {
this.header = header;
this.singleSectionHeader = true;
}
public View getHeader() {
return header;
}
public void setShowArtist(boolean showArtist) {
this.showArtist = showArtist;
}
public void setShowArtist(boolean showArtist) {
this.showArtist = showArtist;
}
public void setShowAlbum(boolean showAlbum) {
this.showAlbum = showAlbum;
}
public void setShowAlbum(boolean showAlbum) {
this.showAlbum = showAlbum;
}
public void removeAt(int index) {
sections.get(0).remove(index);
if(header != null) {
index++;
}
notifyItemRemoved(index);
}
public void removeAt(int index) {
sections.get(0).remove(index);
if(header != null) {
index++;
}
notifyItemRemoved(index);
}
public void setRemoveFromPlaylist(boolean removeFromPlaylist) {
this.removeFromPlaylist = removeFromPlaylist;
}
public void setRemoveFromPlaylist(boolean removeFromPlaylist) {
this.removeFromPlaylist = removeFromPlaylist;
}
@Override
public void onCreateActionModeMenu(Menu menu, MenuInflater menuInflater) {
if(Util.isOffline(context)) {
menuInflater.inflate(R.menu.multiselect_media_offline, menu);
} else {
menuInflater.inflate(R.menu.multiselect_media, menu);
}
@Override
public void onCreateActionModeMenu(Menu menu, MenuInflater menuInflater) {
if(Util.isOffline(context)) {
menuInflater.inflate(R.menu.multiselect_media_offline, menu);
} else {
menuInflater.inflate(R.menu.multiselect_media, menu);
}
if(!removeFromPlaylist) {
menu.removeItem(R.id.menu_remove_playlist);
}
}
if(!removeFromPlaylist) {
menu.removeItem(R.id.menu_remove_playlist);
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.adapter;
@ -33,120 +33,120 @@ import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.view.UpdateView;
public class EntryInfiniteGridAdapter extends EntryGridAdapter {
public static int VIEW_TYPE_LOADING = 4;
public static int VIEW_TYPE_LOADING = 4;
private String type;
private String extra;
private int size;
private String type;
private String extra;
private int size;
private boolean loading = false;
private boolean allLoaded = false;
private boolean loading = false;
private boolean allLoaded = false;
public EntryInfiniteGridAdapter(Context context, List<Entry> entries, ImageLoader imageLoader, boolean largeCell) {
super(context, entries, imageLoader, largeCell);
}
public EntryInfiniteGridAdapter(Context context, List<Entry> entries, ImageLoader imageLoader, boolean largeCell) {
super(context, entries, imageLoader, largeCell);
}
@Override
public UpdateView.UpdateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == VIEW_TYPE_LOADING) {
View progress = LayoutInflater.from(context).inflate(R.layout.tab_progress, null);
progress.setVisibility(View.VISIBLE);
return new UpdateView.UpdateViewHolder(progress, false);
}
@Override
public UpdateView.UpdateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == VIEW_TYPE_LOADING) {
View progress = LayoutInflater.from(context).inflate(R.layout.tab_progress, null);
progress.setVisibility(View.VISIBLE);
return new UpdateView.UpdateViewHolder(progress, false);
}
return super.onCreateViewHolder(parent, viewType);
}
return super.onCreateViewHolder(parent, viewType);
}
@Override
public int getItemViewType(int position) {
if(isLoadingView(position)) {
return VIEW_TYPE_LOADING;
}
@Override
public int getItemViewType(int position) {
if(isLoadingView(position)) {
return VIEW_TYPE_LOADING;
}
return super.getItemViewType(position);
}
return super.getItemViewType(position);
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, int position) {
if(!isLoadingView(position)) {
super.onBindViewHolder(holder, position);
}
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, int position) {
if(!isLoadingView(position)) {
super.onBindViewHolder(holder, position);
}
}
@Override
public int getItemCount() {
int size = super.getItemCount();
@Override
public int getItemCount() {
int size = super.getItemCount();
if(!allLoaded) {
size++;
}
if(!allLoaded) {
size++;
}
return size;
}
return size;
}
public void setData(String type, String extra, int size) {
this.type = type;
this.extra = extra;
this.size = size;
public void setData(String type, String extra, int size) {
this.type = type;
this.extra = extra;
this.size = size;
if(super.getItemCount() < size) {
allLoaded = true;
}
}
if(super.getItemCount() < size) {
allLoaded = true;
}
}
public void loadMore() {
if(loading || allLoaded) {
return;
}
loading = true;
public void loadMore() {
if(loading || allLoaded) {
return;
}
loading = true;
new SilentBackgroundTask<Void>(context) {
private List<Entry> newData;
new SilentBackgroundTask<Void>(context) {
private List<Entry> newData;
@Override
protected Void doInBackground() throws Throwable {
newData = cacheInBackground();
return null;
}
@Override
protected Void doInBackground() throws Throwable {
newData = cacheInBackground();
return null;
}
@Override
protected void done(Void result) {
appendCachedData(newData);
loading = false;
@Override
protected void done(Void result) {
appendCachedData(newData);
loading = false;
if(newData.size() < size) {
allLoaded = true;
notifyDataSetChanged();
}
}
}.execute();
}
if(newData.size() < size) {
allLoaded = true;
notifyDataSetChanged();
}
}
}.execute();
}
protected List<Entry> cacheInBackground() throws Exception {
MusicService service = MusicServiceFactory.getMusicService(context);
MusicDirectory result;
int offset = sections.get(0).size();
if("genres".equals(type) || "years".equals(type)) {
result = service.getAlbumList(type, extra, size, offset, false, context, null);
} else if("genres".equals(type) || "genres-songs".equals(type)) {
result = service.getSongsByGenre(extra, size, offset, context, null);
}else if(type.indexOf(MainFragment.SONGS_LIST_PREFIX) != -1) {
result = service.getSongList(type, size, offset, context, null);
} else {
result = service.getAlbumList(type, size, offset, false, context, null);
}
return result.getChildren();
}
protected List<Entry> cacheInBackground() throws Exception {
MusicService service = MusicServiceFactory.getMusicService(context);
MusicDirectory result;
int offset = sections.get(0).size();
if("genres".equals(type) || "years".equals(type)) {
result = service.getAlbumList(type, extra, size, offset, false, context, null);
} else if("genres".equals(type) || "genres-songs".equals(type)) {
result = service.getSongsByGenre(extra, size, offset, context, null);
}else if(type.indexOf(MainFragment.SONGS_LIST_PREFIX) != -1) {
result = service.getSongList(type, size, offset, context, null);
} else {
result = service.getAlbumList(type, size, offset, false, context, null);
}
return result.getChildren();
}
protected void appendCachedData(List<Entry> newData) {
if(newData.size() > 0) {
int start = sections.get(0).size();
sections.get(0).addAll(newData);
this.notifyItemRangeInserted(start, newData.size());
}
}
protected void appendCachedData(List<Entry> newData) {
if(newData.size() > 0) {
int start = sections.get(0).size();
sections.get(0).addAll(newData);
this.notifyItemRangeInserted(start, newData.size());
}
}
protected boolean isLoadingView(int position) {
return !allLoaded && position >= sections.get(0).size();
}
protected boolean isLoadingView(int position) {
return !allLoaded && position >= sections.get(0).size();
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.adapter;
@ -31,120 +31,120 @@ import net.nullsum.audinaut.view.BasicHeaderView;
import net.nullsum.audinaut.view.UpdateView;
public abstract class ExpandableSectionAdapter<T> extends SectionAdapter<T> {
private static final String TAG = ExpandableSectionAdapter.class.getSimpleName();
private static final int DEFAULT_VISIBLE = 4;
private static final int EXPAND_TOGGLE = R.attr.select_server;
private static final int COLLAPSE_TOGGLE = R.attr.select_tabs;
private static final String TAG = ExpandableSectionAdapter.class.getSimpleName();
private static final int DEFAULT_VISIBLE = 4;
private static final int EXPAND_TOGGLE = R.attr.select_server;
private static final int COLLAPSE_TOGGLE = R.attr.select_tabs;
protected List<Integer> sectionsDefaultVisible;
protected List<List<T>> sectionsExtras;
protected int expandToggleRes;
protected int collapseToggleRes;
protected List<Integer> sectionsDefaultVisible;
protected List<List<T>> sectionsExtras;
protected int expandToggleRes;
protected int collapseToggleRes;
protected ExpandableSectionAdapter() {}
public ExpandableSectionAdapter(Context context, List<T> section) {
List<List<T>> sections = new ArrayList<>();
sections.add(section);
protected ExpandableSectionAdapter() {}
public ExpandableSectionAdapter(Context context, List<T> section) {
List<List<T>> sections = new ArrayList<>();
sections.add(section);
init(context, Arrays.asList("Section"), sections, Arrays.asList((Integer) null));
}
public ExpandableSectionAdapter(Context context, List<String> headers, List<List<T>> sections) {
init(context, headers, sections, null);
}
public ExpandableSectionAdapter(Context context, List<String> headers, List<List<T>> sections, List<Integer> sectionsDefaultVisible) {
init(context, headers, sections, sectionsDefaultVisible);
}
protected void init(Context context, List<String> headers, List<List<T>> fullSections, List<Integer> sectionsDefaultVisible) {
this.context = context;
this.headers = headers;
this.sectionsDefaultVisible = sectionsDefaultVisible;
if(sectionsDefaultVisible == null) {
sectionsDefaultVisible = new ArrayList<>(fullSections.size());
for(int i = 0; i < fullSections.size(); i++) {
sectionsDefaultVisible.add(DEFAULT_VISIBLE);
}
}
init(context, Arrays.asList("Section"), sections, Arrays.asList((Integer) null));
}
public ExpandableSectionAdapter(Context context, List<String> headers, List<List<T>> sections) {
init(context, headers, sections, null);
}
public ExpandableSectionAdapter(Context context, List<String> headers, List<List<T>> sections, List<Integer> sectionsDefaultVisible) {
init(context, headers, sections, sectionsDefaultVisible);
}
protected void init(Context context, List<String> headers, List<List<T>> fullSections, List<Integer> sectionsDefaultVisible) {
this.context = context;
this.headers = headers;
this.sectionsDefaultVisible = sectionsDefaultVisible;
if(sectionsDefaultVisible == null) {
sectionsDefaultVisible = new ArrayList<>(fullSections.size());
for(int i = 0; i < fullSections.size(); i++) {
sectionsDefaultVisible.add(DEFAULT_VISIBLE);
}
}
this.sections = new ArrayList<>();
this.sectionsExtras = new ArrayList<>();
int i = 0;
for(List<T> fullSection: fullSections) {
List<T> visibleSection = new ArrayList<>();
this.sections = new ArrayList<>();
this.sectionsExtras = new ArrayList<>();
int i = 0;
for(List<T> fullSection: fullSections) {
List<T> visibleSection = new ArrayList<>();
Integer defaultVisible = sectionsDefaultVisible.get(i);
if(defaultVisible == null || defaultVisible >= fullSection.size()) {
visibleSection.addAll(fullSection);
this.sectionsExtras.add(null);
} else {
visibleSection.addAll(fullSection.subList(0, defaultVisible));
this.sectionsExtras.add(fullSection.subList(defaultVisible, fullSection.size()));
}
this.sections.add(visibleSection);
Integer defaultVisible = sectionsDefaultVisible.get(i);
if(defaultVisible == null || defaultVisible >= fullSection.size()) {
visibleSection.addAll(fullSection);
this.sectionsExtras.add(null);
} else {
visibleSection.addAll(fullSection.subList(0, defaultVisible));
this.sectionsExtras.add(fullSection.subList(defaultVisible, fullSection.size()));
}
this.sections.add(visibleSection);
i++;
}
i++;
}
expandToggleRes = DrawableTint.getDrawableRes(context, EXPAND_TOGGLE);
collapseToggleRes = DrawableTint.getDrawableRes(context, COLLAPSE_TOGGLE);
}
expandToggleRes = DrawableTint.getDrawableRes(context, EXPAND_TOGGLE);
collapseToggleRes = DrawableTint.getDrawableRes(context, COLLAPSE_TOGGLE);
}
@Override
public UpdateView.UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) {
return new UpdateView.UpdateViewHolder(new BasicHeaderView(context, R.layout.expandable_header));
}
@Override
public UpdateView.UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) {
return new UpdateView.UpdateViewHolder(new BasicHeaderView(context, R.layout.expandable_header));
}
@Override
public void onBindHeaderHolder(UpdateView.UpdateViewHolder holder, String header, final int sectionIndex) {
UpdateView view = holder.getUpdateView();
ImageView toggleSelectionView = (ImageView) view.findViewById(R.id.item_select);
@Override
public void onBindHeaderHolder(UpdateView.UpdateViewHolder holder, String header, final int sectionIndex) {
UpdateView view = holder.getUpdateView();
ImageView toggleSelectionView = (ImageView) view.findViewById(R.id.item_select);
List<T> visibleSelection = sections.get(sectionIndex);
List<T> sectionExtras = sectionsExtras.get(sectionIndex);
List<T> visibleSelection = sections.get(sectionIndex);
List<T> sectionExtras = sectionsExtras.get(sectionIndex);
if(sectionExtras != null && !sectionExtras.isEmpty()) {
toggleSelectionView.setVisibility(View.VISIBLE);
toggleSelectionView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<T> visibleSelection = sections.get(sectionIndex);
List<T> sectionExtras = sectionsExtras.get(sectionIndex);
if(sectionExtras != null && !sectionExtras.isEmpty()) {
toggleSelectionView.setVisibility(View.VISIBLE);
toggleSelectionView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<T> visibleSelection = sections.get(sectionIndex);
List<T> sectionExtras = sectionsExtras.get(sectionIndex);
// Update icon
int selectToggleAttr;
if (!visibleSelection.contains(sectionExtras.get(0))) {
selectToggleAttr = COLLAPSE_TOGGLE;
// Update icon
int selectToggleAttr;
if (!visibleSelection.contains(sectionExtras.get(0))) {
selectToggleAttr = COLLAPSE_TOGGLE;
// Update how many are displayed
int lastIndex = getItemPosition(visibleSelection.get(visibleSelection.size() - 1));
visibleSelection.addAll(sectionExtras);
notifyItemRangeInserted(lastIndex, sectionExtras.size());
} else {
selectToggleAttr = EXPAND_TOGGLE;
// Update how many are displayed
int lastIndex = getItemPosition(visibleSelection.get(visibleSelection.size() - 1));
visibleSelection.addAll(sectionExtras);
notifyItemRangeInserted(lastIndex, sectionExtras.size());
} else {
selectToggleAttr = EXPAND_TOGGLE;
// Update how many are displayed
visibleSelection.removeAll(sectionExtras);
int lastIndex = getItemPosition(visibleSelection.get(visibleSelection.size() - 1));
notifyItemRangeRemoved(lastIndex, sectionExtras.size());
}
// Update how many are displayed
visibleSelection.removeAll(sectionExtras);
int lastIndex = getItemPosition(visibleSelection.get(visibleSelection.size() - 1));
notifyItemRangeRemoved(lastIndex, sectionExtras.size());
}
((ImageView) v).setImageResource(DrawableTint.getDrawableRes(context, selectToggleAttr));
}
});
((ImageView) v).setImageResource(DrawableTint.getDrawableRes(context, selectToggleAttr));
}
});
int selectToggleAttr;
if (!visibleSelection.contains(sectionExtras.get(0))) {
selectToggleAttr = EXPAND_TOGGLE;
} else {
selectToggleAttr = COLLAPSE_TOGGLE;
}
int selectToggleAttr;
if (!visibleSelection.contains(sectionExtras.get(0))) {
selectToggleAttr = EXPAND_TOGGLE;
} else {
selectToggleAttr = COLLAPSE_TOGGLE;
}
toggleSelectionView.setImageResource(DrawableTint.getDrawableRes(context, selectToggleAttr));
} else {
toggleSelectionView.setVisibility(View.GONE);
}
toggleSelectionView.setImageResource(DrawableTint.getDrawableRes(context, selectToggleAttr));
} else {
toggleSelectionView.setVisibility(View.GONE);
}
if(view != null) {
view.setObject(header);
}
}
if(view != null) {
view.setObject(header);
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.adapter;
@ -25,30 +25,30 @@ import net.nullsum.audinaut.view.UpdateView;
import java.util.List;
public class GenreAdapter extends SectionAdapter<Genre> implements FastScroller.BubbleTextGetter{
public static int VIEW_TYPE_GENRE = 1;
public static int VIEW_TYPE_GENRE = 1;
public GenreAdapter(Context context, List<Genre> genres, OnItemClickedListener listener) {
public GenreAdapter(Context context, List<Genre> genres, OnItemClickedListener listener) {
super(context, genres);
this.onItemClickedListener = listener;
this.onItemClickedListener = listener;
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
return new UpdateView.UpdateViewHolder(new GenreView(context));
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
return new UpdateView.UpdateViewHolder(new GenreView(context));
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Genre item, int viewType) {
holder.getUpdateView().setObject(item);
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Genre item, int viewType) {
holder.getUpdateView().setObject(item);
}
@Override
public int getItemViewType(Genre item) {
return VIEW_TYPE_GENRE;
}
@Override
public int getItemViewType(Genre item) {
return VIEW_TYPE_GENRE;
}
@Override
public String getTextToShowInBubble(int position) {
return getNameIndex(getItemForPosition(position).getName());
}
@Override
public String getTextToShowInBubble(int position) {
return getNameIndex(getItemForPosition(position).getName());
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.adapter;
@ -30,55 +30,55 @@ import net.nullsum.audinaut.view.BasicListView;
import net.nullsum.audinaut.view.UpdateView;
public class MainAdapter extends SectionAdapter<Integer> {
public static final int VIEW_TYPE_ALBUM_LIST = 1;
public static final int VIEW_TYPE_ALBUM_LIST = 1;
public MainAdapter(Context context, List<String> headers, List<List<Integer>> sections, OnItemClickedListener onItemClickedListener) {
super(context, headers, sections);
this.onItemClickedListener = onItemClickedListener;
}
public MainAdapter(Context context, List<String> headers, List<List<Integer>> sections, OnItemClickedListener onItemClickedListener) {
super(context, headers, sections);
this.onItemClickedListener = onItemClickedListener;
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
UpdateView updateView = new BasicListView(context);
return new UpdateView.UpdateViewHolder(updateView);
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
UpdateView updateView = new BasicListView(context);
return new UpdateView.UpdateViewHolder(updateView);
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Integer item, int viewType) {
UpdateView updateView = holder.getUpdateView();
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Integer item, int viewType) {
UpdateView updateView = holder.getUpdateView();
if(viewType == VIEW_TYPE_ALBUM_LIST) {
updateView.setObject(context.getResources().getString(item));
} else {
updateView.setObject(item);
}
}
if(viewType == VIEW_TYPE_ALBUM_LIST) {
updateView.setObject(context.getResources().getString(item));
} else {
updateView.setObject(item);
}
}
@Override
public int getItemViewType(Integer item) {
return VIEW_TYPE_ALBUM_LIST;
}
@Override
public int getItemViewType(Integer item) {
return VIEW_TYPE_ALBUM_LIST;
}
@Override
public UpdateView.UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) {
return new UpdateView.UpdateViewHolder(new BasicHeaderView(context, R.layout.album_list_header));
}
@Override
public void onBindHeaderHolder(UpdateView.UpdateViewHolder holder, String header, int sectionIndex) {
UpdateView view = holder.getUpdateView();
CheckBox checkBox = (CheckBox) view.findViewById(R.id.item_checkbox);
@Override
public UpdateView.UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) {
return new UpdateView.UpdateViewHolder(new BasicHeaderView(context, R.layout.album_list_header));
}
@Override
public void onBindHeaderHolder(UpdateView.UpdateViewHolder holder, String header, int sectionIndex) {
UpdateView view = holder.getUpdateView();
CheckBox checkBox = (CheckBox) view.findViewById(R.id.item_checkbox);
String display;
if("songs".equals(header)) {
display = context.getResources().getString(R.string.search_songs);
checkBox.setVisibility(View.GONE);
} else {
display = header;
checkBox.setVisibility(View.GONE);
}
String display;
if("songs".equals(header)) {
display = context.getResources().getString(R.string.search_songs);
checkBox.setVisibility(View.GONE);
} else {
display = header;
checkBox.setVisibility(View.GONE);
}
if(view != null) {
view.setObject(display);
}
}
if(view != null) {
view.setObject(display);
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.adapter;
@ -26,47 +26,47 @@ import net.nullsum.audinaut.view.PlaylistView;
import net.nullsum.audinaut.view.UpdateView;
public class PlaylistAdapter extends SectionAdapter<Playlist> implements FastScroller.BubbleTextGetter {
public static int VIEW_TYPE_PLAYLIST = 1;
public static int VIEW_TYPE_PLAYLIST = 1;
private ImageLoader imageLoader;
private boolean largeCell;
private ImageLoader imageLoader;
private boolean largeCell;
public PlaylistAdapter(Context context, List<Playlist> playlists, ImageLoader imageLoader, boolean largeCell, OnItemClickedListener listener) {
super(context, playlists);
this.imageLoader = imageLoader;
this.largeCell = largeCell;
this.onItemClickedListener = listener;
}
public PlaylistAdapter(Context context, List<String> headers, List<List<Playlist>> sections, ImageLoader imageLoader, boolean largeCell, OnItemClickedListener listener) {
super(context, headers, sections);
this.imageLoader = imageLoader;
this.largeCell = largeCell;
this.onItemClickedListener = listener;
}
public PlaylistAdapter(Context context, List<Playlist> playlists, ImageLoader imageLoader, boolean largeCell, OnItemClickedListener listener) {
super(context, playlists);
this.imageLoader = imageLoader;
this.largeCell = largeCell;
this.onItemClickedListener = listener;
}
public PlaylistAdapter(Context context, List<String> headers, List<List<Playlist>> sections, ImageLoader imageLoader, boolean largeCell, OnItemClickedListener listener) {
super(context, headers, sections);
this.imageLoader = imageLoader;
this.largeCell = largeCell;
this.onItemClickedListener = listener;
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
return new UpdateView.UpdateViewHolder(new PlaylistView(context, imageLoader, largeCell));
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
return new UpdateView.UpdateViewHolder(new PlaylistView(context, imageLoader, largeCell));
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Playlist playlist, int viewType) {
holder.getUpdateView().setObject(playlist);
holder.setItem(playlist);
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Playlist playlist, int viewType) {
holder.getUpdateView().setObject(playlist);
holder.setItem(playlist);
}
@Override
public int getItemViewType(Playlist playlist) {
return VIEW_TYPE_PLAYLIST;
}
@Override
public int getItemViewType(Playlist playlist) {
return VIEW_TYPE_PLAYLIST;
}
@Override
public String getTextToShowInBubble(int position) {
Object item = getItemForPosition(position);
if(item instanceof Playlist) {
return getNameIndex(((Playlist) item).getName());
} else {
return null;
}
}
@Override
public String getTextToShowInBubble(int position) {
Object item = getItemForPosition(position);
if(item instanceof Playlist) {
return getNameIndex(((Playlist) item).getName());
} else {
return null;
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.adapter;
@ -45,96 +45,96 @@ import static net.nullsum.audinaut.adapter.EntryGridAdapter.VIEW_TYPE_ALBUM_LINE
import static net.nullsum.audinaut.adapter.EntryGridAdapter.VIEW_TYPE_SONG;
public class SearchAdapter extends ExpandableSectionAdapter<Serializable> {
private ImageLoader imageLoader;
private boolean largeAlbums;
private ImageLoader imageLoader;
private boolean largeAlbums;
private static final int MAX_ARTISTS = 10;
private static final int MAX_ALBUMS = 4;
private static final int MAX_SONGS = 10;
private static final int MAX_ARTISTS = 10;
private static final int MAX_ALBUMS = 4;
private static final int MAX_SONGS = 10;
public SearchAdapter(Context context, SearchResult searchResult, ImageLoader imageLoader, boolean largeAlbums, OnItemClickedListener listener) {
this.imageLoader = imageLoader;
this.largeAlbums = largeAlbums;
public SearchAdapter(Context context, SearchResult searchResult, ImageLoader imageLoader, boolean largeAlbums, OnItemClickedListener listener) {
this.imageLoader = imageLoader;
this.largeAlbums = largeAlbums;
List<List<Serializable>> sections = new ArrayList<>();
List<String> headers = new ArrayList<>();
List<Integer> defaultVisible = new ArrayList<>();
Resources res = context.getResources();
if(!searchResult.getArtists().isEmpty()) {
sections.add((List<Serializable>) (List<?>) searchResult.getArtists());
headers.add(res.getString(R.string.search_artists));
defaultVisible.add(MAX_ARTISTS);
}
if(!searchResult.getAlbums().isEmpty()) {
sections.add((List<Serializable>) (List<?>) searchResult.getAlbums());
headers.add(res.getString(R.string.search_albums));
defaultVisible.add(MAX_ALBUMS);
}
if(!searchResult.getSongs().isEmpty()) {
sections.add((List<Serializable>) (List<?>) searchResult.getSongs());
headers.add(res.getString(R.string.search_songs));
defaultVisible.add(MAX_SONGS);
}
init(context, headers, sections, defaultVisible);
List<List<Serializable>> sections = new ArrayList<>();
List<String> headers = new ArrayList<>();
List<Integer> defaultVisible = new ArrayList<>();
Resources res = context.getResources();
if(!searchResult.getArtists().isEmpty()) {
sections.add((List<Serializable>) (List<?>) searchResult.getArtists());
headers.add(res.getString(R.string.search_artists));
defaultVisible.add(MAX_ARTISTS);
}
if(!searchResult.getAlbums().isEmpty()) {
sections.add((List<Serializable>) (List<?>) searchResult.getAlbums());
headers.add(res.getString(R.string.search_albums));
defaultVisible.add(MAX_ALBUMS);
}
if(!searchResult.getSongs().isEmpty()) {
sections.add((List<Serializable>) (List<?>) searchResult.getSongs());
headers.add(res.getString(R.string.search_songs));
defaultVisible.add(MAX_SONGS);
}
init(context, headers, sections, defaultVisible);
this.onItemClickedListener = listener;
checkable = true;
}
this.onItemClickedListener = listener;
checkable = true;
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
UpdateView updateView = null;
if(viewType == VIEW_TYPE_ALBUM_CELL || viewType == VIEW_TYPE_ALBUM_LINE) {
updateView = new AlbumView(context, viewType == VIEW_TYPE_ALBUM_CELL);
} else if(viewType == VIEW_TYPE_SONG) {
updateView = new SongView(context);
} else if(viewType == VIEW_TYPE_ARTIST) {
updateView = new ArtistView(context);
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
UpdateView updateView = null;
if(viewType == VIEW_TYPE_ALBUM_CELL || viewType == VIEW_TYPE_ALBUM_LINE) {
updateView = new AlbumView(context, viewType == VIEW_TYPE_ALBUM_CELL);
} else if(viewType == VIEW_TYPE_SONG) {
updateView = new SongView(context);
} else if(viewType == VIEW_TYPE_ARTIST) {
updateView = new ArtistView(context);
}
return new UpdateView.UpdateViewHolder(updateView);
}
return new UpdateView.UpdateViewHolder(updateView);
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Serializable item, int viewType) {
UpdateView view = holder.getUpdateView();
if(viewType == VIEW_TYPE_ALBUM_CELL || viewType == VIEW_TYPE_ALBUM_LINE) {
AlbumView albumView = (AlbumView) view;
albumView.setObject((Entry) item, imageLoader);
} else if(viewType == VIEW_TYPE_SONG) {
SongView songView = (SongView) view;
songView.setObject((Entry) item, true);
} else if(viewType == VIEW_TYPE_ARTIST) {
view.setObject(item);
}
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Serializable item, int viewType) {
UpdateView view = holder.getUpdateView();
if(viewType == VIEW_TYPE_ALBUM_CELL || viewType == VIEW_TYPE_ALBUM_LINE) {
AlbumView albumView = (AlbumView) view;
albumView.setObject((Entry) item, imageLoader);
} else if(viewType == VIEW_TYPE_SONG) {
SongView songView = (SongView) view;
songView.setObject((Entry) item, true);
} else if(viewType == VIEW_TYPE_ARTIST) {
view.setObject(item);
}
}
@Override
public int getItemViewType(Serializable item) {
if(item instanceof Entry) {
Entry entry = (Entry) item;
if (entry.isDirectory()) {
if (largeAlbums) {
return VIEW_TYPE_ALBUM_CELL;
} else {
return VIEW_TYPE_ALBUM_LINE;
}
} else {
return VIEW_TYPE_SONG;
}
} else {
return VIEW_TYPE_ARTIST;
}
}
@Override
public int getItemViewType(Serializable item) {
if(item instanceof Entry) {
Entry entry = (Entry) item;
if (entry.isDirectory()) {
if (largeAlbums) {
return VIEW_TYPE_ALBUM_CELL;
} else {
return VIEW_TYPE_ALBUM_LINE;
}
} else {
return VIEW_TYPE_SONG;
}
} else {
return VIEW_TYPE_ARTIST;
}
}
@Override
public void onCreateActionModeMenu(Menu menu, MenuInflater menuInflater) {
if(Util.isOffline(context)) {
menuInflater.inflate(R.menu.multiselect_media_offline, menu);
} else {
menuInflater.inflate(R.menu.multiselect_media, menu);
}
@Override
public void onCreateActionModeMenu(Menu menu, MenuInflater menuInflater) {
if(Util.isOffline(context)) {
menuInflater.inflate(R.menu.multiselect_media_offline, menu);
} else {
menuInflater.inflate(R.menu.multiselect_media, menu);
}
menu.removeItem(R.id.menu_remove_playlist);
}
menu.removeItem(R.id.menu_remove_playlist);
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.adapter;
@ -46,471 +46,471 @@ import net.nullsum.audinaut.view.UpdateView;
import net.nullsum.audinaut.view.UpdateView.UpdateViewHolder;
public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewHolder<T>> {
private static String TAG = SectionAdapter.class.getSimpleName();
public static int VIEW_TYPE_HEADER = 0;
public static String[] ignoredArticles;
private static String TAG = SectionAdapter.class.getSimpleName();
public static int VIEW_TYPE_HEADER = 0;
public static String[] ignoredArticles;
protected Context context;
protected List<String> headers;
protected List<List<T>> sections;
protected boolean singleSectionHeader;
protected OnItemClickedListener<T> onItemClickedListener;
protected List<T> selected = new ArrayList<>();
protected List<UpdateView> selectedViews = new ArrayList<>();
protected ActionMode currentActionMode;
protected boolean checkable = false;
protected Context context;
protected List<String> headers;
protected List<List<T>> sections;
protected boolean singleSectionHeader;
protected OnItemClickedListener<T> onItemClickedListener;
protected List<T> selected = new ArrayList<>();
protected List<UpdateView> selectedViews = new ArrayList<>();
protected ActionMode currentActionMode;
protected boolean checkable = false;
protected SectionAdapter() {}
public SectionAdapter(Context context, List<T> section) {
this(context, section, false);
}
public SectionAdapter(Context context, List<T> section, boolean singleSectionHeader) {
this.context = context;
this.headers = Arrays.asList("Section");
this.sections = new ArrayList<>();
this.sections.add(section);
this.singleSectionHeader = singleSectionHeader;
}
public SectionAdapter(Context context, List<String> headers, List<List<T>> sections) {
this(context, headers, sections, true);
}
public SectionAdapter(Context context, List<String> headers, List<List<T>> sections, boolean singleSectionHeader){
this.context = context;
this.headers = headers;
this.sections = sections;
this.singleSectionHeader = singleSectionHeader;
}
protected SectionAdapter() {}
public SectionAdapter(Context context, List<T> section) {
this(context, section, false);
}
public SectionAdapter(Context context, List<T> section, boolean singleSectionHeader) {
this.context = context;
this.headers = Arrays.asList("Section");
this.sections = new ArrayList<>();
this.sections.add(section);
this.singleSectionHeader = singleSectionHeader;
}
public SectionAdapter(Context context, List<String> headers, List<List<T>> sections) {
this(context, headers, sections, true);
}
public SectionAdapter(Context context, List<String> headers, List<List<T>> sections, boolean singleSectionHeader){
this.context = context;
this.headers = headers;
this.sections = sections;
this.singleSectionHeader = singleSectionHeader;
}
public void replaceExistingData(List<T> section) {
this.sections = new ArrayList<>();
this.sections.add(section);
notifyDataSetChanged();
}
public void replaceExistingData(List<String> headers, List<List<T>> sections) {
this.headers = headers;
this.sections = sections;
notifyDataSetChanged();
}
public void replaceExistingData(List<T> section) {
this.sections = new ArrayList<>();
this.sections.add(section);
notifyDataSetChanged();
}
public void replaceExistingData(List<String> headers, List<List<T>> sections) {
this.headers = headers;
this.sections = sections;
notifyDataSetChanged();
}
@Override
public UpdateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == VIEW_TYPE_HEADER) {
return onCreateHeaderHolder(parent);
} else {
final UpdateViewHolder<T> holder = onCreateSectionViewHolder(parent, viewType);
final UpdateView updateView = holder.getUpdateView();
@Override
public UpdateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == VIEW_TYPE_HEADER) {
return onCreateHeaderHolder(parent);
} else {
final UpdateViewHolder<T> holder = onCreateSectionViewHolder(parent, viewType);
final UpdateView updateView = holder.getUpdateView();
if(updateView != null) {
updateView.getChildAt(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
T item = holder.getItem();
updateView.onClick();
if (currentActionMode != null) {
if(updateView.isCheckable()) {
if (selected.contains(item)) {
selected.remove(item);
selectedViews.remove(updateView);
setChecked(updateView, false);
} else {
selected.add(item);
selectedViews.add(updateView);
setChecked(updateView, true);
}
if(updateView != null) {
updateView.getChildAt(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
T item = holder.getItem();
updateView.onClick();
if (currentActionMode != null) {
if(updateView.isCheckable()) {
if (selected.contains(item)) {
selected.remove(item);
selectedViews.remove(updateView);
setChecked(updateView, false);
} else {
selected.add(item);
selectedViews.add(updateView);
setChecked(updateView, true);
}
if (selected.isEmpty()) {
currentActionMode.finish();
} else {
currentActionMode.setTitle(context.getResources().getString(R.string.select_album_n_selected, selected.size()));
}
}
} else if (onItemClickedListener != null) {
onItemClickedListener.onItemClicked(updateView, item);
}
}
});
if (selected.isEmpty()) {
currentActionMode.finish();
} else {
currentActionMode.setTitle(context.getResources().getString(R.string.select_album_n_selected, selected.size()));
}
}
} else if (onItemClickedListener != null) {
onItemClickedListener.onItemClicked(updateView, item);
}
}
});
View moreButton = updateView.findViewById(R.id.item_more);
if (moreButton != null) {
moreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
final T item = holder.getItem();
if (onItemClickedListener != null) {
PopupMenu popup = new PopupMenu(context, v);
onItemClickedListener.onCreateContextMenu(popup.getMenu(), popup.getMenuInflater(), updateView, item);
View moreButton = updateView.findViewById(R.id.item_more);
if (moreButton != null) {
moreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
final T item = holder.getItem();
if (onItemClickedListener != null) {
PopupMenu popup = new PopupMenu(context, v);
onItemClickedListener.onCreateContextMenu(popup.getMenu(), popup.getMenuInflater(), updateView, item);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
return onItemClickedListener.onContextItemSelected(menuItem, updateView, item);
}
});
popup.show();
}
} catch(Exception e) {
Log.w(TAG, "Failed to show popup", e);
}
}
});
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
return onItemClickedListener.onContextItemSelected(menuItem, updateView, item);
}
});
popup.show();
}
} catch(Exception e) {
Log.w(TAG, "Failed to show popup", e);
}
}
});
if(checkable) {
updateView.getChildAt(0).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if(updateView.isCheckable()) {
if (currentActionMode == null) {
startActionMode(holder);
} else {
updateView.getChildAt(0).performClick();
}
}
return true;
}
});
}
}
}
if(checkable) {
updateView.getChildAt(0).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if(updateView.isCheckable()) {
if (currentActionMode == null) {
startActionMode(holder);
} else {
updateView.getChildAt(0).performClick();
}
}
return true;
}
});
}
}
}
return holder;
}
}
return holder;
}
}
@Override
public void onBindViewHolder(UpdateViewHolder holder, int position) {
UpdateView updateView = holder.getUpdateView();
@Override
public void onBindViewHolder(UpdateViewHolder holder, int position) {
UpdateView updateView = holder.getUpdateView();
if(sections.size() == 1 && !singleSectionHeader) {
T item = sections.get(0).get(position);
onBindViewHolder(holder, item, getItemViewType(position));
postBindView(updateView, item);
holder.setItem(item);
return;
}
if(sections.size() == 1 && !singleSectionHeader) {
T item = sections.get(0).get(position);
onBindViewHolder(holder, item, getItemViewType(position));
postBindView(updateView, item);
holder.setItem(item);
return;
}
int subPosition = 0;
int subHeader = 0;
for(List<T> section: sections) {
boolean validHeader = headers.get(subHeader) != null;
if(position == subPosition && validHeader) {
onBindHeaderHolder(holder, headers.get(subHeader), subHeader);
return;
}
int subPosition = 0;
int subHeader = 0;
for(List<T> section: sections) {
boolean validHeader = headers.get(subHeader) != null;
if(position == subPosition && validHeader) {
onBindHeaderHolder(holder, headers.get(subHeader), subHeader);
return;
}
int headerOffset = validHeader ? 1 : 0;
if(position < (subPosition + section.size() + headerOffset)) {
T item = section.get(position - subPosition - headerOffset);
onBindViewHolder(holder, item, getItemViewType(item));
int headerOffset = validHeader ? 1 : 0;
if(position < (subPosition + section.size() + headerOffset)) {
T item = section.get(position - subPosition - headerOffset);
onBindViewHolder(holder, item, getItemViewType(item));
postBindView(updateView, item);
holder.setItem(item);
return;
}
postBindView(updateView, item);
holder.setItem(item);
return;
}
subPosition += section.size();
if(validHeader) {
subPosition += 1;
}
subHeader++;
}
}
subPosition += section.size();
if(validHeader) {
subPosition += 1;
}
subHeader++;
}
}
private void postBindView(UpdateView updateView, T item) {
if(updateView.isCheckable()) {
setChecked(updateView, selected.contains(item));
}
private void postBindView(UpdateView updateView, T item) {
if(updateView.isCheckable()) {
setChecked(updateView, selected.contains(item));
}
View moreButton = updateView.findViewById(R.id.item_more);
if(moreButton != null) {
if(onItemClickedListener != null) {
PopupMenu popup = new PopupMenu(context, moreButton);
Menu menu = popup.getMenu();
onItemClickedListener.onCreateContextMenu(popup.getMenu(), popup.getMenuInflater(), updateView, item);
if (menu.size() == 0) {
moreButton.setVisibility(View.GONE);
} else {
moreButton.setVisibility(View.VISIBLE);
}
} else {
moreButton.setVisibility(View.VISIBLE);
}
}
}
View moreButton = updateView.findViewById(R.id.item_more);
if(moreButton != null) {
if(onItemClickedListener != null) {
PopupMenu popup = new PopupMenu(context, moreButton);
Menu menu = popup.getMenu();
onItemClickedListener.onCreateContextMenu(popup.getMenu(), popup.getMenuInflater(), updateView, item);
if (menu.size() == 0) {
moreButton.setVisibility(View.GONE);
} else {
moreButton.setVisibility(View.VISIBLE);
}
} else {
moreButton.setVisibility(View.VISIBLE);
}
}
}
@Override
public int getItemCount() {
if(sections.size() == 1 && !singleSectionHeader) {
return sections.get(0).size();
}
@Override
public int getItemCount() {
if(sections.size() == 1 && !singleSectionHeader) {
return sections.get(0).size();
}
int count = 0;
for(String header: headers) {
if(header != null) {
count++;
}
}
for(List<T> section: sections) {
count += section.size();
}
int count = 0;
for(String header: headers) {
if(header != null) {
count++;
}
}
for(List<T> section: sections) {
count += section.size();
}
return count;
}
return count;
}
@Override
public int getItemViewType(int position) {
if(sections.size() == 1 && !singleSectionHeader) {
return getItemViewType(sections.get(0).get(position));
}
@Override
public int getItemViewType(int position) {
if(sections.size() == 1 && !singleSectionHeader) {
return getItemViewType(sections.get(0).get(position));
}
int subPosition = 0;
int subHeader = 0;
for(List<T> section: sections) {
boolean validHeader = headers.get(subHeader) != null;
if(position == subPosition && validHeader) {
return VIEW_TYPE_HEADER;
}
int subPosition = 0;
int subHeader = 0;
for(List<T> section: sections) {
boolean validHeader = headers.get(subHeader) != null;
if(position == subPosition && validHeader) {
return VIEW_TYPE_HEADER;
}
int headerOffset = validHeader ? 1 : 0;
if(position < (subPosition + section.size() + headerOffset)) {
return getItemViewType(section.get(position - subPosition - headerOffset));
}
int headerOffset = validHeader ? 1 : 0;
if(position < (subPosition + section.size() + headerOffset)) {
return getItemViewType(section.get(position - subPosition - headerOffset));
}
subPosition += section.size();
if(validHeader) {
subPosition += 1;
}
subHeader++;
}
subPosition += section.size();
if(validHeader) {
subPosition += 1;
}
subHeader++;
}
return -1;
}
return -1;
}
public UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) {
return new UpdateViewHolder(new BasicHeaderView(context));
}
public void onBindHeaderHolder(UpdateViewHolder holder, String header, int sectionIndex) {
UpdateView view = holder.getUpdateView();
if(view != null) {
view.setObject(header);
}
}
public UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) {
return new UpdateViewHolder(new BasicHeaderView(context));
}
public void onBindHeaderHolder(UpdateViewHolder holder, String header, int sectionIndex) {
UpdateView view = holder.getUpdateView();
if(view != null) {
view.setObject(header);
}
}
public T getItemForPosition(int position) {
if(sections.size() == 1 && !singleSectionHeader) {
return sections.get(0).get(position);
}
public T getItemForPosition(int position) {
if(sections.size() == 1 && !singleSectionHeader) {
return sections.get(0).get(position);
}
int subPosition = 0;
for(List<T> section: sections) {
if(position == subPosition) {
return null;
}
int subPosition = 0;
for(List<T> section: sections) {
if(position == subPosition) {
return null;
}
if(position <= (subPosition + section.size())) {
return section.get(position - subPosition - 1);
}
if(position <= (subPosition + section.size())) {
return section.get(position - subPosition - 1);
}
subPosition += section.size() + 1;
}
subPosition += section.size() + 1;
}
return null;
}
public int getItemPosition(T item) {
if(sections.size() == 1 && !singleSectionHeader) {
return sections.get(0).indexOf(item);
}
return null;
}
public int getItemPosition(T item) {
if(sections.size() == 1 && !singleSectionHeader) {
return sections.get(0).indexOf(item);
}
int subPosition = 0;
for(List<T> section: sections) {
subPosition += section.size() + 1;
int subPosition = 0;
for(List<T> section: sections) {
subPosition += section.size() + 1;
int position = section.indexOf(item);
if(position != -1) {
return position + subPosition;
}
}
int position = section.indexOf(item);
if(position != -1) {
return position + subPosition;
}
}
return -1;
}
return -1;
}
public void setOnItemClickedListener(OnItemClickedListener<T> onItemClickedListener) {
this.onItemClickedListener = onItemClickedListener;
}
public void setOnItemClickedListener(OnItemClickedListener<T> onItemClickedListener) {
this.onItemClickedListener = onItemClickedListener;
}
public void addSelected(T item) {
selected.add(item);
}
public List<T> getSelected() {
List<T> selected = new ArrayList<>();
selected.addAll(this.selected);
return selected;
}
public void addSelected(T item) {
selected.add(item);
}
public List<T> getSelected() {
List<T> selected = new ArrayList<>();
selected.addAll(this.selected);
return selected;
}
public void clearSelected() {
// TODO: This needs to work with multiple sections
for(T item: selected) {
int index = sections.get(0).indexOf(item);
public void clearSelected() {
// TODO: This needs to work with multiple sections
for(T item: selected) {
int index = sections.get(0).indexOf(item);
if(singleSectionHeader) {
index++;
}
}
selected.clear();
if(singleSectionHeader) {
index++;
}
}
selected.clear();
for(UpdateView updateView: selectedViews) {
updateView.setChecked(false);
}
}
for(UpdateView updateView: selectedViews) {
updateView.setChecked(false);
}
}
public void moveItem(int from, int to) {
List<T> section = sections.get(0);
int max = section.size();
if(to >= max) {
to = max - 1;
} else if(to < 0) {
to = 0;
}
public void moveItem(int from, int to) {
List<T> section = sections.get(0);
int max = section.size();
if(to >= max) {
to = max - 1;
} else if(to < 0) {
to = 0;
}
T moved = section.remove(from);
section.add(to, moved);
T moved = section.remove(from);
section.add(to, moved);
notifyItemMoved(from, to);
}
public void removeItem(T item) {
int subPosition = 0;
for(List<T> section: sections) {
if(sections.size() > 1 || singleSectionHeader) {
subPosition++;
}
notifyItemMoved(from, to);
}
public void removeItem(T item) {
int subPosition = 0;
for(List<T> section: sections) {
if(sections.size() > 1 || singleSectionHeader) {
subPosition++;
}
int index = section.indexOf(item);
if (index != -1) {
section.remove(item);
notifyItemRemoved(subPosition + index);
break;
}
int index = section.indexOf(item);
if (index != -1) {
section.remove(item);
notifyItemRemoved(subPosition + index);
break;
}
subPosition += section.size();
}
}
subPosition += section.size();
}
}
public abstract UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType);
public abstract void onBindViewHolder(UpdateViewHolder holder, T item, int viewType);
public abstract int getItemViewType(T item);
public void setCheckable(boolean checkable) {
this.checkable = checkable;
}
public void setChecked(UpdateView updateView, boolean checked) {
updateView.setChecked(checked);
}
public void onCreateActionModeMenu(Menu menu, MenuInflater menuInflater) {}
public abstract UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType);
public abstract void onBindViewHolder(UpdateViewHolder holder, T item, int viewType);
public abstract int getItemViewType(T item);
public void setCheckable(boolean checkable) {
this.checkable = checkable;
}
public void setChecked(UpdateView updateView, boolean checked) {
updateView.setChecked(checked);
}
public void onCreateActionModeMenu(Menu menu, MenuInflater menuInflater) {}
private void startActionMode(final UpdateView.UpdateViewHolder<T> holder) {
final UpdateView<T> updateView = holder.getUpdateView();
if (context instanceof SubsonicFragmentActivity && currentActionMode == null) {
final SubsonicFragmentActivity fragmentActivity = (SubsonicFragmentActivity) context;
fragmentActivity.startSupportActionMode(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
currentActionMode = mode;
private void startActionMode(final UpdateView.UpdateViewHolder<T> holder) {
final UpdateView<T> updateView = holder.getUpdateView();
if (context instanceof SubsonicFragmentActivity && currentActionMode == null) {
final SubsonicFragmentActivity fragmentActivity = (SubsonicFragmentActivity) context;
fragmentActivity.startSupportActionMode(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
currentActionMode = mode;
T item = holder.getItem();
selected.add(item);
selectedViews.add(updateView);
setChecked(updateView, true);
T item = holder.getItem();
selected.add(item);
selectedViews.add(updateView);
setChecked(updateView, true);
onCreateActionModeMenu(menu, mode.getMenuInflater());
MenuUtil.hideMenuItems(context, menu, updateView);
onCreateActionModeMenu(menu, mode.getMenuInflater());
MenuUtil.hideMenuItems(context, menu, updateView);
mode.setTitle(context.getResources().getString(R.string.select_album_n_selected, selected.size()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_COLOR_ACTION_BAR, true)) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
int colorPrimaryDark = typedValue.data;
mode.setTitle(context.getResources().getString(R.string.select_album_n_selected, selected.size()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_COLOR_ACTION_BAR, true)) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
int colorPrimaryDark = typedValue.data;
Window window = ((SubsonicFragmentActivity) context).getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(colorPrimaryDark);
}
return true;
}
Window window = ((SubsonicFragmentActivity) context).getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(colorPrimaryDark);
}
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (fragmentActivity.onOptionsItemSelected(item)) {
currentActionMode.finish();
return true;
} else {
return false;
}
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (fragmentActivity.onOptionsItemSelected(item)) {
currentActionMode.finish();
return true;
} else {
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
currentActionMode = null;
selected.clear();
for (UpdateView<T> updateView : selectedViews) {
updateView.setChecked(false);
}
selectedViews.clear();
@Override
public void onDestroyActionMode(ActionMode mode) {
currentActionMode = null;
selected.clear();
for (UpdateView<T> updateView : selectedViews) {
updateView.setChecked(false);
}
selectedViews.clear();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_COLOR_ACTION_BAR, true)) {
Window window = ((SubsonicFragmentActivity) context).getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
});
}
}
public void stopActionMode() {
if(currentActionMode != null) {
currentActionMode.finish();
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_COLOR_ACTION_BAR, true)) {
Window window = ((SubsonicFragmentActivity) context).getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
});
}
}
public void stopActionMode() {
if(currentActionMode != null) {
currentActionMode.finish();
}
}
public String getNameIndex(String name) {
return getNameIndex(name, false);
}
public String getNameIndex(String name, boolean removeIgnoredArticles) {
if(name == null) {
return "*";
}
public String getNameIndex(String name) {
return getNameIndex(name, false);
}
public String getNameIndex(String name, boolean removeIgnoredArticles) {
if(name == null) {
return "*";
}
if(removeIgnoredArticles) {
if (ignoredArticles == null) {
SharedPreferences prefs = Util.getPreferences(context);
String ignoredArticlesString = prefs.getString(Constants.CACHE_KEY_IGNORE, "The El La Los Las Le Les");
ignoredArticles = ignoredArticlesString.split(" ");
}
if(removeIgnoredArticles) {
if (ignoredArticles == null) {
SharedPreferences prefs = Util.getPreferences(context);
String ignoredArticlesString = prefs.getString(Constants.CACHE_KEY_IGNORE, "The El La Los Las Le Les");
ignoredArticles = ignoredArticlesString.split(" ");
}
name = name.toLowerCase();
for (String article : ignoredArticles) {
int index = name.indexOf(article.toLowerCase() + " ");
if (index == 0) {
name = name.substring(article.length() + 1);
}
}
}
name = name.toLowerCase();
for (String article : ignoredArticles) {
int index = name.indexOf(article.toLowerCase() + " ");
if (index == 0) {
name = name.substring(article.length() + 1);
}
}
}
String index = name.substring(0, 1).toUpperCase();
if (!Character.isLetter(index.charAt(0))) {
index = "#";
}
String index = name.substring(0, 1).toUpperCase();
if (!Character.isLetter(index.charAt(0))) {
index = "#";
}
return index;
}
return index;
}
public interface OnItemClickedListener<T> {
void onItemClicked(UpdateView<T> updateView, T item);
void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<T> updateView, T item);
boolean onContextItemSelected(MenuItem menuItem, UpdateView<T> updateView, T item);
}
public interface OnItemClickedListener<T> {
void onItemClicked(UpdateView<T> updateView, T item);
void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<T> updateView, T item);
boolean onContextItemSelected(MenuItem menuItem, UpdateView<T> updateView, T item);
}
}

View File

@ -1,16 +1,16 @@
/*
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 2014 (C) Scott Jackson
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 2014 (C) Scott Jackson
*/
package net.nullsum.audinaut.adapter;
@ -36,86 +36,86 @@ import net.nullsum.audinaut.view.UpdateView;
import static net.nullsum.audinaut.domain.User.Setting;
public class SettingsAdapter extends SectionAdapter<Setting> {
private static final String TAG = SettingsAdapter.class.getSimpleName();
public final int VIEW_TYPE_SETTING = 1;
public final int VIEW_TYPE_SETTING_HEADER = 2;
private static final String TAG = SettingsAdapter.class.getSimpleName();
public final int VIEW_TYPE_SETTING = 1;
public final int VIEW_TYPE_SETTING_HEADER = 2;
private final User user;
private final boolean editable;
private final ImageLoader imageLoader;
private final User user;
private final boolean editable;
private final ImageLoader imageLoader;
public SettingsAdapter(Context context, User user, List<String> headers, List<List<User.Setting>> settingSections, ImageLoader imageLoader, boolean editable, OnItemClickedListener<Setting> onItemClickedListener) {
super(context, headers, settingSections, imageLoader != null);
this.user = user;
this.imageLoader = imageLoader;
this.editable = editable;
this.onItemClickedListener = onItemClickedListener;
public SettingsAdapter(Context context, User user, List<String> headers, List<List<User.Setting>> settingSections, ImageLoader imageLoader, boolean editable, OnItemClickedListener<Setting> onItemClickedListener) {
super(context, headers, settingSections, imageLoader != null);
this.user = user;
this.imageLoader = imageLoader;
this.editable = editable;
this.onItemClickedListener = onItemClickedListener;
for(List<Setting> settings: sections) {
for (Setting setting : settings) {
if (setting.getValue()) {
addSelected(setting);
}
}
}
}
for(List<Setting> settings: sections) {
for (Setting setting : settings) {
if (setting.getValue()) {
addSelected(setting);
}
}
}
}
@Override
public int getItemViewType(int position) {
int viewType = super.getItemViewType(position);
if(viewType == SectionAdapter.VIEW_TYPE_HEADER) {
if(position == 0 && imageLoader != null) {
return VIEW_TYPE_HEADER;
} else {
return VIEW_TYPE_SETTING_HEADER;
}
} else {
return viewType;
}
}
@Override
public int getItemViewType(int position) {
int viewType = super.getItemViewType(position);
if(viewType == SectionAdapter.VIEW_TYPE_HEADER) {
if(position == 0 && imageLoader != null) {
return VIEW_TYPE_HEADER;
} else {
return VIEW_TYPE_SETTING_HEADER;
}
} else {
return viewType;
}
}
public void onBindHeaderHolder(UpdateView.UpdateViewHolder holder, String description, int sectionIndex) {
View header = holder.getView();
}
public void onBindHeaderHolder(UpdateView.UpdateViewHolder holder, String description, int sectionIndex) {
View header = holder.getView();
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
if(viewType == VIEW_TYPE_SETTING_HEADER) {
return new UpdateView.UpdateViewHolder(new BasicHeaderView(context));
} else {
return new UpdateView.UpdateViewHolder(new SettingView(context));
}
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
if(viewType == VIEW_TYPE_SETTING_HEADER) {
return new UpdateView.UpdateViewHolder(new BasicHeaderView(context));
} else {
return new UpdateView.UpdateViewHolder(new SettingView(context));
}
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Setting item, int viewType) {
holder.getUpdateView().setObject(item, editable);
}
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Setting item, int viewType) {
holder.getUpdateView().setObject(item, editable);
}
@Override
public int getItemViewType(Setting item) {
return VIEW_TYPE_SETTING;
}
@Override
public int getItemViewType(Setting item) {
return VIEW_TYPE_SETTING;
}
@Override
public void setChecked(UpdateView updateView, boolean checked) {
if(updateView instanceof SettingView) {
updateView.setChecked(checked);
}
}
@Override
public void setChecked(UpdateView updateView, boolean checked) {
if(updateView instanceof SettingView) {
updateView.setChecked(checked);
}
}
public static SettingsAdapter getSettingsAdapter(Context context, User user, ImageLoader imageLoader, OnItemClickedListener<Setting> onItemClickedListener) {
return getSettingsAdapter(context, user, imageLoader, true, onItemClickedListener);
}
public static SettingsAdapter getSettingsAdapter(Context context, User user, ImageLoader imageLoader, boolean isEditable, OnItemClickedListener<Setting> onItemClickedListener) {
List<String> headers = new ArrayList<>();
List<List<User.Setting>> settingsSections = new ArrayList<>();
settingsSections.add(user.getSettings());
public static SettingsAdapter getSettingsAdapter(Context context, User user, ImageLoader imageLoader, OnItemClickedListener<Setting> onItemClickedListener) {
return getSettingsAdapter(context, user, imageLoader, true, onItemClickedListener);
}
public static SettingsAdapter getSettingsAdapter(Context context, User user, ImageLoader imageLoader, boolean isEditable, OnItemClickedListener<Setting> onItemClickedListener) {
List<String> headers = new ArrayList<>();
List<List<User.Setting>> settingsSections = new ArrayList<>();
settingsSections.add(user.getSettings());
if(user.getMusicFolderSettings() != null) {
settingsSections.add(user.getMusicFolderSettings());
}
if(user.getMusicFolderSettings() != null) {
settingsSections.add(user.getMusicFolderSettings());
}
return new SettingsAdapter(context, user, headers, settingsSections, imageLoader, isEditable, onItemClickedListener);
}
return new SettingsAdapter(context, user, headers, settingsSections, imageLoader, isEditable, onItemClickedListener);
}
}

View File

@ -29,27 +29,27 @@ public class AudioEffectsController {
private static final String TAG = AudioEffectsController.class.getSimpleName();
private final Context context;
private int audioSessionId = 0;
private int audioSessionId = 0;
private EqualizerController equalizerController;
private EqualizerController equalizerController;
public AudioEffectsController(Context context, int audioSessionId) {
this.context = context;
this.audioSessionId = audioSessionId;
this.audioSessionId = audioSessionId;
}
public void release() {
if(equalizerController != null) {
equalizerController.release();
}
}
public void release() {
if(equalizerController != null) {
equalizerController.release();
}
}
public EqualizerController getEqualizerController() {
if (equalizerController == null) {
equalizerController = new EqualizerController(context, audioSessionId);
public EqualizerController getEqualizerController() {
if (equalizerController == null) {
equalizerController = new EqualizerController(context, audioSessionId);
equalizerController.loadSettings();
}
return equalizerController;
}
}
return equalizerController;
}
}

View File

@ -1,20 +1,20 @@
/*
This file is part of Subsonic.
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 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.
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/>.
You should have received a copy of the GNU General Public License
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2014 (C) Scott Jackson
Copyright 2014 (C) Scott Jackson
*/
package net.nullsum.audinaut.audiofx;
@ -23,55 +23,55 @@ import android.media.audiofx.LoudnessEnhancer;
import android.util.Log;
public class LoudnessEnhancerController {
private static final String TAG = LoudnessEnhancerController.class.getSimpleName();
private static final String TAG = LoudnessEnhancerController.class.getSimpleName();
private final Context context;
private LoudnessEnhancer enhancer;
private boolean released = false;
private int audioSessionId = 0;
private final Context context;
private LoudnessEnhancer enhancer;
private boolean released = false;
private int audioSessionId = 0;
public LoudnessEnhancerController(Context context, int audioSessionId) {
this.context = context;
try {
this.audioSessionId = audioSessionId;
enhancer = new LoudnessEnhancer(audioSessionId);
} catch (Throwable x) {
Log.w(TAG, "Failed to create enhancer", x);
}
}
public LoudnessEnhancerController(Context context, int audioSessionId) {
this.context = context;
try {
this.audioSessionId = audioSessionId;
enhancer = new LoudnessEnhancer(audioSessionId);
} catch (Throwable x) {
Log.w(TAG, "Failed to create enhancer", x);
}
}
public boolean isAvailable() {
return enhancer != null;
}
public boolean isAvailable() {
return enhancer != null;
}
public boolean isEnabled() {
try {
return isAvailable() && enhancer.getEnabled();
} catch(Exception e) {
return false;
}
}
public boolean isEnabled() {
try {
return isAvailable() && enhancer.getEnabled();
} catch(Exception e) {
return false;
}
}
public void enable() {
enhancer.setEnabled(true);
}
public void disable() {
enhancer.setEnabled(false);
}
public void enable() {
enhancer.setEnabled(true);
}
public void disable() {
enhancer.setEnabled(false);
}
public float getGain() {
return enhancer.getTargetGain();
}
public void setGain(int gain) {
enhancer.setTargetGain(gain);
}
public float getGain() {
return enhancer.getTargetGain();
}
public void setGain(int gain) {
enhancer.setTargetGain(gain);
}
public void release() {
if (isAvailable()) {
enhancer.release();
released = true;
}
}
public void release() {
if (isAvailable()) {
enhancer.release();
released = true;
}
}
}

View File

@ -43,19 +43,19 @@ import net.nullsum.audinaut.util.Util;
* @author Sindre Mehus
*/
public class MusicDirectory implements Serializable {
private static final String TAG = MusicDirectory.class.getSimpleName();
private static final String TAG = MusicDirectory.class.getSimpleName();
private String name;
private String id;
private String parent;
private String id;
private String parent;
private List<Entry> children;
public MusicDirectory() {
children = new ArrayList<Entry>();
}
public MusicDirectory(List<Entry> children) {
this.children = children;
}
public MusicDirectory() {
children = new ArrayList<Entry>();
}
public MusicDirectory(List<Entry> children) {
this.children = children;
}
public String getName() {
return name;
@ -65,34 +65,34 @@ public class MusicDirectory implements Serializable {
this.name = name;
}
public String getId() {
return id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setId(String id) {
this.id = id;
}
public String getParent() {
return parent;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public void addChild(Entry child) {
if(child != null) {
children.add(child);
}
}
public void addChildren(List<Entry> children) {
this.children.addAll(children);
}
public void addChild(Entry child) {
if(child != null) {
children.add(child);
}
}
public void addChildren(List<Entry> children) {
this.children.addAll(children);
}
public void replaceChildren(List<Entry> children) {
this.children = children;
}
public void replaceChildren(List<Entry> children) {
this.children = children;
}
public synchronized List<Entry> getChildren() {
return getChildren(true, true);
@ -111,209 +111,209 @@ public class MusicDirectory implements Serializable {
}
return result;
}
public synchronized List<Entry> getSongs() {
List<Entry> result = new ArrayList<Entry>();
for (Entry child : children) {
if (child != null && !child.isDirectory()) {
result.add(child);
}
}
return result;
}
public synchronized List<Entry> getSongs() {
List<Entry> result = new ArrayList<Entry>();
for (Entry child : children) {
if (child != null && !child.isDirectory()) {
result.add(child);
}
}
return result;
}
public synchronized int getChildrenSize() {
return children.size();
}
public synchronized int getChildrenSize() {
return children.size();
}
public void shuffleChildren() {
Collections.shuffle(this.children);
}
public void shuffleChildren() {
Collections.shuffle(this.children);
}
public void sortChildren(Context context, int instance) {
public void sortChildren(Context context, int instance) {
sortChildren(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_CUSTOM_SORT_ENABLED, true));
}
public void sortChildren(boolean byYear) {
EntryComparator.sort(children, byYear);
}
}
public void sortChildren(boolean byYear) {
EntryComparator.sort(children, byYear);
}
public synchronized boolean updateMetadata(MusicDirectory refreshedDirectory) {
boolean metadataUpdated = false;
Iterator<Entry> it = children.iterator();
while(it.hasNext()) {
Entry entry = it.next();
int index = refreshedDirectory.children.indexOf(entry);
if(index != -1) {
final Entry refreshed = refreshedDirectory.children.get(index);
public synchronized boolean updateMetadata(MusicDirectory refreshedDirectory) {
boolean metadataUpdated = false;
Iterator<Entry> it = children.iterator();
while(it.hasNext()) {
Entry entry = it.next();
int index = refreshedDirectory.children.indexOf(entry);
if(index != -1) {
final Entry refreshed = refreshedDirectory.children.get(index);
entry.setTitle(refreshed.getTitle());
entry.setAlbum(refreshed.getAlbum());
entry.setArtist(refreshed.getArtist());
entry.setTrack(refreshed.getTrack());
entry.setYear(refreshed.getYear());
entry.setGenre(refreshed.getGenre());
entry.setTranscodedContentType(refreshed.getTranscodedContentType());
entry.setTranscodedSuffix(refreshed.getTranscodedSuffix());
entry.setDiscNumber(refreshed.getDiscNumber());
entry.setType(refreshed.getType());
if(!Util.equals(entry.getCoverArt(), refreshed.getCoverArt())) {
metadataUpdated = true;
entry.setCoverArt(refreshed.getCoverArt());
}
entry.setTitle(refreshed.getTitle());
entry.setAlbum(refreshed.getAlbum());
entry.setArtist(refreshed.getArtist());
entry.setTrack(refreshed.getTrack());
entry.setYear(refreshed.getYear());
entry.setGenre(refreshed.getGenre());
entry.setTranscodedContentType(refreshed.getTranscodedContentType());
entry.setTranscodedSuffix(refreshed.getTranscodedSuffix());
entry.setDiscNumber(refreshed.getDiscNumber());
entry.setType(refreshed.getType());
if(!Util.equals(entry.getCoverArt(), refreshed.getCoverArt())) {
metadataUpdated = true;
entry.setCoverArt(refreshed.getCoverArt());
}
new UpdateHelper.EntryInstanceUpdater(entry) {
@Override
public void update(Entry found) {
found.setTitle(refreshed.getTitle());
found.setAlbum(refreshed.getAlbum());
found.setArtist(refreshed.getArtist());
found.setTrack(refreshed.getTrack());
found.setYear(refreshed.getYear());
found.setGenre(refreshed.getGenre());
found.setTranscodedContentType(refreshed.getTranscodedContentType());
found.setTranscodedSuffix(refreshed.getTranscodedSuffix());
found.setDiscNumber(refreshed.getDiscNumber());
found.setType(refreshed.getType());
if(!Util.equals(found.getCoverArt(), refreshed.getCoverArt())) {
found.setCoverArt(refreshed.getCoverArt());
metadataUpdate = DownloadService.METADATA_UPDATED_COVER_ART;
}
}
}.execute();
}
}
new UpdateHelper.EntryInstanceUpdater(entry) {
@Override
public void update(Entry found) {
found.setTitle(refreshed.getTitle());
found.setAlbum(refreshed.getAlbum());
found.setArtist(refreshed.getArtist());
found.setTrack(refreshed.getTrack());
found.setYear(refreshed.getYear());
found.setGenre(refreshed.getGenre());
found.setTranscodedContentType(refreshed.getTranscodedContentType());
found.setTranscodedSuffix(refreshed.getTranscodedSuffix());
found.setDiscNumber(refreshed.getDiscNumber());
found.setType(refreshed.getType());
if(!Util.equals(found.getCoverArt(), refreshed.getCoverArt())) {
found.setCoverArt(refreshed.getCoverArt());
metadataUpdate = DownloadService.METADATA_UPDATED_COVER_ART;
}
}
}.execute();
}
}
return metadataUpdated;
}
public synchronized boolean updateEntriesList(Context context, int instance, MusicDirectory refreshedDirectory) {
boolean changed = false;
Iterator<Entry> it = children.iterator();
while(it.hasNext()) {
Entry entry = it.next();
// No longer exists in here
if(refreshedDirectory.children.indexOf(entry) == -1) {
it.remove();
changed = true;
}
}
return metadataUpdated;
}
public synchronized boolean updateEntriesList(Context context, int instance, MusicDirectory refreshedDirectory) {
boolean changed = false;
Iterator<Entry> it = children.iterator();
while(it.hasNext()) {
Entry entry = it.next();
// No longer exists in here
if(refreshedDirectory.children.indexOf(entry) == -1) {
it.remove();
changed = true;
}
}
// Make sure we contain all children from refreshed set
boolean resort = false;
for(Entry refreshed: refreshedDirectory.children) {
if(!this.children.contains(refreshed)) {
this.children.add(refreshed);
resort = true;
changed = true;
}
}
// Make sure we contain all children from refreshed set
boolean resort = false;
for(Entry refreshed: refreshedDirectory.children) {
if(!this.children.contains(refreshed)) {
this.children.add(refreshed);
resort = true;
changed = true;
}
}
if(resort) {
this.sortChildren(context, instance);
}
if(resort) {
this.sortChildren(context, instance);
}
return changed;
}
return changed;
}
public static class Entry implements Serializable {
public static final int TYPE_SONG = 0;
public static final int TYPE_SONG = 0;
private String id;
private String parent;
private String grandParent;
private String albumId;
private String artistId;
private boolean directory;
private String title;
private String album;
private String artist;
private Integer track;
private Integer year;
private String genre;
private String contentType;
private String suffix;
private String transcodedContentType;
private String transcodedSuffix;
private String coverArt;
private Long size;
private Integer duration;
private Integer bitRate;
private String path;
private Integer discNumber;
private int type = 0;
private int closeness;
private transient Artist linkedArtist;
private String id;
private String parent;
private String grandParent;
private String albumId;
private String artistId;
private boolean directory;
private String title;
private String album;
private String artist;
private Integer track;
private Integer year;
private String genre;
private String contentType;
private String suffix;
private String transcodedContentType;
private String transcodedSuffix;
private String coverArt;
private Long size;
private Integer duration;
private Integer bitRate;
private String path;
private Integer discNumber;
private int type = 0;
private int closeness;
private transient Artist linkedArtist;
public Entry() {
public Entry() {
}
public Entry(String id) {
this.id = id;
}
public Entry(Artist artist) {
this.id = artist.getId();
this.title = artist.getName();
this.directory = true;
this.linkedArtist = artist;
}
}
public Entry(String id) {
this.id = id;
}
public Entry(Artist artist) {
this.id = artist.getId();
this.title = artist.getName();
this.directory = true;
this.linkedArtist = artist;
}
public void loadMetadata(File file) {
try {
MediaMetadataRetriever metadata = new MediaMetadataRetriever();
metadata.setDataSource(file.getAbsolutePath());
String discNumber = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DISC_NUMBER);
if(discNumber == null) {
discNumber = "1/1";
}
int slashIndex = discNumber.indexOf("/");
if(slashIndex > 0) {
discNumber = discNumber.substring(0, slashIndex);
}
try {
setDiscNumber(Integer.parseInt(discNumber));
} catch(Exception e) {
Log.w(TAG, "Non numbers in disc field!");
}
String bitrate = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE);
setBitRate(Integer.parseInt((bitrate != null) ? bitrate : "0") / 1000);
String length = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
setDuration(Integer.parseInt(length) / 1000);
String artist = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
if(artist != null) {
setArtist(artist);
}
String album = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
if(album != null) {
setAlbum(album);
}
metadata.release();
} catch(Exception e) {
Log.i(TAG, "Device doesn't properly support MediaMetadataRetreiver", e);
}
}
public void rebaseTitleOffPath() {
try {
String filename = getPath();
if(filename == null) {
return;
}
public void loadMetadata(File file) {
try {
MediaMetadataRetriever metadata = new MediaMetadataRetriever();
metadata.setDataSource(file.getAbsolutePath());
String discNumber = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DISC_NUMBER);
if(discNumber == null) {
discNumber = "1/1";
}
int slashIndex = discNumber.indexOf("/");
if(slashIndex > 0) {
discNumber = discNumber.substring(0, slashIndex);
}
try {
setDiscNumber(Integer.parseInt(discNumber));
} catch(Exception e) {
Log.w(TAG, "Non numbers in disc field!");
}
String bitrate = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE);
setBitRate(Integer.parseInt((bitrate != null) ? bitrate : "0") / 1000);
String length = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
setDuration(Integer.parseInt(length) / 1000);
String artist = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
if(artist != null) {
setArtist(artist);
}
String album = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
if(album != null) {
setAlbum(album);
}
metadata.release();
} catch(Exception e) {
Log.i(TAG, "Device doesn't properly support MediaMetadataRetreiver", e);
}
}
public void rebaseTitleOffPath() {
try {
String filename = getPath();
if(filename == null) {
return;
}
int index = filename.lastIndexOf('/');
if (index != -1) {
filename = filename.substring(index + 1);
if (getTrack() != null) {
filename = filename.replace(String.format("%02d ", getTrack()), "");
}
int index = filename.lastIndexOf('/');
if (index != -1) {
filename = filename.substring(index + 1);
if (getTrack() != null) {
filename = filename.replace(String.format("%02d ", getTrack()), "");
}
index = filename.lastIndexOf('.');
if(index != -1) {
filename = filename.substring(0, index);
}
index = filename.lastIndexOf('.');
if(index != -1) {
filename = filename.substring(0, index);
}
setTitle(filename);
}
} catch(Exception e) {
Log.w(TAG, "Failed to update title based off of path", e);
}
}
setTitle(filename);
}
} catch(Exception e) {
Log.w(TAG, "Failed to update title based off of path", e);
}
}
public String getId() {
return id;
@ -331,7 +331,7 @@ public class MusicDirectory implements Serializable {
this.parent = parent;
}
public String getGrandParent() {
public String getGrandParent() {
return grandParent;
}
@ -339,21 +339,21 @@ public class MusicDirectory implements Serializable {
this.grandParent = grandParent;
}
public String getAlbumId() {
return albumId;
}
public String getAlbumId() {
return albumId;
}
public void setAlbumId(String albumId) {
this.albumId = albumId;
}
public void setAlbumId(String albumId) {
this.albumId = albumId;
}
public String getArtistId() {
return artistId;
}
public String getArtistId() {
return artistId;
}
public void setArtistId(String artistId) {
this.artistId = artistId;
}
public void setArtistId(String artistId) {
this.artistId = artistId;
}
public boolean isDirectory() {
return directory;
@ -375,17 +375,17 @@ public class MusicDirectory implements Serializable {
return album;
}
public boolean isAlbum() {
return getParent() != null || getArtist() != null;
}
public boolean isAlbum() {
return getParent() != null || getArtist() != null;
}
public String getAlbumDisplay() {
if(album != null && title.startsWith("Disc ")) {
return album;
} else {
return title;
}
}
public String getAlbumDisplay() {
if(album != null && title.startsWith("Disc ")) {
return album;
} else {
return title;
}
}
public void setAlbum(String album) {
this.album = album;
@ -495,43 +495,43 @@ public class MusicDirectory implements Serializable {
this.path = path;
}
public Integer getDiscNumber() {
return discNumber;
}
public Integer getDiscNumber() {
return discNumber;
}
public void setDiscNumber(Integer discNumber) {
this.discNumber = discNumber;
}
public void setDiscNumber(Integer discNumber) {
this.discNumber = discNumber;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public boolean isSong() {
return type == TYPE_SONG;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public boolean isSong() {
return type == TYPE_SONG;
}
public int getCloseness() {
return closeness;
}
public int getCloseness() {
return closeness;
}
public void setCloseness(int closeness) {
this.closeness = closeness;
}
public void setCloseness(int closeness) {
this.closeness = closeness;
}
public boolean isOnlineId(Context context) {
try {
String cacheLocation = Util.getPreferences(context).getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null);
return cacheLocation == null || id == null || id.indexOf(cacheLocation) == -1;
} catch(Exception e) {
Log.w(TAG, "Failed to check online id validity");
public boolean isOnlineId(Context context) {
try {
String cacheLocation = Util.getPreferences(context).getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null);
return cacheLocation == null || id == null || id.indexOf(cacheLocation) == -1;
} catch(Exception e) {
Log.w(TAG, "Failed to check online id validity");
// Err on the side of default functionality
return true;
}
}
// Err on the side of default functionality
return true;
}
}
@Override
public boolean equals(Object o) {
@ -555,72 +555,72 @@ public class MusicDirectory implements Serializable {
public String toString() {
return title;
}
}
}
public static class EntryComparator implements Comparator<Entry> {
private boolean byYear;
private Collator collator;
public static class EntryComparator implements Comparator<Entry> {
private boolean byYear;
private Collator collator;
public EntryComparator(boolean byYear) {
this.byYear = byYear;
this.collator = Collator.getInstance(Locale.US);
this.collator.setStrength(Collator.PRIMARY);
}
public EntryComparator(boolean byYear) {
this.byYear = byYear;
this.collator = Collator.getInstance(Locale.US);
this.collator.setStrength(Collator.PRIMARY);
}
public int compare(Entry lhs, Entry rhs) {
if(lhs.isDirectory() && !rhs.isDirectory()) {
return -1;
} else if(!lhs.isDirectory() && rhs.isDirectory()) {
return 1;
} else if(lhs.isDirectory() && rhs.isDirectory()) {
if(byYear) {
Integer lhsYear = lhs.getYear();
Integer rhsYear = rhs.getYear();
if(lhsYear != null && rhsYear != null) {
return lhsYear.compareTo(rhsYear);
} else if(lhsYear != null) {
return -1;
} else if(rhsYear != null) {
return 1;
}
}
public int compare(Entry lhs, Entry rhs) {
if(lhs.isDirectory() && !rhs.isDirectory()) {
return -1;
} else if(!lhs.isDirectory() && rhs.isDirectory()) {
return 1;
} else if(lhs.isDirectory() && rhs.isDirectory()) {
if(byYear) {
Integer lhsYear = lhs.getYear();
Integer rhsYear = rhs.getYear();
if(lhsYear != null && rhsYear != null) {
return lhsYear.compareTo(rhsYear);
} else if(lhsYear != null) {
return -1;
} else if(rhsYear != null) {
return 1;
}
}
return collator.compare(lhs.getAlbumDisplay(), rhs.getAlbumDisplay());
}
return collator.compare(lhs.getAlbumDisplay(), rhs.getAlbumDisplay());
}
Integer lhsDisc = lhs.getDiscNumber();
Integer rhsDisc = rhs.getDiscNumber();
Integer lhsDisc = lhs.getDiscNumber();
Integer rhsDisc = rhs.getDiscNumber();
if(lhsDisc != null && rhsDisc != null) {
if(lhsDisc < rhsDisc) {
return -1;
} else if(lhsDisc > rhsDisc) {
return 1;
}
}
if(lhsDisc != null && rhsDisc != null) {
if(lhsDisc < rhsDisc) {
return -1;
} else if(lhsDisc > rhsDisc) {
return 1;
}
}
Integer lhsTrack = lhs.getTrack();
Integer rhsTrack = rhs.getTrack();
if(lhsTrack != null && rhsTrack != null && lhsTrack != rhsTrack) {
return lhsTrack.compareTo(rhsTrack);
} else if(lhsTrack != null) {
return -1;
} else if(rhsTrack != null) {
return 1;
}
Integer lhsTrack = lhs.getTrack();
Integer rhsTrack = rhs.getTrack();
if(lhsTrack != null && rhsTrack != null && lhsTrack != rhsTrack) {
return lhsTrack.compareTo(rhsTrack);
} else if(lhsTrack != null) {
return -1;
} else if(rhsTrack != null) {
return 1;
}
return collator.compare(lhs.getTitle(), rhs.getTitle());
}
return collator.compare(lhs.getTitle(), rhs.getTitle());
}
public static void sort(List<Entry> entries) {
sort(entries, true);
}
public static void sort(List<Entry> entries, boolean byYear) {
try {
Collections.sort(entries, new EntryComparator(byYear));
} catch (Exception e) {
Log.w(TAG, "Failed to sort MusicDirectory");
}
}
}
public static void sort(List<Entry> entries) {
sort(entries, true);
}
public static void sort(List<Entry> entries, boolean byYear) {
try {
Collections.sort(entries, new EntryComparator(byYear));
} catch (Exception e) {
Log.w(TAG, "Failed to sort MusicDirectory");
}
}
}
}

View File

@ -32,49 +32,49 @@ import java.util.List;
* @version $Id$
*/
public class MusicFolder implements Serializable {
private static final String TAG = MusicFolder.class.getSimpleName();
private String id;
private String name;
private boolean enabled;
private static final String TAG = MusicFolder.class.getSimpleName();
private String id;
private String name;
private boolean enabled;
public MusicFolder() {
public MusicFolder() {
}
public MusicFolder(String id, String name) {
this.id = id;
this.name = name;
}
}
public MusicFolder(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getName() {
return name;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean getEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean getEnabled() {
return enabled;
}
public static class MusicFolderComparator implements Comparator<MusicFolder> {
public int compare(MusicFolder lhsMusicFolder, MusicFolder rhsMusicFolder) {
if(lhsMusicFolder == rhsMusicFolder || lhsMusicFolder.getName().equals(rhsMusicFolder.getName())) {
return 0;
} else {
return lhsMusicFolder.getName().compareToIgnoreCase(rhsMusicFolder.getName());
}
}
}
public static class MusicFolderComparator implements Comparator<MusicFolder> {
public int compare(MusicFolder lhsMusicFolder, MusicFolder rhsMusicFolder) {
if(lhsMusicFolder == rhsMusicFolder || lhsMusicFolder.getName().equals(rhsMusicFolder.getName())) {
return 0;
} else {
return lhsMusicFolder.getName().compareToIgnoreCase(rhsMusicFolder.getName());
}
}
}
public static void sort(List<MusicFolder> musicFolders) {
try {
Collections.sort(musicFolders, new MusicFolderComparator());
} catch (Exception e) {
Log.w(TAG, "Failed to sort music folders", e);
}
}
public static void sort(List<MusicFolder> musicFolders) {
try {
Collections.sort(musicFolders, new MusicFolderComparator());
} catch (Exception e) {
Log.w(TAG, "Failed to sort music folders", e);
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.domain;
@ -21,10 +21,10 @@ import java.util.Date;
import java.util.List;
public class PlayerQueue implements Serializable {
public List<MusicDirectory.Entry> songs = new ArrayList<MusicDirectory.Entry>();
public List<MusicDirectory.Entry> toDelete = new ArrayList<MusicDirectory.Entry>();
public int currentPlayingIndex;
public int currentPlayingPosition;
public boolean renameCurrent = false;
public Date changed = null;
public List<MusicDirectory.Entry> songs = new ArrayList<MusicDirectory.Entry>();
public List<MusicDirectory.Entry> toDelete = new ArrayList<MusicDirectory.Entry>();
public int currentPlayingIndex;
public int currentPlayingPosition;
public boolean renameCurrent = false;
public Date changed = null;
}

View File

@ -34,31 +34,31 @@ public class Playlist implements Serializable {
private String id;
private String name;
private String owner;
private String comment;
private String songCount;
private Boolean pub;
private Date created;
private Date changed;
private Integer duration;
private String owner;
private String comment;
private String songCount;
private Boolean pub;
private Date created;
private Date changed;
private Integer duration;
public Playlist() {
public Playlist() {
}
}
public Playlist(String id, String name) {
this.id = id;
this.name = name;
}
public Playlist(String id, String name, String owner, String comment, String songCount, String pub, String created, String changed, Integer duration) {
public Playlist(String id, String name, String owner, String comment, String songCount, String pub, String created, String changed, Integer duration) {
this.id = id;
this.name = name;
this.owner = (owner == null) ? "" : owner;
this.comment = (comment == null) ? "" : comment;
this.songCount = (songCount == null) ? "" : songCount;
this.pub = (pub == null) ? null : (pub.equals("true"));
setCreated(created);
setChanged(changed);
this.duration = duration;
this.owner = (owner == null) ? "" : owner;
this.comment = (comment == null) ? "" : comment;
this.songCount = (songCount == null) ? "" : songCount;
this.pub = (pub == null) ? null : (pub.equals("true"));
setCreated(created);
setChanged(changed);
this.duration = duration;
}
public String getId() {
@ -77,111 +77,111 @@ public class Playlist implements Serializable {
this.name = name;
}
public String getOwner() {
return this.owner;
}
public String getOwner() {
return this.owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getComment() {
return this.comment;
}
public String getComment() {
return this.comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getSongCount() {
return this.songCount;
}
public String getSongCount() {
return this.songCount;
}
public void setSongCount(String songCount) {
this.songCount = songCount;
}
public void setSongCount(String songCount) {
this.songCount = songCount;
}
public Boolean getPublic() {
return this.pub;
}
public void setPublic(Boolean pub) {
this.pub = pub;
}
public Boolean getPublic() {
return this.pub;
}
public void setPublic(Boolean pub) {
this.pub = pub;
}
public Date getCreated() {
return created;
}
public Date getCreated() {
return created;
}
public void setCreated(String created) {
if (created != null) {
try {
this.created = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).parse(created);
} catch (ParseException e) {
this.created = null;
}
} else {
this.created = null;
}
}
public void setCreated(Date created) {
this.created = created;
}
public void setCreated(String created) {
if (created != null) {
try {
this.created = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).parse(created);
} catch (ParseException e) {
this.created = null;
}
} else {
this.created = null;
}
}
public void setCreated(Date created) {
this.created = created;
}
public Date getChanged() {
return changed;
}
public void setChanged(String changed) {
if (changed != null) {
try {
this.changed = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).parse(changed);
} catch (ParseException e) {
this.changed = null;
}
} else {
this.changed = null;
}
}
public void setChanged(Date changed) {
this.changed = changed;
}
public Date getChanged() {
return changed;
}
public void setChanged(String changed) {
if (changed != null) {
try {
this.changed = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).parse(changed);
} catch (ParseException e) {
this.changed = null;
}
} else {
this.changed = null;
}
}
public void setChanged(Date changed) {
this.changed = changed;
}
public Integer getDuration() {
return duration;
}
public void setDuration(Integer duration) {
this.duration = duration;
}
public Integer getDuration() {
return duration;
}
public void setDuration(Integer duration) {
this.duration = duration;
}
@Override
public String toString() {
return name;
}
@Override
public String toString() {
return name;
}
@Override
public boolean equals(Object o) {
if(o == this) {
return true;
} else if(o == null) {
return false;
} else if(o instanceof String) {
return o.equals(this.id);
} else if(o.getClass() != getClass()) {
return false;
}
@Override
public boolean equals(Object o) {
if(o == this) {
return true;
} else if(o == null) {
return false;
} else if(o instanceof String) {
return o.equals(this.id);
} else if(o.getClass() != getClass()) {
return false;
}
Playlist playlist = (Playlist) o;
return playlist.id.equals(this.id);
}
Playlist playlist = (Playlist) o;
return playlist.id.equals(this.id);
}
public static class PlaylistComparator implements Comparator<Playlist> {
@Override
public int compare(Playlist playlist1, Playlist playlist2) {
return playlist1.getName().compareToIgnoreCase(playlist2.getName());
}
public static class PlaylistComparator implements Comparator<Playlist> {
@Override
public int compare(Playlist playlist1, Playlist playlist2) {
return playlist1.getName().compareToIgnoreCase(playlist2.getName());
}
public static List<Playlist> sort(List<Playlist> playlists) {
Collections.sort(playlists, new PlaylistComparator());
return playlists;
}
}
public static List<Playlist> sort(List<Playlist> playlists) {
Collections.sort(playlists, new PlaylistComparator());
return playlists;
}
}
}

View File

@ -27,67 +27,67 @@ import java.util.regex.Pattern;
*/
public class SearchCritera {
private final String query;
private final int artistCount;
private final int albumCount;
private final int songCount;
private Pattern pattern;
private final String query;
private final int artistCount;
private final int albumCount;
private final int songCount;
private Pattern pattern;
public SearchCritera(String query, int artistCount, int albumCount, int songCount) {
this.query = query;
this.artistCount = artistCount;
this.albumCount = albumCount;
this.songCount = songCount;
}
public SearchCritera(String query, int artistCount, int albumCount, int songCount) {
this.query = query;
this.artistCount = artistCount;
this.albumCount = albumCount;
this.songCount = songCount;
}
public String getQuery() {
return query;
}
public String getQuery() {
return query;
}
public int getArtistCount() {
return artistCount;
}
public int getArtistCount() {
return artistCount;
}
public int getAlbumCount() {
return albumCount;
}
public int getAlbumCount() {
return albumCount;
}
public int getSongCount() {
return songCount;
}
public int getSongCount() {
return songCount;
}
/**
* Returns and caches a pattern instance that can be used to check if a
* string matches the query.
*/
public Pattern getPattern() {
/**
* Returns and caches a pattern instance that can be used to check if a
* string matches the query.
*/
public Pattern getPattern() {
// If the pattern wasn't already cached, create a new regular expression
// from the search string :
// * Surround the search string with ".*" (match anything)
// * Replace spaces and wildcard '*' characters with ".*"
// * All other characters are properly quoted
if (this.pattern == null) {
String regex = ".*";
String currentPart = "";
for (int i = 0; i < query.length(); i++) {
char c = query.charAt(i);
if (c == '*' || c == ' ') {
regex += Pattern.quote(currentPart);
regex += ".*";
currentPart = "";
} else {
currentPart += c;
}
}
if (currentPart.length() > 0) {
regex += Pattern.quote(currentPart);
}
// If the pattern wasn't already cached, create a new regular expression
// from the search string :
// * Surround the search string with ".*" (match anything)
// * Replace spaces and wildcard '*' characters with ".*"
// * All other characters are properly quoted
if (this.pattern == null) {
String regex = ".*";
String currentPart = "";
for (int i = 0; i < query.length(); i++) {
char c = query.charAt(i);
if (c == '*' || c == ' ') {
regex += Pattern.quote(currentPart);
regex += ".*";
currentPart = "";
} else {
currentPart += c;
}
}
if (currentPart.length() > 0) {
regex += Pattern.quote(currentPart);
}
regex += ".*";
this.pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
}
regex += ".*";
this.pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
}
return this.pattern;
}
return this.pattern;
}
}

View File

@ -50,13 +50,13 @@ public class SearchResult implements Serializable {
return songs;
}
public boolean hasArtists() {
return !artists.isEmpty();
}
public boolean hasAlbums() {
return !albums.isEmpty();
}
public boolean hasSongs() {
return !songs.isEmpty();
}
public boolean hasArtists() {
return !artists.isEmpty();
}
public boolean hasAlbums() {
return !albums.isEmpty();
}
public boolean hasSongs() {
return !songs.isEmpty();
}
}

View File

@ -1,16 +1,16 @@
/*
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 2014 (C) Scott Jackson
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 2014 (C) Scott Jackson
*/
package net.nullsum.audinaut.domain;
@ -22,125 +22,125 @@ import java.util.ArrayList;
import java.util.List;
public class User implements Serializable {
public static final String ADMIN = "adminRole";
public static final String SETTINGS = "settingsRole";
public static final String DOWNLOAD = "downloadRole";
public static final String UPLOAD = "uploadRole";
public static final String COVERART = "coverArtRole";
public static final String COMMENT = "commentRole";
public static final String STREAM = "streamRole";
public static final List<String> ROLES = new ArrayList<>();
public static final String ADMIN = "adminRole";
public static final String SETTINGS = "settingsRole";
public static final String DOWNLOAD = "downloadRole";
public static final String UPLOAD = "uploadRole";
public static final String COVERART = "coverArtRole";
public static final String COMMENT = "commentRole";
public static final String STREAM = "streamRole";
public static final List<String> ROLES = new ArrayList<>();
static {
ROLES.add(ADMIN);
ROLES.add(SETTINGS);
ROLES.add(STREAM);
ROLES.add(DOWNLOAD);
ROLES.add(UPLOAD);
ROLES.add(COVERART);
ROLES.add(COMMENT);
}
static {
ROLES.add(ADMIN);
ROLES.add(SETTINGS);
ROLES.add(STREAM);
ROLES.add(DOWNLOAD);
ROLES.add(UPLOAD);
ROLES.add(COVERART);
ROLES.add(COMMENT);
}
private String username;
private String password;
private String email;
private String username;
private String password;
private String email;
private List<Setting> settings = new ArrayList<Setting>();
private List<Setting> musicFolders;
private List<Setting> settings = new ArrayList<Setting>();
private List<Setting> musicFolders;
public User() {
public User() {
}
}
public String getUsername() {
return username;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setEmail(String email) {
this.email = email;
}
public List<Setting> getSettings() {
return settings;
}
public void setSettings(List<Setting> settings) {
this.settings.clear();
this.settings.addAll(settings);
}
public void addSetting(String name, Boolean value) {
settings.add(new Setting(name, value));
}
public List<Setting> getSettings() {
return settings;
}
public void setSettings(List<Setting> settings) {
this.settings.clear();
this.settings.addAll(settings);
}
public void addSetting(String name, Boolean value) {
settings.add(new Setting(name, value));
}
public void addMusicFolder(MusicFolder musicFolder) {
if(musicFolders == null) {
musicFolders = new ArrayList<>();
}
public void addMusicFolder(MusicFolder musicFolder) {
if(musicFolders == null) {
musicFolders = new ArrayList<>();
}
musicFolders.add(new MusicFolderSetting(musicFolder.getId(), musicFolder.getName(), false));
}
public void addMusicFolder(MusicFolderSetting musicFolderSetting, boolean defaultValue) {
if(musicFolders == null) {
musicFolders = new ArrayList<>();
}
musicFolders.add(new MusicFolderSetting(musicFolder.getId(), musicFolder.getName(), false));
}
public void addMusicFolder(MusicFolderSetting musicFolderSetting, boolean defaultValue) {
if(musicFolders == null) {
musicFolders = new ArrayList<>();
}
musicFolders.add(new MusicFolderSetting(musicFolderSetting.getName(), musicFolderSetting.getLabel(), defaultValue));
}
public List<Setting> getMusicFolderSettings() {
return musicFolders;
}
musicFolders.add(new MusicFolderSetting(musicFolderSetting.getName(), musicFolderSetting.getLabel(), defaultValue));
}
public List<Setting> getMusicFolderSettings() {
return musicFolders;
}
public static class Setting implements Serializable {
private String name;
private Boolean value;
public static class Setting implements Serializable {
private String name;
private Boolean value;
public Setting() {
public Setting() {
}
public Setting(String name, Boolean value) {
this.name = name;
this.value = value;
}
}
public Setting(String name, Boolean value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public Boolean getValue() {
return value;
}
public void setValue(Boolean value) {
this.value = value;
}
}
public String getName() {
return name;
}
public Boolean getValue() {
return value;
}
public void setValue(Boolean value) {
this.value = value;
}
}
public static class MusicFolderSetting extends Setting {
private String label;
public static class MusicFolderSetting extends Setting {
private String label;
public MusicFolderSetting() {
public MusicFolderSetting() {
}
public MusicFolderSetting(String name, String label, Boolean value) {
super(name, value);
this.label = label;
}
}
public MusicFolderSetting(String name, String label, Boolean value) {
super(name, value);
this.label = label;
}
public String getLabel() {
return label;
}
}
public String getLabel() {
return label;
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2014 (C) Scott Jackson
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 2014 (C) Scott Jackson
*/
package net.nullsum.audinaut.fragments;
@ -46,145 +46,145 @@ import net.nullsum.audinaut.adapter.DownloadFileAdapter;
import net.nullsum.audinaut.view.UpdateView;
public class DownloadFragment extends SelectRecyclerFragment<DownloadFile> implements SectionAdapter.OnItemClickedListener<DownloadFile> {
private long currentRevision;
private ScheduledExecutorService executorService;
private long currentRevision;
private ScheduledExecutorService executorService;
public DownloadFragment() {
serialize = false;
pullToRefresh = false;
}
public DownloadFragment() {
serialize = false;
pullToRefresh = false;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
super.onCreateView(inflater, container, bundle);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
super.onCreateView(inflater, container, bundle);
ItemTouchHelper touchHelper = new ItemTouchHelper(new DownloadFileItemHelperCallback(this, false));
touchHelper.attachToRecyclerView(recyclerView);
ItemTouchHelper touchHelper = new ItemTouchHelper(new DownloadFileItemHelperCallback(this, false));
touchHelper.attachToRecyclerView(recyclerView);
return rootView;
}
return rootView;
}
@Override
public void onResume() {
super.onResume();
@Override
public void onResume() {
super.onResume();
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
update();
}
});
}
};
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
update();
}
});
}
};
executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleWithFixedDelay(runnable, 0L, 1000L, TimeUnit.MILLISECONDS);
}
executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleWithFixedDelay(runnable, 0L, 1000L, TimeUnit.MILLISECONDS);
}
@Override
public void onPause() {
super.onPause();
executorService.shutdown();
}
@Override
public void onPause() {
super.onPause();
executorService.shutdown();
}
@Override
public int getOptionsMenu() {
return R.menu.downloading;
}
@Override
public int getOptionsMenu() {
return R.menu.downloading;
}
@Override
public SectionAdapter getAdapter(List<DownloadFile> objs) {
return new DownloadFileAdapter(context, objs, this);
}
@Override
public SectionAdapter getAdapter(List<DownloadFile> objs) {
return new DownloadFileAdapter(context, objs, this);
}
@Override
public List<DownloadFile> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
DownloadService downloadService = getDownloadService();
if(downloadService == null) {
return new ArrayList<DownloadFile>();
}
@Override
public List<DownloadFile> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
DownloadService downloadService = getDownloadService();
if(downloadService == null) {
return new ArrayList<DownloadFile>();
}
List<DownloadFile> songList = new ArrayList<DownloadFile>();
songList.addAll(downloadService.getBackgroundDownloads());
currentRevision = downloadService.getDownloadListUpdateRevision();
return songList;
}
List<DownloadFile> songList = new ArrayList<DownloadFile>();
songList.addAll(downloadService.getBackgroundDownloads());
currentRevision = downloadService.getDownloadListUpdateRevision();
return songList;
}
@Override
public int getTitleResource() {
return R.string.button_bar_downloading;
}
@Override
public int getTitleResource() {
return R.string.button_bar_downloading;
}
@Override
public void onItemClicked(UpdateView<DownloadFile> updateView, DownloadFile item) {
@Override
public void onItemClicked(UpdateView<DownloadFile> updateView, DownloadFile item) {
}
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<DownloadFile> updateView, DownloadFile downloadFile) {
MusicDirectory.Entry selectedItem = downloadFile.getSong();
onCreateContextMenuSupport(menu, menuInflater, updateView, selectedItem);
if(!Util.isOffline(context)) {
menu.removeItem(R.id.song_menu_remove_playlist);
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<DownloadFile> updateView, DownloadFile downloadFile) {
MusicDirectory.Entry selectedItem = downloadFile.getSong();
onCreateContextMenuSupport(menu, menuInflater, updateView, selectedItem);
if(!Util.isOffline(context)) {
menu.removeItem(R.id.song_menu_remove_playlist);
}
recreateContextMenu(menu);
}
recreateContextMenu(menu);
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<DownloadFile> updateView, DownloadFile downloadFile) {
MusicDirectory.Entry selectedItem = downloadFile.getSong();
return onContextItemSelected(menuItem, selectedItem);
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<DownloadFile> updateView, DownloadFile downloadFile) {
MusicDirectory.Entry selectedItem = downloadFile.getSong();
return onContextItemSelected(menuItem, selectedItem);
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if(super.onOptionsItemSelected(menuItem)) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if(super.onOptionsItemSelected(menuItem)) {
return true;
}
switch (menuItem.getItemId()) {
case R.id.menu_remove_all:
Util.confirmDialog(context, R.string.download_menu_remove_all, "", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
getDownloadService().clearBackground();
return null;
}
switch (menuItem.getItemId()) {
case R.id.menu_remove_all:
Util.confirmDialog(context, R.string.download_menu_remove_all, "", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
getDownloadService().clearBackground();
return null;
}
@Override
protected void done(Void result) {
update();
}
}.execute();
}
});
return true;
}
@Override
protected void done(Void result) {
update();
}
}.execute();
}
});
return true;
}
return false;
}
return false;
}
private void update() {
DownloadService downloadService = getDownloadService();
if (downloadService == null || objects == null || adapter == null) {
return;
}
private void update() {
DownloadService downloadService = getDownloadService();
if (downloadService == null || objects == null || adapter == null) {
return;
}
if (currentRevision != downloadService.getDownloadListUpdateRevision()) {
List<DownloadFile> downloadFileList = downloadService.getBackgroundDownloads();
objects.clear();
objects.addAll(downloadFileList);
adapter.notifyDataSetChanged();
if (currentRevision != downloadService.getDownloadListUpdateRevision()) {
List<DownloadFile> downloadFileList = downloadService.getBackgroundDownloads();
objects.clear();
objects.addAll(downloadFileList);
adapter.notifyDataSetChanged();
currentRevision = downloadService.getDownloadListUpdateRevision();
}
}
currentRevision = downloadService.getDownloadListUpdateRevision();
}
}
}

View File

@ -48,412 +48,412 @@ import net.nullsum.audinaut.util.Util;
* Created by Scott on 10/27/13.
*/
public class EqualizerFragment extends SubsonicFragment {
private static final String TAG = EqualizerFragment.class.getSimpleName();
private static final String TAG = EqualizerFragment.class.getSimpleName();
private static final int MENU_GROUP_PRESET = 100;
private static final int MENU_GROUP_PRESET = 100;
private final Map<Short, SeekBar> bars = new HashMap<Short, SeekBar>();
private SeekBar bassBar;
private SeekBar loudnessBar;
private EqualizerController equalizerController;
private Equalizer equalizer;
private BassBoost bass;
private LoudnessEnhancerController loudnessEnhancer;
private short masterLevel = 0;
private final Map<Short, SeekBar> bars = new HashMap<Short, SeekBar>();
private SeekBar bassBar;
private SeekBar loudnessBar;
private EqualizerController equalizerController;
private Equalizer equalizer;
private BassBoost bass;
private LoudnessEnhancerController loudnessEnhancer;
private short masterLevel = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
rootView = inflater.inflate(R.layout.equalizer, container, false);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
rootView = inflater.inflate(R.layout.equalizer, container, false);
try {
DownloadService service = DownloadService.getInstance();
equalizerController = service.getEqualizerController();
equalizer = equalizerController.getEqualizer();
bass = equalizerController.getBassBoost();
loudnessEnhancer = equalizerController.getLoudnessEnhancerController();
try {
DownloadService service = DownloadService.getInstance();
equalizerController = service.getEqualizerController();
equalizer = equalizerController.getEqualizer();
bass = equalizerController.getBassBoost();
loudnessEnhancer = equalizerController.getLoudnessEnhancerController();
initEqualizer();
} catch(Exception e) {
Log.e(TAG, "Failed to initialize EQ", e);
Util.toast(context, "Failed to initialize EQ");
context.onBackPressed();
}
initEqualizer();
} catch(Exception e) {
Log.e(TAG, "Failed to initialize EQ", e);
Util.toast(context, "Failed to initialize EQ");
context.onBackPressed();
}
final View presetButton = rootView.findViewById(R.id.equalizer_preset);
registerForContextMenu(presetButton);
presetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
presetButton.showContextMenu();
}
});
final View presetButton = rootView.findViewById(R.id.equalizer_preset);
registerForContextMenu(presetButton);
presetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
presetButton.showContextMenu();
}
});
CheckBox enabledCheckBox = (CheckBox) rootView.findViewById(R.id.equalizer_enabled);
enabledCheckBox.setChecked(equalizer.getEnabled());
enabledCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
try {
setEqualizerEnabled(b);
} catch(Exception e) {
Log.e(TAG, "Failed to set EQ enabled", e);
Util.toast(context, "Failed to set EQ enabled");
context.onBackPressed();
}
}
});
CheckBox enabledCheckBox = (CheckBox) rootView.findViewById(R.id.equalizer_enabled);
enabledCheckBox.setChecked(equalizer.getEnabled());
enabledCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
try {
setEqualizerEnabled(b);
} catch(Exception e) {
Log.e(TAG, "Failed to set EQ enabled", e);
Util.toast(context, "Failed to set EQ enabled");
context.onBackPressed();
}
}
});
setTitle(R.string.equalizer_label);
setSubtitle(null);
setTitle(R.string.equalizer_label);
setSubtitle(null);
return rootView;
}
return rootView;
}
@Override
public void onPause() {
super.onPause();
@Override
public void onPause() {
super.onPause();
try {
equalizerController.saveSettings();
try {
equalizerController.saveSettings();
if (!equalizer.getEnabled()) {
equalizerController.release();
}
} catch(Exception e) {
Log.w(TAG, "Failed to release controller", e);
}
}
if (!equalizer.getEnabled()) {
equalizerController.release();
}
} catch(Exception e) {
Log.w(TAG, "Failed to release controller", e);
}
}
@Override
public void onResume() {
super.onResume();
equalizerController = DownloadService.getInstance().getEqualizerController();
equalizer = equalizerController.getEqualizer();
bass = equalizerController.getBassBoost();
}
@Override
public void onResume() {
super.onResume();
equalizerController = DownloadService.getInstance().getEqualizerController();
equalizer = equalizerController.getEqualizer();
bass = equalizerController.getBassBoost();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
if(!primaryFragment) {
return;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
if(!primaryFragment) {
return;
}
short currentPreset;
try {
currentPreset = equalizer.getCurrentPreset();
} catch (Exception x) {
currentPreset = -1;
}
short currentPreset;
try {
currentPreset = equalizer.getCurrentPreset();
} catch (Exception x) {
currentPreset = -1;
}
for (short preset = 0; preset < equalizer.getNumberOfPresets(); preset++) {
MenuItem menuItem = menu.add(MENU_GROUP_PRESET, preset, preset, equalizer.getPresetName(preset));
if (preset == currentPreset) {
menuItem.setChecked(true);
}
}
menu.setGroupCheckable(MENU_GROUP_PRESET, true, true);
}
for (short preset = 0; preset < equalizer.getNumberOfPresets(); preset++) {
MenuItem menuItem = menu.add(MENU_GROUP_PRESET, preset, preset, equalizer.getPresetName(preset));
if (preset == currentPreset) {
menuItem.setChecked(true);
}
}
menu.setGroupCheckable(MENU_GROUP_PRESET, true, true);
}
@Override
public boolean onContextItemSelected(MenuItem menuItem) {
short preset = (short) menuItem.getItemId();
for(int i = 0; i < 10; i++) {
try {
equalizer.usePreset(preset);
i = 10;
} catch (UnsupportedOperationException e) {
equalizerController.release();
equalizer = equalizerController.getEqualizer();
bass = equalizerController.getBassBoost();
loudnessEnhancer = equalizerController.getLoudnessEnhancerController();
}
}
updateBars(false);
return true;
}
@Override
public boolean onContextItemSelected(MenuItem menuItem) {
short preset = (short) menuItem.getItemId();
for(int i = 0; i < 10; i++) {
try {
equalizer.usePreset(preset);
i = 10;
} catch (UnsupportedOperationException e) {
equalizerController.release();
equalizer = equalizerController.getEqualizer();
bass = equalizerController.getBassBoost();
loudnessEnhancer = equalizerController.getLoudnessEnhancerController();
}
}
updateBars(false);
return true;
}
private void setEqualizerEnabled(boolean enabled) {
SharedPreferences prefs = Util.getPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(Constants.PREFERENCES_EQUALIZER_ON, enabled);
editor.apply();
for(int i = 0; i < 10; i++) {
try {
equalizer.setEnabled(enabled);
updateBars(true);
i = 10;
} catch (UnsupportedOperationException e) {
equalizerController.release();
equalizer = equalizerController.getEqualizer();
bass = equalizerController.getBassBoost();
loudnessEnhancer = equalizerController.getLoudnessEnhancerController();
}
}
}
private void setEqualizerEnabled(boolean enabled) {
SharedPreferences prefs = Util.getPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(Constants.PREFERENCES_EQUALIZER_ON, enabled);
editor.apply();
for(int i = 0; i < 10; i++) {
try {
equalizer.setEnabled(enabled);
updateBars(true);
i = 10;
} catch (UnsupportedOperationException e) {
equalizerController.release();
equalizer = equalizerController.getEqualizer();
bass = equalizerController.getBassBoost();
loudnessEnhancer = equalizerController.getLoudnessEnhancerController();
}
}
}
private void updateBars(boolean changedEnabled) {
try {
boolean isEnabled = equalizer.getEnabled();
short minEQLevel = equalizer.getBandLevelRange()[0];
short maxEQLevel = equalizer.getBandLevelRange()[1];
for (Map.Entry<Short, SeekBar> entry : bars.entrySet()) {
short band = entry.getKey();
SeekBar bar = entry.getValue();
bar.setEnabled(isEnabled);
if (band >= (short) 0) {
short setLevel;
if (changedEnabled) {
setLevel = (short) (equalizer.getBandLevel(band) - masterLevel);
if (isEnabled) {
bar.setProgress(equalizer.getBandLevel(band) - minEQLevel);
} else {
bar.setProgress(-minEQLevel);
}
} else {
bar.setProgress(equalizer.getBandLevel(band) - minEQLevel);
setLevel = (short) (equalizer.getBandLevel(band) + masterLevel);
}
if (setLevel < minEQLevel) {
setLevel = minEQLevel;
} else if (setLevel > maxEQLevel) {
setLevel = maxEQLevel;
}
equalizer.setBandLevel(band, setLevel);
} else if (!isEnabled) {
bar.setProgress(-minEQLevel);
}
}
private void updateBars(boolean changedEnabled) {
try {
boolean isEnabled = equalizer.getEnabled();
short minEQLevel = equalizer.getBandLevelRange()[0];
short maxEQLevel = equalizer.getBandLevelRange()[1];
for (Map.Entry<Short, SeekBar> entry : bars.entrySet()) {
short band = entry.getKey();
SeekBar bar = entry.getValue();
bar.setEnabled(isEnabled);
if (band >= (short) 0) {
short setLevel;
if (changedEnabled) {
setLevel = (short) (equalizer.getBandLevel(band) - masterLevel);
if (isEnabled) {
bar.setProgress(equalizer.getBandLevel(band) - minEQLevel);
} else {
bar.setProgress(-minEQLevel);
}
} else {
bar.setProgress(equalizer.getBandLevel(band) - minEQLevel);
setLevel = (short) (equalizer.getBandLevel(band) + masterLevel);
}
if (setLevel < minEQLevel) {
setLevel = minEQLevel;
} else if (setLevel > maxEQLevel) {
setLevel = maxEQLevel;
}
equalizer.setBandLevel(band, setLevel);
} else if (!isEnabled) {
bar.setProgress(-minEQLevel);
}
}
bassBar.setEnabled(isEnabled);
if (loudnessBar != null) {
loudnessBar.setEnabled(isEnabled);
}
if (changedEnabled && !isEnabled) {
bass.setStrength((short) 0);
bassBar.setProgress(0);
if (loudnessBar != null) {
loudnessEnhancer.setGain(0);
loudnessBar.setProgress(0);
}
}
bassBar.setEnabled(isEnabled);
if (loudnessBar != null) {
loudnessBar.setEnabled(isEnabled);
}
if (changedEnabled && !isEnabled) {
bass.setStrength((short) 0);
bassBar.setProgress(0);
if (loudnessBar != null) {
loudnessEnhancer.setGain(0);
loudnessBar.setProgress(0);
}
}
if (!isEnabled) {
masterLevel = 0;
SharedPreferences prefs = Util.getPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(Constants.PREFERENCES_EQUALIZER_SETTINGS, masterLevel);
editor.apply();
}
} catch(Exception e) {
Log.e(TAG, "Failed to update bars");
}
}
if (!isEnabled) {
masterLevel = 0;
SharedPreferences prefs = Util.getPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(Constants.PREFERENCES_EQUALIZER_SETTINGS, masterLevel);
editor.apply();
}
} catch(Exception e) {
Log.e(TAG, "Failed to update bars");
}
}
private void initEqualizer() {
LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.equalizer_layout);
private void initEqualizer() {
LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.equalizer_layout);
final short minEQLevel = equalizer.getBandLevelRange()[0];
final short maxEQLevel = equalizer.getBandLevelRange()[1];
final short minEQLevel = equalizer.getBandLevelRange()[0];
final short maxEQLevel = equalizer.getBandLevelRange()[1];
// Setup Pregain
SharedPreferences prefs = Util.getPreferences(context);
masterLevel = (short)prefs.getInt(Constants.PREFERENCES_EQUALIZER_SETTINGS, 0);
initPregain(layout, minEQLevel, maxEQLevel);
// Setup Pregain
SharedPreferences prefs = Util.getPreferences(context);
masterLevel = (short)prefs.getInt(Constants.PREFERENCES_EQUALIZER_SETTINGS, 0);
initPregain(layout, minEQLevel, maxEQLevel);
for (short i = 0; i < equalizer.getNumberOfBands(); i++) {
final short band = i;
for (short i = 0; i < equalizer.getNumberOfBands(); i++) {
final short band = i;
View bandBar = LayoutInflater.from(context).inflate(R.layout.equalizer_bar, null);
TextView freqTextView = (TextView) bandBar.findViewById(R.id.equalizer_frequency);
final TextView levelTextView = (TextView) bandBar.findViewById(R.id.equalizer_level);
SeekBar bar = (SeekBar) bandBar.findViewById(R.id.equalizer_bar);
View bandBar = LayoutInflater.from(context).inflate(R.layout.equalizer_bar, null);
TextView freqTextView = (TextView) bandBar.findViewById(R.id.equalizer_frequency);
final TextView levelTextView = (TextView) bandBar.findViewById(R.id.equalizer_level);
SeekBar bar = (SeekBar) bandBar.findViewById(R.id.equalizer_bar);
freqTextView.setText((equalizer.getCenterFreq(band) / 1000) + " Hz");
freqTextView.setText((equalizer.getCenterFreq(band) / 1000) + " Hz");
bars.put(band, bar);
bar.setMax(maxEQLevel - minEQLevel);
short level = equalizer.getBandLevel(band);
if(equalizer.getEnabled()) {
level = (short) (level - masterLevel);
}
bar.setProgress(level - minEQLevel);
bar.setEnabled(equalizer.getEnabled());
updateLevelText(levelTextView, level);
bars.put(band, bar);
bar.setMax(maxEQLevel - minEQLevel);
short level = equalizer.getBandLevel(band);
if(equalizer.getEnabled()) {
level = (short) (level - masterLevel);
}
bar.setProgress(level - minEQLevel);
bar.setEnabled(equalizer.getEnabled());
updateLevelText(levelTextView, level);
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
try {
short level = (short) (progress + minEQLevel);
if (fromUser) {
equalizer.setBandLevel(band, (short) (level + masterLevel));
}
updateLevelText(levelTextView, level);
} catch(Exception e) {
Log.e(TAG, "Failed to change equalizer", e);
}
}
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
try {
short level = (short) (progress + minEQLevel);
if (fromUser) {
equalizer.setBandLevel(band, (short) (level + masterLevel));
}
updateLevelText(levelTextView, level);
} catch(Exception e) {
Log.e(TAG, "Failed to change equalizer", e);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
layout.addView(bandBar);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
layout.addView(bandBar);
}
LinearLayout specialLayout = (LinearLayout) rootView.findViewById(R.id.special_effects_layout);
LinearLayout specialLayout = (LinearLayout) rootView.findViewById(R.id.special_effects_layout);
// Setup bass booster
View bandBar = LayoutInflater.from(context).inflate(R.layout.equalizer_bar, null);
TextView freqTextView = (TextView) bandBar.findViewById(R.id.equalizer_frequency);
final TextView bassTextView = (TextView) bandBar.findViewById(R.id.equalizer_level);
bassBar = (SeekBar) bandBar.findViewById(R.id.equalizer_bar);
// Setup bass booster
View bandBar = LayoutInflater.from(context).inflate(R.layout.equalizer_bar, null);
TextView freqTextView = (TextView) bandBar.findViewById(R.id.equalizer_frequency);
final TextView bassTextView = (TextView) bandBar.findViewById(R.id.equalizer_level);
bassBar = (SeekBar) bandBar.findViewById(R.id.equalizer_bar);
freqTextView.setText(R.string.equalizer_bass_booster);
bassBar.setEnabled(equalizer.getEnabled());
short bassLevel = 0;
if(bass.getEnabled()) {
bassLevel = bass.getRoundedStrength();
}
bassTextView.setText(context.getResources().getString(R.string.equalizer_bass_size, bassLevel));
bassBar.setMax(1000);
bassBar.setProgress(bassLevel);
bassBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
try {
bassTextView.setText(context.getResources().getString(R.string.equalizer_bass_size, progress));
if (fromUser) {
if (progress > 0) {
if (!bass.getEnabled()) {
bass.setEnabled(true);
}
bass.setStrength((short) progress);
} else if (progress == 0 && bass.getEnabled()) {
bass.setStrength((short) progress);
bass.setEnabled(false);
}
}
} catch(Exception e) {
Log.w(TAG, "Error on changing bass: ", e);
}
}
freqTextView.setText(R.string.equalizer_bass_booster);
bassBar.setEnabled(equalizer.getEnabled());
short bassLevel = 0;
if(bass.getEnabled()) {
bassLevel = bass.getRoundedStrength();
}
bassTextView.setText(context.getResources().getString(R.string.equalizer_bass_size, bassLevel));
bassBar.setMax(1000);
bassBar.setProgress(bassLevel);
bassBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
try {
bassTextView.setText(context.getResources().getString(R.string.equalizer_bass_size, progress));
if (fromUser) {
if (progress > 0) {
if (!bass.getEnabled()) {
bass.setEnabled(true);
}
bass.setStrength((short) progress);
} else if (progress == 0 && bass.getEnabled()) {
bass.setStrength((short) progress);
bass.setEnabled(false);
}
}
} catch(Exception e) {
Log.w(TAG, "Error on changing bass: ", e);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
specialLayout.addView(bandBar);
}
});
specialLayout.addView(bandBar);
if(loudnessEnhancer != null && loudnessEnhancer.isAvailable()) {
// Setup loudness enhancer
bandBar = LayoutInflater.from(context).inflate(R.layout.equalizer_bar, null);
freqTextView = (TextView) bandBar.findViewById(R.id.equalizer_frequency);
final TextView loudnessTextView = (TextView) bandBar.findViewById(R.id.equalizer_level);
loudnessBar = (SeekBar) bandBar.findViewById(R.id.equalizer_bar);
if(loudnessEnhancer != null && loudnessEnhancer.isAvailable()) {
// Setup loudness enhancer
bandBar = LayoutInflater.from(context).inflate(R.layout.equalizer_bar, null);
freqTextView = (TextView) bandBar.findViewById(R.id.equalizer_frequency);
final TextView loudnessTextView = (TextView) bandBar.findViewById(R.id.equalizer_level);
loudnessBar = (SeekBar) bandBar.findViewById(R.id.equalizer_bar);
freqTextView.setText(R.string.equalizer_voice_booster);
loudnessBar.setEnabled(equalizer.getEnabled());
int loudnessLevel = 0;
if(loudnessEnhancer.isEnabled()) {
loudnessLevel = (int) loudnessEnhancer.getGain();
}
loudnessBar.setProgress(loudnessLevel / 100);
loudnessTextView.setText(context.getResources().getString(R.string.equalizer_db_size, loudnessLevel / 100));
loudnessBar.setMax(15);
loudnessBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
try {
loudnessTextView.setText(context.getResources().getString(R.string.equalizer_db_size, progress));
if(fromUser) {
if(progress > 0) {
if(!loudnessEnhancer.isEnabled()) {
loudnessEnhancer.enable();
}
loudnessEnhancer.setGain(progress * 100);
} else if(progress == 0 && loudnessEnhancer.isEnabled()) {
loudnessEnhancer.setGain(progress * 100);
loudnessEnhancer.disable();
}
}
} catch(Exception e) {
Log.w(TAG, "Error on changing loudness: ", e);
}
}
freqTextView.setText(R.string.equalizer_voice_booster);
loudnessBar.setEnabled(equalizer.getEnabled());
int loudnessLevel = 0;
if(loudnessEnhancer.isEnabled()) {
loudnessLevel = (int) loudnessEnhancer.getGain();
}
loudnessBar.setProgress(loudnessLevel / 100);
loudnessTextView.setText(context.getResources().getString(R.string.equalizer_db_size, loudnessLevel / 100));
loudnessBar.setMax(15);
loudnessBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
try {
loudnessTextView.setText(context.getResources().getString(R.string.equalizer_db_size, progress));
if(fromUser) {
if(progress > 0) {
if(!loudnessEnhancer.isEnabled()) {
loudnessEnhancer.enable();
}
loudnessEnhancer.setGain(progress * 100);
} else if(progress == 0 && loudnessEnhancer.isEnabled()) {
loudnessEnhancer.setGain(progress * 100);
loudnessEnhancer.disable();
}
}
} catch(Exception e) {
Log.w(TAG, "Error on changing loudness: ", e);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
specialLayout.addView(bandBar);
}
}
}
});
specialLayout.addView(bandBar);
}
}
private void initPregain(LinearLayout layout, final short minEQLevel, final short maxEQLevel) {
View bandBar = LayoutInflater.from(context).inflate(R.layout.equalizer_bar, null);
TextView freqTextView = (TextView) bandBar.findViewById(R.id.equalizer_frequency);
final TextView levelTextView = (TextView) bandBar.findViewById(R.id.equalizer_level);
SeekBar bar = (SeekBar) bandBar.findViewById(R.id.equalizer_bar);
private void initPregain(LinearLayout layout, final short minEQLevel, final short maxEQLevel) {
View bandBar = LayoutInflater.from(context).inflate(R.layout.equalizer_bar, null);
TextView freqTextView = (TextView) bandBar.findViewById(R.id.equalizer_frequency);
final TextView levelTextView = (TextView) bandBar.findViewById(R.id.equalizer_level);
SeekBar bar = (SeekBar) bandBar.findViewById(R.id.equalizer_bar);
freqTextView.setText("Master");
freqTextView.setText("Master");
bars.put((short)-1, bar);
bar.setMax(maxEQLevel - minEQLevel);
bar.setProgress(masterLevel - minEQLevel);
bar.setEnabled(equalizer.getEnabled());
updateLevelText(levelTextView, masterLevel);
bars.put((short)-1, bar);
bar.setMax(maxEQLevel - minEQLevel);
bar.setProgress(masterLevel - minEQLevel);
bar.setEnabled(equalizer.getEnabled());
updateLevelText(levelTextView, masterLevel);
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
try {
masterLevel = (short) (progress + minEQLevel);
if (fromUser) {
SharedPreferences prefs = Util.getPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(Constants.PREFERENCES_EQUALIZER_SETTINGS, masterLevel);
editor.apply();
for (short i = 0; i < equalizer.getNumberOfBands(); i++) {
short level = (short) ((bars.get(i).getProgress() + minEQLevel) + masterLevel);
equalizer.setBandLevel(i, level);
}
}
updateLevelText(levelTextView, masterLevel);
} catch(Exception e) {
Log.e(TAG, "Failed to change equalizer", e);
}
}
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
try {
masterLevel = (short) (progress + minEQLevel);
if (fromUser) {
SharedPreferences prefs = Util.getPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(Constants.PREFERENCES_EQUALIZER_SETTINGS, masterLevel);
editor.apply();
for (short i = 0; i < equalizer.getNumberOfBands(); i++) {
short level = (short) ((bars.get(i).getProgress() + minEQLevel) + masterLevel);
equalizer.setBandLevel(i, level);
}
}
updateLevelText(levelTextView, masterLevel);
} catch(Exception e) {
Log.e(TAG, "Failed to change equalizer", e);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
layout.addView(bandBar);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
layout.addView(bandBar);
}
private void updateLevelText(TextView levelTextView, short level) {
levelTextView.setText((level > 0 ? "+" : "") + context.getResources().getString(R.string.equalizer_db_size, level / 100));
}
private void updateLevelText(TextView levelTextView, short level) {
levelTextView.setText((level > 0 ? "+" : "") + context.getResources().getString(R.string.equalizer_db_size, level / 100));
}
}

View File

@ -42,179 +42,179 @@ import java.util.Arrays;
import java.util.List;
public class MainFragment extends SelectRecyclerFragment<Integer> {
private static final String TAG = MainFragment.class.getSimpleName();
public static final String SONGS_LIST_PREFIX = "songs-";
public static final String SONGS_NEWEST = SONGS_LIST_PREFIX + "newest";
public static final String SONGS_TOP_PLAYED = SONGS_LIST_PREFIX + "topPlayed";
public static final String SONGS_RECENT = SONGS_LIST_PREFIX + "recent";
public static final String SONGS_FREQUENT = SONGS_LIST_PREFIX + "frequent";
private static final String TAG = MainFragment.class.getSimpleName();
public static final String SONGS_LIST_PREFIX = "songs-";
public static final String SONGS_NEWEST = SONGS_LIST_PREFIX + "newest";
public static final String SONGS_TOP_PLAYED = SONGS_LIST_PREFIX + "topPlayed";
public static final String SONGS_RECENT = SONGS_LIST_PREFIX + "recent";
public static final String SONGS_FREQUENT = SONGS_LIST_PREFIX + "frequent";
public MainFragment() {
super();
pullToRefresh = false;
serialize = false;
backgroundUpdate = false;
alwaysFullscreen = true;
}
public MainFragment() {
super();
pullToRefresh = false;
serialize = false;
backgroundUpdate = false;
alwaysFullscreen = true;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.main, menu);
onFinishSetupOptionsMenu(menu);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.main, menu);
onFinishSetupOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(super.onOptionsItemSelected(item)) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(super.onOptionsItemSelected(item)) {
return true;
}
return false;
}
return false;
}
@Override
public int getOptionsMenu() {
return 0;
}
@Override
public int getOptionsMenu() {
return 0;
}
@Override
public SectionAdapter getAdapter(List objs) {
List<List<Integer>> sections = new ArrayList<>();
List<String> headers = new ArrayList<>();
@Override
public SectionAdapter getAdapter(List objs) {
List<List<Integer>> sections = new ArrayList<>();
List<String> headers = new ArrayList<>();
List<Integer> albums = new ArrayList<>();
albums.add(R.string.main_albums_random);
List<Integer> albums = new ArrayList<>();
albums.add(R.string.main_albums_random);
albums.add(R.string.main_albums_alphabetical);
albums.add(R.string.main_albums_genres);
albums.add(R.string.main_albums_year);
albums.add(R.string.main_albums_recent);
albums.add(R.string.main_albums_frequent);
albums.add(R.string.main_albums_genres);
albums.add(R.string.main_albums_year);
albums.add(R.string.main_albums_recent);
albums.add(R.string.main_albums_frequent);
sections.add(albums);
headers.add("albums");
sections.add(albums);
headers.add("albums");
return new MainAdapter(context, headers, sections, this);
}
return new MainAdapter(context, headers, sections, this);
}
@Override
public List<Integer> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
return Arrays.asList(0);
}
@Override
public List<Integer> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
return Arrays.asList(0);
}
@Override
public int getTitleResource() {
return R.string.common_appname;
}
@Override
public int getTitleResource() {
return R.string.common_appname;
}
private void showAlbumList(String type) {
if("genres".equals(type)) {
SubsonicFragment fragment = new SelectGenreFragment();
replaceFragment(fragment);
} else if("years".equals(type)) {
SubsonicFragment fragment = new SelectYearFragment();
replaceFragment(fragment);
} else {
// Clear out recently added count when viewing
if("newest".equals(type)) {
SharedPreferences.Editor editor = Util.getPreferences(context).edit();
editor.putInt(Constants.PREFERENCES_KEY_RECENT_COUNT + Util.getActiveServer(context), 0);
editor.apply();
}
private void showAlbumList(String type) {
if("genres".equals(type)) {
SubsonicFragment fragment = new SelectGenreFragment();
replaceFragment(fragment);
} else if("years".equals(type)) {
SubsonicFragment fragment = new SelectYearFragment();
replaceFragment(fragment);
} else {
// Clear out recently added count when viewing
if("newest".equals(type)) {
SharedPreferences.Editor editor = Util.getPreferences(context).edit();
editor.putInt(Constants.PREFERENCES_KEY_RECENT_COUNT + Util.getActiveServer(context), 0);
editor.apply();
}
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE, type);
args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 20);
args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0);
fragment.setArguments(args);
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE, type);
args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 20);
args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0);
fragment.setArguments(args);
replaceFragment(fragment);
}
}
replaceFragment(fragment);
}
}
private void showAboutDialog() {
new LoadingTask<Void>(context) {
Long[] used;
long bytesTotalFs;
long bytesAvailableFs;
private void showAboutDialog() {
new LoadingTask<Void>(context) {
Long[] used;
long bytesTotalFs;
long bytesAvailableFs;
@Override
protected Void doInBackground() throws Throwable {
File rootFolder = FileUtil.getMusicDirectory(context);
StatFs stat = new StatFs(rootFolder.getPath());
bytesTotalFs = (long) stat.getBlockCount() * (long) stat.getBlockSize();
bytesAvailableFs = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
@Override
protected Void doInBackground() throws Throwable {
File rootFolder = FileUtil.getMusicDirectory(context);
StatFs stat = new StatFs(rootFolder.getPath());
bytesTotalFs = (long) stat.getBlockCount() * (long) stat.getBlockSize();
bytesAvailableFs = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
used = FileUtil.getUsedSize(context, rootFolder);
return null;
}
used = FileUtil.getUsedSize(context, rootFolder);
return null;
}
@Override
protected void done(Void result) {
List<Integer> headers = new ArrayList<>();
List<String> details = new ArrayList<>();
@Override
protected void done(Void result) {
List<Integer> headers = new ArrayList<>();
List<String> details = new ArrayList<>();
headers.add(R.string.details_author);
details.add("Andrew Rabert");
headers.add(R.string.details_author);
details.add("Andrew Rabert");
headers.add(R.string.details_email);
details.add("ar@nullsum.net");
headers.add(R.string.details_email);
details.add("ar@nullsum.net");
try {
headers.add(R.string.details_version);
details.add(context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName);
} catch(Exception e) {
details.add("");
}
try {
headers.add(R.string.details_version);
details.add(context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName);
} catch(Exception e) {
details.add("");
}
Resources res = context.getResources();
headers.add(R.string.details_files_cached);
details.add(Long.toString(used[0]));
Resources res = context.getResources();
headers.add(R.string.details_files_cached);
details.add(Long.toString(used[0]));
headers.add(R.string.details_files_permanent);
details.add(Long.toString(used[1]));
headers.add(R.string.details_files_permanent);
details.add(Long.toString(used[1]));
headers.add(R.string.details_used_space);
details.add(res.getString(R.string.details_of, Util.formatLocalizedBytes(used[2], context), Util.formatLocalizedBytes(Util.getCacheSizeMB(context) * 1024L * 1024L, context)));
headers.add(R.string.details_used_space);
details.add(res.getString(R.string.details_of, Util.formatLocalizedBytes(used[2], context), Util.formatLocalizedBytes(Util.getCacheSizeMB(context) * 1024L * 1024L, context)));
headers.add(R.string.details_available_space);
details.add(res.getString(R.string.details_of, Util.formatLocalizedBytes(bytesAvailableFs, context), Util.formatLocalizedBytes(bytesTotalFs, context)));
headers.add(R.string.details_available_space);
details.add(res.getString(R.string.details_of, Util.formatLocalizedBytes(bytesAvailableFs, context), Util.formatLocalizedBytes(bytesTotalFs, context)));
Util.showDetailsDialog(context, R.string.main_about_title, headers, details);
}
}.execute();
}
Util.showDetailsDialog(context, R.string.main_about_title, headers, details);
}
}.execute();
}
@Override
public void onItemClicked(UpdateView<Integer> updateView, Integer item) {
if (item == R.string.main_albums_random) {
showAlbumList("random");
} else if (item == R.string.main_albums_recent) {
showAlbumList("recent");
} else if (item == R.string.main_albums_frequent) {
showAlbumList("frequent");
} else if(item == R.string.main_albums_genres) {
showAlbumList("genres");
} else if(item == R.string.main_albums_year) {
showAlbumList("years");
} else if(item == R.string.main_albums_alphabetical) {
showAlbumList("alphabeticalByName");
} else if (item == R.string.main_songs_newest) {
showAlbumList(SONGS_NEWEST);
} else if (item == R.string.main_songs_top_played) {
showAlbumList(SONGS_TOP_PLAYED);
} else if (item == R.string.main_songs_recent) {
showAlbumList(SONGS_RECENT);
} else if (item == R.string.main_songs_frequent) {
showAlbumList(SONGS_FREQUENT);
}
}
@Override
public void onItemClicked(UpdateView<Integer> updateView, Integer item) {
if (item == R.string.main_albums_random) {
showAlbumList("random");
} else if (item == R.string.main_albums_recent) {
showAlbumList("recent");
} else if (item == R.string.main_albums_frequent) {
showAlbumList("frequent");
} else if(item == R.string.main_albums_genres) {
showAlbumList("genres");
} else if(item == R.string.main_albums_year) {
showAlbumList("years");
} else if(item == R.string.main_albums_alphabetical) {
showAlbumList("alphabeticalByName");
} else if (item == R.string.main_songs_newest) {
showAlbumList(SONGS_NEWEST);
} else if (item == R.string.main_songs_top_played) {
showAlbumList(SONGS_TOP_PLAYED);
} else if (item == R.string.main_songs_recent) {
showAlbumList(SONGS_RECENT);
} else if (item == R.string.main_songs_frequent) {
showAlbumList(SONGS_FREQUENT);
}
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Integer> updateView, Integer item) {}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Integer> updateView, Integer item) {}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Integer> updateView, Integer item) {
return false;
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Integer> updateView, Integer item) {
return false;
}
}

View File

@ -1,20 +1,20 @@
/*
This file is part of Subsonic.
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 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.
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/>.
You should have received a copy of the GNU General Public License
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2014 (C) Scott Jackson
Copyright 2014 (C) Scott Jackson
*/
package net.nullsum.audinaut.fragments;
@ -42,293 +42,293 @@ import net.nullsum.audinaut.R;
import net.nullsum.audinaut.util.Constants;
public abstract class PreferenceCompatFragment extends SubsonicFragment {
private static final String TAG = PreferenceCompatFragment.class.getSimpleName();
private static final int FIRST_REQUEST_CODE = 100;
private static final int MSG_BIND_PREFERENCES = 1;
private static final String PREFERENCES_TAG = "android:preferences";
private boolean mHavePrefs;
private boolean mInitDone;
private ListView mList;
private PreferenceManager mPreferenceManager;
private static final String TAG = PreferenceCompatFragment.class.getSimpleName();
private static final int FIRST_REQUEST_CODE = 100;
private static final int MSG_BIND_PREFERENCES = 1;
private static final String PREFERENCES_TAG = "android:preferences";
private boolean mHavePrefs;
private boolean mInitDone;
private ListView mList;
private PreferenceManager mPreferenceManager;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_BIND_PREFERENCES:
bindPreferences();
break;
}
}
};
case MSG_BIND_PREFERENCES:
bindPreferences();
break;
}
}
};
final private Runnable mRequestFocus = new Runnable() {
public void run() {
mList.focusableViewAvailable(mList);
}
};
final private Runnable mRequestFocus = new Runnable() {
public void run() {
mList.focusableViewAvailable(mList);
}
};
private void bindPreferences() {
PreferenceScreen localPreferenceScreen = getPreferenceScreen();
if (localPreferenceScreen != null) {
ListView localListView = getListView();
localPreferenceScreen.bind(localListView);
}
}
private void bindPreferences() {
PreferenceScreen localPreferenceScreen = getPreferenceScreen();
if (localPreferenceScreen != null) {
ListView localListView = getListView();
localPreferenceScreen.bind(localListView);
}
}
private void ensureList() {
if (mList == null) {
View view = getView();
if (view == null) {
throw new IllegalStateException("Content view not yet created");
}
private void ensureList() {
if (mList == null) {
View view = getView();
if (view == null) {
throw new IllegalStateException("Content view not yet created");
}
View listView = view.findViewById(android.R.id.list);
if (!(listView instanceof ListView)) {
throw new RuntimeException("Content has view with id attribute 'android.R.id.list' that is not a ListView class");
}
View listView = view.findViewById(android.R.id.list);
if (!(listView instanceof ListView)) {
throw new RuntimeException("Content has view with id attribute 'android.R.id.list' that is not a ListView class");
}
mList = (ListView)listView;
if (mList == null) {
throw new RuntimeException("Your content must have a ListView whose id attribute is 'android.R.id.list'");
}
mList = (ListView)listView;
if (mList == null) {
throw new RuntimeException("Your content must have a ListView whose id attribute is 'android.R.id.list'");
}
mHandler.post(mRequestFocus);
}
}
mHandler.post(mRequestFocus);
}
}
private void postBindPreferences() {
if (mHandler.hasMessages(MSG_BIND_PREFERENCES)) {
mHandler.obtainMessage(MSG_BIND_PREFERENCES).sendToTarget();
}
}
private void postBindPreferences() {
if (mHandler.hasMessages(MSG_BIND_PREFERENCES)) {
mHandler.obtainMessage(MSG_BIND_PREFERENCES).sendToTarget();
}
}
private void requirePreferenceManager() {
if (this.mPreferenceManager == null) {
throw new RuntimeException("This should be called after super.onCreate.");
}
}
private void requirePreferenceManager() {
if (this.mPreferenceManager == null) {
throw new RuntimeException("This should be called after super.onCreate.");
}
}
public void addPreferencesFromIntent(Intent intent) {
requirePreferenceManager();
PreferenceScreen screen = inflateFromIntent(intent, getPreferenceScreen());
setPreferenceScreen(screen);
}
public void addPreferencesFromIntent(Intent intent) {
requirePreferenceManager();
PreferenceScreen screen = inflateFromIntent(intent, getPreferenceScreen());
setPreferenceScreen(screen);
}
public PreferenceScreen addPreferencesFromResource(int resId) {
requirePreferenceManager();
PreferenceScreen screen = inflateFromResource(getActivity(), resId, getPreferenceScreen());
setPreferenceScreen(screen);
public PreferenceScreen addPreferencesFromResource(int resId) {
requirePreferenceManager();
PreferenceScreen screen = inflateFromResource(getActivity(), resId, getPreferenceScreen());
setPreferenceScreen(screen);
for(int i = 0; i < screen.getPreferenceCount(); i++) {
Preference preference = screen.getPreference(i);
if(preference instanceof PreferenceScreen && preference.getKey() != null) {
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onStartNewFragment(preference.getKey());
return false;
}
});
}
}
for(int i = 0; i < screen.getPreferenceCount(); i++) {
Preference preference = screen.getPreference(i);
if(preference instanceof PreferenceScreen && preference.getKey() != null) {
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onStartNewFragment(preference.getKey());
return false;
}
});
}
}
return screen;
}
return screen;
}
public Preference findPreference(CharSequence key) {
if (mPreferenceManager == null) {
return null;
}
return mPreferenceManager.findPreference(key);
}
public Preference findPreference(CharSequence key) {
if (mPreferenceManager == null) {
return null;
}
return mPreferenceManager.findPreference(key);
}
public ListView getListView() {
ensureList();
return mList;
}
public ListView getListView() {
ensureList();
return mList;
}
public PreferenceManager getPreferenceManager() {
return mPreferenceManager;
}
public PreferenceManager getPreferenceManager() {
return mPreferenceManager;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
if (mHavePrefs) {
bindPreferences();
}
mInitDone = true;
if (savedInstanceState != null) {
Bundle localBundle = savedInstanceState.getBundle(PREFERENCES_TAG);
if (localBundle != null) {
PreferenceScreen screen = getPreferenceScreen();
if (screen != null) {
screen.restoreHierarchyState(localBundle);
}
}
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
if (mHavePrefs) {
bindPreferences();
}
mInitDone = true;
if (savedInstanceState != null) {
Bundle localBundle = savedInstanceState.getBundle(PREFERENCES_TAG);
if (localBundle != null) {
PreferenceScreen screen = getPreferenceScreen();
if (screen != null) {
screen.restoreHierarchyState(localBundle);
}
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
dispatchActivityResult(requestCode, resultCode, data);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
dispatchActivityResult(requestCode, resultCode, data);
}
@Override
public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
mPreferenceManager = createPreferenceManager();
@Override
public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
mPreferenceManager = createPreferenceManager();
int res = this.getArguments().getInt(Constants.INTENT_EXTRA_FRAGMENT_TYPE, 0);
if(res != 0) {
PreferenceScreen preferenceScreen = addPreferencesFromResource(res);
onInitPreferences(preferenceScreen);
}
}
int res = this.getArguments().getInt(Constants.INTENT_EXTRA_FRAGMENT_TYPE, 0);
if(res != 0) {
PreferenceScreen preferenceScreen = addPreferencesFromResource(res);
onInitPreferences(preferenceScreen);
}
}
@Override
public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle) {
return paramLayoutInflater.inflate(R.layout.preferences, paramViewGroup, false);
}
@Override
public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle) {
return paramLayoutInflater.inflate(R.layout.preferences, paramViewGroup, false);
}
@Override
public void onDestroy() {
super.onDestroy();
dispatchActivityDestroy();
}
@Override
public void onDestroy() {
super.onDestroy();
dispatchActivityDestroy();
}
@Override
public void onDestroyView() {
mList = null;
mHandler.removeCallbacks(mRequestFocus);
mHandler.removeMessages(MSG_BIND_PREFERENCES);
super.onDestroyView();
}
@Override
public void onDestroyView() {
mList = null;
mHandler.removeCallbacks(mRequestFocus);
mHandler.removeMessages(MSG_BIND_PREFERENCES);
super.onDestroyView();
}
@Override
public void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
PreferenceScreen screen = getPreferenceScreen();
if (screen != null) {
Bundle localBundle = new Bundle();
screen.saveHierarchyState(localBundle);
bundle.putBundle(PREFERENCES_TAG, localBundle);
}
}
@Override
public void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
PreferenceScreen screen = getPreferenceScreen();
if (screen != null) {
Bundle localBundle = new Bundle();
screen.saveHierarchyState(localBundle);
bundle.putBundle(PREFERENCES_TAG, localBundle);
}
}
@Override
public void onStop() {
super.onStop();
dispatchActivityStop();
}
@Override
public void onStop() {
super.onStop();
dispatchActivityStop();
}
/** Access methods with visibility private **/
/** Access methods with visibility private **/
private PreferenceManager createPreferenceManager() {
try {
Constructor<PreferenceManager> c = PreferenceManager.class.getDeclaredConstructor(Activity.class, int.class);
c.setAccessible(true);
return c.newInstance(this.getActivity(), FIRST_REQUEST_CODE);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private PreferenceManager createPreferenceManager() {
try {
Constructor<PreferenceManager> c = PreferenceManager.class.getDeclaredConstructor(Activity.class, int.class);
c.setAccessible(true);
return c.newInstance(this.getActivity(), FIRST_REQUEST_CODE);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private PreferenceScreen getPreferenceScreen() {
try {
Method m = PreferenceManager.class.getDeclaredMethod("getPreferenceScreen");
m.setAccessible(true);
return (PreferenceScreen) m.invoke(mPreferenceManager);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private PreferenceScreen getPreferenceScreen() {
try {
Method m = PreferenceManager.class.getDeclaredMethod("getPreferenceScreen");
m.setAccessible(true);
return (PreferenceScreen) m.invoke(mPreferenceManager);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected void setPreferenceScreen(PreferenceScreen preferenceScreen) {
try {
Method m = PreferenceManager.class.getDeclaredMethod("setPreferences", PreferenceScreen.class);
m.setAccessible(true);
boolean result = (Boolean) m.invoke(mPreferenceManager, preferenceScreen);
if (result && preferenceScreen != null) {
mHavePrefs = true;
if (mInitDone) {
postBindPreferences();
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected void setPreferenceScreen(PreferenceScreen preferenceScreen) {
try {
Method m = PreferenceManager.class.getDeclaredMethod("setPreferences", PreferenceScreen.class);
m.setAccessible(true);
boolean result = (Boolean) m.invoke(mPreferenceManager, preferenceScreen);
if (result && preferenceScreen != null) {
mHavePrefs = true;
if (mInitDone) {
postBindPreferences();
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void dispatchActivityResult(int requestCode, int resultCode, Intent data) {
try {
Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityResult", int.class, int.class, Intent.class);
m.setAccessible(true);
m.invoke(mPreferenceManager, requestCode, resultCode, data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void dispatchActivityResult(int requestCode, int resultCode, Intent data) {
try {
Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityResult", int.class, int.class, Intent.class);
m.setAccessible(true);
m.invoke(mPreferenceManager, requestCode, resultCode, data);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void dispatchActivityDestroy() {
try {
Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityDestroy");
m.setAccessible(true);
m.invoke(mPreferenceManager);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void dispatchActivityDestroy() {
try {
Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityDestroy");
m.setAccessible(true);
m.invoke(mPreferenceManager);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void dispatchActivityStop() {
try {
Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityStop");
m.setAccessible(true);
m.invoke(mPreferenceManager);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void dispatchActivityStop() {
try {
Method m = PreferenceManager.class.getDeclaredMethod("dispatchActivityStop");
m.setAccessible(true);
m.invoke(mPreferenceManager);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void setFragment(PreferenceFragment preferenceFragment) {
try {
Method m = PreferenceManager.class.getDeclaredMethod("setFragment", PreferenceFragment.class);
m.setAccessible(true);
m.invoke(mPreferenceManager, preferenceFragment);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void setFragment(PreferenceFragment preferenceFragment) {
try {
Method m = PreferenceManager.class.getDeclaredMethod("setFragment", PreferenceFragment.class);
m.setAccessible(true);
m.invoke(mPreferenceManager, preferenceFragment);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public PreferenceScreen inflateFromResource(Context context, int resId, PreferenceScreen rootPreferences) {
PreferenceScreen preferenceScreen ;
try {
Method m = PreferenceManager.class.getDeclaredMethod("inflateFromResource", Context.class, int.class, PreferenceScreen.class);
m.setAccessible(true);
preferenceScreen = (PreferenceScreen) m.invoke(mPreferenceManager, context, resId, rootPreferences);
} catch (Exception e) {
throw new RuntimeException(e);
}
return preferenceScreen;
}
public PreferenceScreen inflateFromResource(Context context, int resId, PreferenceScreen rootPreferences) {
PreferenceScreen preferenceScreen ;
try {
Method m = PreferenceManager.class.getDeclaredMethod("inflateFromResource", Context.class, int.class, PreferenceScreen.class);
m.setAccessible(true);
preferenceScreen = (PreferenceScreen) m.invoke(mPreferenceManager, context, resId, rootPreferences);
} catch (Exception e) {
throw new RuntimeException(e);
}
return preferenceScreen;
}
public PreferenceScreen inflateFromIntent(Intent queryIntent, PreferenceScreen rootPreferences) {
PreferenceScreen preferenceScreen ;
try {
Method m = PreferenceManager.class.getDeclaredMethod("inflateFromIntent", Intent.class, PreferenceScreen.class);
m.setAccessible(true);
preferenceScreen = (PreferenceScreen) m.invoke(mPreferenceManager, queryIntent, rootPreferences);
} catch (Exception e) {
throw new RuntimeException(e);
}
return preferenceScreen;
}
public PreferenceScreen inflateFromIntent(Intent queryIntent, PreferenceScreen rootPreferences) {
PreferenceScreen preferenceScreen ;
try {
Method m = PreferenceManager.class.getDeclaredMethod("inflateFromIntent", Intent.class, PreferenceScreen.class);
m.setAccessible(true);
preferenceScreen = (PreferenceScreen) m.invoke(mPreferenceManager, queryIntent, rootPreferences);
} catch (Exception e) {
throw new RuntimeException(e);
}
return preferenceScreen;
}
protected abstract void onInitPreferences(PreferenceScreen preferenceScreen);
protected abstract void onStartNewFragment(String name);
protected abstract void onInitPreferences(PreferenceScreen preferenceScreen);
protected abstract void onStartNewFragment(String name);
}

View File

@ -38,254 +38,254 @@ import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.view.UpdateView;
public class SearchFragment extends SubsonicFragment implements SectionAdapter.OnItemClickedListener<Serializable> {
private static final String TAG = SearchFragment.class.getSimpleName();
private static final String TAG = SearchFragment.class.getSimpleName();
private static final int MAX_ARTISTS = 20;
private static final int MAX_ALBUMS = 20;
private static final int MAX_SONGS = 50;
private static final int MIN_CLOSENESS = 1;
private static final int MAX_ARTISTS = 20;
private static final int MAX_ALBUMS = 20;
private static final int MAX_SONGS = 50;
private static final int MIN_CLOSENESS = 1;
protected RecyclerView recyclerView;
protected SearchAdapter adapter;
protected boolean largeAlbums = false;
protected RecyclerView recyclerView;
protected SearchAdapter adapter;
protected boolean largeAlbums = false;
private SearchResult searchResult;
private boolean skipSearch = false;
private String currentQuery;
private SearchResult searchResult;
private boolean skipSearch = false;
private String currentQuery;
public SearchFragment() {
super();
alwaysStartFullscreen = true;
}
public SearchFragment() {
super();
alwaysStartFullscreen = true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null) {
searchResult = (SearchResult) savedInstanceState.getSerializable(Constants.FRAGMENT_LIST);
}
largeAlbums = Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_LARGE_ALBUM_ART, true);
}
if(savedInstanceState != null) {
searchResult = (SearchResult) savedInstanceState.getSerializable(Constants.FRAGMENT_LIST);
}
largeAlbums = Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_LARGE_ALBUM_ART, true);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(Constants.FRAGMENT_LIST, searchResult);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(Constants.FRAGMENT_LIST, searchResult);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
rootView = inflater.inflate(R.layout.abstract_recycler_fragment, container, false);
setTitle(R.string.search_title);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
rootView = inflater.inflate(R.layout.abstract_recycler_fragment, container, false);
setTitle(R.string.search_title);
refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_layout);
refreshLayout.setEnabled(false);
refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_layout);
refreshLayout.setEnabled(false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.fragment_recycler);
setupLayoutManager(recyclerView, largeAlbums);
recyclerView = (RecyclerView) rootView.findViewById(R.id.fragment_recycler);
setupLayoutManager(recyclerView, largeAlbums);
registerForContextMenu(recyclerView);
context.onNewIntent(context.getIntent());
registerForContextMenu(recyclerView);
context.onNewIntent(context.getIntent());
if(searchResult != null) {
skipSearch = true;
recyclerView.setAdapter(adapter = new SearchAdapter(context, searchResult, getImageLoader(), largeAlbums, this));
}
if(searchResult != null) {
skipSearch = true;
recyclerView.setAdapter(adapter = new SearchAdapter(context, searchResult, getImageLoader(), largeAlbums, this));
}
return rootView;
}
return rootView;
}
@Override
public void setIsOnlyVisible(boolean isOnlyVisible) {
boolean update = this.isOnlyVisible != isOnlyVisible;
super.setIsOnlyVisible(isOnlyVisible);
if(update && adapter != null) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if(layoutManager instanceof GridLayoutManager) {
((GridLayoutManager) layoutManager).setSpanCount(getRecyclerColumnCount());
}
}
}
@Override
public void setIsOnlyVisible(boolean isOnlyVisible) {
boolean update = this.isOnlyVisible != isOnlyVisible;
super.setIsOnlyVisible(isOnlyVisible);
if(update && adapter != null) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if(layoutManager instanceof GridLayoutManager) {
((GridLayoutManager) layoutManager).setSpanCount(getRecyclerColumnCount());
}
}
}
@Override
public GridLayoutManager.SpanSizeLookup getSpanSizeLookup(final GridLayoutManager gridLayoutManager) {
return new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int viewType = adapter.getItemViewType(position);
if(viewType == EntryGridAdapter.VIEW_TYPE_SONG || viewType == EntryGridAdapter.VIEW_TYPE_HEADER || viewType == ArtistAdapter.VIEW_TYPE_ARTIST) {
return gridLayoutManager.getSpanCount();
} else {
return 1;
}
}
};
}
@Override
public GridLayoutManager.SpanSizeLookup getSpanSizeLookup(final GridLayoutManager gridLayoutManager) {
return new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int viewType = adapter.getItemViewType(position);
if(viewType == EntryGridAdapter.VIEW_TYPE_SONG || viewType == EntryGridAdapter.VIEW_TYPE_HEADER || viewType == ArtistAdapter.VIEW_TYPE_ARTIST) {
return gridLayoutManager.getSpanCount();
} else {
return 1;
}
}
};
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.search, menu);
onFinishSetupOptionsMenu(menu);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.search, menu);
onFinishSetupOptionsMenu(menu);
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Serializable> updateView, Serializable item) {
onCreateContextMenuSupport(menu, menuInflater, updateView, item);
if(item instanceof MusicDirectory.Entry && !Util.isOffline(context)) {
menu.removeItem(R.id.song_menu_remove_playlist);
}
recreateContextMenu(menu);
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Serializable> updateView, Serializable item) {
onCreateContextMenuSupport(menu, menuInflater, updateView, item);
if(item instanceof MusicDirectory.Entry && !Util.isOffline(context)) {
menu.removeItem(R.id.song_menu_remove_playlist);
}
recreateContextMenu(menu);
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Serializable> updateView, Serializable item) {
return onContextItemSelected(menuItem, item);
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Serializable> updateView, Serializable item) {
return onContextItemSelected(menuItem, item);
}
@Override
public void refresh(boolean refresh) {
context.onNewIntent(context.getIntent());
}
@Override
public void refresh(boolean refresh) {
context.onNewIntent(context.getIntent());
}
@Override
public void onItemClicked(UpdateView<Serializable> updateView, Serializable item) {
if (item instanceof Artist) {
onArtistSelected((Artist) item, false);
} else if (item instanceof MusicDirectory.Entry) {
MusicDirectory.Entry entry = (MusicDirectory.Entry) item;
if (entry.isDirectory()) {
onAlbumSelected(entry, false);
} else {
onSongSelected(entry, false, true, true, false);
}
}
}
@Override
public void onItemClicked(UpdateView<Serializable> updateView, Serializable item) {
if (item instanceof Artist) {
onArtistSelected((Artist) item, false);
} else if (item instanceof MusicDirectory.Entry) {
MusicDirectory.Entry entry = (MusicDirectory.Entry) item;
if (entry.isDirectory()) {
onAlbumSelected(entry, false);
} else {
onSongSelected(entry, false, true, true, false);
}
}
}
@Override
protected List<MusicDirectory.Entry> getSelectedEntries() {
List<Serializable> selected = adapter.getSelected();
List<MusicDirectory.Entry> selectedMedia = new ArrayList<>();
for(Serializable ser: selected) {
if(ser instanceof MusicDirectory.Entry) {
selectedMedia.add((MusicDirectory.Entry) ser);
}
}
@Override
protected List<MusicDirectory.Entry> getSelectedEntries() {
List<Serializable> selected = adapter.getSelected();
List<MusicDirectory.Entry> selectedMedia = new ArrayList<>();
for(Serializable ser: selected) {
if(ser instanceof MusicDirectory.Entry) {
selectedMedia.add((MusicDirectory.Entry) ser);
}
}
return selectedMedia;
}
return selectedMedia;
}
@Override
protected boolean isShowArtistEnabled() {
return true;
}
@Override
protected boolean isShowArtistEnabled() {
return true;
}
public void search(final String query, final boolean autoplay) {
if(skipSearch) {
skipSearch = false;
return;
}
currentQuery = query;
public void search(final String query, final boolean autoplay) {
if(skipSearch) {
skipSearch = false;
return;
}
currentQuery = query;
BackgroundTask<SearchResult> task = new TabBackgroundTask<SearchResult>(this) {
@Override
protected SearchResult doInBackground() throws Throwable {
SearchCritera criteria = new SearchCritera(query, MAX_ARTISTS, MAX_ALBUMS, MAX_SONGS);
MusicService service = MusicServiceFactory.getMusicService(context);
return service.search(criteria, context, this);
}
BackgroundTask<SearchResult> task = new TabBackgroundTask<SearchResult>(this) {
@Override
protected SearchResult doInBackground() throws Throwable {
SearchCritera criteria = new SearchCritera(query, MAX_ARTISTS, MAX_ALBUMS, MAX_SONGS);
MusicService service = MusicServiceFactory.getMusicService(context);
return service.search(criteria, context, this);
}
@Override
protected void done(SearchResult result) {
searchResult = result;
recyclerView.setAdapter(adapter = new SearchAdapter(context, searchResult, getImageLoader(), largeAlbums, SearchFragment.this));
if (autoplay) {
autoplay(query);
}
@Override
protected void done(SearchResult result) {
searchResult = result;
recyclerView.setAdapter(adapter = new SearchAdapter(context, searchResult, getImageLoader(), largeAlbums, SearchFragment.this));
if (autoplay) {
autoplay(query);
}
}
};
task.execute();
}
};
task.execute();
if(searchItem != null) {
MenuItemCompat.collapseActionView(searchItem);
}
}
if(searchItem != null) {
MenuItemCompat.collapseActionView(searchItem);
}
}
protected String getCurrentQuery() {
return currentQuery;
}
protected String getCurrentQuery() {
return currentQuery;
}
private void onArtistSelected(Artist artist, boolean autoplay) {
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName());
if(autoplay) {
args.putBoolean(Constants.INTENT_EXTRA_NAME_AUTOPLAY, true);
}
args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true);
fragment.setArguments(args);
private void onArtistSelected(Artist artist, boolean autoplay) {
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName());
if(autoplay) {
args.putBoolean(Constants.INTENT_EXTRA_NAME_AUTOPLAY, true);
}
args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true);
fragment.setArguments(args);
replaceFragment(fragment);
}
replaceFragment(fragment);
}
private void onAlbumSelected(MusicDirectory.Entry album, boolean autoplay) {
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, album.getId());
args.putString(Constants.INTENT_EXTRA_NAME_NAME, album.getTitle());
if(autoplay) {
args.putBoolean(Constants.INTENT_EXTRA_NAME_AUTOPLAY, true);
}
fragment.setArguments(args);
private void onAlbumSelected(MusicDirectory.Entry album, boolean autoplay) {
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, album.getId());
args.putString(Constants.INTENT_EXTRA_NAME_NAME, album.getTitle());
if(autoplay) {
args.putBoolean(Constants.INTENT_EXTRA_NAME_AUTOPLAY, true);
}
fragment.setArguments(args);
replaceFragment(fragment);
}
replaceFragment(fragment);
}
private void onSongSelected(MusicDirectory.Entry song, boolean save, boolean append, boolean autoplay, boolean playNext) {
DownloadService downloadService = getDownloadService();
if (downloadService != null) {
if (!append) {
downloadService.clear();
}
downloadService.download(Arrays.asList(song), save, false, playNext, false);
if (autoplay) {
downloadService.play(downloadService.size() - 1);
}
private void onSongSelected(MusicDirectory.Entry song, boolean save, boolean append, boolean autoplay, boolean playNext) {
DownloadService downloadService = getDownloadService();
if (downloadService != null) {
if (!append) {
downloadService.clear();
}
downloadService.download(Arrays.asList(song), save, false, playNext, false);
if (autoplay) {
downloadService.play(downloadService.size() - 1);
}
Util.toast(context, getResources().getQuantityString(R.plurals.select_album_n_songs_added, 1, 1));
}
}
Util.toast(context, getResources().getQuantityString(R.plurals.select_album_n_songs_added, 1, 1));
}
}
private void autoplay(String query) {
query = query.toLowerCase();
private void autoplay(String query) {
query = query.toLowerCase();
Artist artist = null;
if(!searchResult.getArtists().isEmpty()) {
artist = searchResult.getArtists().get(0);
artist.setCloseness(Util.getStringDistance(artist.getName().toLowerCase(), query));
}
MusicDirectory.Entry album = null;
if(!searchResult.getAlbums().isEmpty()) {
album = searchResult.getAlbums().get(0);
album.setCloseness(Util.getStringDistance(album.getTitle().toLowerCase(), query));
}
MusicDirectory.Entry song = null;
if(!searchResult.getSongs().isEmpty()) {
song = searchResult.getSongs().get(0);
song.setCloseness(Util.getStringDistance(song.getTitle().toLowerCase(), query));
}
Artist artist = null;
if(!searchResult.getArtists().isEmpty()) {
artist = searchResult.getArtists().get(0);
artist.setCloseness(Util.getStringDistance(artist.getName().toLowerCase(), query));
}
MusicDirectory.Entry album = null;
if(!searchResult.getAlbums().isEmpty()) {
album = searchResult.getAlbums().get(0);
album.setCloseness(Util.getStringDistance(album.getTitle().toLowerCase(), query));
}
MusicDirectory.Entry song = null;
if(!searchResult.getSongs().isEmpty()) {
song = searchResult.getSongs().get(0);
song.setCloseness(Util.getStringDistance(song.getTitle().toLowerCase(), query));
}
if(artist != null && (artist.getCloseness() <= MIN_CLOSENESS ||
(album == null || artist.getCloseness() <= album.getCloseness()) &&
(song == null || artist.getCloseness() <= song.getCloseness()))) {
onArtistSelected(artist, true);
} else if(album != null && (album.getCloseness() <= MIN_CLOSENESS ||
song == null || album.getCloseness() <= song.getCloseness())) {
onAlbumSelected(album, true);
} else if(song != null) {
onSongSelected(song, false, false, true, false);
}
}
if(artist != null && (artist.getCloseness() <= MIN_CLOSENESS ||
(album == null || artist.getCloseness() <= album.getCloseness()) &&
(song == null || artist.getCloseness() <= song.getCloseness()))) {
onArtistSelected(artist, true);
} else if(album != null && (album.getCloseness() <= MIN_CLOSENESS ||
song == null || album.getCloseness() <= song.getCloseness())) {
onAlbumSelected(album, true);
} else if(song != null) {
onSongSelected(song, false, false, true, false);
}
}
}

View File

@ -29,224 +29,224 @@ import java.util.ArrayList;
import java.util.List;
public class SelectArtistFragment extends SelectRecyclerFragment<Serializable> implements ArtistAdapter.OnMusicFolderChanged {
private static final String TAG = SelectArtistFragment.class.getSimpleName();
private static final String TAG = SelectArtistFragment.class.getSimpleName();
private List<MusicFolder> musicFolders = null;
private List<Entry> entries;
private String groupId;
private String groupName;
private List<MusicFolder> musicFolders = null;
private List<Entry> entries;
private String groupId;
private String groupName;
public SelectArtistFragment() {
super();
}
public SelectArtistFragment() {
super();
}
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
if(bundle != null) {
musicFolders = (List<MusicFolder>) bundle.getSerializable(Constants.FRAGMENT_LIST2);
}
artist = true;
}
if(bundle != null) {
musicFolders = (List<MusicFolder>) bundle.getSerializable(Constants.FRAGMENT_LIST2);
}
artist = true;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(Constants.FRAGMENT_LIST2, (Serializable) musicFolders);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(Constants.FRAGMENT_LIST2, (Serializable) musicFolders);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
Bundle args = getArguments();
if(args != null) {
if(args.getBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, false)) {
groupId = args.getString(Constants.INTENT_EXTRA_NAME_ID);
groupName = args.getString(Constants.INTENT_EXTRA_NAME_NAME);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
Bundle args = getArguments();
if(args != null) {
if(args.getBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, false)) {
groupId = args.getString(Constants.INTENT_EXTRA_NAME_ID);
groupName = args.getString(Constants.INTENT_EXTRA_NAME_NAME);
if (groupName != null) {
setTitle(groupName);
context.invalidateOptionsMenu();
}
}
}
if (groupName != null) {
setTitle(groupName);
context.invalidateOptionsMenu();
}
}
}
super.onCreateView(inflater, container, bundle);
super.onCreateView(inflater, container, bundle);
return rootView;
}
return rootView;
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Serializable> updateView, Serializable item) {
onCreateContextMenuSupport(menu, menuInflater, updateView, item);
recreateContextMenu(menu);
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Serializable> updateView, Serializable item) {
onCreateContextMenuSupport(menu, menuInflater, updateView, item);
recreateContextMenu(menu);
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Serializable> updateView, Serializable item) {
return onContextItemSelected(menuItem, item);
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Serializable> updateView, Serializable item) {
return onContextItemSelected(menuItem, item);
}
@Override
public void onItemClicked(UpdateView<Serializable> updateView, Serializable item) {
SubsonicFragment fragment;
if(item instanceof Artist) {
Artist artist = (Artist) item;
@Override
public void onItemClicked(UpdateView<Serializable> updateView, Serializable item) {
SubsonicFragment fragment;
if(item instanceof Artist) {
Artist artist = (Artist) item;
if ((Util.isFirstLevelArtist(context) || Util.isOffline(context) || Util.isTagBrowsing(context)) || groupId != null) {
fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName());
if ((Util.isFirstLevelArtist(context) || Util.isOffline(context) || Util.isTagBrowsing(context)) || groupId != null) {
fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName());
if (!Util.isOffline(context)) {
args.putSerializable(Constants.INTENT_EXTRA_NAME_DIRECTORY, new Entry(artist));
}
args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true);
if (!Util.isOffline(context)) {
args.putSerializable(Constants.INTENT_EXTRA_NAME_DIRECTORY, new Entry(artist));
}
args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true);
fragment.setArguments(args);
} else {
fragment = new SelectArtistFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName());
args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true);
if (!Util.isOffline(context)) {
args.putSerializable(Constants.INTENT_EXTRA_NAME_DIRECTORY, new Entry(artist));
}
fragment.setArguments(args);
} else {
fragment = new SelectArtistFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName());
args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true);
if (!Util.isOffline(context)) {
args.putSerializable(Constants.INTENT_EXTRA_NAME_DIRECTORY, new Entry(artist));
}
fragment.setArguments(args);
}
fragment.setArguments(args);
}
replaceFragment(fragment);
} else {
Entry entry = (Entry) item;
replaceFragment(fragment);
} else {
Entry entry = (Entry) item;
onSongPress(entries, entry);
}
}
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
super.onCreateOptionsMenu(menu, menuInflater);
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
super.onCreateOptionsMenu(menu, menuInflater);
if(Util.isOffline(context) || Util.isTagBrowsing(context) || groupId != null) {
menu.removeItem(R.id.menu_first_level_artist);
} else {
if (Util.isFirstLevelArtist(context)) {
menu.findItem(R.id.menu_first_level_artist).setChecked(true);
}
}
}
if(Util.isOffline(context) || Util.isTagBrowsing(context) || groupId != null) {
menu.removeItem(R.id.menu_first_level_artist);
} else {
if (Util.isFirstLevelArtist(context)) {
menu.findItem(R.id.menu_first_level_artist).setChecked(true);
}
}
}
@Override
public int getOptionsMenu() {
return R.menu.select_artist;
}
@Override
public int getOptionsMenu() {
return R.menu.select_artist;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(super.onOptionsItemSelected(item)) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(super.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.menu_first_level_artist:
toggleFirstLevelArtist();
break;
}
switch (item.getItemId()) {
case R.id.menu_first_level_artist:
toggleFirstLevelArtist();
break;
}
return false;
}
return false;
}
@Override
public SectionAdapter getAdapter(List<Serializable> objects) {
return new ArtistAdapter(context, objects, musicFolders, this, this);
}
@Override
public SectionAdapter getAdapter(List<Serializable> objects) {
return new ArtistAdapter(context, objects, musicFolders, this, this);
}
@Override
public List<Serializable> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
List<Serializable> items;
if(groupId == null) {
if (!Util.isOffline(context) && !Util.isTagBrowsing(context)) {
musicFolders = musicService.getMusicFolders(refresh, context, listener);
@Override
public List<Serializable> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
List<Serializable> items;
if(groupId == null) {
if (!Util.isOffline(context) && !Util.isTagBrowsing(context)) {
musicFolders = musicService.getMusicFolders(refresh, context, listener);
// Hide folders option if there is only one
if (musicFolders.size() == 1) {
musicFolders = null;
Util.setSelectedMusicFolderId(context, null);
}
} else {
musicFolders = null;
}
String musicFolderId = Util.getSelectedMusicFolderId(context);
// Hide folders option if there is only one
if (musicFolders.size() == 1) {
musicFolders = null;
Util.setSelectedMusicFolderId(context, null);
}
} else {
musicFolders = null;
}
String musicFolderId = Util.getSelectedMusicFolderId(context);
Indexes indexes = musicService.getIndexes(musicFolderId, refresh, context, listener);
indexes.sortChildren(context);
items = new ArrayList<>(indexes.getShortcuts().size() + indexes.getArtists().size());
items.addAll(indexes.getShortcuts());
items.addAll(indexes.getArtists());
entries = indexes.getEntries();
items.addAll(entries);
} else {
List<Artist> artists = new ArrayList<>();
items = new ArrayList<>();
MusicDirectory dir = musicService.getMusicDirectory(groupId, groupName, refresh, context, listener);
for(Entry entry: dir.getChildren(true, false)) {
Artist artist = new Artist();
artist.setId(entry.getId());
artist.setName(entry.getTitle());
artists.add(artist);
}
Indexes indexes = musicService.getIndexes(musicFolderId, refresh, context, listener);
indexes.sortChildren(context);
items = new ArrayList<>(indexes.getShortcuts().size() + indexes.getArtists().size());
items.addAll(indexes.getShortcuts());
items.addAll(indexes.getArtists());
entries = indexes.getEntries();
items.addAll(entries);
} else {
List<Artist> artists = new ArrayList<>();
items = new ArrayList<>();
MusicDirectory dir = musicService.getMusicDirectory(groupId, groupName, refresh, context, listener);
for(Entry entry: dir.getChildren(true, false)) {
Artist artist = new Artist();
artist.setId(entry.getId());
artist.setName(entry.getTitle());
artists.add(artist);
}
Indexes indexes = new Indexes();
//indexes.setArtists = artists;
indexes.sortChildren(context);
items.addAll(indexes.getArtists());
Indexes indexes = new Indexes();
//indexes.setArtists = artists;
indexes.sortChildren(context);
items.addAll(indexes.getArtists());
entries = dir.getChildren(false, true);
for(Entry entry: entries) {
items.add(entry);
}
}
entries = dir.getChildren(false, true);
for(Entry entry: entries) {
items.add(entry);
}
}
return items;
}
return items;
}
@Override
public int getTitleResource() {
return groupId == null ? R.string.button_bar_browse : 0;
}
@Override
public int getTitleResource() {
return groupId == null ? R.string.button_bar_browse : 0;
}
@Override
public void setEmpty(boolean empty) {
super.setEmpty(empty);
@Override
public void setEmpty(boolean empty) {
super.setEmpty(empty);
if(empty && !Util.isOffline(context)) {
objects.clear();
recyclerView.setAdapter(new ArtistAdapter(context, objects, musicFolders, this, this));
recyclerView.setVisibility(View.VISIBLE);
if(empty && !Util.isOffline(context)) {
objects.clear();
recyclerView.setAdapter(new ArtistAdapter(context, objects, musicFolders, this, this));
recyclerView.setVisibility(View.VISIBLE);
View view = rootView.findViewById(R.id.tab_progress);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.height = 0;
params.weight = 5;
view.setLayoutParams(params);
}
}
View view = rootView.findViewById(R.id.tab_progress);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.height = 0;
params.weight = 5;
view.setLayoutParams(params);
}
}
private void toggleFirstLevelArtist() {
Util.toggleFirstLevelArtist(context);
context.invalidateOptionsMenu();
}
private void toggleFirstLevelArtist() {
Util.toggleFirstLevelArtist(context);
context.invalidateOptionsMenu();
}
@Override
public void onMusicFolderChanged(MusicFolder selectedFolder) {
String startMusicFolderId = Util.getSelectedMusicFolderId(context);
String musicFolderId = selectedFolder == null ? null : selectedFolder.getId();
@Override
public void onMusicFolderChanged(MusicFolder selectedFolder) {
String startMusicFolderId = Util.getSelectedMusicFolderId(context);
String musicFolderId = selectedFolder == null ? null : selectedFolder.getId();
if(!Util.equals(startMusicFolderId, musicFolderId)) {
Util.setSelectedMusicFolderId(context, musicFolderId);
context.invalidate();
}
}
if(!Util.equals(startMusicFolderId, musicFolderId)) {
Util.setSelectedMusicFolderId(context, musicFolderId);
context.invalidate();
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.fragments;
@ -32,46 +32,46 @@ import net.nullsum.audinaut.view.UpdateView;
import java.util.List;
public class SelectGenreFragment extends SelectRecyclerFragment<Genre> {
private static final String TAG = SelectGenreFragment.class.getSimpleName();
private static final String TAG = SelectGenreFragment.class.getSimpleName();
@Override
public int getOptionsMenu() {
return R.menu.empty;
}
@Override
public int getOptionsMenu() {
return R.menu.empty;
}
@Override
public SectionAdapter getAdapter(List<Genre> objs) {
return new GenreAdapter(context, objs, this);
}
@Override
public SectionAdapter getAdapter(List<Genre> objs) {
return new GenreAdapter(context, objs, this);
}
@Override
public List<Genre> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
return musicService.getGenres(refresh, context, listener);
}
@Override
public List<Genre> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
return musicService.getGenres(refresh, context, listener);
}
@Override
public int getTitleResource() {
return R.string.main_albums_genres;
}
@Override
public int getTitleResource() {
return R.string.main_albums_genres;
}
@Override
public void onItemClicked(UpdateView<Genre> updateView, Genre genre) {
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE, "genres");
args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 20);
args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0);
args.putString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_EXTRA, genre.getName());
fragment.setArguments(args);
@Override
public void onItemClicked(UpdateView<Genre> updateView, Genre genre) {
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE, "genres");
args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 20);
args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0);
args.putString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_EXTRA, genre.getName());
fragment.setArguments(args);
replaceFragment(fragment);
}
replaceFragment(fragment);
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Genre> updateView, Genre item) {}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Genre> updateView, Genre item) {}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Genre> updateView, Genre item) {
return false;
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Genre> updateView, Genre item) {
return false;
}
}

View File

@ -36,311 +36,311 @@ import java.util.Arrays;
import java.util.List;
public class SelectPlaylistFragment extends SelectRecyclerFragment<Playlist> {
private static final String TAG = SelectPlaylistFragment.class.getSimpleName();
private static final String TAG = SelectPlaylistFragment.class.getSimpleName();
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
if (Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_LARGE_ALBUM_ART, true)) {
largeAlbums = true;
}
}
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
if (Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_LARGE_ALBUM_ART, true)) {
largeAlbums = true;
}
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Playlist> updateView, Playlist playlist) {
if (Util.isOffline(context)) {
menuInflater.inflate(R.menu.select_playlist_context_offline, menu);
}
else {
menuInflater.inflate(R.menu.select_playlist_context, menu);
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Playlist> updateView, Playlist playlist) {
if (Util.isOffline(context)) {
menuInflater.inflate(R.menu.select_playlist_context_offline, menu);
}
else {
menuInflater.inflate(R.menu.select_playlist_context, menu);
if(playlist.getPublic() != null && playlist.getPublic() == true && playlist.getId().indexOf(".m3u") == -1 && !UserUtil.getCurrentUsername(context).equals(playlist.getOwner())) {
menu.removeItem(R.id.playlist_update_info);
menu.removeItem(R.id.playlist_menu_delete);
}
}
if(playlist.getPublic() != null && playlist.getPublic() == true && playlist.getId().indexOf(".m3u") == -1 && !UserUtil.getCurrentUsername(context).equals(playlist.getOwner())) {
menu.removeItem(R.id.playlist_update_info);
menu.removeItem(R.id.playlist_menu_delete);
}
}
recreateContextMenu(menu);
}
recreateContextMenu(menu);
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Playlist> updateView, Playlist playlist) {
SubsonicFragment fragment;
Bundle args;
FragmentTransaction trans;
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Playlist> updateView, Playlist playlist) {
SubsonicFragment fragment;
Bundle args;
FragmentTransaction trans;
switch (menuItem.getItemId()) {
case R.id.playlist_menu_download:
downloadPlaylist(playlist.getId(), playlist.getName(), false, true, false, false, true);
break;
case R.id.playlist_menu_play_now:
fragment = new SelectDirectoryFragment();
args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_PLAYLIST_ID, playlist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_PLAYLIST_NAME, playlist.getName());
args.putBoolean(Constants.INTENT_EXTRA_NAME_AUTOPLAY, true);
fragment.setArguments(args);
switch (menuItem.getItemId()) {
case R.id.playlist_menu_download:
downloadPlaylist(playlist.getId(), playlist.getName(), false, true, false, false, true);
break;
case R.id.playlist_menu_play_now:
fragment = new SelectDirectoryFragment();
args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_PLAYLIST_ID, playlist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_PLAYLIST_NAME, playlist.getName());
args.putBoolean(Constants.INTENT_EXTRA_NAME_AUTOPLAY, true);
fragment.setArguments(args);
replaceFragment(fragment);
break;
case R.id.playlist_menu_play_shuffled:
fragment = new SelectDirectoryFragment();
args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_PLAYLIST_ID, playlist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_PLAYLIST_NAME, playlist.getName());
args.putBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE, true);
args.putBoolean(Constants.INTENT_EXTRA_NAME_AUTOPLAY, true);
fragment.setArguments(args);
replaceFragment(fragment);
break;
case R.id.playlist_menu_play_shuffled:
fragment = new SelectDirectoryFragment();
args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_PLAYLIST_ID, playlist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_PLAYLIST_NAME, playlist.getName());
args.putBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE, true);
args.putBoolean(Constants.INTENT_EXTRA_NAME_AUTOPLAY, true);
fragment.setArguments(args);
replaceFragment(fragment);
break;
case R.id.playlist_menu_delete:
deletePlaylist(playlist);
break;
case R.id.playlist_info:
displayPlaylistInfo(playlist);
break;
case R.id.playlist_update_info:
updatePlaylistInfo(playlist);
break;
}
replaceFragment(fragment);
break;
case R.id.playlist_menu_delete:
deletePlaylist(playlist);
break;
case R.id.playlist_info:
displayPlaylistInfo(playlist);
break;
case R.id.playlist_update_info:
updatePlaylistInfo(playlist);
break;
}
return false;
}
return false;
}
@Override
public int getOptionsMenu() {
return R.menu.abstract_top_menu;
}
@Override
public int getOptionsMenu() {
return R.menu.abstract_top_menu;
}
@Override
public SectionAdapter<Playlist> getAdapter(List<Playlist> playlists) {
List<Playlist> mine = new ArrayList<>();
@Override
public SectionAdapter<Playlist> getAdapter(List<Playlist> playlists) {
List<Playlist> mine = new ArrayList<>();
String currentUsername = UserUtil.getCurrentUsername(context);
for(Playlist playlist: playlists) {
if(playlist.getOwner() == null || playlist.getOwner().equals(currentUsername)) {
mine.add(playlist);
}
}
String currentUsername = UserUtil.getCurrentUsername(context);
for(Playlist playlist: playlists) {
if(playlist.getOwner() == null || playlist.getOwner().equals(currentUsername)) {
mine.add(playlist);
}
}
return new PlaylistAdapter(context, playlists, getImageLoader(), largeAlbums, this);
}
}
@Override
public List<Playlist> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
List<Playlist> playlists = musicService.getPlaylists(refresh, context, listener);
if(!Util.isOffline(context) && refresh) {
new CacheCleaner(context, getDownloadService()).cleanPlaylists(playlists);
}
return playlists;
}
@Override
public List<Playlist> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
List<Playlist> playlists = musicService.getPlaylists(refresh, context, listener);
if(!Util.isOffline(context) && refresh) {
new CacheCleaner(context, getDownloadService()).cleanPlaylists(playlists);
}
return playlists;
}
@Override
public int getTitleResource() {
return R.string.playlist_label;
}
@Override
public int getTitleResource() {
return R.string.playlist_label;
}
@Override
public void onItemClicked(UpdateView<Playlist> updateView, Playlist playlist) {
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_PLAYLIST_ID, playlist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_PLAYLIST_NAME, playlist.getName());
if((playlist.getOwner() != null && playlist.getOwner().equals(UserUtil.getCurrentUsername(context)) || playlist.getId().indexOf(".m3u") != -1)) {
args.putBoolean(Constants.INTENT_EXTRA_NAME_PLAYLIST_OWNER, true);
}
fragment.setArguments(args);
@Override
public void onItemClicked(UpdateView<Playlist> updateView, Playlist playlist) {
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_PLAYLIST_ID, playlist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_PLAYLIST_NAME, playlist.getName());
if((playlist.getOwner() != null && playlist.getOwner().equals(UserUtil.getCurrentUsername(context)) || playlist.getId().indexOf(".m3u") != -1)) {
args.putBoolean(Constants.INTENT_EXTRA_NAME_PLAYLIST_OWNER, true);
}
fragment.setArguments(args);
replaceFragment(fragment);
}
replaceFragment(fragment);
}
@Override
public void onFinishRefresh() {
Bundle args = getArguments();
if(args != null) {
String playlistId = args.getString(Constants.INTENT_EXTRA_NAME_ID, null);
if (playlistId != null && objects != null) {
for (Playlist playlist : objects) {
if (playlistId.equals(playlist.getId())) {
onItemClicked(null, playlist);
break;
}
}
}
}
}
@Override
public void onFinishRefresh() {
Bundle args = getArguments();
if(args != null) {
String playlistId = args.getString(Constants.INTENT_EXTRA_NAME_ID, null);
if (playlistId != null && objects != null) {
for (Playlist playlist : objects) {
if (playlistId.equals(playlist.getId())) {
onItemClicked(null, playlist);
break;
}
}
}
}
}
private void deletePlaylist(final Playlist playlist) {
Util.confirmDialog(context, R.string.common_delete, playlist.getName(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new LoadingTask<Void>(context, false) {
@Override
protected Void doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(context);
musicService.deletePlaylist(playlist.getId(), context, null);
SyncUtil.removeSyncedPlaylist(context, playlist.getId());
return null;
}
private void deletePlaylist(final Playlist playlist) {
Util.confirmDialog(context, R.string.common_delete, playlist.getName(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new LoadingTask<Void>(context, false) {
@Override
protected Void doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(context);
musicService.deletePlaylist(playlist.getId(), context, null);
SyncUtil.removeSyncedPlaylist(context, playlist.getId());
return null;
}
@Override
protected void done(Void result) {
adapter.removeItem(playlist);
Util.toast(context, context.getResources().getString(R.string.menu_deleted_playlist, playlist.getName()));
}
@Override
protected void done(Void result) {
adapter.removeItem(playlist);
Util.toast(context, context.getResources().getString(R.string.menu_deleted_playlist, playlist.getName()));
}
@Override
protected void error(Throwable error) {
String msg;
if (error instanceof OfflineException) {
msg = getErrorMessage(error);
} else {
msg = context.getResources().getString(R.string.menu_deleted_playlist_error, playlist.getName()) + " " + getErrorMessage(error);
}
@Override
protected void error(Throwable error) {
String msg;
if (error instanceof OfflineException) {
msg = getErrorMessage(error);
} else {
msg = context.getResources().getString(R.string.menu_deleted_playlist_error, playlist.getName()) + " " + getErrorMessage(error);
}
Util.toast(context, msg, false);
}
}.execute();
}
});
}
Util.toast(context, msg, false);
}
}.execute();
}
});
}
private void displayPlaylistInfo(final Playlist playlist) {
List<Integer> headers = new ArrayList<>();
List<String> details = new ArrayList<>();
private void displayPlaylistInfo(final Playlist playlist) {
List<Integer> headers = new ArrayList<>();
List<String> details = new ArrayList<>();
headers.add(R.string.details_title);
details.add(playlist.getName());
headers.add(R.string.details_title);
details.add(playlist.getName());
if(playlist.getOwner() != null) {
headers.add(R.string.details_owner);
details.add(playlist.getOwner());
}
if(playlist.getOwner() != null) {
headers.add(R.string.details_owner);
details.add(playlist.getOwner());
}
if(playlist.getComment() != null) {
headers.add(R.string.details_comments);
details.add(playlist.getComment());
}
if(playlist.getComment() != null) {
headers.add(R.string.details_comments);
details.add(playlist.getComment());
}
headers.add(R.string.details_song_count);
details.add(playlist.getSongCount());
headers.add(R.string.details_song_count);
details.add(playlist.getSongCount());
if(playlist.getDuration() != null) {
headers.add(R.string.details_length);
details.add(Util.formatDuration(playlist.getDuration()));
}
if(playlist.getDuration() != null) {
headers.add(R.string.details_length);
details.add(Util.formatDuration(playlist.getDuration()));
}
if(playlist.getPublic() != null) {
headers.add(R.string.details_public);
details.add(Util.formatBoolean(context, playlist.getPublic()));
}
if(playlist.getPublic() != null) {
headers.add(R.string.details_public);
details.add(Util.formatBoolean(context, playlist.getPublic()));
}
if(playlist.getCreated() != null) {
headers.add(R.string.details_created);
if(playlist.getCreated() != null) {
headers.add(R.string.details_created);
DateFormat dateFormat = DateFormat.getDateInstance();
details.add(dateFormat.format(playlist.getCreated()));
}
if(playlist.getChanged() != null) {
headers.add(R.string.details_updated);
details.add(dateFormat.format(playlist.getCreated()));
}
if(playlist.getChanged() != null) {
headers.add(R.string.details_updated);
DateFormat dateFormat = DateFormat.getDateInstance();
details.add(dateFormat.format(playlist.getChanged()));
}
details.add(dateFormat.format(playlist.getChanged()));
}
Util.showDetailsDialog(context, R.string.details_title_playlist, headers, details);
}
Util.showDetailsDialog(context, R.string.details_title_playlist, headers, details);
}
private void updatePlaylistInfo(final Playlist playlist) {
View dialogView = context.getLayoutInflater().inflate(R.layout.update_playlist, null);
final EditText nameBox = (EditText)dialogView.findViewById(R.id.get_playlist_name);
final EditText commentBox = (EditText)dialogView.findViewById(R.id.get_playlist_comment);
final CheckBox publicBox = (CheckBox)dialogView.findViewById(R.id.get_playlist_public);
private void updatePlaylistInfo(final Playlist playlist) {
View dialogView = context.getLayoutInflater().inflate(R.layout.update_playlist, null);
final EditText nameBox = (EditText)dialogView.findViewById(R.id.get_playlist_name);
final EditText commentBox = (EditText)dialogView.findViewById(R.id.get_playlist_comment);
final CheckBox publicBox = (CheckBox)dialogView.findViewById(R.id.get_playlist_public);
nameBox.setText(playlist.getName());
commentBox.setText(playlist.getComment());
Boolean pub = playlist.getPublic();
if(pub == null) {
publicBox.setEnabled(false);
} else {
publicBox.setChecked(pub);
}
nameBox.setText(playlist.getName());
commentBox.setText(playlist.getComment());
Boolean pub = playlist.getPublic();
if(pub == null) {
publicBox.setEnabled(false);
} else {
publicBox.setChecked(pub);
}
new AlertDialog.Builder(context)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.playlist_update_info)
.setView(dialogView)
.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new LoadingTask<Void>(context, false) {
@Override
protected Void doInBackground() throws Throwable {
String name = nameBox.getText().toString();
String comment = commentBox.getText().toString();
boolean isPublic = publicBox.isChecked();
new AlertDialog.Builder(context)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.playlist_update_info)
.setView(dialogView)
.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new LoadingTask<Void>(context, false) {
@Override
protected Void doInBackground() throws Throwable {
String name = nameBox.getText().toString();
String comment = commentBox.getText().toString();
boolean isPublic = publicBox.isChecked();
MusicService musicService = MusicServiceFactory.getMusicService(context);
musicService.updatePlaylist(playlist.getId(), name, comment, isPublic, context, null);
MusicService musicService = MusicServiceFactory.getMusicService(context);
musicService.updatePlaylist(playlist.getId(), name, comment, isPublic, context, null);
playlist.setName(name);
playlist.setComment(comment);
playlist.setPublic(isPublic);
playlist.setName(name);
playlist.setComment(comment);
playlist.setPublic(isPublic);
return null;
}
return null;
}
@Override
protected void done(Void result) {
Util.toast(context, context.getResources().getString(R.string.playlist_updated_info, playlist.getName()));
}
@Override
protected void done(Void result) {
Util.toast(context, context.getResources().getString(R.string.playlist_updated_info, playlist.getName()));
}
@Override
protected void error(Throwable error) {
String msg;
if (error instanceof OfflineException) {
msg = getErrorMessage(error);
} else {
msg = context.getResources().getString(R.string.playlist_updated_info_error, playlist.getName()) + " " + getErrorMessage(error);
}
@Override
protected void error(Throwable error) {
String msg;
if (error instanceof OfflineException) {
msg = getErrorMessage(error);
} else {
msg = context.getResources().getString(R.string.playlist_updated_info_error, playlist.getName()) + " " + getErrorMessage(error);
}
Util.toast(context, msg, false);
}
}.execute();
}
Util.toast(context, msg, false);
}
}.execute();
}
})
.setNegativeButton(R.string.common_cancel, null)
.show();
}
})
.setNegativeButton(R.string.common_cancel, null)
.show();
}
private void syncPlaylist(Playlist playlist) {
SyncUtil.addSyncedPlaylist(context, playlist.getId());
downloadPlaylist(playlist.getId(), playlist.getName(), true, true, false, false, true);
}
private void syncPlaylist(Playlist playlist) {
SyncUtil.addSyncedPlaylist(context, playlist.getId());
downloadPlaylist(playlist.getId(), playlist.getName(), true, true, false, false, true);
}
private void stopSyncPlaylist(final Playlist playlist) {
SyncUtil.removeSyncedPlaylist(context, playlist.getId());
private void stopSyncPlaylist(final Playlist playlist) {
SyncUtil.removeSyncedPlaylist(context, playlist.getId());
new LoadingTask<Void>(context, false) {
@Override
protected Void doInBackground() throws Throwable {
// Unpin all of the songs in playlist
MusicService musicService = MusicServiceFactory.getMusicService(context);
MusicDirectory root = musicService.getPlaylist(true, playlist.getId(), playlist.getName(), context, this);
for(MusicDirectory.Entry entry: root.getChildren()) {
DownloadFile file = new DownloadFile(context, entry, false);
file.unpin();
}
new LoadingTask<Void>(context, false) {
@Override
protected Void doInBackground() throws Throwable {
// Unpin all of the songs in playlist
MusicService musicService = MusicServiceFactory.getMusicService(context);
MusicDirectory root = musicService.getPlaylist(true, playlist.getId(), playlist.getName(), context, this);
for(MusicDirectory.Entry entry: root.getChildren()) {
DownloadFile file = new DownloadFile(context, entry, false);
file.unpin();
}
return null;
}
return null;
}
@Override
protected void done(Void result) {
@Override
protected void done(Void result) {
}
}.execute();
}
}
}.execute();
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.fragments;
@ -46,174 +46,174 @@ import net.nullsum.audinaut.util.TabBackgroundTask;
import net.nullsum.audinaut.view.FastScroller;
public abstract class SelectRecyclerFragment<T> extends SubsonicFragment implements SectionAdapter.OnItemClickedListener<T> {
private static final String TAG = SelectRecyclerFragment.class.getSimpleName();
protected RecyclerView recyclerView;
protected FastScroller fastScroller;
protected SectionAdapter<T> adapter;
protected UpdateTask currentTask;
protected List<T> objects;
protected boolean serialize = true;
protected boolean largeAlbums = false;
protected boolean pullToRefresh = true;
protected boolean backgroundUpdate = true;
private static final String TAG = SelectRecyclerFragment.class.getSimpleName();
protected RecyclerView recyclerView;
protected FastScroller fastScroller;
protected SectionAdapter<T> adapter;
protected UpdateTask currentTask;
protected List<T> objects;
protected boolean serialize = true;
protected boolean largeAlbums = false;
protected boolean pullToRefresh = true;
protected boolean backgroundUpdate = true;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
if(bundle != null && serialize) {
objects = (List<T>) bundle.getSerializable(Constants.FRAGMENT_LIST);
}
}
if(bundle != null && serialize) {
objects = (List<T>) bundle.getSerializable(Constants.FRAGMENT_LIST);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(serialize) {
outState.putSerializable(Constants.FRAGMENT_LIST, (Serializable) objects);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(serialize) {
outState.putSerializable(Constants.FRAGMENT_LIST, (Serializable) objects);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
rootView = inflater.inflate(R.layout.abstract_recycler_fragment, container, false);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
rootView = inflater.inflate(R.layout.abstract_recycler_fragment, container, false);
refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_layout);
refreshLayout.setOnRefreshListener(this);
refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_layout);
refreshLayout.setOnRefreshListener(this);
recyclerView = (RecyclerView) rootView.findViewById(R.id.fragment_recycler);
fastScroller = (FastScroller) rootView.findViewById(R.id.fragment_fast_scroller);
setupLayoutManager();
recyclerView = (RecyclerView) rootView.findViewById(R.id.fragment_recycler);
fastScroller = (FastScroller) rootView.findViewById(R.id.fragment_fast_scroller);
setupLayoutManager();
if(pullToRefresh) {
setupScrollList(recyclerView);
} else {
refreshLayout.setEnabled(false);
}
if(pullToRefresh) {
setupScrollList(recyclerView);
} else {
refreshLayout.setEnabled(false);
}
if(objects == null) {
refresh(false);
} else {
recyclerView.setAdapter(adapter = getAdapter(objects));
}
if(objects == null) {
refresh(false);
} else {
recyclerView.setAdapter(adapter = getAdapter(objects));
}
return rootView;
}
return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
if(!primaryFragment) {
return;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
if(!primaryFragment) {
return;
}
menuInflater.inflate(getOptionsMenu(), menu);
onFinishSetupOptionsMenu(menu);
}
menuInflater.inflate(getOptionsMenu(), menu);
onFinishSetupOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public void setIsOnlyVisible(boolean isOnlyVisible) {
boolean update = this.isOnlyVisible != isOnlyVisible;
super.setIsOnlyVisible(isOnlyVisible);
if(update && adapter != null) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if(layoutManager instanceof GridLayoutManager) {
((GridLayoutManager) layoutManager).setSpanCount(getRecyclerColumnCount());
}
}
}
@Override
public void setIsOnlyVisible(boolean isOnlyVisible) {
boolean update = this.isOnlyVisible != isOnlyVisible;
super.setIsOnlyVisible(isOnlyVisible);
if(update && adapter != null) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if(layoutManager instanceof GridLayoutManager) {
((GridLayoutManager) layoutManager).setSpanCount(getRecyclerColumnCount());
}
}
}
@Override
protected void refresh(final boolean refresh) {
int titleRes = getTitleResource();
if(titleRes != 0) {
setTitle(getTitleResource());
}
if(backgroundUpdate) {
recyclerView.setVisibility(View.GONE);
}
@Override
protected void refresh(final boolean refresh) {
int titleRes = getTitleResource();
if(titleRes != 0) {
setTitle(getTitleResource());
}
if(backgroundUpdate) {
recyclerView.setVisibility(View.GONE);
}
// Cancel current running task before starting another one
if(currentTask != null) {
currentTask.cancel();
}
// Cancel current running task before starting another one
if(currentTask != null) {
currentTask.cancel();
}
currentTask = new UpdateTask(this, refresh);
currentTask = new UpdateTask(this, refresh);
if(backgroundUpdate) {
currentTask.execute();
} else {
objects = new ArrayList<T>();
if(backgroundUpdate) {
currentTask.execute();
} else {
objects = new ArrayList<T>();
try {
objects = getObjects(null, refresh, null);
} catch (Exception x) {
Log.e(TAG, "Failed to load", x);
}
try {
objects = getObjects(null, refresh, null);
} catch (Exception x) {
Log.e(TAG, "Failed to load", x);
}
currentTask.done(objects);
}
}
currentTask.done(objects);
}
}
public SectionAdapter getCurrentAdapter() {
return adapter;
}
public SectionAdapter getCurrentAdapter() {
return adapter;
}
private void setupLayoutManager() {
setupLayoutManager(recyclerView, largeAlbums);
}
private void setupLayoutManager() {
setupLayoutManager(recyclerView, largeAlbums);
}
public abstract int getOptionsMenu();
public abstract SectionAdapter<T> getAdapter(List<T> objs);
public abstract List<T> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception;
public abstract int getTitleResource();
public abstract int getOptionsMenu();
public abstract SectionAdapter<T> getAdapter(List<T> objs);
public abstract List<T> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception;
public abstract int getTitleResource();
public void onFinishRefresh() {
public void onFinishRefresh() {
}
}
private class UpdateTask extends TabBackgroundTask<List<T>> {
private boolean refresh;
private class UpdateTask extends TabBackgroundTask<List<T>> {
private boolean refresh;
public UpdateTask(SubsonicFragment fragment, boolean refresh) {
super(fragment);
this.refresh = refresh;
}
public UpdateTask(SubsonicFragment fragment, boolean refresh) {
super(fragment);
this.refresh = refresh;
}
@Override
public List<T> doInBackground() throws Exception {
MusicService musicService = MusicServiceFactory.getMusicService(context);
@Override
public List<T> doInBackground() throws Exception {
MusicService musicService = MusicServiceFactory.getMusicService(context);
objects = new ArrayList<T>();
objects = new ArrayList<T>();
try {
objects = getObjects(musicService, refresh, this);
} catch (Exception x) {
Log.e(TAG, "Failed to load", x);
}
try {
objects = getObjects(musicService, refresh, this);
} catch (Exception x) {
Log.e(TAG, "Failed to load", x);
}
return objects;
}
return objects;
}
@Override
public void done(List<T> result) {
if (result != null && !result.isEmpty()) {
recyclerView.setAdapter(adapter = getAdapter(result));
if(!fastScroller.isAttached()) {
fastScroller.attachRecyclerView(recyclerView);
}
@Override
public void done(List<T> result) {
if (result != null && !result.isEmpty()) {
recyclerView.setAdapter(adapter = getAdapter(result));
if(!fastScroller.isAttached()) {
fastScroller.attachRecyclerView(recyclerView);
}
onFinishRefresh();
recyclerView.setVisibility(View.VISIBLE);
} else {
setEmpty(true);
}
onFinishRefresh();
recyclerView.setVisibility(View.VISIBLE);
} else {
setEmpty(true);
}
currentTask = null;
}
}
currentTask = null;
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.fragments;
@ -33,56 +33,56 @@ import net.nullsum.audinaut.view.UpdateView;
public class SelectYearFragment extends SelectRecyclerFragment<String> {
public SelectYearFragment() {
super();
pullToRefresh = false;
serialize = false;
backgroundUpdate = false;
}
public SelectYearFragment() {
super();
pullToRefresh = false;
serialize = false;
backgroundUpdate = false;
}
@Override
public int getOptionsMenu() {
return R.menu.empty;
}
@Override
public int getOptionsMenu() {
return R.menu.empty;
}
@Override
public SectionAdapter getAdapter(List<String> objs) {
return new BasicListAdapter(context, objs, this);
}
@Override
public SectionAdapter getAdapter(List<String> objs) {
return new BasicListAdapter(context, objs, this);
}
@Override
public List<String> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
List<String> decades = new ArrayList<>();
for(int i = 2010; i >= 1800; i -= 10) {
decades.add(String.valueOf(i));
}
@Override
public List<String> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
List<String> decades = new ArrayList<>();
for(int i = 2010; i >= 1800; i -= 10) {
decades.add(String.valueOf(i));
}
return decades;
}
return decades;
}
@Override
public int getTitleResource() {
return R.string.main_albums_year;
}
@Override
public int getTitleResource() {
return R.string.main_albums_year;
}
@Override
public void onItemClicked(UpdateView<String> updateView, String decade) {
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE, "years");
args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 20);
args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0);
args.putString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_EXTRA, decade);
fragment.setArguments(args);
@Override
public void onItemClicked(UpdateView<String> updateView, String decade) {
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE, "years");
args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 20);
args.putInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET, 0);
args.putString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_EXTRA, decade);
fragment.setArguments(args);
replaceFragment(fragment);
}
replaceFragment(fragment);
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<String> updateView, String item) {}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<String> updateView, String item) {}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<String> updateView, String item) {
return false;
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<String> updateView, String item) {
return false;
}
}

View File

@ -46,164 +46,164 @@ import net.nullsum.audinaut.util.Util;
* @author Sindre Mehus
*/
public class AudinautSearchProvider extends ContentProvider {
private static final String TAG = AudinautSearchProvider.class.getSimpleName();
private static final String TAG = AudinautSearchProvider.class.getSimpleName();
private static final String RESOURCE_PREFIX = "android.resource://net.nullsum.audinaut/";
private static final String[] COLUMNS = {"_id",
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_TEXT_2,
SearchManager.SUGGEST_COLUMN_INTENT_DATA,
SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA,
SearchManager.SUGGEST_COLUMN_ICON_1};
private static final String RESOURCE_PREFIX = "android.resource://net.nullsum.audinaut/";
private static final String[] COLUMNS = {"_id",
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_TEXT_2,
SearchManager.SUGGEST_COLUMN_INTENT_DATA,
SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA,
SearchManager.SUGGEST_COLUMN_ICON_1};
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
if(selectionArgs[0].isEmpty()) {
return null;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
if(selectionArgs[0].isEmpty()) {
return null;
}
String query = selectionArgs[0] + "*";
SearchResult searchResult = search(query);
return createCursor(selectionArgs[0], searchResult);
}
String query = selectionArgs[0] + "*";
SearchResult searchResult = search(query);
return createCursor(selectionArgs[0], searchResult);
}
private SearchResult search(String query) {
MusicService musicService = MusicServiceFactory.getMusicService(getContext());
if (musicService == null) {
return null;
}
private SearchResult search(String query) {
MusicService musicService = MusicServiceFactory.getMusicService(getContext());
if (musicService == null) {
return null;
}
try {
return musicService.search(new SearchCritera(query, 5, 10, 10), getContext(), null);
} catch (Exception e) {
return null;
}
}
try {
return musicService.search(new SearchCritera(query, 5, 10, 10), getContext(), null);
} catch (Exception e) {
return null;
}
}
private Cursor createCursor(String query, SearchResult searchResult) {
MatrixCursor cursor = new MatrixCursor(COLUMNS);
if (searchResult == null) {
return cursor;
}
private Cursor createCursor(String query, SearchResult searchResult) {
MatrixCursor cursor = new MatrixCursor(COLUMNS);
if (searchResult == null) {
return cursor;
}
// Add all results into one pot
List<Object> results = new ArrayList<Object>();
results.addAll(searchResult.getArtists());
results.addAll(searchResult.getAlbums());
results.addAll(searchResult.getSongs());
// Add all results into one pot
List<Object> results = new ArrayList<Object>();
results.addAll(searchResult.getArtists());
results.addAll(searchResult.getAlbums());
results.addAll(searchResult.getSongs());
// For each, calculate its string distance to the query
for(Object obj: results) {
if(obj instanceof Artist) {
Artist artist = (Artist) obj;
artist.setCloseness(Util.getStringDistance(query, artist.getName()));
} else {
MusicDirectory.Entry entry = (MusicDirectory.Entry) obj;
entry.setCloseness(Util.getStringDistance(query, entry.getTitle()));
}
}
// For each, calculate its string distance to the query
for(Object obj: results) {
if(obj instanceof Artist) {
Artist artist = (Artist) obj;
artist.setCloseness(Util.getStringDistance(query, artist.getName()));
} else {
MusicDirectory.Entry entry = (MusicDirectory.Entry) obj;
entry.setCloseness(Util.getStringDistance(query, entry.getTitle()));
}
}
// Sort based on the closeness paramater
Collections.sort(results, new Comparator<Object>() {
@Override
public int compare(Object lhs, Object rhs) {
// Get the closeness of the two objects
int left, right;
boolean leftArtist = lhs instanceof Artist;
boolean rightArtist = rhs instanceof Artist;
if (leftArtist) {
left = ((Artist) lhs).getCloseness();
} else {
left = ((MusicDirectory.Entry) lhs).getCloseness();
}
if (rightArtist) {
right = ((Artist) rhs).getCloseness();
} else {
right = ((MusicDirectory.Entry) rhs).getCloseness();
}
// Sort based on the closeness paramater
Collections.sort(results, new Comparator<Object>() {
@Override
public int compare(Object lhs, Object rhs) {
// Get the closeness of the two objects
int left, right;
boolean leftArtist = lhs instanceof Artist;
boolean rightArtist = rhs instanceof Artist;
if (leftArtist) {
left = ((Artist) lhs).getCloseness();
} else {
left = ((MusicDirectory.Entry) lhs).getCloseness();
}
if (rightArtist) {
right = ((Artist) rhs).getCloseness();
} else {
right = ((MusicDirectory.Entry) rhs).getCloseness();
}
if (left == right) {
if(leftArtist && rightArtist) {
return 0;
} else if(leftArtist) {
return -1;
} else if(rightArtist) {
return 1;
} else {
return 0;
}
} else if (left > right) {
return 1;
} else {
return -1;
}
}
});
if (left == right) {
if(leftArtist && rightArtist) {
return 0;
} else if(leftArtist) {
return -1;
} else if(rightArtist) {
return 1;
} else {
return 0;
}
} else if (left > right) {
return 1;
} else {
return -1;
}
}
});
// Done sorting, add results to cursor
for(Object obj: results) {
if(obj instanceof Artist) {
Artist artist = (Artist) obj;
String icon = RESOURCE_PREFIX + R.drawable.ic_action_artist;
cursor.addRow(new Object[]{artist.getId().hashCode(), artist.getName(), null, "ar-" + artist.getId(), artist.getName(), icon});
} else {
MusicDirectory.Entry entry = (MusicDirectory.Entry) obj;
// Done sorting, add results to cursor
for(Object obj: results) {
if(obj instanceof Artist) {
Artist artist = (Artist) obj;
String icon = RESOURCE_PREFIX + R.drawable.ic_action_artist;
cursor.addRow(new Object[]{artist.getId().hashCode(), artist.getName(), null, "ar-" + artist.getId(), artist.getName(), icon});
} else {
MusicDirectory.Entry entry = (MusicDirectory.Entry) obj;
if(entry.isDirectory()) {
String icon = RESOURCE_PREFIX + R.drawable.ic_action_album;
cursor.addRow(new Object[]{entry.getId().hashCode(), entry.getTitle(), entry.getArtist(), entry.getId(), entry.getTitle(), icon});
} else {
String icon = RESOURCE_PREFIX + R.drawable.ic_action_song;
String id;
if(Util.isTagBrowsing(getContext())) {
id = entry.getAlbumId();
} else {
id = entry.getParent();
}
if(entry.isDirectory()) {
String icon = RESOURCE_PREFIX + R.drawable.ic_action_album;
cursor.addRow(new Object[]{entry.getId().hashCode(), entry.getTitle(), entry.getArtist(), entry.getId(), entry.getTitle(), icon});
} else {
String icon = RESOURCE_PREFIX + R.drawable.ic_action_song;
String id;
if(Util.isTagBrowsing(getContext())) {
id = entry.getAlbumId();
} else {
id = entry.getParent();
}
String artistDisplay;
if(entry.getArtist() == null) {
if(entry.getAlbum() != null) {
artistDisplay = entry.getAlbumDisplay();
} else {
artistDisplay = "";
}
} else if(entry.getAlbum() != null) {
artistDisplay = entry.getArtist() + " - " + entry.getAlbumDisplay();
} else {
artistDisplay = entry.getArtist();
}
String artistDisplay;
if(entry.getArtist() == null) {
if(entry.getAlbum() != null) {
artistDisplay = entry.getAlbumDisplay();
} else {
artistDisplay = "";
}
} else if(entry.getAlbum() != null) {
artistDisplay = entry.getArtist() + " - " + entry.getAlbumDisplay();
} else {
artistDisplay = entry.getArtist();
}
cursor.addRow(new Object[]{entry.getId().hashCode(), entry.getTitle(), artistDisplay, "so-" + id, entry.getTitle(), icon});
}
}
}
return cursor;
}
cursor.addRow(new Object[]{entry.getId().hashCode(), entry.getTitle(), artistDisplay, "so-" + id, entry.getTitle(), icon});
}
}
}
return cursor;
}
@Override
public boolean onCreate() {
return false;
}
@Override
public boolean onCreate() {
return false;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
return null;
}
@Override
public int delete(Uri uri, String s, String[] strings) {
return 0;
}
@Override
public int delete(Uri uri, String s, String[] strings) {
return 0;
}
@Override
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
return 0;
}
@Override
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
return 0;
}
}

View File

@ -21,8 +21,8 @@ package net.nullsum.audinaut.provider;
import net.nullsum.audinaut.R;
public class AudinautWidget4x1 extends AudinautWidgetProvider {
@Override
protected int getLayout() {
return R.layout.appwidget4x1;
}
@Override
protected int getLayout() {
return R.layout.appwidget4x1;
}
}

View File

@ -21,8 +21,8 @@ package net.nullsum.audinaut.provider;
import net.nullsum.audinaut.R;
public class AudinautWidget4x2 extends AudinautWidgetProvider {
@Override
protected int getLayout() {
return R.layout.appwidget4x2;
}
@Override
protected int getLayout() {
return R.layout.appwidget4x2;
}
}

View File

@ -21,8 +21,8 @@ package net.nullsum.audinaut.provider;
import net.nullsum.audinaut.R;
public class AudinautWidget4x3 extends AudinautWidgetProvider {
@Override
protected int getLayout() {
return R.layout.appwidget4x3;
}
@Override
protected int getLayout() {
return R.layout.appwidget4x3;
}
}

View File

@ -21,8 +21,8 @@ package net.nullsum.audinaut.provider;
import net.nullsum.audinaut.R;
public class AudinautWidget4x4 extends AudinautWidgetProvider {
@Override
protected int getLayout() {
return R.layout.appwidget4x4;
}
@Override
protected int getLayout() {
return R.layout.appwidget4x4;
}
}

View File

@ -60,44 +60,44 @@ import net.nullsum.audinaut.util.Util;
*/
public class AudinautWidgetProvider extends AppWidgetProvider {
private static final String TAG = AudinautWidgetProvider.class.getSimpleName();
private static AudinautWidget4x1 instance4x1;
private static AudinautWidget4x2 instance4x2;
private static AudinautWidget4x3 instance4x3;
private static AudinautWidget4x4 instance4x4;
private static AudinautWidget4x1 instance4x1;
private static AudinautWidget4x2 instance4x2;
private static AudinautWidget4x3 instance4x3;
private static AudinautWidget4x4 instance4x4;
public static synchronized void notifyInstances(Context context, DownloadService service, boolean playing) {
if(instance4x1 == null) {
instance4x1 = new AudinautWidget4x1();
}
if(instance4x2 == null) {
instance4x2 = new AudinautWidget4x2();
}
if(instance4x3 == null) {
instance4x3 = new AudinautWidget4x3();
}
if(instance4x4 == null) {
instance4x4 = new AudinautWidget4x4();
}
public static synchronized void notifyInstances(Context context, DownloadService service, boolean playing) {
if(instance4x1 == null) {
instance4x1 = new AudinautWidget4x1();
}
if(instance4x2 == null) {
instance4x2 = new AudinautWidget4x2();
}
if(instance4x3 == null) {
instance4x3 = new AudinautWidget4x3();
}
if(instance4x4 == null) {
instance4x4 = new AudinautWidget4x4();
}
instance4x1.notifyChange(context, service, playing);
instance4x2.notifyChange(context, service, playing);
instance4x3.notifyChange(context, service, playing);
instance4x4.notifyChange(context, service, playing);
}
instance4x1.notifyChange(context, service, playing);
instance4x2.notifyChange(context, service, playing);
instance4x3.notifyChange(context, service, playing);
instance4x4.notifyChange(context, service, playing);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
defaultAppWidget(context, appWidgetIds);
}
@Override
public void onEnabled(Context context) {
notifyInstances(context, DownloadService.getInstance(), false);
}
@Override
public void onEnabled(Context context) {
notifyInstances(context, DownloadService.getInstance(), false);
}
protected int getLayout() {
return 0;
}
protected int getLayout() {
return 0;
}
/**
* Initialize given widgets to default state, where we launch Subsonic on default click
@ -108,12 +108,12 @@ public class AudinautWidgetProvider extends AppWidgetProvider {
final RemoteViews views = new RemoteViews(context.getPackageName(), getLayout());
views.setTextViewText(R.id.artist, res.getText(R.string.widget_initial_text));
if(getLayout() == R.layout.appwidget4x2) {
views.setTextViewText(R.id.album, "");
}
if(getLayout() == R.layout.appwidget4x2) {
views.setTextViewText(R.id.album, "");
}
linkButtons(context, views, false);
performUpdate(context, null, appWidgetIds, false);
performUpdate(context, null, appWidgetIds, false);
}
private void pushUpdate(Context context, int[] appWidgetIds, RemoteViews views) {
@ -151,20 +151,20 @@ public class AudinautWidgetProvider extends AppWidgetProvider {
final Resources res = context.getResources();
final RemoteViews views = new RemoteViews(context.getPackageName(), getLayout());
if(playing) {
views.setViewVisibility(R.id.widget_root, View.VISIBLE);
} else {
// Hide widget
SharedPreferences prefs = Util.getPreferences(context);
if(prefs.getBoolean(Constants.PREFERENCES_KEY_HIDE_WIDGET, false)) {
views.setViewVisibility(R.id.widget_root, View.GONE);
}
}
if(playing) {
views.setViewVisibility(R.id.widget_root, View.VISIBLE);
} else {
// Hide widget
SharedPreferences prefs = Util.getPreferences(context);
if(prefs.getBoolean(Constants.PREFERENCES_KEY_HIDE_WIDGET, false)) {
views.setViewVisibility(R.id.widget_root, View.GONE);
}
}
// Get Entry from current playing DownloadFile
// Get Entry from current playing DownloadFile
MusicDirectory.Entry currentPlaying = null;
if(service == null) {
// Deserialize from playling list to setup
// Deserialize from playling list to setup
try {
PlayerQueue state = FileUtil.deserialize(context, DownloadServiceLifecycleSupport.FILENAME_DOWNLOADS_SER, PlayerQueue.class);
if (state != null && state.currentPlayingIndex != -1) {
@ -174,12 +174,12 @@ public class AudinautWidgetProvider extends AppWidgetProvider {
Log.e(TAG, "Failed to grab current playing", e);
}
} else {
currentPlaying = service.getCurrentPlaying() == null ? null : service.getCurrentPlaying().getSong();
currentPlaying = service.getCurrentPlaying() == null ? null : service.getCurrentPlaying().getSong();
}
String title = currentPlaying == null ? null : currentPlaying.getTitle();
CharSequence artist = currentPlaying == null ? null : currentPlaying.getArtist();
CharSequence album = currentPlaying == null ? null : currentPlaying.getAlbum();
CharSequence album = currentPlaying == null ? null : currentPlaying.getAlbum();
CharSequence errorState = null;
// Show error message?
@ -195,19 +195,19 @@ public class AudinautWidgetProvider extends AppWidgetProvider {
if (errorState != null) {
// Show error state to user
views.setTextViewText(R.id.title,null);
views.setTextViewText(R.id.title,null);
views.setTextViewText(R.id.artist, errorState);
views.setTextViewText(R.id.album, "");
if(getLayout() != R.layout.appwidget4x1) {
views.setImageViewResource(R.id.appwidget_coverart, R.drawable.appwidget_art_default);
}
views.setTextViewText(R.id.album, "");
if(getLayout() != R.layout.appwidget4x1) {
views.setImageViewResource(R.id.appwidget_coverart, R.drawable.appwidget_art_default);
}
} else {
// No error, so show normal titles
views.setTextViewText(R.id.title, title);
views.setTextViewText(R.id.artist, artist);
if(getLayout() != R.layout.appwidget4x1) {
views.setTextViewText(R.id.album, album);
}
if(getLayout() != R.layout.appwidget4x1) {
views.setTextViewText(R.id.album, album);
}
}
// Set correct drawable for pause state
@ -220,10 +220,10 @@ public class AudinautWidgetProvider extends AppWidgetProvider {
// Set the cover art
try {
boolean large = false;
if(getLayout() != R.layout.appwidget4x1 && getLayout() != R.layout.appwidget4x2) {
large = true;
}
ImageLoader imageLoader = SubsonicActivity.getStaticImageLoader(context);
if(getLayout() != R.layout.appwidget4x1 && getLayout() != R.layout.appwidget4x2) {
large = true;
}
ImageLoader imageLoader = SubsonicActivity.getStaticImageLoader(context);
Bitmap bitmap = imageLoader == null ? null : imageLoader.getCachedImage(context, currentPlaying, large);
if (bitmap == null) {
@ -276,9 +276,9 @@ public class AudinautWidgetProvider extends AppWidgetProvider {
* @param playerActive @param playerActive True if player is active in background. Launch {@link net.nullsum.audinaut.activity.SubsonicFragmentActivity}.
*/
private void linkButtons(Context context, RemoteViews views, boolean playerActive) {
Intent intent = new Intent(context, SubsonicFragmentActivity.class);
intent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD, true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent(context, SubsonicFragmentActivity.class);
intent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD, true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.appwidget_coverart, pendingIntent);
views.setOnClickPendingIntent(R.id.appwidget_top, pendingIntent);
@ -286,19 +286,19 @@ public class AudinautWidgetProvider extends AppWidgetProvider {
// Emulate media button clicks.
intent = new Intent("Audinaut.PLAY_PAUSE");
intent.setComponent(new ComponentName(context, DownloadService.class));
intent.setAction(DownloadService.CMD_TOGGLEPAUSE);
intent.setAction(DownloadService.CMD_TOGGLEPAUSE);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.control_play, pendingIntent);
intent = new Intent("Audinaut.NEXT"); // Use a unique action name to ensure a different PendingIntent to be created.
intent.setComponent(new ComponentName(context, DownloadService.class));
intent.setAction(DownloadService.CMD_NEXT);
intent.setAction(DownloadService.CMD_NEXT);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.control_next, pendingIntent);
intent = new Intent("Audinaut.PREVIOUS"); // Use a unique action name to ensure a different PendingIntent to be created.
intent.setComponent(new ComponentName(context, DownloadService.class));
intent.setAction(DownloadService.CMD_PREVIOUS);
intent.setAction(DownloadService.CMD_PREVIOUS);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.control_previous, pendingIntent);
}

View File

@ -7,41 +7,41 @@ import android.util.Log;
import net.nullsum.audinaut.service.DownloadService;
public class A2dpIntentReceiver extends BroadcastReceiver {
private static final String PLAYSTATUS_RESPONSE = "com.android.music.playstatusresponse";
private String TAG = A2dpIntentReceiver.class.getSimpleName();
private static final String PLAYSTATUS_RESPONSE = "com.android.music.playstatusresponse";
private String TAG = A2dpIntentReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "GOT INTENT " + intent);
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "GOT INTENT " + intent);
DownloadService downloadService = DownloadService.getInstance();
DownloadService downloadService = DownloadService.getInstance();
if (downloadService != null){
if (downloadService != null){
Intent avrcpIntent = new Intent(PLAYSTATUS_RESPONSE);
Intent avrcpIntent = new Intent(PLAYSTATUS_RESPONSE);
avrcpIntent.putExtra("duration", (long) downloadService.getPlayerDuration());
avrcpIntent.putExtra("position", (long) downloadService.getPlayerPosition());
avrcpIntent.putExtra("ListSize", (long) downloadService.getSongs().size());
avrcpIntent.putExtra("duration", (long) downloadService.getPlayerDuration());
avrcpIntent.putExtra("position", (long) downloadService.getPlayerPosition());
avrcpIntent.putExtra("ListSize", (long) downloadService.getSongs().size());
switch (downloadService.getPlayerState()){
case STARTED:
avrcpIntent.putExtra("playing", true);
break;
case STOPPED:
avrcpIntent.putExtra("playing", false);
break;
case PAUSED:
avrcpIntent.putExtra("playing", false);
break;
case COMPLETED:
avrcpIntent.putExtra("playing", false);
break;
default:
return;
}
switch (downloadService.getPlayerState()){
case STARTED:
avrcpIntent.putExtra("playing", true);
break;
case STOPPED:
avrcpIntent.putExtra("playing", false);
break;
case PAUSED:
avrcpIntent.putExtra("playing", false);
break;
case COMPLETED:
avrcpIntent.putExtra("playing", false);
break;
default:
return;
}
context.sendBroadcast(avrcpIntent);
}
}
context.sendBroadcast(avrcpIntent);
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2014 (C) Scott Jackson
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 2014 (C) Scott Jackson
*/
package net.nullsum.audinaut.receiver;
@ -28,24 +28,24 @@ import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.Util;
public class AudioNoisyReceiver extends BroadcastReceiver {
private static final String TAG = AudioNoisyReceiver.class.getSimpleName();
private static final String TAG = AudioNoisyReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
DownloadService downloadService = DownloadService.getInstance();
// Don't do anything if downloadService is not started
if(downloadService == null) {
return;
}
@Override
public void onReceive(Context context, Intent intent) {
DownloadService downloadService = DownloadService.getInstance();
// Don't do anything if downloadService is not started
if(downloadService == null) {
return;
}
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals (intent.getAction ())) {
if((downloadService.getPlayerState() == PlayerState.STARTED || downloadService.getPlayerState() == PlayerState.PAUSED_TEMP)) {
SharedPreferences prefs = Util.getPreferences(downloadService);
int pausePref = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_PAUSE_DISCONNECT, "0"));
if(pausePref == 0) {
downloadService.pause();
}
}
}
}
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals (intent.getAction ())) {
if((downloadService.getPlayerState() == PlayerState.STARTED || downloadService.getPlayerState() == PlayerState.PAUSED_TEMP)) {
SharedPreferences prefs = Util.getPreferences(downloadService);
int pausePref = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_PAUSE_DISCONNECT, "0"));
if(pausePref == 0) {
downloadService.pause();
}
}
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.receiver;
@ -23,12 +23,12 @@ import net.nullsum.audinaut.service.HeadphoneListenerService;
import net.nullsum.audinaut.util.Util;
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(Util.shouldStartOnHeadphones(context)) {
Intent serviceIntent = new Intent();
serviceIntent.setClassName(context.getPackageName(), HeadphoneListenerService.class.getName());
context.startService(serviceIntent);
}
}
@Override
public void onReceive(Context context, Intent intent) {
if(Util.shouldStartOnHeadphones(context)) {
Intent serviceIntent = new Intent();
serviceIntent.setClassName(context.getPackageName(), HeadphoneListenerService.class.getName());
context.startService(serviceIntent);
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.receiver;
@ -24,17 +24,17 @@ import net.nullsum.audinaut.service.DownloadService;
import net.nullsum.audinaut.util.Util;
public class HeadphonePlugReceiver extends BroadcastReceiver {
private static final String TAG = HeadphonePlugReceiver.class.getSimpleName();
private static final String TAG = HeadphonePlugReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_HEADSET_PLUG.equals(intent.getAction())) {
int headphoneState = intent.getIntExtra("state", -1);
if(headphoneState == 1 && Util.shouldStartOnHeadphones(context)) {
Intent start = new Intent(context, DownloadService.class);
start.setAction(DownloadService.START_PLAY);
context.startService(start);
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_HEADSET_PLUG.equals(intent.getAction())) {
int headphoneState = intent.getIntExtra("state", -1);
if(headphoneState == 1 && Util.shouldStartOnHeadphones(context)) {
Intent start = new Intent(context, DownloadService.class);
start.setAction(DownloadService.START_PLAY);
context.startService(start);
}
}
}
}

View File

@ -36,22 +36,22 @@ public class MediaButtonIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
KeyEvent event = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT);
if(DownloadService.getInstance() == null && (event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_STOP ||
event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE || event.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK)) {
Log.w(TAG, "Ignore keycode event because downloadService is off");
return;
}
if(DownloadService.getInstance() == null && (event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_STOP ||
event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE || event.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK)) {
Log.w(TAG, "Ignore keycode event because downloadService is off");
return;
}
Log.i(TAG, "Got MEDIA_BUTTON key event: " + event);
Intent serviceIntent = new Intent(context, DownloadService.class);
serviceIntent.putExtra(Intent.EXTRA_KEY_EVENT, event);
context.startService(serviceIntent);
if (isOrderedBroadcast()) {
try {
abortBroadcast();
} catch (Exception x) {
// Ignored.
}
}
try {
abortBroadcast();
} catch (Exception x) {
// Ignored.
}
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2014 (C) Scott Jackson
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 2014 (C) Scott Jackson
*/
package net.nullsum.audinaut.receiver;
@ -25,22 +25,22 @@ import net.nullsum.audinaut.service.DownloadService;
import net.nullsum.audinaut.util.Constants;
public class PlayActionReceiver extends BroadcastReceiver {
private static final String TAG = PlayActionReceiver.class.getSimpleName();
private static final String TAG = PlayActionReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
if(intent.hasExtra(Constants.TASKER_EXTRA_BUNDLE)) {
Bundle data = intent.getBundleExtra(Constants.TASKER_EXTRA_BUNDLE);
Boolean startShuffled = data.getBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE);
@Override
public void onReceive(Context context, Intent intent) {
if(intent.hasExtra(Constants.TASKER_EXTRA_BUNDLE)) {
Bundle data = intent.getBundleExtra(Constants.TASKER_EXTRA_BUNDLE);
Boolean startShuffled = data.getBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE);
Intent start = new Intent(context, DownloadService.class);
start.setAction(DownloadService.START_PLAY);
start.putExtra(Constants.INTENT_EXTRA_NAME_SHUFFLE, startShuffled);
start.putExtra(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, data.getString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR));
start.putExtra(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, data.getString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR));
start.putExtra(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, data.getString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE));
start.putExtra(Constants.PREFERENCES_KEY_OFFLINE, data.getInt(Constants.PREFERENCES_KEY_OFFLINE));
context.startService(start);
}
}
Intent start = new Intent(context, DownloadService.class);
start.setAction(DownloadService.START_PLAY);
start.putExtra(Constants.INTENT_EXTRA_NAME_SHUFFLE, startShuffled);
start.putExtra(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, data.getString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR));
start.putExtra(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, data.getString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR));
start.putExtra(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, data.getString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE));
start.putExtra(Constants.PREFERENCES_KEY_OFFLINE, data.getInt(Constants.PREFERENCES_KEY_OFFLINE));
context.startService(start);
}
}
}

View File

@ -53,378 +53,378 @@ import static net.nullsum.audinaut.domain.PlayerState.PREPARING;
* @author Sindre Mehus
*/
public class DownloadServiceLifecycleSupport {
private static final String TAG = DownloadServiceLifecycleSupport.class.getSimpleName();
public static final String FILENAME_DOWNLOADS_SER = "downloadstate2.ser";
private static final int DEBOUNCE_TIME = 200;
private static final String TAG = DownloadServiceLifecycleSupport.class.getSimpleName();
public static final String FILENAME_DOWNLOADS_SER = "downloadstate2.ser";
private static final int DEBOUNCE_TIME = 200;
private final DownloadService downloadService;
private Looper eventLooper;
private Handler eventHandler;
private BroadcastReceiver ejectEventReceiver;
private PhoneStateListener phoneStateListener;
private boolean externalStorageAvailable= true;
private ReentrantLock lock = new ReentrantLock();
private final AtomicBoolean setup = new AtomicBoolean(false);
private long lastPressTime = 0;
private SilentBackgroundTask<Void> currentSavePlayQueueTask = null;
private Date lastChange = null;
private final DownloadService downloadService;
private Looper eventLooper;
private Handler eventHandler;
private BroadcastReceiver ejectEventReceiver;
private PhoneStateListener phoneStateListener;
private boolean externalStorageAvailable= true;
private ReentrantLock lock = new ReentrantLock();
private final AtomicBoolean setup = new AtomicBoolean(false);
private long lastPressTime = 0;
private SilentBackgroundTask<Void> currentSavePlayQueueTask = null;
private Date lastChange = null;
/**
* This receiver manages the intent that could come from other applications.
*/
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
eventHandler.post(new Runnable() {
@Override
public void run() {
String action = intent.getAction();
Log.i(TAG, "intentReceiver.onReceive: " + action);
if (DownloadService.CMD_PLAY.equals(action)) {
downloadService.play();
} else if (DownloadService.CMD_NEXT.equals(action)) {
downloadService.next();
} else if (DownloadService.CMD_PREVIOUS.equals(action)) {
downloadService.previous();
} else if (DownloadService.CMD_TOGGLEPAUSE.equals(action)) {
downloadService.togglePlayPause();
} else if (DownloadService.CMD_PAUSE.equals(action)) {
downloadService.pause();
} else if (DownloadService.CMD_STOP.equals(action)) {
downloadService.pause();
downloadService.seekTo(0);
}
}
});
}
};
/**
* This receiver manages the intent that could come from other applications.
*/
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
eventHandler.post(new Runnable() {
@Override
public void run() {
String action = intent.getAction();
Log.i(TAG, "intentReceiver.onReceive: " + action);
if (DownloadService.CMD_PLAY.equals(action)) {
downloadService.play();
} else if (DownloadService.CMD_NEXT.equals(action)) {
downloadService.next();
} else if (DownloadService.CMD_PREVIOUS.equals(action)) {
downloadService.previous();
} else if (DownloadService.CMD_TOGGLEPAUSE.equals(action)) {
downloadService.togglePlayPause();
} else if (DownloadService.CMD_PAUSE.equals(action)) {
downloadService.pause();
} else if (DownloadService.CMD_STOP.equals(action)) {
downloadService.pause();
downloadService.seekTo(0);
}
}
});
}
};
public DownloadServiceLifecycleSupport(DownloadService downloadService) {
this.downloadService = downloadService;
}
public DownloadServiceLifecycleSupport(DownloadService downloadService) {
this.downloadService = downloadService;
}
public void onCreate() {
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
eventLooper = Looper.myLooper();
eventHandler = new Handler(eventLooper);
public void onCreate() {
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
eventLooper = Looper.myLooper();
eventHandler = new Handler(eventLooper);
// Deserialize queue before starting looper
try {
lock.lock();
deserializeDownloadQueueNow();
// Deserialize queue before starting looper
try {
lock.lock();
deserializeDownloadQueueNow();
// Wait until PREPARING is done to mark lifecycle as ready to receive events
while(downloadService.getPlayerState() == PREPARING) {
Util.sleepQuietly(50L);
}
// Wait until PREPARING is done to mark lifecycle as ready to receive events
while(downloadService.getPlayerState() == PREPARING) {
Util.sleepQuietly(50L);
}
setup.set(true);
} finally {
lock.unlock();
}
setup.set(true);
} finally {
lock.unlock();
}
Looper.loop();
}
}, "DownloadServiceLifecycleSupport").start();
Looper.loop();
}
}, "DownloadServiceLifecycleSupport").start();
// Stop when SD card is ejected.
ejectEventReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
externalStorageAvailable = Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction());
if (!externalStorageAvailable) {
Log.i(TAG, "External media is ejecting. Stopping playback.");
downloadService.reset();
} else {
Log.i(TAG, "External media is available.");
}
}
};
IntentFilter ejectFilter = new IntentFilter(Intent.ACTION_MEDIA_EJECT);
ejectFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
ejectFilter.addDataScheme("file");
downloadService.registerReceiver(ejectEventReceiver, ejectFilter);
// Stop when SD card is ejected.
ejectEventReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
externalStorageAvailable = Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction());
if (!externalStorageAvailable) {
Log.i(TAG, "External media is ejecting. Stopping playback.");
downloadService.reset();
} else {
Log.i(TAG, "External media is available.");
}
}
};
IntentFilter ejectFilter = new IntentFilter(Intent.ACTION_MEDIA_EJECT);
ejectFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
ejectFilter.addDataScheme("file");
downloadService.registerReceiver(ejectEventReceiver, ejectFilter);
// React to media buttons.
Util.registerMediaButtonEventReceiver(downloadService);
// React to media buttons.
Util.registerMediaButtonEventReceiver(downloadService);
// Pause temporarily on incoming phone calls.
phoneStateListener = new MyPhoneStateListener();
// Pause temporarily on incoming phone calls.
phoneStateListener = new MyPhoneStateListener();
// Android 6.0 removes requirement for android.Manifest.permission.READ_PHONE_STATE;
TelephonyManager telephonyManager = (TelephonyManager) downloadService.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
// Android 6.0 removes requirement for android.Manifest.permission.READ_PHONE_STATE;
TelephonyManager telephonyManager = (TelephonyManager) downloadService.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
// Register the handler for outside intents.
IntentFilter commandFilter = new IntentFilter();
commandFilter.addAction(DownloadService.CMD_PLAY);
commandFilter.addAction(DownloadService.CMD_TOGGLEPAUSE);
commandFilter.addAction(DownloadService.CMD_PAUSE);
commandFilter.addAction(DownloadService.CMD_STOP);
commandFilter.addAction(DownloadService.CMD_PREVIOUS);
commandFilter.addAction(DownloadService.CMD_NEXT);
commandFilter.addAction(DownloadService.CANCEL_DOWNLOADS);
downloadService.registerReceiver(intentReceiver, commandFilter);
// Register the handler for outside intents.
IntentFilter commandFilter = new IntentFilter();
commandFilter.addAction(DownloadService.CMD_PLAY);
commandFilter.addAction(DownloadService.CMD_TOGGLEPAUSE);
commandFilter.addAction(DownloadService.CMD_PAUSE);
commandFilter.addAction(DownloadService.CMD_STOP);
commandFilter.addAction(DownloadService.CMD_PREVIOUS);
commandFilter.addAction(DownloadService.CMD_NEXT);
commandFilter.addAction(DownloadService.CANCEL_DOWNLOADS);
downloadService.registerReceiver(intentReceiver, commandFilter);
new CacheCleaner(downloadService, downloadService).clean();
}
new CacheCleaner(downloadService, downloadService).clean();
}
public boolean isInitialized() {
return setup.get();
}
public boolean isInitialized() {
return setup.get();
}
public void onStart(final Intent intent) {
if (intent != null) {
final String action = intent.getAction();
public void onStart(final Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if(eventHandler == null) {
Util.sleepQuietly(100L);
}
if(eventHandler == null) {
return;
}
if(eventHandler == null) {
Util.sleepQuietly(100L);
}
if(eventHandler == null) {
return;
}
eventHandler.post(new Runnable() {
@Override
public void run() {
if(!setup.get()) {
lock.lock();
lock.unlock();
}
eventHandler.post(new Runnable() {
@Override
public void run() {
if(!setup.get()) {
lock.lock();
lock.unlock();
}
if(DownloadService.START_PLAY.equals(action)) {
int offlinePref = intent.getIntExtra(Constants.PREFERENCES_KEY_OFFLINE, 0);
if(offlinePref != 0) {
boolean offline = (offlinePref == 2);
Util.setOffline(downloadService, offline);
if (offline) {
downloadService.clearIncomplete();
} else {
downloadService.checkDownloads();
}
}
if(DownloadService.START_PLAY.equals(action)) {
int offlinePref = intent.getIntExtra(Constants.PREFERENCES_KEY_OFFLINE, 0);
if(offlinePref != 0) {
boolean offline = (offlinePref == 2);
Util.setOffline(downloadService, offline);
if (offline) {
downloadService.clearIncomplete();
} else {
downloadService.checkDownloads();
}
}
if(intent.getBooleanExtra(Constants.INTENT_EXTRA_NAME_SHUFFLE, false)) {
// Add shuffle parameters
SharedPreferences.Editor editor = Util.getPreferences(downloadService).edit();
String startYear = intent.getStringExtra(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR);
if(startYear != null) {
editor.putString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, startYear);
}
if(intent.getBooleanExtra(Constants.INTENT_EXTRA_NAME_SHUFFLE, false)) {
// Add shuffle parameters
SharedPreferences.Editor editor = Util.getPreferences(downloadService).edit();
String startYear = intent.getStringExtra(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR);
if(startYear != null) {
editor.putString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, startYear);
}
String endYear = intent.getStringExtra(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR);
if(endYear != null) {
editor.putString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, endYear);
}
String endYear = intent.getStringExtra(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR);
if(endYear != null) {
editor.putString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, endYear);
}
String genre = intent.getStringExtra(Constants.PREFERENCES_KEY_SHUFFLE_GENRE);
if(genre != null) {
editor.putString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, genre);
}
editor.apply();
String genre = intent.getStringExtra(Constants.PREFERENCES_KEY_SHUFFLE_GENRE);
if(genre != null) {
editor.putString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, genre);
}
editor.apply();
downloadService.clear();
downloadService.setShufflePlayEnabled(true);
} else {
downloadService.start();
}
} else if(DownloadService.CMD_TOGGLEPAUSE.equals(action)) {
downloadService.togglePlayPause();
} else if(DownloadService.CMD_NEXT.equals(action)) {
downloadService.next();
} else if(DownloadService.CMD_PREVIOUS.equals(action)) {
downloadService.previous();
} else if(DownloadService.CANCEL_DOWNLOADS.equals(action)) {
downloadService.clearBackground();
} else if(intent.getExtras() != null) {
final KeyEvent event = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT);
if (event != null) {
handleKeyEvent(event);
}
}
}
});
}
}
downloadService.clear();
downloadService.setShufflePlayEnabled(true);
} else {
downloadService.start();
}
} else if(DownloadService.CMD_TOGGLEPAUSE.equals(action)) {
downloadService.togglePlayPause();
} else if(DownloadService.CMD_NEXT.equals(action)) {
downloadService.next();
} else if(DownloadService.CMD_PREVIOUS.equals(action)) {
downloadService.previous();
} else if(DownloadService.CANCEL_DOWNLOADS.equals(action)) {
downloadService.clearBackground();
} else if(intent.getExtras() != null) {
final KeyEvent event = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT);
if (event != null) {
handleKeyEvent(event);
}
}
}
});
}
}
public void onDestroy() {
serializeDownloadQueue();
eventLooper.quit();
downloadService.unregisterReceiver(ejectEventReceiver);
downloadService.unregisterReceiver(intentReceiver);
public void onDestroy() {
serializeDownloadQueue();
eventLooper.quit();
downloadService.unregisterReceiver(ejectEventReceiver);
downloadService.unregisterReceiver(intentReceiver);
TelephonyManager telephonyManager = (TelephonyManager) downloadService.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
}
TelephonyManager telephonyManager = (TelephonyManager) downloadService.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
}
public boolean isExternalStorageAvailable() {
return externalStorageAvailable;
}
public boolean isExternalStorageAvailable() {
return externalStorageAvailable;
}
public void serializeDownloadQueue() {
serializeDownloadQueue(true);
}
public void serializeDownloadQueue(final boolean serializeRemote) {
if(!setup.get()) {
return;
}
public void serializeDownloadQueue() {
serializeDownloadQueue(true);
}
public void serializeDownloadQueue(final boolean serializeRemote) {
if(!setup.get()) {
return;
}
final List<DownloadFile> songs = new ArrayList<DownloadFile>(downloadService.getSongs());
eventHandler.post(new Runnable() {
@Override
public void run() {
if(lock.tryLock()) {
try {
serializeDownloadQueueNow(songs, serializeRemote);
} finally {
lock.unlock();
}
}
}
});
}
final List<DownloadFile> songs = new ArrayList<DownloadFile>(downloadService.getSongs());
eventHandler.post(new Runnable() {
@Override
public void run() {
if(lock.tryLock()) {
try {
serializeDownloadQueueNow(songs, serializeRemote);
} finally {
lock.unlock();
}
}
}
});
}
public void serializeDownloadQueueNow(List<DownloadFile> songs, boolean serializeRemote) {
final PlayerQueue state = new PlayerQueue();
for (DownloadFile downloadFile : songs) {
state.songs.add(downloadFile.getSong());
}
for (DownloadFile downloadFile : downloadService.getToDelete()) {
state.toDelete.add(downloadFile.getSong());
}
state.currentPlayingIndex = downloadService.getCurrentPlayingIndex();
state.currentPlayingPosition = downloadService.getPlayerPosition();
public void serializeDownloadQueueNow(List<DownloadFile> songs, boolean serializeRemote) {
final PlayerQueue state = new PlayerQueue();
for (DownloadFile downloadFile : songs) {
state.songs.add(downloadFile.getSong());
}
for (DownloadFile downloadFile : downloadService.getToDelete()) {
state.toDelete.add(downloadFile.getSong());
}
state.currentPlayingIndex = downloadService.getCurrentPlayingIndex();
state.currentPlayingPosition = downloadService.getPlayerPosition();
DownloadFile currentPlaying = downloadService.getCurrentPlaying();
if(currentPlaying != null) {
state.renameCurrent = currentPlaying.isWorkDone() && !currentPlaying.isCompleteFileAvailable();
}
state.changed = lastChange = new Date();
DownloadFile currentPlaying = downloadService.getCurrentPlaying();
if(currentPlaying != null) {
state.renameCurrent = currentPlaying.isWorkDone() && !currentPlaying.isCompleteFileAvailable();
}
state.changed = lastChange = new Date();
Log.i(TAG, "Serialized currentPlayingIndex: " + state.currentPlayingIndex + ", currentPlayingPosition: " + state.currentPlayingPosition);
FileUtil.serialize(downloadService, state, FILENAME_DOWNLOADS_SER);
}
Log.i(TAG, "Serialized currentPlayingIndex: " + state.currentPlayingIndex + ", currentPlayingPosition: " + state.currentPlayingPosition);
FileUtil.serialize(downloadService, state, FILENAME_DOWNLOADS_SER);
}
public void post(Runnable runnable) {
eventHandler.post(runnable);
}
public void post(Runnable runnable) {
eventHandler.post(runnable);
}
private void deserializeDownloadQueueNow() {
PlayerQueue state = FileUtil.deserialize(downloadService, FILENAME_DOWNLOADS_SER, PlayerQueue.class);
if (state == null) {
return;
}
Log.i(TAG, "Deserialized currentPlayingIndex: " + state.currentPlayingIndex + ", currentPlayingPosition: " + state.currentPlayingPosition);
private void deserializeDownloadQueueNow() {
PlayerQueue state = FileUtil.deserialize(downloadService, FILENAME_DOWNLOADS_SER, PlayerQueue.class);
if (state == null) {
return;
}
Log.i(TAG, "Deserialized currentPlayingIndex: " + state.currentPlayingIndex + ", currentPlayingPosition: " + state.currentPlayingPosition);
// Rename first thing before anything else starts
if(state.renameCurrent && state.currentPlayingIndex != -1 && state.currentPlayingIndex < state.songs.size()) {
DownloadFile currentPlaying = new DownloadFile(downloadService, state.songs.get(state.currentPlayingIndex), false);
currentPlaying.renamePartial();
}
// Rename first thing before anything else starts
if(state.renameCurrent && state.currentPlayingIndex != -1 && state.currentPlayingIndex < state.songs.size()) {
DownloadFile currentPlaying = new DownloadFile(downloadService, state.songs.get(state.currentPlayingIndex), false);
currentPlaying.renamePartial();
}
downloadService.restore(state.songs, state.toDelete, state.currentPlayingIndex, state.currentPlayingPosition);
downloadService.restore(state.songs, state.toDelete, state.currentPlayingIndex, state.currentPlayingPosition);
if(state != null) {
lastChange = state.changed;
}
}
if(state != null) {
lastChange = state.changed;
}
}
public Date getLastChange() {
return lastChange;
}
public Date getLastChange() {
return lastChange;
}
public void handleKeyEvent(KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() > 0) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
downloadService.fastForward();
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
downloadService.rewind();
break;
}
} else if(event.getAction() == KeyEvent.ACTION_UP) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_HEADSETHOOK:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
if(lastPressTime < (System.currentTimeMillis() - 500)) {
lastPressTime = System.currentTimeMillis();
downloadService.togglePlayPause();
} else {
downloadService.next(false, true);
}
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
if(lastPressTime < (System.currentTimeMillis() - DEBOUNCE_TIME)) {
lastPressTime = System.currentTimeMillis();
downloadService.previous();
}
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
if(lastPressTime < (System.currentTimeMillis() - DEBOUNCE_TIME)) {
lastPressTime = System.currentTimeMillis();
downloadService.next();
}
break;
case KeyEvent.KEYCODE_MEDIA_REWIND:
downloadService.rewind();
break;
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
downloadService.fastForward();
break;
case KeyEvent.KEYCODE_MEDIA_STOP:
downloadService.stop();
break;
case KeyEvent.KEYCODE_MEDIA_PLAY:
if(downloadService.getPlayerState() != PlayerState.STARTED) {
downloadService.start();
}
break;
case KeyEvent.KEYCODE_MEDIA_PAUSE:
downloadService.pause();
default:
break;
}
}
}
public void handleKeyEvent(KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() > 0) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
downloadService.fastForward();
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
downloadService.rewind();
break;
}
} else if(event.getAction() == KeyEvent.ACTION_UP) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_HEADSETHOOK:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
if(lastPressTime < (System.currentTimeMillis() - 500)) {
lastPressTime = System.currentTimeMillis();
downloadService.togglePlayPause();
} else {
downloadService.next(false, true);
}
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
if(lastPressTime < (System.currentTimeMillis() - DEBOUNCE_TIME)) {
lastPressTime = System.currentTimeMillis();
downloadService.previous();
}
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
if(lastPressTime < (System.currentTimeMillis() - DEBOUNCE_TIME)) {
lastPressTime = System.currentTimeMillis();
downloadService.next();
}
break;
case KeyEvent.KEYCODE_MEDIA_REWIND:
downloadService.rewind();
break;
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
downloadService.fastForward();
break;
case KeyEvent.KEYCODE_MEDIA_STOP:
downloadService.stop();
break;
case KeyEvent.KEYCODE_MEDIA_PLAY:
if(downloadService.getPlayerState() != PlayerState.STARTED) {
downloadService.start();
}
break;
case KeyEvent.KEYCODE_MEDIA_PAUSE:
downloadService.pause();
default:
break;
}
}
}
/**
* Logic taken from packages/apps/Music. Will pause when an incoming
* call rings or if a call (incoming or outgoing) is connected.
*/
private class MyPhoneStateListener extends PhoneStateListener {
private boolean resumeAfterCall;
/**
* Logic taken from packages/apps/Music. Will pause when an incoming
* call rings or if a call (incoming or outgoing) is connected.
*/
private class MyPhoneStateListener extends PhoneStateListener {
private boolean resumeAfterCall;
@Override
public void onCallStateChanged(final int state, String incomingNumber) {
eventHandler.post(new Runnable() {
@Override
public void run() {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
case TelephonyManager.CALL_STATE_OFFHOOK:
if (downloadService.getPlayerState() == PlayerState.STARTED) {
resumeAfterCall = true;
downloadService.pause(true);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (resumeAfterCall) {
resumeAfterCall = false;
if(downloadService.getPlayerState() == PlayerState.PAUSED_TEMP) {
downloadService.start();
}
}
break;
default:
break;
}
}
});
}
}
@Override
public void onCallStateChanged(final int state, String incomingNumber) {
eventHandler.post(new Runnable() {
@Override
public void run() {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
case TelephonyManager.CALL_STATE_OFFHOOK:
if (downloadService.getPlayerState() == PlayerState.STARTED) {
resumeAfterCall = true;
downloadService.pause(true);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (resumeAfterCall) {
resumeAfterCall = false;
if(downloadService.getPlayerState() == PlayerState.PAUSED_TEMP) {
downloadService.start();
}
}
break;
default:
break;
}
}
});
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.service;
@ -27,40 +27,40 @@ import net.nullsum.audinaut.util.Util;
* Created by Scott on 4/6/2015.
*/
public class HeadphoneListenerService extends Service {
private HeadphonePlugReceiver receiver;
private HeadphonePlugReceiver receiver;
@Override
public void onCreate() {
super.onCreate();
@Override
public void onCreate() {
super.onCreate();
receiver = new HeadphonePlugReceiver();
registerReceiver(receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
}
receiver = new HeadphonePlugReceiver();
registerReceiver(receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!Util.shouldStartOnHeadphones(this)) {
stopSelf();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!Util.shouldStartOnHeadphones(this)) {
stopSelf();
}
return Service.START_STICKY;
}
return Service.START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
@Override
public void onDestroy() {
super.onDestroy();
try {
if(receiver != null) {
unregisterReceiver(receiver);
}
} catch(Exception e) {
// Don't care
}
}
try {
if(receiver != null) {
unregisterReceiver(receiver);
}
} catch(Exception e) {
// Don't care
}
}
}

View File

@ -36,24 +36,24 @@ import net.nullsum.audinaut.util.Util;
*/
public class MediaStoreService {
private static final String TAG = MediaStoreService.class.getSimpleName();
private static final Uri ALBUM_ART_URI = Uri.parse("content://media/external/audio/albumart");
private static final String TAG = MediaStoreService.class.getSimpleName();
private static final Uri ALBUM_ART_URI = Uri.parse("content://media/external/audio/albumart");
private final Context context;
private final Context context;
public MediaStoreService(Context context) {
this.context = context;
}
public MediaStoreService(Context context) {
this.context = context;
}
public void saveInMediaStore(DownloadFile downloadFile) {
MusicDirectory.Entry song = downloadFile.getSong();
File songFile = downloadFile.getCompleteFile();
public void saveInMediaStore(DownloadFile downloadFile) {
MusicDirectory.Entry song = downloadFile.getSong();
File songFile = downloadFile.getCompleteFile();
// Delete existing row in case the song has been downloaded before.
deleteFromMediaStore(downloadFile);
// Delete existing row in case the song has been downloaded before.
deleteFromMediaStore(downloadFile);
ContentResolver contentResolver = context.getContentResolver();
ContentValues values = new ContentValues();
ContentResolver contentResolver = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.TITLE, song.getTitle());
values.put(MediaStore.MediaColumns.DATA, songFile.getAbsolutePath());
values.put(MediaStore.Audio.AudioColumns.ARTIST, song.getArtist());
@ -84,68 +84,68 @@ public class MediaStoreService {
}
cursor.close();
}
}
public void deleteFromMediaStore(DownloadFile downloadFile) {
ContentResolver contentResolver = context.getContentResolver();
MusicDirectory.Entry song = downloadFile.getSong();
File file = downloadFile.getCompleteFile();
public void deleteFromMediaStore(DownloadFile downloadFile) {
ContentResolver contentResolver = context.getContentResolver();
MusicDirectory.Entry song = downloadFile.getSong();
File file = downloadFile.getCompleteFile();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
int n = contentResolver.delete(uri,
MediaStore.MediaColumns.DATA + "=?",
new String[]{file.getAbsolutePath()});
if (n > 0) {
Log.i(TAG, "Deleting media store row for " + song);
}
}
int n = contentResolver.delete(uri,
MediaStore.MediaColumns.DATA + "=?",
new String[]{file.getAbsolutePath()});
if (n > 0) {
Log.i(TAG, "Deleting media store row for " + song);
}
}
public void deleteFromMediaStore(File file) {
ContentResolver contentResolver = context.getContentResolver();
public void deleteFromMediaStore(File file) {
ContentResolver contentResolver = context.getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
int n = contentResolver.delete(uri,
MediaStore.MediaColumns.DATA + "=?",
new String[]{file.getAbsolutePath()});
if (n > 0) {
Log.i(TAG, "Deleting media store row for " + file);
}
}
int n = contentResolver.delete(uri,
MediaStore.MediaColumns.DATA + "=?",
new String[]{file.getAbsolutePath()});
if (n > 0) {
Log.i(TAG, "Deleting media store row for " + file);
}
}
public void renameInMediaStore(File start, File end) {
ContentResolver contentResolver = context.getContentResolver();
public void renameInMediaStore(File start, File end) {
ContentResolver contentResolver = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, end.getAbsolutePath());
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, end.getAbsolutePath());
int n = contentResolver.update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
values,
MediaStore.MediaColumns.DATA + "=?",
new String[]{start.getAbsolutePath()});
if (n > 0) {
Log.i(TAG, "Rename media store row for " + start + " to " + end);
}
}
int n = contentResolver.update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
values,
MediaStore.MediaColumns.DATA + "=?",
new String[]{start.getAbsolutePath()});
if (n > 0) {
Log.i(TAG, "Rename media store row for " + start + " to " + end);
}
}
private void insertAlbumArt(int albumId, DownloadFile downloadFile) {
ContentResolver contentResolver = context.getContentResolver();
private void insertAlbumArt(int albumId, DownloadFile downloadFile) {
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(Uri.withAppendedPath(ALBUM_ART_URI, String.valueOf(albumId)), null, null, null, null);
if (!cursor.moveToFirst()) {
Cursor cursor = contentResolver.query(Uri.withAppendedPath(ALBUM_ART_URI, String.valueOf(albumId)), null, null, null, null);
if (!cursor.moveToFirst()) {
// No album art found, add it.
File albumArtFile = FileUtil.getAlbumArtFile(context, downloadFile.getSong());
if (albumArtFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.AlbumColumns.ALBUM_ID, albumId);
values.put(MediaStore.MediaColumns.DATA, albumArtFile.getPath());
contentResolver.insert(ALBUM_ART_URI, values);
Log.i(TAG, "Added album art: " + albumArtFile);
}
}
cursor.close();
}
// No album art found, add it.
File albumArtFile = FileUtil.getAlbumArtFile(context, downloadFile.getSong());
if (albumArtFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.AlbumColumns.ALBUM_ID, albumId);
values.put(MediaStore.MediaColumns.DATA, albumArtFile.getPath());
contentResolver.insert(ALBUM_ART_URI, values);
Log.i(TAG, "Added album art: " + albumArtFile);
}
}
cursor.close();
}
}

View File

@ -50,9 +50,9 @@ public interface MusicService {
MusicDirectory getMusicDirectory(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception;
MusicDirectory getArtist(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception;
MusicDirectory getArtist(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception;
MusicDirectory getAlbum(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception;
MusicDirectory getAlbum(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception;
SearchResult search(SearchCritera criteria, Context context, ProgressListener progressListener) throws Exception;
@ -62,40 +62,40 @@ public interface MusicService {
void createPlaylist(String id, String name, List<MusicDirectory.Entry> entries, Context context, ProgressListener progressListener) throws Exception;
void deletePlaylist(String id, Context context, ProgressListener progressListener) throws Exception;
void deletePlaylist(String id, Context context, ProgressListener progressListener) throws Exception;
void addToPlaylist(String id, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception;
void addToPlaylist(String id, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception;
void removeFromPlaylist(String id, List<Integer> toRemove, Context context, ProgressListener progressListener) throws Exception;
void removeFromPlaylist(String id, List<Integer> toRemove, Context context, ProgressListener progressListener) throws Exception;
void overwritePlaylist(String id, String name, int toRemove, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception;
void overwritePlaylist(String id, String name, int toRemove, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception;
void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception;
void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception;
MusicDirectory getAlbumList(String type, int size, int offset, boolean refresh, Context context, ProgressListener progressListener) throws Exception;
MusicDirectory getAlbumList(String type, String extra, int size, int offset, boolean refresh, Context context, ProgressListener progressListener) throws Exception;
MusicDirectory getAlbumList(String type, String extra, int size, int offset, boolean refresh, Context context, ProgressListener progressListener) throws Exception;
MusicDirectory getSongList(String type, int size, int offset, Context context, ProgressListener progressListener) throws Exception;
MusicDirectory getSongList(String type, int size, int offset, Context context, ProgressListener progressListener) throws Exception;
MusicDirectory getRandomSongs(int size, String artistId, Context context, ProgressListener progressListener) throws Exception;
MusicDirectory getRandomSongs(int size, String artistId, Context context, ProgressListener progressListener) throws Exception;
MusicDirectory getRandomSongs(int size, String folder, String genre, String startYear, String endYear, Context context, ProgressListener progressListener) throws Exception;
Bitmap getCoverArt(Context context, MusicDirectory.Entry entry, int size, ProgressListener progressListener, SilentBackgroundTask task) throws Exception;
Response getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, SilentBackgroundTask task) throws Exception;
List<Genre> getGenres(boolean refresh, Context context, ProgressListener progressListener) throws Exception;
List<Genre> getGenres(boolean refresh, Context context, ProgressListener progressListener) throws Exception;
MusicDirectory getSongsByGenre(String genre, int count, int offset, Context context, ProgressListener progressListener) throws Exception;
MusicDirectory getSongsByGenre(String genre, int count, int offset, Context context, ProgressListener progressListener) throws Exception;
User getUser(boolean refresh, String username, Context context, ProgressListener progressListener) throws Exception;
User getUser(boolean refresh, String username, Context context, ProgressListener progressListener) throws Exception;
Bitmap getBitmap(String url, int size, Context context, ProgressListener progressListener, SilentBackgroundTask task) throws Exception;
Bitmap getBitmap(String url, int size, Context context, ProgressListener progressListener, SilentBackgroundTask task) throws Exception;
void savePlayQueue(List<MusicDirectory.Entry> songs, MusicDirectory.Entry currentPlaying, int position, Context context, ProgressListener progressListener) throws Exception;
void savePlayQueue(List<MusicDirectory.Entry> songs, MusicDirectory.Entry currentPlaying, int position, Context context, ProgressListener progressListener) throws Exception;
PlayerQueue getPlayQueue(Context context, ProgressListener progressListener) throws Exception;
PlayerQueue getPlayQueue(Context context, ProgressListener progressListener) throws Exception;
void setInstance(Integer instance) throws Exception;
void setInstance(Integer instance) throws Exception;
}

View File

@ -62,19 +62,19 @@ import java.util.SortedSet;
* @author Sindre Mehus
*/
public class OfflineMusicService implements MusicService {
private static final String TAG = OfflineMusicService.class.getSimpleName();
private static final String ERRORMSG = "Not available in offline mode";
private static final Random random = new Random();
private static final String TAG = OfflineMusicService.class.getSimpleName();
private static final String ERRORMSG = "Not available in offline mode";
private static final Random random = new Random();
@Override
public void ping(Context context, ProgressListener progressListener) throws Exception {
@Override
public void ping(Context context, ProgressListener progressListener) throws Exception {
}
}
@Override
public Indexes getIndexes(String musicFolderId, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
List<Artist> artists = new ArrayList<Artist>();
List<Entry> entries = new ArrayList<>();
List<Entry> entries = new ArrayList<>();
File root = FileUtil.getMusicDirectory(context);
for (File file : FileUtil.listFiles(root)) {
if (file.isDirectory()) {
@ -84,47 +84,47 @@ public class OfflineMusicService implements MusicService {
artist.setName(file.getName());
artists.add(artist);
} else if(!file.getName().equals("albumart.jpg") && !file.getName().equals(".nomedia")) {
entries.add(createEntry(context, file));
}
entries.add(createEntry(context, file));
}
}
Indexes indexes = new Indexes(0L, Collections.<Artist>emptyList(), artists, entries);
return indexes;
return indexes;
}
@Override
public MusicDirectory getMusicDirectory(String id, String artistName, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
return getMusicDirectory(id, artistName, refresh, context, progressListener, false);
}
private MusicDirectory getMusicDirectory(String id, String artistName, boolean refresh, Context context, ProgressListener progressListener, boolean isPodcast) throws Exception {
File dir = new File(id);
MusicDirectory result = new MusicDirectory();
result.setName(dir.getName());
private MusicDirectory getMusicDirectory(String id, String artistName, boolean refresh, Context context, ProgressListener progressListener, boolean isPodcast) throws Exception {
File dir = new File(id);
MusicDirectory result = new MusicDirectory();
result.setName(dir.getName());
Set<String> names = new HashSet<String>();
Set<String> names = new HashSet<String>();
for (File file : FileUtil.listMediaFiles(dir)) {
String name = getName(file);
if (name != null & !names.contains(name)) {
names.add(name);
result.addChild(createEntry(context, file, name, true, isPodcast));
}
}
result.sortChildren(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_CUSTOM_SORT_ENABLED, true));
return result;
}
for (File file : FileUtil.listMediaFiles(dir)) {
String name = getName(file);
if (name != null & !names.contains(name)) {
names.add(name);
result.addChild(createEntry(context, file, name, true, isPodcast));
}
}
result.sortChildren(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_CUSTOM_SORT_ENABLED, true));
return result;
}
@Override
public MusicDirectory getArtist(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public MusicDirectory getArtist(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public MusicDirectory getAlbum(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public MusicDirectory getAlbum(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
private String getName(File file) {
private String getName(File file) {
String name = file.getName();
if (file.isDirectory()) {
return name;
@ -138,401 +138,401 @@ public class OfflineMusicService implements MusicService {
return FileUtil.getBaseName(name);
}
private Entry createEntry(Context context, File file) {
return createEntry(context, file, getName(file));
}
private Entry createEntry(Context context, File file, String name) {
return createEntry(context, file, name, true);
}
private Entry createEntry(Context context, File file) {
return createEntry(context, file, getName(file));
}
private Entry createEntry(Context context, File file, String name) {
return createEntry(context, file, name, true);
}
private Entry createEntry(Context context, File file, String name, boolean load) {
return createEntry(context, file, name, load, false);
}
private Entry createEntry(Context context, File file, String name, boolean load, boolean isPodcast) {
Entry entry;
private Entry createEntry(Context context, File file, String name, boolean load, boolean isPodcast) {
Entry entry;
entry = new Entry();
entry.setDirectory(file.isDirectory());
entry.setId(file.getPath());
entry.setParent(file.getParent());
entry.setSize(file.length());
String root = FileUtil.getMusicDirectory(context).getPath();
if(!file.getParentFile().getParentFile().getPath().equals(root)) {
entry.setGrandParent(file.getParentFile().getParent());
}
entry.setPath(file.getPath().replaceFirst("^" + root + "/" , ""));
String title = name;
if (file.isFile()) {
File artistFolder = file.getParentFile().getParentFile();
File albumFolder = file.getParentFile();
if(artistFolder.getPath().equals(root)) {
entry.setArtist(albumFolder.getName());
} else {
entry.setArtist(artistFolder.getName());
}
entry.setAlbum(albumFolder.getName());
entry.setDirectory(file.isDirectory());
entry.setId(file.getPath());
entry.setParent(file.getParent());
entry.setSize(file.length());
String root = FileUtil.getMusicDirectory(context).getPath();
if(!file.getParentFile().getParentFile().getPath().equals(root)) {
entry.setGrandParent(file.getParentFile().getParent());
}
entry.setPath(file.getPath().replaceFirst("^" + root + "/" , ""));
String title = name;
if (file.isFile()) {
File artistFolder = file.getParentFile().getParentFile();
File albumFolder = file.getParentFile();
if(artistFolder.getPath().equals(root)) {
entry.setArtist(albumFolder.getName());
} else {
entry.setArtist(artistFolder.getName());
}
entry.setAlbum(albumFolder.getName());
int index = name.indexOf('-');
if(index != -1) {
try {
entry.setTrack(Integer.parseInt(name.substring(0, index)));
title = title.substring(index + 1);
} catch(Exception e) {
// Failed parseInt, just means track filled out
}
}
int index = name.indexOf('-');
if(index != -1) {
try {
entry.setTrack(Integer.parseInt(name.substring(0, index)));
title = title.substring(index + 1);
} catch(Exception e) {
// Failed parseInt, just means track filled out
}
}
if(load) {
entry.loadMetadata(file);
}
}
entry.setTitle(title);
entry.setSuffix(FileUtil.getExtension(file.getName().replace(".complete", "")));
File albumArt = FileUtil.getAlbumArtFile(context, entry);
if (albumArt.exists()) {
entry.setCoverArt(albumArt.getPath());
}
return entry;
}
@Override
public Bitmap getCoverArt(Context context, Entry entry, int size, ProgressListener progressListener, SilentBackgroundTask task) throws Exception {
try {
return FileUtil.getAlbumArtBitmap(context, entry, size);
} catch(Exception e) {
return null;
}
}
@Override
public Response getDownloadInputStream(Context context, Entry song, long offset, int maxBitrate, SilentBackgroundTask task) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public List<MusicFolder> getMusicFolders(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public SearchResult search(SearchCritera criteria, Context context, ProgressListener progressListener) throws Exception {
List<Artist> artists = new ArrayList<Artist>();
List<Entry> albums = new ArrayList<Entry>();
List<Entry> songs = new ArrayList<Entry>();
File root = FileUtil.getMusicDirectory(context);
int closeness = 0;
for (File artistFile : FileUtil.listFiles(root)) {
String artistName = artistFile.getName();
if (artistFile.isDirectory()) {
if((closeness = matchCriteria(criteria, artistName)) > 0) {
Artist artist = new Artist();
artist.setId(artistFile.getPath());
artist.setIndex(artistFile.getName().substring(0, 1));
artist.setName(artistName);
artist.setCloseness(closeness);
artists.add(artist);
}
recursiveAlbumSearch(artistName, artistFile, criteria, context, albums, songs);
if(load) {
entry.loadMetadata(file);
}
}
Collections.sort(artists, new Comparator<Artist>() {
public int compare(Artist lhs, Artist rhs) {
if(lhs.getCloseness() == rhs.getCloseness()) {
return 0;
}
else if(lhs.getCloseness() > rhs.getCloseness()) {
return -1;
}
else {
return 1;
}
}
});
Collections.sort(albums, new Comparator<Entry>() {
public int compare(Entry lhs, Entry rhs) {
if(lhs.getCloseness() == rhs.getCloseness()) {
return 0;
}
else if(lhs.getCloseness() > rhs.getCloseness()) {
return -1;
}
else {
return 1;
}
}
});
Collections.sort(songs, new Comparator<Entry>() {
public int compare(Entry lhs, Entry rhs) {
if(lhs.getCloseness() == rhs.getCloseness()) {
return 0;
}
else if(lhs.getCloseness() > rhs.getCloseness()) {
return -1;
}
else {
return 1;
}
}
});
entry.setTitle(title);
entry.setSuffix(FileUtil.getExtension(file.getName().replace(".complete", "")));
// Respect counts in search criteria
int artistCount = Math.min(artists.size(), criteria.getArtistCount());
int albumCount = Math.min(albums.size(), criteria.getAlbumCount());
int songCount = Math.min(songs.size(), criteria.getSongCount());
artists = artists.subList(0, artistCount);
albums = albums.subList(0, albumCount);
songs = songs.subList(0, songCount);
return new SearchResult(artists, albums, songs);
File albumArt = FileUtil.getAlbumArtFile(context, entry);
if (albumArt.exists()) {
entry.setCoverArt(albumArt.getPath());
}
return entry;
}
private void recursiveAlbumSearch(String artistName, File file, SearchCritera criteria, Context context, List<Entry> albums, List<Entry> songs) {
int closeness;
for(File albumFile : FileUtil.listMediaFiles(file)) {
if(albumFile.isDirectory()) {
String albumName = getName(albumFile);
if((closeness = matchCriteria(criteria, albumName)) > 0) {
Entry album = createEntry(context, albumFile, albumName);
album.setArtist(artistName);
album.setCloseness(closeness);
albums.add(album);
}
@Override
public Bitmap getCoverArt(Context context, Entry entry, int size, ProgressListener progressListener, SilentBackgroundTask task) throws Exception {
try {
return FileUtil.getAlbumArtBitmap(context, entry, size);
} catch(Exception e) {
return null;
}
}
for(File songFile : FileUtil.listMediaFiles(albumFile)) {
String songName = getName(songFile);
if(songName == null) {
continue;
}
@Override
public Response getDownloadInputStream(Context context, Entry song, long offset, int maxBitrate, SilentBackgroundTask task) throws Exception {
throw new OfflineException(ERRORMSG);
}
if(songFile.isDirectory()) {
recursiveAlbumSearch(artistName, songFile, criteria, context, albums, songs);
}
else if((closeness = matchCriteria(criteria, songName)) > 0){
Entry song = createEntry(context, albumFile, songName);
song.setArtist(artistName);
song.setAlbum(albumName);
song.setCloseness(closeness);
songs.add(song);
}
}
}
else {
String songName = getName(albumFile);
if((closeness = matchCriteria(criteria, songName)) > 0) {
Entry song = createEntry(context, albumFile, songName);
song.setArtist(artistName);
song.setAlbum(songName);
song.setCloseness(closeness);
songs.add(song);
}
}
}
}
private int matchCriteria(SearchCritera criteria, String name) {
if (criteria.getPattern().matcher(name).matches()) {
return Util.getStringDistance(
criteria.getQuery().toLowerCase(),
name.toLowerCase());
} else {
return 0;
}
}
@Override
public List<MusicFolder> getMusicFolders(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public SearchResult search(SearchCritera criteria, Context context, ProgressListener progressListener) throws Exception {
List<Artist> artists = new ArrayList<Artist>();
List<Entry> albums = new ArrayList<Entry>();
List<Entry> songs = new ArrayList<Entry>();
File root = FileUtil.getMusicDirectory(context);
int closeness = 0;
for (File artistFile : FileUtil.listFiles(root)) {
String artistName = artistFile.getName();
if (artistFile.isDirectory()) {
if((closeness = matchCriteria(criteria, artistName)) > 0) {
Artist artist = new Artist();
artist.setId(artistFile.getPath());
artist.setIndex(artistFile.getName().substring(0, 1));
artist.setName(artistName);
artist.setCloseness(closeness);
artists.add(artist);
}
recursiveAlbumSearch(artistName, artistFile, criteria, context, albums, songs);
}
}
Collections.sort(artists, new Comparator<Artist>() {
public int compare(Artist lhs, Artist rhs) {
if(lhs.getCloseness() == rhs.getCloseness()) {
return 0;
}
else if(lhs.getCloseness() > rhs.getCloseness()) {
return -1;
}
else {
return 1;
}
}
});
Collections.sort(albums, new Comparator<Entry>() {
public int compare(Entry lhs, Entry rhs) {
if(lhs.getCloseness() == rhs.getCloseness()) {
return 0;
}
else if(lhs.getCloseness() > rhs.getCloseness()) {
return -1;
}
else {
return 1;
}
}
});
Collections.sort(songs, new Comparator<Entry>() {
public int compare(Entry lhs, Entry rhs) {
if(lhs.getCloseness() == rhs.getCloseness()) {
return 0;
}
else if(lhs.getCloseness() > rhs.getCloseness()) {
return -1;
}
else {
return 1;
}
}
});
// Respect counts in search criteria
int artistCount = Math.min(artists.size(), criteria.getArtistCount());
int albumCount = Math.min(albums.size(), criteria.getAlbumCount());
int songCount = Math.min(songs.size(), criteria.getSongCount());
artists = artists.subList(0, artistCount);
albums = albums.subList(0, albumCount);
songs = songs.subList(0, songCount);
return new SearchResult(artists, albums, songs);
}
private void recursiveAlbumSearch(String artistName, File file, SearchCritera criteria, Context context, List<Entry> albums, List<Entry> songs) {
int closeness;
for(File albumFile : FileUtil.listMediaFiles(file)) {
if(albumFile.isDirectory()) {
String albumName = getName(albumFile);
if((closeness = matchCriteria(criteria, albumName)) > 0) {
Entry album = createEntry(context, albumFile, albumName);
album.setArtist(artistName);
album.setCloseness(closeness);
albums.add(album);
}
for(File songFile : FileUtil.listMediaFiles(albumFile)) {
String songName = getName(songFile);
if(songName == null) {
continue;
}
if(songFile.isDirectory()) {
recursiveAlbumSearch(artistName, songFile, criteria, context, albums, songs);
}
else if((closeness = matchCriteria(criteria, songName)) > 0){
Entry song = createEntry(context, albumFile, songName);
song.setArtist(artistName);
song.setAlbum(albumName);
song.setCloseness(closeness);
songs.add(song);
}
}
}
else {
String songName = getName(albumFile);
if((closeness = matchCriteria(criteria, songName)) > 0) {
Entry song = createEntry(context, albumFile, songName);
song.setArtist(artistName);
song.setAlbum(songName);
song.setCloseness(closeness);
songs.add(song);
}
}
}
}
private int matchCriteria(SearchCritera criteria, String name) {
if (criteria.getPattern().matcher(name).matches()) {
return Util.getStringDistance(
criteria.getQuery().toLowerCase(),
name.toLowerCase());
} else {
return 0;
}
}
@Override
public List<Playlist> getPlaylists(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
List<Playlist> playlists = new ArrayList<Playlist>();
File root = FileUtil.getPlaylistDirectory(context);
String lastServer = null;
boolean removeServer = true;
String lastServer = null;
boolean removeServer = true;
for (File folder : FileUtil.listFiles(root)) {
if(folder.isDirectory()) {
String server = folder.getName();
SortedSet<File> fileList = FileUtil.listFiles(folder);
for(File file: fileList) {
if(FileUtil.isPlaylistFile(file)) {
String id = file.getName();
String filename = FileUtil.getBaseName(id);
String name = server + ": " + filename;
Playlist playlist = new Playlist(server, name);
playlist.setComment(filename);
if(folder.isDirectory()) {
String server = folder.getName();
SortedSet<File> fileList = FileUtil.listFiles(folder);
for(File file: fileList) {
if(FileUtil.isPlaylistFile(file)) {
String id = file.getName();
String filename = FileUtil.getBaseName(id);
String name = server + ": " + filename;
Playlist playlist = new Playlist(server, name);
playlist.setComment(filename);
Reader reader = null;
BufferedReader buffer = null;
int songCount = 0;
try {
reader = new FileReader(file);
buffer = new BufferedReader(reader);
Reader reader = null;
BufferedReader buffer = null;
int songCount = 0;
try {
reader = new FileReader(file);
buffer = new BufferedReader(reader);
String line = buffer.readLine();
while( (line = buffer.readLine()) != null ){
// No matter what, end file can't have .complete in it
line = line.replace(".complete", "");
File entryFile = new File(line);
String line = buffer.readLine();
while( (line = buffer.readLine()) != null ){
// No matter what, end file can't have .complete in it
line = line.replace(".complete", "");
File entryFile = new File(line);
// Don't add file to playlist if it doesn't exist as cached or pinned!
File checkFile = entryFile;
if(!checkFile.exists()) {
// If normal file doens't exist, check if .complete version does
checkFile = new File(entryFile.getParent(), FileUtil.getBaseName(entryFile.getName())
+ ".complete." + FileUtil.getExtension(entryFile.getName()));
}
// Don't add file to playlist if it doesn't exist as cached or pinned!
File checkFile = entryFile;
if(!checkFile.exists()) {
// If normal file doens't exist, check if .complete version does
checkFile = new File(entryFile.getParent(), FileUtil.getBaseName(entryFile.getName())
+ ".complete." + FileUtil.getExtension(entryFile.getName()));
}
String entryName = getName(entryFile);
if(checkFile.exists() && entryName != null){
songCount++;
}
}
String entryName = getName(entryFile);
if(checkFile.exists() && entryName != null){
songCount++;
}
}
playlist.setSongCount(Integer.toString(songCount));
} catch(Exception e) {
Log.w(TAG, "Failed to count songs in playlist", e);
} finally {
Util.close(buffer);
Util.close(reader);
}
playlist.setSongCount(Integer.toString(songCount));
} catch(Exception e) {
Log.w(TAG, "Failed to count songs in playlist", e);
} finally {
Util.close(buffer);
Util.close(reader);
}
if(songCount > 0) {
playlists.add(playlist);
}
}
}
if(songCount > 0) {
playlists.add(playlist);
}
}
}
if(!server.equals(lastServer) && fileList.size() > 0) {
if(lastServer != null) {
removeServer = false;
}
lastServer = server;
}
} else {
// Delete legacy playlist files
try {
folder.delete();
} catch(Exception e) {
Log.w(TAG, "Failed to delete old playlist file: " + folder.getName());
}
}
if(!server.equals(lastServer) && fileList.size() > 0) {
if(lastServer != null) {
removeServer = false;
}
lastServer = server;
}
} else {
// Delete legacy playlist files
try {
folder.delete();
} catch(Exception e) {
Log.w(TAG, "Failed to delete old playlist file: " + folder.getName());
}
}
}
if(removeServer) {
for(Playlist playlist: playlists) {
playlist.setName(playlist.getName().substring(playlist.getId().length() + 2));
}
}
if(removeServer) {
for(Playlist playlist: playlists) {
playlist.setName(playlist.getName().substring(playlist.getId().length() + 2));
}
}
return playlists;
}
@Override
public MusicDirectory getPlaylist(boolean refresh, String id, String name, Context context, ProgressListener progressListener) throws Exception {
DownloadService downloadService = DownloadService.getInstance();
DownloadService downloadService = DownloadService.getInstance();
if (downloadService == null) {
return new MusicDirectory();
}
Reader reader = null;
BufferedReader buffer = null;
try {
int firstIndex = name.indexOf(id);
if(firstIndex != -1) {
name = name.substring(id.length() + 2);
}
BufferedReader buffer = null;
try {
int firstIndex = name.indexOf(id);
if(firstIndex != -1) {
name = name.substring(id.length() + 2);
}
File playlistFile = FileUtil.getPlaylistFile(context, id, name);
reader = new FileReader(playlistFile);
buffer = new BufferedReader(reader);
File playlistFile = FileUtil.getPlaylistFile(context, id, name);
reader = new FileReader(playlistFile);
buffer = new BufferedReader(reader);
MusicDirectory playlist = new MusicDirectory();
String line = buffer.readLine();
if(!"#EXTM3U".equals(line)) return playlist;
MusicDirectory playlist = new MusicDirectory();
String line = buffer.readLine();
if(!"#EXTM3U".equals(line)) return playlist;
while( (line = buffer.readLine()) != null ){
// No matter what, end file can't have .complete in it
line = line.replace(".complete", "");
File entryFile = new File(line);
while( (line = buffer.readLine()) != null ){
// No matter what, end file can't have .complete in it
line = line.replace(".complete", "");
File entryFile = new File(line);
// Don't add file to playlist if it doesn't exist as cached or pinned!
File checkFile = entryFile;
if(!checkFile.exists()) {
// If normal file doens't exist, check if .complete version does
checkFile = new File(entryFile.getParent(), FileUtil.getBaseName(entryFile.getName())
+ ".complete." + FileUtil.getExtension(entryFile.getName()));
}
// Don't add file to playlist if it doesn't exist as cached or pinned!
File checkFile = entryFile;
if(!checkFile.exists()) {
// If normal file doens't exist, check if .complete version does
checkFile = new File(entryFile.getParent(), FileUtil.getBaseName(entryFile.getName())
+ ".complete." + FileUtil.getExtension(entryFile.getName()));
}
String entryName = getName(entryFile);
if(checkFile.exists() && entryName != null){
playlist.addChild(createEntry(context, entryFile, entryName, false));
}
}
String entryName = getName(entryFile);
if(checkFile.exists() && entryName != null){
playlist.addChild(createEntry(context, entryFile, entryName, false));
}
}
return playlist;
} finally {
Util.close(buffer);
Util.close(reader);
}
return playlist;
} finally {
Util.close(buffer);
Util.close(reader);
}
}
@Override
public void createPlaylist(String id, String name, List<Entry> entries, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
throw new OfflineException(ERRORMSG);
}
@Override
public void deletePlaylist(String id, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public void deletePlaylist(String id, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public void addToPlaylist(String id, List<Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public void addToPlaylist(String id, List<Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public void removeFromPlaylist(String id, List<Integer> toRemove, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public void removeFromPlaylist(String id, List<Integer> toRemove, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public void overwritePlaylist(String id, String name, int toRemove, List<Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public void overwritePlaylist(String id, String name, int toRemove, List<Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public MusicDirectory getAlbumList(String type, int size, int offset, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
throw new OfflineException(ERRORMSG);
}
@Override
public MusicDirectory getAlbumList(String type, String extra, int size, int offset, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public MusicDirectory getAlbumList(String type, String extra, int size, int offset, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public MusicDirectory getSongList(String type, int size, int offset, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public MusicDirectory getSongList(String type, int size, int offset, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public MusicDirectory getRandomSongs(int size, String artistId, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public MusicDirectory getRandomSongs(int size, String artistId, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public List<Genre> getGenres(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public List<Genre> getGenres(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public MusicDirectory getSongsByGenre(String genre, int count, int offset, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public MusicDirectory getSongsByGenre(String genre, int count, int offset, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
@Override
public MusicDirectory getRandomSongs(int size, String folder, String genre, String startYear, String endYear, Context context, ProgressListener progressListener) throws Exception {
File root = FileUtil.getMusicDirectory(context);
List<File> children = new LinkedList<File>();
@ -550,29 +550,29 @@ public class OfflineMusicService implements MusicService {
return result;
}
@Override
public User getUser(boolean refresh, String username, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public User getUser(boolean refresh, String username, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public Bitmap getBitmap(String url, int size, Context context, ProgressListener progressListener, SilentBackgroundTask task) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public Bitmap getBitmap(String url, int size, Context context, ProgressListener progressListener, SilentBackgroundTask task) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public void savePlayQueue(List<Entry> songs, Entry currentPlaying, int position, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public void savePlayQueue(List<Entry> songs, Entry currentPlaying, int position, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public PlayerQueue getPlayQueue(Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public PlayerQueue getPlayQueue(Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public void setInstance(Integer instance) throws Exception{
throw new OfflineException(ERRORMSG);
throw new OfflineException(ERRORMSG);
}
private void listFilesRecursively(File parent, List<File> children) {

View File

@ -45,10 +45,10 @@ public class EntryListParser extends MusicDirectoryEntryParser {
if (eventType == XmlPullParser.START_TAG) {
String name = getElementName();
if ("album".equals(name)) {
MusicDirectory.Entry entry = parseEntry("");
if(get("isDir") == null) {
entry.setDirectory(true);
}
MusicDirectory.Entry entry = parseEntry("");
if(get("isDir") == null) {
entry.setDirectory(true);
}
dir.addChild(entry);
} else if ("song".equals(name)) {
MusicDirectory.Entry entry = parseEntry("");

View File

@ -29,8 +29,8 @@ import java.io.InputStream;
public class ErrorParser extends AbstractParser {
public ErrorParser(Context context, int instance) {
super(context, instance);
}
super(context, instance);
}
public void parse(InputStream inputStream) throws Exception {

View File

@ -53,13 +53,13 @@ public class IndexesParser extends MusicDirectoryEntryParser {
List<Artist> artists = new ArrayList<Artist>();
List<Artist> shortcuts = new ArrayList<Artist>();
List<MusicDirectory.Entry> entries = new ArrayList<MusicDirectory.Entry>();
List<MusicDirectory.Entry> entries = new ArrayList<MusicDirectory.Entry>();
Long lastModified = null;
int eventType;
String index = "#";
String ignoredArticles = null;
String ignoredArticles = null;
boolean changed = false;
Map<String, Artist> artistList = new HashMap<String, Artist>();
Map<String, Artist> artistList = new HashMap<String, Artist>();
do {
eventType = nextParseEvent();
@ -68,7 +68,7 @@ public class IndexesParser extends MusicDirectoryEntryParser {
if ("indexes".equals(name) || "artists".equals(name)) {
changed = true;
lastModified = getLong("lastModified");
ignoredArticles = get("ignoredArticles");
ignoredArticles = get("ignoredArticles");
} else if ("index".equals(name)) {
index = get("name");
@ -78,14 +78,14 @@ public class IndexesParser extends MusicDirectoryEntryParser {
artist.setName(get("name"));
artist.setIndex(index);
// Combine the id's for the two artists
if(artistList.containsKey(artist.getName())) {
Artist originalArtist = artistList.get(artist.getName());
originalArtist.setId(originalArtist.getId() + ";" + artist.getId());
} else {
artistList.put(artist.getName(), artist);
artists.add(artist);
}
// Combine the id's for the two artists
if(artistList.containsKey(artist.getName())) {
Artist originalArtist = artistList.get(artist.getName());
originalArtist.setId(originalArtist.getId() + ";" + artist.getId());
} else {
artistList.put(artist.getName(), artist);
artists.add(artist);
}
if (artists.size() % 10 == 0) {
String msg = getContext().getResources().getString(R.string.parser_artist_count, artists.size());
@ -97,10 +97,10 @@ public class IndexesParser extends MusicDirectoryEntryParser {
shortcut.setName(get("name"));
shortcut.setIndex("*");
shortcuts.add(shortcut);
} else if("child".equals(name)) {
MusicDirectory.Entry entry = parseEntry("");
entries.add(entry);
} else if ("error".equals(name)) {
} else if("child".equals(name)) {
MusicDirectory.Entry entry = parseEntry("");
entries.add(entry);
} else if ("error".equals(name)) {
handleError();
}
}
@ -108,11 +108,11 @@ public class IndexesParser extends MusicDirectoryEntryParser {
validate();
if(ignoredArticles != null) {
SharedPreferences.Editor prefs = Util.getPreferences(context).edit();
prefs.putString(Constants.CACHE_KEY_IGNORE, ignoredArticles);
prefs.apply();
}
if(ignoredArticles != null) {
SharedPreferences.Editor prefs = Util.getPreferences(context).edit();
prefs.putString(Constants.CACHE_KEY_IGNORE, ignoredArticles);
prefs.apply();
}
if (!changed) {
return null;

View File

@ -33,21 +33,21 @@ public class MusicDirectoryEntryParser extends AbstractParser {
protected MusicDirectory.Entry parseEntry(String artist) {
MusicDirectory.Entry entry = new MusicDirectory.Entry();
entry.setId(get("id"));
entry.setParent(get("parent"));
entry.setArtistId(get("artistId"));
entry.setParent(get("parent"));
entry.setArtistId(get("artistId"));
entry.setTitle(get("title"));
if(entry.getTitle() == null) {
entry.setTitle(get("name"));
}
if(entry.getTitle() == null) {
entry.setTitle(get("name"));
}
entry.setDirectory(getBoolean("isDir"));
entry.setCoverArt(get("coverArt"));
entry.setArtist(get("artist"));
entry.setYear(getInteger("year"));
entry.setGenre(get("genre"));
entry.setAlbum(get("album"));
entry.setAlbum(get("album"));
if (!entry.isDirectory()) {
entry.setAlbumId(get("albumId"));
entry.setAlbumId(get("albumId"));
entry.setTrack(getInteger("track"));
entry.setContentType(get("contentType"));
entry.setSuffix(get("suffix"));
@ -57,23 +57,23 @@ public class MusicDirectoryEntryParser extends AbstractParser {
entry.setDuration(getInteger("duration"));
entry.setBitRate(getInteger("bitRate"));
entry.setPath(get("path"));
entry.setDiscNumber(getInteger("discNumber"));
entry.setDiscNumber(getInteger("discNumber"));
String type = get("type");
String type = get("type");
} else if(!"".equals(artist)) {
entry.setPath(artist + "/" + entry.getTitle());
}
entry.setPath(artist + "/" + entry.getTitle());
}
return entry;
}
protected MusicDirectory.Entry parseArtist() {
MusicDirectory.Entry entry = new MusicDirectory.Entry();
protected MusicDirectory.Entry parseArtist() {
MusicDirectory.Entry entry = new MusicDirectory.Entry();
entry.setId(get("id"));
entry.setTitle(get("name"));
entry.setPath(entry.getTitle());
entry.setDirectory(true);
entry.setId(get("id"));
entry.setTitle(get("name"));
entry.setPath(entry.getTitle());
entry.setDirectory(true);
return entry;
}
return entry;
}
}

View File

@ -49,52 +49,52 @@ public class MusicDirectoryParser extends MusicDirectoryEntryParser {
MusicDirectory dir = new MusicDirectory();
int eventType;
boolean isArtist = false;
Map<String, Entry> titleMap = new HashMap<String, Entry>();
boolean isArtist = false;
Map<String, Entry> titleMap = new HashMap<String, Entry>();
do {
eventType = nextParseEvent();
if (eventType == XmlPullParser.START_TAG) {
String name = getElementName();
if ("child".equals(name) || "song".equals(name)) {
Entry entry = parseEntry(artist);
entry.setGrandParent(dir.getParent());
Entry entry = parseEntry(artist);
entry.setGrandParent(dir.getParent());
// Only check for songs
if(!entry.isDirectory()) {
// Check if duplicates
String disc = (entry.getDiscNumber() != null) ? Integer.toString(entry.getDiscNumber()) : "";
String track = (entry.getTrack() != null) ? Integer.toString(entry.getTrack()) : "";
String duplicateId = disc + "-" + track + "-" + entry.getTitle();
// Only check for songs
if(!entry.isDirectory()) {
// Check if duplicates
String disc = (entry.getDiscNumber() != null) ? Integer.toString(entry.getDiscNumber()) : "";
String track = (entry.getTrack() != null) ? Integer.toString(entry.getTrack()) : "";
String duplicateId = disc + "-" + track + "-" + entry.getTitle();
Entry duplicate = titleMap.get(duplicateId);
if (duplicate != null) {
// Check if the first already has been rebased or not
if (duplicate.getTitle().equals(entry.getTitle())) {
duplicate.rebaseTitleOffPath();
}
Entry duplicate = titleMap.get(duplicateId);
if (duplicate != null) {
// Check if the first already has been rebased or not
if (duplicate.getTitle().equals(entry.getTitle())) {
duplicate.rebaseTitleOffPath();
}
// Rebase if this is the second instance of this title found
entry.rebaseTitleOffPath();
} else {
titleMap.put(duplicateId, entry);
}
}
// Rebase if this is the second instance of this title found
entry.rebaseTitleOffPath();
} else {
titleMap.put(duplicateId, entry);
}
}
dir.addChild(entry);
} else if ("directory".equals(name) || "artist".equals(name) || ("album".equals(name) && !isArtist)) {
dir.setName(get("name"));
dir.setId(get("id"));
if(Util.isTagBrowsing(context, instance)) {
dir.setParent(get("artistId"));
} else {
dir.setParent(get("parent"));
}
isArtist = true;
dir.setId(get("id"));
if(Util.isTagBrowsing(context, instance)) {
dir.setParent(get("artistId"));
} else {
dir.setParent(get("parent"));
}
isArtist = true;
} else if("album".equals(name)) {
Entry entry = parseEntry(artist);
entry.setDirectory(true);
dir.addChild(entry);
} else if ("error".equals(name)) {
Entry entry = parseEntry(artist);
entry.setDirectory(true);
dir.addChild(entry);
} else if ("error".equals(name)) {
handleError();
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.service.parser;
@ -30,54 +30,54 @@ import net.nullsum.audinaut.domain.PlayerQueue;
import net.nullsum.audinaut.util.ProgressListener;
public class PlayQueueParser extends MusicDirectoryEntryParser {
private static final String TAG = PlayQueueParser.class.getSimpleName();
private static final String TAG = PlayQueueParser.class.getSimpleName();
public PlayQueueParser(Context context, int instance) {
super(context, instance);
}
public PlayQueueParser(Context context, int instance) {
super(context, instance);
}
public PlayerQueue parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
init(inputStream);
public PlayerQueue parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
init(inputStream);
PlayerQueue state = new PlayerQueue();
String currentId = null;
int eventType;
do {
eventType = nextParseEvent();
if (eventType == XmlPullParser.START_TAG) {
String name = getElementName();
if("playQueue".equals(name)) {
currentId = get("current");
state.currentPlayingPosition = getInteger("position");
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
state.changed = dateFormat.parse(get("changed"));
} catch (ParseException e) {
state.changed = null;
}
} else if ("entry".equals(name)) {
MusicDirectory.Entry entry = parseEntry("");
// Only add songs
PlayerQueue state = new PlayerQueue();
String currentId = null;
int eventType;
do {
eventType = nextParseEvent();
if (eventType == XmlPullParser.START_TAG) {
String name = getElementName();
if("playQueue".equals(name)) {
currentId = get("current");
state.currentPlayingPosition = getInteger("position");
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
state.changed = dateFormat.parse(get("changed"));
} catch (ParseException e) {
state.changed = null;
}
} else if ("entry".equals(name)) {
MusicDirectory.Entry entry = parseEntry("");
// Only add songs
state.songs.add(entry);
} else if ("error".equals(name)) {
handleError();
}
}
} while (eventType != XmlPullParser.END_DOCUMENT);
} else if ("error".equals(name)) {
handleError();
}
}
} while (eventType != XmlPullParser.END_DOCUMENT);
if(currentId != null) {
for (MusicDirectory.Entry entry : state.songs) {
if (entry.getId().equals(currentId)) {
state.currentPlayingIndex = state.songs.indexOf(entry);
}
}
} else {
state.currentPlayingIndex = 0;
state.currentPlayingPosition = 0;
}
if(currentId != null) {
for (MusicDirectory.Entry entry : state.songs) {
if (entry.getId().equals(currentId)) {
state.currentPlayingIndex = state.songs.indexOf(entry);
}
}
} else {
state.currentPlayingIndex = 0;
state.currentPlayingPosition = 0;
}
validate();
return state;
}
validate();
return state;
}
}

View File

@ -32,8 +32,8 @@ import java.io.InputStream;
public class PlaylistParser extends MusicDirectoryEntryParser {
public PlaylistParser(Context context, int instance) {
super(context, instance);
}
super(context, instance);
}
public MusicDirectory parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
init(inputStream);
@ -49,9 +49,9 @@ public class PlaylistParser extends MusicDirectoryEntryParser {
} else if ("error".equals(name)) {
handleError();
} else if ("playlist".equals(name)) {
dir.setName(get("name"));
dir.setId(get("id"));
}
dir.setName(get("name"));
dir.setId(get("id"));
}
}
} while (eventType != XmlPullParser.END_DOCUMENT);

View File

@ -34,8 +34,8 @@ import java.util.List;
public class PlaylistsParser extends AbstractParser {
public PlaylistsParser(Context context, int instance) {
super(context, instance);
}
super(context, instance);
}
public List<Playlist> parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
init(inputStream);
@ -49,13 +49,13 @@ public class PlaylistsParser extends AbstractParser {
if ("playlist".equals(tag)) {
String id = get("id");
String name = get("name");
String owner = get("owner");
String comment = get("comment");
String songCount = get("songCount");
String pub = get("public");
String created = get("created");
String changed = get("changed");
Integer duration = getInteger("duration");
String owner = get("owner");
String comment = get("comment");
String songCount = get("songCount");
String pub = get("public");
String created = get("created");
String changed = get("changed");
Integer duration = getInteger("duration");
result.add(new Playlist(id, name, owner, comment, songCount, pub, created, changed, duration));
} else if ("error".equals(tag)) {
handleError();

View File

@ -32,8 +32,8 @@ import java.io.InputStream;
public class RandomSongsParser extends MusicDirectoryEntryParser {
public RandomSongsParser(Context context, int instance) {
super(context, instance);
}
super(context, instance);
}
public MusicDirectory parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
init(inputStream);

View File

@ -36,8 +36,8 @@ import java.util.ArrayList;
public class SearchResult2Parser extends MusicDirectoryEntryParser {
public SearchResult2Parser(Context context, int instance) {
super(context, instance);
}
super(context, instance);
}
public SearchResult parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
init(inputStream);
@ -56,8 +56,8 @@ public class SearchResult2Parser extends MusicDirectoryEntryParser {
artist.setName(get("name"));
artists.add(artist);
} else if ("album".equals(name)) {
MusicDirectory.Entry entry = parseEntry("");
entry.setDirectory(true);
MusicDirectory.Entry entry = parseEntry("");
entry.setDirectory(true);
albums.add(entry);
} else if ("song".equals(name)) {
songs.add(parseEntry(""));

View File

@ -1,16 +1,16 @@
/*
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 2014 (C) Scott Jackson
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 2014 (C) Scott Jackson
*/
package net.nullsum.audinaut.service.parser;
@ -33,76 +33,76 @@ import net.nullsum.audinaut.service.MusicServiceFactory;
import net.nullsum.audinaut.util.ProgressListener;
public class UserParser extends AbstractParser {
private static final String TAG = UserParser.class.getSimpleName();
private static final String TAG = UserParser.class.getSimpleName();
public UserParser(Context context, int instance) {
super(context, instance);
}
public UserParser(Context context, int instance) {
super(context, instance);
}
public List<User> parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
init(inputStream);
List<User> result = new ArrayList<User>();
List<MusicFolder> musicFolders = null;
User user = null;
int eventType;
public List<User> parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
init(inputStream);
List<User> result = new ArrayList<User>();
List<MusicFolder> musicFolders = null;
User user = null;
int eventType;
String tagName = null;
do {
eventType = nextParseEvent();
if (eventType == XmlPullParser.START_TAG) {
tagName = getElementName();
if ("user".equals(tagName)) {
user = new User();
String tagName = null;
do {
eventType = nextParseEvent();
if (eventType == XmlPullParser.START_TAG) {
tagName = getElementName();
if ("user".equals(tagName)) {
user = new User();
user.setUsername(get("username"));
user.setEmail(get("email"));
for(String role: User.ROLES) {
parseSetting(user, role);
}
user.setUsername(get("username"));
user.setEmail(get("email"));
for(String role: User.ROLES) {
parseSetting(user, role);
}
result.add(user);
} else if ("error".equals(tagName)) {
handleError();
}
} else if(eventType == XmlPullParser.TEXT) {
if("folder".equals(tagName)) {
String id = getText();
if(musicFolders == null) {
musicFolders = getMusicFolders();
}
result.add(user);
} else if ("error".equals(tagName)) {
handleError();
}
} else if(eventType == XmlPullParser.TEXT) {
if("folder".equals(tagName)) {
String id = getText();
if(musicFolders == null) {
musicFolders = getMusicFolders();
}
if(user != null) {
if(user.getMusicFolderSettings() == null) {
for (MusicFolder musicFolder : musicFolders) {
user.addMusicFolder(musicFolder);
}
}
if(user != null) {
if(user.getMusicFolderSettings() == null) {
for (MusicFolder musicFolder : musicFolders) {
user.addMusicFolder(musicFolder);
}
}
for(Setting musicFolder: user.getMusicFolderSettings()) {
if(musicFolder.getName().equals(id)) {
musicFolder.setValue(true);
break;
}
}
}
}
}
} while (eventType != XmlPullParser.END_DOCUMENT);
for(Setting musicFolder: user.getMusicFolderSettings()) {
if(musicFolder.getName().equals(id)) {
musicFolder.setValue(true);
break;
}
}
}
}
}
} while (eventType != XmlPullParser.END_DOCUMENT);
validate();
validate();
return result;
}
return result;
}
private List<MusicFolder> getMusicFolders() throws Exception{
MusicService musicService = MusicServiceFactory.getMusicService(context);
return musicService.getMusicFolders(false, context, null);
}
private List<MusicFolder> getMusicFolders() throws Exception{
MusicService musicService = MusicServiceFactory.getMusicService(context);
return musicService.getMusicFolders(false, context, null);
}
private void parseSetting(User user, String name) {
String value = get(name);
if(value != null) {
user.addSetting(name, "true".equals(value));
}
}
private void parseSetting(User user, String name) {
String value = get(name);
if(value != null) {
user.addSetting(name, "true".equals(value));
}
}
}

View File

@ -34,57 +34,57 @@ import android.os.IBinder;
*/
public class AuthenticatorService extends Service {
private SubsonicAuthenticator authenticator;
private SubsonicAuthenticator authenticator;
@Override
public void onCreate() {
authenticator = new SubsonicAuthenticator(this);
}
@Override
public void onCreate() {
authenticator = new SubsonicAuthenticator(this);
}
@Override
public IBinder onBind(Intent intent) {
return authenticator.getIBinder();
@Override
public IBinder onBind(Intent intent) {
return authenticator.getIBinder();
}
}
private class SubsonicAuthenticator extends AbstractAccountAuthenticator {
public SubsonicAuthenticator(Context context) {
super(context);
}
private class SubsonicAuthenticator extends AbstractAccountAuthenticator {
public SubsonicAuthenticator(Context context) {
super(context);
}
@Override
public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
return null;
}
@Override
public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
return null;
}
@Override
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {
return null;
}
@Override
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {
return null;
}
@Override
public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException {
return null;
}
@Override
public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException {
return null;
}
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
return null;
}
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
return null;
}
@Override
public String getAuthTokenLabel(String authTokenType) {
return null;
}
@Override
public String getAuthTokenLabel(String authTokenType) {
return null;
}
@Override
public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
return null;
}
@Override
public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
return null;
}
@Override
public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException {
return null;
}
}
@Override
public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException {
return null;
}
}
}

View File

@ -32,71 +32,71 @@ import java.util.List;
* @author Scott
*/
public class Updater {
protected String TAG = Updater.class.getSimpleName();
protected int version;
protected Context context;
protected String TAG = Updater.class.getSimpleName();
protected int version;
protected Context context;
public Updater(int version) {
// 5.2 should show as 520 instead of 52
if(version < 100) {
version *= 10;
}
this.version = version;
}
public Updater(int version) {
// 5.2 should show as 520 instead of 52
if(version < 100) {
version *= 10;
}
this.version = version;
}
public void checkUpdates(Context context) {
this.context = context;
List<Updater> updaters = new ArrayList<Updater>();
updaters.add(new UpdaterSongPress());
public void checkUpdates(Context context) {
this.context = context;
List<Updater> updaters = new ArrayList<Updater>();
updaters.add(new UpdaterSongPress());
SharedPreferences prefs = Util.getPreferences(context);
int lastVersion = prefs.getInt(Constants.LAST_VERSION, 0);
if(lastVersion == 0) {
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(Constants.LAST_VERSION, version);
editor.apply();
}
else if(version > lastVersion) {
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(Constants.LAST_VERSION, version);
editor.apply();
SharedPreferences prefs = Util.getPreferences(context);
int lastVersion = prefs.getInt(Constants.LAST_VERSION, 0);
if(lastVersion == 0) {
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(Constants.LAST_VERSION, version);
editor.apply();
}
else if(version > lastVersion) {
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(Constants.LAST_VERSION, version);
editor.apply();
Log.i(TAG, "Updating from version " + lastVersion + " to " + version);
for(Updater updater: updaters) {
if(updater.shouldUpdate(lastVersion)) {
new BackgroundUpdate(context, updater).execute();
}
}
}
}
Log.i(TAG, "Updating from version " + lastVersion + " to " + version);
for(Updater updater: updaters) {
if(updater.shouldUpdate(lastVersion)) {
new BackgroundUpdate(context, updater).execute();
}
}
}
}
public String getName() {
return this.TAG;
}
public String getName() {
return this.TAG;
}
private class BackgroundUpdate extends SilentBackgroundTask<Void> {
private final Updater updater;
private class BackgroundUpdate extends SilentBackgroundTask<Void> {
private final Updater updater;
public BackgroundUpdate(Context context, Updater updater) {
super(context);
this.updater = updater;
}
public BackgroundUpdate(Context context, Updater updater) {
super(context);
this.updater = updater;
}
@Override
protected Void doInBackground() {
try {
updater.update(context);
} catch(Exception e) {
Log.w(TAG, "Failed to run update for " + updater.getName());
}
return null;
}
}
@Override
protected Void doInBackground() {
try {
updater.update(context);
} catch(Exception e) {
Log.w(TAG, "Failed to run update for " + updater.getName());
}
return null;
}
}
public boolean shouldUpdate(int version) {
return this.version > version;
}
public void update(Context context) {
public boolean shouldUpdate(int version) {
return this.version > version;
}
public void update(Context context) {
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2016 (C) Scott Jackson
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 2016 (C) Scott Jackson
*/
package net.nullsum.audinaut.updates;
@ -22,21 +22,21 @@ import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.Util;
public class UpdaterSongPress extends Updater {
public UpdaterSongPress() {
super(521);
TAG = this.getClass().getSimpleName();
}
public UpdaterSongPress() {
super(521);
TAG = this.getClass().getSimpleName();
}
@Override
public void update(Context context) {
SharedPreferences prefs = Util.getPreferences(context);
boolean playNowAfter = prefs.getBoolean("playNowAfter", true);
@Override
public void update(Context context) {
SharedPreferences prefs = Util.getPreferences(context);
boolean playNowAfter = prefs.getBoolean("playNowAfter", true);
// Migrate the old preference so behavior stays the same
if(playNowAfter == false) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(Constants.PREFERENCES_KEY_SONG_PRESS_ACTION, "single");
editor.apply();
}
}
// Migrate the old preference so behavior stays the same
if(playNowAfter == false) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(Constants.PREFERENCES_KEY_SONG_PRESS_ACTION, "single");
editor.apply();
}
}
}

View File

@ -45,62 +45,62 @@ public abstract class BackgroundTask<T> implements ProgressListener {
private static final String TAG = BackgroundTask.class.getSimpleName();
private final Context context;
protected AtomicBoolean cancelled = new AtomicBoolean(false);
protected OnCancelListener cancelListener;
protected Runnable onCompletionListener = null;
protected Task task;
protected AtomicBoolean cancelled = new AtomicBoolean(false);
protected OnCancelListener cancelListener;
protected Runnable onCompletionListener = null;
protected Task task;
private static final int DEFAULT_CONCURRENCY = 8;
private static final Collection<Thread> threads = Collections.synchronizedCollection(new ArrayList<Thread>());
protected static final BlockingQueue<BackgroundTask.Task> queue = new LinkedBlockingQueue<BackgroundTask.Task>(10);
private static Handler handler = null;
static {
try {
handler = new Handler(Looper.getMainLooper());
} catch(Exception e) {
// Not called from main thread
}
}
private static final int DEFAULT_CONCURRENCY = 8;
private static final Collection<Thread> threads = Collections.synchronizedCollection(new ArrayList<Thread>());
protected static final BlockingQueue<BackgroundTask.Task> queue = new LinkedBlockingQueue<BackgroundTask.Task>(10);
private static Handler handler = null;
static {
try {
handler = new Handler(Looper.getMainLooper());
} catch(Exception e) {
// Not called from main thread
}
}
public BackgroundTask(Context context) {
this.context = context;
if(threads.size() < DEFAULT_CONCURRENCY) {
for(int i = threads.size(); i < DEFAULT_CONCURRENCY; i++) {
Thread thread = new Thread(new TaskRunnable(), String.format("BackgroundTask_%d", i));
threads.add(thread);
thread.start();
}
}
if(handler == null) {
try {
handler = new Handler(Looper.getMainLooper());
} catch(Exception e) {
// Not called from main thread
}
}
if(threads.size() < DEFAULT_CONCURRENCY) {
for(int i = threads.size(); i < DEFAULT_CONCURRENCY; i++) {
Thread thread = new Thread(new TaskRunnable(), String.format("BackgroundTask_%d", i));
threads.add(thread);
thread.start();
}
}
if(handler == null) {
try {
handler = new Handler(Looper.getMainLooper());
} catch(Exception e) {
// Not called from main thread
}
}
}
public static void stopThreads() {
for(Thread thread: threads) {
thread.interrupt();
}
threads.clear();
queue.clear();
}
public static void stopThreads() {
for(Thread thread: threads) {
thread.interrupt();
}
threads.clear();
queue.clear();
}
protected Activity getActivity() {
return (context instanceof Activity) ? ((Activity) context) : null;
}
protected Context getContext() {
return context;
}
protected Context getContext() {
return context;
}
protected Handler getHandler() {
return handler;
}
public abstract void execute();
public abstract void execute();
protected abstract T doInBackground() throws Throwable;
@ -108,10 +108,10 @@ public abstract class BackgroundTask<T> implements ProgressListener {
protected void error(Throwable error) {
Log.w(TAG, "Got exception: " + error, error);
Activity activity = getActivity();
if(activity != null) {
new ErrorDialog(activity, getErrorMessage(error), true);
}
Activity activity = getActivity();
if(activity != null) {
new ErrorDialog(activity, getErrorMessage(error), true);
}
}
protected String getErrorMessage(Throwable error) {
@ -139,33 +139,33 @@ public abstract class BackgroundTask<T> implements ProgressListener {
return error.getClass().getSimpleName();
}
public void cancel() {
if(cancelled.compareAndSet(false, true)) {
if(isRunning()) {
if(cancelListener != null) {
cancelListener.onCancel();
} else {
task.cancel();
}
}
public void cancel() {
if(cancelled.compareAndSet(false, true)) {
if(isRunning()) {
if(cancelListener != null) {
cancelListener.onCancel();
} else {
task.cancel();
}
}
task = null;
}
}
public boolean isCancelled() {
return cancelled.get();
}
public void setOnCancelListener(OnCancelListener listener) {
cancelListener = listener;
}
task = null;
}
}
public boolean isCancelled() {
return cancelled.get();
}
public void setOnCancelListener(OnCancelListener listener) {
cancelListener = listener;
}
public boolean isRunning() {
if(task == null) {
return false;
} else {
return task.isRunning();
}
}
public boolean isRunning() {
if(task == null) {
return false;
} else {
return task.isRunning();
}
}
@Override
public abstract void updateProgress(final String message);
@ -175,151 +175,151 @@ public abstract class BackgroundTask<T> implements ProgressListener {
updateProgress(context.getResources().getString(messageId));
}
@Override
public void updateCache(int changeCode) {
@Override
public void updateCache(int changeCode) {
}
}
public void setOnCompletionListener(Runnable onCompletionListener) {
this.onCompletionListener = onCompletionListener;
}
public void setOnCompletionListener(Runnable onCompletionListener) {
this.onCompletionListener = onCompletionListener;
}
protected class Task {
private Thread thread;
private AtomicBoolean taskStart = new AtomicBoolean(false);
protected class Task {
private Thread thread;
private AtomicBoolean taskStart = new AtomicBoolean(false);
private void execute() throws Exception {
// Don't run if cancelled already
if(isCancelled()) {
return;
}
private void execute() throws Exception {
// Don't run if cancelled already
if(isCancelled()) {
return;
}
try {
thread = Thread.currentThread();
taskStart.set(true);
try {
thread = Thread.currentThread();
taskStart.set(true);
final T result = doInBackground();
if(isCancelled()) {
taskStart.set(false);
return;
}
final T result = doInBackground();
if(isCancelled()) {
taskStart.set(false);
return;
}
if(handler != null) {
handler.post(new Runnable() {
@Override
public void run() {
if (!isCancelled()) {
try {
onDone(result);
} catch (Throwable t) {
if(!isCancelled()) {
try {
onError(t);
} catch(Exception e) {
// Don't care
}
}
}
}
if(handler != null) {
handler.post(new Runnable() {
@Override
public void run() {
if (!isCancelled()) {
try {
onDone(result);
} catch (Throwable t) {
if(!isCancelled()) {
try {
onError(t);
} catch(Exception e) {
// Don't care
}
}
}
}
taskStart.set(false);
}
});
} else {
taskStart.set(false);
}
} catch(InterruptedException interrupt) {
if(taskStart.get()) {
// Don't exit root thread if task cancelled
throw interrupt;
}
} catch(final Throwable t) {
if(isCancelled()) {
taskStart.set(false);
return;
}
taskStart.set(false);
}
});
} else {
taskStart.set(false);
}
} catch(InterruptedException interrupt) {
if(taskStart.get()) {
// Don't exit root thread if task cancelled
throw interrupt;
}
} catch(final Throwable t) {
if(isCancelled()) {
taskStart.set(false);
return;
}
if(handler != null) {
handler.post(new Runnable() {
@Override
public void run() {
if(!isCancelled()) {
try {
onError(t);
} catch(Exception e) {
// Don't care
}
}
if(handler != null) {
handler.post(new Runnable() {
@Override
public void run() {
if(!isCancelled()) {
try {
onError(t);
} catch(Exception e) {
// Don't care
}
}
taskStart.set(false);
}
});
} else {
taskStart.set(false);
}
} finally {
thread = null;
}
}
taskStart.set(false);
}
});
} else {
taskStart.set(false);
}
} finally {
thread = null;
}
}
public void cancel() {
if(taskStart.compareAndSet(true, false)) {
if (thread != null) {
thread.interrupt();
}
}
}
public boolean isCancelled() {
if(Thread.interrupted()) {
return true;
} else if(BackgroundTask.this.isCancelled()) {
return true;
} else {
return false;
}
}
public void onDone(T result) {
done(result);
public void cancel() {
if(taskStart.compareAndSet(true, false)) {
if (thread != null) {
thread.interrupt();
}
}
}
public boolean isCancelled() {
if(Thread.interrupted()) {
return true;
} else if(BackgroundTask.this.isCancelled()) {
return true;
} else {
return false;
}
}
public void onDone(T result) {
done(result);
if(onCompletionListener != null) {
onCompletionListener.run();
}
}
public void onError(Throwable t) {
error(t);
}
if(onCompletionListener != null) {
onCompletionListener.run();
}
}
public void onError(Throwable t) {
error(t);
}
public boolean isRunning() {
return taskStart.get();
}
}
public boolean isRunning() {
return taskStart.get();
}
}
private class TaskRunnable implements Runnable {
private boolean running = true;
private class TaskRunnable implements Runnable {
private boolean running = true;
public TaskRunnable() {
public TaskRunnable() {
}
}
@Override
public void run() {
Looper.prepare();
while(running) {
try {
Task task = queue.take();
task.execute();
} catch(InterruptedException stop) {
Log.e(TAG, "Thread died");
running = false;
threads.remove(Thread.currentThread());
} catch(Throwable t) {
Log.e(TAG, "Unexpected crash in BackgroundTask thread", t);
}
}
}
}
@Override
public void run() {
Looper.prepare();
while(running) {
try {
Task task = queue.take();
task.execute();
} catch(InterruptedException stop) {
Log.e(TAG, "Thread died");
running = false;
threads.remove(Thread.currentThread());
} catch(Throwable t) {
Log.e(TAG, "Unexpected crash in BackgroundTask thread", t);
}
}
}
}
public static interface OnCancelListener {
void onCancel();
}
public static interface OnCancelListener {
void onCancel();
}
}

View File

@ -1,16 +1,16 @@
/*
This file is part of ServerProxy.
SocketProxy 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 2014 (C) Scott Jackson
This file is part of ServerProxy.
SocketProxy 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 2014 (C) Scott Jackson
*/
package net.nullsum.audinaut.util;
@ -18,11 +18,11 @@ package net.nullsum.audinaut.util;
import java.io.File;
public interface BufferFile {
File getFile();
Long getContentLength();
long getEstimatedSize();
boolean isWorkDone();
void onStart();
void onStop();
void onResume();
File getFile();
Long getContentLength();
long getEstimatedSize();
boolean isWorkDone();
void onStart();
void onStop();
void onResume();
}

View File

@ -23,60 +23,60 @@ import android.content.Context;
import net.nullsum.audinaut.util.FileProxy;
public class BufferProxy extends FileProxy {
private static final String TAG = BufferProxy.class.getSimpleName();
protected BufferFile progress;
private static final String TAG = BufferProxy.class.getSimpleName();
protected BufferFile progress;
public BufferProxy(Context context) {
super(context);
}
public BufferProxy(Context context) {
super(context);
}
protected ProxyTask getTask(Socket client) {
return new BufferFileTask(client);
}
protected ProxyTask getTask(Socket client) {
return new BufferFileTask(client);
}
public void setBufferFile(BufferFile progress) {
this.progress = progress;
}
public void setBufferFile(BufferFile progress) {
this.progress = progress;
}
protected class BufferFileTask extends StreamFileTask {
public BufferFileTask(Socket client) {
super(client);
}
protected class BufferFileTask extends StreamFileTask {
public BufferFileTask(Socket client) {
super(client);
}
@Override
File getFile(String path) {
return progress.getFile();
}
@Override
File getFile(String path) {
return progress.getFile();
}
@Override
Long getContentLength() {
Long contentLength = progress.getContentLength();
if(contentLength == null && progress.isWorkDone()) {
contentLength = file.length();
}
return contentLength;
}
@Override
long getFileSize() {
return progress.getEstimatedSize();
}
@Override
Long getContentLength() {
Long contentLength = progress.getContentLength();
if(contentLength == null && progress.isWorkDone()) {
contentLength = file.length();
}
return contentLength;
}
@Override
long getFileSize() {
return progress.getEstimatedSize();
}
@Override
public void onStart() {
progress.onStart();
}
@Override
public void onStop() {
progress.onStop();
}
@Override
public void onResume() {
progress.onResume();
}
@Override
public void onStart() {
progress.onStart();
}
@Override
public void onStop() {
progress.onStop();
}
@Override
public void onResume() {
progress.onResume();
}
@Override
public boolean isWorkDone() {
return progress.isWorkDone() && cbSkip >= file.length();
}
}
@Override
public boolean isWorkDone() {
return progress.isWorkDone() && cbSkip >= file.length();
}
}
}

View File

@ -25,28 +25,28 @@ import java.util.*;
public class CacheCleaner {
private static final String TAG = CacheCleaner.class.getSimpleName();
private static final long MIN_FREE_SPACE = 500 * 1024L * 1024L;
private static final long MAX_COVER_ART_SPACE = 100 * 1024L * 1024L;
private static final long MIN_FREE_SPACE = 500 * 1024L * 1024L;
private static final long MAX_COVER_ART_SPACE = 100 * 1024L * 1024L;
private final Context context;
private final DownloadService downloadService;
private final MediaStoreService mediaStore;
private final MediaStoreService mediaStore;
public CacheCleaner(Context context, DownloadService downloadService) {
this.context = context;
this.downloadService = downloadService;
this.mediaStore = new MediaStoreService(context);
this.mediaStore = new MediaStoreService(context);
}
public void clean() {
new BackgroundCleanup(context).execute();
new BackgroundCleanup(context).execute();
}
public void cleanSpace() {
new BackgroundSpaceCleanup(context).execute();
}
public void cleanPlaylists(List<Playlist> playlists) {
new BackgroundPlaylistsCleanup(context, playlists).execute();
}
public void cleanSpace() {
new BackgroundSpaceCleanup(context).execute();
}
public void cleanPlaylists(List<Playlist> playlists) {
new BackgroundPlaylistsCleanup(context, playlists).execute();
}
private void deleteEmptyDirs(List<File> dirs, Set<File> undeletable) {
for (File dir : dirs) {
@ -58,12 +58,12 @@ public class CacheCleaner {
}
}
private long getMinimumDelete(List<File> files, List<File> pinned) {
if(files.size() == 0) {
return 0L;
}
private long getMinimumDelete(List<File> files, List<File> pinned) {
if(files.size() == 0) {
return 0L;
}
long cacheSizeBytes = Util.getCacheSizeMB(context) * 1024L * 1024L;
long cacheSizeBytes = Util.getCacheSizeMB(context) * 1024L * 1024L;
long bytesUsedBySubsonic = 0L;
for (File file : files) {
@ -73,7 +73,7 @@ public class CacheCleaner {
bytesUsedBySubsonic += file.length();
}
// Ensure that file system is not more than 95% full.
// Ensure that file system is not more than 95% full.
StatFs stat = new StatFs(files.get(0).getPath());
long bytesTotalFs = (long) stat.getBlockCount() * (long) stat.getBlockSize();
long bytesAvailableFs = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
@ -89,8 +89,8 @@ public class CacheCleaner {
Log.i(TAG, "Cache size before : " + Util.formatBytes(bytesUsedBySubsonic));
Log.i(TAG, "Minimum to delete : " + Util.formatBytes(bytesToDelete));
return bytesToDelete;
}
return bytesToDelete;
}
private void deleteFiles(List<File> files, Set<File> undeletable, long bytesToDelete, boolean deletePartials) {
if (files.isEmpty()) {
@ -99,14 +99,14 @@ public class CacheCleaner {
long bytesDeleted = 0L;
for (File file : files) {
if(!deletePartials && bytesDeleted > bytesToDelete) break;
if(!deletePartials && bytesDeleted > bytesToDelete) break;
if (bytesToDelete > bytesDeleted || (deletePartials && (file.getName().endsWith(".partial") || file.getName().contains(".partial.")))) {
if (!undeletable.contains(file) && !file.getName().equals(Constants.ALBUM_ART_FILE)) {
long size = file.length();
if (Util.delete(file)) {
bytesDeleted += size;
mediaStore.deleteFromMediaStore(file);
mediaStore.deleteFromMediaStore(file);
}
}
}
@ -122,8 +122,8 @@ public class CacheCleaner {
if (isCacheFile) {
files.add(file);
} else {
pinned.add(file);
}
pinned.add(file);
}
} else {
// Depth-first
for (File child : FileUtil.listFiles(file)) {
@ -160,133 +160,133 @@ public class CacheCleaner {
return undeletable;
}
private void cleanupCoverArt(Context context) {
File dir = FileUtil.getAlbumArtDirectory(context);
private void cleanupCoverArt(Context context) {
File dir = FileUtil.getAlbumArtDirectory(context);
List<File> files = new ArrayList<File>();
long bytesUsed = 0L;
for(File file: dir.listFiles()) {
if(file.isFile()) {
files.add(file);
bytesUsed += file.length();
}
}
List<File> files = new ArrayList<File>();
long bytesUsed = 0L;
for(File file: dir.listFiles()) {
if(file.isFile()) {
files.add(file);
bytesUsed += file.length();
}
}
// Don't waste time sorting if under limit already
if(bytesUsed < MAX_COVER_ART_SPACE) {
return;
}
// Don't waste time sorting if under limit already
if(bytesUsed < MAX_COVER_ART_SPACE) {
return;
}
sortByAscendingModificationTime(files);
long bytesDeleted = 0L;
for(File file: files) {
// End as soon as the space used is below the threshold
if(bytesUsed < MAX_COVER_ART_SPACE) {
break;
}
sortByAscendingModificationTime(files);
long bytesDeleted = 0L;
for(File file: files) {
// End as soon as the space used is below the threshold
if(bytesUsed < MAX_COVER_ART_SPACE) {
break;
}
long bytes = file.length();
if(file.delete()) {
bytesUsed -= bytes;
bytesDeleted += bytes;
}
}
long bytes = file.length();
if(file.delete()) {
bytesUsed -= bytes;
bytesDeleted += bytes;
}
}
Log.i(TAG, "Deleted " + Util.formatBytes(bytesDeleted) + " worth of cover art");
}
Log.i(TAG, "Deleted " + Util.formatBytes(bytesDeleted) + " worth of cover art");
}
private class BackgroundCleanup extends SilentBackgroundTask<Void> {
public BackgroundCleanup(Context context) {
super(context);
}
private class BackgroundCleanup extends SilentBackgroundTask<Void> {
public BackgroundCleanup(Context context) {
super(context);
}
@Override
protected Void doInBackground() {
if (downloadService == null) {
Log.e(TAG, "DownloadService not set. Aborting cache cleaning.");
return null;
}
@Override
protected Void doInBackground() {
if (downloadService == null) {
Log.e(TAG, "DownloadService not set. Aborting cache cleaning.");
return null;
}
try {
List<File> files = new ArrayList<File>();
List<File> pinned = new ArrayList<File>();
List<File> dirs = new ArrayList<File>();
try {
List<File> files = new ArrayList<File>();
List<File> pinned = new ArrayList<File>();
List<File> dirs = new ArrayList<File>();
findCandidatesForDeletion(FileUtil.getMusicDirectory(context), files, pinned, dirs);
sortByAscendingModificationTime(files);
findCandidatesForDeletion(FileUtil.getMusicDirectory(context), files, pinned, dirs);
sortByAscendingModificationTime(files);
Set<File> undeletable = findUndeletableFiles();
Set<File> undeletable = findUndeletableFiles();
deleteFiles(files, undeletable, getMinimumDelete(files, pinned), true);
deleteEmptyDirs(dirs, undeletable);
deleteFiles(files, undeletable, getMinimumDelete(files, pinned), true);
deleteEmptyDirs(dirs, undeletable);
// Make sure cover art directory does not grow too large
cleanupCoverArt(context);
} catch (RuntimeException x) {
Log.e(TAG, "Error in cache cleaning.", x);
}
// Make sure cover art directory does not grow too large
cleanupCoverArt(context);
} catch (RuntimeException x) {
Log.e(TAG, "Error in cache cleaning.", x);
}
return null;
}
}
return null;
}
}
private class BackgroundSpaceCleanup extends SilentBackgroundTask<Void> {
public BackgroundSpaceCleanup(Context context) {
super(context);
}
private class BackgroundSpaceCleanup extends SilentBackgroundTask<Void> {
public BackgroundSpaceCleanup(Context context) {
super(context);
}
@Override
protected Void doInBackground() {
if (downloadService == null) {
Log.e(TAG, "DownloadService not set. Aborting cache cleaning.");
return null;
}
@Override
protected Void doInBackground() {
if (downloadService == null) {
Log.e(TAG, "DownloadService not set. Aborting cache cleaning.");
return null;
}
try {
List<File> files = new ArrayList<File>();
List<File> pinned = new ArrayList<File>();
List<File> dirs = new ArrayList<File>();
findCandidatesForDeletion(FileUtil.getMusicDirectory(context), files, pinned, dirs);
try {
List<File> files = new ArrayList<File>();
List<File> pinned = new ArrayList<File>();
List<File> dirs = new ArrayList<File>();
findCandidatesForDeletion(FileUtil.getMusicDirectory(context), files, pinned, dirs);
long bytesToDelete = getMinimumDelete(files, pinned);
if(bytesToDelete > 0L) {
sortByAscendingModificationTime(files);
Set<File> undeletable = findUndeletableFiles();
deleteFiles(files, undeletable, bytesToDelete, false);
}
} catch (RuntimeException x) {
Log.e(TAG, "Error in cache cleaning.", x);
}
long bytesToDelete = getMinimumDelete(files, pinned);
if(bytesToDelete > 0L) {
sortByAscendingModificationTime(files);
Set<File> undeletable = findUndeletableFiles();
deleteFiles(files, undeletable, bytesToDelete, false);
}
} catch (RuntimeException x) {
Log.e(TAG, "Error in cache cleaning.", x);
}
return null;
}
}
return null;
}
}
private class BackgroundPlaylistsCleanup extends SilentBackgroundTask<Void> {
private final List<Playlist> playlists;
private class BackgroundPlaylistsCleanup extends SilentBackgroundTask<Void> {
private final List<Playlist> playlists;
public BackgroundPlaylistsCleanup(Context context, List<Playlist> playlists) {
super(context);
this.playlists = playlists;
}
public BackgroundPlaylistsCleanup(Context context, List<Playlist> playlists) {
super(context);
this.playlists = playlists;
}
@Override
protected Void doInBackground() {
try {
String server = Util.getServerName(context);
SortedSet<File> playlistFiles = FileUtil.listFiles(FileUtil.getPlaylistDirectory(context, server));
for (Playlist playlist : playlists) {
playlistFiles.remove(FileUtil.getPlaylistFile(context, server, playlist.getName()));
}
@Override
protected Void doInBackground() {
try {
String server = Util.getServerName(context);
SortedSet<File> playlistFiles = FileUtil.listFiles(FileUtil.getPlaylistDirectory(context, server));
for (Playlist playlist : playlists) {
playlistFiles.remove(FileUtil.getPlaylistFile(context, server, playlist.getName()));
}
for(File playlist : playlistFiles) {
playlist.delete();
}
} catch (RuntimeException x) {
Log.e(TAG, "Error in playlist cache cleaning.", x);
}
for(File playlist : playlistFiles) {
playlist.delete();
}
} catch (RuntimeException x) {
Log.e(TAG, "Error in playlist cache cleaning.", x);
}
return null;
}
}
return null;
}
}
}

View File

@ -29,13 +29,13 @@ public final class Constants {
public static final String REST_PROTOCOL_VERSION_SUBSONIC = "1.13.0";
public static final String REST_CLIENT_ID = "Audinaut";
public static final String LAST_VERSION = "subsonic.version";
public static final String LAST_VERSION = "subsonic.version";
// Names for intent extras.
public static final String INTENT_EXTRA_NAME_ID = "subsonic.id";
public static final String INTENT_EXTRA_NAME_NAME = "subsonic.name";
public static final String INTENT_EXTRA_NAME_DIRECTORY = "subsonic.directory";
public static final String INTENT_EXTRA_NAME_CHILD_ID = "subsonic.child.id";
public static final String INTENT_EXTRA_NAME_DIRECTORY = "subsonic.directory";
public static final String INTENT_EXTRA_NAME_CHILD_ID = "subsonic.child.id";
public static final String INTENT_EXTRA_NAME_ARTIST = "subsonic.artist";
public static final String INTENT_EXTRA_NAME_TITLE = "subsonic.title";
public static final String INTENT_EXTRA_NAME_AUTOPLAY = "subsonic.playall";
@ -44,131 +44,131 @@ public final class Constants {
public static final String INTENT_EXTRA_NAME_PLAYLIST_NAME = "subsonic.playlist.name";
public static final String INTENT_EXTRA_NAME_PLAYLIST_OWNER = "subsonic.playlist.isOwner";
public static final String INTENT_EXTRA_NAME_ALBUM_LIST_TYPE = "subsonic.albumlisttype";
public static final String INTENT_EXTRA_NAME_ALBUM_LIST_EXTRA = "subsonic.albumlistextra";
public static final String INTENT_EXTRA_NAME_ALBUM_LIST_EXTRA = "subsonic.albumlistextra";
public static final String INTENT_EXTRA_NAME_ALBUM_LIST_SIZE = "subsonic.albumlistsize";
public static final String INTENT_EXTRA_NAME_ALBUM_LIST_OFFSET = "subsonic.albumlistoffset";
public static final String INTENT_EXTRA_NAME_SHUFFLE = "subsonic.shuffle";
public static final String INTENT_EXTRA_REQUEST_SEARCH = "subsonic.requestsearch";
public static final String INTENT_EXTRA_NAME_EXIT = "subsonic.exit" ;
public static final String INTENT_EXTRA_NAME_DOWNLOAD = "subsonic.download";
public static final String INTENT_EXTRA_NAME_DOWNLOAD_VIEW = "subsonic.download_view";
public static final String INTENT_EXTRA_VIEW_ALBUM = "subsonic.view_album";
public static final String INTENT_EXTRA_NAME_SHARE = "subsonic.share";
public static final String INTENT_EXTRA_FRAGMENT_TYPE = "fragmentType";
public static final String INTENT_EXTRA_REFRESH_LISTINGS = "refreshListings";
public static final String INTENT_EXTRA_SEARCH_SONG = "searchSong";
public static final String INTENT_EXTRA_TOP_TRACKS = "topTracks";
public static final String INTENT_EXTRA_PLAY_LAST = "playLast";
public static final String INTENT_EXTRA_ENTRY = "passedEntry";
public static final String INTENT_EXTRA_NAME_DOWNLOAD = "subsonic.download";
public static final String INTENT_EXTRA_NAME_DOWNLOAD_VIEW = "subsonic.download_view";
public static final String INTENT_EXTRA_VIEW_ALBUM = "subsonic.view_album";
public static final String INTENT_EXTRA_NAME_SHARE = "subsonic.share";
public static final String INTENT_EXTRA_FRAGMENT_TYPE = "fragmentType";
public static final String INTENT_EXTRA_REFRESH_LISTINGS = "refreshListings";
public static final String INTENT_EXTRA_SEARCH_SONG = "searchSong";
public static final String INTENT_EXTRA_TOP_TRACKS = "topTracks";
public static final String INTENT_EXTRA_PLAY_LAST = "playLast";
public static final String INTENT_EXTRA_ENTRY = "passedEntry";
// Preferences keys.
public static final String PREFERENCES_KEY_SERVER_KEY = "server";
public static final String PREFERENCES_KEY_SERVER_COUNT = "serverCount";
public static final String PREFERENCES_KEY_SERVER_ADD = "serverAdd";
public static final String PREFERENCES_KEY_SERVER_REMOVE = "serverRemove";
public static final String PREFERENCES_KEY_SERVER_KEY = "server";
public static final String PREFERENCES_KEY_SERVER_COUNT = "serverCount";
public static final String PREFERENCES_KEY_SERVER_ADD = "serverAdd";
public static final String PREFERENCES_KEY_SERVER_REMOVE = "serverRemove";
public static final String PREFERENCES_KEY_SERVER_INSTANCE = "serverInstanceId";
public static final String PREFERENCES_KEY_SERVER_NAME = "serverName";
public static final String PREFERENCES_KEY_SERVER_URL = "serverUrl";
public static final String PREFERENCES_KEY_SERVER_INTERNAL_URL = "serverInternalUrl";
public static final String PREFERENCES_KEY_SERVER_LOCAL_NETWORK_SSID = "serverLocalNetworkSSID";
public static final String PREFERENCES_KEY_TEST_CONNECTION = "serverTestConnection";
public static final String PREFERENCES_KEY_OPEN_BROWSER = "openBrowser";
public static final String PREFERENCES_KEY_SERVER_INTERNAL_URL = "serverInternalUrl";
public static final String PREFERENCES_KEY_SERVER_LOCAL_NETWORK_SSID = "serverLocalNetworkSSID";
public static final String PREFERENCES_KEY_TEST_CONNECTION = "serverTestConnection";
public static final String PREFERENCES_KEY_OPEN_BROWSER = "openBrowser";
public static final String PREFERENCES_KEY_MUSIC_FOLDER_ID = "musicFolderId";
public static final String PREFERENCES_KEY_USERNAME = "username";
public static final String PREFERENCES_KEY_PASSWORD = "password";
public static final String PREFERENCES_KEY_INSTALL_TIME = "installTime";
public static final String PREFERENCES_KEY_THEME = "theme";
public static final String PREFERENCES_KEY_FULL_SCREEN = "fullScreen";
public static final String PREFERENCES_KEY_DISPLAY_TRACK = "displayTrack";
public static final String PREFERENCES_KEY_DISPLAY_TRACK = "displayTrack";
public static final String PREFERENCES_KEY_MAX_BITRATE_WIFI = "maxBitrateWifi";
public static final String PREFERENCES_KEY_MAX_BITRATE_MOBILE = "maxBitrateMobile";
public static final String PREFERENCES_KEY_NETWORK_TIMEOUT = "networkTimeout";
public static final String PREFERENCES_KEY_NETWORK_TIMEOUT = "networkTimeout";
public static final String PREFERENCES_KEY_CACHE_SIZE = "cacheSize";
public static final String PREFERENCES_KEY_CACHE_LOCATION = "cacheLocation";
public static final String PREFERENCES_KEY_PRELOAD_COUNT_WIFI = "preloadCountWifi";
public static final String PREFERENCES_KEY_PRELOAD_COUNT_MOBILE = "preloadCountMobile";
public static final String PREFERENCES_KEY_PRELOAD_COUNT_MOBILE = "preloadCountMobile";
public static final String PREFERENCES_KEY_HIDE_MEDIA = "hideMedia";
public static final String PREFERENCES_KEY_MEDIA_BUTTONS = "mediaButtons";
public static final String PREFERENCES_KEY_SCREEN_LIT_ON_DOWNLOAD = "screenLitOnDownload";
public static final String PREFERENCES_KEY_REPEAT_MODE = "repeatMode";
public static final String PREFERENCES_KEY_WIFI_REQUIRED_FOR_DOWNLOAD = "wifiRequiredForDownload";
public static final String PREFERENCES_KEY_RANDOM_SIZE = "randomSize";
public static final String PREFERENCES_KEY_OFFLINE = "offline";
public static final String PREFERENCES_KEY_TEMP_LOSS = "tempLoss";
public static final String PREFERENCES_KEY_SHUFFLE_START_YEAR = "startYear";
public static final String PREFERENCES_KEY_SHUFFLE_END_YEAR = "endYear";
public static final String PREFERENCES_KEY_SHUFFLE_GENRE = "genre";
public static final String PREFERENCES_KEY_KEEP_SCREEN_ON = "keepScreenOn";
public static final String PREFERENCES_EQUALIZER_ON = "equalizerOn";
public static final String PREFERENCES_EQUALIZER_SETTINGS = "equalizerSettings";
public static final String PREFERENCES_KEY_PERSISTENT_NOTIFICATION = "persistentNotification";
public static final String PREFERENCES_KEY_GAPLESS_PLAYBACK = "gaplessPlayback";
public static final String PREFERENCES_KEY_REMOVE_PLAYED = "removePlayed";
public static final String PREFERENCES_KEY_KEEP_PLAYED_CNT = "keepPlayedCount";
public static final String PREFERENCES_KEY_SHUFFLE_MODE = "shuffleMode2";
public static final String PREFERENCES_KEY_SHUFFLE_MODE_EXTRA = "shuffleModeExtra";
public static final String PREFERENCES_KEY_SYNC_ENABLED = "syncEnabled";
public static final String PREFERENCES_KEY_SYNC_INTERVAL = "syncInterval";
public static final String PREFERENCES_KEY_SYNC_WIFI = "syncWifi";
public static final String PREFERENCES_KEY_SYNC_NOTIFICATION = "syncNotification";
public static final String PREFERENCES_KEY_SYNC_MOST_RECENT = "syncMostRecent";
public static final String PREFERENCES_KEY_PAUSE_DISCONNECT = "pauseOnDisconnect";
public static final String PREFERENCES_KEY_HIDE_WIDGET = "hideWidget";
public static final String PREFERENCES_KEY_CUSTOM_SORT_ENABLED = "customSortEnabled";
public static final String PREFERENCES_KEY_SHARED_ENABLED = "sharedEnabled";
public static final String PREFERENCES_KEY_OPEN_TO_TAB = "openToTab";
// public static final String PREFERENCES_KEY_PLAY_NOW_AFTER = "playNowAfter";
public static final String PREFERENCES_KEY_SONG_PRESS_ACTION = "songPressAction";
public static final String PREFERENCES_KEY_LARGE_ALBUM_ART = "largeAlbumArt";
public static final String PREFERENCES_KEY_PLAYLIST_NAME = "suggestedPlaylistName";
public static final String PREFERENCES_KEY_PLAYLIST_ID = "suggestedPlaylistId";
public static final String PREFERENCES_KEY_RECENT_COUNT = "mostRecentCount";
public static final String PREFERENCES_KEY_REPLAY_GAIN = "replayGain";
public static final String PREFERENCES_KEY_REPLAY_GAIN_BUMP = "replayGainBump2";
public static final String PREFERENCES_KEY_REPLAY_GAIN_UNTAGGED = "replayGainUntagged2";
public static final String PREFERENCES_KEY_REPLAY_GAIN_TYPE= "replayGainType";
public static final String PREFERENCES_KEY_ALBUMS_PER_FOLDER = "albumsPerFolder";
public static final String PREFERENCES_KEY_FIRST_LEVEL_ARTIST = "firstLevelArtist";
public static final String PREFERENCES_KEY_START_ON_HEADPHONES = "startOnHeadphones";
public static final String PREFERENCES_KEY_COLOR_ACTION_BAR = "colorActionBar";
public static final String PREFERENCES_KEY_SHUFFLE_BY_ALBUM = "shuffleByAlbum";
public static final String PREFERENCES_KEY_RESUME_PLAY_QUEUE_NEVER = "neverResumePlayQueue";
public static final String PREFERENCES_KEY_BATCH_MODE = "batchMode";
public static final String PREFERENCES_KEY_HEADS_UP_NOTIFICATION = "headsUpNotification";
public static final String PREFERENCES_KEY_RANDOM_SIZE = "randomSize";
public static final String PREFERENCES_KEY_OFFLINE = "offline";
public static final String PREFERENCES_KEY_TEMP_LOSS = "tempLoss";
public static final String PREFERENCES_KEY_SHUFFLE_START_YEAR = "startYear";
public static final String PREFERENCES_KEY_SHUFFLE_END_YEAR = "endYear";
public static final String PREFERENCES_KEY_SHUFFLE_GENRE = "genre";
public static final String PREFERENCES_KEY_KEEP_SCREEN_ON = "keepScreenOn";
public static final String PREFERENCES_EQUALIZER_ON = "equalizerOn";
public static final String PREFERENCES_EQUALIZER_SETTINGS = "equalizerSettings";
public static final String PREFERENCES_KEY_PERSISTENT_NOTIFICATION = "persistentNotification";
public static final String PREFERENCES_KEY_GAPLESS_PLAYBACK = "gaplessPlayback";
public static final String PREFERENCES_KEY_REMOVE_PLAYED = "removePlayed";
public static final String PREFERENCES_KEY_KEEP_PLAYED_CNT = "keepPlayedCount";
public static final String PREFERENCES_KEY_SHUFFLE_MODE = "shuffleMode2";
public static final String PREFERENCES_KEY_SHUFFLE_MODE_EXTRA = "shuffleModeExtra";
public static final String PREFERENCES_KEY_SYNC_ENABLED = "syncEnabled";
public static final String PREFERENCES_KEY_SYNC_INTERVAL = "syncInterval";
public static final String PREFERENCES_KEY_SYNC_WIFI = "syncWifi";
public static final String PREFERENCES_KEY_SYNC_NOTIFICATION = "syncNotification";
public static final String PREFERENCES_KEY_SYNC_MOST_RECENT = "syncMostRecent";
public static final String PREFERENCES_KEY_PAUSE_DISCONNECT = "pauseOnDisconnect";
public static final String PREFERENCES_KEY_HIDE_WIDGET = "hideWidget";
public static final String PREFERENCES_KEY_CUSTOM_SORT_ENABLED = "customSortEnabled";
public static final String PREFERENCES_KEY_SHARED_ENABLED = "sharedEnabled";
public static final String PREFERENCES_KEY_OPEN_TO_TAB = "openToTab";
// public static final String PREFERENCES_KEY_PLAY_NOW_AFTER = "playNowAfter";
public static final String PREFERENCES_KEY_SONG_PRESS_ACTION = "songPressAction";
public static final String PREFERENCES_KEY_LARGE_ALBUM_ART = "largeAlbumArt";
public static final String PREFERENCES_KEY_PLAYLIST_NAME = "suggestedPlaylistName";
public static final String PREFERENCES_KEY_PLAYLIST_ID = "suggestedPlaylistId";
public static final String PREFERENCES_KEY_RECENT_COUNT = "mostRecentCount";
public static final String PREFERENCES_KEY_REPLAY_GAIN = "replayGain";
public static final String PREFERENCES_KEY_REPLAY_GAIN_BUMP = "replayGainBump2";
public static final String PREFERENCES_KEY_REPLAY_GAIN_UNTAGGED = "replayGainUntagged2";
public static final String PREFERENCES_KEY_REPLAY_GAIN_TYPE= "replayGainType";
public static final String PREFERENCES_KEY_ALBUMS_PER_FOLDER = "albumsPerFolder";
public static final String PREFERENCES_KEY_FIRST_LEVEL_ARTIST = "firstLevelArtist";
public static final String PREFERENCES_KEY_START_ON_HEADPHONES = "startOnHeadphones";
public static final String PREFERENCES_KEY_COLOR_ACTION_BAR = "colorActionBar";
public static final String PREFERENCES_KEY_SHUFFLE_BY_ALBUM = "shuffleByAlbum";
public static final String PREFERENCES_KEY_RESUME_PLAY_QUEUE_NEVER = "neverResumePlayQueue";
public static final String PREFERENCES_KEY_BATCH_MODE = "batchMode";
public static final String PREFERENCES_KEY_HEADS_UP_NOTIFICATION = "headsUpNotification";
public static final String OFFLINE_STAR_COUNT = "starCount";
public static final String OFFLINE_STAR_ID = "starID";
public static final String OFFLINE_STAR_SEARCH = "starTitle";
public static final String OFFLINE_STAR_SETTING = "starSetting";
public static final String OFFLINE_STAR_COUNT = "starCount";
public static final String OFFLINE_STAR_ID = "starID";
public static final String OFFLINE_STAR_SEARCH = "starTitle";
public static final String OFFLINE_STAR_SETTING = "starSetting";
public static final String CACHE_KEY_IGNORE = "ignoreArticles";
public static final String CACHE_AUDIO_SESSION_ID = "audioSessionId";
public static final String CACHE_BLOCK_TOKEN_USE = "blockTokenUse";
public static final String CACHE_KEY_IGNORE = "ignoreArticles";
public static final String CACHE_AUDIO_SESSION_ID = "audioSessionId";
public static final String CACHE_BLOCK_TOKEN_USE = "blockTokenUse";
public static final String MAIN_BACK_STACK = "backStackIds";
public static final String MAIN_BACK_STACK_SIZE = "backStackIdsSize";
public static final String MAIN_NOW_PLAYING = "nowPlayingId";
public static final String MAIN_NOW_PLAYING_SECONDARY = "nowPlayingSecondaryId";
public static final String MAIN_SLIDE_PANEL_STATE = "slidePanelState";
public static final String FRAGMENT_LIST = "fragmentList";
public static final String FRAGMENT_LIST2 = "fragmentList2";
public static final String FRAGMENT_EXTRA = "fragmentExtra";
public static final String FRAGMENT_DOWNLOAD_FLIPPER = "fragmentDownloadFlipper";
public static final String FRAGMENT_NAME = "fragmentName";
public static final String FRAGMENT_POSITION = "fragmentPosition";
public static final String MAIN_BACK_STACK = "backStackIds";
public static final String MAIN_BACK_STACK_SIZE = "backStackIdsSize";
public static final String MAIN_NOW_PLAYING = "nowPlayingId";
public static final String MAIN_NOW_PLAYING_SECONDARY = "nowPlayingSecondaryId";
public static final String MAIN_SLIDE_PANEL_STATE = "slidePanelState";
public static final String FRAGMENT_LIST = "fragmentList";
public static final String FRAGMENT_LIST2 = "fragmentList2";
public static final String FRAGMENT_EXTRA = "fragmentExtra";
public static final String FRAGMENT_DOWNLOAD_FLIPPER = "fragmentDownloadFlipper";
public static final String FRAGMENT_NAME = "fragmentName";
public static final String FRAGMENT_POSITION = "fragmentPosition";
// Name of the preferences file.
public static final String PREFERENCES_FILE_NAME = "net.nullsum.audinaut_preferences";
public static final String OFFLINE_SYNC_NAME = "net.nullsum.audinaut.offline";
public static final String OFFLINE_SYNC_DEFAULT = "syncDefaults";
public static final String OFFLINE_SYNC_NAME = "net.nullsum.audinaut.offline";
public static final String OFFLINE_SYNC_DEFAULT = "syncDefaults";
// Account prefs
public static final String SYNC_ACCOUNT_NAME = "Subsonic Account";
public static final String SYNC_ACCOUNT_TYPE = "Audinaut";
public static final String SYNC_ACCOUNT_PLAYLIST_AUTHORITY = "net.nullsum.audinaut.playlists.provider";
public static final String SYNC_ACCOUNT_MOST_RECENT_AUTHORITY = "net.nullsum.audinaut.mostrecent.provider";
// Account prefs
public static final String SYNC_ACCOUNT_NAME = "Subsonic Account";
public static final String SYNC_ACCOUNT_TYPE = "Audinaut";
public static final String SYNC_ACCOUNT_PLAYLIST_AUTHORITY = "net.nullsum.audinaut.playlists.provider";
public static final String SYNC_ACCOUNT_MOST_RECENT_AUTHORITY = "net.nullsum.audinaut.mostrecent.provider";
public static final String TASKER_EXTRA_BUNDLE = "com.twofortyfouram.locale.intent.extra.BUNDLE";
public static final String TASKER_EXTRA_BUNDLE = "com.twofortyfouram.locale.intent.extra.BUNDLE";
public static final String ALBUM_ART_FILE = "albumart.jpg";

View File

@ -15,100 +15,100 @@ import net.nullsum.audinaut.view.SongView;
import net.nullsum.audinaut.view.UpdateView;
public class DownloadFileItemHelperCallback extends ItemTouchHelper.SimpleCallback {
private static final String TAG = DownloadFileItemHelperCallback.class.getSimpleName();
private static final String TAG = DownloadFileItemHelperCallback.class.getSimpleName();
private SubsonicFragment fragment;
private boolean mainList;
private SubsonicFragment fragment;
private boolean mainList;
private BackgroundTask pendingTask = null;
private Deque pendingOperations = new ArrayDeque();
private BackgroundTask pendingTask = null;
private Deque pendingOperations = new ArrayDeque();
public DownloadFileItemHelperCallback(SubsonicFragment fragment, boolean mainList) {
super(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
this.fragment = fragment;
this.mainList = mainList;
}
public DownloadFileItemHelperCallback(SubsonicFragment fragment, boolean mainList) {
super(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
this.fragment = fragment;
this.mainList = mainList;
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder fromHolder, RecyclerView.ViewHolder toHolder) {
int from = fromHolder.getAdapterPosition();
int to = toHolder.getAdapterPosition();
getSectionAdapter().moveItem(from, to);
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder fromHolder, RecyclerView.ViewHolder toHolder) {
int from = fromHolder.getAdapterPosition();
int to = toHolder.getAdapterPosition();
getSectionAdapter().moveItem(from, to);
synchronized (pendingOperations) {
pendingOperations.add(new Pair<>(from, to));
updateDownloadService();
}
return true;
}
synchronized (pendingOperations) {
pendingOperations.add(new Pair<>(from, to));
updateDownloadService();
}
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
SongView songView = (SongView) ((UpdateView.UpdateViewHolder) viewHolder).getUpdateView();
DownloadFile downloadFile = songView.getDownloadFile();
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
SongView songView = (SongView) ((UpdateView.UpdateViewHolder) viewHolder).getUpdateView();
DownloadFile downloadFile = songView.getDownloadFile();
getSectionAdapter().removeItem(downloadFile);
synchronized (pendingOperations) {
pendingOperations.add(downloadFile);
updateDownloadService();
}
}
getSectionAdapter().removeItem(downloadFile);
synchronized (pendingOperations) {
pendingOperations.add(downloadFile);
updateDownloadService();
}
}
public DownloadService getDownloadService() {
return fragment.getDownloadService();
}
public SectionAdapter getSectionAdapter() {
return fragment.getCurrentAdapter();
}
public DownloadService getDownloadService() {
return fragment.getDownloadService();
}
public SectionAdapter getSectionAdapter() {
return fragment.getCurrentAdapter();
}
private void updateDownloadService() {
if(pendingTask == null) {
final DownloadService downloadService = getDownloadService();
if(downloadService == null) {
return;
}
private void updateDownloadService() {
if(pendingTask == null) {
final DownloadService downloadService = getDownloadService();
if(downloadService == null) {
return;
}
pendingTask = new SilentBackgroundTask<Void>(downloadService) {
@Override
protected Void doInBackground() throws Throwable {
boolean running = true;
while(running) {
Object nextOperation = null;
synchronized (pendingOperations) {
if(!pendingOperations.isEmpty()) {
nextOperation = pendingOperations.remove();
}
}
pendingTask = new SilentBackgroundTask<Void>(downloadService) {
@Override
protected Void doInBackground() throws Throwable {
boolean running = true;
while(running) {
Object nextOperation = null;
synchronized (pendingOperations) {
if(!pendingOperations.isEmpty()) {
nextOperation = pendingOperations.remove();
}
}
if(nextOperation != null) {
if(nextOperation instanceof Pair) {
Pair<Integer, Integer> swap = (Pair) nextOperation;
downloadService.swap(mainList, swap.getFirst(), swap.getSecond());
} else if(nextOperation instanceof DownloadFile) {
DownloadFile downloadFile = (DownloadFile) nextOperation;
if(mainList) {
downloadService.remove(downloadFile);
} else {
downloadService.removeBackground(downloadFile);
}
}
} else {
running = false;
}
}
if(nextOperation != null) {
if(nextOperation instanceof Pair) {
Pair<Integer, Integer> swap = (Pair) nextOperation;
downloadService.swap(mainList, swap.getFirst(), swap.getSecond());
} else if(nextOperation instanceof DownloadFile) {
DownloadFile downloadFile = (DownloadFile) nextOperation;
if(mainList) {
downloadService.remove(downloadFile);
} else {
downloadService.removeBackground(downloadFile);
}
}
} else {
running = false;
}
}
synchronized (pendingOperations) {
pendingTask = null;
synchronized (pendingOperations) {
pendingTask = null;
// Start a task if this is non-empty. Means someone added while we were running operations
if(!pendingOperations.isEmpty()) {
updateDownloadService();
}
}
return null;
}
};
pendingTask.execute();
}
}
// Start a task if this is non-empty. Means someone added while we were running operations
if(!pendingOperations.isEmpty()) {
updateDownloadService();
}
}
return null;
}
};
pendingTask.execute();
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.util;
@ -32,71 +32,71 @@ import java.util.WeakHashMap;
import net.nullsum.audinaut.R;
public class DrawableTint {
private static final Map<Integer, Integer> attrMap = new HashMap<>();
private static final WeakHashMap<Integer, Drawable> tintedDrawables = new WeakHashMap<>();
private static final Map<Integer, Integer> attrMap = new HashMap<>();
private static final WeakHashMap<Integer, Drawable> tintedDrawables = new WeakHashMap<>();
public static Drawable getTintedDrawable(Context context, @DrawableRes int drawableRes) {
return getTintedDrawable(context, drawableRes, R.attr.colorAccent);
}
public static Drawable getTintedDrawable(Context context, @DrawableRes int drawableRes, @AttrRes int colorAttr) {
if(tintedDrawables.containsKey(drawableRes)) {
return tintedDrawables.get(drawableRes);
}
public static Drawable getTintedDrawable(Context context, @DrawableRes int drawableRes) {
return getTintedDrawable(context, drawableRes, R.attr.colorAccent);
}
public static Drawable getTintedDrawable(Context context, @DrawableRes int drawableRes, @AttrRes int colorAttr) {
if(tintedDrawables.containsKey(drawableRes)) {
return tintedDrawables.get(drawableRes);
}
int color = getColorRes(context, colorAttr);
Drawable background = context.getResources().getDrawable(drawableRes);
background.setColorFilter(color, PorterDuff.Mode.SRC_IN);
tintedDrawables.put(drawableRes, background);
return background;
}
public static Drawable getTintedDrawableFromColor(Context context, @DrawableRes int drawableRes, @ColorRes int colorRes) {
if(tintedDrawables.containsKey(drawableRes)) {
return tintedDrawables.get(drawableRes);
}
int color = getColorRes(context, colorAttr);
Drawable background = context.getResources().getDrawable(drawableRes);
background.setColorFilter(color, PorterDuff.Mode.SRC_IN);
tintedDrawables.put(drawableRes, background);
return background;
}
public static Drawable getTintedDrawableFromColor(Context context, @DrawableRes int drawableRes, @ColorRes int colorRes) {
if(tintedDrawables.containsKey(drawableRes)) {
return tintedDrawables.get(drawableRes);
}
int color = context.getResources().getColor(colorRes);
Drawable background = context.getResources().getDrawable(drawableRes);
background.setColorFilter(color, PorterDuff.Mode.SRC_IN);
tintedDrawables.put(drawableRes, background);
return background;
}
public static int getColorRes(Context context, @AttrRes int colorAttr) {
int color;
if(attrMap.containsKey(colorAttr)) {
color = attrMap.get(colorAttr);
} else {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(colorAttr, typedValue, true);
color = typedValue.data;
attrMap.put(colorAttr, color);
}
int color = context.getResources().getColor(colorRes);
Drawable background = context.getResources().getDrawable(drawableRes);
background.setColorFilter(color, PorterDuff.Mode.SRC_IN);
tintedDrawables.put(drawableRes, background);
return background;
}
public static int getColorRes(Context context, @AttrRes int colorAttr) {
int color;
if(attrMap.containsKey(colorAttr)) {
color = attrMap.get(colorAttr);
} else {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(colorAttr, typedValue, true);
color = typedValue.data;
attrMap.put(colorAttr, color);
}
return color;
}
public static int getDrawableRes(Context context, @AttrRes int drawableAttr) {
if(attrMap.containsKey(drawableAttr)) {
return attrMap.get(drawableAttr);
} else {
int[] attrs = new int[]{drawableAttr};
TypedArray typedArray = context.obtainStyledAttributes(attrs);
@DrawableRes int drawableRes = typedArray.getResourceId(0, 0);
typedArray.recycle();
attrMap.put(drawableAttr, drawableRes);
return drawableRes;
}
}
public static Drawable getTintedAttrDrawable(Context context, @AttrRes int drawableAttr, @AttrRes int colorAttr) {
if(tintedDrawables.containsKey(drawableAttr)) {
return getTintedDrawable(context, attrMap.get(drawableAttr), colorAttr);
}
return color;
}
public static int getDrawableRes(Context context, @AttrRes int drawableAttr) {
if(attrMap.containsKey(drawableAttr)) {
return attrMap.get(drawableAttr);
} else {
int[] attrs = new int[]{drawableAttr};
TypedArray typedArray = context.obtainStyledAttributes(attrs);
@DrawableRes int drawableRes = typedArray.getResourceId(0, 0);
typedArray.recycle();
attrMap.put(drawableAttr, drawableRes);
return drawableRes;
}
}
public static Drawable getTintedAttrDrawable(Context context, @AttrRes int drawableAttr, @AttrRes int colorAttr) {
if(tintedDrawables.containsKey(drawableAttr)) {
return getTintedDrawable(context, attrMap.get(drawableAttr), colorAttr);
}
@DrawableRes int drawableRes = getDrawableRes(context, drawableAttr);
return getTintedDrawable(context, drawableRes, colorAttr);
}
@DrawableRes int drawableRes = getDrawableRes(context, drawableAttr);
return getTintedDrawable(context, drawableRes, colorAttr);
}
public static void wipeTintCache() {
attrMap.clear();
tintedDrawables.clear();
}
public static void wipeTintCache() {
attrMap.clear();
tintedDrawables.clear();
}
}

View File

@ -1,20 +1,20 @@
/*
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 2016 (C) Scott Jackson
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 2016 (C) Scott Jackson
*/
package net.nullsum.audinaut.util;
public final class EnvironmentVariables {
public static final String PASTEBIN_DEV_KEY = "";
public static final String PASTEBIN_DEV_KEY = "";
}

View File

@ -1,16 +1,16 @@
/*
This file is part of ServerProxy.
SocketProxy 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 2014 (C) Scott Jackson
This file is part of ServerProxy.
SocketProxy 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 2014 (C) Scott Jackson
*/
package net.nullsum.audinaut.util;
@ -29,191 +29,191 @@ import android.util.Log;
import net.nullsum.audinaut.util.ServerProxy;
public class FileProxy extends ServerProxy {
private static final String TAG = FileProxy.class.getSimpleName();
private static final String TAG = FileProxy.class.getSimpleName();
public FileProxy(Context context) {
super(context);
}
public FileProxy(Context context) {
super(context);
}
protected ProxyTask getTask(Socket client) {
return new StreamFileTask(client);
}
protected ProxyTask getTask(Socket client) {
return new StreamFileTask(client);
}
protected class StreamFileTask extends ProxyTask {
File file;
protected class StreamFileTask extends ProxyTask {
File file;
public StreamFileTask(Socket client) {
super(client);
}
public StreamFileTask(Socket client) {
super(client);
}
@Override
public boolean processRequest() {
if(!super.processRequest()) {
return false;
}
@Override
public boolean processRequest() {
if(!super.processRequest()) {
return false;
}
Log.i(TAG, "Processing request for file " + path);
file = getFile(path);
if (!file.exists()) {
Log.e(TAG, "File " + path + " does not exist");
return false;
}
Log.i(TAG, "Processing request for file " + path);
file = getFile(path);
if (!file.exists()) {
Log.e(TAG, "File " + path + " does not exist");
return false;
}
// Make sure to not try to read past where the file is downloaded
if(cbSkip != 0 && cbSkip >= file.length()) {
return false;
}
// Make sure to not try to read past where the file is downloaded
if(cbSkip != 0 && cbSkip >= file.length()) {
return false;
}
return true;
}
return true;
}
File getFile(String path) {
return new File(path);
}
File getFile(String path) {
return new File(path);
}
Long getContentLength() {
return file.length();
}
long getFileSize() {
return file.length();
}
Long getContentLength() {
return file.length();
}
long getFileSize() {
return file.length();
}
@Override
public void run() {
Long contentLength = getContentLength();
@Override
public void run() {
Long contentLength = getContentLength();
// Create HTTP header
String headers;
if(cbSkip == 0) {
headers = "HTTP/1.0 200 OK\r\n";
} else {
headers = "HTTP/1.0 206 OK\r\n";
headers += "Content-Range: bytes " + cbSkip + "-" + (file.length() - 1) + "/";
if(contentLength == null) {
headers += "*";
} else {
headers += contentLength;
}
headers += "\r\n";
// Create HTTP header
String headers;
if(cbSkip == 0) {
headers = "HTTP/1.0 200 OK\r\n";
} else {
headers = "HTTP/1.0 206 OK\r\n";
headers += "Content-Range: bytes " + cbSkip + "-" + (file.length() - 1) + "/";
if(contentLength == null) {
headers += "*";
} else {
headers += contentLength;
}
headers += "\r\n";
Log.i(TAG, "Streaming starts from: " + cbSkip);
}
Log.i(TAG, "Streaming starts from: " + cbSkip);
}
String name = file.getPath();
int index = name.lastIndexOf('.');
String ext = "";
if(index != -1) {
ext = name.substring(index + 1).toLowerCase();
}
if("mp3".equals(ext)) {
headers += "Content-Type: audio/mpeg\r\n";
} else {
headers += "Content-Type: " + "application/octet-stream" + "\r\n";
}
String name = file.getPath();
int index = name.lastIndexOf('.');
String ext = "";
if(index != -1) {
ext = name.substring(index + 1).toLowerCase();
}
if("mp3".equals(ext)) {
headers += "Content-Type: audio/mpeg\r\n";
} else {
headers += "Content-Type: " + "application/octet-stream" + "\r\n";
}
long fileSize;
if(contentLength == null) {
fileSize = getFileSize();
} else {
fileSize = contentLength;
if(cbSkip > 0) {
headers += "Content-Length: " + (fileSize - cbSkip) + "\r\n";
} else {
headers += "Content-Length: " + fileSize + "\r\n";
}
headers += "Accept-Ranges: bytes \r\n";
}
Log.i(TAG, "Streaming fileSize: " + fileSize);
long fileSize;
if(contentLength == null) {
fileSize = getFileSize();
} else {
fileSize = contentLength;
if(cbSkip > 0) {
headers += "Content-Length: " + (fileSize - cbSkip) + "\r\n";
} else {
headers += "Content-Length: " + fileSize + "\r\n";
}
headers += "Accept-Ranges: bytes \r\n";
}
Log.i(TAG, "Streaming fileSize: " + fileSize);
headers += "Connection: close\r\n";
headers += "\r\n";
headers += "Connection: close\r\n";
headers += "\r\n";
long cbToSend = fileSize - cbSkip;
OutputStream output = null;
byte[] buff = new byte[64 * 1024];
try {
output = new BufferedOutputStream(client.getOutputStream(), 32*1024);
output.write(headers.getBytes());
long cbToSend = fileSize - cbSkip;
OutputStream output = null;
byte[] buff = new byte[64 * 1024];
try {
output = new BufferedOutputStream(client.getOutputStream(), 32*1024);
output.write(headers.getBytes());
// Make sure to have file lock
onStart();
// Make sure to have file lock
onStart();
// Loop as long as there's stuff to send
while (isRunning && !client.isClosed()) {
onResume();
// Loop as long as there's stuff to send
while (isRunning && !client.isClosed()) {
onResume();
// See if there's more to send
int cbSentThisBatch = 0;
if (file.exists()) {
FileInputStream input = new FileInputStream(file);
input.skip(cbSkip);
int cbToSendThisBatch = input.available();
while (cbToSendThisBatch > 0) {
int cbToRead = Math.min(cbToSendThisBatch, buff.length);
int cbRead = input.read(buff, 0, cbToRead);
if (cbRead == -1) {
break;
}
cbToSendThisBatch -= cbRead;
cbToSend -= cbRead;
output.write(buff, 0, cbRead);
output.flush();
cbSkip += cbRead;
cbSentThisBatch += cbRead;
}
input.close();
}
// See if there's more to send
int cbSentThisBatch = 0;
if (file.exists()) {
FileInputStream input = new FileInputStream(file);
input.skip(cbSkip);
int cbToSendThisBatch = input.available();
while (cbToSendThisBatch > 0) {
int cbToRead = Math.min(cbToSendThisBatch, buff.length);
int cbRead = input.read(buff, 0, cbToRead);
if (cbRead == -1) {
break;
}
cbToSendThisBatch -= cbRead;
cbToSend -= cbRead;
output.write(buff, 0, cbRead);
output.flush();
cbSkip += cbRead;
cbSentThisBatch += cbRead;
}
input.close();
}
// Done regardless of whether or not it thinks it is
if(isWorkDone()) {
break;
}
// Done regardless of whether or not it thinks it is
if(isWorkDone()) {
break;
}
// If we did nothing this batch, block for a second
if (cbSentThisBatch == 0) {
Log.d(TAG, "Blocking until more data appears (" + cbToSend + ")");
Thread.sleep(1000);
}
}
// If we did nothing this batch, block for a second
if (cbSentThisBatch == 0) {
Log.d(TAG, "Blocking until more data appears (" + cbToSend + ")");
Thread.sleep(1000);
}
}
// Release file lock, use of stream proxy means nothing else is using it
onStop();
}
catch (SocketException socketException) {
Log.e(TAG, "SocketException() thrown, proxy client has probably closed. This can exit harmlessly");
// Release file lock, use of stream proxy means nothing else is using it
onStop();
}
catch (SocketException socketException) {
Log.e(TAG, "SocketException() thrown, proxy client has probably closed. This can exit harmlessly");
// Release file lock, use of stream proxy means nothing else is using it
onStop();
}
catch (Exception e) {
Log.e(TAG, "Exception thrown from streaming task:");
Log.e(TAG, e.getClass().getName() + " : " + e.getLocalizedMessage());
}
// Release file lock, use of stream proxy means nothing else is using it
onStop();
}
catch (Exception e) {
Log.e(TAG, "Exception thrown from streaming task:");
Log.e(TAG, e.getClass().getName() + " : " + e.getLocalizedMessage());
}
// Cleanup
try {
if (output != null) {
output.close();
}
client.close();
}
catch (IOException e) {
Log.e(TAG, "IOException while cleaning up streaming task:");
Log.e(TAG, e.getClass().getName() + " : " + e.getLocalizedMessage());
}
}
// Cleanup
try {
if (output != null) {
output.close();
}
client.close();
}
catch (IOException e) {
Log.e(TAG, "IOException while cleaning up streaming task:");
Log.e(TAG, e.getClass().getName() + " : " + e.getLocalizedMessage());
}
}
public void onStart() {
public void onStart() {
}
public void onStop() {
}
public void onStop() {
}
public void onResume() {
}
public void onResume() {
}
public boolean isWorkDone() {
return cbSkip >= file.length();
}
}
}
public boolean isWorkDone() {
return cbSkip >= file.length();
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -54,386 +54,386 @@ import net.nullsum.audinaut.service.MusicServiceFactory;
* @author Sindre Mehus
*/
public class ImageLoader {
private static final String TAG = ImageLoader.class.getSimpleName();
public static final String PLAYLIST_PREFIX = "pl-";
private static final String TAG = ImageLoader.class.getSimpleName();
public static final String PLAYLIST_PREFIX = "pl-";
private Context context;
private LruCache<String, Bitmap> cache;
private Handler handler;
private Bitmap nowPlaying;
private Bitmap nowPlayingSmall;
private final int imageSizeDefault;
private final int imageSizeLarge;
private boolean clearingCache = false;
private final int cacheSize;
private Context context;
private LruCache<String, Bitmap> cache;
private Handler handler;
private Bitmap nowPlaying;
private Bitmap nowPlayingSmall;
private final int imageSizeDefault;
private final int imageSizeLarge;
private boolean clearingCache = false;
private final int cacheSize;
private final static int[] COLORS = {0xFF33B5E5, 0xFFAA66CC, 0xFF99CC00, 0xFFFFBB33, 0xFFFF4444};
private final static int[] COLORS = {0xFF33B5E5, 0xFFAA66CC, 0xFF99CC00, 0xFFFFBB33, 0xFFFF4444};
public ImageLoader(Context context) {
this.context = context;
handler = new Handler(Looper.getMainLooper());
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
cacheSize = maxMemory / 4;
public ImageLoader(Context context) {
this.context = context;
handler = new Handler(Looper.getMainLooper());
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
cacheSize = maxMemory / 4;
// Determine the density-dependent image sizes.
imageSizeDefault = context.getResources().getDrawable(R.drawable.unknown_album).getIntrinsicHeight();
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
imageSizeLarge = Math.round(Math.min(metrics.widthPixels, metrics.heightPixels));
// Determine the density-dependent image sizes.
imageSizeDefault = context.getResources().getDrawable(R.drawable.unknown_album).getIntrinsicHeight();
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
imageSizeLarge = Math.round(Math.min(metrics.widthPixels, metrics.heightPixels));
cache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
}
cache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
}
@Override
protected void entryRemoved(boolean evicted, String key, Bitmap oldBitmap, Bitmap newBitmap) {
if(evicted) {
if((oldBitmap != nowPlaying && oldBitmap != nowPlayingSmall) || clearingCache) {
oldBitmap.recycle();
} else if(oldBitmap != newBitmap) {
cache.put(key, oldBitmap);
}
}
}
};
}
@Override
protected void entryRemoved(boolean evicted, String key, Bitmap oldBitmap, Bitmap newBitmap) {
if(evicted) {
if((oldBitmap != nowPlaying && oldBitmap != nowPlayingSmall) || clearingCache) {
oldBitmap.recycle();
} else if(oldBitmap != newBitmap) {
cache.put(key, oldBitmap);
}
}
}
};
}
public void clearCache() {
nowPlaying = null;
nowPlayingSmall = null;
new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
clearingCache = true;
cache.evictAll();
clearingCache = false;
return null;
}
}.execute();
}
public void onLowMemory(float percent) {
Log.i(TAG, "Cache size: " + cache.size() + " => " + Math.round(cacheSize * (1 - percent)) + " out of " + cache.maxSize());
cache.resize(Math.round(cacheSize * (1 - percent)));
}
public void onUIVisible() {
if(cache.maxSize() != cacheSize) {
Log.i(TAG, "Returned to full cache size");
cache.resize(cacheSize);
}
}
public void clearCache() {
nowPlaying = null;
nowPlayingSmall = null;
new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
clearingCache = true;
cache.evictAll();
clearingCache = false;
return null;
}
}.execute();
}
public void onLowMemory(float percent) {
Log.i(TAG, "Cache size: " + cache.size() + " => " + Math.round(cacheSize * (1 - percent)) + " out of " + cache.maxSize());
cache.resize(Math.round(cacheSize * (1 - percent)));
}
public void onUIVisible() {
if(cache.maxSize() != cacheSize) {
Log.i(TAG, "Returned to full cache size");
cache.resize(cacheSize);
}
}
public void setNowPlayingSmall(Bitmap bitmap) {
nowPlayingSmall = bitmap;
}
public void setNowPlayingSmall(Bitmap bitmap) {
nowPlayingSmall = bitmap;
}
private Bitmap getUnknownImage(MusicDirectory.Entry entry, int size) {
String key;
int color;
if(entry == null) {
key = getKey("unknown", size);
color = COLORS[0];
private Bitmap getUnknownImage(MusicDirectory.Entry entry, int size) {
String key;
int color;
if(entry == null) {
key = getKey("unknown", size);
color = COLORS[0];
return getUnknownImage(key, size, color, null, null);
} else {
key = getKey(entry.getId() + "unknown", size);
String hash;
if(entry.getAlbum() != null) {
hash = entry.getAlbum();
} else if(entry.getArtist() != null) {
hash = entry.getArtist();
} else {
hash = entry.getId();
}
color = COLORS[Math.abs(hash.hashCode()) % COLORS.length];
return getUnknownImage(key, size, color, null, null);
} else {
key = getKey(entry.getId() + "unknown", size);
String hash;
if(entry.getAlbum() != null) {
hash = entry.getAlbum();
} else if(entry.getArtist() != null) {
hash = entry.getArtist();
} else {
hash = entry.getId();
}
color = COLORS[Math.abs(hash.hashCode()) % COLORS.length];
return getUnknownImage(key, size, color, entry.getAlbum(), entry.getArtist());
}
}
private Bitmap getUnknownImage(String key, int size, int color, String topText, String bottomText) {
Bitmap bitmap = cache.get(key);
if(bitmap == null) {
bitmap = createUnknownImage(size, color, topText, bottomText);
cache.put(key, bitmap);
}
return getUnknownImage(key, size, color, entry.getAlbum(), entry.getArtist());
}
}
private Bitmap getUnknownImage(String key, int size, int color, String topText, String bottomText) {
Bitmap bitmap = cache.get(key);
if(bitmap == null) {
bitmap = createUnknownImage(size, color, topText, bottomText);
cache.put(key, bitmap);
}
return bitmap;
}
private Bitmap createUnknownImage(int size, int primaryColor, String topText, String bottomText) {
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
return bitmap;
}
private Bitmap createUnknownImage(int size, int primaryColor, String topText, String bottomText) {
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint color = new Paint();
color.setColor(primaryColor);
canvas.drawRect(0, 0, size, size * 2.0f / 3.0f, color);
Paint color = new Paint();
color.setColor(primaryColor);
canvas.drawRect(0, 0, size, size * 2.0f / 3.0f, color);
color.setShader(new LinearGradient(0, 0, 0, size / 3.0f, Color.rgb(82, 82, 82), Color.BLACK, Shader.TileMode.MIRROR));
canvas.drawRect(0, size * 2.0f / 3.0f, size, size, color);
color.setShader(new LinearGradient(0, 0, 0, size / 3.0f, Color.rgb(82, 82, 82), Color.BLACK, Shader.TileMode.MIRROR));
canvas.drawRect(0, size * 2.0f / 3.0f, size, size, color);
if(topText != null || bottomText != null) {
Paint font = new Paint();
font.setFlags(Paint.ANTI_ALIAS_FLAG);
font.setColor(Color.WHITE);
font.setTextSize(3.0f + size * 0.07f);
if(topText != null || bottomText != null) {
Paint font = new Paint();
font.setFlags(Paint.ANTI_ALIAS_FLAG);
font.setColor(Color.WHITE);
font.setTextSize(3.0f + size * 0.07f);
if(topText != null) {
canvas.drawText(topText, size * 0.05f, size * 0.6f, font);
}
if(topText != null) {
canvas.drawText(topText, size * 0.05f, size * 0.6f, font);
}
if(bottomText != null) {
canvas.drawText(bottomText, size * 0.05f, size * 0.8f, font);
}
}
if(bottomText != null) {
canvas.drawText(bottomText, size * 0.05f, size * 0.8f, font);
}
}
return bitmap;
}
return bitmap;
}
public Bitmap getCachedImage(Context context, MusicDirectory.Entry entry, boolean large) {
int size = large ? imageSizeLarge : imageSizeDefault;
if(entry == null || entry.getCoverArt() == null) {
return getUnknownImage(entry, size);
}
public Bitmap getCachedImage(Context context, MusicDirectory.Entry entry, boolean large) {
int size = large ? imageSizeLarge : imageSizeDefault;
if(entry == null || entry.getCoverArt() == null) {
return getUnknownImage(entry, size);
}
Bitmap bitmap = cache.get(getKey(entry.getCoverArt(), size));
if(bitmap == null || bitmap.isRecycled()) {
bitmap = FileUtil.getAlbumArtBitmap(context, entry, size);
String key = getKey(entry.getCoverArt(), size);
cache.put(key, bitmap);
cache.get(key);
}
Bitmap bitmap = cache.get(getKey(entry.getCoverArt(), size));
if(bitmap == null || bitmap.isRecycled()) {
bitmap = FileUtil.getAlbumArtBitmap(context, entry, size);
String key = getKey(entry.getCoverArt(), size);
cache.put(key, bitmap);
cache.get(key);
}
if(bitmap != null && bitmap.isRecycled()) {
bitmap = null;
}
return bitmap;
}
if(bitmap != null && bitmap.isRecycled()) {
bitmap = null;
}
return bitmap;
}
public SilentBackgroundTask loadImage(View view, MusicDirectory.Entry entry, boolean large, boolean crossfade) {
int size = large ? imageSizeLarge : imageSizeDefault;
return loadImage(view, entry, large, size, crossfade);
}
public SilentBackgroundTask loadImage(View view, MusicDirectory.Entry entry, boolean large, int size, boolean crossfade) {
// If we know this a artist, try to load artist info instead
if(entry != null && !entry.isAlbum() && !Util.isOffline(context)) {
SilentBackgroundTask task = new ArtistImageTask(view.getContext(), entry, size, imageSizeLarge, large, view, crossfade);
task.execute();
return task;
} else if(entry != null && entry.getCoverArt() == null && entry.isDirectory() && !Util.isOffline(context)) {
// Try to lookup child cover art
MusicDirectory.Entry firstChild = FileUtil.lookupChild(context, entry, true);
if(firstChild != null) {
entry.setCoverArt(firstChild.getCoverArt());
}
}
public SilentBackgroundTask loadImage(View view, MusicDirectory.Entry entry, boolean large, boolean crossfade) {
int size = large ? imageSizeLarge : imageSizeDefault;
return loadImage(view, entry, large, size, crossfade);
}
public SilentBackgroundTask loadImage(View view, MusicDirectory.Entry entry, boolean large, int size, boolean crossfade) {
// If we know this a artist, try to load artist info instead
if(entry != null && !entry.isAlbum() && !Util.isOffline(context)) {
SilentBackgroundTask task = new ArtistImageTask(view.getContext(), entry, size, imageSizeLarge, large, view, crossfade);
task.execute();
return task;
} else if(entry != null && entry.getCoverArt() == null && entry.isDirectory() && !Util.isOffline(context)) {
// Try to lookup child cover art
MusicDirectory.Entry firstChild = FileUtil.lookupChild(context, entry, true);
if(firstChild != null) {
entry.setCoverArt(firstChild.getCoverArt());
}
}
Bitmap bitmap;
if (entry == null || entry.getCoverArt() == null) {
bitmap = getUnknownImage(entry, size);
setImage(view, Util.createDrawableFromBitmap(context, bitmap), crossfade);
return null;
}
Bitmap bitmap;
if (entry == null || entry.getCoverArt() == null) {
bitmap = getUnknownImage(entry, size);
setImage(view, Util.createDrawableFromBitmap(context, bitmap), crossfade);
return null;
}
bitmap = cache.get(getKey(entry.getCoverArt(), size));
if (bitmap != null && !bitmap.isRecycled()) {
final Drawable drawable = Util.createDrawableFromBitmap(this.context, bitmap);
setImage(view, drawable, crossfade);
if(large) {
nowPlaying = bitmap;
}
return null;
}
bitmap = cache.get(getKey(entry.getCoverArt(), size));
if (bitmap != null && !bitmap.isRecycled()) {
final Drawable drawable = Util.createDrawableFromBitmap(this.context, bitmap);
setImage(view, drawable, crossfade);
if(large) {
nowPlaying = bitmap;
}
return null;
}
if (!large) {
setImage(view, null, false);
}
ImageTask task = new ViewImageTask(view.getContext(), entry, size, imageSizeLarge, large, view, crossfade);
task.execute();
return task;
}
if (!large) {
setImage(view, null, false);
}
ImageTask task = new ViewImageTask(view.getContext(), entry, size, imageSizeLarge, large, view, crossfade);
task.execute();
return task;
}
public SilentBackgroundTask<Void> loadImage(View view, String url, boolean large) {
Bitmap bitmap;
int size = large ? imageSizeLarge : imageSizeDefault;
if (url == null) {
String key = getKey(url + "unknown", size);
int color = COLORS[Math.abs(key.hashCode()) % COLORS.length];
bitmap = getUnknownImage(key, size, color, null, null);
setImage(view, Util.createDrawableFromBitmap(context, bitmap), true);
return null;
}
public SilentBackgroundTask<Void> loadImage(View view, String url, boolean large) {
Bitmap bitmap;
int size = large ? imageSizeLarge : imageSizeDefault;
if (url == null) {
String key = getKey(url + "unknown", size);
int color = COLORS[Math.abs(key.hashCode()) % COLORS.length];
bitmap = getUnknownImage(key, size, color, null, null);
setImage(view, Util.createDrawableFromBitmap(context, bitmap), true);
return null;
}
bitmap = cache.get(getKey(url, size));
if (bitmap != null && !bitmap.isRecycled()) {
final Drawable drawable = Util.createDrawableFromBitmap(this.context, bitmap);
setImage(view, drawable, true);
return null;
}
setImage(view, null, false);
bitmap = cache.get(getKey(url, size));
if (bitmap != null && !bitmap.isRecycled()) {
final Drawable drawable = Util.createDrawableFromBitmap(this.context, bitmap);
setImage(view, drawable, true);
return null;
}
setImage(view, null, false);
SilentBackgroundTask<Void> task = new ViewUrlTask(view.getContext(), view, url, size);
task.execute();
return task;
}
SilentBackgroundTask<Void> task = new ViewUrlTask(view.getContext(), view, url, size);
task.execute();
return task;
}
public SilentBackgroundTask loadImage(View view, Playlist playlist, boolean large, boolean crossfade) {
MusicDirectory.Entry entry = new MusicDirectory.Entry();
String id;
if(Util.isOffline(context)) {
id = PLAYLIST_PREFIX + playlist.getName();
entry.setTitle(playlist.getComment());
} else {
id = PLAYLIST_PREFIX + playlist.getId();
entry.setTitle(playlist.getName());
}
entry.setId(id);
entry.setCoverArt(id);
// So this isn't treated as a artist
entry.setParent("");
public SilentBackgroundTask loadImage(View view, Playlist playlist, boolean large, boolean crossfade) {
MusicDirectory.Entry entry = new MusicDirectory.Entry();
String id;
if(Util.isOffline(context)) {
id = PLAYLIST_PREFIX + playlist.getName();
entry.setTitle(playlist.getComment());
} else {
id = PLAYLIST_PREFIX + playlist.getId();
entry.setTitle(playlist.getName());
}
entry.setId(id);
entry.setCoverArt(id);
// So this isn't treated as a artist
entry.setParent("");
return loadImage(view, entry, large, crossfade);
}
return loadImage(view, entry, large, crossfade);
}
private String getKey(String coverArtId, int size) {
return coverArtId + size;
}
private String getKey(String coverArtId, int size) {
return coverArtId + size;
}
private void setImage(View view, final Drawable drawable, boolean crossfade) {
if (view instanceof TextView) {
// Cross-fading is not implemented for TextView since it's not in use. It would be easy to add it, though.
TextView textView = (TextView) view;
textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
} else if (view instanceof ImageView) {
final ImageView imageView = (ImageView) view;
if (crossfade && drawable != null) {
Drawable existingDrawable = imageView.getDrawable();
if (existingDrawable == null) {
Bitmap emptyImage;
if(drawable.getIntrinsicWidth() > 0 && drawable.getIntrinsicHeight() > 0) {
emptyImage = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
} else {
emptyImage = Bitmap.createBitmap(imageSizeDefault, imageSizeDefault, Bitmap.Config.ARGB_8888);
}
existingDrawable = new BitmapDrawable(context.getResources(), emptyImage);
} else if(existingDrawable instanceof TransitionDrawable) {
// This should only ever be used if user is skipping through many songs quickly
TransitionDrawable tmp = (TransitionDrawable) existingDrawable;
existingDrawable = tmp.getDrawable(tmp.getNumberOfLayers() - 1);
}
if(existingDrawable != null && drawable != null) {
Drawable[] layers = new Drawable[]{existingDrawable, drawable};
final TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
imageView.setImageDrawable(transitionDrawable);
transitionDrawable.startTransition(250);
private void setImage(View view, final Drawable drawable, boolean crossfade) {
if (view instanceof TextView) {
// Cross-fading is not implemented for TextView since it's not in use. It would be easy to add it, though.
TextView textView = (TextView) view;
textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
} else if (view instanceof ImageView) {
final ImageView imageView = (ImageView) view;
if (crossfade && drawable != null) {
Drawable existingDrawable = imageView.getDrawable();
if (existingDrawable == null) {
Bitmap emptyImage;
if(drawable.getIntrinsicWidth() > 0 && drawable.getIntrinsicHeight() > 0) {
emptyImage = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
} else {
emptyImage = Bitmap.createBitmap(imageSizeDefault, imageSizeDefault, Bitmap.Config.ARGB_8888);
}
existingDrawable = new BitmapDrawable(context.getResources(), emptyImage);
} else if(existingDrawable instanceof TransitionDrawable) {
// This should only ever be used if user is skipping through many songs quickly
TransitionDrawable tmp = (TransitionDrawable) existingDrawable;
existingDrawable = tmp.getDrawable(tmp.getNumberOfLayers() - 1);
}
if(existingDrawable != null && drawable != null) {
Drawable[] layers = new Drawable[]{existingDrawable, drawable};
final TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
imageView.setImageDrawable(transitionDrawable);
transitionDrawable.startTransition(250);
// Get rid of transition drawable after transition occurs
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Only execute if still on same transition drawable
if (imageView.getDrawable() == transitionDrawable) {
imageView.setImageDrawable(drawable);
}
}
}, 500L);
} else {
imageView.setImageDrawable(drawable);
}
} else {
imageView.setImageDrawable(drawable);
}
}
}
// Get rid of transition drawable after transition occurs
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Only execute if still on same transition drawable
if (imageView.getDrawable() == transitionDrawable) {
imageView.setImageDrawable(drawable);
}
}
}, 500L);
} else {
imageView.setImageDrawable(drawable);
}
} else {
imageView.setImageDrawable(drawable);
}
}
}
public abstract class ImageTask extends SilentBackgroundTask<Void> {
private final Context mContext;
protected final MusicDirectory.Entry mEntry;
private final int mSize;
private final int mSaveSize;
private final boolean mIsNowPlaying;
protected Drawable mDrawable;
public abstract class ImageTask extends SilentBackgroundTask<Void> {
private final Context mContext;
protected final MusicDirectory.Entry mEntry;
private final int mSize;
private final int mSaveSize;
private final boolean mIsNowPlaying;
protected Drawable mDrawable;
public ImageTask(Context context, MusicDirectory.Entry entry, int size, int saveSize, boolean isNowPlaying) {
super(context);
mContext = context;
mEntry = entry;
mSize = size;
mSaveSize = saveSize;
mIsNowPlaying = isNowPlaying;
}
public ImageTask(Context context, MusicDirectory.Entry entry, int size, int saveSize, boolean isNowPlaying) {
super(context);
mContext = context;
mEntry = entry;
mSize = size;
mSaveSize = saveSize;
mIsNowPlaying = isNowPlaying;
}
@Override
protected Void doInBackground() throws Throwable {
try {
MusicService musicService = MusicServiceFactory.getMusicService(mContext);
Bitmap bitmap = musicService.getCoverArt(mContext, mEntry, mSize, null, this);
if(bitmap != null) {
String key = getKey(mEntry.getCoverArt(), mSize);
cache.put(key, bitmap);
// Make sure key is the most recently "used"
cache.get(key);
if (mIsNowPlaying) {
nowPlaying = bitmap;
}
} else {
bitmap = getUnknownImage(mEntry, mSize);
}
@Override
protected Void doInBackground() throws Throwable {
try {
MusicService musicService = MusicServiceFactory.getMusicService(mContext);
Bitmap bitmap = musicService.getCoverArt(mContext, mEntry, mSize, null, this);
if(bitmap != null) {
String key = getKey(mEntry.getCoverArt(), mSize);
cache.put(key, bitmap);
// Make sure key is the most recently "used"
cache.get(key);
if (mIsNowPlaying) {
nowPlaying = bitmap;
}
} else {
bitmap = getUnknownImage(mEntry, mSize);
}
mDrawable = Util.createDrawableFromBitmap(mContext, bitmap);
} catch (Throwable x) {
Log.e(TAG, "Failed to download album art.", x);
cancelled.set(true);
}
mDrawable = Util.createDrawableFromBitmap(mContext, bitmap);
} catch (Throwable x) {
Log.e(TAG, "Failed to download album art.", x);
cancelled.set(true);
}
return null;
}
}
return null;
}
}
private class ViewImageTask extends ImageTask {
protected boolean mCrossfade;
private View mView;
private class ViewImageTask extends ImageTask {
protected boolean mCrossfade;
private View mView;
public ViewImageTask(Context context, MusicDirectory.Entry entry, int size, int saveSize, boolean isNowPlaying, View view, boolean crossfade) {
super(context, entry, size, saveSize, isNowPlaying);
public ViewImageTask(Context context, MusicDirectory.Entry entry, int size, int saveSize, boolean isNowPlaying, View view, boolean crossfade) {
super(context, entry, size, saveSize, isNowPlaying);
mView = view;
mCrossfade = crossfade;
}
mView = view;
mCrossfade = crossfade;
}
@Override
protected void done(Void result) {
setImage(mView, mDrawable, mCrossfade);
}
}
@Override
protected void done(Void result) {
setImage(mView, mDrawable, mCrossfade);
}
}
private class ArtistImageTask extends SilentBackgroundTask<Void> {
private final Context mContext;
private final MusicDirectory.Entry mEntry;
private final int mSize;
private final int mSaveSize;
private final boolean mIsNowPlaying;
private Drawable mDrawable;
private boolean mCrossfade;
private View mView;
private class ArtistImageTask extends SilentBackgroundTask<Void> {
private final Context mContext;
private final MusicDirectory.Entry mEntry;
private final int mSize;
private final int mSaveSize;
private final boolean mIsNowPlaying;
private Drawable mDrawable;
private boolean mCrossfade;
private View mView;
private SilentBackgroundTask subTask;
private SilentBackgroundTask subTask;
public ArtistImageTask(Context context, MusicDirectory.Entry entry, int size, int saveSize, boolean isNowPlaying, View view, boolean crossfade) {
super(context);
mContext = context;
mEntry = entry;
mSize = size;
mSaveSize = saveSize;
mIsNowPlaying = isNowPlaying;
mView = view;
mCrossfade = crossfade;
}
public ArtistImageTask(Context context, MusicDirectory.Entry entry, int size, int saveSize, boolean isNowPlaying, View view, boolean crossfade) {
super(context);
mContext = context;
mEntry = entry;
mSize = size;
mSaveSize = saveSize;
mIsNowPlaying = isNowPlaying;
mView = view;
mCrossfade = crossfade;
}
@Override
protected Void doInBackground() throws Throwable {
try {
MusicService musicService = MusicServiceFactory.getMusicService(mContext);
@Override
protected Void doInBackground() throws Throwable {
try {
MusicService musicService = MusicServiceFactory.getMusicService(mContext);
// Figure out whether we are going to get a artist image or the standard image
// Figure out whether we are going to get a artist image or the standard image
if (mEntry != null && mEntry.getCoverArt() == null && mEntry.isDirectory() && !Util.isOffline(context)) {
// Try to lookup child cover art
MusicDirectory.Entry firstChild = FileUtil.lookupChild(context, mEntry, true);
@ -451,72 +451,72 @@ public class ImageLoader {
return null;
}
// Execute whichever way we decided to go
subTask.doInBackground();
} catch (Throwable x) {
Log.e(TAG, "Failed to get artist info", x);
cancelled.set(true);
}
return null;
}
// Execute whichever way we decided to go
subTask.doInBackground();
} catch (Throwable x) {
Log.e(TAG, "Failed to get artist info", x);
cancelled.set(true);
}
return null;
}
@Override
public void done(Void result) {
if(subTask != null) {
subTask.done(result);
} else if(mDrawable != null) {
setImage(mView, mDrawable, mCrossfade);
}
}
}
@Override
public void done(Void result) {
if(subTask != null) {
subTask.done(result);
} else if(mDrawable != null) {
setImage(mView, mDrawable, mCrossfade);
}
}
}
private class ViewUrlTask extends SilentBackgroundTask<Void> {
private final Context mContext;
private final String mUrl;
private final ImageView mView;
private Drawable mDrawable;
private int mSize;
private class ViewUrlTask extends SilentBackgroundTask<Void> {
private final Context mContext;
private final String mUrl;
private final ImageView mView;
private Drawable mDrawable;
private int mSize;
public ViewUrlTask(Context context, View view, String url, int size) {
super(context);
mContext = context;
mView = (ImageView) view;
mUrl = url;
mSize = size;
}
public ViewUrlTask(Context context, View view, String url, int size) {
super(context);
mContext = context;
mView = (ImageView) view;
mUrl = url;
mSize = size;
}
@Override
protected Void doInBackground() throws Throwable {
try {
MusicService musicService = MusicServiceFactory.getMusicService(mContext);
Bitmap bitmap = musicService.getBitmap(mUrl, mSize, mContext, null, this);
if(bitmap != null) {
String key = getKey(mUrl, mSize);
cache.put(key, bitmap);
// Make sure key is the most recently "used"
cache.get(key);
@Override
protected Void doInBackground() throws Throwable {
try {
MusicService musicService = MusicServiceFactory.getMusicService(mContext);
Bitmap bitmap = musicService.getBitmap(mUrl, mSize, mContext, null, this);
if(bitmap != null) {
String key = getKey(mUrl, mSize);
cache.put(key, bitmap);
// Make sure key is the most recently "used"
cache.get(key);
mDrawable = Util.createDrawableFromBitmap(mContext, bitmap);
}
} catch (Throwable x) {
Log.e(TAG, "Failed to download from url " + mUrl, x);
cancelled.set(true);
}
mDrawable = Util.createDrawableFromBitmap(mContext, bitmap);
}
} catch (Throwable x) {
Log.e(TAG, "Failed to download from url " + mUrl, x);
cancelled.set(true);
}
return null;
}
return null;
}
@Override
protected void done(Void result) {
if(mDrawable != null) {
mView.setImageDrawable(mDrawable);
} else {
failedToDownload();
}
}
@Override
protected void done(Void result) {
if(mDrawable != null) {
mView.setImageDrawable(mDrawable);
} else {
failedToDownload();
}
}
protected void failedToDownload() {
protected void failedToDownload() {
}
}
}
}
}

View File

@ -13,61 +13,61 @@ import net.nullsum.audinaut.activity.SubsonicActivity;
public abstract class LoadingTask<T> extends BackgroundTask<T> {
private final Activity tabActivity;
private ProgressDialog loading;
private final boolean cancellable;
private ProgressDialog loading;
private final boolean cancellable;
public LoadingTask(Activity activity) {
super(activity);
tabActivity = activity;
this.cancellable = true;
}
public LoadingTask(Activity activity) {
super(activity);
tabActivity = activity;
this.cancellable = true;
}
public LoadingTask(Activity activity, final boolean cancellable) {
super(activity);
tabActivity = activity;
this.cancellable = cancellable;
this.cancellable = cancellable;
}
@Override
public void execute() {
loading = ProgressDialog.show(tabActivity, "", "Loading. Please Wait...", true, cancellable, new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
cancel();
}
});
public void onCancel(DialogInterface dialog) {
cancel();
}
});
queue.offer(task = new Task() {
@Override
public void onDone(T result) {
if(loading.isShowing()) {
loading.dismiss();
}
done(result);
}
queue.offer(task = new Task() {
@Override
public void onDone(T result) {
if(loading.isShowing()) {
loading.dismiss();
}
done(result);
}
@Override
public void onError(Throwable t) {
if(loading.isShowing()) {
loading.dismiss();
}
error(t);
}
});
@Override
public void onError(Throwable t) {
if(loading.isShowing()) {
loading.dismiss();
}
error(t);
}
});
}
@Override
@Override
public boolean isCancelled() {
return (tabActivity instanceof SubsonicActivity && ((SubsonicActivity) tabActivity).isDestroyedCompat()) || cancelled.get();
}
@Override
@Override
public void updateProgress(final String message) {
if(!cancelled.get()) {
getHandler().post(new Runnable() {
@Override
public void run() {
loading.setMessage(message);
}
});
}
if(!cancelled.get()) {
getHandler().post(new Runnable() {
@Override
public void run() {
loading.setMessage(message);
}
});
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.util;
@ -31,53 +31,53 @@ import net.nullsum.audinaut.view.SongView;
import net.nullsum.audinaut.view.UpdateView;
public final class MenuUtil {
private final static String TAG = MenuUtil.class.getSimpleName();
private final static String TAG = MenuUtil.class.getSimpleName();
public static void hideMenuItems(Context context, Menu menu, UpdateView updateView) {
if(!Util.isOffline(context)) {
// If we are looking at a standard song view, get downloadFile to cache what options to show
if(updateView instanceof SongView) {
SongView songView = (SongView) updateView;
DownloadFile downloadFile = songView.getDownloadFile();
public static void hideMenuItems(Context context, Menu menu, UpdateView updateView) {
if(!Util.isOffline(context)) {
// If we are looking at a standard song view, get downloadFile to cache what options to show
if(updateView instanceof SongView) {
SongView songView = (SongView) updateView;
DownloadFile downloadFile = songView.getDownloadFile();
try {
if(downloadFile != null) {
if(downloadFile.isWorkDone()) {
// Remove permanent cache menu if already perma cached
if(downloadFile.isSaved()) {
menu.setGroupVisible(R.id.hide_pin, false);
}
try {
if(downloadFile != null) {
if(downloadFile.isWorkDone()) {
// Remove permanent cache menu if already perma cached
if(downloadFile.isSaved()) {
menu.setGroupVisible(R.id.hide_pin, false);
}
// Remove cache option no matter what if already downloaded
menu.setGroupVisible(R.id.hide_download, false);
} else {
// Remove delete option if nothing to delete
menu.setGroupVisible(R.id.hide_delete, false);
}
}
} catch(Exception e) {
Log.w(TAG, "Failed to lookup downloadFile info", e);
}
}
// Apply similar logic to album views
else if(updateView instanceof AlbumView || updateView instanceof ArtistView || updateView instanceof ArtistEntryView) {
File folder = null;
if(updateView instanceof AlbumView) {
folder = ((AlbumView) updateView).getFile();
} else if(updateView instanceof ArtistView) {
folder = ((ArtistView) updateView).getFile();
} else if(updateView instanceof ArtistEntryView) {
folder = ((ArtistEntryView) updateView).getFile();
}
// Remove cache option no matter what if already downloaded
menu.setGroupVisible(R.id.hide_download, false);
} else {
// Remove delete option if nothing to delete
menu.setGroupVisible(R.id.hide_delete, false);
}
}
} catch(Exception e) {
Log.w(TAG, "Failed to lookup downloadFile info", e);
}
}
// Apply similar logic to album views
else if(updateView instanceof AlbumView || updateView instanceof ArtistView || updateView instanceof ArtistEntryView) {
File folder = null;
if(updateView instanceof AlbumView) {
folder = ((AlbumView) updateView).getFile();
} else if(updateView instanceof ArtistView) {
folder = ((ArtistView) updateView).getFile();
} else if(updateView instanceof ArtistEntryView) {
folder = ((ArtistEntryView) updateView).getFile();
}
try {
if(folder != null && !folder.exists()) {
menu.setGroupVisible(R.id.hide_delete, false);
}
} catch(Exception e) {
Log.w(TAG, "Failed to lookup album directory info", e);
}
}
}
}
try {
if(folder != null && !folder.exists()) {
menu.setGroupVisible(R.id.hide_delete, false);
}
} catch(Exception e) {
Log.w(TAG, "Failed to lookup album directory info", e);
}
}
}
}
}

View File

@ -24,5 +24,5 @@ package net.nullsum.audinaut.util;
public interface ProgressListener {
void updateProgress(String message);
void updateProgress(int messageId);
void updateCache(int changeCode);
void updateCache(int changeCode);
}

View File

@ -1,16 +1,16 @@
/*
This file is part of ServerProxy.
SocketProxy 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 2014 (C) Scott Jackson
This file is part of ServerProxy.
SocketProxy 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 2014 (C) Scott Jackson
*/
package net.nullsum.audinaut.util;
@ -40,192 +40,192 @@ import android.net.wifi.WifiManager;
import android.util.Log;
public abstract class ServerProxy implements Runnable {
private static final String TAG = ServerProxy.class.getSimpleName();
private static final String TAG = ServerProxy.class.getSimpleName();
private Thread thread;
protected boolean isRunning;
private ServerSocket socket;
private int port;
private Context context;
private Thread thread;
protected boolean isRunning;
private ServerSocket socket;
private int port;
private Context context;
public ServerProxy(Context context) {
// Create listening socket
try {
socket = new ServerSocket(0);
socket.setSoTimeout(5000);
port = socket.getLocalPort();
this.context = context;
} catch (UnknownHostException e) { // impossible
} catch (IOException e) {
Log.e(TAG, "IOException initializing server", e);
}
}
public ServerProxy(Context context) {
// Create listening socket
try {
socket = new ServerSocket(0);
socket.setSoTimeout(5000);
port = socket.getLocalPort();
this.context = context;
} catch (UnknownHostException e) { // impossible
} catch (IOException e) {
Log.e(TAG, "IOException initializing server", e);
}
}
public void start() {
if(socket.isBound()) {
thread = new Thread(this, "Socket Proxy");
thread.start();
} else {
Log.e(TAG, "Attempting to start a non-initialized proxy");
}
}
public void start() {
if(socket.isBound()) {
thread = new Thread(this, "Socket Proxy");
thread.start();
} else {
Log.e(TAG, "Attempting to start a non-initialized proxy");
}
}
public void stop() {
isRunning = false;
if(thread != null) {
thread.interrupt();
}
}
public void stop() {
isRunning = false;
if(thread != null) {
thread.interrupt();
}
}
public String getPrivateAddress(String request) {
return getAddress("127.0.0.1", request);
}
public String getPublicAddress(String request) {
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
public String getPrivateAddress(String request) {
return getAddress("127.0.0.1", request);
}
public String getPublicAddress(String request) {
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
ipAddress = Integer.reverseBytes(ipAddress);
}
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
ipAddress = Integer.reverseBytes(ipAddress);
}
byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray();
String ipAddressString = null;
try {
ipAddressString = InetAddress.getByAddress(ipByteArray).getHostAddress();
} catch(UnknownHostException ex) {
Log.e(TAG, "Unable to get host address.");
}
byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray();
String ipAddressString = null;
try {
ipAddressString = InetAddress.getByAddress(ipByteArray).getHostAddress();
} catch(UnknownHostException ex) {
Log.e(TAG, "Unable to get host address.");
}
return getAddress(ipAddressString, request);
}
private String getAddress(String host, String request) {
try {
return String.format("http://%s:%d/%s", host, port, URLEncoder.encode(request, "UTF-8"));
} catch (UnsupportedEncodingException e) {
return null;
}
}
return getAddress(ipAddressString, request);
}
private String getAddress(String host, String request) {
try {
return String.format("http://%s:%d/%s", host, port, URLEncoder.encode(request, "UTF-8"));
} catch (UnsupportedEncodingException e) {
return null;
}
}
@Override
public void run() {
isRunning = true;
while (isRunning) {
try {
Socket client = socket.accept();
if (client == null) {
continue;
}
Log.i(TAG, "client connected");
@Override
public void run() {
isRunning = true;
while (isRunning) {
try {
Socket client = socket.accept();
if (client == null) {
continue;
}
Log.i(TAG, "client connected");
ProxyTask task = getTask(client);
if (task.processRequest()) {
new Thread(task, "ProxyTask").start();
}
ProxyTask task = getTask(client);
if (task.processRequest()) {
new Thread(task, "ProxyTask").start();
}
} catch (SocketTimeoutException e) {
// Do nothing
} catch (IOException e) {
Log.e(TAG, "Error connecting to client", e);
}
}
Log.i(TAG, "Proxy interrupted. Shutting down.");
}
} catch (SocketTimeoutException e) {
// Do nothing
} catch (IOException e) {
Log.e(TAG, "Error connecting to client", e);
}
}
Log.i(TAG, "Proxy interrupted. Shutting down.");
}
abstract ProxyTask getTask(Socket client);
abstract ProxyTask getTask(Socket client);
protected abstract class ProxyTask implements Runnable {
protected Socket client;
protected String path;
protected int cbSkip = 0;
protected Map<String, String> requestHeaders = new HashMap<>();
protected abstract class ProxyTask implements Runnable {
protected Socket client;
protected String path;
protected int cbSkip = 0;
protected Map<String, String> requestHeaders = new HashMap<>();
public ProxyTask(Socket client) {
this.client = client;
}
public ProxyTask(Socket client) {
this.client = client;
}
protected boolean readRequest() {
InputStream is;
String firstLine;
BufferedReader reader;
try {
is = client.getInputStream();
reader = new BufferedReader(new InputStreamReader(is), 8192);
firstLine = reader.readLine();
} catch (IOException e) {
Log.e(TAG, "Error parsing request", e);
return false;
}
protected boolean readRequest() {
InputStream is;
String firstLine;
BufferedReader reader;
try {
is = client.getInputStream();
reader = new BufferedReader(new InputStreamReader(is), 8192);
firstLine = reader.readLine();
} catch (IOException e) {
Log.e(TAG, "Error parsing request", e);
return false;
}
if (firstLine == null) {
Log.i(TAG, "Proxy client closed connection without a request.");
return false;
}
if (firstLine == null) {
Log.i(TAG, "Proxy client closed connection without a request.");
return false;
}
StringTokenizer st = new StringTokenizer(firstLine);
if(!st.hasMoreTokens()) {
Log.w(TAG, "Unknown request with no tokens");
return false;
} else if(st.countTokens() < 2) {
Log.w(TAG, "Unknown request with no uri: \"" + firstLine + '"');
return false;
}
String method = st.nextToken();
String uri = st.nextToken();
String realUri = uri.substring(1);
StringTokenizer st = new StringTokenizer(firstLine);
if(!st.hasMoreTokens()) {
Log.w(TAG, "Unknown request with no tokens");
return false;
} else if(st.countTokens() < 2) {
Log.w(TAG, "Unknown request with no uri: \"" + firstLine + '"');
return false;
}
String method = st.nextToken();
String uri = st.nextToken();
String realUri = uri.substring(1);
// Process path
try {
path = URLDecoder.decode(realUri, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Unsupported encoding", e);
return false;
}
// Process path
try {
path = URLDecoder.decode(realUri, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Unsupported encoding", e);
return false;
}
// Get all of the headers
try {
String line;
while((line = reader.readLine()) != null && !"".equals(line)) {
int index = line.indexOf(':');
// Ignore headers without ':' or where ':' is the last thing in the string
if(index != -1 && (index + 2) < line.length()) {
String headerName = line.substring(0, index);
String headerValue = line.substring(index + 2);
// Get all of the headers
try {
String line;
while((line = reader.readLine()) != null && !"".equals(line)) {
int index = line.indexOf(':');
// Ignore headers without ':' or where ':' is the last thing in the string
if(index != -1 && (index + 2) < line.length()) {
String headerName = line.substring(0, index);
String headerValue = line.substring(index + 2);
requestHeaders.put(headerName, headerValue);
}
}
} catch(IOException e) {
// Don't really care once past first line
} catch(Exception e) {
Log.w(TAG, "Exception reading request", e);
}
requestHeaders.put(headerName, headerValue);
}
}
} catch(IOException e) {
// Don't really care once past first line
} catch(Exception e) {
Log.w(TAG, "Exception reading request", e);
}
return true;
}
return true;
}
public boolean processRequest() {
if (!readRequest()) {
return false;
}
Log.i(TAG, "Processing request for " + path);
public boolean processRequest() {
if (!readRequest()) {
return false;
}
Log.i(TAG, "Processing request for " + path);
// Try to get range requested
String range = requestHeaders.get("Range");
if(range != null) {
int index = range.indexOf("=");
if(index >= 0) {
range = range.substring(index + 1);
// Try to get range requested
String range = requestHeaders.get("Range");
if(range != null) {
int index = range.indexOf("=");
if(index >= 0) {
range = range.substring(index + 1);
index = range.indexOf("-");
if(index > 0) {
range = range.substring(0, index);
}
index = range.indexOf("-");
if(index > 0) {
range = range.substring(0, index);
}
cbSkip = Integer.parseInt(range);
}
}
cbSkip = Integer.parseInt(range);
}
}
return true;
}
}
return true;
}
}
}

View File

@ -1,20 +1,20 @@
/*
This file is part of Subsonic.
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 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.
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/>.
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
Copyright 2009 (C) Sindre Mehus
*/
package net.nullsum.audinaut.util;
@ -29,16 +29,16 @@ import java.io.IOException;
import net.nullsum.audinaut.util.Constants;
public class SettingsBackupAgent extends BackupAgentHelper {
@Override
public void onCreate() {
super.onCreate();
SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, Constants.PREFERENCES_FILE_NAME);
addHelper("mypreferences", helper);
}
@Override
public void onCreate() {
super.onCreate();
SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, Constants.PREFERENCES_FILE_NAME);
addHelper("mypreferences", helper);
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException{
super.onRestore(data, appVersionCode, newState);
Util.getPreferences(this).edit().remove(Constants.PREFERENCES_KEY_CACHE_LOCATION).apply();
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException{
super.onRestore(data, appVersionCode, newState);
Util.getPreferences(this).edit().remove(Constants.PREFERENCES_KEY_CACHE_LOCATION).apply();
}
}

View File

@ -40,173 +40,173 @@ import net.nullsum.audinaut.util.FileUtil;
*/
public class ShufflePlayBuffer {
private static final String TAG = ShufflePlayBuffer.class.getSimpleName();
private static final String CACHE_FILENAME = "shuffleBuffer.ser";
private static final String TAG = ShufflePlayBuffer.class.getSimpleName();
private static final String CACHE_FILENAME = "shuffleBuffer.ser";
private ScheduledExecutorService executorService;
private Runnable runnable;
private boolean firstRun = true;
private final ArrayList<MusicDirectory.Entry> buffer = new ArrayList<MusicDirectory.Entry>();
private int lastCount = -1;
private DownloadService context;
private boolean awaitingResults = false;
private int capacity;
private int refillThreshold;
private ScheduledExecutorService executorService;
private Runnable runnable;
private boolean firstRun = true;
private final ArrayList<MusicDirectory.Entry> buffer = new ArrayList<MusicDirectory.Entry>();
private int lastCount = -1;
private DownloadService context;
private boolean awaitingResults = false;
private int capacity;
private int refillThreshold;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
private int currentServer;
private String currentFolder = "";
private String genre = "";
private String startYear = "";
private String endYear = "";
private SharedPreferences.OnSharedPreferenceChangeListener listener;
private int currentServer;
private String currentFolder = "";
private String genre = "";
private String startYear = "";
private String endYear = "";
public ShufflePlayBuffer(DownloadService context) {
this.context = context;
public ShufflePlayBuffer(DownloadService context) {
this.context = context;
executorService = Executors.newSingleThreadScheduledExecutor();
runnable = new Runnable() {
@Override
public void run() {
refill();
}
};
executorService.scheduleWithFixedDelay(runnable, 1, 10, TimeUnit.SECONDS);
executorService = Executors.newSingleThreadScheduledExecutor();
runnable = new Runnable() {
@Override
public void run() {
refill();
}
};
executorService.scheduleWithFixedDelay(runnable, 1, 10, TimeUnit.SECONDS);
// Calculate out the capacity and refill threshold based on the user's random size preference
int shuffleListSize = Math.max(1, Integer.parseInt(Util.getPreferences(context).getString(Constants.PREFERENCES_KEY_RANDOM_SIZE, "20")));
// ex: default 20 -> 50
capacity = shuffleListSize * 5 / 2;
capacity = Math.min(500, capacity);
// Calculate out the capacity and refill threshold based on the user's random size preference
int shuffleListSize = Math.max(1, Integer.parseInt(Util.getPreferences(context).getString(Constants.PREFERENCES_KEY_RANDOM_SIZE, "20")));
// ex: default 20 -> 50
capacity = shuffleListSize * 5 / 2;
capacity = Math.min(500, capacity);
// ex: default 20 -> 40
refillThreshold = capacity * 4 / 5;
}
// ex: default 20 -> 40
refillThreshold = capacity * 4 / 5;
}
public List<MusicDirectory.Entry> get(int size) {
clearBufferIfnecessary();
// Make sure fetcher is running if needed
restart();
public List<MusicDirectory.Entry> get(int size) {
clearBufferIfnecessary();
// Make sure fetcher is running if needed
restart();
List<MusicDirectory.Entry> result = new ArrayList<MusicDirectory.Entry>(size);
synchronized (buffer) {
boolean removed = false;
while (!buffer.isEmpty() && result.size() < size) {
result.add(buffer.remove(buffer.size() - 1));
removed = true;
}
List<MusicDirectory.Entry> result = new ArrayList<MusicDirectory.Entry>(size);
synchronized (buffer) {
boolean removed = false;
while (!buffer.isEmpty() && result.size() < size) {
result.add(buffer.remove(buffer.size() - 1));
removed = true;
}
// Re-cache if anything is taken out
if(removed) {
FileUtil.serialize(context, buffer, CACHE_FILENAME);
}
}
Log.i(TAG, "Taking " + result.size() + " songs from shuffle play buffer. " + buffer.size() + " remaining.");
if(result.isEmpty()) {
awaitingResults = true;
}
return result;
}
// Re-cache if anything is taken out
if(removed) {
FileUtil.serialize(context, buffer, CACHE_FILENAME);
}
}
Log.i(TAG, "Taking " + result.size() + " songs from shuffle play buffer. " + buffer.size() + " remaining.");
if(result.isEmpty()) {
awaitingResults = true;
}
return result;
}
public void shutdown() {
executorService.shutdown();
Util.getPreferences(context).unregisterOnSharedPreferenceChangeListener(listener);
}
public void shutdown() {
executorService.shutdown();
Util.getPreferences(context).unregisterOnSharedPreferenceChangeListener(listener);
}
private void restart() {
synchronized(buffer) {
if(buffer.size() <= refillThreshold && lastCount != 0 && executorService.isShutdown()) {
executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleWithFixedDelay(runnable, 0, 10, TimeUnit.SECONDS);
}
}
}
private void restart() {
synchronized(buffer) {
if(buffer.size() <= refillThreshold && lastCount != 0 && executorService.isShutdown()) {
executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleWithFixedDelay(runnable, 0, 10, TimeUnit.SECONDS);
}
}
}
private void refill() {
// Check if active server has changed.
clearBufferIfnecessary();
private void refill() {
// Check if active server has changed.
clearBufferIfnecessary();
if (buffer != null && (buffer.size() > refillThreshold || (!Util.isNetworkConnected(context) && !Util.isOffline(context)) || lastCount == 0)) {
executorService.shutdown();
return;
}
if (buffer != null && (buffer.size() > refillThreshold || (!Util.isNetworkConnected(context) && !Util.isOffline(context)) || lastCount == 0)) {
executorService.shutdown();
return;
}
try {
MusicService service = MusicServiceFactory.getMusicService(context);
try {
MusicService service = MusicServiceFactory.getMusicService(context);
// Get capacity based
int n = capacity - buffer.size();
String folder = null;
if(!Util.isTagBrowsing(context)) {
folder = Util.getSelectedMusicFolderId(context);
}
MusicDirectory songs = service.getRandomSongs(n, folder, genre, startYear, endYear, context, null);
// Get capacity based
int n = capacity - buffer.size();
String folder = null;
if(!Util.isTagBrowsing(context)) {
folder = Util.getSelectedMusicFolderId(context);
}
MusicDirectory songs = service.getRandomSongs(n, folder, genre, startYear, endYear, context, null);
synchronized (buffer) {
lastCount = 0;
for(MusicDirectory.Entry entry: songs.getChildren()) {
if(!buffer.contains(entry)) {
buffer.add(entry);
lastCount++;
}
}
Log.i(TAG, "Refilled shuffle play buffer with " + lastCount + " songs.");
synchronized (buffer) {
lastCount = 0;
for(MusicDirectory.Entry entry: songs.getChildren()) {
if(!buffer.contains(entry)) {
buffer.add(entry);
lastCount++;
}
}
Log.i(TAG, "Refilled shuffle play buffer with " + lastCount + " songs.");
// Cache buffer
FileUtil.serialize(context, buffer, CACHE_FILENAME);
}
} catch (Exception x) {
// Give it one more try before quitting
if(lastCount != -2) {
lastCount = -2;
} else if(lastCount == -2) {
lastCount = 0;
}
Log.w(TAG, "Failed to refill shuffle play buffer.", x);
}
// Cache buffer
FileUtil.serialize(context, buffer, CACHE_FILENAME);
}
} catch (Exception x) {
// Give it one more try before quitting
if(lastCount != -2) {
lastCount = -2;
} else if(lastCount == -2) {
lastCount = 0;
}
Log.w(TAG, "Failed to refill shuffle play buffer.", x);
}
if(awaitingResults) {
awaitingResults = false;
context.checkDownloads();
}
}
if(awaitingResults) {
awaitingResults = false;
context.checkDownloads();
}
}
private void clearBufferIfnecessary() {
synchronized (buffer) {
final SharedPreferences prefs = Util.getPreferences(context);
if (currentServer != Util.getActiveServer(context)
|| !Util.equals(currentFolder, Util.getSelectedMusicFolderId(context))
|| (genre != null && !genre.equals(prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, "")))
|| (startYear != null && !startYear.equals(prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, "")))
|| (endYear != null && !endYear.equals(prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, "")))) {
lastCount = -1;
currentServer = Util.getActiveServer(context);
currentFolder = Util.getSelectedMusicFolderId(context);
genre = prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, "");
startYear = prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, "");
endYear = prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, "");
buffer.clear();
private void clearBufferIfnecessary() {
synchronized (buffer) {
final SharedPreferences prefs = Util.getPreferences(context);
if (currentServer != Util.getActiveServer(context)
|| !Util.equals(currentFolder, Util.getSelectedMusicFolderId(context))
|| (genre != null && !genre.equals(prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, "")))
|| (startYear != null && !startYear.equals(prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, "")))
|| (endYear != null && !endYear.equals(prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, "")))) {
lastCount = -1;
currentServer = Util.getActiveServer(context);
currentFolder = Util.getSelectedMusicFolderId(context);
genre = prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, "");
startYear = prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, "");
endYear = prefs.getString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, "");
buffer.clear();
if(firstRun) {
ArrayList cacheList = FileUtil.deserialize(context, CACHE_FILENAME, ArrayList.class);
if(cacheList != null) {
buffer.addAll(cacheList);
}
if(firstRun) {
ArrayList cacheList = FileUtil.deserialize(context, CACHE_FILENAME, ArrayList.class);
if(cacheList != null) {
buffer.addAll(cacheList);
}
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
clearBufferIfnecessary();
restart();
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
firstRun = false;
} else {
// Clear cache
File file = new File(context.getCacheDir(), CACHE_FILENAME);
file.delete();
}
}
}
}
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
clearBufferIfnecessary();
restart();
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
firstRun = false;
} else {
// Clear cache
File file = new File(context.getCacheDir(), CACHE_FILENAME);
file.delete();
}
}
}
}
}

View File

@ -30,13 +30,13 @@ public abstract class SilentBackgroundTask<T> extends BackgroundTask<T> {
@Override
public void execute() {
queue.offer(task = new Task());
queue.offer(task = new Task());
}
@Override
protected void done(T result) {
// Don't do anything unless overriden
}
@Override
protected void done(T result) {
// Don't do anything unless overriden
}
@Override
public void updateProgress(int messageId) {

View File

@ -1,20 +1,20 @@
/*
This file is part of Subsonic.
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 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.
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/>.
You should have received a copy of the GNU General Public License
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2015 (C) Scott Jackson
Copyright 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.util;
@ -25,17 +25,17 @@ import net.nullsum.audinaut.service.MusicService;
import net.nullsum.audinaut.service.MusicServiceFactory;
public abstract class SilentServiceTask<T> extends SilentBackgroundTask<T> {
protected MusicService musicService;
protected MusicService musicService;
public SilentServiceTask(Context context) {
super(context);
}
public SilentServiceTask(Context context) {
super(context);
}
@Override
protected T doInBackground() throws Throwable {
musicService = MusicServiceFactory.getMusicService(getContext());
return doInBackground(musicService);
}
@Override
protected T doInBackground() throws Throwable {
musicService = MusicServiceFactory.getMusicService(getContext());
return doInBackground(musicService);
}
protected abstract T doInBackground(MusicService musicService) throws Throwable;
protected abstract T doInBackground(MusicService musicService) throws Throwable;
}

View File

@ -1,16 +1,16 @@
/*
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 2015 (C) Scott Jackson
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 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.util;
@ -29,235 +29,235 @@ import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.service.DownloadFile;
public class SongDBHandler extends SQLiteOpenHelper {
private static final String TAG = SongDBHandler.class.getSimpleName();
private static SongDBHandler dbHandler;
private static final String TAG = SongDBHandler.class.getSimpleName();
private static SongDBHandler dbHandler;
private static final int DATABASE_VERSION = 2;
public static final String DATABASE_NAME = "SongsDB";
private static final int DATABASE_VERSION = 2;
public static final String DATABASE_NAME = "SongsDB";
public static final String TABLE_SONGS = "RegisteredSongs";
public static final String SONGS_ID = "id";
public static final String SONGS_SERVER_KEY = "serverKey";
public static final String SONGS_SERVER_ID = "serverId";
public static final String SONGS_COMPLETE_PATH = "completePath";
public static final String SONGS_LAST_PLAYED = "lastPlayed";
public static final String SONGS_LAST_COMPLETED = "lastCompleted";
public static final String TABLE_SONGS = "RegisteredSongs";
public static final String SONGS_ID = "id";
public static final String SONGS_SERVER_KEY = "serverKey";
public static final String SONGS_SERVER_ID = "serverId";
public static final String SONGS_COMPLETE_PATH = "completePath";
public static final String SONGS_LAST_PLAYED = "lastPlayed";
public static final String SONGS_LAST_COMPLETED = "lastCompleted";
private Context context;
private Context context;
private SongDBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
private SongDBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_SONGS + " ( " +
SONGS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
SONGS_SERVER_KEY + " INTEGER NOT NULL, " +
SONGS_SERVER_ID + " TEXT NOT NULL, " +
SONGS_COMPLETE_PATH + " TEXT NOT NULL, " +
SONGS_LAST_PLAYED + " INTEGER, " +
SONGS_LAST_COMPLETED + " INTEGER, " +
"UNIQUE(" + SONGS_SERVER_KEY + ", " + SONGS_SERVER_ID + "))");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_SONGS + " ( " +
SONGS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
SONGS_SERVER_KEY + " INTEGER NOT NULL, " +
SONGS_SERVER_ID + " TEXT NOT NULL, " +
SONGS_COMPLETE_PATH + " TEXT NOT NULL, " +
SONGS_LAST_PLAYED + " INTEGER, " +
SONGS_LAST_COMPLETED + " INTEGER, " +
"UNIQUE(" + SONGS_SERVER_KEY + ", " + SONGS_SERVER_ID + "))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SONGS);
this.onCreate(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SONGS);
this.onCreate(db);
}
public synchronized void addSong(DownloadFile downloadFile) {
addSong(Util.getMostRecentActiveServer(context), downloadFile);
}
public synchronized void addSong(int instance, DownloadFile downloadFile) {
SQLiteDatabase db = this.getWritableDatabase();
addSong(db, instance, downloadFile);
db.close();
}
protected synchronized void addSong(SQLiteDatabase db, DownloadFile downloadFile) {
addSong(db, Util.getMostRecentActiveServer(context), downloadFile);
}
protected synchronized void addSong(SQLiteDatabase db, int instance, DownloadFile downloadFile) {
addSong(db, instance, downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath());
}
public synchronized void addSong(DownloadFile downloadFile) {
addSong(Util.getMostRecentActiveServer(context), downloadFile);
}
public synchronized void addSong(int instance, DownloadFile downloadFile) {
SQLiteDatabase db = this.getWritableDatabase();
addSong(db, instance, downloadFile);
db.close();
}
protected synchronized void addSong(SQLiteDatabase db, DownloadFile downloadFile) {
addSong(db, Util.getMostRecentActiveServer(context), downloadFile);
}
protected synchronized void addSong(SQLiteDatabase db, int instance, DownloadFile downloadFile) {
addSong(db, instance, downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath());
}
protected synchronized void addSong(SQLiteDatabase db, String id, String absolutePath) {
addSong(db, Util.getMostRecentActiveServer(context), id, absolutePath);
}
protected synchronized void addSong(SQLiteDatabase db, int instance, String id, String absolutePath) {
addSongImpl(db, Util.getRestUrlHash(context, instance), id, absolutePath);
}
protected synchronized void addSongImpl(SQLiteDatabase db, int serverKey, String id, String absolutePath) {
ContentValues values = new ContentValues();
values.put(SONGS_SERVER_KEY, serverKey);
values.put(SONGS_SERVER_ID, id);
values.put(SONGS_COMPLETE_PATH, absolutePath);
protected synchronized void addSong(SQLiteDatabase db, String id, String absolutePath) {
addSong(db, Util.getMostRecentActiveServer(context), id, absolutePath);
}
protected synchronized void addSong(SQLiteDatabase db, int instance, String id, String absolutePath) {
addSongImpl(db, Util.getRestUrlHash(context, instance), id, absolutePath);
}
protected synchronized void addSongImpl(SQLiteDatabase db, int serverKey, String id, String absolutePath) {
ContentValues values = new ContentValues();
values.put(SONGS_SERVER_KEY, serverKey);
values.put(SONGS_SERVER_ID, id);
values.put(SONGS_COMPLETE_PATH, absolutePath);
db.insertWithOnConflict(TABLE_SONGS, null, values, SQLiteDatabase.CONFLICT_IGNORE);
}
db.insertWithOnConflict(TABLE_SONGS, null, values, SQLiteDatabase.CONFLICT_IGNORE);
}
public synchronized void addSongs(int instance, List<MusicDirectory.Entry> entries) {
SQLiteDatabase db = this.getWritableDatabase();
public synchronized void addSongs(int instance, List<MusicDirectory.Entry> entries) {
SQLiteDatabase db = this.getWritableDatabase();
List<Pair<String, String>> pairs = new ArrayList<>();
for(MusicDirectory.Entry entry: entries) {
pairs.add(new Pair<>(entry.getId(), FileUtil.getSongFile(context, entry).getAbsolutePath()));
}
addSongs(db, instance, pairs);
List<Pair<String, String>> pairs = new ArrayList<>();
for(MusicDirectory.Entry entry: entries) {
pairs.add(new Pair<>(entry.getId(), FileUtil.getSongFile(context, entry).getAbsolutePath()));
}
addSongs(db, instance, pairs);
db.close();
}
public synchronized void addSongs(SQLiteDatabase db, int instance, List<Pair<String, String>> entries) {
addSongsImpl(db, Util.getRestUrlHash(context, instance), entries);
}
protected synchronized void addSongsImpl(SQLiteDatabase db, int serverKey, List<Pair<String, String>> entries) {
db.beginTransaction();
try {
for (Pair<String, String> entry : entries) {
ContentValues values = new ContentValues();
values.put(SONGS_SERVER_KEY, serverKey);
values.put(SONGS_SERVER_ID, entry.getFirst());
values.put(SONGS_COMPLETE_PATH, entry.getSecond());
// Util.sleepQuietly(10000);
db.close();
}
public synchronized void addSongs(SQLiteDatabase db, int instance, List<Pair<String, String>> entries) {
addSongsImpl(db, Util.getRestUrlHash(context, instance), entries);
}
protected synchronized void addSongsImpl(SQLiteDatabase db, int serverKey, List<Pair<String, String>> entries) {
db.beginTransaction();
try {
for (Pair<String, String> entry : entries) {
ContentValues values = new ContentValues();
values.put(SONGS_SERVER_KEY, serverKey);
values.put(SONGS_SERVER_ID, entry.getFirst());
values.put(SONGS_COMPLETE_PATH, entry.getSecond());
// Util.sleepQuietly(10000);
db.insertWithOnConflict(TABLE_SONGS, null, values, SQLiteDatabase.CONFLICT_IGNORE);
}
db.insertWithOnConflict(TABLE_SONGS, null, values, SQLiteDatabase.CONFLICT_IGNORE);
}
db.setTransactionSuccessful();
} catch(Exception e) {}
db.setTransactionSuccessful();
} catch(Exception e) {}
db.endTransaction();
}
db.endTransaction();
}
public synchronized void setSongPlayed(DownloadFile downloadFile, boolean submission) {
// TODO: In case of offline want to update all matches
Pair<Integer, String> pair = getOnlineSongId(downloadFile);
if(pair == null) {
return;
}
int serverKey = pair.getFirst();
String id = pair.getSecond();
public synchronized void setSongPlayed(DownloadFile downloadFile, boolean submission) {
// TODO: In case of offline want to update all matches
Pair<Integer, String> pair = getOnlineSongId(downloadFile);
if(pair == null) {
return;
}
int serverKey = pair.getFirst();
String id = pair.getSecond();
// Open and make sure song is in db
SQLiteDatabase db = this.getWritableDatabase();
addSongImpl(db, serverKey, id, downloadFile.getSaveFile().getAbsolutePath());
// Open and make sure song is in db
SQLiteDatabase db = this.getWritableDatabase();
addSongImpl(db, serverKey, id, downloadFile.getSaveFile().getAbsolutePath());
// Update song's last played
ContentValues values = new ContentValues();
values.put(submission ? SONGS_LAST_COMPLETED : SONGS_LAST_PLAYED, System.currentTimeMillis());
db.update(TABLE_SONGS, values, SONGS_SERVER_KEY + " = ? AND " + SONGS_SERVER_ID + " = ?", new String[]{Integer.toString(serverKey), id});
db.close();
}
// Update song's last played
ContentValues values = new ContentValues();
values.put(submission ? SONGS_LAST_COMPLETED : SONGS_LAST_PLAYED, System.currentTimeMillis());
db.update(TABLE_SONGS, values, SONGS_SERVER_KEY + " = ? AND " + SONGS_SERVER_ID + " = ?", new String[]{Integer.toString(serverKey), id});
db.close();
}
public boolean hasBeenPlayed(MusicDirectory.Entry entry) {
Long[] lastPlayed = getLastPlayed(entry);
return lastPlayed != null && lastPlayed[0] != null && lastPlayed[0] > 0;
}
public boolean hasBeenCompleted(MusicDirectory.Entry entry) {
Long[] lastPlayed = getLastPlayed(entry);
return lastPlayed != null && lastPlayed[1] != null && lastPlayed[1] > 0;
}
public synchronized Long[] getLastPlayed(MusicDirectory.Entry entry) {
return getLastPlayed(getOnlineSongId(entry));
}
protected synchronized Long[] getLastPlayed(Pair<Integer, String> pair) {
if(pair == null) {
return null;
} else {
return getLastPlayed(pair.getFirst(), pair.getSecond());
}
}
public synchronized Long[] getLastPlayed(int serverKey, String id) {
SQLiteDatabase db = this.getReadableDatabase();
public boolean hasBeenPlayed(MusicDirectory.Entry entry) {
Long[] lastPlayed = getLastPlayed(entry);
return lastPlayed != null && lastPlayed[0] != null && lastPlayed[0] > 0;
}
public boolean hasBeenCompleted(MusicDirectory.Entry entry) {
Long[] lastPlayed = getLastPlayed(entry);
return lastPlayed != null && lastPlayed[1] != null && lastPlayed[1] > 0;
}
public synchronized Long[] getLastPlayed(MusicDirectory.Entry entry) {
return getLastPlayed(getOnlineSongId(entry));
}
protected synchronized Long[] getLastPlayed(Pair<Integer, String> pair) {
if(pair == null) {
return null;
} else {
return getLastPlayed(pair.getFirst(), pair.getSecond());
}
}
public synchronized Long[] getLastPlayed(int serverKey, String id) {
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {SONGS_LAST_PLAYED, SONGS_LAST_COMPLETED};
Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_SERVER_KEY + " = ? AND " + SONGS_SERVER_ID + " = ?", new String[]{Integer.toString(serverKey), id}, null, null, null, null);
String[] columns = {SONGS_LAST_PLAYED, SONGS_LAST_COMPLETED};
Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_SERVER_KEY + " = ? AND " + SONGS_SERVER_ID + " = ?", new String[]{Integer.toString(serverKey), id}, null, null, null, null);
try {
cursor.moveToFirst();
try {
cursor.moveToFirst();
Long[] dates = new Long[2];
dates[0] = cursor.getLong(0);
dates[1] = cursor.getLong(1);
return dates;
} catch(Exception e) {
return null;
}
finally {
cursor.close();
db.close();
}
}
public synchronized Pair<Integer, String> getOnlineSongId(MusicDirectory.Entry entry) {
return getOnlineSongId(Util.getRestUrlHash(context), entry.getId(), FileUtil.getSongFile(context, entry).getAbsolutePath(), Util.isOffline(context) ? false : true);
}
public synchronized Pair<Integer, String> getOnlineSongId(DownloadFile downloadFile) {
return getOnlineSongId(Util.getRestUrlHash(context), downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath(), Util.isOffline(context) ? false : true);
}
public synchronized Pair<Integer, String> getOnlineSongId(int serverKey, MusicDirectory.Entry entry) {
return getOnlineSongId(serverKey, new DownloadFile(context, entry, true));
}
public synchronized Pair<Integer, String> getOnlineSongId(int serverKey, DownloadFile downloadFile) {
return getOnlineSongId(serverKey, downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath(), true);
}
public synchronized Pair<Integer, String> getOnlineSongId(int serverKey, String id, String savePath, boolean requireServerKey) {
SharedPreferences prefs = Util.getPreferences(context);
String cacheLocn = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null);
if(cacheLocn != null && id.indexOf(cacheLocn) != -1) {
if(requireServerKey) {
return getIdFromPath(serverKey, savePath);
} else {
return getIdFromPath(savePath);
}
} else {
return new Pair<>(serverKey, id);
}
}
public synchronized Pair<Integer, String> getIdFromPath(String path) {
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {SONGS_SERVER_KEY, SONGS_SERVER_ID};
Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_COMPLETE_PATH + " = ?", new String[] { path }, null, null, SONGS_LAST_PLAYED + " DESC", null);
try {
cursor.moveToFirst();
return new Pair(cursor.getInt(0), cursor.getString(1));
} catch(Exception e) {
return null;
}
finally {
Long[] dates = new Long[2];
dates[0] = cursor.getLong(0);
dates[1] = cursor.getLong(1);
return dates;
} catch(Exception e) {
return null;
}
finally {
cursor.close();
db.close();
}
}
public synchronized Pair<Integer, String> getIdFromPath(int serverKey, String path) {
SQLiteDatabase db = this.getReadableDatabase();
db.close();
}
}
String[] columns = {SONGS_SERVER_KEY, SONGS_SERVER_ID};
Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_SERVER_KEY + " = ? AND " + SONGS_COMPLETE_PATH + " = ?", new String[] {Integer.toString(serverKey), path }, null, null, null, null);
public synchronized Pair<Integer, String> getOnlineSongId(MusicDirectory.Entry entry) {
return getOnlineSongId(Util.getRestUrlHash(context), entry.getId(), FileUtil.getSongFile(context, entry).getAbsolutePath(), Util.isOffline(context) ? false : true);
}
public synchronized Pair<Integer, String> getOnlineSongId(DownloadFile downloadFile) {
return getOnlineSongId(Util.getRestUrlHash(context), downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath(), Util.isOffline(context) ? false : true);
}
try {
cursor.moveToFirst();
return new Pair(cursor.getInt(0), cursor.getString(1));
} catch(Exception e) {
return null;
}
finally {
public synchronized Pair<Integer, String> getOnlineSongId(int serverKey, MusicDirectory.Entry entry) {
return getOnlineSongId(serverKey, new DownloadFile(context, entry, true));
}
public synchronized Pair<Integer, String> getOnlineSongId(int serverKey, DownloadFile downloadFile) {
return getOnlineSongId(serverKey, downloadFile.getSong().getId(), downloadFile.getSaveFile().getAbsolutePath(), true);
}
public synchronized Pair<Integer, String> getOnlineSongId(int serverKey, String id, String savePath, boolean requireServerKey) {
SharedPreferences prefs = Util.getPreferences(context);
String cacheLocn = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null);
if(cacheLocn != null && id.indexOf(cacheLocn) != -1) {
if(requireServerKey) {
return getIdFromPath(serverKey, savePath);
} else {
return getIdFromPath(savePath);
}
} else {
return new Pair<>(serverKey, id);
}
}
public synchronized Pair<Integer, String> getIdFromPath(String path) {
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {SONGS_SERVER_KEY, SONGS_SERVER_ID};
Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_COMPLETE_PATH + " = ?", new String[] { path }, null, null, SONGS_LAST_PLAYED + " DESC", null);
try {
cursor.moveToFirst();
return new Pair(cursor.getInt(0), cursor.getString(1));
} catch(Exception e) {
return null;
}
finally {
cursor.close();
db.close();
}
}
db.close();
}
}
public synchronized Pair<Integer, String> getIdFromPath(int serverKey, String path) {
SQLiteDatabase db = this.getReadableDatabase();
public static SongDBHandler getHandler(Context context) {
if(dbHandler == null) {
dbHandler = new SongDBHandler(context);
}
String[] columns = {SONGS_SERVER_KEY, SONGS_SERVER_ID};
Cursor cursor = db.query(TABLE_SONGS, columns, SONGS_SERVER_KEY + " = ? AND " + SONGS_COMPLETE_PATH + " = ?", new String[] {Integer.toString(serverKey), path }, null, null, null, null);
return dbHandler;
}
try {
cursor.moveToFirst();
return new Pair(cursor.getInt(0), cursor.getString(1));
} catch(Exception e) {
return null;
}
finally {
cursor.close();
db.close();
}
}
public static SongDBHandler getHandler(Context context) {
if(dbHandler == null) {
dbHandler = new SongDBHandler(context);
}
return dbHandler;
}
}

View File

@ -18,139 +18,139 @@ import net.nullsum.audinaut.activity.SubsonicFragmentActivity;
* Created by Scott on 11/24/13.
*/
public final class SyncUtil {
private static String TAG = SyncUtil.class.getSimpleName();
private static ArrayList<SyncSet> syncedPlaylists;
private static String url;
private static String TAG = SyncUtil.class.getSimpleName();
private static ArrayList<SyncSet> syncedPlaylists;
private static String url;
private static void checkRestURL(Context context) {
int instance = Util.getActiveServer(context);
String newURL = Util.getRestUrl(context, null, instance, false);
if(url == null || !url.equals(newURL)) {
syncedPlaylists = null;
url = newURL;
}
}
private static void checkRestURL(Context context) {
int instance = Util.getActiveServer(context);
String newURL = Util.getRestUrl(context, null, instance, false);
if(url == null || !url.equals(newURL)) {
syncedPlaylists = null;
url = newURL;
}
}
// Playlist sync
public static boolean isSyncedPlaylist(Context context, String playlistId) {
checkRestURL(context);
if(syncedPlaylists == null) {
syncedPlaylists = getSyncedPlaylists(context);
}
return syncedPlaylists.contains(new SyncSet(playlistId));
}
public static ArrayList<SyncSet> getSyncedPlaylists(Context context) {
return getSyncedPlaylists(context, Util.getActiveServer(context));
}
public static ArrayList<SyncSet> getSyncedPlaylists(Context context, int instance) {
String syncFile = getPlaylistSyncFile(context, instance);
ArrayList<SyncSet> playlists = FileUtil.deserializeCompressed(context, syncFile, ArrayList.class);
if(playlists == null) {
playlists = new ArrayList<SyncSet>();
// Playlist sync
public static boolean isSyncedPlaylist(Context context, String playlistId) {
checkRestURL(context);
if(syncedPlaylists == null) {
syncedPlaylists = getSyncedPlaylists(context);
}
return syncedPlaylists.contains(new SyncSet(playlistId));
}
public static ArrayList<SyncSet> getSyncedPlaylists(Context context) {
return getSyncedPlaylists(context, Util.getActiveServer(context));
}
public static ArrayList<SyncSet> getSyncedPlaylists(Context context, int instance) {
String syncFile = getPlaylistSyncFile(context, instance);
ArrayList<SyncSet> playlists = FileUtil.deserializeCompressed(context, syncFile, ArrayList.class);
if(playlists == null) {
playlists = new ArrayList<SyncSet>();
// Try to convert old style into new style
ArrayList<String> oldPlaylists = FileUtil.deserialize(context, syncFile, ArrayList.class);
// If exists, time to convert!
if(oldPlaylists != null) {
for(String id: oldPlaylists) {
playlists.add(new SyncSet(id));
}
// Try to convert old style into new style
ArrayList<String> oldPlaylists = FileUtil.deserialize(context, syncFile, ArrayList.class);
// If exists, time to convert!
if(oldPlaylists != null) {
for(String id: oldPlaylists) {
playlists.add(new SyncSet(id));
}
FileUtil.serializeCompressed(context, playlists, syncFile);
}
}
return playlists;
}
public static void setSyncedPlaylists(Context context, int instance, ArrayList<SyncSet> playlists) {
FileUtil.serializeCompressed(context, playlists, getPlaylistSyncFile(context, instance));
}
public static void addSyncedPlaylist(Context context, String playlistId) {
String playlistFile = getPlaylistSyncFile(context);
ArrayList<SyncSet> playlists = getSyncedPlaylists(context);
SyncSet set = new SyncSet(playlistId);
if(!playlists.contains(set)) {
playlists.add(set);
}
FileUtil.serializeCompressed(context, playlists, playlistFile);
syncedPlaylists = playlists;
}
public static void removeSyncedPlaylist(Context context, String playlistId) {
int instance = Util.getActiveServer(context);
removeSyncedPlaylist(context, playlistId, instance);
}
public static void removeSyncedPlaylist(Context context, String playlistId, int instance) {
String playlistFile = getPlaylistSyncFile(context, instance);
ArrayList<SyncSet> playlists = getSyncedPlaylists(context, instance);
SyncSet set = new SyncSet(playlistId);
if(playlists.contains(set)) {
playlists.remove(set);
FileUtil.serializeCompressed(context, playlists, playlistFile);
syncedPlaylists = playlists;
}
}
public static String getPlaylistSyncFile(Context context) {
int instance = Util.getActiveServer(context);
return getPlaylistSyncFile(context, instance);
}
public static String getPlaylistSyncFile(Context context, int instance) {
return "sync-playlist-" + (Util.getRestUrl(context, null, instance, false)).hashCode() + ".ser";
}
FileUtil.serializeCompressed(context, playlists, syncFile);
}
}
return playlists;
}
public static void setSyncedPlaylists(Context context, int instance, ArrayList<SyncSet> playlists) {
FileUtil.serializeCompressed(context, playlists, getPlaylistSyncFile(context, instance));
}
public static void addSyncedPlaylist(Context context, String playlistId) {
String playlistFile = getPlaylistSyncFile(context);
ArrayList<SyncSet> playlists = getSyncedPlaylists(context);
SyncSet set = new SyncSet(playlistId);
if(!playlists.contains(set)) {
playlists.add(set);
}
FileUtil.serializeCompressed(context, playlists, playlistFile);
syncedPlaylists = playlists;
}
public static void removeSyncedPlaylist(Context context, String playlistId) {
int instance = Util.getActiveServer(context);
removeSyncedPlaylist(context, playlistId, instance);
}
public static void removeSyncedPlaylist(Context context, String playlistId, int instance) {
String playlistFile = getPlaylistSyncFile(context, instance);
ArrayList<SyncSet> playlists = getSyncedPlaylists(context, instance);
SyncSet set = new SyncSet(playlistId);
if(playlists.contains(set)) {
playlists.remove(set);
FileUtil.serializeCompressed(context, playlists, playlistFile);
syncedPlaylists = playlists;
}
}
public static String getPlaylistSyncFile(Context context) {
int instance = Util.getActiveServer(context);
return getPlaylistSyncFile(context, instance);
}
public static String getPlaylistSyncFile(Context context, int instance) {
return "sync-playlist-" + (Util.getRestUrl(context, null, instance, false)).hashCode() + ".ser";
}
// Most Recently Added
public static ArrayList<String> getSyncedMostRecent(Context context, int instance) {
ArrayList<String> list = FileUtil.deserialize(context, getMostRecentSyncFile(context, instance), ArrayList.class);
if(list == null) {
list = new ArrayList<String>();
}
return list;
}
public static void removeMostRecentSyncFiles(Context context) {
int total = Util.getServerCount(context);
for(int i = 0; i < total; i++) {
File file = new File(context.getCacheDir(), getMostRecentSyncFile(context, i));
file.delete();
}
}
public static String getMostRecentSyncFile(Context context, int instance) {
return "sync-most_recent-" + (Util.getRestUrl(context, null, instance, false)).hashCode() + ".ser";
}
// Most Recently Added
public static ArrayList<String> getSyncedMostRecent(Context context, int instance) {
ArrayList<String> list = FileUtil.deserialize(context, getMostRecentSyncFile(context, instance), ArrayList.class);
if(list == null) {
list = new ArrayList<String>();
}
return list;
}
public static void removeMostRecentSyncFiles(Context context) {
int total = Util.getServerCount(context);
for(int i = 0; i < total; i++) {
File file = new File(context.getCacheDir(), getMostRecentSyncFile(context, i));
file.delete();
}
}
public static String getMostRecentSyncFile(Context context, int instance) {
return "sync-most_recent-" + (Util.getRestUrl(context, null, instance, false)).hashCode() + ".ser";
}
public static String joinNames(List<String> names) {
StringBuilder builder = new StringBuilder();
for (String val : names) {
builder.append(val).append(", ");
}
builder.setLength(builder.length() - 2);
return builder.toString();
}
public static String joinNames(List<String> names) {
StringBuilder builder = new StringBuilder();
for (String val : names) {
builder.append(val).append(", ");
}
builder.setLength(builder.length() - 2);
return builder.toString();
}
public static class SyncSet implements Serializable {
public String id;
public List<String> synced;
public static class SyncSet implements Serializable {
public String id;
public List<String> synced;
protected SyncSet() {
protected SyncSet() {
}
public SyncSet(String id) {
this.id = id;
}
public SyncSet(String id, List<String> synced) {
this.id = id;
this.synced = synced;
}
}
public SyncSet(String id) {
this.id = id;
}
public SyncSet(String id, List<String> synced) {
this.id = id;
this.synced = synced;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof SyncSet) {
return this.id.equals(((SyncSet)obj).id);
} else {
return false;
}
}
@Override
public boolean equals(Object obj) {
if(obj instanceof SyncSet) {
return this.id.equals(((SyncSet)obj).id);
} else {
return false;
}
}
@Override
public int hashCode() {
return id.hashCode();
}
}
@Override
public int hashCode() {
return id.hashCode();
}
}
}

View File

@ -19,22 +19,22 @@ public abstract class TabBackgroundTask<T> extends BackgroundTask<T> {
public void execute() {
tabFragment.setProgressVisible(true);
queue.offer(task = new Task() {
@Override
public void onDone(T result) {
tabFragment.setProgressVisible(false);
done(result);
}
queue.offer(task = new Task() {
@Override
public void onDone(T result) {
tabFragment.setProgressVisible(false);
done(result);
}
@Override
public void onError(Throwable t) {
tabFragment.setProgressVisible(false);
error(t);
}
});
@Override
public void onError(Throwable t) {
tabFragment.setProgressVisible(false);
error(t);
}
});
}
@Override
@Override
public boolean isCancelled() {
return !tabFragment.isAdded() || cancelled.get();
}

View File

@ -1,16 +1,16 @@
/*
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 2016 (C) Scott Jackson
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 2016 (C) Scott Jackson
*/
package net.nullsum.audinaut.util;
@ -26,73 +26,73 @@ import net.nullsum.audinaut.activity.SettingsActivity;
import net.nullsum.audinaut.activity.SubsonicFragmentActivity;
public final class ThemeUtil {
public static final String THEME_DARK = "dark";
public static final String THEME_BLACK = "black";
public static final String THEME_LIGHT = "light";
public static final String THEME_DAY_NIGHT = "day/night";
public static final String THEME_DAY_BLACK_NIGHT = "day/black";
public static final String THEME_DARK = "dark";
public static final String THEME_BLACK = "black";
public static final String THEME_LIGHT = "light";
public static final String THEME_DAY_NIGHT = "day/night";
public static final String THEME_DAY_BLACK_NIGHT = "day/black";
public static String getTheme(Context context) {
SharedPreferences prefs = Util.getPreferences(context);
String theme = prefs.getString(Constants.PREFERENCES_KEY_THEME, null);
public static String getTheme(Context context) {
SharedPreferences prefs = Util.getPreferences(context);
String theme = prefs.getString(Constants.PREFERENCES_KEY_THEME, null);
if(THEME_DAY_NIGHT.equals(theme)) {
int currentNightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if(currentNightMode == Configuration.UI_MODE_NIGHT_YES) {
theme = THEME_DARK;
} else {
theme = THEME_LIGHT;
}
} else if(THEME_DAY_BLACK_NIGHT.equals(theme)) {
int currentNightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if(currentNightMode == Configuration.UI_MODE_NIGHT_YES) {
theme = THEME_BLACK;
} else {
theme = THEME_LIGHT;
}
}
if(THEME_DAY_NIGHT.equals(theme)) {
int currentNightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if(currentNightMode == Configuration.UI_MODE_NIGHT_YES) {
theme = THEME_DARK;
} else {
theme = THEME_LIGHT;
}
} else if(THEME_DAY_BLACK_NIGHT.equals(theme)) {
int currentNightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if(currentNightMode == Configuration.UI_MODE_NIGHT_YES) {
theme = THEME_BLACK;
} else {
theme = THEME_LIGHT;
}
}
return theme;
}
public static int getThemeRes(Context context) {
return getThemeRes(context, getTheme(context));
}
public static int getThemeRes(Context context, String theme) {
if(context instanceof SubsonicFragmentActivity || context instanceof SettingsActivity) {
if(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_COLOR_ACTION_BAR, true)) {
if (THEME_DARK.equals(theme)) {
return R.style.Theme_Audinaut_Dark_No_Actionbar;
} else if (THEME_BLACK.equals(theme)) {
return R.style.Theme_Audinaut_Black_No_Actionbar;
} else {
return R.style.Theme_Audinaut_Light_No_Actionbar;
}
} else {
if (THEME_DARK.equals(theme)) {
return R.style.Theme_Audinaut_Dark_No_Color;
} else if (THEME_BLACK.equals(theme)) {
return R.style.Theme_Audinaut_Black_No_Color;
} else {
return R.style.Theme_Audinaut_Light_No_Color;
}
}
} else {
if (THEME_DARK.equals(theme)) {
return R.style.Theme_Audinaut_Dark;
} else if (THEME_BLACK.equals(theme)) {
return R.style.Theme_Audinaut_Black;
} else {
return R.style.Theme_Audinaut_Light;
}
}
}
public static void setTheme(Context context, String theme) {
SharedPreferences.Editor editor = Util.getPreferences(context).edit();
editor.putString(Constants.PREFERENCES_KEY_THEME, theme);
editor.apply();
}
return theme;
}
public static int getThemeRes(Context context) {
return getThemeRes(context, getTheme(context));
}
public static int getThemeRes(Context context, String theme) {
if(context instanceof SubsonicFragmentActivity || context instanceof SettingsActivity) {
if(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_COLOR_ACTION_BAR, true)) {
if (THEME_DARK.equals(theme)) {
return R.style.Theme_Audinaut_Dark_No_Actionbar;
} else if (THEME_BLACK.equals(theme)) {
return R.style.Theme_Audinaut_Black_No_Actionbar;
} else {
return R.style.Theme_Audinaut_Light_No_Actionbar;
}
} else {
if (THEME_DARK.equals(theme)) {
return R.style.Theme_Audinaut_Dark_No_Color;
} else if (THEME_BLACK.equals(theme)) {
return R.style.Theme_Audinaut_Black_No_Color;
} else {
return R.style.Theme_Audinaut_Light_No_Color;
}
}
} else {
if (THEME_DARK.equals(theme)) {
return R.style.Theme_Audinaut_Dark;
} else if (THEME_BLACK.equals(theme)) {
return R.style.Theme_Audinaut_Black;
} else {
return R.style.Theme_Audinaut_Light;
}
}
}
public static void setTheme(Context context, String theme) {
SharedPreferences.Editor editor = Util.getPreferences(context).edit();
editor.putString(Constants.PREFERENCES_KEY_THEME, theme);
editor.apply();
}
public static void applyTheme(Context context, String theme) {
context.setTheme(getThemeRes(context, theme));
}
public static void applyTheme(Context context, String theme) {
context.setTheme(getThemeRes(context, theme));
}
}

View File

@ -1,20 +1,20 @@
/*
This file is part of Subsonic.
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 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.
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/>.
You should have received a copy of the GNU General Public License
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2015 (C) Scott Jackson
Copyright 2015 (C) Scott Jackson
*/
package net.nullsum.audinaut.util;
@ -43,50 +43,50 @@ import net.nullsum.audinaut.service.OfflineException;
import net.nullsum.audinaut.view.UpdateView;
public final class UpdateHelper {
private static final String TAG = UpdateHelper.class.getSimpleName();
private static final String TAG = UpdateHelper.class.getSimpleName();
public static abstract class EntryInstanceUpdater {
private Entry entry;
protected int metadataUpdate = DownloadService.METADATA_UPDATED_ALL;
public static abstract class EntryInstanceUpdater {
private Entry entry;
protected int metadataUpdate = DownloadService.METADATA_UPDATED_ALL;
public EntryInstanceUpdater(Entry entry) {
this.entry = entry;
}
public EntryInstanceUpdater(Entry entry, int metadataUpdate) {
this.entry = entry;
this.metadataUpdate = metadataUpdate;
}
public EntryInstanceUpdater(Entry entry) {
this.entry = entry;
}
public EntryInstanceUpdater(Entry entry, int metadataUpdate) {
this.entry = entry;
this.metadataUpdate = metadataUpdate;
}
public abstract void update(Entry found);
public abstract void update(Entry found);
public void execute() {
DownloadService downloadService = DownloadService.getInstance();
if(downloadService != null && !entry.isDirectory()) {
boolean serializeChanges = false;
List<DownloadFile> downloadFiles = downloadService.getDownloads();
DownloadFile currentPlaying = downloadService.getCurrentPlaying();
public void execute() {
DownloadService downloadService = DownloadService.getInstance();
if(downloadService != null && !entry.isDirectory()) {
boolean serializeChanges = false;
List<DownloadFile> downloadFiles = downloadService.getDownloads();
DownloadFile currentPlaying = downloadService.getCurrentPlaying();
for(DownloadFile file: downloadFiles) {
Entry check = file.getSong();
if(entry.getId().equals(check.getId())) {
update(check);
serializeChanges = true;
for(DownloadFile file: downloadFiles) {
Entry check = file.getSong();
if(entry.getId().equals(check.getId())) {
update(check);
serializeChanges = true;
if(currentPlaying != null && currentPlaying.getSong() != null && currentPlaying.getSong().getId().equals(entry.getId())) {
downloadService.onMetadataUpdate(metadataUpdate);
}
}
}
if(currentPlaying != null && currentPlaying.getSong() != null && currentPlaying.getSong().getId().equals(entry.getId())) {
downloadService.onMetadataUpdate(metadataUpdate);
}
}
}
if(serializeChanges) {
downloadService.serializeQueue();
}
}
if(serializeChanges) {
downloadService.serializeQueue();
}
}
Entry find = UpdateView.findEntry(entry);
if(find != null) {
update(find);
}
}
}
Entry find = UpdateView.findEntry(entry);
if(find != null) {
update(find);
}
}
}
}

View File

@ -1,16 +1,16 @@
/*
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 2014 (C) Scott Jackson
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 2014 (C) Scott Jackson
*/
package net.nullsum.audinaut.util;
@ -43,80 +43,80 @@ import net.nullsum.audinaut.adapter.SettingsAdapter;
import net.nullsum.audinaut.view.UpdateView;
public final class UserUtil {
private static final String TAG = UserUtil.class.getSimpleName();
private static final long MIN_VERIFY_DURATION = 1000L * 60L * 60L;
private static final String TAG = UserUtil.class.getSimpleName();
private static final long MIN_VERIFY_DURATION = 1000L * 60L * 60L;
private static int instance = -1;
private static int instanceHash = -1;
private static User currentUser;
private static long lastVerifiedTime = 0;
private static int instance = -1;
private static int instanceHash = -1;
private static User currentUser;
private static long lastVerifiedTime = 0;
public static void refreshCurrentUser(Context context, boolean forceRefresh) {
refreshCurrentUser(context, forceRefresh, false);
}
public static void refreshCurrentUser(Context context, boolean forceRefresh, boolean unAuth) {
currentUser = null;
if(unAuth) {
lastVerifiedTime = 0;
}
seedCurrentUser(context, forceRefresh);
}
public static void refreshCurrentUser(Context context, boolean forceRefresh) {
refreshCurrentUser(context, forceRefresh, false);
}
public static void refreshCurrentUser(Context context, boolean forceRefresh, boolean unAuth) {
currentUser = null;
if(unAuth) {
lastVerifiedTime = 0;
}
seedCurrentUser(context, forceRefresh);
}
public static void seedCurrentUser(Context context) {
seedCurrentUser(context, false);
}
public static void seedCurrentUser(final Context context, final boolean refresh) {
// Only try to seed if online
if(Util.isOffline(context)) {
currentUser = null;
return;
}
public static void seedCurrentUser(Context context) {
seedCurrentUser(context, false);
}
public static void seedCurrentUser(final Context context, final boolean refresh) {
// Only try to seed if online
if(Util.isOffline(context)) {
currentUser = null;
return;
}
final int instance = Util.getActiveServer(context);
final int instanceHash = (instance == 0) ? 0 : Util.getRestUrl(context, null).hashCode();
if(UserUtil.instance == instance && UserUtil.instanceHash == instanceHash && currentUser != null) {
return;
} else {
UserUtil.instance = instance;
UserUtil.instanceHash = instanceHash;
}
final int instance = Util.getActiveServer(context);
final int instanceHash = (instance == 0) ? 0 : Util.getRestUrl(context, null).hashCode();
if(UserUtil.instance == instance && UserUtil.instanceHash == instanceHash && currentUser != null) {
return;
} else {
UserUtil.instance = instance;
UserUtil.instanceHash = instanceHash;
}
new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
currentUser = MusicServiceFactory.getMusicService(context).getUser(refresh, getCurrentUsername(context, instance), context, null);
new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
currentUser = MusicServiceFactory.getMusicService(context).getUser(refresh, getCurrentUsername(context, instance), context, null);
// If running, redo cast selector
DownloadService downloadService = DownloadService.getInstance();
// If running, redo cast selector
DownloadService downloadService = DownloadService.getInstance();
return null;
}
return null;
}
@Override
protected void done(Void result) {
if(context instanceof AppCompatActivity) {
((AppCompatActivity) context).supportInvalidateOptionsMenu();
}
}
@Override
protected void done(Void result) {
if(context instanceof AppCompatActivity) {
((AppCompatActivity) context).supportInvalidateOptionsMenu();
}
}
@Override
protected void error(Throwable error) {
// Don't do anything, supposed to be background pull
Log.e(TAG, "Failed to seed user information");
}
}.execute();
}
@Override
protected void error(Throwable error) {
// Don't do anything, supposed to be background pull
Log.e(TAG, "Failed to seed user information");
}
}.execute();
}
public static User getCurrentUser() {
return currentUser;
}
public static String getCurrentUsername(Context context, int instance) {
SharedPreferences prefs = Util.getPreferences(context);
return prefs.getString(Constants.PREFERENCES_KEY_USERNAME + instance, null);
}
public static User getCurrentUser() {
return currentUser;
}
public static String getCurrentUsername(Context context, int instance) {
SharedPreferences prefs = Util.getPreferences(context);
return prefs.getString(Constants.PREFERENCES_KEY_USERNAME + instance, null);
}
public static String getCurrentUsername(Context context) {
return getCurrentUsername(context, Util.getActiveServer(context));
}
public static String getCurrentUsername(Context context) {
return getCurrentUsername(context, Util.getActiveServer(context));
}
}

Some files were not shown because too many files have changed in this diff Show More