IDE-fueled mega refactor

This commit is contained in:
Andrew Rabert 2018-03-24 21:28:28 -04:00
parent 1396ff5e48
commit c7a49ee88e
267 changed files with 6271 additions and 9399 deletions

6
.gitignore vendored
View File

@ -37,11 +37,7 @@ captures/
# Intellij
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/dictionaries
.idea/libraries
.idea
# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK" />
</project>

View File

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Audinaut.iml" filepath="$PROJECT_DIR$/Audinaut.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
</modules>
</component>
</project>

View File

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="org.jetbrains.plugins.gradle.execution.test.runner.AllInPackageGradleConfigurationProducer" />
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestClassGradleConfigurationProducer" />
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestMethodGradleConfigurationProducer" />
</set>
</option>
</component>
</project>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -1,100 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.nullsum.audinaut"
android:installLocation="internalOnly">
package="net.nullsum.audinaut"
android:installLocation="internalOnly">
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="net.nullsum.audinaut"
android:label="Tests" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission
android:name="android.permission.READ_PHONE_STATE"
android:maxSdkVersion="22" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.READ_LOGS"/>
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" android:maxSdkVersion="22"/>
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<uses-feature
android:name="android.hardware.bluetooth"
android:required="false" />
<uses-feature
android:name="android.hardware.microphone"
android:required="false" />
<uses-feature
android:name="android.hardware.wifi"
android:required="false" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-feature android:name="android.hardware.bluetooth" android:required="false" />
<uses-feature android:name="android.hardware.microphone" android:required="false" />
<uses-feature android:name="android.hardware.wifi" android:required="false" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<supports-screens android:anyDensity="true" android:xlargeScreens="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true"/>
<application android:label="@string/common.appname"
<application
android:backupAgent="net.nullsum.audinaut.util.SettingsBackupAgent"
android:icon="@drawable/launch"
android:label="@string/common.appname"
android:theme="@style/LaunchScreen">
<uses-library android:name="android.test.runner" />
<activity android:name="net.nullsum.audinaut.activity.SubsonicFragmentActivity"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTask">
<activity
android:name="net.nullsum.audinaut.activity.SubsonicFragmentActivity"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="net.nullsum.audinaut.activity.SettingsActivity"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTask"/>
<activity
android:name="net.nullsum.audinaut.activity.SettingsActivity"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTask" />
<activity android:name="net.nullsum.audinaut.activity.VoiceQueryReceiverActivity"
android:launchMode="singleTask">
<activity
android:name="net.nullsum.audinaut.activity.VoiceQueryReceiverActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.media.action.MEDIA_PLAY_FROM_SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="net.nullsum.audinaut.activity.QueryReceiverActivity"
android:launchMode="singleTask">
<activity
android:name="net.nullsum.audinaut.activity.QueryReceiverActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name="net.nullsum.audinaut.activity.EditPlayActionActivity"
android:label="@string/tasker.start_playing"
android:icon="@drawable/launch">
android:icon="@drawable/launch"
android:label="@string/tasker.start_playing">
<intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING" />
</intent-filter>
</activity>
<service android:name=".service.DownloadService"
android:label="Audinaut Playback Service"/>
<service
android:name=".service.DownloadService"
android:label="Audinaut Playback Service" />
<service android:name="net.nullsum.audinaut.service.sync.AuthenticatorService">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data android:name="android.accounts.AccountAuthenticator"
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>
<service android:name="net.nullsum.audinaut.service.HeadphoneListenerService"
android:label="Audinaut Headphone Listener"/>
<receiver
android:name="net.nullsum.audinaut.receiver.BootReceiver">
<service
android:name="net.nullsum.audinaut.service.HeadphoneListenerService"
android:label="Audinaut Headphone Listener" />
<receiver android:name="net.nullsum.audinaut.receiver.BootReceiver">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
@ -106,7 +126,7 @@
<receiver android:name="net.nullsum.audinaut.receiver.A2dpIntentReceiver">
<intent-filter>
<action android:name="android.music.playstatusrequest"/>
<action android:name="android.music.playstatusrequest" />
</intent-filter>
</receiver>
@ -114,48 +134,59 @@
android:name="net.nullsum.audinaut.provider.AudinautWidget4x1"
android:label="@string/widget.4x1">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget4x1"/>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget4x1" />
</receiver>
<receiver
android:name="net.nullsum.audinaut.provider.AudinautWidget4x2"
android:label="@string/widget.4x2">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget4x2"/>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget4x2" />
</receiver>
<receiver
android:name="net.nullsum.audinaut.provider.AudinautWidget4x3"
android:label="@string/widget.4x3">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget4x3"/>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget4x3" />
</receiver>
<receiver
android:name="net.nullsum.audinaut.provider.AudinautWidget4x4"
android:label="@string/widget.4x4">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget4x4"/>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget4x4" />
</receiver>
<receiver
android:name="net.nullsum.audinaut.receiver.PlayActionReceiver">
android:name="net.nullsum.audinaut.receiver.PlayActionReceiver"
android:permission="">
<intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.FIRE_SETTING" />
</intent-filter>
</receiver>
<provider android:name="net.nullsum.audinaut.provider.AudinautSearchProvider"
android:authorities="net.nullsum.audinaut.provider.AudinautSearchProvider"/>
<provider
android:name="net.nullsum.audinaut.provider.AudinautSearchProvider"
android:authorities="net.nullsum.audinaut.provider.AudinautSearchProvider" />
<meta-data android:name="android.app.default_searchable"
android:value="net.nullsum.audinaut.activity.QueryReceiverActivity"/>
<meta-data
android:name="android.app.default_searchable"
android:value="net.nullsum.audinaut.activity.QueryReceiverActivity" />
</application>
</manifest>

View File

@ -16,12 +16,10 @@
package net.nullsum.audinaut.activity;
import android.app.Activity;
import android.support.v7.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@ -29,13 +27,9 @@ import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.Genre;
import net.nullsum.audinaut.service.MusicService;
@ -45,6 +39,9 @@ import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.LoadingTask;
import net.nullsum.audinaut.util.Util;
import java.util.ArrayList;
import java.util.List;
public class EditPlayActionActivity extends SubsonicActivity {
private CheckBox shuffleCheckbox;
private CheckBox startYearCheckbox;
@ -64,36 +61,23 @@ public class EditPlayActionActivity extends SubsonicActivity {
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 = findViewById(R.id.edit_shuffle_checkbox);
shuffleCheckbox.setOnCheckedChangeListener((view, 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);
startYearCheckbox = findViewById(R.id.edit_start_year_checkbox);
startYearBox = 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.setOnCheckedChangeListener((view, 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 = findViewById(R.id.edit_end_year_checkbox);
endYearBox = findViewById(R.id.edit_end_year);
endYearCheckbox.setOnCheckedChangeListener((view, isChecked) -> endYearBox.setEnabled(isChecked));
genreButton = (Button) findViewById(R.id.edit_genre_spinner);
genreButton = findViewById(R.id.edit_genre_spinner);
genreButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new LoadingTask<List<Genre>>(context, true) {
@ -105,24 +89,22 @@ public class EditPlayActionActivity extends SubsonicActivity {
@Override
protected void done(final List<Genre> genres) {
List<String> names = new ArrayList<String>();
List<String> names = new ArrayList<>();
String blank = context.getResources().getString(R.string.select_genre_blank);
names.add(doNothing);
names.add(blank);
for(Genre genre: genres) {
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));
}
.setItems(names.toArray(new CharSequence[names.size()]), (dialog, which) -> {
if (which == 1) {
genreButton.setText("");
} else {
genreButton.setText(finalNames.get(which));
}
});
AlertDialog dialog = builder.create();
@ -145,36 +127,36 @@ public class EditPlayActionActivity extends SubsonicActivity {
});
genreButton.setText(doNothing);
offlineSpinner = (Spinner) findViewById(R.id.edit_offline_spinner);
offlineSpinner = 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)) {
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) {
if (startYear != null) {
startYearCheckbox.setEnabled(true);
startYearBox.setText(startYear);
}
String endYear = extras.getString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, null);
if(endYear != null) {
if (endYear != null) {
endYearCheckbox.setEnabled(true);
endYearBox.setText(endYear);
}
String genre = extras.getString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, doNothing);
if(genre != null) {
if (genre != null) {
genreButton.setText(genre);
}
int offline = extras.getInt(Constants.PREFERENCES_KEY_OFFLINE, 0);
if(offline != 0) {
if (offline != 0) {
offlineSpinner.setSelection(offline);
}
}
@ -191,13 +173,13 @@ public class EditPlayActionActivity extends SubsonicActivity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home) {
if (item.getItemId() == android.R.id.home) {
cancel();
return true;
} else if(item.getItemId() == R.id.menu_accept) {
} else if (item.getItemId() == R.id.menu_accept) {
accept();
return true;
} else if(item.getItemId() == R.id.menu_cancel) {
} else if (item.getItemId() == R.id.menu_cancel) {
cancel();
return true;
}
@ -215,21 +197,21 @@ public class EditPlayActionActivity extends SubsonicActivity {
Bundle data = new Bundle();
boolean shuffle = shuffleCheckbox.isChecked();
data.putBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE, shuffle);
if(shuffle) {
if(startYearCheckbox.isChecked()) {
if (shuffle) {
if (startYearCheckbox.isChecked()) {
data.putString(Constants.PREFERENCES_KEY_SHUFFLE_START_YEAR, startYearBox.getText().toString());
}
if(endYearCheckbox.isChecked()) {
if (endYearCheckbox.isChecked()) {
data.putString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, endYearBox.getText().toString());
}
String genre = genreButton.getText().toString();
if(!genre.equals(doNothing)) {
if (!genre.equals(doNothing)) {
data.putString(Constants.PREFERENCES_KEY_SHUFFLE_GENRE, genre);
}
}
int offline = offlineSpinner.getSelectedItemPosition();
if(offline != 0) {
if (offline != 0) {
data.putInt(Constants.PREFERENCES_KEY_OFFLINE, offline);
}
@ -238,6 +220,7 @@ public class EditPlayActionActivity extends SubsonicActivity {
setResult(Activity.RESULT_OK, intent);
finish();
}
private void cancel() {
setResult(Activity.RESULT_CANCELED);
finish();

View File

@ -23,13 +23,9 @@ import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.util.Log;
import net.nullsum.audinaut.fragments.SubsonicFragment;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.provider.AudinautSearchProvider;
/**
* Receives search queries and forwards to the SearchFragment.
@ -38,8 +34,6 @@ import net.nullsum.audinaut.provider.AudinautSearchProvider;
*/
public class QueryReceiverActivity extends Activity {
private static final String TAG = QueryReceiverActivity.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -47,11 +41,10 @@ public class QueryReceiverActivity extends Activity {
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
doSearch();
} else if(Intent.ACTION_VIEW.equals(intent.getAction())) {
} else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
showResult(intent.getDataString(), intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
}
finish();
Util.disablePendingTransition(this);
}
private void doSearch() {
@ -63,15 +56,16 @@ public class QueryReceiverActivity extends Activity {
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) {
if (albumId.indexOf("ar-") == 0) {
intent.putExtra(Constants.INTENT_EXTRA_NAME_ARTIST, true);
albumId = albumId.replace("ar-", "");
} else if(albumId.indexOf("so-") == 0) {
} else if (albumId.indexOf("so-") == 0) {
intent.putExtra(Constants.INTENT_EXTRA_SEARCH_SONG, name);
albumId = albumId.replace("so-", "");
}

View File

@ -18,7 +18,6 @@
*/
package net.nullsum.audinaut.activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
@ -28,8 +27,6 @@ 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;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -38,7 +35,7 @@ public class SettingsActivity extends SubsonicActivity {
setContentView(R.layout.settings_activity);
if (savedInstanceState == null) {
fragment = new SettingsFragment();
PreferenceCompatFragment fragment = new SettingsFragment();
Bundle args = new Bundle();
args.putInt(Constants.INTENT_EXTRA_FRAGMENT_TYPE, R.xml.settings);
@ -50,7 +47,7 @@ public class SettingsActivity extends SubsonicActivity {
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, currentFragment, currentFragment.getSupportTag() + "").commit();
}
Toolbar mainToolbar = (Toolbar) findViewById(R.id.main_toolbar);
Toolbar mainToolbar = findViewById(R.id.main_toolbar);
setSupportActionBar(mainToolbar);
}
}

View File

@ -18,116 +18,108 @@
*/
package net.nullsum.audinaut.activity;
import android.app.UiModeManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.design.widget.NavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.fragments.SubsonicFragment;
import net.nullsum.audinaut.service.DownloadService;
import net.nullsum.audinaut.service.HeadphoneListenerService;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.DrawableTint;
import net.nullsum.audinaut.util.ImageLoader;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.util.ThemeUtil;
import net.nullsum.audinaut.util.UserUtil;
import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.view.UpdateView;
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.fragments.SubsonicFragment;
import net.nullsum.audinaut.service.DownloadService;
import net.nullsum.audinaut.service.HeadphoneListenerService;
import net.nullsum.audinaut.service.MusicService;
import net.nullsum.audinaut.service.MusicServiceFactory;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.DrawableTint;
import net.nullsum.audinaut.util.ImageLoader;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.util.ThemeUtil;
import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.view.UpdateView;
import net.nullsum.audinaut.util.UserUtil;
import static android.Manifest.*;
import static android.Manifest.permission;
public class SubsonicActivity extends AppCompatActivity implements OnItemSelectedListener {
private static final String TAG = SubsonicActivity.class.getSimpleName();
private static ImageLoader IMAGE_LOADER;
protected static String theme;
protected static boolean fullScreen;
protected static boolean actionbarColored;
private static final int MENU_GROUP_SERVER = 10;
private static final int MENU_ITEM_SERVER_BASE = 100;
private static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
private final List<Runnable> afterServiceAvailable = new ArrayList<>();
private boolean drawerIdle = true;
private boolean destroyed = false;
private boolean finished = false;
protected List<SubsonicFragment> backStack = new ArrayList<SubsonicFragment>();
protected SubsonicFragment currentFragment;
protected View primaryContainer;
protected View secondaryContainer;
protected boolean touchscreen = true;
protected Handler handler = new Handler();
Spinner actionBarSpinner;
ArrayAdapter<CharSequence> spinnerAdapter;
ViewGroup rootView;
DrawerLayout drawer;
ActionBarDrawerToggle drawerToggle;
NavigationView drawerList;
View drawerHeader;
ImageView drawerHeaderToggle;
TextView drawerServerName;
TextView drawerUserName;
int lastSelectedPosition = 0;
boolean showingTabs = true;
boolean drawerOpen = false;
SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
private static String theme;
private static boolean fullScreen;
private static boolean actionbarColored;
private static ImageLoader IMAGE_LOADER;
static {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
}
final List<SubsonicFragment> backStack = new ArrayList<>();
final Handler handler = new Handler();
private final List<Runnable> afterServiceAvailable = new ArrayList<>();
SubsonicFragment currentFragment;
View secondaryContainer;
DrawerLayout drawer;
ActionBarDrawerToggle drawerToggle;
NavigationView drawerList;
int lastSelectedPosition = 0;
private boolean touchscreen = true;
private Spinner actionBarSpinner;
private ArrayAdapter<CharSequence> spinnerAdapter;
private View drawerHeader;
private ImageView drawerHeaderToggle;
private TextView drawerServerName;
private TextView drawerUserName;
private boolean showingTabs = true;
private boolean drawerOpen = false;
private SharedPreferences.OnSharedPreferenceChangeListener preferencesListener;
private boolean drawerIdle = true;
private boolean destroyed = false;
public synchronized static ImageLoader getStaticImageLoader(Context context) {
if (IMAGE_LOADER == null) {
IMAGE_LOADER = new ImageLoader(context);
}
return IMAGE_LOADER;
}
@Override
protected void onCreate(Bundle bundle) {
UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
PackageManager pm = getPackageManager();
if(!pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN)) {
if (!pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN)) {
touchscreen = false;
}
@ -138,16 +130,16 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
startService(new Intent(this, DownloadService.class));
setVolumeControlStream(AudioManager.STREAM_MUSIC);
if(getIntent().hasExtra(Constants.FRAGMENT_POSITION)) {
if (getIntent().hasExtra(Constants.FRAGMENT_POSITION)) {
lastSelectedPosition = getIntent().getIntExtra(Constants.FRAGMENT_POSITION, 0);
}
if(preferencesListener == null) {
if (preferencesListener == null) {
Util.getPreferences(this).registerOnSharedPreferenceChangeListener(preferencesListener);
}
if (ContextCompat.checkSelfPermission(this, permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{ permission.WRITE_EXTERNAL_STORAGE }, PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
ActivityCompat.requestPermissions(this, new String[]{permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
}
@ -156,9 +148,7 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
switch (requestCode) {
case PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
if (!(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
Util.toast(this, R.string.permission_external_storage_failed);
finish();
}
@ -170,23 +160,23 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
if(spinnerAdapter == null) {
if (spinnerAdapter == null) {
createCustomActionBarView();
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
if(Util.shouldStartOnHeadphones(this)) {
if (Util.shouldStartOnHeadphones(this)) {
Intent serviceIntent = new Intent();
serviceIntent.setClassName(this.getPackageName(), HeadphoneListenerService.class.getName());
this.startService(serviceIntent);
}
}
protected void createCustomActionBarView() {
private void createCustomActionBarView() {
actionBarSpinner = (Spinner) getLayoutInflater().inflate(R.layout.actionbar_spinner, null);
if((this instanceof SubsonicFragmentActivity || this instanceof SettingsActivity) && (Util.getPreferences(this).getBoolean(Constants.PREFERENCES_KEY_COLOR_ACTION_BAR, true) || ThemeUtil.getThemeRes(this) != R.style.Theme_Audinaut_Light_No_Color)) {
actionBarSpinner.setBackgroundDrawable(DrawableTint.getTintedDrawableFromColor(this, R.drawable.abc_spinner_mtrl_am_alpha, android.R.color.white));
if ((this instanceof SubsonicFragmentActivity || this instanceof SettingsActivity) && (Util.getPreferences(this).getBoolean(Constants.PREFERENCES_KEY_COLOR_ACTION_BAR, true) || ThemeUtil.getThemeRes(this) != R.style.Theme_Audinaut_Light_No_Color)) {
actionBarSpinner.setBackground(DrawableTint.getTintedDrawableFromColor(this));
}
spinnerAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
@ -228,91 +218,79 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
Util.getPreferences(this).unregisterOnSharedPreferenceChangeListener(preferencesListener);
}
@Override
public void finish() {
super.finish();
Util.disablePendingTransition(this);
}
@Override
public void setContentView(int viewId) {
super.setContentView(R.layout.abstract_activity);
rootView = (ViewGroup) findViewById(R.id.content_frame);
ViewGroup rootView = findViewById(R.id.content_frame);
if(viewId != 0) {
if (viewId != 0) {
LayoutInflater layoutInflater = getLayoutInflater();
layoutInflater.inflate(viewId, rootView);
}
drawerList = (NavigationView) findViewById(R.id.left_drawer);
drawerList.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
if(showingTabs) {
// Settings are on a different selectable track
if (menuItem.getItemId() != R.id.drawer_settings && menuItem.getItemId() != R.id.drawer_offline) {
menuItem.setChecked(true);
lastSelectedPosition = menuItem.getItemId();
}
switch (menuItem.getItemId()) {
case R.id.drawer_library:
drawerItemSelected("Artist");
return true;
case R.id.drawer_playlists:
drawerItemSelected("Playlist");
return true;
case R.id.drawer_downloading:
drawerItemSelected("Download");
return true;
case R.id.drawer_offline:
toggleOffline();
return true;
case R.id.drawer_settings:
startActivity(new Intent(SubsonicActivity.this, SettingsActivity.class));
drawer.closeDrawers();
return true;
}
} else {
int activeServer = menuItem.getItemId() - MENU_ITEM_SERVER_BASE;
SubsonicActivity.this.setActiveServer(activeServer);
populateTabs();
return true;
drawerList = findViewById(R.id.left_drawer);
drawerList.setNavigationItemSelectedListener(menuItem -> {
if (showingTabs) {
// Settings are on a different selectable track
if (menuItem.getItemId() != R.id.drawer_settings && menuItem.getItemId() != R.id.drawer_offline) {
menuItem.setChecked(true);
lastSelectedPosition = menuItem.getItemId();
}
return false;
switch (menuItem.getItemId()) {
case R.id.drawer_library:
drawerItemSelected("Artist");
return true;
case R.id.drawer_playlists:
drawerItemSelected("Playlist");
return true;
case R.id.drawer_downloading:
drawerItemSelected("Download");
return true;
case R.id.drawer_offline:
toggleOffline();
return true;
case R.id.drawer_settings:
startActivity(new Intent(SubsonicActivity.this, SettingsActivity.class));
drawer.closeDrawers();
return true;
}
} else {
int activeServer = menuItem.getItemId() - MENU_ITEM_SERVER_BASE;
SubsonicActivity.this.setActiveServer(activeServer);
populateTabs();
return true;
}
return false;
});
drawerHeader = drawerList.inflateHeaderView(R.layout.drawer_header);
drawerHeader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(showingTabs) {
populateServers();
} else {
populateTabs();
}
drawerHeader.setOnClickListener(v -> {
if (showingTabs) {
populateServers();
} else {
populateTabs();
}
});
drawerHeaderToggle = (ImageView) drawerHeader.findViewById(R.id.header_select_image);
drawerServerName = (TextView) drawerHeader.findViewById(R.id.header_server_name);
drawerUserName = (TextView) drawerHeader.findViewById(R.id.header_user_name);
drawerHeaderToggle = drawerHeader.findViewById(R.id.header_select_image);
drawerServerName = drawerHeader.findViewById(R.id.header_server_name);
drawerUserName = drawerHeader.findViewById(R.id.header_user_name);
updateDrawerHeader();
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer = findViewById(R.id.drawer_layout);
// Pass in toolbar if it exists
Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
Toolbar toolbar = findViewById(R.id.main_toolbar);
drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.common_appname, R.string.common_appname) {
@Override
public void onDrawerClosed(View view) {
drawerIdle = true;
drawerOpen = false;
if(!showingTabs) {
if (!showingTabs) {
populateTabs();
}
}
@ -321,10 +299,10 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
public void onDrawerOpened(View view) {
DownloadService downloadService = getDownloadService();
boolean downloadingVisible = downloadService != null && !downloadService.getBackgroundDownloads().isEmpty();
if(lastSelectedPosition == R.id.drawer_downloading) {
if (lastSelectedPosition == R.id.drawer_downloading) {
downloadingVisible = true;
}
setDrawerItemVisible(R.id.drawer_downloading, downloadingVisible);
setDrawerItemVisible(downloadingVisible);
drawerIdle = true;
drawerOpen = true;
@ -336,24 +314,13 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
drawerIdle = false;
}
};
drawer.setDrawerListener(drawerToggle);
drawer.addDrawerListener(drawerToggle);
drawerToggle.setDrawerIndicatorEnabled(true);
drawer.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (drawerIdle && currentFragment != null && currentFragment.getGestureDetector() != null) {
return currentFragment.getGestureDetector().onTouchEvent(event);
} else {
return false;
}
}
});
drawer.setOnTouchListener((v, event) -> drawerIdle && currentFragment != null && currentFragment.getGestureDetector() != null && currentFragment.getGestureDetector().onTouchEvent(event));
// Check whether this is a tablet or not
secondaryContainer = findViewById(R.id.fragment_second_container);
if(secondaryContainer != null) {
primaryContainer = findViewById(R.id.fragment_container);
}
}
@Override
@ -362,7 +329,7 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
String[] ids = new String[backStack.size() + 1];
ids[0] = currentFragment.getTag();
int i = 1;
for(SubsonicFragment frag: backStack) {
for (SubsonicFragment frag : backStack) {
ids[i] = frag.getTag();
i++;
}
@ -370,21 +337,22 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
savedInstanceState.putInt(Constants.MAIN_BACK_STACK_SIZE, backStack.size() + 1);
savedInstanceState.putInt(Constants.FRAGMENT_POSITION, lastSelectedPosition);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int size = savedInstanceState.getInt(Constants.MAIN_BACK_STACK_SIZE);
String[] ids = savedInstanceState.getStringArray(Constants.MAIN_BACK_STACK);
FragmentManager fm = getSupportFragmentManager();
currentFragment = (SubsonicFragment)fm.findFragmentByTag(ids[0]);
currentFragment = (SubsonicFragment) fm.findFragmentByTag(ids[0]);
currentFragment.setPrimaryFragment(true);
currentFragment.setSupportTag(ids[0]);
supportInvalidateOptionsMenu();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
for(int i = 1; i < size; i++) {
SubsonicFragment frag = (SubsonicFragment)fm.findFragmentByTag(ids[i]);
for (int i = 1; i < size; i++) {
SubsonicFragment frag = (SubsonicFragment) fm.findFragmentByTag(ids[i]);
frag.setSupportTag(ids[i]);
if(secondaryContainer != null) {
if (secondaryContainer != null) {
frag.setPrimaryFragment(false, true);
}
trans.hide(frag);
@ -393,7 +361,7 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
trans.commit();
// Current fragment is hidden in secondaryContainer
if(secondaryContainer == null && !currentFragment.isVisible()) {
if (secondaryContainer == null && !currentFragment.isVisible()) {
trans = getSupportFragmentManager().beginTransaction();
trans.remove(currentFragment);
trans.commit();
@ -404,7 +372,7 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
trans.commit();
}
// Current fragment needs to be moved over to secondaryContainer
else if(secondaryContainer != null && secondaryContainer.findViewById(currentFragment.getRootId()) == null && backStack.size() > 0) {
else if (secondaryContainer != null && secondaryContainer.findViewById(currentFragment.getRootId()) == null && backStack.size() > 0) {
trans = getSupportFragmentManager().beginTransaction();
trans.remove(currentFragment);
trans.show(backStack.get(backStack.size() - 1));
@ -419,9 +387,9 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
}
lastSelectedPosition = savedInstanceState.getInt(Constants.FRAGMENT_POSITION);
if(lastSelectedPosition != 0) {
if (lastSelectedPosition != 0) {
MenuItem item = drawerList.getMenu().findItem(lastSelectedPosition);
if(item != null) {
if (item != null) {
item.setChecked(true);
}
}
@ -437,26 +405,27 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
SubsonicFragment currentFragment = getCurrentFragment();
if(currentFragment != null) {
if (currentFragment != null) {
try {
SubsonicFragment fragment = getCurrentFragment();
fragment.setContext(this);
fragment.onCreateOptionsMenu(menu, menuInflater);
if(isTouchscreen()) {
if (isTouchscreen()) {
menu.setGroupVisible(R.id.not_touchscreen, false);
}
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Error on creating options menu", e);
}
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(drawerToggle != null && drawerToggle.onOptionsItemSelected(item)) {
if (drawerToggle != null && drawerToggle.onOptionsItemSelected(item)) {
return true;
} else if(item.getItemId() == android.R.id.home) {
} else if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
@ -464,22 +433,14 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
return getCurrentFragment().onOptionsItemSelected(item);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
boolean isVolumeDown = keyCode == KeyEvent.KEYCODE_VOLUME_DOWN;
boolean isVolumeUp = keyCode == KeyEvent.KEYCODE_VOLUME_UP;
boolean isVolumeAdjust = isVolumeDown || isVolumeUp;
return super.onKeyDown(keyCode, event);
}
@Override
public void setTitle(CharSequence title) {
if(title != null && getSupportActionBar() != null && !title.equals(getSupportActionBar().getTitle())) {
if (title != null && getSupportActionBar() != null && !title.equals(getSupportActionBar().getTitle())) {
getSupportActionBar().setTitle(title);
recreateSpinner();
}
}
public void setSubtitle(CharSequence title) {
getSupportActionBar().setSubtitle(title);
}
@ -487,8 +448,8 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int top = spinnerAdapter.getCount() - 1;
if(position < top) {
for(int i = top; i > position && i >= 0; i--) {
if (position < top) {
for (int i = top; i > position && i >= 0; i--) {
removeCurrent();
}
}
@ -503,17 +464,10 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
drawerList.getMenu().clear();
drawerList.inflateMenu(R.menu.drawer_navigation);
SharedPreferences prefs = Util.getPreferences(this);
boolean sharedEnabled = prefs.getBoolean(Constants.PREFERENCES_KEY_SHARED_ENABLED, true) && !Util.isOffline(this);
MenuItem offlineMenuItem = drawerList.getMenu().findItem(R.id.drawer_offline);
if(Util.isOffline(this)) {
if(lastSelectedPosition == 0 || lastSelectedPosition == R.id.drawer_library) {
String newFragment = Util.openToTab(this);
if(newFragment == null || "Library".equals(newFragment)) {
newFragment = "Artist";
}
if (Util.isOffline(this)) {
if (lastSelectedPosition == 0 || lastSelectedPosition == R.id.drawer_library) {
String newFragment = "Artist";
lastSelectedPosition = getDrawerItemId(newFragment);
drawerItemSelected(newFragment);
}
@ -523,9 +477,9 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
offlineMenuItem.setTitle(R.string.main_offline);
}
if(lastSelectedPosition != 0) {
if (lastSelectedPosition != 0) {
MenuItem item = drawerList.getMenu().findItem(lastSelectedPosition);
if(item != null) {
if (item != null) {
item.setChecked(true);
}
}
@ -533,14 +487,15 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
showingTabs = true;
}
private void populateServers() {
drawerList.getMenu().clear();
int serverCount = Util.getServerCount(this);
int activeServer = Util.getActiveServer(this);
for(int i = 1; i <= serverCount; i++) {
for (int i = 1; i <= serverCount; i++) {
MenuItem item = drawerList.getMenu().add(MENU_GROUP_SERVER, MENU_ITEM_SERVER_BASE + i, MENU_ITEM_SERVER_BASE + i, Util.getServerName(this, i));
if(activeServer == i) {
if (activeServer == i) {
item.setChecked(true);
}
}
@ -549,52 +504,40 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
showingTabs = false;
}
private void setDrawerItemVisible(int id, boolean visible) {
MenuItem item = drawerList.getMenu().findItem(id);
if(item != null) {
private void setDrawerItemVisible(boolean visible) {
MenuItem item = drawerList.getMenu().findItem(R.id.drawer_downloading);
if (item != null) {
item.setVisible(visible);
}
}
protected void drawerItemSelected(String fragmentType) {
if(currentFragment != null) {
void drawerItemSelected(String fragmentType) {
if (currentFragment != null) {
currentFragment.stopActionMode();
}
startFragmentActivity(fragmentType);
}
public void startFragmentActivity(String fragmentType) {
void startFragmentActivity(String fragmentType) {
Intent intent = new Intent();
intent.setClass(SubsonicActivity.this, SubsonicFragmentActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if(!"".equals(fragmentType)) {
if (!"".equals(fragmentType)) {
intent.putExtra(Constants.INTENT_EXTRA_FRAGMENT_TYPE, fragmentType);
}
if(lastSelectedPosition != 0) {
if (lastSelectedPosition != 0) {
intent.putExtra(Constants.FRAGMENT_POSITION, lastSelectedPosition);
}
startActivity(intent);
finish();
}
protected void exit() {
if(((Object) this).getClass() != SubsonicFragmentActivity.class) {
Intent intent = new Intent(this, SubsonicFragmentActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(Constants.INTENT_EXTRA_NAME_EXIT, true);
Util.startActivityWithoutTransition(this, intent);
} else {
finished = true;
this.stopService(new Intent(this, DownloadService.class));
this.finish();
}
}
public boolean onBackPressedSupport() {
if(drawerOpen) {
boolean onBackPressedSupport() {
if (drawerOpen) {
drawer.closeDrawers();
return false;
} else if(backStack.size() > 0) {
} else if (backStack.size() > 0) {
removeCurrent();
return false;
} else {
@ -604,7 +547,7 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
@Override
public void onBackPressed() {
if(onBackPressedSupport()) {
if (onBackPressedSupport()) {
super.onBackPressed();
}
}
@ -613,12 +556,13 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
return this.currentFragment;
}
public void replaceFragment(SubsonicFragment fragment, int tag) {
void replaceFragment(SubsonicFragment fragment, int tag) {
replaceFragment(fragment, tag, false);
}
public void replaceFragment(SubsonicFragment fragment, int tag, boolean replaceCurrent) {
SubsonicFragment oldFragment = currentFragment;
if(currentFragment != null) {
if (currentFragment != null) {
currentFragment.setPrimaryFragment(false, secondaryContainer != null);
}
backStack.add(currentFragment);
@ -627,7 +571,7 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
currentFragment.setPrimaryFragment(true);
supportInvalidateOptionsMenu();
if(secondaryContainer == null || oldFragment.isAlwaysFullscreen() || currentFragment.isAlwaysStartFullscreen()) {
if (secondaryContainer == null || oldFragment.isAlwaysFullscreen() || currentFragment.isAlwaysStartFullscreen()) {
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
trans.hide(oldFragment);
@ -640,16 +584,13 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
// Check to see if you need to put on top of old left or not
if(backStack.size() > 1) {
if (backStack.size() > 1) {
// Move old right to left if there is a backstack already
SubsonicFragment newLeftFragment = backStack.get(backStack.size() - 1);
if(replaceCurrent) {
// trans.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
}
trans.remove(newLeftFragment);
// Only move right to left if replaceCurrent is false
if(!replaceCurrent) {
if (!replaceCurrent) {
SubsonicFragment oldLeftFragment = backStack.get(backStack.size() - 2);
oldLeftFragment.setSecondaryFragment(false);
// trans.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
@ -679,13 +620,14 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
}
recreateSpinner();
}
public void removeCurrent() {
// Don't try to remove current if there is no backstack to remove from
if(backStack.isEmpty()) {
if (backStack.isEmpty()) {
return;
}
if(currentFragment != null) {
if (currentFragment != null) {
currentFragment.setPrimaryFragment(false);
}
SubsonicFragment oldFragment = currentFragment;
@ -694,7 +636,7 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
currentFragment.setPrimaryFragment(true, false);
supportInvalidateOptionsMenu();
if(secondaryContainer == null || currentFragment.isAlwaysFullscreen() || oldFragment.isAlwaysStartFullscreen()) {
if (secondaryContainer == null || currentFragment.isAlwaysFullscreen() || oldFragment.isAlwaysStartFullscreen()) {
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
trans.remove(oldFragment);
@ -708,7 +650,7 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
trans.remove(oldFragment);
// Only switch places if there is a backstack, otherwise primary container is correct
if(backStack.size() > 0 && !backStack.get(backStack.size() - 1).isAlwaysFullscreen() && !currentFragment.isAlwaysStartFullscreen()) {
if (backStack.size() > 0 && !backStack.get(backStack.size() - 1).isAlwaysFullscreen() && !currentFragment.isAlwaysStartFullscreen()) {
trans.setCustomAnimations(0, 0, 0, 0);
// Add current left fragment to right side
trans.remove(currentFragment);
@ -735,20 +677,10 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
}
recreateSpinner();
}
public void replaceExistingFragment(SubsonicFragment fragment, int tag) {
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.remove(currentFragment);
trans.add(R.id.fragment_container, fragment, tag + "");
trans.commit();
currentFragment = fragment;
currentFragment.setPrimaryFragment(true);
supportInvalidateOptionsMenu();
}
public void invalidate() {
if(currentFragment != null) {
while(backStack.size() > 0) {
if (currentFragment != null) {
while (backStack.size() > 0) {
removeCurrent();
}
@ -759,26 +691,26 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
supportInvalidateOptionsMenu();
}
protected void recreateSpinner() {
if(currentFragment == null || currentFragment.getTitle() == null) {
void recreateSpinner() {
if (currentFragment == null || currentFragment.getTitle() == null) {
return;
}
if(spinnerAdapter == null || getSupportActionBar().getCustomView() == null) {
if (spinnerAdapter == null || getSupportActionBar().getCustomView() == null) {
createCustomActionBarView();
}
if(backStack.size() > 0) {
if (backStack.size() > 0) {
createCustomActionBarView();
spinnerAdapter.clear();
for(int i = 0; i < backStack.size(); i++) {
for (int i = 0; i < backStack.size(); i++) {
CharSequence title = backStack.get(i).getTitle();
if(title != null) {
if (title != null) {
spinnerAdapter.add(title);
} else {
spinnerAdapter.add("null");
}
}
if(currentFragment.getTitle() != null) {
if (currentFragment.getTitle() != null) {
spinnerAdapter.add(currentFragment.getTitle());
} else {
spinnerAdapter.add("null");
@ -788,7 +720,7 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
if(drawerToggle.isDrawerIndicatorEnabled()) {
if (drawerToggle.isDrawerIndicatorEnabled()) {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
drawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -801,19 +733,10 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
}
}
protected void restart() {
restart(true);
}
protected void restart(boolean resumePosition) {
private void restart() {
Intent intent = new Intent(this, this.getClass());
intent.putExtras(getIntent());
if(resumePosition) {
intent.putExtra(Constants.FRAGMENT_POSITION, lastSelectedPosition);
} else {
String fragmentType = Util.openToTab(this);
intent.putExtra(Constants.INTENT_EXTRA_FRAGMENT_TYPE, fragmentType);
intent.putExtra(Constants.FRAGMENT_POSITION, getDrawerItemId(fragmentType));
}
intent.putExtra(Constants.FRAGMENT_POSITION, lastSelectedPosition);
finish();
Util.startActivityWithoutTransition(this, intent);
}
@ -821,7 +744,7 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
private void applyTheme() {
theme = ThemeUtil.getTheme(this);
if(theme != null && theme.indexOf("fullscreen") != -1) {
if (theme != null && theme.contains("fullscreen")) {
theme = theme.substring(0, theme.indexOf("_fullscreen"));
ThemeUtil.setTheme(this, theme);
}
@ -829,9 +752,10 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
ThemeUtil.applyTheme(this, theme);
actionbarColored = Util.getPreferences(this).getBoolean(Constants.PREFERENCES_KEY_COLOR_ACTION_BAR, true);
}
private void applyFullscreen() {
fullScreen = Util.getPreferences(this).getBoolean(Constants.PREFERENCES_KEY_FULL_SCREEN, false);
if(fullScreen ) {
if (fullScreen) {
int flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
@ -851,15 +775,10 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
}
return IMAGE_LOADER;
}
public synchronized static ImageLoader getStaticImageLoader(Context context) {
if (IMAGE_LOADER == null) {
IMAGE_LOADER = new ImageLoader(context);
}
return IMAGE_LOADER;
}
public DownloadService getDownloadService() {
if(finished) {
boolean finished = false;
if (finished) {
return null;
}
@ -875,42 +794,35 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
}
final DownloadService downloadService = DownloadService.getInstance();
if(downloadService != null && afterServiceAvailable.size() > 0) {
for(Runnable runnable: afterServiceAvailable) {
if (downloadService != null && afterServiceAvailable.size() > 0) {
for (Runnable runnable : afterServiceAvailable) {
handler.post(runnable);
}
afterServiceAvailable.clear();
}
return downloadService;
}
public void runWhenServiceAvailable(Runnable runnable) {
if(getDownloadService() != null) {
if (getDownloadService() != null) {
runnable.run();
} else {
afterServiceAvailable.add(runnable);
checkIfServiceAvailable();
}
}
private void checkIfServiceAvailable() {
if(getDownloadService() == null) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
checkIfServiceAvailable();
}
}, 50);
} else if(afterServiceAvailable.size() > 0) {
for(Runnable runnable: afterServiceAvailable) {
if (getDownloadService() == null) {
handler.postDelayed(this::checkIfServiceAvailable, 50);
} else if (afterServiceAvailable.size() > 0) {
for (Runnable runnable : afterServiceAvailable) {
handler.post(runnable);
}
afterServiceAvailable.clear();
}
}
public static String getThemeName() {
return theme;
}
public boolean isTouchscreen() {
return touchscreen;
}
@ -918,11 +830,12 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
public void openNowPlaying() {
}
public void closeNowPlaying() {
}
public void setActiveServer(int instance) {
private void setActiveServer(int instance) {
if (Util.getActiveServer(this) != instance) {
final DownloadService service = getDownloadService();
if (service != null) {
@ -937,12 +850,13 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
}
Util.setActiveServer(this, instance);
invalidate();
UserUtil.refreshCurrentUser(this, false, true);
UserUtil.refreshCurrentUser(this);
updateDrawerHeader();
}
}
public void updateDrawerHeader() {
if(Util.isOffline(this)) {
private void updateDrawerHeader() {
if (Util.isOffline(this)) {
drawerServerName.setText(R.string.select_album_offline);
drawerUserName.setText("");
drawerHeader.setClickable(false);
@ -955,7 +869,7 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
}
}
public void toggleOffline() {
private void toggleOffline() {
boolean isOffline = Util.isOffline(this);
Util.setOffline(this, !isOffline);
invalidate();
@ -969,12 +883,12 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
drawer.closeDrawers();
}
public int getDrawerItemId(String fragmentType) {
if(fragmentType == null) {
int getDrawerItemId(String fragmentType) {
if (fragmentType == null) {
return R.id.drawer_library;
}
switch(fragmentType) {
switch (fragmentType) {
case "Artist":
return R.id.drawer_library;
case "Playlist":
@ -1013,7 +927,6 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte
PackageInfo packageInfo = context.getPackageManager().getPackageInfo("net.nullsum.audinaut", 0);
file = new File(Environment.getExternalStorageDirectory(), "audinaut-stacktrace.txt");
printWriter = new PrintWriter(file);
printWriter.println("Android API level: " + Build.VERSION.SDK);
printWriter.println("Subsonic version name: " + packageInfo.versionName);
printWriter.println("Subsonic version code: " + packageInfo.versionCode);
printWriter.println();

View File

@ -20,42 +20,28 @@ package net.nullsum.audinaut.activity;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.sothree.slidinguppanel.SlidingUpPanelLayout.PanelState;
import com.sothree.slidinguppanel.SlidingUpPanelLayout;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import com.sothree.slidinguppanel.SlidingUpPanelLayout.PanelState;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.domain.PlayerQueue;
import net.nullsum.audinaut.domain.PlayerState;
import net.nullsum.audinaut.fragments.DownloadFragment;
import net.nullsum.audinaut.fragments.NowPlayingFragment;
@ -66,8 +52,6 @@ import net.nullsum.audinaut.fragments.SelectPlaylistFragment;
import net.nullsum.audinaut.fragments.SubsonicFragment;
import net.nullsum.audinaut.service.DownloadFile;
import net.nullsum.audinaut.service.DownloadService;
import net.nullsum.audinaut.service.MusicService;
import net.nullsum.audinaut.service.MusicServiceFactory;
import net.nullsum.audinaut.updates.Updater;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.FileUtil;
@ -75,15 +59,16 @@ import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.util.UserUtil;
import net.nullsum.audinaut.util.Util;
import java.io.File;
import java.util.List;
/**
* Created by Scott on 10/14/13.
*/
public class SubsonicFragmentActivity extends SubsonicActivity implements DownloadService.OnSongChangedListener {
private static String TAG = SubsonicFragmentActivity.class.getSimpleName();
private static final String TAG = SubsonicFragmentActivity.class.getSimpleName();
private static boolean infoDialogDisplayed;
private static boolean sessionInitialized = false;
private static long ALLOWED_SKEW = 30000L;
private SlidingUpPanelLayout slideUpPanel;
private SlidingUpPanelLayout.PanelSlideListener panelSlideListener;
private boolean isPanelClosing = false;
@ -97,9 +82,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
private TextView trackView;
private TextView artistView;
private ImageButton startButton;
private long lastBackPressTime = 0;
private DownloadFile currentPlaying;
private PlayerState currentState;
private ImageButton previousButton;
private ImageButton nextButton;
private ImageButton rewindButton;
@ -107,14 +90,12 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
@Override
public void onCreate(Bundle savedInstanceState) {
if(savedInstanceState == null) {
if (savedInstanceState == null) {
String fragmentType = getIntent().getStringExtra(Constants.INTENT_EXTRA_FRAGMENT_TYPE);
boolean firstRun = false;
if (fragmentType == null) {
fragmentType = Util.openToTab(this);
if (fragmentType != null) {
firstRun = true;
}
fragmentType = "Library";
firstRun = true;
}
if ("".equals(fragmentType) || fragmentType == null || firstRun) {
@ -130,7 +111,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
stopService(new Intent(this, DownloadService.class));
finish();
getImageLoader().clearCache();
} else if(getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD_VIEW)) {
} else if (getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD_VIEW)) {
getIntent().putExtra(Constants.INTENT_EXTRA_FRAGMENT_TYPE, "Download");
lastSelectedPosition = R.id.drawer_downloading;
}
@ -138,17 +119,13 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
if (findViewById(R.id.fragment_container) != null && savedInstanceState == null) {
String fragmentType = getIntent().getStringExtra(Constants.INTENT_EXTRA_FRAGMENT_TYPE);
if(fragmentType == null) {
fragmentType = Util.openToTab(this);
if(fragmentType != null) {
getIntent().putExtra(Constants.INTENT_EXTRA_FRAGMENT_TYPE, fragmentType);
lastSelectedPosition = getDrawerItemId(fragmentType);
} else {
lastSelectedPosition = R.id.drawer_library;
}
if (fragmentType == null) {
fragmentType = "Library";
getIntent().putExtra(Constants.INTENT_EXTRA_FRAGMENT_TYPE, fragmentType);
lastSelectedPosition = getDrawerItemId(fragmentType);
MenuItem item = drawerList.getMenu().findItem(lastSelectedPosition);
if(item != null) {
if (item != null) {
item.setChecked(true);
}
} else {
@ -156,9 +133,9 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
}
currentFragment = getNewFragment(fragmentType);
if(getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_ID)) {
if (getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_ID)) {
Bundle currentArguments = currentFragment.getArguments();
if(currentArguments == null) {
if (currentArguments == null) {
currentArguments = new Bundle();
}
currentArguments.putString(Constants.INTENT_EXTRA_NAME_ID, getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_ID));
@ -167,14 +144,14 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
currentFragment.setPrimaryFragment(true);
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, currentFragment, currentFragment.getSupportTag() + "").commit();
if(getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_QUERY) != null) {
if (getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_QUERY) != null) {
SearchFragment fragment = new SearchFragment();
replaceFragment(fragment, fragment.getSupportTag());
}
// If a album type is set, switch to that album type view
String albumType = getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE);
if(albumType != null) {
if (albumType != null) {
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
@ -187,7 +164,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
}
}
slideUpPanel = (SlidingUpPanelLayout) findViewById(R.id.slide_up_panel);
slideUpPanel = findViewById(R.id.slide_up_panel);
panelSlideListener = new SlidingUpPanelLayout.PanelSlideListener() {
@Override
public void onPanelSlide(View panel, float slideOffset) {
@ -198,7 +175,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
public void onPanelStateChanged(View panel, PanelState previousState, PanelState newState) {
if (newState == PanelState.COLLAPSED) {
isPanelClosing = false;
if(bottomBar.getVisibility() == View.GONE) {
if (bottomBar.getVisibility() == View.GONE) {
bottomBar.setVisibility(View.VISIBLE);
nowPlayingToolbar.setVisibility(View.GONE);
nowPlayingFragment.setPrimaryFragment(false);
@ -217,7 +194,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
nowPlayingToolbar.setVisibility(View.VISIBLE);
setSupportActionBar(nowPlayingToolbar);
if(secondaryFragment == null) {
if (secondaryFragment == null) {
nowPlayingFragment.setPrimaryFragment(true);
} else {
secondaryFragment.setPrimaryFragment(true);
@ -230,24 +207,19 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
};
slideUpPanel.addPanelSlideListener(panelSlideListener);
if(getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD)) {
if (getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD)) {
// Post this later so it actually runs
handler.postDelayed(new Runnable() {
@Override
public void run() {
openNowPlaying();
}
}, 200);
handler.postDelayed(this::openNowPlaying, 200);
getIntent().removeExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD);
}
bottomBar = findViewById(R.id.bottom_bar);
mainToolbar = (Toolbar) findViewById(R.id.main_toolbar);
nowPlayingToolbar = (Toolbar) findViewById(R.id.now_playing_toolbar);
coverArtView = (ImageView) bottomBar.findViewById(R.id.album_art);
trackView = (TextView) bottomBar.findViewById(R.id.track_name);
artistView = (TextView) bottomBar.findViewById(R.id.artist_name);
mainToolbar = findViewById(R.id.main_toolbar);
nowPlayingToolbar = findViewById(R.id.now_playing_toolbar);
coverArtView = bottomBar.findViewById(R.id.album_art);
trackView = bottomBar.findViewById(R.id.track_name);
artistView = bottomBar.findViewById(R.id.artist_name);
setSupportActionBar(mainToolbar);
@ -258,97 +230,72 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
trans.commit();
}
rewindButton = (ImageButton) findViewById(R.id.download_rewind);
rewindButton.setOnClickListener(new View.OnClickListener() {
rewindButton = findViewById(R.id.download_rewind);
rewindButton.setOnClickListener(v -> new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) {
@Override
public void onClick(View v) {
new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) {
@Override
protected Void doInBackground() throws Throwable {
if (getDownloadService() == null) {
return null;
}
protected Void doInBackground() throws Throwable {
if (getDownloadService() == null) {
return null;
}
getDownloadService().rewind();
return null;
}
}.execute();
getDownloadService().rewind();
return null;
}
});
}.execute());
previousButton = (ImageButton) findViewById(R.id.download_previous);
previousButton.setOnClickListener(new View.OnClickListener() {
previousButton = findViewById(R.id.download_previous);
previousButton.setOnClickListener(v -> new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) {
@Override
public void onClick(View v) {
new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) {
@Override
protected Void doInBackground() throws Throwable {
if(getDownloadService() == null) {
return null;
}
protected Void doInBackground() throws Throwable {
if (getDownloadService() == null) {
return null;
}
getDownloadService().previous();
return null;
}
}.execute();
getDownloadService().previous();
return null;
}
});
}.execute());
startButton = (ImageButton) findViewById(R.id.download_start);
startButton.setOnClickListener(new View.OnClickListener() {
startButton = findViewById(R.id.download_start);
startButton.setOnClickListener(v -> new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) {
@Override
public void onClick(View v) {
new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) {
@Override
protected Void doInBackground() throws Throwable {
PlayerState state = getDownloadService().getPlayerState();
if(state == PlayerState.STARTED) {
getDownloadService().pause();
} else {
getDownloadService().start();
}
protected Void doInBackground() throws Throwable {
PlayerState state = getDownloadService().getPlayerState();
if (state == PlayerState.STARTED) {
getDownloadService().pause();
} else {
getDownloadService().start();
}
return null;
}
}.execute();
return null;
}
});
}.execute());
nextButton = (ImageButton) findViewById(R.id.download_next);
nextButton.setOnClickListener(new View.OnClickListener() {
nextButton = findViewById(R.id.download_next);
nextButton.setOnClickListener(v -> new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) {
@Override
public void onClick(View v) {
new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) {
@Override
protected Void doInBackground() throws Throwable {
if(getDownloadService() == null) {
return null;
}
protected Void doInBackground() throws Throwable {
if (getDownloadService() == null) {
return null;
}
getDownloadService().next();
return null;
}
}.execute();
getDownloadService().next();
return null;
}
});
}.execute());
fastforwardButton = (ImageButton) findViewById(R.id.download_fastforward);
fastforwardButton.setOnClickListener(new View.OnClickListener() {
fastforwardButton = findViewById(R.id.download_fastforward);
fastforwardButton.setOnClickListener(v -> new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) {
@Override
public void onClick(View v) {
new SilentBackgroundTask<Void>(SubsonicFragmentActivity.this) {
@Override
protected Void doInBackground() throws Throwable {
if (getDownloadService() == null) {
return null;
}
protected Void doInBackground() throws Throwable {
if (getDownloadService() == null) {
return null;
}
getDownloadService().fastForward();
return null;
}
}.execute();
getDownloadService().fastForward();
return null;
}
});
}.execute());
}
@Override
@ -363,17 +310,17 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if(currentFragment != null && intent.getStringExtra(Constants.INTENT_EXTRA_NAME_QUERY) != null) {
if(slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) {
if (currentFragment != null && intent.getStringExtra(Constants.INTENT_EXTRA_NAME_QUERY) != null) {
if (slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) {
closeNowPlaying();
}
if(currentFragment instanceof SearchFragment) {
if (currentFragment instanceof SearchFragment) {
String query = intent.getStringExtra(Constants.INTENT_EXTRA_NAME_QUERY);
boolean autoplay = intent.getBooleanExtra(Constants.INTENT_EXTRA_NAME_AUTOPLAY, false);
if (query != null) {
((SearchFragment)currentFragment).search(query, autoplay);
((SearchFragment) currentFragment).search(query, autoplay);
}
getIntent().removeExtra(Constants.INTENT_EXTRA_NAME_QUERY);
} else {
@ -382,17 +329,17 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
SearchFragment fragment = new SearchFragment();
replaceFragment(fragment, fragment.getSupportTag());
}
} else if(intent.getBooleanExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD, false)) {
if(slideUpPanel.getPanelState() != SlidingUpPanelLayout.PanelState.EXPANDED) {
} else if (intent.getBooleanExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD, false)) {
if (slideUpPanel.getPanelState() != SlidingUpPanelLayout.PanelState.EXPANDED) {
openNowPlaying();
}
} else {
if(slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) {
if (slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) {
closeNowPlaying();
}
setIntent(intent);
}
if(drawer != null) {
if (drawer != null) {
drawer.closeDrawers();
}
}
@ -401,16 +348,16 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
public void onResume() {
super.onResume();
if(getIntent().hasExtra(Constants.INTENT_EXTRA_VIEW_ALBUM)) {
if (getIntent().hasExtra(Constants.INTENT_EXTRA_VIEW_ALBUM)) {
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_ID));
args.putString(Constants.INTENT_EXTRA_NAME_NAME, getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_NAME));
args.putString(Constants.INTENT_EXTRA_SEARCH_SONG, getIntent().getStringExtra(Constants.INTENT_EXTRA_SEARCH_SONG));
if(getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_ARTIST)) {
if (getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_ARTIST)) {
args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true);
}
if(getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_CHILD_ID)) {
if (getIntent().hasExtra(Constants.INTENT_EXTRA_NAME_CHILD_ID)) {
args.putString(Constants.INTENT_EXTRA_NAME_CHILD_ID, getIntent().getStringExtra(Constants.INTENT_EXTRA_NAME_CHILD_ID));
}
fragment.setArguments(args);
@ -421,19 +368,14 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
UserUtil.seedCurrentUser(this);
createAccount();
runWhenServiceAvailable(new Runnable() {
@Override
public void run() {
getDownloadService().addOnSongChangedListener(SubsonicFragmentActivity.this, true);
}
});
runWhenServiceAvailable(() -> getDownloadService().addOnSongChangedListener(SubsonicFragmentActivity.this));
}
@Override
public void onPause() {
super.onPause();
DownloadService downloadService = getDownloadService();
if(downloadService != null) {
if (downloadService != null) {
downloadService.removeOnSongChangeListener(this);
}
}
@ -442,11 +384,12 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString(Constants.MAIN_NOW_PLAYING, nowPlayingFragment.getTag());
if(secondaryFragment != null) {
if (secondaryFragment != null) {
savedInstanceState.putString(Constants.MAIN_NOW_PLAYING_SECONDARY, secondaryFragment.getTag());
}
savedInstanceState.putInt(Constants.MAIN_SLIDE_PANEL_STATE, slideUpPanel.getPanelState().hashCode());
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
@ -456,7 +399,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
nowPlayingFragment = (NowPlayingFragment) fm.findFragmentByTag(id);
String secondaryId = savedInstanceState.getString(Constants.MAIN_NOW_PLAYING_SECONDARY);
if(secondaryId != null) {
if (secondaryId != null) {
secondaryFragment = (SubsonicFragment) fm.findFragmentByTag(secondaryId);
nowPlayingFragment.setPrimaryFragment(false);
@ -467,11 +410,11 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
trans.commit();
}
if(drawerToggle != null && backStack.size() > 0) {
if (drawerToggle != null && backStack.size() > 0) {
drawerToggle.setDrawerIndicatorEnabled(false);
}
if(savedInstanceState.getInt(Constants.MAIN_SLIDE_PANEL_STATE, -1) == SlidingUpPanelLayout.PanelState.EXPANDED.hashCode()) {
if (savedInstanceState.getInt(Constants.MAIN_SLIDE_PANEL_STATE, -1) == SlidingUpPanelLayout.PanelState.EXPANDED.hashCode()) {
panelSlideListener.onPanelStateChanged(null, null, PanelState.EXPANDED);
}
}
@ -479,28 +422,23 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
@Override
public void setContentView(int viewId) {
super.setContentView(viewId);
if(drawerToggle != null){
if (drawerToggle != null) {
drawerToggle.setDrawerIndicatorEnabled(true);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if(slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED && secondaryFragment == null) {
if (slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED && secondaryFragment == null) {
slideUpPanel.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
} else if(onBackPressedSupport()) {
} else if (onBackPressedSupport()) {
finish();
}
}
@Override
public boolean onBackPressedSupport() {
if(slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) {
boolean onBackPressedSupport() {
if (slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) {
removeCurrent();
return false;
} else {
@ -510,8 +448,8 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
@Override
public SubsonicFragment getCurrentFragment() {
if(slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) {
if(secondaryFragment == null) {
if (slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) {
if (secondaryFragment == null) {
return nowPlayingFragment;
} else {
return secondaryFragment;
@ -523,7 +461,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
@Override
public void replaceFragment(SubsonicFragment fragment, int tag, boolean replaceCurrent) {
if(slideUpPanel != null && slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED && !isPanelClosing) {
if (slideUpPanel != null && slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED && !isPanelClosing) {
secondaryFragment = fragment;
nowPlayingFragment.setPrimaryFragment(false);
secondaryFragment.setPrimaryFragment(true);
@ -538,9 +476,10 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
super.replaceFragment(fragment, tag, replaceCurrent);
}
}
@Override
public void removeCurrent() {
if(slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED && secondaryFragment != null) {
if (slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED && secondaryFragment != null) {
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
trans.remove(secondaryFragment);
@ -557,7 +496,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
@Override
public void setTitle(CharSequence title) {
if(slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) {
if (slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) {
getSupportActionBar().setTitle(title);
} else {
super.setTitle(title);
@ -568,7 +507,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
protected void drawerItemSelected(String fragmentType) {
super.drawerItemSelected(fragmentType);
if(slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) {
if (slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) {
slideUpPanel.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
}
}
@ -579,7 +518,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
// Clear existing stack
for(int i = backStack.size() - 1; i >= 0; i--) {
for (int i = backStack.size() - 1; i >= 0; i--) {
trans.remove(backStack.get(i));
}
trans.remove(currentFragment);
@ -594,14 +533,14 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
trans.commit();
supportInvalidateOptionsMenu();
recreateSpinner();
if(drawer != null) {
if (drawer != null) {
drawer.closeDrawers();
}
if(secondaryContainer != null) {
if (secondaryContainer != null) {
secondaryContainer.setVisibility(View.GONE);
}
if(drawerToggle != null) {
if (drawerToggle != null) {
drawerToggle.setDrawerIndicatorEnabled(true);
}
}
@ -610,6 +549,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
public void openNowPlaying() {
slideUpPanel.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);
}
@Override
public void closeNowPlaying() {
slideUpPanel.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
@ -617,25 +557,24 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
}
private SubsonicFragment getNewFragment(String fragmentType) {
if("Artist".equals(fragmentType)) {
if ("Artist".equals(fragmentType)) {
return new SelectArtistFragment();
} else if("Playlist".equals(fragmentType)) {
} else if ("Playlist".equals(fragmentType)) {
return new SelectPlaylistFragment();
} else if("Download".equals(fragmentType)) {
} else if ("Download".equals(fragmentType)) {
return new DownloadFragment();
} else {
return new SelectArtistFragment();
}
}
public void checkUpdates() {
private void checkUpdates() {
try {
String version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
int ver = Integer.parseInt(version.replace(".", ""));
Updater updater = new Updater(ver);
updater.checkUpdates(this);
}
catch(Exception e) {
} catch (Exception ignored) {
}
}
@ -643,12 +582,13 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
private void loadSession() {
loadSettings();
// If we are on Subsonic 5.2+, save play queue
if(!Util.isOffline(this)) {
if (!Util.isOffline(this)) {
loadRemotePlayQueue();
}
sessionInitialized = true;
}
private void loadSettings() {
PreferenceManager.setDefaultValues(this, R.xml.settings_appearance, false);
PreferenceManager.setDefaultValues(this, R.xml.settings_cache, false);
@ -660,9 +600,9 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
} else {
String path = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null);
File cacheLocation = new File(path);
if(!FileUtil.verifyCanWrite(cacheLocation)) {
if (!FileUtil.verifyCanWrite(cacheLocation)) {
// Only warn user if there is a difference saved
if(resetCacheLocation(prefs)) {
if (resetCacheLocation(prefs)) {
Util.info(this, R.string.common_warning, R.string.settings_cache_location_reset);
}
}
@ -679,7 +619,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
editor.putInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, 1);
editor.apply();
}
if(!prefs.contains(Constants.PREFERENCES_KEY_SERVER_COUNT)) {
if (!prefs.contains(Constants.PREFERENCES_KEY_SERVER_COUNT)) {
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(Constants.PREFERENCES_KEY_SERVER_COUNT, 1);
editor.apply();
@ -689,7 +629,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
private boolean resetCacheLocation(SharedPreferences prefs) {
String newDirectory = FileUtil.getDefaultMusicDirectory(this).getPath();
String oldDirectory = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null);
if(newDirectory == null || (oldDirectory != null && newDirectory.equals(oldDirectory))) {
if (newDirectory == null || (oldDirectory != null && newDirectory.equals(oldDirectory))) {
return false;
} else {
SharedPreferences.Editor editor = prefs.edit();
@ -700,36 +640,20 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
}
private void loadRemotePlayQueue() {
if(Util.getPreferences(this).getBoolean(Constants.PREFERENCES_KEY_RESUME_PLAY_QUEUE_NEVER, false)) {
if (Util.getPreferences(this).getBoolean(Constants.PREFERENCES_KEY_RESUME_PLAY_QUEUE_NEVER, false)) {
return;
}
final SubsonicActivity context = this;
new SilentBackgroundTask<Void>(this) {
private PlayerQueue playerQueue;
@Override
protected Void doInBackground() throws Throwable {
try {
MusicService musicService = MusicServiceFactory.getMusicService(context);
PlayerQueue remoteState = musicService.getPlayQueue(context, null);
// Make sure we wait until download service is ready
DownloadService downloadService = getDownloadService();
while(downloadService == null || !downloadService.isInitialized()) {
while (downloadService == null || !downloadService.isInitialized()) {
Util.sleepQuietly(100L);
downloadService = getDownloadService();
}
// If we had a remote state and it's changed is more recent than our existing state
if(remoteState != null && remoteState.changed != null) {
// Check if changed + 30 seconds since some servers have slight skew
Date remoteChange = new Date(remoteState.changed.getTime() - ALLOWED_SKEW);
Date localChange = downloadService.getLastStateChanged();
if(localChange == null || localChange.before(remoteChange)) {
playerQueue = remoteState;
}
}
} catch (Exception e) {
Log.e(TAG, "Failed to get playing queue to server", e);
}
@ -759,27 +683,18 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
return null;
}
@Override
protected void done(Void result) {
}
}.execute();
}
private void showInfoDialog() {
if (!infoDialogDisplayed) {
infoDialogDisplayed = true;
if (Util.getRestUrl(this, null).contains("demo.subsonic.org")) {
if (Util.getRestUrl(this).contains("demo.subsonic.org")) {
Util.info(this, R.string.main_welcome_title, R.string.main_welcome_text);
}
}
}
public Toolbar getActiveToolbar() {
return slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED ? nowPlayingToolbar : mainToolbar;
}
@Override
public void onSongChanged(DownloadFile currentPlaying, int currentPlayingIndex) {
this.currentPlaying = currentPlaying;
@ -789,7 +704,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
song = currentPlaying.getSong();
trackView.setText(song.getTitle());
if(song.getArtist() != null) {
if (song.getArtist() != null) {
artistView.setVisibility(View.VISIBLE);
artistView.setText(song.getArtist());
} else {
@ -820,7 +735,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
@Override
public void onSongsChanged(List<DownloadFile> songs, DownloadFile currentPlaying, int currentPlayingIndex) {
if(this.currentPlaying != currentPlaying || this.currentPlaying == null) {
if (this.currentPlaying != currentPlaying || this.currentPlaying == null) {
onSongChanged(currentPlaying, currentPlayingIndex);
}
}
@ -831,7 +746,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
}
@Override
public void onStateUpdate(DownloadFile downloadFile, PlayerState playerState) {
public void onStateUpdate(PlayerState playerState) {
int[] attrs = new int[]{(playerState == PlayerState.STARTED) ? R.attr.actionbar_pause : R.attr.actionbar_start};
TypedArray typedArray = this.obtainStyledAttributes(attrs);
startButton.setImageResource(typedArray.getResourceId(0, 0));
@ -840,7 +755,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
@Override
public void onMetadataUpdate(MusicDirectory.Entry song, int fieldChange) {
if(song != null && coverArtView != null && fieldChange == DownloadService.METADATA_UPDATED_COVER_ART) {
if (song != null && coverArtView != null && fieldChange == DownloadService.METADATA_UPDATED_COVER_ART) {
int height = coverArtView.getHeight();
if (height <= 0) {
int[] attrs = new int[]{R.attr.actionBarSize};
@ -851,7 +766,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo
getImageLoader().loadImage(coverArtView, song, false, height, false);
// We need to update it immediately since it won't update if updater is not running for it
if(nowPlayingFragment != null && slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.COLLAPSED) {
if (nowPlayingFragment != null && slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.COLLAPSED) {
nowPlayingFragment.onMetadataUpdate(song, fieldChange);
}
}

View File

@ -24,23 +24,18 @@ import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.SearchRecentSuggestions;
import android.util.Log;
import net.nullsum.audinaut.fragments.SubsonicFragment;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.provider.AudinautSearchProvider;
/**
* Receives voice search queries and forwards to the SearchFragment.
*
* <p>
* http://android-developers.blogspot.com/2010/09/supporting-new-music-voice-action.html
*
* @author Sindre Mehus
*/
public class VoiceQueryReceiverActivity extends Activity {
private static final String TAG = VoiceQueryReceiverActivity.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
@ -57,6 +52,5 @@ public class VoiceQueryReceiverActivity extends Activity {
Util.startActivityWithoutTransition(VoiceQueryReceiverActivity.this, intent);
}
finish();
Util.disablePendingTransition(this);
}
}

View File

@ -17,12 +17,12 @@ package net.nullsum.audinaut.adapter;
import android.content.Context;
import java.util.List;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.util.ImageLoader;
import net.nullsum.audinaut.view.FastScroller;
import java.util.List;
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);
@ -31,8 +31,8 @@ public class AlphabeticalAlbumAdapter extends EntryInfiniteGridAdapter implement
@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) {
if (position >= sections.get(0).size()) {
if (sections.get(0).size() > 0) {
return getTextToShowInBubble(position - 1);
} else {
return "*";

View File

@ -18,17 +18,12 @@ package net.nullsum.audinaut.adapter;
import android.content.Context;
import android.support.v7.widget.PopupMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.io.Serializable;
import java.util.List;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.Artist;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.domain.MusicDirectory.Entry;
import net.nullsum.audinaut.domain.MusicFolder;
import net.nullsum.audinaut.util.Util;
@ -37,16 +32,14 @@ import net.nullsum.audinaut.view.FastScroller;
import net.nullsum.audinaut.view.SongView;
import net.nullsum.audinaut.view.UpdateView;
import java.io.Serializable;
import java.util.List;
public class ArtistAdapter extends SectionAdapter<Serializable> implements FastScroller.BubbleTextGetter {
public static int VIEW_TYPE_SONG = 3;
public static int VIEW_TYPE_ARTIST = 4;
private List<MusicFolder> musicFolders;
private OnMusicFolderChanged onMusicFolderChanged;
public ArtistAdapter(Context context, List<Serializable> artists, OnItemClickedListener listener) {
this(context, artists, null, listener, null);
}
public static final int VIEW_TYPE_ARTIST = 4;
private static final int VIEW_TYPE_SONG = 3;
private final List<MusicFolder> musicFolders;
private final OnMusicFolderChanged onMusicFolderChanged;
public ArtistAdapter(Context context, List<Serializable> artists, List<MusicFolder> musicFolders, OnItemClickedListener onItemClickedListener, OnMusicFolderChanged onMusicFolderChanged) {
super(context, artists);
@ -54,7 +47,7 @@ public class ArtistAdapter extends SectionAdapter<Serializable> implements FastS
this.onItemClickedListener = onItemClickedListener;
this.onMusicFolderChanged = onMusicFolderChanged;
if(musicFolders != null) {
if (musicFolders != null) {
this.singleSectionHeader = true;
}
}
@ -62,46 +55,41 @@ public class ArtistAdapter extends SectionAdapter<Serializable> implements FastS
@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));
header.setOnClickListener(v -> {
PopupMenu popup = new PopupMenu(context, header.findViewById(R.id.select_artist_folder_2));
popup.getMenu().add(R.string.select_artist_all_folders);
popup.getMenu().add(R.string.select_artist_all_folders);
for (MusicFolder musicFolder : musicFolders) {
popup.getMenu().add(musicFolder.getName());
}
popup.setOnMenuItemClickListener(item -> {
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;
}
}
if(onMusicFolderChanged != null) {
onMusicFolderChanged.onMusicFolderChanged(null);
if (item.getTitle().equals(musicFolder.getName())) {
if (onMusicFolderChanged != null) {
onMusicFolderChanged.onMusicFolderChanged(musicFolder);
}
return true;
}
});
popup.show();
}
}
if (onMusicFolderChanged != null) {
onMusicFolderChanged.onMusicFolderChanged(null);
}
return true;
});
popup.show();
});
return new UpdateView.UpdateViewHolder(header, false);
return new UpdateView.UpdateViewHolder(header);
}
@Override
public void onBindHeaderHolder(UpdateView.UpdateViewHolder holder, String header, int sectionIndex) {
TextView folderName = (TextView) holder.getView().findViewById(R.id.select_artist_folder_2);
TextView folderName = holder.getView().findViewById(R.id.select_artist_folder_2);
String musicFolderId = Util.getSelectedMusicFolderId(context);
if(musicFolderId != null) {
if (musicFolderId != null) {
for (MusicFolder musicFolder : musicFolders) {
if (musicFolder.getId().equals(musicFolderId)) {
folderName.setText(musicFolder.getName());
@ -114,11 +102,11 @@ public class ArtistAdapter extends SectionAdapter<Serializable> implements FastS
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(int viewType) {
UpdateView updateView = null;
if(viewType == VIEW_TYPE_ARTIST) {
if (viewType == VIEW_TYPE_ARTIST) {
updateView = new ArtistView(context);
} else if(viewType == VIEW_TYPE_SONG) {
} else if (viewType == VIEW_TYPE_SONG) {
updateView = new SongView(context);
}
@ -128,9 +116,9 @@ public class ArtistAdapter extends SectionAdapter<Serializable> implements FastS
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Serializable item, int viewType) {
UpdateView view = holder.getUpdateView();
if(viewType == VIEW_TYPE_ARTIST) {
if (viewType == VIEW_TYPE_ARTIST) {
view.setObject(item);
} else if(viewType == VIEW_TYPE_SONG) {
} else if (viewType == VIEW_TYPE_SONG) {
SongView songView = (SongView) view;
Entry entry = (Entry) item;
songView.setObject(entry, checkable);
@ -139,7 +127,7 @@ public class ArtistAdapter extends SectionAdapter<Serializable> implements FastS
@Override
public int getItemViewType(Serializable item) {
if(item instanceof Artist) {
if (item instanceof Artist) {
return VIEW_TYPE_ARTIST;
} else {
return VIEW_TYPE_SONG;
@ -149,7 +137,7 @@ public class ArtistAdapter extends SectionAdapter<Serializable> implements FastS
@Override
public String getTextToShowInBubble(int position) {
Object item = getItemForPosition(position);
if(item instanceof Artist) {
if (item instanceof Artist) {
return getNameIndex(((Artist) item).getName(), true);
} else {
return null;

View File

@ -16,15 +16,14 @@
package net.nullsum.audinaut.adapter;
import android.content.Context;
import android.view.ViewGroup;
import java.util.List;
import net.nullsum.audinaut.view.BasicListView;
import net.nullsum.audinaut.view.UpdateView;
import java.util.List;
public class BasicListAdapter extends SectionAdapter<String> {
public static int VIEW_TYPE_LINE = 1;
private static final int VIEW_TYPE_LINE = 1;
public BasicListAdapter(Context context, List<String> strings, OnItemClickedListener listener) {
super(context, strings);
@ -32,7 +31,7 @@ public class BasicListAdapter extends SectionAdapter<String> {
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(int viewType) {
return new UpdateView.UpdateViewHolder(new BasicListView(context));
}

View File

@ -16,8 +16,6 @@
package net.nullsum.audinaut.adapter;
import android.content.Context;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
@ -25,32 +23,32 @@ import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
import net.nullsum.audinaut.R;
public class DetailsAdapter extends ArrayAdapter<String> {
private List<String> headers;
private List<String> details;
import java.util.List;
public DetailsAdapter(Context context, int layout, List<String> headers, List<String> details) {
super(context, layout, headers);
public class DetailsAdapter extends ArrayAdapter<String> {
private final List<String> headers;
private final List<String> details;
public DetailsAdapter(Context context, List<String> headers, List<String> details) {
super(context, R.layout.details_item, headers);
this.headers = headers;
this.details = details;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null) {
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 = view.findViewById(R.id.detail_name);
TextView detailsView = view.findViewById(R.id.detail_value);
nameView.setText(headers.get(position));

View File

@ -18,12 +18,6 @@ package net.nullsum.audinaut.adapter;
import android.content.Context;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import java.util.List;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.service.DownloadFile;
@ -32,8 +26,10 @@ import net.nullsum.audinaut.view.FastScroller;
import net.nullsum.audinaut.view.SongView;
import net.nullsum.audinaut.view.UpdateView;
import java.util.List;
public class DownloadFileAdapter extends SectionAdapter<DownloadFile> implements FastScroller.BubbleTextGetter {
public static int VIEW_TYPE_DOWNLOAD_FILE = 1;
private static final int VIEW_TYPE_DOWNLOAD_FILE = 1;
public DownloadFileAdapter(Context context, List<DownloadFile> entries, OnItemClickedListener onItemClickedListener) {
super(context, entries);
@ -42,7 +38,7 @@ public class DownloadFileAdapter extends SectionAdapter<DownloadFile> implements
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(int viewType) {
return new UpdateView.UpdateViewHolder(new SongView(context));
}
@ -65,7 +61,7 @@ public class DownloadFileAdapter extends SectionAdapter<DownloadFile> implements
@Override
public void onCreateActionModeMenu(Menu menu, MenuInflater menuInflater) {
if(Util.isOffline(context)) {
if (Util.isOffline(context)) {
menuInflater.inflate(R.menu.multiselect_nowplaying_offline, menu);
} else {
menuInflater.inflate(R.menu.multiselect_nowplaying, menu);

View File

@ -18,11 +18,6 @@ package net.nullsum.audinaut.adapter;
import android.content.Context;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.MusicDirectory;
@ -34,19 +29,17 @@ import net.nullsum.audinaut.view.SongView;
import net.nullsum.audinaut.view.UpdateView;
import net.nullsum.audinaut.view.UpdateView.UpdateViewHolder;
import java.util.List;
public class EntryGridAdapter extends SectionAdapter<Entry> {
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;
private ImageLoader imageLoader;
private boolean largeAlbums;
public static final int VIEW_TYPE_ALBUM_CELL = 1;
public static final int VIEW_TYPE_ALBUM_LINE = 2;
public static final int VIEW_TYPE_SONG = 3;
private final ImageLoader imageLoader;
private final 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);
@ -55,12 +48,12 @@ public class EntryGridAdapter extends SectionAdapter<Entry> {
// Always show artist if they aren't all the same
String artist = null;
for(MusicDirectory.Entry entry: entries) {
if(artist == null) {
for (MusicDirectory.Entry entry : entries) {
if (artist == null) {
artist = entry.getArtist();
}
if(artist != null && !artist.equals(entry.getArtist())) {
if (artist != null && !artist.equals(entry.getArtist())) {
showArtist = true;
}
}
@ -68,11 +61,11 @@ public class EntryGridAdapter extends SectionAdapter<Entry> {
}
@Override
public UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
public UpdateViewHolder onCreateSectionViewHolder(int viewType) {
UpdateView updateView = null;
if(viewType == VIEW_TYPE_ALBUM_LINE || viewType == VIEW_TYPE_ALBUM_CELL) {
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) {
} else if (viewType == VIEW_TYPE_SONG) {
updateView = new SongView(context);
}
@ -82,27 +75,24 @@ public class EntryGridAdapter extends SectionAdapter<Entry> {
@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) {
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) {
} 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) {
}
@Override
public int getItemViewType(Entry entry) {
if(entry.isDirectory()) {
if (entry.isDirectory()) {
if (largeAlbums) {
return VIEW_TYPE_ALBUM_CELL;
} else {
@ -113,27 +103,16 @@ public class EntryGridAdapter extends SectionAdapter<Entry> {
}
}
public void setHeader(View header) {
this.header = header;
this.singleSectionHeader = true;
}
public View getHeader() {
return header;
public void setShowArtist() {
this.showArtist = true;
}
public void setShowArtist(boolean showArtist) {
this.showArtist = showArtist;
}
public void setShowAlbum(boolean showAlbum) {
this.showAlbum = showAlbum;
public void setShowAlbum() {
this.showAlbum = true;
}
public void removeAt(int index) {
sections.get(0).remove(index);
if(header != null) {
index++;
}
notifyItemRemoved(index);
}
@ -143,13 +122,13 @@ public class EntryGridAdapter extends SectionAdapter<Entry> {
@Override
public void onCreateActionModeMenu(Menu menu, MenuInflater menuInflater) {
if(Util.isOffline(context)) {
if (Util.isOffline(context)) {
menuInflater.inflate(R.menu.multiselect_media_offline, menu);
} else {
menuInflater.inflate(R.menu.multiselect_media, menu);
}
if(!removeFromPlaylist) {
if (!removeFromPlaylist) {
menu.removeItem(R.id.menu_remove_playlist);
}
}

View File

@ -20,8 +20,6 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.domain.MusicDirectory.Entry;
@ -32,8 +30,10 @@ import net.nullsum.audinaut.util.ImageLoader;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.view.UpdateView;
import java.util.List;
public class EntryInfiniteGridAdapter extends EntryGridAdapter {
public static int VIEW_TYPE_LOADING = 4;
public static final int VIEW_TYPE_LOADING = 4;
private String type;
private String extra;
@ -48,10 +48,10 @@ public class EntryInfiniteGridAdapter extends EntryGridAdapter {
@Override
public UpdateView.UpdateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == VIEW_TYPE_LOADING) {
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 new UpdateView.UpdateViewHolder(progress);
}
return super.onCreateViewHolder(parent, viewType);
@ -59,7 +59,7 @@ public class EntryInfiniteGridAdapter extends EntryGridAdapter {
@Override
public int getItemViewType(int position) {
if(isLoadingView(position)) {
if (isLoadingView(position)) {
return VIEW_TYPE_LOADING;
}
@ -68,7 +68,7 @@ public class EntryInfiniteGridAdapter extends EntryGridAdapter {
@Override
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, int position) {
if(!isLoadingView(position)) {
if (!isLoadingView(position)) {
super.onBindViewHolder(holder, position);
}
}
@ -77,7 +77,7 @@ public class EntryInfiniteGridAdapter extends EntryGridAdapter {
public int getItemCount() {
int size = super.getItemCount();
if(!allLoaded) {
if (!allLoaded) {
size++;
}
@ -89,13 +89,13 @@ public class EntryInfiniteGridAdapter extends EntryGridAdapter {
this.extra = extra;
this.size = size;
if(super.getItemCount() < size) {
if (super.getItemCount() < size) {
allLoaded = true;
}
}
public void loadMore() {
if(loading || allLoaded) {
if (loading || allLoaded) {
return;
}
loading = true;
@ -114,7 +114,7 @@ public class EntryInfiniteGridAdapter extends EntryGridAdapter {
appendCachedData(newData);
loading = false;
if(newData.size() < size) {
if (newData.size() < size) {
allLoaded = true;
notifyDataSetChanged();
}
@ -122,15 +122,15 @@ public class EntryInfiniteGridAdapter extends EntryGridAdapter {
}.execute();
}
protected List<Entry> cacheInBackground() throws Exception {
private 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)) {
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)) {
} 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) {
} else if (type.contains(MainFragment.SONGS_LIST_PREFIX)) {
result = service.getSongList(type, size, offset, context, null);
} else {
result = service.getAlbumList(type, size, offset, false, context, null);
@ -138,15 +138,15 @@ public class EntryInfiniteGridAdapter extends EntryGridAdapter {
return result.getChildren();
}
protected void appendCachedData(List<Entry> newData) {
if(newData.size() > 0) {
private 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) {
private boolean isLoadingView(int position) {
return !allLoaded && position >= sections.get(0).size();
}
}

View File

@ -16,51 +16,34 @@
package net.nullsum.audinaut.adapter;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.util.DrawableTint;
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();
import java.util.ArrayList;
import java.util.List;
abstract class ExpandableSectionAdapter<T> extends SectionAdapter<T> {
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;
private List<List<T>> sectionsExtras;
protected ExpandableSectionAdapter() {}
public ExpandableSectionAdapter(Context context, List<T> section) {
List<List<T>> sections = new ArrayList<>();
sections.add(section);
ExpandableSectionAdapter() {
}
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) {
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) {
if (sectionsDefaultVisible == null) {
sectionsDefaultVisible = new ArrayList<>(fullSections.size());
for(int i = 0; i < fullSections.size(); i++) {
for (int i = 0; i < fullSections.size(); i++) {
sectionsDefaultVisible.add(DEFAULT_VISIBLE);
}
}
@ -68,11 +51,11 @@ public abstract class ExpandableSectionAdapter<T> extends SectionAdapter<T> {
this.sections = new ArrayList<>();
this.sectionsExtras = new ArrayList<>();
int i = 0;
for(List<T> fullSection: fullSections) {
for (List<T> fullSection : fullSections) {
List<T> visibleSection = new ArrayList<>();
Integer defaultVisible = sectionsDefaultVisible.get(i);
if(defaultVisible == null || defaultVisible >= fullSection.size()) {
if (defaultVisible == null || defaultVisible >= fullSection.size()) {
visibleSection.addAll(fullSection);
this.sectionsExtras.add(null);
} else {
@ -83,9 +66,6 @@ public abstract class ExpandableSectionAdapter<T> extends SectionAdapter<T> {
i++;
}
expandToggleRes = DrawableTint.getDrawableRes(context, EXPAND_TOGGLE);
collapseToggleRes = DrawableTint.getDrawableRes(context, COLLAPSE_TOGGLE);
}
@Override
@ -96,39 +76,36 @@ public abstract class ExpandableSectionAdapter<T> extends SectionAdapter<T> {
@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);
ImageView toggleSelectionView = view.findViewById(R.id.item_select);
List<T> visibleSelection = sections.get(sectionIndex);
List<T> sectionExtras = sectionsExtras.get(sectionIndex);
if(sectionExtras != null && !sectionExtras.isEmpty()) {
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);
toggleSelectionView.setOnClickListener(v -> {
List<T> visibleSelection1 = sections.get(sectionIndex);
List<T> sectionExtras1 = sectionsExtras.get(sectionIndex);
// Update icon
int selectToggleAttr;
if (!visibleSelection.contains(sectionExtras.get(0))) {
selectToggleAttr = COLLAPSE_TOGGLE;
// Update icon
int selectToggleAttr;
if (!visibleSelection1.contains(sectionExtras1.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(visibleSelection1.get(visibleSelection1.size() - 1));
visibleSelection1.addAll(sectionExtras1);
notifyItemRangeInserted(lastIndex, sectionExtras1.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());
}
((ImageView) v).setImageResource(DrawableTint.getDrawableRes(context, selectToggleAttr));
// Update how many are displayed
visibleSelection1.removeAll(sectionExtras1);
int lastIndex = getItemPosition(visibleSelection1.get(visibleSelection1.size() - 1));
notifyItemRangeRemoved(lastIndex, sectionExtras1.size());
}
((ImageView) v).setImageResource(DrawableTint.getDrawableRes(context, selectToggleAttr));
});
int selectToggleAttr;
@ -143,7 +120,7 @@ public abstract class ExpandableSectionAdapter<T> extends SectionAdapter<T> {
toggleSelectionView.setVisibility(View.GONE);
}
if(view != null) {
if (view != null) {
view.setObject(header);
}
}

View File

@ -16,7 +16,7 @@
package net.nullsum.audinaut.adapter;
import android.content.Context;
import android.view.ViewGroup;
import net.nullsum.audinaut.domain.Genre;
import net.nullsum.audinaut.view.FastScroller;
import net.nullsum.audinaut.view.GenreView;
@ -24,8 +24,8 @@ 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 class GenreAdapter extends SectionAdapter<Genre> implements FastScroller.BubbleTextGetter {
private static final int VIEW_TYPE_GENRE = 1;
public GenreAdapter(Context context, List<Genre> genres, OnItemClickedListener listener) {
super(context, genres);
@ -33,7 +33,7 @@ public class GenreAdapter extends SectionAdapter<Genre> implements FastScroller.
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(int viewType) {
return new UpdateView.UpdateViewHolder(new GenreView(context));
}

View File

@ -19,18 +19,16 @@ import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import java.util.List;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.view.BasicHeaderView;
import net.nullsum.audinaut.view.BasicListView;
import net.nullsum.audinaut.view.UpdateView;
import java.util.List;
public class MainAdapter extends SectionAdapter<Integer> {
public static final int VIEW_TYPE_ALBUM_LIST = 1;
private 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);
@ -38,7 +36,7 @@ public class MainAdapter extends SectionAdapter<Integer> {
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(int viewType) {
UpdateView updateView = new BasicListView(context);
return new UpdateView.UpdateViewHolder(updateView);
}
@ -47,7 +45,7 @@ public class MainAdapter extends SectionAdapter<Integer> {
public void onBindViewHolder(UpdateView.UpdateViewHolder holder, Integer item, int viewType) {
UpdateView updateView = holder.getUpdateView();
if(viewType == VIEW_TYPE_ALBUM_LIST) {
if (viewType == VIEW_TYPE_ALBUM_LIST) {
updateView.setObject(context.getResources().getString(item));
} else {
updateView.setObject(item);
@ -63,13 +61,14 @@ public class MainAdapter extends SectionAdapter<Integer> {
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);
CheckBox checkBox = view.findViewById(R.id.item_checkbox);
String display;
if("songs".equals(header)) {
if ("songs".equals(header)) {
display = context.getResources().getString(R.string.search_songs);
checkBox.setVisibility(View.GONE);
} else {
@ -77,7 +76,7 @@ public class MainAdapter extends SectionAdapter<Integer> {
checkBox.setVisibility(View.GONE);
}
if(view != null) {
if (view != null) {
view.setObject(display);
}
}

View File

@ -16,20 +16,19 @@ package net.nullsum.audinaut.adapter;
import android.content.Context;
import java.util.List;
import android.view.ViewGroup;
import net.nullsum.audinaut.domain.Playlist;
import net.nullsum.audinaut.util.ImageLoader;
import net.nullsum.audinaut.view.FastScroller;
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;
import java.util.List;
private ImageLoader imageLoader;
private boolean largeCell;
public class PlaylistAdapter extends SectionAdapter<Playlist> implements FastScroller.BubbleTextGetter {
private static final int VIEW_TYPE_PLAYLIST = 1;
private final ImageLoader imageLoader;
private final boolean largeCell;
public PlaylistAdapter(Context context, List<Playlist> playlists, ImageLoader imageLoader, boolean largeCell, OnItemClickedListener listener) {
super(context, playlists);
@ -37,15 +36,9 @@ public class PlaylistAdapter extends SectionAdapter<Playlist> implements FastScr
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) {
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(int viewType) {
return new UpdateView.UpdateViewHolder(new PlaylistView(context, imageLoader, largeCell));
}
@ -63,7 +56,7 @@ public class PlaylistAdapter extends SectionAdapter<Playlist> implements FastScr
@Override
public String getTextToShowInBubble(int position) {
Object item = getItemForPosition(position);
if(item instanceof Playlist) {
if (item instanceof Playlist) {
return getNameIndex(((Playlist) item).getName());
} else {
return null;

View File

@ -19,38 +19,32 @@ import android.content.Context;
import android.content.res.Resources;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.MusicDirectory.Entry;
import net.nullsum.audinaut.domain.SearchResult;
import net.nullsum.audinaut.util.DrawableTint;
import net.nullsum.audinaut.util.ImageLoader;
import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.view.AlbumView;
import net.nullsum.audinaut.view.ArtistView;
import net.nullsum.audinaut.view.BasicHeaderView;
import net.nullsum.audinaut.view.SongView;
import net.nullsum.audinaut.view.UpdateView;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import static net.nullsum.audinaut.adapter.ArtistAdapter.VIEW_TYPE_ARTIST;
import static net.nullsum.audinaut.adapter.EntryGridAdapter.VIEW_TYPE_ALBUM_CELL;
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 static final int MAX_ARTISTS = 10;
private static final int MAX_ALBUMS = 4;
private static final int MAX_SONGS = 10;
private final ImageLoader imageLoader;
private final boolean largeAlbums;
public SearchAdapter(Context context, SearchResult searchResult, ImageLoader imageLoader, boolean largeAlbums, OnItemClickedListener listener) {
this.imageLoader = imageLoader;
@ -60,17 +54,17 @@ public class SearchAdapter extends ExpandableSectionAdapter<Serializable> {
List<String> headers = new ArrayList<>();
List<Integer> defaultVisible = new ArrayList<>();
Resources res = context.getResources();
if(!searchResult.getArtists().isEmpty()) {
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()) {
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()) {
if (!searchResult.getSongs().isEmpty()) {
sections.add((List<Serializable>) (List<?>) searchResult.getSongs());
headers.add(res.getString(R.string.search_songs));
defaultVisible.add(MAX_SONGS);
@ -82,13 +76,13 @@ public class SearchAdapter extends ExpandableSectionAdapter<Serializable> {
}
@Override
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType) {
public UpdateView.UpdateViewHolder onCreateSectionViewHolder(int viewType) {
UpdateView updateView = null;
if(viewType == VIEW_TYPE_ALBUM_CELL || viewType == VIEW_TYPE_ALBUM_LINE) {
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) {
} else if (viewType == VIEW_TYPE_SONG) {
updateView = new SongView(context);
} else if(viewType == VIEW_TYPE_ARTIST) {
} else if (viewType == VIEW_TYPE_ARTIST) {
updateView = new ArtistView(context);
}
@ -98,20 +92,20 @@ public class SearchAdapter extends ExpandableSectionAdapter<Serializable> {
@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) {
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) {
} else if (viewType == VIEW_TYPE_SONG) {
SongView songView = (SongView) view;
songView.setObject((Entry) item, true);
} else if(viewType == VIEW_TYPE_ARTIST) {
} else if (viewType == VIEW_TYPE_ARTIST) {
view.setObject(item);
}
}
@Override
public int getItemViewType(Serializable item) {
if(item instanceof Entry) {
if (item instanceof Entry) {
Entry entry = (Entry) item;
if (entry.isDirectory()) {
if (largeAlbums) {
@ -129,7 +123,7 @@ public class SearchAdapter extends ExpandableSectionAdapter<Serializable> {
@Override
public void onCreateActionModeMenu(Menu menu, MenuInflater menuInflater) {
if(Util.isOffline(context)) {
if (Util.isOffline(context)) {
menuInflater.inflate(R.menu.multiselect_media_offline, menu);
} else {
menuInflater.inflate(R.menu.multiselect_media, menu);

View File

@ -32,10 +32,6 @@ import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupMenu;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.activity.SubsonicFragmentActivity;
import net.nullsum.audinaut.util.Constants;
@ -45,129 +41,103 @@ import net.nullsum.audinaut.view.BasicHeaderView;
import net.nullsum.audinaut.view.UpdateView;
import net.nullsum.audinaut.view.UpdateView.UpdateViewHolder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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;
public static final int VIEW_TYPE_HEADER = 0;
private static final String TAG = SectionAdapter.class.getSimpleName();
private static String[] ignoredArticles;
private final List<T> selected = new ArrayList<>();
private final List<UpdateView> selectedViews = new ArrayList<>();
Context context;
List<String> headers;
List<List<T>> sections;
boolean singleSectionHeader;
OnItemClickedListener<T> onItemClickedListener;
boolean checkable = false;
private ActionMode currentActionMode;
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);
SectionAdapter() {
}
public SectionAdapter(Context context, List<T> section, boolean singleSectionHeader) {
SectionAdapter(Context context, List<T> section) {
this.context = context;
this.headers = Arrays.asList("Section");
this.headers = Collections.singletonList("Section");
this.sections = new ArrayList<>();
this.sections.add(section);
this.singleSectionHeader = singleSectionHeader;
this.singleSectionHeader = false;
}
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){
SectionAdapter(Context context, List<String> headers, List<List<T>> sections) {
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();
this.singleSectionHeader = true;
}
@Override
public UpdateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == VIEW_TYPE_HEADER) {
if (viewType == VIEW_TYPE_HEADER) {
return onCreateHeaderHolder(parent);
} else {
final UpdateViewHolder<T> holder = onCreateSectionViewHolder(parent, viewType);
final UpdateViewHolder<T> holder = onCreateSectionViewHolder(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 (selected.isEmpty()) {
currentActionMode.finish();
} else {
currentActionMode.setTitle(context.getResources().getString(R.string.select_album_n_selected, selected.size()));
}
if (updateView != null) {
updateView.getChildAt(0).setOnClickListener(v -> {
T item = holder.getItem();
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);
}
} 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);
moreButton.setOnClickListener(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(menuItem -> 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();
}
if (checkable) {
updateView.getChildAt(0).setOnLongClickListener(v -> {
if (updateView.isCheckable()) {
if (currentActionMode == null) {
startActionMode(holder);
} else {
updateView.getChildAt(0).performClick();
}
return true;
}
return true;
});
}
}
@ -181,7 +151,7 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH
public void onBindViewHolder(UpdateViewHolder holder, int position) {
UpdateView updateView = holder.getUpdateView();
if(sections.size() == 1 && !singleSectionHeader) {
if (sections.size() == 1 && !singleSectionHeader) {
T item = sections.get(0).get(position);
onBindViewHolder(holder, item, getItemViewType(position));
postBindView(updateView, item);
@ -191,15 +161,15 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH
int subPosition = 0;
int subHeader = 0;
for(List<T> section: sections) {
for (List<T> section : sections) {
boolean validHeader = headers.get(subHeader) != null;
if(position == subPosition && validHeader) {
if (position == subPosition && validHeader) {
onBindHeaderHolder(holder, headers.get(subHeader), subHeader);
return;
}
int headerOffset = validHeader ? 1 : 0;
if(position < (subPosition + section.size() + headerOffset)) {
if (position < (subPosition + section.size() + headerOffset)) {
T item = section.get(position - subPosition - headerOffset);
onBindViewHolder(holder, item, getItemViewType(item));
@ -209,7 +179,7 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH
}
subPosition += section.size();
if(validHeader) {
if (validHeader) {
subPosition += 1;
}
subHeader++;
@ -217,13 +187,13 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH
}
private void postBindView(UpdateView updateView, T item) {
if(updateView.isCheckable()) {
if (updateView.isCheckable()) {
setChecked(updateView, selected.contains(item));
}
View moreButton = updateView.findViewById(R.id.item_more);
if(moreButton != null) {
if(onItemClickedListener != null) {
if (moreButton != null) {
if (onItemClickedListener != null) {
PopupMenu popup = new PopupMenu(context, moreButton);
Menu menu = popup.getMenu();
onItemClickedListener.onCreateContextMenu(popup.getMenu(), popup.getMenuInflater(), updateView, item);
@ -240,17 +210,17 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH
@Override
public int getItemCount() {
if(sections.size() == 1 && !singleSectionHeader) {
if (sections.size() == 1 && !singleSectionHeader) {
return sections.get(0).size();
}
int count = 0;
for(String header: headers) {
if(header != null) {
for (String header : headers) {
if (header != null) {
count++;
}
}
for(List<T> section: sections) {
for (List<T> section : sections) {
count += section.size();
}
@ -259,25 +229,25 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH
@Override
public int getItemViewType(int position) {
if(sections.size() == 1 && !singleSectionHeader) {
if (sections.size() == 1 && !singleSectionHeader) {
return getItemViewType(sections.get(0).get(position));
}
int subPosition = 0;
int subHeader = 0;
for(List<T> section: sections) {
for (List<T> section : sections) {
boolean validHeader = headers.get(subHeader) != null;
if(position == subPosition && validHeader) {
if (position == subPosition && validHeader) {
return VIEW_TYPE_HEADER;
}
int headerOffset = validHeader ? 1 : 0;
if(position < (subPosition + section.size() + headerOffset)) {
if (position < (subPosition + section.size() + headerOffset)) {
return getItemViewType(section.get(position - subPosition - headerOffset));
}
subPosition += section.size();
if(validHeader) {
if (validHeader) {
subPosition += 1;
}
subHeader++;
@ -286,28 +256,29 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH
return -1;
}
public UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) {
UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) {
return new UpdateViewHolder(new BasicHeaderView(context));
}
public void onBindHeaderHolder(UpdateViewHolder holder, String header, int sectionIndex) {
void onBindHeaderHolder(UpdateViewHolder holder, String header, int sectionIndex) {
UpdateView view = holder.getUpdateView();
if(view != null) {
if (view != null) {
view.setObject(header);
}
}
public T getItemForPosition(int position) {
if(sections.size() == 1 && !singleSectionHeader) {
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) {
for (List<T> section : sections) {
if (position == subPosition) {
return null;
}
if(position <= (subPosition + section.size())) {
if (position <= (subPosition + section.size())) {
return section.get(position - subPosition - 1);
}
@ -316,17 +287,18 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH
return null;
}
public int getItemPosition(T item) {
if(sections.size() == 1 && !singleSectionHeader) {
if (sections.size() == 1 && !singleSectionHeader) {
return sections.get(0).indexOf(item);
}
int subPosition = 0;
for(List<T> section: sections) {
for (List<T> section : sections) {
subPosition += section.size() + 1;
int position = section.indexOf(item);
if(position != -1) {
if (position != -1) {
return position + subPosition;
}
}
@ -341,6 +313,7 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH
public void addSelected(T item) {
selected.add(item);
}
public List<T> getSelected() {
List<T> selected = new ArrayList<>();
selected.addAll(this.selected);
@ -348,17 +321,9 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH
}
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();
for(UpdateView updateView: selectedViews) {
for (UpdateView updateView : selectedViews) {
updateView.setChecked(false);
}
}
@ -366,9 +331,9 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH
public void moveItem(int from, int to) {
List<T> section = sections.get(0);
int max = section.size();
if(to >= max) {
if (to >= max) {
to = max - 1;
} else if(to < 0) {
} else if (to < 0) {
to = 0;
}
@ -377,10 +342,11 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH
notifyItemMoved(from, to);
}
public void removeItem(T item) {
int subPosition = 0;
for(List<T> section: sections) {
if(sections.size() > 1 || singleSectionHeader) {
for (List<T> section : sections) {
if (sections.size() > 1 || singleSectionHeader) {
subPosition++;
}
@ -395,16 +361,18 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH
}
}
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) {
protected abstract UpdateView.UpdateViewHolder onCreateSectionViewHolder(int viewType);
protected abstract void onBindViewHolder(UpdateViewHolder holder, T item, int viewType);
protected abstract int getItemViewType(T item);
private void setChecked(UpdateView updateView, boolean checked) {
updateView.setChecked(checked);
}
public void onCreateActionModeMenu(Menu menu, MenuInflater menuInflater) {}
void onCreateActionModeMenu(Menu menu, MenuInflater menuInflater) {
}
private void startActionMode(final UpdateView.UpdateViewHolder<T> holder) {
final UpdateView<T> updateView = holder.getUpdateView();
@ -470,21 +438,23 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH
});
}
}
public void stopActionMode() {
if(currentActionMode != null) {
if (currentActionMode != null) {
currentActionMode.finish();
}
}
public String getNameIndex(String name) {
String getNameIndex(String name) {
return getNameIndex(name, false);
}
public String getNameIndex(String name, boolean removeIgnoredArticles) {
if(name == null) {
String getNameIndex(String name, boolean removeIgnoredArticles) {
if (name == null) {
return "*";
}
if(removeIgnoredArticles) {
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");
@ -510,7 +480,9 @@ public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewH
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,121 +0,0 @@
/*
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
*/
package net.nullsum.audinaut.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.User;
import net.nullsum.audinaut.util.ImageLoader;
import net.nullsum.audinaut.util.UserUtil;
import net.nullsum.audinaut.view.BasicHeaderView;
import net.nullsum.audinaut.view.RecyclingImageView;
import net.nullsum.audinaut.view.SettingView;
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 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;
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;
}
}
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 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 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());
if(user.getMusicFolderSettings() != null) {
settingsSections.add(user.getMusicFolderSettings());
}
return new SettingsAdapter(context, user, headers, settingsSections, imageLoader, isEditable, onItemClickedListener);
}
}

View File

@ -19,14 +19,8 @@
package net.nullsum.audinaut.audiofx;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.audiofx.AudioEffect;
import android.media.audiofx.LoudnessEnhancer;
import android.os.Build;
import android.util.Log;
public class AudioEffectsController {
private static final String TAG = AudioEffectsController.class.getSimpleName();
private final Context context;
private int audioSessionId = 0;
@ -39,7 +33,7 @@ public class AudioEffectsController {
}
public void release() {
if(equalizerController != null) {
if (equalizerController != null) {
equalizerController.release();
}
}

View File

@ -18,14 +18,15 @@
*/
package net.nullsum.audinaut.audiofx;
import java.io.Serializable;
import android.content.Context;
import android.media.audiofx.BassBoost;
import android.media.audiofx.Equalizer;
import android.util.Log;
import net.nullsum.audinaut.util.FileUtil;
import java.io.Serializable;
/**
* Backward-compatible wrapper for {@link Equalizer}, which is API Level 9.
*
@ -54,7 +55,7 @@ public class EqualizerController {
equalizer = new Equalizer(0, audioSessionId);
bass = new BassBoost(0, audioSessionId);
loudnessAvailable = true;
loudnessEnhancerController = new LoudnessEnhancerController(context, audioSessionId);
loudnessEnhancerController = new LoudnessEnhancerController(audioSessionId);
}
public void saveSettings() {
@ -80,14 +81,14 @@ public class EqualizerController {
}
}
public boolean isAvailable() {
private boolean isAvailable() {
return equalizer != null && bass != null;
}
public boolean isEnabled() {
try {
return isAvailable() && equalizer.getEnabled();
} catch(Exception e) {
} catch (Exception e) {
return false;
}
}
@ -97,14 +98,14 @@ public class EqualizerController {
released = true;
equalizer.release();
bass.release();
if(loudnessEnhancerController != null && loudnessEnhancerController.isAvailable()) {
if (loudnessEnhancerController != null && loudnessEnhancerController.isAvailable()) {
loudnessEnhancerController.release();
}
}
}
public Equalizer getEqualizer() {
if(released) {
if (released) {
released = false;
try {
init();
@ -116,8 +117,9 @@ public class EqualizerController {
}
return equalizer;
}
public BassBoost getBassBoost() {
if(released) {
if (released) {
released = false;
try {
init();
@ -128,8 +130,9 @@ public class EqualizerController {
}
return bass;
}
public LoudnessEnhancerController getLoudnessEnhancerController() {
if(loudnessAvailable && released) {
if (loudnessAvailable && released) {
released = false;
try {
init();
@ -144,7 +147,6 @@ public class EqualizerController {
private static class EqualizerSettings implements Serializable {
private short[] bandLevels;
private short preset;
private boolean enabled;
private short bass;
private int loudness;
@ -152,26 +154,22 @@ public class EqualizerController {
public EqualizerSettings() {
}
public EqualizerSettings(Equalizer equalizer, BassBoost boost, LoudnessEnhancerController loudnessEnhancerController) {
enabled = equalizer.getEnabled();
bandLevels = new short[equalizer.getNumberOfBands()];
for (short i = 0; i < equalizer.getNumberOfBands(); i++) {
bandLevels[i] = equalizer.getBandLevel(i);
}
try {
preset = equalizer.getCurrentPreset();
} catch (Exception x) {
preset = -1;
}
try {
bass = boost.getRoundedStrength();
} catch(Exception e) {
} catch (Exception e) {
bass = 0;
}
try {
loudness = (int) loudnessEnhancerController.getGain();
} catch(Exception e) {
} catch (Exception e) {
loudness = 0;
}
}
@ -181,11 +179,11 @@ public class EqualizerController {
equalizer.setBandLevel(i, bandLevels[i]);
}
equalizer.setEnabled(enabled);
if(bass != 0) {
if (bass != 0) {
boost.setEnabled(true);
boost.setStrength(bass);
}
if(loudness != 0) {
if (loudness != 0) {
loudnessController.enable();
loudnessController.setGain(loudness);
}

View File

@ -18,22 +18,16 @@
*/
package net.nullsum.audinaut.audiofx;
import android.content.Context;
import android.media.audiofx.LoudnessEnhancer;
import android.util.Log;
public class LoudnessEnhancerController {
private static final String TAG = LoudnessEnhancerController.class.getSimpleName();
private final Context context;
private LoudnessEnhancer enhancer;
private boolean released = false;
private int audioSessionId = 0;
public LoudnessEnhancerController(Context context, int audioSessionId) {
this.context = context;
public LoudnessEnhancerController(int audioSessionId) {
try {
this.audioSessionId = audioSessionId;
enhancer = new LoudnessEnhancer(audioSessionId);
} catch (Throwable x) {
Log.w(TAG, "Failed to create enhancer", x);
@ -47,7 +41,7 @@ public class LoudnessEnhancerController {
public boolean isEnabled() {
try {
return isAvailable() && enhancer.getEnabled();
} catch(Exception e) {
} catch (Exception e) {
return false;
}
}
@ -55,6 +49,7 @@ public class LoudnessEnhancerController {
public void enable() {
enhancer.setEnabled(true);
}
public void disable() {
enhancer.setEnabled(false);
}
@ -62,6 +57,7 @@ public class LoudnessEnhancerController {
public float getGain() {
return enhancer.getTargetGain();
}
public void setGain(int gain) {
enhancer.setTargetGain(gain);
}
@ -69,7 +65,6 @@ public class LoudnessEnhancerController {
public void release() {
if (isAvailable()) {
enhancer.release();
released = true;
}
}

View File

@ -19,26 +19,25 @@
package net.nullsum.audinaut.domain;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.MediaMetadataRetriever;
import android.os.Build;
import android.util.Log;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.io.File;
import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.Locale;
import net.nullsum.audinaut.service.DownloadService;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.UpdateHelper;
import net.nullsum.audinaut.util.Util;
import java.io.File;
import java.io.Serializable;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
/**
* @author Sindre Mehus
*/
@ -51,8 +50,9 @@ public class MusicDirectory implements Serializable {
private List<Entry> children;
public MusicDirectory() {
children = new ArrayList<Entry>();
children = new ArrayList<>();
}
public MusicDirectory(List<Entry> children) {
this.children = children;
}
@ -65,7 +65,7 @@ public class MusicDirectory implements Serializable {
this.name = name;
}
public String getId() {
public String getId() {
return id;
}
@ -82,10 +82,11 @@ public class MusicDirectory implements Serializable {
}
public void addChild(Entry child) {
if(child != null) {
if (child != null) {
children.add(child);
}
}
public void addChildren(List<Entry> children) {
this.children.addAll(children);
}
@ -103,7 +104,7 @@ public class MusicDirectory implements Serializable {
return children;
}
List<Entry> result = new ArrayList<Entry>(children.size());
List<Entry> result = new ArrayList<>(children.size());
for (Entry child : children) {
if (child != null && child.isDirectory() && includeDirs || !child.isDirectory() && includeFiles) {
result.add(child);
@ -111,8 +112,9 @@ public class MusicDirectory implements Serializable {
}
return result;
}
public synchronized List<Entry> getSongs() {
List<Entry> result = new ArrayList<Entry>();
List<Entry> result = new ArrayList<>();
for (Entry child : children) {
if (child != null && !child.isDirectory()) {
result.add(child);
@ -125,24 +127,19 @@ public class MusicDirectory implements Serializable {
return children.size();
}
public void shuffleChildren() {
Collections.shuffle(this.children);
}
public void sortChildren(Context context, int instance) {
public void sortChildren(Context context) {
sortChildren(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_CUSTOM_SORT_ENABLED, true));
}
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();
for (Entry entry : children) {
int index = refreshedDirectory.children.indexOf(entry);
if(index != -1) {
if (index != -1) {
final Entry refreshed = refreshedDirectory.children.get(index);
entry.setTitle(refreshed.getTitle());
@ -155,7 +152,7 @@ public class MusicDirectory implements Serializable {
entry.setTranscodedSuffix(refreshed.getTranscodedSuffix());
entry.setDiscNumber(refreshed.getDiscNumber());
entry.setType(refreshed.getType());
if(!Util.equals(entry.getCoverArt(), refreshed.getCoverArt())) {
if (!Util.equals(entry.getCoverArt(), refreshed.getCoverArt())) {
metadataUpdated = true;
entry.setCoverArt(refreshed.getCoverArt());
}
@ -173,7 +170,7 @@ public class MusicDirectory implements Serializable {
found.setTranscodedSuffix(refreshed.getTranscodedSuffix());
found.setDiscNumber(refreshed.getDiscNumber());
found.setType(refreshed.getType());
if(!Util.equals(found.getCoverArt(), refreshed.getCoverArt())) {
if (!Util.equals(found.getCoverArt(), refreshed.getCoverArt())) {
found.setCoverArt(refreshed.getCoverArt());
metadataUpdate = DownloadService.METADATA_UPDATED_COVER_ART;
}
@ -184,13 +181,14 @@ public class MusicDirectory implements Serializable {
return metadataUpdated;
}
public synchronized boolean updateEntriesList(Context context, int instance, MusicDirectory refreshedDirectory) {
public synchronized boolean updateEntriesList(Context context, MusicDirectory refreshedDirectory) {
boolean changed = false;
Iterator<Entry> it = children.iterator();
while(it.hasNext()) {
while (it.hasNext()) {
Entry entry = it.next();
// No longer exists in here
if(refreshedDirectory.children.indexOf(entry) == -1) {
if (refreshedDirectory.children.indexOf(entry) == -1) {
it.remove();
changed = true;
}
@ -198,16 +196,16 @@ public class MusicDirectory implements Serializable {
// Make sure we contain all children from refreshed set
boolean resort = false;
for(Entry refreshed: refreshedDirectory.children) {
if(!this.children.contains(refreshed)) {
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);
}
return changed;
@ -218,7 +216,6 @@ public class MusicDirectory implements Serializable {
private String id;
private String parent;
private String grandParent;
private String albumId;
private String artistId;
private boolean directory;
@ -233,26 +230,25 @@ public class MusicDirectory implements Serializable {
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(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) {
@ -260,16 +256,16 @@ public class MusicDirectory implements Serializable {
MediaMetadataRetriever metadata = new MediaMetadataRetriever();
metadata.setDataSource(file.getAbsolutePath());
String discNumber = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DISC_NUMBER);
if(discNumber == null) {
if (discNumber == null) {
discNumber = "1/1";
}
int slashIndex = discNumber.indexOf("/");
if(slashIndex > 0) {
if (slashIndex > 0) {
discNumber = discNumber.substring(0, slashIndex);
}
try {
setDiscNumber(Integer.parseInt(discNumber));
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Non numbers in disc field!");
}
String bitrate = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE);
@ -277,22 +273,23 @@ public class MusicDirectory implements Serializable {
String length = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
setDuration(Integer.parseInt(length) / 1000);
String artist = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
if(artist != null) {
if (artist != null) {
setArtist(artist);
}
String album = metadata.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
if(album != null) {
if (album != null) {
setAlbum(album);
}
metadata.release();
} catch(Exception e) {
} catch (Exception e) {
Log.i(TAG, "Device doesn't properly support MediaMetadataRetreiver", e);
}
}
public void rebaseTitleOffPath() {
try {
String filename = getPath();
if(filename == null) {
if (filename == null) {
return;
}
@ -304,13 +301,13 @@ public class MusicDirectory implements Serializable {
}
index = filename.lastIndexOf('.');
if(index != -1) {
if (index != -1) {
filename = filename.substring(0, index);
}
setTitle(filename);
}
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Failed to update title based off of path", e);
}
}
@ -331,14 +328,6 @@ public class MusicDirectory implements Serializable {
this.parent = parent;
}
public String getGrandParent() {
return grandParent;
}
public void setGrandParent(String grandParent) {
this.grandParent = grandParent;
}
public String getAlbumId() {
return albumId;
}
@ -379,18 +368,18 @@ public class MusicDirectory implements Serializable {
return getParent() != null || getArtist() != null;
}
public void setAlbum(String album) {
this.album = album;
}
public String getAlbumDisplay() {
if(album != null && title.startsWith("Disc ")) {
if (album != null && title.startsWith("Disc ")) {
return album;
} else {
return title;
}
}
public void setAlbum(String album) {
this.album = album;
}
public String getArtist() {
return artist;
}
@ -455,14 +444,6 @@ public class MusicDirectory implements Serializable {
this.transcodedSuffix = transcodedSuffix;
}
public Long getSize() {
return size;
}
public void setSize(Long size) {
this.size = size;
}
public Integer getDuration() {
return duration;
}
@ -506,9 +487,11 @@ public class MusicDirectory implements Serializable {
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public boolean isSong() {
return type == TYPE_SONG;
}
@ -521,18 +504,6 @@ public class MusicDirectory implements Serializable {
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");
// Err on the side of default functionality
return true;
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
@ -558,8 +529,8 @@ public class MusicDirectory implements Serializable {
}
public static class EntryComparator implements Comparator<Entry> {
private boolean byYear;
private Collator collator;
private final boolean byYear;
private final Collator collator;
public EntryComparator(boolean byYear) {
this.byYear = byYear;
@ -567,20 +538,28 @@ public class MusicDirectory implements Serializable {
this.collator.setStrength(Collator.PRIMARY);
}
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 int compare(Entry lhs, Entry rhs) {
if(lhs.isDirectory() && !rhs.isDirectory()) {
if (lhs.isDirectory() && !rhs.isDirectory()) {
return -1;
} else if(!lhs.isDirectory() && rhs.isDirectory()) {
} else if (!lhs.isDirectory() && rhs.isDirectory()) {
return 1;
} else if(lhs.isDirectory() && rhs.isDirectory()) {
if(byYear) {
} else if (lhs.isDirectory() && rhs.isDirectory()) {
if (byYear) {
Integer lhsYear = lhs.getYear();
Integer rhsYear = rhs.getYear();
if(lhsYear != null && rhsYear != null) {
if (lhsYear != null && rhsYear != null) {
return lhsYear.compareTo(rhsYear);
} else if(lhsYear != null) {
} else if (lhsYear != null) {
return -1;
} else if(rhsYear != null) {
} else if (rhsYear != null) {
return 1;
}
}
@ -591,36 +570,25 @@ public class MusicDirectory implements Serializable {
Integer lhsDisc = lhs.getDiscNumber();
Integer rhsDisc = rhs.getDiscNumber();
if(lhsDisc != null && rhsDisc != null) {
if(lhsDisc < rhsDisc) {
if (lhsDisc != null && rhsDisc != null) {
if (lhsDisc < rhsDisc) {
return -1;
} else if(lhsDisc > rhsDisc) {
} else if (lhsDisc > rhsDisc) {
return 1;
}
}
Integer lhsTrack = lhs.getTrack();
Integer rhsTrack = rhs.getTrack();
if(lhsTrack != null && rhsTrack != null && lhsTrack != rhsTrack) {
if (lhsTrack != null && rhsTrack != null && !Objects.equals(lhsTrack, rhsTrack)) {
return lhsTrack.compareTo(rhsTrack);
} else if(lhsTrack != null) {
} else if (lhsTrack != null) {
return -1;
} else if(rhsTrack != null) {
} else if (rhsTrack != null) {
return 1;
}
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");
}
}
}
}

View File

@ -35,16 +35,24 @@ public class MusicFolder implements Serializable {
private static final String TAG = MusicFolder.class.getSimpleName();
private String id;
private String name;
private boolean enabled;
public MusicFolder() {
}
public MusicFolder(String id, String name) {
this.id = id;
this.name = name;
}
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 String getId() {
return id;
}
@ -53,28 +61,13 @@ public class MusicFolder implements Serializable {
return name;
}
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())) {
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);
}
}
}

View File

@ -17,14 +17,12 @@ package net.nullsum.audinaut.domain;
import java.io.Serializable;
import java.util.ArrayList;
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 final List<MusicDirectory.Entry> songs = new ArrayList<>();
public final List<MusicDirectory.Entry> toDelete = new ArrayList<>();
public int currentPlayingIndex;
public int currentPlayingPosition;
public boolean renameCurrent = false;
public Date changed = null;
}

View File

@ -45,10 +45,12 @@ public class Playlist implements Serializable {
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) {
this.id = id;
this.name = name;
@ -65,10 +67,6 @@ public class Playlist implements Serializable {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
@ -81,10 +79,6 @@ public class Playlist implements Serializable {
return this.owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getComment() {
return this.comment;
}
@ -104,6 +98,7 @@ public class Playlist implements Serializable {
public Boolean getPublic() {
return this.pub;
}
public void setPublic(Boolean pub) {
this.pub = pub;
}
@ -112,7 +107,7 @@ public class Playlist implements Serializable {
return created;
}
public void setCreated(String created) {
private void setCreated(String created) {
if (created != null) {
try {
this.created = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).parse(created);
@ -123,14 +118,12 @@ public class Playlist implements Serializable {
this.created = null;
}
}
public void setCreated(Date created) {
this.created = created;
}
public Date getChanged() {
return changed;
}
public void setChanged(String changed) {
private void setChanged(String changed) {
if (changed != null) {
try {
this.changed = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).parse(changed);
@ -141,16 +134,10 @@ public class Playlist implements Serializable {
this.changed = null;
}
}
public void setChanged(Date changed) {
this.changed = changed;
}
public Integer getDuration() {
return duration;
}
public void setDuration(Integer duration) {
this.duration = duration;
}
@Override
public String toString() {
@ -159,13 +146,13 @@ public class Playlist implements Serializable {
@Override
public boolean equals(Object o) {
if(o == this) {
if (o == this) {
return true;
} else if(o == null) {
} else if (o == null) {
return false;
} else if(o instanceof String) {
} else if (o instanceof String) {
return o.equals(this.id);
} else if(o.getClass() != getClass()) {
} else if (o.getClass() != getClass()) {
return false;
}
@ -174,14 +161,14 @@ public class Playlist implements Serializable {
}
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;
}
@Override
public int compare(Playlist playlist1, Playlist playlist2) {
return playlist1.getName().compareToIgnoreCase(playlist2.getName());
}
}
}

View File

@ -68,24 +68,24 @@ public class SearchCritera {
// * Replace spaces and wildcard '*' characters with ".*"
// * All other characters are properly quoted
if (this.pattern == null) {
String regex = ".*";
String currentPart = "";
StringBuilder regex = new StringBuilder(".*");
StringBuilder currentPart = new StringBuilder();
for (int i = 0; i < query.length(); i++) {
char c = query.charAt(i);
if (c == '*' || c == ' ') {
regex += Pattern.quote(currentPart);
regex += ".*";
currentPart = "";
regex.append(Pattern.quote(currentPart.toString()));
regex.append(".*");
currentPart = new StringBuilder();
} else {
currentPart += c;
currentPart.append(c);
}
}
if (currentPart.length() > 0) {
regex += Pattern.quote(currentPart);
regex.append(Pattern.quote(currentPart.toString()));
}
regex += ".*";
this.pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
regex.append(".*");
this.pattern = Pattern.compile(regex.toString(), Pattern.CASE_INSENSITIVE);
}
return this.pattern;

View File

@ -50,13 +50,4 @@ 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();
}
}

View File

@ -15,21 +15,19 @@
package net.nullsum.audinaut.domain;
import android.util.Pair;
import java.io.Serializable;
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<>();
private static final String ADMIN = "adminRole";
private static final String SETTINGS = "settingsRole";
private static final String DOWNLOAD = "downloadRole";
private static final String UPLOAD = "uploadRole";
private static final String COVERART = "coverArtRole";
private static final String COMMENT = "commentRole";
private static final String STREAM = "streamRole";
static {
ROLES.add(ADMIN);
@ -41,66 +39,25 @@ public class User implements Serializable {
ROLES.add(COMMENT);
}
private String username;
private String password;
private String email;
private List<Setting> settings = new ArrayList<Setting>();
private final List<Setting> settings = new ArrayList<>();
private List<Setting> musicFolders;
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return 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 void addMusicFolder(MusicFolder musicFolder) {
if(musicFolders == null) {
if (musicFolders == null) {
musicFolders = new ArrayList<>();
}
musicFolders.add(new MusicFolderSetting(musicFolder.getId(), musicFolder.getName(), false));
musicFolders.add(new MusicFolderSetting(musicFolder.getId(), musicFolder.getName()));
}
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;
}
@ -112,6 +69,7 @@ public class User implements Serializable {
public Setting() {
}
public Setting(String name, Boolean value) {
this.name = name;
this.value = value;
@ -120,22 +78,21 @@ public class User implements Serializable {
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;
private final String label;
public MusicFolderSetting() {
}
public MusicFolderSetting(String name, String label, Boolean value) {
super(name, value);
public MusicFolderSetting(String name, String label) {
super(name, false);
this.label = label;
}

View File

@ -15,7 +15,6 @@
package net.nullsum.audinaut.fragments;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.helper.ItemTouchHelper;
@ -26,13 +25,8 @@ import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.adapter.DownloadFileAdapter;
import net.nullsum.audinaut.adapter.SectionAdapter;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.service.DownloadFile;
@ -42,9 +36,14 @@ import net.nullsum.audinaut.util.DownloadFileItemHelperCallback;
import net.nullsum.audinaut.util.ProgressListener;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.adapter.DownloadFileAdapter;
import net.nullsum.audinaut.view.UpdateView;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class DownloadFragment extends SelectRecyclerFragment<DownloadFile> implements SectionAdapter.OnItemClickedListener<DownloadFile> {
private long currentRevision;
private ScheduledExecutorService executorService;
@ -69,17 +68,7 @@ public class DownloadFragment extends SelectRecyclerFragment<DownloadFile> imple
super.onResume();
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
update();
}
});
}
};
Runnable runnable = () -> handler.post(this::update);
executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleWithFixedDelay(runnable, 0L, 1000L, TimeUnit.MILLISECONDS);
@ -104,11 +93,11 @@ public class DownloadFragment extends SelectRecyclerFragment<DownloadFile> imple
@Override
public List<DownloadFile> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
DownloadService downloadService = getDownloadService();
if(downloadService == null) {
return new ArrayList<DownloadFile>();
if (downloadService == null) {
return new ArrayList<>();
}
List<DownloadFile> songList = new ArrayList<DownloadFile>();
List<DownloadFile> songList = new ArrayList<>();
songList.addAll(downloadService.getBackgroundDownloads());
currentRevision = downloadService.getDownloadListUpdateRevision();
return songList;
@ -128,7 +117,7 @@ public class DownloadFragment extends SelectRecyclerFragment<DownloadFile> imple
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)) {
if (!Util.isOffline(context)) {
menu.removeItem(R.id.song_menu_remove_playlist);
}
@ -143,29 +132,24 @@ public class DownloadFragment extends SelectRecyclerFragment<DownloadFile> imple
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if(super.onOptionsItemSelected(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() {
Util.confirmDialog(context, R.string.download_menu_remove_all, "", (dialog, which) -> new SilentBackgroundTask<Void>(context) {
@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();
protected Void doInBackground() throws Throwable {
getDownloadService().clearBackground();
return null;
}
});
@Override
protected void done(Void result) {
update();
}
}.execute());
return true;
}

View File

@ -29,14 +29,10 @@ import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import java.util.HashMap;
import java.util.Map;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.audiofx.EqualizerController;
import net.nullsum.audinaut.audiofx.LoudnessEnhancerController;
@ -44,6 +40,9 @@ import net.nullsum.audinaut.service.DownloadService;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.Util;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Scott on 10/27/13.
*/
@ -52,7 +51,7 @@ public class EqualizerFragment extends SubsonicFragment {
private static final int MENU_GROUP_PRESET = 100;
private final Map<Short, SeekBar> bars = new HashMap<Short, SeekBar>();
private final Map<Short, SeekBar> bars = new HashMap<>();
private SeekBar bassBar;
private SeekBar loudnessBar;
private EqualizerController equalizerController;
@ -73,7 +72,7 @@ public class EqualizerFragment extends SubsonicFragment {
loudnessEnhancer = equalizerController.getLoudnessEnhancerController();
initEqualizer();
} catch(Exception e) {
} catch (Exception e) {
Log.e(TAG, "Failed to initialize EQ", e);
Util.toast(context, "Failed to initialize EQ");
context.onBackPressed();
@ -81,25 +80,17 @@ public class EqualizerFragment extends SubsonicFragment {
final View presetButton = rootView.findViewById(R.id.equalizer_preset);
registerForContextMenu(presetButton);
presetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
presetButton.showContextMenu();
}
});
presetButton.setOnClickListener(view -> presetButton.showContextMenu());
CheckBox enabledCheckBox = (CheckBox) rootView.findViewById(R.id.equalizer_enabled);
CheckBox enabledCheckBox = 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();
}
enabledCheckBox.setOnCheckedChangeListener((compoundButton, 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();
}
});
@ -119,7 +110,7 @@ public class EqualizerFragment extends SubsonicFragment {
if (!equalizer.getEnabled()) {
equalizerController.release();
}
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Failed to release controller", e);
}
}
@ -135,7 +126,7 @@ public class EqualizerFragment extends SubsonicFragment {
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
if(!primaryFragment) {
if (!primaryFragment) {
return;
}
@ -158,7 +149,7 @@ public class EqualizerFragment extends SubsonicFragment {
@Override
public boolean onContextItemSelected(MenuItem menuItem) {
short preset = (short) menuItem.getItemId();
for(int i = 0; i < 10; i++) {
for (int i = 0; i < 10; i++) {
try {
equalizer.usePreset(preset);
i = 10;
@ -178,7 +169,7 @@ public class EqualizerFragment extends SubsonicFragment {
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(Constants.PREFERENCES_EQUALIZER_ON, enabled);
editor.apply();
for(int i = 0; i < 10; i++) {
for (int i = 0; i < 10; i++) {
try {
equalizer.setEnabled(enabled);
updateBars(true);
@ -245,36 +236,36 @@ public class EqualizerFragment extends SubsonicFragment {
editor.putInt(Constants.PREFERENCES_EQUALIZER_SETTINGS, masterLevel);
editor.apply();
}
} catch(Exception e) {
} catch (Exception e) {
Log.e(TAG, "Failed to update bars");
}
}
private void initEqualizer() {
LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.equalizer_layout);
LinearLayout layout = rootView.findViewById(R.id.equalizer_layout);
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);
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;
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);
TextView freqTextView = bandBar.findViewById(R.id.equalizer_frequency);
final TextView levelTextView = bandBar.findViewById(R.id.equalizer_level);
SeekBar bar = bandBar.findViewById(R.id.equalizer_bar);
freqTextView.setText((equalizer.getCenterFreq(band) / 1000) + " Hz");
bars.put(band, bar);
bar.setMax(maxEQLevel - minEQLevel);
short level = equalizer.getBandLevel(band);
if(equalizer.getEnabled()) {
if (equalizer.getEnabled()) {
level = (short) (level - masterLevel);
}
bar.setProgress(level - minEQLevel);
@ -290,7 +281,7 @@ public class EqualizerFragment extends SubsonicFragment {
equalizer.setBandLevel(band, (short) (level + masterLevel));
}
updateLevelText(levelTextView, level);
} catch(Exception e) {
} catch (Exception e) {
Log.e(TAG, "Failed to change equalizer", e);
}
}
@ -306,18 +297,18 @@ public class EqualizerFragment extends SubsonicFragment {
layout.addView(bandBar);
}
LinearLayout specialLayout = (LinearLayout) rootView.findViewById(R.id.special_effects_layout);
LinearLayout specialLayout = 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);
TextView freqTextView = bandBar.findViewById(R.id.equalizer_frequency);
final TextView bassTextView = bandBar.findViewById(R.id.equalizer_level);
bassBar = bandBar.findViewById(R.id.equalizer_bar);
freqTextView.setText(R.string.equalizer_bass_booster);
bassBar.setEnabled(equalizer.getEnabled());
short bassLevel = 0;
if(bass.getEnabled()) {
if (bass.getEnabled()) {
bassLevel = bass.getRoundedStrength();
}
bassTextView.setText(context.getResources().getString(R.string.equalizer_bass_size, bassLevel));
@ -339,7 +330,7 @@ public class EqualizerFragment extends SubsonicFragment {
bass.setEnabled(false);
}
}
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Error on changing bass: ", e);
}
}
@ -356,17 +347,17 @@ public class EqualizerFragment extends SubsonicFragment {
});
specialLayout.addView(bandBar);
if(loudnessEnhancer != null && loudnessEnhancer.isAvailable()) {
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 = bandBar.findViewById(R.id.equalizer_frequency);
final TextView loudnessTextView = bandBar.findViewById(R.id.equalizer_level);
loudnessBar = bandBar.findViewById(R.id.equalizer_bar);
freqTextView.setText(R.string.equalizer_voice_booster);
loudnessBar.setEnabled(equalizer.getEnabled());
int loudnessLevel = 0;
if(loudnessEnhancer.isEnabled()) {
if (loudnessEnhancer.isEnabled()) {
loudnessLevel = (int) loudnessEnhancer.getGain();
}
loudnessBar.setProgress(loudnessLevel / 100);
@ -377,18 +368,18 @@ public class EqualizerFragment extends SubsonicFragment {
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()) {
if (fromUser) {
if (progress > 0) {
if (!loudnessEnhancer.isEnabled()) {
loudnessEnhancer.enable();
}
loudnessEnhancer.setGain(progress * 100);
} else if(progress == 0 && loudnessEnhancer.isEnabled()) {
} else if (progress == 0 && loudnessEnhancer.isEnabled()) {
loudnessEnhancer.setGain(progress * 100);
loudnessEnhancer.disable();
}
}
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Error on changing loudness: ", e);
}
}
@ -409,13 +400,13 @@ public class EqualizerFragment extends SubsonicFragment {
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);
TextView freqTextView = bandBar.findViewById(R.id.equalizer_frequency);
final TextView levelTextView = bandBar.findViewById(R.id.equalizer_level);
SeekBar bar = bandBar.findViewById(R.id.equalizer_bar);
freqTextView.setText("Master");
bars.put((short)-1, bar);
bars.put((short) -1, bar);
bar.setMax(maxEQLevel - minEQLevel);
bar.setProgress(masterLevel - minEQLevel);
bar.setEnabled(equalizer.getEnabled());
@ -437,7 +428,7 @@ public class EqualizerFragment extends SubsonicFragment {
}
}
updateLevelText(levelTextView, masterLevel);
} catch(Exception e) {
} catch (Exception e) {
Log.e(TAG, "Failed to change equalizer", e);
}
}

View File

@ -1,15 +1,7 @@
package net.nullsum.audinaut.fragments;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StatFs;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@ -17,32 +9,17 @@ import android.view.MenuItem;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.adapter.MainAdapter;
import net.nullsum.audinaut.adapter.SectionAdapter;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.EnvironmentVariables;
import net.nullsum.audinaut.util.FileUtil;
import net.nullsum.audinaut.util.LoadingTask;
import net.nullsum.audinaut.util.ProgressListener;
import net.nullsum.audinaut.util.UserUtil;
import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.service.MusicService;
import net.nullsum.audinaut.service.MusicServiceFactory;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.ProgressListener;
import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.view.UpdateView;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
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";
@ -63,15 +40,6 @@ public class MainFragment extends SelectRecyclerFragment<Integer> {
onFinishSetupOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(super.onOptionsItemSelected(item)) {
return true;
}
return false;
}
@Override
public int getOptionsMenu() {
return 0;
@ -98,7 +66,7 @@ public class MainFragment extends SelectRecyclerFragment<Integer> {
@Override
public List<Integer> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
return Arrays.asList(0);
return Collections.singletonList(0);
}
@Override
@ -107,15 +75,15 @@ public class MainFragment extends SelectRecyclerFragment<Integer> {
}
private void showAlbumList(String type) {
if("genres".equals(type)) {
if ("genres".equals(type)) {
SubsonicFragment fragment = new SelectGenreFragment();
replaceFragment(fragment);
} else if("years".equals(type)) {
} else if ("years".equals(type)) {
SubsonicFragment fragment = new SelectYearFragment();
replaceFragment(fragment);
} else {
// Clear out recently added count when viewing
if("newest".equals(type)) {
if ("newest".equals(type)) {
SharedPreferences.Editor editor = Util.getPreferences(context).edit();
editor.putInt(Constants.PREFERENCES_KEY_RECENT_COUNT + Util.getActiveServer(context), 0);
editor.apply();
@ -132,59 +100,6 @@ public class MainFragment extends SelectRecyclerFragment<Integer> {
}
}
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();
used = FileUtil.getUsedSize(context, rootFolder);
return null;
}
@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_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("");
}
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_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)));
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) {
@ -193,11 +108,11 @@ public class MainFragment extends SelectRecyclerFragment<Integer> {
showAlbumList("recent");
} else if (item == R.string.main_albums_frequent) {
showAlbumList("frequent");
} else if(item == R.string.main_albums_genres) {
} else if (item == R.string.main_albums_genres) {
showAlbumList("genres");
} else if(item == R.string.main_albums_year) {
} else if (item == R.string.main_albums_year) {
showAlbumList("years");
} else if(item == R.string.main_albums_alphabetical) {
} else if (item == R.string.main_albums_alphabetical) {
showAlbumList("alphabeticalByName");
} else if (item == R.string.main_songs_newest) {
showAlbumList(SONGS_NEWEST);
@ -211,7 +126,8 @@ public class MainFragment extends SelectRecyclerFragment<Integer> {
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Integer> updateView, Integer item) {}
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Integer> updateView, Integer item) {
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Integer> updateView, Integer item) {

View File

@ -14,25 +14,13 @@
*/
package net.nullsum.audinaut.fragments;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import android.support.v7.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
@ -42,19 +30,18 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ViewFlipper;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.activity.SubsonicFragmentActivity;
import net.nullsum.audinaut.adapter.DownloadFileAdapter;
import net.nullsum.audinaut.adapter.SectionAdapter;
import net.nullsum.audinaut.audiofx.EqualizerController;
import net.nullsum.audinaut.domain.PlayerState;
@ -62,26 +49,33 @@ import net.nullsum.audinaut.domain.RepeatMode;
import net.nullsum.audinaut.service.DownloadFile;
import net.nullsum.audinaut.service.DownloadService;
import net.nullsum.audinaut.service.DownloadService.OnSongChangedListener;
import net.nullsum.audinaut.service.MusicService;
import net.nullsum.audinaut.service.MusicServiceFactory;
import net.nullsum.audinaut.service.OfflineException;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.DownloadFileItemHelperCallback;
import net.nullsum.audinaut.util.DrawableTint;
import net.nullsum.audinaut.util.FileUtil;
import net.nullsum.audinaut.util.MenuUtil;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.adapter.DownloadFileAdapter;
import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.view.AutoRepeatButton;
import net.nullsum.audinaut.view.FadeOutAnimation;
import net.nullsum.audinaut.view.FastScroller;
import net.nullsum.audinaut.view.UpdateView;
import net.nullsum.audinaut.util.Util;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import static net.nullsum.audinaut.domain.MusicDirectory.Entry;
import static net.nullsum.audinaut.domain.PlayerState.*;
import net.nullsum.audinaut.util.*;
import net.nullsum.audinaut.view.AutoRepeatButton;
import java.util.ArrayList;
import java.util.concurrent.ScheduledFuture;
import static net.nullsum.audinaut.domain.PlayerState.COMPLETED;
import static net.nullsum.audinaut.domain.PlayerState.IDLE;
import static net.nullsum.audinaut.domain.PlayerState.PAUSED;
import static net.nullsum.audinaut.domain.PlayerState.STOPPED;
public class NowPlayingFragment extends SubsonicFragment implements OnGestureListener, SectionAdapter.OnItemClickedListener<DownloadFile>, OnSongChangedListener {
private static final String TAG = NowPlayingFragment.class.getSimpleName();
private static final int PERCENTAGE_OF_SCREEN_FOR_SWIPE = 10;
private static final int ACTION_PREVIOUS = 1;
@ -128,8 +122,8 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null) {
if(savedInstanceState.getInt(Constants.FRAGMENT_DOWNLOAD_FLIPPER) == 1) {
if (savedInstanceState != null) {
if (savedInstanceState.getInt(Constants.FRAGMENT_DOWNLOAD_FLIPPER) == 1) {
startFlipped = true;
}
}
@ -151,198 +145,136 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
Display d = w.getDefaultDisplay();
swipeDistance = (d.getWidth() + d.getHeight()) * PERCENTAGE_OF_SCREEN_FOR_SWIPE / 100;
swipeVelocity = (d.getWidth() + d.getHeight()) * PERCENTAGE_OF_SCREEN_FOR_SWIPE / 100;
gestureScanner = new GestureDetector(this);
gestureScanner = new GestureDetector(context, this);
playlistFlipper = (ViewFlipper)rootView.findViewById(R.id.download_playlist_flipper);
emptyTextView = (TextView)rootView.findViewById(R.id.download_empty);
songTitleTextView = (TextView)rootView.findViewById(R.id.download_song_title);
albumArtImageView = (ImageView)rootView.findViewById(R.id.download_album_art_image);
positionTextView = (TextView)rootView.findViewById(R.id.download_position);
durationTextView = (TextView)rootView.findViewById(R.id.download_duration);
statusTextView = (TextView)rootView.findViewById(R.id.download_status);
progressBar = (SeekBar)rootView.findViewById(R.id.download_progress_bar);
previousButton = (AutoRepeatButton)rootView.findViewById(R.id.download_previous);
nextButton = (AutoRepeatButton)rootView.findViewById(R.id.download_next);
rewindButton = (AutoRepeatButton) rootView.findViewById(R.id.download_rewind);
fastforwardButton = (AutoRepeatButton) rootView.findViewById(R.id.download_fastforward);
pauseButton =rootView.findViewById(R.id.download_pause);
stopButton =rootView.findViewById(R.id.download_stop);
startButton =rootView.findViewById(R.id.download_start);
repeatButton = (ImageButton)rootView.findViewById(R.id.download_repeat);
toggleListButton =rootView.findViewById(R.id.download_toggle_list);
playlistFlipper = rootView.findViewById(R.id.download_playlist_flipper);
emptyTextView = rootView.findViewById(R.id.download_empty);
songTitleTextView = rootView.findViewById(R.id.download_song_title);
albumArtImageView = rootView.findViewById(R.id.download_album_art_image);
positionTextView = rootView.findViewById(R.id.download_position);
durationTextView = rootView.findViewById(R.id.download_duration);
statusTextView = rootView.findViewById(R.id.download_status);
progressBar = rootView.findViewById(R.id.download_progress_bar);
previousButton = rootView.findViewById(R.id.download_previous);
nextButton = rootView.findViewById(R.id.download_next);
rewindButton = rootView.findViewById(R.id.download_rewind);
fastforwardButton = rootView.findViewById(R.id.download_fastforward);
pauseButton = rootView.findViewById(R.id.download_pause);
stopButton = rootView.findViewById(R.id.download_stop);
startButton = rootView.findViewById(R.id.download_start);
repeatButton = rootView.findViewById(R.id.download_repeat);
toggleListButton = rootView.findViewById(R.id.download_toggle_list);
playlistView = (RecyclerView)rootView.findViewById(R.id.download_list);
FastScroller fastScroller = (FastScroller) rootView.findViewById(R.id.download_fast_scroller);
playlistView = rootView.findViewById(R.id.download_list);
FastScroller fastScroller = rootView.findViewById(R.id.download_fast_scroller);
fastScroller.attachRecyclerView(playlistView);
setupLayoutManager(playlistView, false);
ItemTouchHelper touchHelper = new ItemTouchHelper(new DownloadFileItemHelperCallback(this, true));
touchHelper.attachToRecyclerView(playlistView);
View.OnTouchListener touchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent me) {
return gestureScanner.onTouchEvent(me);
}
};
View.OnTouchListener touchListener = (v, me) -> gestureScanner.onTouchEvent(me);
pauseButton.setOnTouchListener(touchListener);
stopButton.setOnTouchListener(touchListener);
startButton.setOnTouchListener(touchListener);
emptyTextView.setOnTouchListener(touchListener);
albumArtImageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
lastY = (int) me.getRawY();
albumArtImageView.setOnTouchListener((v, me) -> {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
lastY = (int) me.getRawY();
}
return gestureScanner.onTouchEvent(me);
});
previousButton.setOnClickListener(view -> {
warnIfStorageUnavailable();
new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
getDownloadService().previous();
return null;
}
return gestureScanner.onTouchEvent(me);
}
}.execute();
setControlsVisible(true);
});
previousButton.setOnRepeatListener(() -> changeProgress(true));
previousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
warnIfStorageUnavailable();
new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
getDownloadService().previous();
return null;
}
}.execute();
setControlsVisible(true);
}
});
previousButton.setOnRepeatListener(new Runnable() {
public void run() {
changeProgress(true);
}
});
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
warnIfStorageUnavailable();
new SilentBackgroundTask<Boolean>(context) {
@Override
protected Boolean doInBackground() throws Throwable {
getDownloadService().next();
return true;
}
}.execute();
setControlsVisible(true);
}
});
nextButton.setOnRepeatListener(new Runnable() {
public void run() {
changeProgress(false);
}
});
rewindButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
changeProgress(true);
}
});
rewindButton.setOnRepeatListener(new Runnable() {
public void run() {
changeProgress(true);
}
});
fastforwardButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
changeProgress(false);
}
});
fastforwardButton.setOnRepeatListener(new Runnable() {
public void run() {
changeProgress(false);
}
});
pauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
getDownloadService().pause();
return null;
}
}.execute();
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
getDownloadService().reset();
return null;
}
}.execute();
}
});
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
warnIfStorageUnavailable();
new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
start();
return null;
}
}.execute();
}
});
repeatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RepeatMode repeatMode = getDownloadService().getRepeatMode().next();
getDownloadService().setRepeatMode(repeatMode);
switch (repeatMode) {
case OFF:
Util.toast(context, R.string.download_repeat_off);
break;
case ALL:
Util.toast(context, R.string.download_repeat_all);
break;
case SINGLE:
Util.toast(context, R.string.download_repeat_single);
break;
default:
break;
nextButton.setOnClickListener(view -> {
warnIfStorageUnavailable();
new SilentBackgroundTask<Boolean>(context) {
@Override
protected Boolean doInBackground() throws Throwable {
getDownloadService().next();
return true;
}
updateRepeatButton();
setControlsVisible(true);
}.execute();
setControlsVisible(true);
});
nextButton.setOnRepeatListener(() -> changeProgress(false));
rewindButton.setOnClickListener(view -> changeProgress(true));
rewindButton.setOnRepeatListener(() -> changeProgress(true));
fastforwardButton.setOnClickListener(view -> changeProgress(false));
fastforwardButton.setOnRepeatListener(() -> changeProgress(false));
pauseButton.setOnClickListener(view -> new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
getDownloadService().pause();
return null;
}
}.execute());
stopButton.setOnClickListener(view -> new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
getDownloadService().reset();
return null;
}
}.execute());
startButton.setOnClickListener(view -> {
warnIfStorageUnavailable();
new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
start();
return null;
}
}.execute();
});
toggleListButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toggleFullscreenAlbumArt();
setControlsVisible(true);
repeatButton.setOnClickListener(view -> {
RepeatMode repeatMode = getDownloadService().getRepeatMode().next();
getDownloadService().setRepeatMode(repeatMode);
switch (repeatMode) {
case OFF:
Util.toast(context, R.string.download_repeat_off);
break;
case ALL:
Util.toast(context, R.string.download_repeat_all);
break;
case SINGLE:
Util.toast(context, R.string.download_repeat_single);
break;
default:
break;
}
updateRepeatButton();
setControlsVisible(true);
});
toggleListButton.setOnClickListener(view -> {
toggleFullscreenAlbumArt();
setControlsVisible(true);
});
View overlay = rootView.findViewById(R.id.download_overlay_buttons);
final int overlayHeight = overlay != null ? overlay.getHeight() : -1;
albumArtImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (overlayHeight == -1 || lastY < (view.getBottom() - overlayHeight)) {
toggleFullscreenAlbumArt();
setControlsVisible(true);
}
albumArtImageView.setOnClickListener(view -> {
if (overlayHeight == -1 || lastY < (view.getBottom() - overlayHeight)) {
toggleFullscreenAlbumArt();
setControlsVisible(true);
}
});
@ -383,20 +315,20 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
DownloadService downloadService = getDownloadService();
if(Util.isOffline(context)) {
if (Util.isOffline(context)) {
menuInflater.inflate(R.menu.nowplaying_offline, menu);
} else {
menuInflater.inflate(R.menu.nowplaying, menu);
}
if(downloadService != null && downloadService.isRemovePlayed()) {
if (downloadService != null && downloadService.isRemovePlayed()) {
menu.findItem(R.id.menu_remove_played).setChecked(true);
}
if(downloadService != null) {
if (downloadService != null) {
SharedPreferences prefs = Util.getPreferences(context);
boolean equalizerOn = prefs.getBoolean(Constants.PREFERENCES_EQUALIZER_ON, false);
if (equalizerOn && downloadService != null) {
if(downloadService.getEqualizerController() != null && downloadService.getEqualizerController().isEnabled()) {
if (downloadService.getEqualizerController() != null && downloadService.getEqualizerController().isEnabled()) {
menu.findItem(R.id.menu_equalizer).setChecked(true);
}
}
@ -404,23 +336,20 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
menu.removeItem(R.id.menu_equalizer);
}
if(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_BATCH_MODE, false)) {
if (Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_BATCH_MODE, false)) {
menu.findItem(R.id.menu_batch_mode).setChecked(true);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if(menuItemSelected(menuItem.getItemId(), null)) {
return true;
}
return menuItemSelected(menuItem.getItemId(), null) || super.onOptionsItemSelected(menuItem);
return super.onOptionsItemSelected(menuItem);
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<DownloadFile> updateView, DownloadFile downloadFile) {
if(Util.isOffline(context)) {
if (Util.isOffline(context)) {
menuInflater.inflate(R.menu.nowplaying_context_offline, menu);
} else {
menuInflater.inflate(R.menu.nowplaying_context, menu);
@ -436,39 +365,25 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<DownloadFile> updateView, DownloadFile downloadFile) {
if(onContextItemSelected(menuItem, downloadFile.getSong())) {
return true;
}
return onContextItemSelected(menuItem, downloadFile.getSong()) || menuItemSelected(menuItem.getItemId(), downloadFile);
return menuItemSelected(menuItem.getItemId(), downloadFile);
}
private boolean menuItemSelected(int menuItemId, final DownloadFile song) {
List<Entry> songs;
switch (menuItemId) {
case R.id.menu_show_album: case R.id.menu_show_artist:
case R.id.menu_show_album:
case R.id.menu_show_artist:
Entry entry = song.getSong();
Intent intent = new Intent(context, SubsonicFragmentActivity.class);
intent.putExtra(Constants.INTENT_EXTRA_VIEW_ALBUM, true);
String albumId;
String albumName;
if(menuItemId == R.id.menu_show_album) {
if(Util.isTagBrowsing(context)) {
albumId = entry.getAlbumId();
} else {
albumId = entry.getParent();
}
if (menuItemId == R.id.menu_show_album) {
albumId = entry.getAlbumId();
albumName = entry.getAlbum();
} else {
if(Util.isTagBrowsing(context)) {
albumId = entry.getArtistId();
} else {
albumId = entry.getGrandParent();
if(albumId == null) {
intent.putExtra(Constants.INTENT_EXTRA_NAME_CHILD_ID, entry.getParent());
}
}
albumId = entry.getArtistId();
albumName = entry.getArtist();
intent.putExtra(Constants.INTENT_EXTRA_NAME_ARTIST, true);
}
@ -476,23 +391,23 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
intent.putExtra(Constants.INTENT_EXTRA_NAME_NAME, albumName);
intent.putExtra(Constants.INTENT_EXTRA_FRAGMENT_TYPE, "Artist");
if(Util.isOffline(context)) {
if (Util.isOffline(context)) {
try {
// This should only be successful if this is a online song in offline mode
Integer.parseInt(entry.getParent());
String root = FileUtil.getMusicDirectory(context).getPath();
String id = root + "/" + entry.getPath();
id = id.substring(0, id.lastIndexOf("/"));
if(menuItemId == R.id.menu_show_album) {
if (menuItemId == R.id.menu_show_album) {
intent.putExtra(Constants.INTENT_EXTRA_NAME_ID, id);
}
id = id.substring(0, id.lastIndexOf("/"));
if(menuItemId != R.id.menu_show_album) {
if (menuItemId != R.id.menu_show_album) {
intent.putExtra(Constants.INTENT_EXTRA_NAME_ID, id);
intent.putExtra(Constants.INTENT_EXTRA_NAME_NAME, entry.getArtist());
intent.removeExtra(Constants.INTENT_EXTRA_NAME_CHILD_ID);
}
} catch(Exception e) {
} catch (Exception e) {
// Do nothing, entry.getParent() is fine
}
}
@ -501,24 +416,19 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
Util.startActivityWithoutTransition(context, intent);
return true;
case R.id.menu_remove_all:
Util.confirmDialog(context, R.string.download_menu_remove_all, "", new DialogInterface.OnClickListener() {
Util.confirmDialog(context, R.string.download_menu_remove_all, "", (dialog, which) -> new SilentBackgroundTask<Void>(context) {
@Override
public void onClick(DialogInterface dialog, int which) {
new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
getDownloadService().setShufflePlayEnabled(false);
getDownloadService().clear();
return null;
}
@Override
protected void done(Void result) {
context.closeNowPlaying();
}
}.execute();
protected Void doInBackground() throws Throwable {
getDownloadService().setShufflePlayEnabled(false);
getDownloadService().clear();
return null;
}
});
@Override
protected void done(Void result) {
context.closeNowPlaying();
}
}.execute());
return true;
case R.id.menu_remove_played:
if (getDownloadService().isRemovePlayed()) {
@ -543,7 +453,7 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
}.execute();
return true;
case R.id.menu_save_playlist:
List<Entry> entries = new LinkedList<Entry>();
List<Entry> entries = new LinkedList<>();
for (DownloadFile downloadFile : getDownloadService().getSongs()) {
entries.add(downloadFile.getSong());
}
@ -556,7 +466,7 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
DownloadService downloadService = getDownloadService();
if (downloadService != null) {
EqualizerController controller = downloadService.getEqualizerController();
if(controller != null) {
if (controller != null) {
SubsonicFragment fragment = new EqualizerFragment();
replaceFragment(fragment);
setControlsVisible(true);
@ -568,8 +478,9 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
// Any failed condition will get here
Util.toast(context, "Failed to start equalizer. Try restarting.");
return true;
}case R.id.menu_batch_mode:
if(Util.isBatchMode(context)) {
}
case R.id.menu_batch_mode:
if (Util.isBatchMode(context)) {
Util.setBatchMode(context, false);
songListAdapter.notifyDataSetChanged();
} else {
@ -587,12 +498,13 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
@Override
public void onResume() {
super.onResume();
if(this.primaryFragment) {
if (this.primaryFragment) {
onResumeHandlers();
} else {
update();
}
}
private void onResumeHandlers() {
executorService = Executors.newSingleThreadScheduledExecutor();
setControlsVisible(true);
@ -603,22 +515,17 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
startFlipped = false;
}
updateButtons();
if(currentPlaying == null && downloadService != null && currentPlaying == downloadService.getCurrentPlaying()) {
getImageLoader().loadImage(albumArtImageView, (Entry) null, true, false);
if (currentPlaying == null && downloadService != null && currentPlaying == downloadService.getCurrentPlaying()) {
getImageLoader().loadImage(albumArtImageView, null, true, false);
}
context.runWhenServiceAvailable(new Runnable() {
@Override
public void run() {
if (primaryFragment) {
DownloadService downloadService = getDownloadService();
downloadService.addOnSongChangedListener(NowPlayingFragment.this, true);
}
updateRepeatButton();
updateTitle();
context.runWhenServiceAvailable(() -> {
if (primaryFragment) {
DownloadService downloadService1 = getDownloadService();
downloadService1.addOnSongChangedListener(NowPlayingFragment.this);
}
updateRepeatButton();
updateTitle();
});
}
@ -627,8 +534,9 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
super.onPause();
onPauseHandlers();
}
private void onPauseHandlers() {
if(executorService != null) {
if (executorService != null) {
DownloadService downloadService = getDownloadService();
if (downloadService != null) {
downloadService.removeOnSongChangeListener(this);
@ -640,8 +548,8 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
@Override
public void setPrimaryFragment(boolean primary) {
super.setPrimaryFragment(primary);
if(rootView != null) {
if(primary) {
if (rootView != null) {
if (primary) {
onResumeHandlers();
} else {
onPauseHandlers();
@ -650,16 +558,17 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
}
@Override
public void setTitle(int title) {
void setTitle(int title) {
this.title = context.getResources().getString(title);
if(this.primaryFragment) {
if (this.primaryFragment) {
context.setTitle(this.title);
}
}
@Override
public void setSubtitle(CharSequence title) {
void setSubtitle(CharSequence title) {
this.subtitle = title;
if(this.primaryFragment) {
if (this.primaryFragment) {
context.setSubtitle(title);
}
}
@ -675,22 +584,11 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
}
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
setControlsVisible(false);
}
});
}
};
Runnable runnable = () -> handler.post(() -> setControlsVisible(false));
hideControlsFuture = executorService.schedule(runnable, 3000L, TimeUnit.MILLISECONDS);
}
private void setControlsVisible(boolean visible) {
DownloadService downloadService = getDownloadService();
try {
long duration = 1700L;
FadeOutAnimation.createAndStart(rootView.findViewById(R.id.download_overlay_buttons), !visible, duration);
@ -698,17 +596,11 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
if (visible) {
scheduleHideControls();
}
} catch(Exception e) {
} catch (Exception ignored) {
}
}
private void updateButtons() {
if(context == null) {
return;
}
}
// Scroll to current playing/downloading.
private void scrollToCurrent() {
if (getDownloadService() == null || songListAdapter == null) {
@ -718,13 +610,13 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
// Try to get position of current playing/downloading
int position = songListAdapter.getItemPosition(currentPlaying);
if(position == -1) {
if (position == -1) {
DownloadFile currentDownloading = getDownloadService().getCurrentDownloading();
position = songListAdapter.getItemPosition(currentDownloading);
}
// If found, scroll to it
if(position != -1) {
if (position != -1) {
// RecyclerView.scrollToPosition just puts it on the screen (ie: bottom if scrolled below it)
LinearLayoutManager layoutManager = (LinearLayoutManager) playlistView.getLayoutManager();
layoutManager.scrollToPositionWithOffset(position, 0);
@ -732,26 +624,12 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
}
private void update() {
if(startFlipped) {
if (startFlipped) {
startFlipped = false;
scrollToCurrent();
}
}
private int getMinutes(int progress) {
if(progress < 30) {
return progress + 1;
} else if(progress < 49) {
return (progress - 30) * 5 + getMinutes(29);
} else if(progress < 57) {
return (progress - 48) * 30 + getMinutes(48);
} else if(progress < 81) {
return (progress - 56) * 60 + getMinutes(56);
} else {
return (progress - 80) * 150 + getMinutes(80);
}
}
private void toggleFullscreenAlbumArt() {
if (playlistFlipper.getDisplayedChild() == 1) {
playlistFlipper.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.push_down_in));
@ -786,7 +664,7 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
private void changeProgress(final boolean rewind) {
final DownloadService downloadService = getDownloadService();
if(downloadService == null) {
if (downloadService == null) {
return;
}
@ -795,7 +673,7 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
@Override
protected Void doInBackground() throws Throwable {
if(rewind) {
if (rewind) {
seekTo = downloadService.rewind();
} else {
seekTo = downloadService.fastForward();
@ -841,13 +719,13 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
action = ACTION_REWIND;
}
if(action > 0) {
if (action > 0) {
final int performAction = action;
warnIfStorageUnavailable();
new SilentBackgroundTask<Void>(context) {
@Override
protected Void doInBackground() throws Throwable {
switch(performAction) {
switch (performAction) {
case ACTION_NEXT:
downloadService.next();
break;
@ -929,14 +807,14 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
getImageLoader().loadImage(albumArtImageView, song, true, true);
DownloadService downloadService = getDownloadService();
if(downloadService.isShufflePlayEnabled()) {
if (downloadService.isShufflePlayEnabled()) {
setSubtitle(context.getResources().getString(R.string.download_playerstate_playing_shuffle));
} else {
setSubtitle(context.getResources().getString(R.string.download_playing_out_of, currentPlayingIndex + 1, currentPlayingSize));
}
} else {
songTitleTextView.setText(null);
getImageLoader().loadImage(albumArtImageView, (Entry) null, true, false);
getImageLoader().loadImage(albumArtImageView, null, true, false);
setSubtitle(null);
}
}
@ -946,14 +824,13 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
currentPlayingSize = songs.size();
DownloadService downloadService = getDownloadService();
if(downloadService.isShufflePlayEnabled()) {
if (downloadService.isShufflePlayEnabled()) {
emptyTextView.setText(R.string.download_shuffle_loading);
}
else {
} else {
emptyTextView.setText(R.string.download_empty);
}
if(songListAdapter == null) {
if (songListAdapter == null) {
songList = new ArrayList<>();
songList.addAll(songs);
playlistView.setAdapter(songListAdapter = new DownloadFileAdapter(context, songList, NowPlayingFragment.this));
@ -965,12 +842,12 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
emptyTextView.setVisibility(songs.isEmpty() ? View.VISIBLE : View.GONE);
if(scrollWhenLoaded) {
if (scrollWhenLoaded) {
scrollToCurrent();
scrollWhenLoaded = false;
}
if(this.currentPlaying != currentPlaying) {
if (this.currentPlaying != currentPlaying) {
onSongChanged(currentPlaying, currentPlayingIndex);
onMetadataUpdate(currentPlaying != null ? currentPlaying.getSong() : null, DownloadService.METADATA_UPDATED_ALL);
} else {
@ -987,13 +864,13 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
int millisTotal = duration == null ? 0 : duration;
positionTextView.setText(Util.formatDuration(millisPlayed / 1000));
if(millisTotal > 0) {
if (millisTotal > 0) {
durationTextView.setText(Util.formatDuration(millisTotal / 1000));
} else {
durationTextView.setText("-:--");
}
progressBar.setMax(millisTotal == 0 ? 100 : millisTotal); // Work-around for apparent bug.
if(!seekInProgress) {
if (!seekInProgress) {
progressBar.setProgress(millisPlayed);
}
progressBar.setEnabled(isSeekable);
@ -1006,11 +883,11 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
}
@Override
public void onStateUpdate(DownloadFile downloadFile, PlayerState playerState) {
public void onStateUpdate(PlayerState playerState) {
switch (playerState) {
case DOWNLOADING:
if(currentPlaying != null) {
if(Util.isWifiRequiredForDownload(context)) {
if (currentPlaying != null) {
if (Util.isWifiRequiredForDownload(context)) {
statusTextView.setText(context.getResources().getString(R.string.download_playerstate_mobile_disabled));
} else {
long bytes = currentPlaying.getPartialFile().length();
@ -1022,9 +899,9 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
statusTextView.setText(R.string.download_playerstate_buffering);
break;
default:
if(currentPlaying != null) {
if (currentPlaying != null) {
Entry entry = currentPlaying.getSong();
if(entry.getAlbum() != null) {
if (entry.getAlbum() != null) {
String artist = "";
if (entry.getArtist() != null) {
artist = currentPlaying.getSong().getArtist().trim() + " - ";
@ -1061,12 +938,12 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
@Override
public void onMetadataUpdate(Entry song, int fieldChange) {
if(song != null && albumArtImageView != null && fieldChange == DownloadService.METADATA_UPDATED_COVER_ART) {
if (song != null && albumArtImageView != null && fieldChange == DownloadService.METADATA_UPDATED_COVER_ART) {
getImageLoader().loadImage(albumArtImageView, song, true, true);
}
}
public void updateRepeatButton() {
private void updateRepeatButton() {
DownloadService downloadService = getDownloadService();
switch (downloadService.getRepeatMode()) {
case OFF:
@ -1082,9 +959,8 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
break;
}
}
private void updateTitle() {
DownloadService downloadService = getDownloadService();
private void updateTitle() {
String title = context.getResources().getString(R.string.button_bar_now_playing);
setTitle(title);
@ -1095,8 +971,8 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
List<DownloadFile> selected = getCurrentAdapter().getSelected();
List<Entry> entries = new ArrayList<>();
for(DownloadFile downloadFile: selected) {
if(downloadFile.getSong() != null) {
for (DownloadFile downloadFile : selected) {
if (downloadFile.getSong() != null) {
entries.add(downloadFile.getSong());
}
}

View File

@ -26,32 +26,33 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.util.Constants;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
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;
final private Runnable mRequestFocus = new Runnable() {
public void run() {
mList.focusableViewAvailable(mList);
}
};
private PreferenceManager mPreferenceManager;
private Handler mHandler = new Handler() {
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
@ -63,12 +64,6 @@ public abstract class PreferenceCompatFragment extends SubsonicFragment {
}
};
final private Runnable mRequestFocus = new Runnable() {
public void run() {
mList.focusableViewAvailable(mList);
}
};
private void bindPreferences() {
PreferenceScreen localPreferenceScreen = getPreferenceScreen();
if (localPreferenceScreen != null) {
@ -89,7 +84,7 @@ public abstract class PreferenceCompatFragment extends SubsonicFragment {
throw new RuntimeException("Content has view with id attribute 'android.R.id.list' that is not a ListView class");
}
mList = (ListView)listView;
mList = (ListView) listView;
if (mList == null) {
throw new RuntimeException("Your content must have a ListView whose id attribute is 'android.R.id.list'");
}
@ -110,26 +105,17 @@ public abstract class PreferenceCompatFragment extends SubsonicFragment {
}
}
public void addPreferencesFromIntent(Intent intent) {
requirePreferenceManager();
PreferenceScreen screen = inflateFromIntent(intent, getPreferenceScreen());
setPreferenceScreen(screen);
}
public PreferenceScreen addPreferencesFromResource(int resId) {
private PreferenceScreen addPreferencesFromResource(int resId) {
requirePreferenceManager();
PreferenceScreen screen = inflateFromResource(getActivity(), resId, getPreferenceScreen());
setPreferenceScreen(screen);
for(int i = 0; i < screen.getPreferenceCount(); i++) {
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;
}
if (preference instanceof PreferenceScreen && preference.getKey() != null) {
preference.setOnPreferenceClickListener(preference1 -> {
onStartNewFragment(preference1.getKey());
return false;
});
}
}
@ -137,19 +123,19 @@ public abstract class PreferenceCompatFragment extends SubsonicFragment {
return screen;
}
public Preference findPreference(CharSequence key) {
Preference findPreference(CharSequence key) {
if (mPreferenceManager == null) {
return null;
}
return mPreferenceManager.findPreference(key);
}
public ListView getListView() {
private ListView getListView() {
ensureList();
return mList;
}
public PreferenceManager getPreferenceManager() {
PreferenceManager getPreferenceManager() {
return mPreferenceManager;
}
@ -184,7 +170,7 @@ public abstract class PreferenceCompatFragment extends SubsonicFragment {
mPreferenceManager = createPreferenceManager();
int res = this.getArguments().getInt(Constants.INTENT_EXTRA_FRAGMENT_TYPE, 0);
if(res != 0) {
if (res != 0) {
PreferenceScreen preferenceScreen = addPreferencesFromResource(res);
onInitPreferences(preferenceScreen);
}
@ -226,7 +212,9 @@ public abstract class PreferenceCompatFragment extends SubsonicFragment {
dispatchActivityStop();
}
/** Access methods with visibility private **/
/**
* Access methods with visibility private
**/
private PreferenceManager createPreferenceManager() {
try {
@ -248,7 +236,7 @@ public abstract class PreferenceCompatFragment extends SubsonicFragment {
}
}
protected void setPreferenceScreen(PreferenceScreen preferenceScreen) {
void setPreferenceScreen(PreferenceScreen preferenceScreen) {
try {
Method m = PreferenceManager.class.getDeclaredMethod("setPreferences", PreferenceScreen.class);
m.setAccessible(true);
@ -294,19 +282,8 @@ public abstract class PreferenceCompatFragment extends SubsonicFragment {
}
}
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 ;
private 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);
@ -317,18 +294,7 @@ public abstract class PreferenceCompatFragment extends SubsonicFragment {
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);
}

View File

@ -1,24 +1,16 @@
package net.nullsum.audinaut.fragments;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.MenuItem;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.adapter.ArtistAdapter;
import net.nullsum.audinaut.adapter.EntryGridAdapter;
@ -28,26 +20,30 @@ import net.nullsum.audinaut.domain.Artist;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.domain.SearchCritera;
import net.nullsum.audinaut.domain.SearchResult;
import net.nullsum.audinaut.service.DownloadService;
import net.nullsum.audinaut.service.MusicService;
import net.nullsum.audinaut.service.MusicServiceFactory;
import net.nullsum.audinaut.service.DownloadService;
import net.nullsum.audinaut.util.BackgroundTask;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.TabBackgroundTask;
import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.view.UpdateView;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SearchFragment extends SubsonicFragment implements SectionAdapter.OnItemClickedListener<Serializable> {
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;
protected RecyclerView recyclerView;
protected SearchAdapter adapter;
protected boolean largeAlbums = false;
private RecyclerView recyclerView;
private SearchAdapter adapter;
private boolean largeAlbums = false;
private SearchResult searchResult;
private boolean skipSearch = false;
@ -62,7 +58,7 @@ public class SearchFragment extends SubsonicFragment implements SectionAdapter.O
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null) {
if (savedInstanceState != null) {
searchResult = (SearchResult) savedInstanceState.getSerializable(Constants.FRAGMENT_LIST);
}
largeAlbums = Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_LARGE_ALBUM_ART, true);
@ -79,16 +75,16 @@ public class SearchFragment extends SubsonicFragment implements SectionAdapter.O
rootView = inflater.inflate(R.layout.abstract_recycler_fragment, container, false);
setTitle(R.string.search_title);
refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_layout);
refreshLayout = rootView.findViewById(R.id.refresh_layout);
refreshLayout.setEnabled(false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.fragment_recycler);
recyclerView = rootView.findViewById(R.id.fragment_recycler);
setupLayoutManager(recyclerView, largeAlbums);
registerForContextMenu(recyclerView);
context.onNewIntent(context.getIntent());
if(searchResult != null) {
if (searchResult != null) {
skipSearch = true;
recyclerView.setAdapter(adapter = new SearchAdapter(context, searchResult, getImageLoader(), largeAlbums, this));
}
@ -100,9 +96,9 @@ public class SearchFragment extends SubsonicFragment implements SectionAdapter.O
public void setIsOnlyVisible(boolean isOnlyVisible) {
boolean update = this.isOnlyVisible != isOnlyVisible;
super.setIsOnlyVisible(isOnlyVisible);
if(update && adapter != null) {
if (update && adapter != null) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if(layoutManager instanceof GridLayoutManager) {
if (layoutManager instanceof GridLayoutManager) {
((GridLayoutManager) layoutManager).setSpanCount(getRecyclerColumnCount());
}
}
@ -114,7 +110,7 @@ public class SearchFragment extends SubsonicFragment implements SectionAdapter.O
@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) {
if (viewType == EntryGridAdapter.VIEW_TYPE_SONG || viewType == EntryGridAdapter.VIEW_TYPE_HEADER || viewType == ArtistAdapter.VIEW_TYPE_ARTIST) {
return gridLayoutManager.getSpanCount();
} else {
return 1;
@ -132,7 +128,7 @@ public class SearchFragment extends SubsonicFragment implements SectionAdapter.O
@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)) {
if (item instanceof MusicDirectory.Entry && !Util.isOffline(context)) {
menu.removeItem(R.id.song_menu_remove_playlist);
}
recreateContextMenu(menu);
@ -157,7 +153,7 @@ public class SearchFragment extends SubsonicFragment implements SectionAdapter.O
if (entry.isDirectory()) {
onAlbumSelected(entry, false);
} else {
onSongSelected(entry, false, true, true, false);
onSongSelected(entry, true);
}
}
}
@ -166,8 +162,8 @@ public class SearchFragment extends SubsonicFragment implements SectionAdapter.O
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) {
for (Serializable ser : selected) {
if (ser instanceof MusicDirectory.Entry) {
selectedMedia.add((MusicDirectory.Entry) ser);
}
}
@ -181,7 +177,7 @@ public class SearchFragment extends SubsonicFragment implements SectionAdapter.O
}
public void search(final String query, final boolean autoplay) {
if(skipSearch) {
if (skipSearch) {
skipSearch = false;
return;
}
@ -207,7 +203,7 @@ public class SearchFragment extends SubsonicFragment implements SectionAdapter.O
};
task.execute();
if(searchItem != null) {
if (searchItem != null) {
MenuItemCompat.collapseActionView(searchItem);
}
}
@ -221,7 +217,7 @@ public class SearchFragment extends SubsonicFragment implements SectionAdapter.O
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName());
if(autoplay) {
if (autoplay) {
args.putBoolean(Constants.INTENT_EXTRA_NAME_AUTOPLAY, true);
}
args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true);
@ -235,7 +231,7 @@ public class SearchFragment extends SubsonicFragment implements SectionAdapter.O
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, album.getId());
args.putString(Constants.INTENT_EXTRA_NAME_NAME, album.getTitle());
if(autoplay) {
if (autoplay) {
args.putBoolean(Constants.INTENT_EXTRA_NAME_AUTOPLAY, true);
}
fragment.setArguments(args);
@ -243,16 +239,14 @@ public class SearchFragment extends SubsonicFragment implements SectionAdapter.O
replaceFragment(fragment);
}
private void onSongSelected(MusicDirectory.Entry song, boolean save, boolean append, boolean autoplay, boolean playNext) {
private void onSongSelected(MusicDirectory.Entry song, boolean append) {
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);
}
downloadService.download(Collections.singletonList(song), false, false, false, false);
downloadService.play(downloadService.size() - 1);
Util.toast(context, getResources().getQuantityString(R.plurals.select_album_n_songs_added, 1, 1));
}
@ -262,30 +256,30 @@ public class SearchFragment extends SubsonicFragment implements SectionAdapter.O
query = query.toLowerCase();
Artist artist = null;
if(!searchResult.getArtists().isEmpty()) {
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()) {
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()) {
if (!searchResult.getSongs().isEmpty()) {
song = searchResult.getSongs().get(0);
song.setCloseness(Util.getStringDistance(song.getTitle().toLowerCase(), query));
}
if(artist != null && (artist.getCloseness() <= MIN_CLOSENESS ||
if (artist != null && (artist.getCloseness() <= MIN_CLOSENESS ||
(album == null || artist.getCloseness() <= album.getCloseness()) &&
(song == null || artist.getCloseness() <= song.getCloseness()))) {
(song == null || artist.getCloseness() <= song.getCloseness()))) {
onArtistSelected(artist, true);
} else if(album != null && (album.getCloseness() <= MIN_CLOSENESS ||
song == null || album.getCloseness() <= song.getCloseness())) {
} 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);
} else if (song != null) {
onSongSelected(song, false);
}
}
}

View File

@ -1,6 +1,5 @@
package net.nullsum.audinaut.fragments;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
@ -29,7 +28,6 @@ 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 List<MusicFolder> musicFolders = null;
private List<Entry> entries;
@ -44,7 +42,7 @@ public class SelectArtistFragment extends SelectRecyclerFragment<Serializable> i
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
if(bundle != null) {
if (bundle != null) {
musicFolders = (List<MusicFolder>) bundle.getSerializable(Constants.FRAGMENT_LIST2);
}
artist = true;
@ -59,8 +57,8 @@ public class SelectArtistFragment extends SelectRecyclerFragment<Serializable> i
@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)) {
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);
@ -90,10 +88,10 @@ public class SelectArtistFragment extends SelectRecyclerFragment<Serializable> i
@Override
public void onItemClicked(UpdateView<Serializable> updateView, Serializable item) {
SubsonicFragment fragment;
if(item instanceof Artist) {
if (item instanceof Artist) {
Artist artist = (Artist) item;
if ((Util.isFirstLevelArtist(context) || Util.isOffline(context) || Util.isTagBrowsing(context)) || groupId != null) {
if ((Util.isFirstLevelArtist(context) || Util.isOffline(context)) || groupId != null) {
fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId());
@ -129,7 +127,7 @@ public class SelectArtistFragment extends SelectRecyclerFragment<Serializable> i
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
super.onCreateOptionsMenu(menu, menuInflater);
if(Util.isOffline(context) || Util.isTagBrowsing(context) || groupId != null) {
if (Util.isOffline(context) || groupId != null) {
menu.removeItem(R.id.menu_first_level_artist);
} else {
if (Util.isFirstLevelArtist(context)) {
@ -145,7 +143,7 @@ public class SelectArtistFragment extends SelectRecyclerFragment<Serializable> i
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(super.onOptionsItemSelected(item)) {
if (super.onOptionsItemSelected(item)) {
return true;
}
@ -166,18 +164,8 @@ public class SelectArtistFragment extends SelectRecyclerFragment<Serializable> i
@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;
}
if (groupId == null) {
musicFolders = null;
String musicFolderId = Util.getSelectedMusicFolderId(context);
Indexes indexes = musicService.getIndexes(musicFolderId, refresh, context, listener);
@ -191,7 +179,7 @@ public class SelectArtistFragment extends SelectRecyclerFragment<Serializable> i
List<Artist> artists = new ArrayList<>();
items = new ArrayList<>();
MusicDirectory dir = musicService.getMusicDirectory(groupId, groupName, refresh, context, listener);
for(Entry entry: dir.getChildren(true, false)) {
for (Entry entry : dir.getChildren(true, false)) {
Artist artist = new Artist();
artist.setId(entry.getId());
artist.setName(entry.getTitle());
@ -204,9 +192,7 @@ public class SelectArtistFragment extends SelectRecyclerFragment<Serializable> i
items.addAll(indexes.getArtists());
entries = dir.getChildren(false, true);
for(Entry entry: entries) {
items.add(entry);
}
items.addAll(entries);
}
return items;
@ -221,7 +207,7 @@ public class SelectArtistFragment extends SelectRecyclerFragment<Serializable> i
public void setEmpty(boolean empty) {
super.setEmpty(empty);
if(empty && !Util.isOffline(context)) {
if (empty && !Util.isOffline(context)) {
objects.clear();
recyclerView.setAdapter(new ArtistAdapter(context, objects, musicFolders, this, this));
recyclerView.setVisibility(View.VISIBLE);
@ -244,7 +230,7 @@ public class SelectArtistFragment extends SelectRecyclerFragment<Serializable> i
String startMusicFolderId = Util.getSelectedMusicFolderId(context);
String musicFolderId = selectedFolder == null ? null : selectedFolder.getId();
if(!Util.equals(startMusicFolderId, musicFolderId)) {
if (!Util.equals(startMusicFolderId, musicFolderId)) {
Util.setSelectedMusicFolderId(context, musicFolderId);
context.invalidate();
}

View File

@ -1,74 +1,57 @@
package net.nullsum.audinaut.fragments;
import android.support.v7.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.adapter.AlphabeticalAlbumAdapter;
import net.nullsum.audinaut.adapter.EntryInfiniteGridAdapter;
import net.nullsum.audinaut.adapter.EntryGridAdapter;
import net.nullsum.audinaut.adapter.EntryInfiniteGridAdapter;
import net.nullsum.audinaut.adapter.SectionAdapter;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.service.CachedMusicService;
import net.nullsum.audinaut.service.DownloadService;
import net.nullsum.audinaut.util.DrawableTint;
import net.nullsum.audinaut.util.ImageLoader;
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import net.nullsum.audinaut.service.MusicService;
import net.nullsum.audinaut.service.MusicServiceFactory;
import net.nullsum.audinaut.service.OfflineException;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.LoadingTask;
import net.nullsum.audinaut.util.Pair;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.util.TabBackgroundTask;
import net.nullsum.audinaut.util.UpdateHelper;
import net.nullsum.audinaut.util.UserUtil;
import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.view.FastScroller;
import net.nullsum.audinaut.view.GridSpacingDecoration;
import net.nullsum.audinaut.view.MyLeadingMarginSpan2;
import net.nullsum.audinaut.view.RecyclingImageView;
import net.nullsum.audinaut.view.UpdateView;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.Collections;
import java.util.List;
import static net.nullsum.audinaut.domain.MusicDirectory.Entry;
public class SelectDirectoryFragment extends SubsonicFragment implements SectionAdapter.OnItemClickedListener<Entry> {
private static final String TAG = SelectDirectoryFragment.class.getSimpleName();
private String id;
private String name;
private Entry directory;
private String playlistId;
private String playlistName;
private boolean playlistOwner;
private String albumListType;
private String albumListExtra;
private int albumListSize;
private boolean refreshListing = false;
private boolean restoredInstance = false;
private boolean lookupParent = false;
private boolean largeAlbums = false;
private boolean topTracks = false;
private String lookupEntry;
private RecyclerView recyclerView;
private FastScroller fastScroller;
private EntryGridAdapter entryGridAdapter;
@ -76,27 +59,6 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
private List<Entry> entries;
private LoadTask currentTask;
private SilentBackgroundTask updateCoverArtTask;
private ImageView coverArtView;
private Entry coverArtRep;
private String coverArtId;
String id;
String name;
Entry directory;
String playlistId;
String playlistName;
boolean playlistOwner;
String albumListType;
String albumListExtra;
int albumListSize;
boolean refreshListing = false;
boolean restoredInstance = false;
boolean lookupParent = false;
boolean largeAlbums = false;
boolean topTracks = false;
String lookupEntry;
public SelectDirectoryFragment() {
super();
}
@ -104,10 +66,10 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
if(bundle != null) {
if (bundle != null) {
entries = (List<Entry>) bundle.getSerializable(Constants.FRAGMENT_LIST);
albums = (List<Entry>) bundle.getSerializable(Constants.FRAGMENT_LIST2);
if(albums == null) {
if (albums == null) {
albums = new ArrayList<>();
}
restoredInstance = true;
@ -124,14 +86,13 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
Bundle args = getArguments();
if(args != null) {
if (args != null) {
id = args.getString(Constants.INTENT_EXTRA_NAME_ID);
name = args.getString(Constants.INTENT_EXTRA_NAME_NAME);
directory = (Entry) args.getSerializable(Constants.INTENT_EXTRA_NAME_DIRECTORY);
playlistId = args.getString(Constants.INTENT_EXTRA_NAME_PLAYLIST_ID);
playlistName = args.getString(Constants.INTENT_EXTRA_NAME_PLAYLIST_NAME);
playlistOwner = args.getBoolean(Constants.INTENT_EXTRA_NAME_PLAYLIST_OWNER, false);
Object shareObj = args.getSerializable(Constants.INTENT_EXTRA_NAME_SHARE);
albumListType = args.getString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_TYPE);
albumListExtra = args.getString(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_EXTRA);
albumListSize = args.getInt(Constants.INTENT_EXTRA_NAME_ALBUM_LIST_SIZE, 0);
@ -141,37 +102,37 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
topTracks = args.getBoolean(Constants.INTENT_EXTRA_TOP_TRACKS);
String childId = args.getString(Constants.INTENT_EXTRA_NAME_CHILD_ID);
if(childId != null) {
if (childId != null) {
id = childId;
lookupParent = true;
}
if(entries == null) {
if (entries == null) {
entries = (List<Entry>) args.getSerializable(Constants.FRAGMENT_LIST);
albums = (List<Entry>) args.getSerializable(Constants.FRAGMENT_LIST2);
if(albums == null) {
albums = new ArrayList<Entry>();
if (albums == null) {
albums = new ArrayList<>();
}
}
}
rootView = inflater.inflate(R.layout.abstract_recycler_fragment, container, false);
refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_layout);
refreshLayout = rootView.findViewById(R.id.refresh_layout);
refreshLayout.setOnRefreshListener(this);
if(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_LARGE_ALBUM_ART, true)) {
if (Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_LARGE_ALBUM_ART, true)) {
largeAlbums = true;
}
recyclerView = (RecyclerView) rootView.findViewById(R.id.fragment_recycler);
recyclerView = rootView.findViewById(R.id.fragment_recycler);
recyclerView.setHasFixedSize(true);
fastScroller = (FastScroller) rootView.findViewById(R.id.fragment_fast_scroller);
fastScroller = rootView.findViewById(R.id.fragment_fast_scroller);
setupScrollList(recyclerView);
setupLayoutManager(recyclerView, largeAlbums);
if(entries == null) {
if(primaryFragment || secondaryFragment) {
if (entries == null) {
if (primaryFragment || secondaryFragment) {
load(false);
} else {
invalidated = true;
@ -180,7 +141,7 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
finishLoading();
}
if(name != null) {
if (name != null) {
setTitle(name);
}
@ -191,9 +152,9 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
public void setIsOnlyVisible(boolean isOnlyVisible) {
boolean update = this.isOnlyVisible != isOnlyVisible;
super.setIsOnlyVisible(isOnlyVisible);
if(update && entryGridAdapter != null) {
if (update && entryGridAdapter != null) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if(layoutManager instanceof GridLayoutManager) {
if (layoutManager instanceof GridLayoutManager) {
((GridLayoutManager) layoutManager).setSpanCount(getRecyclerColumnCount());
}
}
@ -201,18 +162,17 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
if(albumListType != null) {
if (albumListType != null) {
menuInflater.inflate(R.menu.select_album_list, menu);
} else if(artist) {
} else if (artist) {
menuInflater.inflate(R.menu.select_album, menu);
} else {
if(Util.isOffline(context)) {
if (Util.isOffline(context)) {
menuInflater.inflate(R.menu.select_song_offline, menu);
}
else {
} else {
menuInflater.inflate(R.menu.select_song, menu);
if(playlistId == null || !playlistOwner) {
if (playlistId == null || !playlistOwner) {
menu.removeItem(R.id.menu_remove_playlist);
}
}
@ -234,21 +194,22 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView updateView, Entry entry) {
onCreateContextMenuSupport(menu, menuInflater, updateView, entry);
if(!Util.isOffline(context) && (playlistId == null || !playlistOwner)) {
if (!Util.isOffline(context) && (playlistId == null || !playlistOwner)) {
menu.removeItem(R.id.song_menu_remove_playlist);
}
recreateContextMenu(menu);
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Entry> updateView, Entry entry) {
if(onContextItemSelected(menuItem, entry)) {
if (onContextItemSelected(menuItem, entry)) {
return true;
}
switch (menuItem.getItemId()) {
case R.id.song_menu_remove_playlist:
removeFromPlaylist(playlistId, playlistName, Arrays.<Integer>asList(entries.indexOf(entry)));
removeFromPlaylist(playlistId, playlistName, Collections.singletonList(entries.indexOf(entry)));
break;
}
@ -266,12 +227,12 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
if ("newest".equals(albumListType)) {
args.putBoolean(Constants.INTENT_EXTRA_REFRESH_LISTINGS, true);
}
if(!entry.isAlbum()) {
if (!entry.isAlbum()) {
args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true);
}
fragment.setArguments(args);
replaceFragment(fragment, true);
replaceFragment(fragment);
} else {
onSongPress(entries, entry, albumListType == null);
}
@ -288,11 +249,11 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
}
private void load(boolean refresh) {
if(refreshListing) {
if (refreshListing) {
refresh = true;
}
if(currentTask != null) {
if (currentTask != null) {
currentTask.cancel();
}
@ -309,21 +270,21 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
private void getMusicDirectory(final String id, final String name, final boolean refresh) {
setTitle(name);
new LoadTask(refresh) {
new LoadTask() {
@Override
protected MusicDirectory load(MusicService service) throws Exception {
MusicDirectory dir = getMusicDirectory(id, name, refresh, service, this);
if(lookupParent && dir.getParent() != null) {
if (lookupParent && dir.getParent() != null) {
dir = getMusicDirectory(dir.getParent(), name, refresh, service, this);
// Update the fragment pointers so other stuff works correctly
SelectDirectoryFragment.this.id = dir.getId();
SelectDirectoryFragment.this.name = dir.getName();
} else if(id != null && directory == null && dir.getParent() != null && !artist) {
} else if (id != null && directory == null && dir.getParent() != null && !artist) {
MusicDirectory parentDir = getMusicDirectory(dir.getParent(), name, refresh, true, service, this);
for(Entry child: parentDir.getChildren()) {
if(id.equals(child.getId())) {
for (Entry child : parentDir.getChildren()) {
if (id.equals(child.getId())) {
directory = child;
break;
}
@ -342,47 +303,10 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
}.execute();
}
private void getRecursiveMusicDirectory(final String id, final String name, final boolean refresh) {
setTitle(name);
new LoadTask(refresh) {
@Override
protected MusicDirectory load(MusicService service) throws Exception {
MusicDirectory root = getMusicDirectory(id, name, refresh, service, this);
List<Entry> songs = new ArrayList<Entry>();
getSongsRecursively(root, songs);
root.replaceChildren(songs);
return root;
}
private void getSongsRecursively(MusicDirectory parent, List<Entry> songs) throws Exception {
songs.addAll(parent.getChildren(false, true));
for (Entry dir : parent.getChildren(true, false)) {
MusicService musicService = MusicServiceFactory.getMusicService(context);
MusicDirectory musicDirectory;
if(Util.isTagBrowsing(context) && !Util.isOffline(context)) {
musicDirectory = musicService.getAlbum(dir.getId(), dir.getTitle(), false, context, this);
} else {
musicDirectory = musicService.getMusicDirectory(dir.getId(), dir.getTitle(), false, context, this);
}
getSongsRecursively(musicDirectory, songs);
}
}
@Override
protected void done(Pair<MusicDirectory, Boolean> result) {
SelectDirectoryFragment.this.name = result.getFirst().getName();
setTitle(SelectDirectoryFragment.this.name);
super.done(result);
}
}.execute();
}
private void getPlaylist(final String playlistId, final String playlistName, final boolean refresh) {
setTitle(playlistName);
new LoadTask(refresh) {
new LoadTask() {
@Override
protected MusicDirectory load(MusicService service) throws Exception {
return service.getPlaylist(refresh, playlistId, playlistName, context, this);
@ -397,11 +321,12 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
setTitle(R.string.main_albums_recent);
} else if ("frequent".equals(albumListType)) {
setTitle(R.string.main_albums_frequent);
} else if("genres".equals(albumListType) || "years".equals(albumListType)) {
} else if ("genres".equals(albumListType) || "years".equals(albumListType)) {
setTitle(albumListExtra);
} else if("alphabeticalByName".equals(albumListType)) {
} else if ("alphabeticalByName".equals(albumListType)) {
setTitle(R.string.main_albums_alphabetical);
} if (MainFragment.SONGS_NEWEST.equals(albumListType)) {
}
if (MainFragment.SONGS_NEWEST.equals(albumListType)) {
setTitle(R.string.main_songs_newest);
} else if (MainFragment.SONGS_TOP_PLAYED.equals(albumListType)) {
setTitle(R.string.main_songs_top_played);
@ -411,19 +336,19 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
setTitle(R.string.main_songs_frequent);
}
new LoadTask(true) {
new LoadTask() {
@Override
protected MusicDirectory load(MusicService service) throws Exception {
MusicDirectory result;
if("genres".equals(albumListType) || "years".equals(albumListType)) {
if ("genres".equals(albumListType) || "years".equals(albumListType)) {
result = service.getAlbumList(albumListType, albumListExtra, size, 0, refresh, context, this);
if(result.getChildrenSize() == 0 && "genres".equals(albumListType)) {
if (result.getChildrenSize() == 0 && "genres".equals(albumListType)) {
SelectDirectoryFragment.this.albumListType = "genres-songs";
result = service.getSongsByGenre(albumListExtra, size, 0, context, this);
}
} else if("genres".equals(albumListType) || "genres-songs".equals(albumListType)) {
} else if ("genres".equals(albumListType) || "genres-songs".equals(albumListType)) {
result = service.getSongsByGenre(albumListExtra, size, 0, context, this);
} else if(albumListType.indexOf(MainFragment.SONGS_LIST_PREFIX) != -1) {
} else if (albumListType.contains(MainFragment.SONGS_LIST_PREFIX)) {
result = service.getSongList(albumListType, size, 0, context, this);
} else {
result = service.getAlbumList(albumListType, size, 0, refresh, context, this);
@ -433,58 +358,6 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
}.execute();
}
private abstract class LoadTask extends TabBackgroundTask<Pair<MusicDirectory, Boolean>> {
private boolean refresh;
public LoadTask(boolean refresh) {
super(SelectDirectoryFragment.this);
this.refresh = refresh;
currentTask = this;
}
protected abstract MusicDirectory load(MusicService service) throws Exception;
@Override
protected Pair<MusicDirectory, Boolean> doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(context);
MusicDirectory dir = load(musicService);
albums = dir.getChildren(true, false);
entries = dir.getChildren();
// This isn't really an artist if no albums on it!
if(albums.size() == 0) {
artist = false;
}
return new Pair<>(dir, true);
}
@Override
protected void done(Pair<MusicDirectory, Boolean> result) {
finishLoading();
currentTask = null;
}
@Override
public void updateCache(int changeCode) {
if(entryGridAdapter != null && changeCode == CachedMusicService.CACHE_UPDATE_LIST) {
entryGridAdapter.notifyDataSetChanged();
} else if(changeCode == CachedMusicService.CACHE_UPDATE_METADATA) {
if(coverArtView != null && coverArtRep != null && !Util.equals(coverArtRep.getCoverArt(), coverArtId)) {
synchronized (coverArtRep) {
if (updateCoverArtTask != null && updateCoverArtTask.isRunning()) {
updateCoverArtTask.cancel();
}
updateCoverArtTask = getImageLoader().loadImage(coverArtView, coverArtRep, false, true);
coverArtId = coverArtRep.getCoverArt();
}
}
}
}
}
@Override
public SectionAdapter<Entry> getCurrentAdapter() {
return entryGridAdapter;
@ -496,7 +369,7 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
@Override
public int getSpanSize(int position) {
int viewType = entryGridAdapter.getItemViewType(position);
if(viewType == EntryGridAdapter.VIEW_TYPE_SONG || viewType == EntryGridAdapter.VIEW_TYPE_HEADER || viewType == EntryInfiniteGridAdapter.VIEW_TYPE_LOADING) {
if (viewType == EntryGridAdapter.VIEW_TYPE_SONG || viewType == EntryGridAdapter.VIEW_TYPE_HEADER || viewType == EntryInfiniteGridAdapter.VIEW_TYPE_LOADING) {
return gridLayoutManager.getSpanCount();
} else {
return 1;
@ -507,19 +380,19 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
private void finishLoading() {
boolean validData = !entries.isEmpty() || !albums.isEmpty();
if(!validData) {
if (!validData) {
setEmpty(true);
}
if(validData) {
if (validData) {
recyclerView.setVisibility(View.VISIBLE);
}
if(albumListType == null) {
if (albumListType == null) {
entryGridAdapter = new EntryGridAdapter(context, entries, getImageLoader(), largeAlbums);
entryGridAdapter.setRemoveFromPlaylist(playlistId != null);
} else {
if("alphabeticalByName".equals(albumListType)) {
if ("alphabeticalByName".equals(albumListType)) {
entryGridAdapter = new AlphabeticalAlbumAdapter(context, entries, getImageLoader(), largeAlbums);
} else {
entryGridAdapter = new EntryInfiniteGridAdapter(context, entries, getImageLoader(), largeAlbums);
@ -530,10 +403,6 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
infiniteGridAdapter.setData(albumListType, albumListExtra, albumListSize);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
@ -542,15 +411,15 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
int totalItemCount = layoutManager.getItemCount();
int lastVisibleItem;
if(layoutManager instanceof GridLayoutManager) {
if (layoutManager instanceof GridLayoutManager) {
lastVisibleItem = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
} else if(layoutManager instanceof LinearLayoutManager) {
} else if (layoutManager instanceof LinearLayoutManager) {
lastVisibleItem = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
} else {
return;
}
if(totalItemCount > 0 && lastVisibleItem >= totalItemCount - 2) {
if (totalItemCount > 0 && lastVisibleItem >= totalItemCount - 2) {
infiniteGridAdapter.loadMore();
}
}
@ -558,17 +427,17 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
}
entryGridAdapter.setOnItemClickedListener(this);
// Always show artist if this is not a artist we are viewing
if(!artist) {
entryGridAdapter.setShowArtist(true);
if (!artist) {
entryGridAdapter.setShowArtist();
}
if(topTracks) {
entryGridAdapter.setShowAlbum(true);
if (topTracks) {
entryGridAdapter.setShowAlbum();
}
int scrollToPosition = -1;
if(lookupEntry != null) {
for(int i = 0; i < entries.size(); i++) {
if(lookupEntry.equals(entries.get(i).getTitle())) {
if (lookupEntry != null) {
for (int i = 0; i < entries.size(); i++) {
if (lookupEntry.equals(entries.get(i).getTitle())) {
scrollToPosition = i;
entryGridAdapter.addSelected(entries.get(i));
lookupEntry = null;
@ -581,7 +450,7 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
fastScroller.attachRecyclerView(recyclerView);
context.supportInvalidateOptionsMenu();
if(scrollToPosition != -1) {
if (scrollToPosition != -1) {
recyclerView.scrollToPosition(scrollToPosition);
}
@ -595,30 +464,31 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
@Override
protected void playNow(final boolean shuffle, final boolean append, final boolean playNext) {
List<Entry> songs = getSelectedEntries();
if(!songs.isEmpty()) {
download(songs, append, false, !append, playNext, shuffle);
if (!songs.isEmpty()) {
download(songs, append, !append, playNext, shuffle);
entryGridAdapter.clearSelected();
} else {
playAll(shuffle, append, playNext);
}
}
private void playAll(final boolean shuffle, final boolean append, final boolean playNext) {
boolean hasSubFolders = albums != null && !albums.isEmpty();
if (hasSubFolders && id != null) {
downloadRecursively(id, false, append, !append, shuffle, false, playNext);
} else if(hasSubFolders && albumListType != null) {
} else if (hasSubFolders && albumListType != null) {
downloadRecursively(albums, shuffle, append, playNext);
} else {
download(entries, append, false, !append, playNext, shuffle);
download(entries, append, !append, playNext, shuffle);
}
}
private List<Integer> getSelectedIndexes() {
List<Entry> selected = entryGridAdapter.getSelected();
List<Integer> indexes = new ArrayList<Integer>();
List<Integer> indexes = new ArrayList<>();
for(Entry entry: selected) {
for (Entry entry : selected) {
indexes.add(entries.indexOf(entry));
}
@ -628,28 +498,29 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
@Override
protected void downloadBackground(final boolean save) {
List<Entry> songs = getSelectedEntries();
if(playlistId != null) {
if (playlistId != null) {
songs = entries;
}
if(songs.isEmpty()) {
if (songs.isEmpty()) {
// Get both songs and albums
downloadRecursively(id, save, false, false, false, true);
downloadRecursively(id, save, false, false, false, true, false);
} else {
downloadBackground(save, songs);
}
}
@Override
protected void downloadBackground(final boolean save, final List<Entry> entries) {
void downloadBackground(final boolean save, final List<Entry> entries) {
if (getDownloadService() == null) {
return;
}
warnIfStorageUnavailable();
RecursiveLoader onValid = new RecursiveLoader(context) {
new RecursiveLoader(context) {
@Override
protected Boolean doInBackground() throws Throwable {
getSongsRecursively(entries, true);
getSongsRecursively(entries);
getDownloadService().downloadBackground(songs, save);
return null;
}
@ -662,16 +533,16 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
}
@Override
protected void download(List<Entry> entries, boolean append, boolean save, boolean autoplay, boolean playNext, boolean shuffle) {
download(entries, append, save, autoplay, playNext, shuffle, playlistName, playlistId);
void download(List<Entry> entries, boolean append, boolean autoplay, boolean playNext, boolean shuffle) {
download(entries, append, autoplay, playNext, shuffle, playlistName, playlistId);
}
@Override
protected void delete() {
List<Entry> songs = getSelectedEntries();
if(songs.isEmpty()) {
for(Entry entry: entries) {
if(entry.isDirectory()) {
if (songs.isEmpty()) {
for (Entry entry : entries) {
if (entry.isDirectory()) {
deleteRecursively(entry);
} else {
songs.add(entry);
@ -683,7 +554,7 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
}
}
public void removeFromPlaylist(final String id, final String name, final List<Integer> indexes) {
private void removeFromPlaylist(final String id, final String name, final List<Integer> indexes) {
new LoadingTask<Void>(context, true) {
@Override
protected Void doInBackground() throws Throwable {
@ -694,7 +565,7 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
@Override
protected void done(Void result) {
for(Integer index: indexes) {
for (Integer index : indexes) {
entryGridAdapter.removeAt(index);
}
Util.toast(context, context.getResources().getString(R.string.removed_playlist, indexes.size(), name));
@ -714,113 +585,43 @@ public class SelectDirectoryFragment extends SubsonicFragment implements Section
}.execute();
}
private void showTopTracks() {
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle(getArguments());
args.putBoolean(Constants.INTENT_EXTRA_TOP_TRACKS, true);
fragment.setArguments(args);
private abstract class LoadTask extends TabBackgroundTask<Pair<MusicDirectory, Boolean>> {
replaceFragment(fragment, true);
}
public LoadTask() {
super(SelectDirectoryFragment.this);
private View createHeader() {
View header = LayoutInflater.from(context).inflate(R.layout.select_album_header, null, false);
setupCoverArt(header);
setupTextDisplay(header);
return header;
}
private void setupCoverArt(View header) {
setupCoverArtImpl((RecyclingImageView) header.findViewById(R.id.select_album_art));
}
private void setupCoverArtImpl(RecyclingImageView coverArtView) {
final ImageLoader imageLoader = getImageLoader();
if(entries.size() > 0) {
coverArtRep = null;
this.coverArtView = coverArtView;
for (int i = 0; (i < 3) && (coverArtRep == null || coverArtRep.getCoverArt() == null); i++) {
coverArtRep = entries.get(random.nextInt(entries.size()));
}
coverArtView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (coverArtRep == null || coverArtRep.getCoverArt() == null) {
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
ImageView fullScreenView = new ImageView(context);
imageLoader.loadImage(fullScreenView, coverArtRep, true, true);
builder.setCancelable(true);
AlertDialog imageDialog = builder.create();
// Set view here with unecessary 0's to remove top/bottom border
imageDialog.setView(fullScreenView, 0, 0, 0, 0);
imageDialog.show();
}
});
synchronized (coverArtRep) {
coverArtId = coverArtRep.getCoverArt();
updateCoverArtTask = imageLoader.loadImage(coverArtView, coverArtRep, false, true);
}
currentTask = this;
}
coverArtView.setOnInvalidated(new RecyclingImageView.OnInvalidated() {
@Override
public void onInvalidated(RecyclingImageView imageView) {
setupCoverArtImpl(imageView);
protected abstract MusicDirectory load(MusicService service) throws Exception;
@Override
protected Pair<MusicDirectory, Boolean> doInBackground() throws Throwable {
MusicService musicService = MusicServiceFactory.getMusicService(context);
MusicDirectory dir = load(musicService);
albums = dir.getChildren(true, false);
entries = dir.getChildren();
// This isn't really an artist if no albums on it!
if (albums.size() == 0) {
artist = false;
}
});
}
private void setupTextDisplay(final View header) {
final TextView titleView = (TextView) header.findViewById(R.id.select_album_title);
if(playlistName != null) {
titleView.setText(playlistName);
} else if(name != null) {
titleView.setText(name);
return new Pair<>(dir, true);
}
int songCount = 0;
Set<String> artists = new HashSet<String>();
Set<Integer> years = new HashSet<Integer>();
Integer totalDuration = 0;
for (Entry entry : entries) {
if (!entry.isDirectory()) {
songCount++;
if (entry.getArtist() != null) {
artists.add(entry.getArtist());
}
if(entry.getYear() != null) {
years.add(entry.getYear());
}
Integer duration = entry.getDuration();
if(duration != null) {
totalDuration += duration;
}
}
@Override
protected void done(Pair<MusicDirectory, Boolean> result) {
finishLoading();
currentTask = null;
}
final TextView artistView = (TextView) header.findViewById(R.id.select_album_artist);
if (artists.size() == 1) {
String artistText = artists.iterator().next();
if(years.size() == 1) {
artistText += " - " + years.iterator().next();
@Override
public void updateCache(int changeCode) {
if (entryGridAdapter != null && changeCode == CachedMusicService.CACHE_UPDATE_LIST) {
entryGridAdapter.notifyDataSetChanged();
}
artistView.setText(artistText);
artistView.setVisibility(View.VISIBLE);
} else {
artistView.setVisibility(View.GONE);
}
TextView songCountView = (TextView) header.findViewById(R.id.select_album_song_count);
TextView songLengthView = (TextView) header.findViewById(R.id.select_album_song_length);
String s = context.getResources().getQuantityString(R.plurals.select_album_n_songs, songCount, songCount);
songCountView.setText(s.toUpperCase());
songLengthView.setText(Util.formatDuration(totalDuration));
}
}

View File

@ -21,18 +21,17 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.adapter.GenreAdapter;
import net.nullsum.audinaut.adapter.SectionAdapter;
import net.nullsum.audinaut.domain.Genre;
import net.nullsum.audinaut.service.MusicService;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.ProgressListener;
import net.nullsum.audinaut.adapter.GenreAdapter;
import net.nullsum.audinaut.view.UpdateView;
import java.util.List;
public class SelectGenreFragment extends SelectRecyclerFragment<Genre> {
private static final String TAG = SelectGenreFragment.class.getSimpleName();
@Override
public int getOptionsMenu() {
@ -68,7 +67,8 @@ public class SelectGenreFragment extends SelectRecyclerFragment<Genre> {
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Genre> updateView, Genre item) {}
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Genre> updateView, Genre item) {
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Genre> updateView, Genre item) {

View File

@ -1,10 +1,7 @@
package net.nullsum.audinaut.fragments;
import android.support.v7.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@ -13,30 +10,26 @@ import android.widget.CheckBox;
import android.widget.EditText;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.adapter.PlaylistAdapter;
import net.nullsum.audinaut.adapter.SectionAdapter;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.domain.Playlist;
import net.nullsum.audinaut.service.DownloadFile;
import net.nullsum.audinaut.service.MusicService;
import net.nullsum.audinaut.service.MusicServiceFactory;
import net.nullsum.audinaut.service.OfflineException;
import net.nullsum.audinaut.util.ProgressListener;
import net.nullsum.audinaut.util.SyncUtil;
import net.nullsum.audinaut.util.CacheCleaner;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.LoadingTask;
import net.nullsum.audinaut.util.ProgressListener;
import net.nullsum.audinaut.util.SyncUtil;
import net.nullsum.audinaut.util.UserUtil;
import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.adapter.PlaylistAdapter;
import net.nullsum.audinaut.view.UpdateView;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SelectPlaylistFragment extends SelectRecyclerFragment<Playlist> {
private static final String TAG = SelectPlaylistFragment.class.getSimpleName();
@Override
public void onCreate(Bundle bundle) {
@ -50,11 +43,10 @@ public class SelectPlaylistFragment extends SelectRecyclerFragment<Playlist> {
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 {
} 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())) {
if (playlist.getPublic() != null && playlist.getPublic() && !playlist.getId().contains(".m3u") && !UserUtil.getCurrentUsername(context).equals(playlist.getOwner())) {
menu.removeItem(R.id.playlist_update_info);
menu.removeItem(R.id.playlist_menu_delete);
}
@ -67,11 +59,10 @@ public class SelectPlaylistFragment extends SelectRecyclerFragment<Playlist> {
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);
downloadPlaylist(playlist.getId(), playlist.getName());
break;
case R.id.playlist_menu_play_now:
fragment = new SelectDirectoryFragment();
@ -118,8 +109,8 @@ public class SelectPlaylistFragment extends SelectRecyclerFragment<Playlist> {
List<Playlist> mine = new ArrayList<>();
String currentUsername = UserUtil.getCurrentUsername(context);
for(Playlist playlist: playlists) {
if(playlist.getOwner() == null || playlist.getOwner().equals(currentUsername)) {
for (Playlist playlist : playlists) {
if (playlist.getOwner() == null || playlist.getOwner().equals(currentUsername)) {
mine.add(playlist);
}
}
@ -130,7 +121,7 @@ public class SelectPlaylistFragment extends SelectRecyclerFragment<Playlist> {
@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) {
if (!Util.isOffline(context) && refresh) {
new CacheCleaner(context, getDownloadService()).cleanPlaylists(playlists);
}
return playlists;
@ -147,7 +138,7 @@ public class SelectPlaylistFragment extends SelectRecyclerFragment<Playlist> {
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)) {
if ((playlist.getOwner() != null && playlist.getOwner().equals(UserUtil.getCurrentUsername(context)) || playlist.getId().contains(".m3u"))) {
args.putBoolean(Constants.INTENT_EXTRA_NAME_PLAYLIST_OWNER, true);
}
fragment.setArguments(args);
@ -158,7 +149,7 @@ public class SelectPlaylistFragment extends SelectRecyclerFragment<Playlist> {
@Override
public void onFinishRefresh() {
Bundle args = getArguments();
if(args != null) {
if (args != null) {
String playlistId = args.getString(Constants.INTENT_EXTRA_NAME_ID, null);
if (playlistId != null && objects != null) {
for (Playlist playlist : objects) {
@ -172,38 +163,33 @@ public class SelectPlaylistFragment extends SelectRecyclerFragment<Playlist> {
}
private void deletePlaylist(final Playlist playlist) {
Util.confirmDialog(context, R.string.common_delete, playlist.getName(), new DialogInterface.OnClickListener() {
Util.confirmDialog(context, R.string.common_delete, playlist.getName(), (dialog, which) -> new LoadingTask<Void>(context, false) {
@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 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();
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 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());
}
private void displayPlaylistInfo(final Playlist playlist) {
@ -213,12 +199,12 @@ public class SelectPlaylistFragment extends SelectRecyclerFragment<Playlist> {
headers.add(R.string.details_title);
details.add(playlist.getName());
if(playlist.getOwner() != null) {
if (playlist.getOwner() != null) {
headers.add(R.string.details_owner);
details.add(playlist.getOwner());
}
if(playlist.getComment() != null) {
if (playlist.getComment() != null) {
headers.add(R.string.details_comments);
details.add(playlist.getComment());
}
@ -226,23 +212,23 @@ public class SelectPlaylistFragment extends SelectRecyclerFragment<Playlist> {
headers.add(R.string.details_song_count);
details.add(playlist.getSongCount());
if(playlist.getDuration() != null) {
if (playlist.getDuration() != null) {
headers.add(R.string.details_length);
details.add(Util.formatDuration(playlist.getDuration()));
}
if(playlist.getPublic() != null) {
if (playlist.getPublic() != null) {
headers.add(R.string.details_public);
details.add(Util.formatBoolean(context, playlist.getPublic()));
}
if(playlist.getCreated() != null) {
if (playlist.getCreated() != null) {
headers.add(R.string.details_created);
DateFormat dateFormat = DateFormat.getDateInstance();
details.add(dateFormat.format(playlist.getCreated()));
}
if(playlist.getChanged() != null) {
if (playlist.getChanged() != null) {
headers.add(R.string.details_updated);
DateFormat dateFormat = DateFormat.getDateInstance();
@ -254,93 +240,59 @@ public class SelectPlaylistFragment extends SelectRecyclerFragment<Playlist> {
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);
final EditText nameBox = dialogView.findViewById(R.id.get_playlist_name);
final EditText commentBox = dialogView.findViewById(R.id.get_playlist_comment);
final CheckBox publicBox = dialogView.findViewById(R.id.get_playlist_public);
nameBox.setText(playlist.getName());
commentBox.setText(playlist.getComment());
Boolean pub = playlist.getPublic();
if(pub == null) {
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();
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.playlist_update_info)
.setView(dialogView)
.setPositiveButton(R.string.common_ok, (dialog, 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 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 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);
}
Util.toast(context, msg, false);
}
}.execute();
}
})
.setNegativeButton(R.string.common_cancel, null)
.show();
Util.toast(context, msg, false);
}
}.execute())
.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 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();
}
return null;
}
@Override
protected void done(Void result) {
}
}.execute();
}
}

View File

@ -15,27 +15,16 @@
package net.nullsum.audinaut.fragments;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.adapter.SectionAdapter;
import net.nullsum.audinaut.service.MusicService;
@ -45,23 +34,27 @@ import net.nullsum.audinaut.util.ProgressListener;
import net.nullsum.audinaut.util.TabBackgroundTask;
import net.nullsum.audinaut.view.FastScroller;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
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;
RecyclerView recyclerView;
SectionAdapter<T> adapter;
List<T> objects;
boolean serialize = true;
boolean largeAlbums = false;
boolean pullToRefresh = true;
boolean backgroundUpdate = true;
private FastScroller fastScroller;
private UpdateTask currentTask;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
if(bundle != null && serialize) {
if (bundle != null && serialize) {
objects = (List<T>) bundle.getSerializable(Constants.FRAGMENT_LIST);
}
}
@ -69,7 +62,7 @@ public abstract class SelectRecyclerFragment<T> extends SubsonicFragment impleme
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(serialize) {
if (serialize) {
outState.putSerializable(Constants.FRAGMENT_LIST, (Serializable) objects);
}
}
@ -78,20 +71,20 @@ public abstract class SelectRecyclerFragment<T> extends SubsonicFragment impleme
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 = 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);
recyclerView = rootView.findViewById(R.id.fragment_recycler);
fastScroller = rootView.findViewById(R.id.fragment_fast_scroller);
setupLayoutManager();
if(pullToRefresh) {
if (pullToRefresh) {
setupScrollList(recyclerView);
} else {
refreshLayout.setEnabled(false);
}
if(objects == null) {
if (objects == null) {
refresh(false);
} else {
recyclerView.setAdapter(adapter = getAdapter(objects));
@ -102,7 +95,7 @@ public abstract class SelectRecyclerFragment<T> extends SubsonicFragment impleme
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
if(!primaryFragment) {
if (!primaryFragment) {
return;
}
@ -110,44 +103,39 @@ public abstract class SelectRecyclerFragment<T> extends SubsonicFragment impleme
onFinishSetupOptionsMenu(menu);
}
@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) {
if (update && adapter != null) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if(layoutManager instanceof GridLayoutManager) {
if (layoutManager instanceof GridLayoutManager) {
((GridLayoutManager) layoutManager).setSpanCount(getRecyclerColumnCount());
}
}
}
@Override
protected void refresh(final boolean refresh) {
void refresh(final boolean refresh) {
int titleRes = getTitleResource();
if(titleRes != 0) {
if (titleRes != 0) {
setTitle(getTitleResource());
}
if(backgroundUpdate) {
if (backgroundUpdate) {
recyclerView.setVisibility(View.GONE);
}
// Cancel current running task before starting another one
if(currentTask != null) {
if (currentTask != null) {
currentTask.cancel();
}
currentTask = new UpdateTask(this, refresh);
if(backgroundUpdate) {
if (backgroundUpdate) {
currentTask.execute();
} else {
objects = new ArrayList<T>();
objects = new ArrayList<>();
try {
objects = getObjects(null, refresh, null);
@ -167,17 +155,20 @@ public abstract class SelectRecyclerFragment<T> extends SubsonicFragment impleme
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();
protected abstract int getOptionsMenu();
public void onFinishRefresh() {
protected abstract SectionAdapter<T> getAdapter(List<T> objs);
protected abstract List<T> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception;
protected abstract int getTitleResource();
void onFinishRefresh() {
}
private class UpdateTask extends TabBackgroundTask<List<T>> {
private boolean refresh;
private final boolean refresh;
public UpdateTask(SubsonicFragment fragment, boolean refresh) {
super(fragment);
@ -188,7 +179,7 @@ public abstract class SelectRecyclerFragment<T> extends SubsonicFragment impleme
public List<T> doInBackground() throws Exception {
MusicService musicService = MusicServiceFactory.getMusicService(context);
objects = new ArrayList<T>();
objects = new ArrayList<>();
try {
objects = getObjects(musicService, refresh, this);
@ -203,7 +194,7 @@ public abstract class SelectRecyclerFragment<T> extends SubsonicFragment impleme
public void done(List<T> result) {
if (result != null && !result.isEmpty()) {
recyclerView.setAdapter(adapter = getAdapter(result));
if(!fastScroller.isAttached()) {
if (!fastScroller.isAttached()) {
fastScroller.attachRecyclerView(recyclerView);
}

View File

@ -20,9 +20,6 @@ import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.adapter.BasicListAdapter;
import net.nullsum.audinaut.adapter.SectionAdapter;
@ -31,6 +28,9 @@ import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.ProgressListener;
import net.nullsum.audinaut.view.UpdateView;
import java.util.ArrayList;
import java.util.List;
public class SelectYearFragment extends SelectRecyclerFragment<String> {
public SelectYearFragment() {
@ -53,7 +53,7 @@ public class SelectYearFragment extends SelectRecyclerFragment<String> {
@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) {
for (int i = 2010; i >= 1800; i -= 10) {
decades.add(String.valueOf(i));
}
@ -79,7 +79,8 @@ public class SelectYearFragment extends SelectRecyclerFragment<String> {
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<String> updateView, String item) {}
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<String> updateView, String item) {
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<String> updateView, String item) {

View File

@ -16,9 +16,8 @@
package net.nullsum.audinaut.fragments;
import android.accounts.Account;
import android.app.backup.BackupManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
@ -31,21 +30,11 @@ import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.LinkedHashMap;
import java.util.Map;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.service.DownloadService;
import net.nullsum.audinaut.service.HeadphoneListenerService;
@ -59,10 +48,16 @@ import net.nullsum.audinaut.util.Util;
import net.nullsum.audinaut.view.CacheLocationPreference;
import net.nullsum.audinaut.view.ErrorDialog;
import java.io.File;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.LinkedHashMap;
import java.util.Map;
public class SettingsFragment extends PreferenceCompatFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
private final static String TAG = SettingsFragment.class.getSimpleName();
private final Map<String, ServerSettings> serverSettings = new LinkedHashMap<String, ServerSettings>();
private final Map<String, ServerSettings> serverSettings = new LinkedHashMap<>();
private boolean testingConnection;
private ListPreference theme;
private ListPreference maxBitrateWifi;
@ -74,7 +69,6 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
private ListPreference keepPlayedCount;
private ListPreference tempLoss;
private ListPreference pauseDisconnect;
private Preference addServerPreference;
private PreferenceCategory serversCategory;
private ListPreference songPressAction;
private ListPreference syncInterval;
@ -122,17 +116,17 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
Bundle args = new Bundle();
int xml = 0;
if("appearance".equals(name)) {
if ("appearance".equals(name)) {
xml = R.xml.settings_appearance;
} else if("cache".equals(name)) {
} else if ("cache".equals(name)) {
xml = R.xml.settings_cache;
} else if("playback".equals(name)) {
} else if ("playback".equals(name)) {
xml = R.xml.settings_playback;
} else if("servers".equals(name)) {
} else if ("servers".equals(name)) {
xml = R.xml.settings_servers;
}
if(xml != 0) {
if (xml != 0) {
args.putInt(Constants.INTENT_EXTRA_FRAGMENT_TYPE, xml);
newFragment.setArguments(args);
replaceFragment(newFragment);
@ -142,7 +136,7 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// Random error I have no idea how to reproduce
if(sharedPreferences == null) {
if (sharedPreferences == null) {
return;
}
@ -150,25 +144,22 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
if (Constants.PREFERENCES_KEY_HIDE_MEDIA.equals(key)) {
setHideMedia(sharedPreferences.getBoolean(key, true));
}
else if (Constants.PREFERENCES_KEY_MEDIA_BUTTONS.equals(key)) {
} else if (Constants.PREFERENCES_KEY_MEDIA_BUTTONS.equals(key)) {
setMediaButtonsEnabled(sharedPreferences.getBoolean(key, true));
}
else if (Constants.PREFERENCES_KEY_CACHE_LOCATION.equals(key)) {
} else if (Constants.PREFERENCES_KEY_CACHE_LOCATION.equals(key)) {
setCacheLocation(sharedPreferences.getString(key, ""));
}
else if(Constants.PREFERENCES_KEY_SYNC_MOST_RECENT.equals(key)) {
} else if (Constants.PREFERENCES_KEY_SYNC_MOST_RECENT.equals(key)) {
SyncUtil.removeMostRecentSyncFiles(context);
} else if(Constants.PREFERENCES_KEY_REPLAY_GAIN.equals(key) || Constants.PREFERENCES_KEY_REPLAY_GAIN_BUMP.equals(key) || Constants.PREFERENCES_KEY_REPLAY_GAIN_UNTAGGED.equals(key)) {
} else if (Constants.PREFERENCES_KEY_REPLAY_GAIN.equals(key) || Constants.PREFERENCES_KEY_REPLAY_GAIN_BUMP.equals(key) || Constants.PREFERENCES_KEY_REPLAY_GAIN_UNTAGGED.equals(key)) {
DownloadService downloadService = DownloadService.getInstance();
if(downloadService != null) {
if (downloadService != null) {
downloadService.reapplyVolume();
}
} else if(Constants.PREFERENCES_KEY_START_ON_HEADPHONES.equals(key)) {
} else if (Constants.PREFERENCES_KEY_START_ON_HEADPHONES.equals(key)) {
Intent serviceIntent = new Intent();
serviceIntent.setClassName(context.getPackageName(), HeadphoneListenerService.class.getName());
if(sharedPreferences.getBoolean(key, false)) {
if (sharedPreferences.getBoolean(key, false)) {
context.startService(serviceIntent);
} else {
context.stopService(serviceIntent);
@ -199,7 +190,7 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
tempLoss = (ListPreference) this.findPreference(Constants.PREFERENCES_KEY_TEMP_LOSS);
pauseDisconnect = (ListPreference) this.findPreference(Constants.PREFERENCES_KEY_PAUSE_DISCONNECT);
serversCategory = (PreferenceCategory) this.findPreference(Constants.PREFERENCES_KEY_SERVER_KEY);
addServerPreference = this.findPreference(Constants.PREFERENCES_KEY_SERVER_ADD);
Preference addServerPreference = this.findPreference(Constants.PREFERENCES_KEY_SERVER_ADD);
songPressAction = (ListPreference) this.findPreference(Constants.PREFERENCES_KEY_SONG_PRESS_ACTION);
syncInterval = (ListPreference) this.findPreference(Constants.PREFERENCES_KEY_SYNC_INTERVAL);
syncEnabled = (CheckBoxPreference) this.findPreference(Constants.PREFERENCES_KEY_SYNC_ENABLED);
@ -215,86 +206,69 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
settings = Util.getPreferences(context);
serverCount = settings.getInt(Constants.PREFERENCES_KEY_SERVER_COUNT, 1);
if(cacheSize != null) {
this.findPreference("clearCache").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Util.confirmDialog(context, R.string.common_delete, R.string.common_confirm_message_cache, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new LoadingTask<Void>(context, false) {
@Override
protected Void doInBackground() throws Throwable {
FileUtil.deleteMusicDirectory(context);
FileUtil.deleteSerializedCache(context);
FileUtil.deleteArtworkCache(context);
return null;
}
if (cacheSize != null) {
this.findPreference("clearCache").setOnPreferenceClickListener(preference -> {
Util.confirmDialog(context, (dialog, which) -> new LoadingTask<Void>(context, false) {
@Override
protected Void doInBackground() throws Throwable {
FileUtil.deleteMusicDirectory(context);
FileUtil.deleteSerializedCache(context);
FileUtil.deleteArtworkCache(context);
return null;
}
@Override
protected void done(Void result) {
Util.toast(context, R.string.settings_cache_clear_complete);
}
@Override
protected void done(Void result) {
Util.toast(context, R.string.settings_cache_clear_complete);
}
@Override
protected void error(Throwable error) {
Util.toast(context, getErrorMessage(error), false);
}
}.execute();
}
});
return false;
}
@Override
protected void error(Throwable error) {
Util.toast(context, getErrorMessage(error), false);
}
}.execute());
return false;
});
}
if(syncEnabled != null) {
this.findPreference(Constants.PREFERENCES_KEY_SYNC_ENABLED).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Boolean syncEnabled = (Boolean) newValue;
if (syncEnabled != null) {
this.findPreference(Constants.PREFERENCES_KEY_SYNC_ENABLED).setOnPreferenceChangeListener((preference, newValue) -> {
Boolean syncEnabled = (Boolean) newValue;
Account account = new Account(Constants.SYNC_ACCOUNT_NAME, Constants.SYNC_ACCOUNT_TYPE);
ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_PLAYLIST_AUTHORITY, syncEnabled);
Account account = new Account(Constants.SYNC_ACCOUNT_NAME, Constants.SYNC_ACCOUNT_TYPE);
ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_PLAYLIST_AUTHORITY, syncEnabled);
return true;
}
return true;
});
syncInterval.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Integer syncInterval = Integer.parseInt(((String) newValue));
syncInterval.setOnPreferenceChangeListener((preference, newValue) -> {
Integer syncInterval = Integer.parseInt(((String) newValue));
Account account = new Account(Constants.SYNC_ACCOUNT_NAME, Constants.SYNC_ACCOUNT_TYPE);
ContentResolver.addPeriodicSync(account, Constants.SYNC_ACCOUNT_PLAYLIST_AUTHORITY, new Bundle(), 60L * syncInterval);
Account account = new Account(Constants.SYNC_ACCOUNT_NAME, Constants.SYNC_ACCOUNT_TYPE);
ContentResolver.addPeriodicSync(account, Constants.SYNC_ACCOUNT_PLAYLIST_AUTHORITY, new Bundle(), 60L * syncInterval);
return true;
}
return true;
});
}
if(serversCategory != null) {
addServerPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
serverCount++;
int instance = serverCount;
serversCategory.addPreference(addServer(serverCount));
if (serversCategory != null) {
addServerPreference.setOnPreferenceClickListener(preference -> {
serverCount++;
int instance = serverCount;
serversCategory.addPreference(addServer(serverCount));
SharedPreferences.Editor editor = settings.edit();
editor.putInt(Constants.PREFERENCES_KEY_SERVER_COUNT, serverCount);
// Reset set folder ID
editor.putString(Constants.PREFERENCES_KEY_MUSIC_FOLDER_ID + instance, null);
editor.putString(Constants.PREFERENCES_KEY_SERVER_URL + instance, "http://yourhost");
editor.putString(Constants.PREFERENCES_KEY_SERVER_NAME + instance, getResources().getString(R.string.settings_server_unused));
editor.apply();
SharedPreferences.Editor editor = settings.edit();
editor.putInt(Constants.PREFERENCES_KEY_SERVER_COUNT, serverCount);
// Reset set folder ID
editor.putString(Constants.PREFERENCES_KEY_MUSIC_FOLDER_ID + instance, null);
editor.putString(Constants.PREFERENCES_KEY_SERVER_URL + instance, "http://yourhost");
editor.putString(Constants.PREFERENCES_KEY_SERVER_NAME + instance, getResources().getString(R.string.settings_server_unused));
editor.apply();
ServerSettings ss = new ServerSettings(instance);
serverSettings.put(String.valueOf(instance), ss);
ss.update();
ServerSettings ss = new ServerSettings(instance);
serverSettings.put(String.valueOf(instance), ss);
ss.update();
return true;
}
return true;
});
serversCategory.setOrderingAsAdded(false);
@ -311,18 +285,8 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
}
private void scheduleBackup() {
try {
Class managerClass = Class.forName("android.app.backup.BackupManager");
Constructor managerConstructor = managerClass.getConstructor(Context.class);
Object manager = managerConstructor.newInstance(context);
Method m = managerClass.getMethod("dataChanged");
m.invoke(manager);
} catch(ClassNotFoundException e) {
Log.e(TAG, "No backup manager found");
} catch(Throwable t) {
Log.e(TAG, "Scheduling backup failed " + t);
t.printStackTrace();
}
BackupManager backupManager = new BackupManager(context);
backupManager.dataChanged();
}
private void update() {
@ -330,11 +294,11 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
return;
}
if(theme != null) {
if (theme != null) {
theme.setSummary(theme.getEntry());
}
if(cacheSize != null) {
if (cacheSize != null) {
maxBitrateWifi.setSummary(maxBitrateWifi.getEntry());
maxBitrateMobile.setSummary(maxBitrateMobile.getEntry());
networkTimeout.setSummary(networkTimeout.getEntry());
@ -343,24 +307,24 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
preloadCountMobile.setSummary(preloadCountMobile.getEntry());
try {
if(megabyteFromat == null) {
if (megabyteFromat == null) {
megabyteFromat = new DecimalFormat(getResources().getString(R.string.util_bytes_format_megabyte));
}
cacheSize.setSummary(megabyteFromat.format((double) Integer.parseInt(cacheSize.getText())).replace(".00", ""));
} catch(Exception e) {
} catch (Exception e) {
Log.e(TAG, "Failed to format cache size", e);
cacheSize.setSummary(cacheSize.getText());
}
}
if(keepPlayedCount != null) {
if (keepPlayedCount != null) {
keepPlayedCount.setSummary(keepPlayedCount.getEntry());
tempLoss.setSummary(tempLoss.getEntry());
pauseDisconnect.setSummary(pauseDisconnect.getEntry());
songPressAction.setSummary(songPressAction.getEntry());
if(replayGain.isChecked()) {
if (replayGain.isChecked()) {
replayGainType.setEnabled(true);
replayGainBump.setEnabled(true);
replayGainUntagged.setEnabled(true);
@ -372,18 +336,18 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
replayGainType.setSummary(replayGainType.getEntry());
}
if(syncEnabled != null) {
if (syncEnabled != null) {
syncInterval.setSummary(syncInterval.getEntry());
if(syncEnabled.isChecked()) {
if(!syncInterval.isEnabled()) {
if (syncEnabled.isChecked()) {
if (!syncInterval.isEnabled()) {
syncInterval.setEnabled(true);
syncWifi.setEnabled(true);
syncNotification.setEnabled(true);
syncMostRecent.setEnabled(true);
}
} else {
if(syncInterval.isEnabled()) {
if (syncInterval.isEnabled()) {
syncInterval.setEnabled(false);
syncWifi.setEnabled(false);
syncNotification.setEnabled(false);
@ -396,9 +360,10 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
ss.update();
}
}
public void checkForRemoved() {
private void checkForRemoved() {
for (ServerSettings ss : serverSettings.values()) {
if(!ss.update()) {
if (!ss.update()) {
serversCategory.removePreference(ss.getScreen());
serverCount--;
}
@ -410,18 +375,15 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
screen.setKey(Constants.PREFERENCES_KEY_SERVER_KEY + instance);
screen.setOrder(instance);
screen.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
SettingsFragment newFragment = new SettingsFragment();
screen.setOnPreferenceClickListener(preference -> {
SettingsFragment newFragment = new SettingsFragment();
Bundle args = new Bundle();
args.putInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, instance);
newFragment.setArguments(args);
Bundle args = new Bundle();
args.putInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, instance);
newFragment.setArguments(args);
replaceFragment(newFragment);
return false;
}
replaceFragment(newFragment);
return false;
});
return screen;
@ -466,12 +428,7 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
Button defaultButton = new Button(getContext());
defaultButton.setText(internalSSIDDisplay);
defaultButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(internalSSID);
}
});
defaultButton.setOnClickListener(v -> editText.setText(internalSSID));
root.addView(defaultButton);
}
};
@ -502,12 +459,9 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
serverOpenBrowser.setKey(Constants.PREFERENCES_KEY_OPEN_BROWSER);
serverOpenBrowser.setPersistent(false);
serverOpenBrowser.setTitle(R.string.settings_server_open_browser);
serverOpenBrowser.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
openInBrowser(instance);
return true;
}
serverOpenBrowser.setOnPreferenceClickListener(preference -> {
openInBrowser(instance);
return true;
});
Preference serverRemoveServerPreference = new Preference(context);
@ -515,53 +469,44 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
serverRemoveServerPreference.setPersistent(false);
serverRemoveServerPreference.setTitle(R.string.settings_servers_remove);
serverRemoveServerPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Util.confirmDialog(context, R.string.common_delete, screen.getTitle().toString(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Reset values to null so when we ask for them again they are new
serverNamePreference.setText(null);
serverUrlPreference.setText(null);
serverUsernamePreference.setText(null);
serverPasswordPreference.setText(null);
serverRemoveServerPreference.setOnPreferenceClickListener(preference -> {
Util.confirmDialog(context, R.string.common_delete, screen.getTitle().toString(), (dialog, which) -> {
// Reset values to null so when we ask for them again they are new
serverNamePreference.setText(null);
serverUrlPreference.setText(null);
serverUsernamePreference.setText(null);
serverPasswordPreference.setText(null);
// Don't use Util.getActiveServer since it is 0 if offline
int activeServer = Util.getPreferences(context).getInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, 1);
for (int i = instance; i <= serverCount; i++) {
Util.removeInstanceName(context, i, activeServer);
}
// Don't use Util.getActiveServer since it is 0 if offline
int activeServer = Util.getPreferences(context).getInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, 1);
for (int i = instance; i <= serverCount; i++) {
Util.removeInstanceName(context, i, activeServer);
}
serverCount--;
SharedPreferences.Editor editor = settings.edit();
editor.putInt(Constants.PREFERENCES_KEY_SERVER_COUNT, serverCount);
editor.apply();
serverCount--;
SharedPreferences.Editor editor = settings.edit();
editor.putInt(Constants.PREFERENCES_KEY_SERVER_COUNT, serverCount);
editor.apply();
removeCurrent();
removeCurrent();
SubsonicFragment parentFragment = context.getCurrentFragment();
if(parentFragment instanceof SettingsFragment) {
SettingsFragment serverSelectionFragment = (SettingsFragment) parentFragment;
serverSelectionFragment.checkForRemoved();
}
}
});
SubsonicFragment parentFragment = context.getCurrentFragment();
if (parentFragment instanceof SettingsFragment) {
SettingsFragment serverSelectionFragment = (SettingsFragment) parentFragment;
serverSelectionFragment.checkForRemoved();
}
});
return true;
}
return true;
});
Preference serverTestConnectionPreference = new Preference(context);
serverTestConnectionPreference.setKey(Constants.PREFERENCES_KEY_TEST_CONNECTION + instance);
serverTestConnectionPreference.setPersistent(false);
serverTestConnectionPreference.setTitle(R.string.settings_test_connection_title);
serverTestConnectionPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
testConnection(instance);
return false;
}
serverTestConnectionPreference.setOnPreferenceClickListener(preference -> {
testConnection(instance);
return false;
});
screen.addPreference(serverNamePreference);
@ -585,22 +530,22 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
if (!nomediaDir.createNewFile()) {
Log.w(TAG, "Failed to create " + nomediaDir);
}
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Failed to create " + nomediaDir, e);
}
try {
if(!musicNoMedia.createNewFile()) {
if (!musicNoMedia.createNewFile()) {
Log.w(TAG, "Failed to create " + musicNoMedia);
}
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Failed to create " + musicNoMedia, e);
}
} else if (!hide && nomediaDir.exists()) {
if (!nomediaDir.delete()) {
Log.w(TAG, "Failed to delete " + nomediaDir);
}
if(!musicNoMedia.delete()) {
if (!musicNoMedia.delete()) {
Log.w(TAG, "Failed to delete " + musicNoMedia);
}
}
@ -628,7 +573,7 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
editor.putString(Constants.PREFERENCES_KEY_CACHE_LOCATION, defaultPath);
editor.apply();
if(cacheLocation != null) {
if (cacheLocation != null) {
cacheLocation.setSummary(defaultPath);
cacheLocation.setText(defaultPath);
}
@ -646,7 +591,7 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
@Override
protected Boolean doInBackground() throws Throwable {
updateProgress(R.string.settings_testing_connection);
updateProgress();
previousInstance = Util.getActiveServer(context);
testingConnection = true;
@ -685,8 +630,8 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
private void openInBrowser(final int instance) {
SharedPreferences prefs = Util.getPreferences(context);
String url = prefs.getString(Constants.PREFERENCES_KEY_SERVER_URL + instance, null);
if(url == null) {
new ErrorDialog(context, R.string.settings_invalid_url, false);
if (url == null) {
new ErrorDialog(context, R.string.settings_invalid_url);
return;
}
Uri uriServer = Uri.parse(url);
@ -696,13 +641,13 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
}
private class ServerSettings {
private int instance;
private EditTextPreference serverName;
private EditTextPreference serverUrl;
private EditTextPreference serverLocalNetworkSSID;
private EditTextPreference serverInternalUrl;
private EditTextPreference username;
private PreferenceScreen screen;
private final int instance;
private final EditTextPreference serverName;
private final EditTextPreference serverUrl;
private final EditTextPreference serverLocalNetworkSSID;
private final EditTextPreference serverInternalUrl;
private final EditTextPreference username;
private final PreferenceScreen screen;
private ServerSettings(int instance) {
this.instance = instance;
@ -713,55 +658,46 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
serverInternalUrl = (EditTextPreference) SettingsFragment.this.findPreference(Constants.PREFERENCES_KEY_SERVER_INTERNAL_URL + instance);
username = (EditTextPreference) SettingsFragment.this.findPreference(Constants.PREFERENCES_KEY_USERNAME + instance);
if(serverName != null) {
serverUrl.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
try {
String url = (String) value;
new URL(url);
if (url.contains(" ") || url.contains("@") || url.contains("_")) {
throw new Exception();
}
} catch (Exception x) {
new ErrorDialog(context, R.string.settings_invalid_url, false);
return false;
if (serverName != null) {
serverUrl.setOnPreferenceChangeListener((preference, value) -> {
try {
String url = (String) value;
new URL(url);
if (url.contains(" ") || url.contains("@") || url.contains("_")) {
throw new Exception();
}
return true;
} catch (Exception x) {
new ErrorDialog(context, R.string.settings_invalid_url);
return false;
}
return true;
});
serverInternalUrl.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
try {
String url = (String) value;
// Allow blank internal IP address
if ("".equals(url) || url == null) {
return true;
}
new URL(url);
if (url.contains(" ") || url.contains("@") || url.contains("_")) {
throw new Exception();
}
} catch (Exception x) {
new ErrorDialog(context, R.string.settings_invalid_url, false);
return false;
serverInternalUrl.setOnPreferenceChangeListener((preference, value) -> {
try {
String url = (String) value;
// Allow blank internal IP address
if ("".equals(url) || url == null) {
return true;
}
return true;
new URL(url);
if (url.contains(" ") || url.contains("@") || url.contains("_")) {
throw new Exception();
}
} catch (Exception x) {
new ErrorDialog(context, R.string.settings_invalid_url);
return false;
}
return true;
});
username.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String username = (String) value;
if (username == null || !username.equals(username.trim())) {
new ErrorDialog(context, R.string.settings_invalid_username, false);
return false;
}
return true;
username.setOnPreferenceChangeListener((preference, value) -> {
String username = (String) value;
if (username == null || !username.equals(username.trim())) {
new ErrorDialog(context, R.string.settings_invalid_username);
return false;
}
return true;
});
}
}
@ -773,7 +709,7 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared
public boolean update() {
SharedPreferences prefs = Util.getPreferences(context);
if(prefs.contains(Constants.PREFERENCES_KEY_SERVER_NAME + instance)) {
if (prefs.contains(Constants.PREFERENCES_KEY_SERVER_NAME + instance)) {
if (serverName != null) {
serverName.setSummary(serverName.getText());
serverUrl.setSummary(serverUrl.getText());

View File

@ -24,12 +24,6 @@ import android.content.ContentValues;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.util.Log;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.Artist;
@ -40,13 +34,16 @@ import net.nullsum.audinaut.service.MusicService;
import net.nullsum.audinaut.service.MusicServiceFactory;
import net.nullsum.audinaut.util.Util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Provides search suggestions based on recent searches.
*
* @author Sindre Mehus
*/
public class AudinautSearchProvider extends ContentProvider {
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",
@ -58,7 +55,7 @@ public class AudinautSearchProvider extends ContentProvider {
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
if(selectionArgs[0].isEmpty()) {
if (selectionArgs[0].isEmpty()) {
return null;
}
@ -87,14 +84,14 @@ public class AudinautSearchProvider extends ContentProvider {
}
// Add all results into one pot
List<Object> results = new ArrayList<Object>();
List<Object> results = new ArrayList<>();
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) {
for (Object obj : results) {
if (obj instanceof Artist) {
Artist artist = (Artist) obj;
artist.setCloseness(Util.getStringDistance(query, artist.getName()));
} else {
@ -104,71 +101,64 @@ public class AudinautSearchProvider extends ContentProvider {
}
// 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();
}
Collections.sort(results, (lhs, 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) {
if (left == right) {
if (leftArtist && rightArtist) {
return 0;
} else if (leftArtist) {
return -1;
} else if (rightArtist) {
return 1;
} else {
return -1;
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) {
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()) {
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();
}
id = entry.getAlbumId();
String artistDisplay;
if(entry.getArtist() == null) {
if(entry.getAlbum() != null) {
if (entry.getArtist() == null) {
if (entry.getAlbum() != null) {
artistDisplay = entry.getAlbumDisplay();
} else {
artistDisplay = "";
}
} else if(entry.getAlbum() != null) {
} else if (entry.getAlbum() != null) {
artistDisplay = entry.getArtist() + " - " + entry.getAlbumDisplay();
} else {
artistDisplay = entry.getArtist();

View File

@ -24,8 +24,8 @@ import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
@ -38,6 +38,7 @@ import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.activity.SubsonicActivity;
import net.nullsum.audinaut.activity.SubsonicFragmentActivity;
@ -66,16 +67,16 @@ public class AudinautWidgetProvider extends AppWidgetProvider {
private static AudinautWidget4x4 instance4x4;
public static synchronized void notifyInstances(Context context, DownloadService service, boolean playing) {
if(instance4x1 == null) {
if (instance4x1 == null) {
instance4x1 = new AudinautWidget4x1();
}
if(instance4x2 == null) {
if (instance4x2 == null) {
instance4x2 = new AudinautWidget4x2();
}
if(instance4x3 == null) {
if (instance4x3 == null) {
instance4x3 = new AudinautWidget4x3();
}
if(instance4x4 == null) {
if (instance4x4 == null) {
instance4x4 = new AudinautWidget4x4();
}
@ -85,165 +86,6 @@ public class AudinautWidgetProvider extends AppWidgetProvider {
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);
}
protected int getLayout() {
return 0;
}
/**
* Initialize given widgets to default state, where we launch Subsonic on default click
* and hide actions if service not running.
*/
private void defaultAppWidget(Context context, int[] appWidgetIds) {
final Resources res = context.getResources();
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, "");
}
linkButtons(context, views, false);
performUpdate(context, null, appWidgetIds, false);
}
private void pushUpdate(Context context, int[] appWidgetIds, RemoteViews views) {
// Update specific list of appWidgetIds if given, otherwise default to all
final AppWidgetManager manager = AppWidgetManager.getInstance(context);
if (appWidgetIds != null) {
manager.updateAppWidget(appWidgetIds, views);
} else {
manager.updateAppWidget(new ComponentName(context, this.getClass()), views);
}
}
/**
* Handle a change notification coming over from {@link DownloadService}
*/
public void notifyChange(Context context, DownloadService service, boolean playing) {
if (hasInstances(context)) {
performUpdate(context, service, null, playing);
}
}
/**
* Check against {@link AppWidgetManager} if there are any instances of this widget.
*/
private boolean hasInstances(Context context) {
AppWidgetManager manager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = manager.getAppWidgetIds(new ComponentName(context, getClass()));
return (appWidgetIds.length > 0);
}
/**
* Update all active widget instances by pushing changes
*/
private void performUpdate(Context context, DownloadService service, int[] appWidgetIds, boolean playing) {
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);
}
}
// Get Entry from current playing DownloadFile
MusicDirectory.Entry currentPlaying = null;
if(service == null) {
// Deserialize from playling list to setup
try {
PlayerQueue state = FileUtil.deserialize(context, DownloadServiceLifecycleSupport.FILENAME_DOWNLOADS_SER, PlayerQueue.class);
if (state != null && state.currentPlayingIndex != -1) {
currentPlaying = state.songs.get(state.currentPlayingIndex);
}
} catch(Exception e) {
Log.e(TAG, "Failed to grab current playing", e);
}
} else {
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 errorState = null;
// Show error message?
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_SHARED) ||
status.equals(Environment.MEDIA_UNMOUNTED)) {
errorState = res.getText(R.string.widget_sdcard_busy);
} else if (status.equals(Environment.MEDIA_REMOVED)) {
errorState = res.getText(R.string.widget_sdcard_missing);
} else if (currentPlaying == null) {
errorState = res.getText(R.string.widget_initial_text);
}
if (errorState != null) {
// Show error state to user
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);
}
} 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);
}
}
// Set correct drawable for pause state
if (playing) {
views.setImageViewResource(R.id.control_play, R.drawable.media_pause_dark);
} else {
views.setImageViewResource(R.id.control_play, R.drawable.media_start_dark);
}
// Set the cover art
try {
boolean large = false;
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) {
// Set default cover art
views.setImageViewResource(R.id.appwidget_coverart, R.drawable.appwidget_art_unknown);
} else {
bitmap = getRoundedCornerBitmap(bitmap);
views.setImageViewBitmap(R.id.appwidget_coverart, bitmap);
}
} catch (Exception x) {
Log.e(TAG, "Failed to load cover art", x);
views.setImageViewResource(R.id.appwidget_coverart, R.drawable.appwidget_art_unknown);
}
// Link actions buttons to intents
linkButtons(context, views, currentPlaying != null);
pushUpdate(context, appWidgetIds, views);
}
/**
* Round the corners of a bitmap for the cover art image
*/
@ -270,12 +112,169 @@ public class AudinautWidgetProvider extends AppWidgetProvider {
return output;
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
defaultAppWidget(context, appWidgetIds);
}
@Override
public void onEnabled(Context context) {
notifyInstances(context, DownloadService.getInstance(), false);
}
int getLayout() {
return 0;
}
/**
* Initialize given widgets to default state, where we launch Subsonic on default click
* and hide actions if service not running.
*/
private void defaultAppWidget(Context context, int[] appWidgetIds) {
final Resources res = context.getResources();
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, "");
}
linkButtons(context, views);
performUpdate(context, null, appWidgetIds, false);
}
private void pushUpdate(Context context, int[] appWidgetIds, RemoteViews views) {
// Update specific list of appWidgetIds if given, otherwise default to all
final AppWidgetManager manager = AppWidgetManager.getInstance(context);
if (appWidgetIds != null) {
manager.updateAppWidget(appWidgetIds, views);
} else {
manager.updateAppWidget(new ComponentName(context, this.getClass()), views);
}
}
/**
* Handle a change notification coming over from {@link DownloadService}
*/
void notifyChange(Context context, DownloadService service, boolean playing) {
if (hasInstances(context)) {
performUpdate(context, service, null, playing);
}
}
/**
* Check against {@link AppWidgetManager} if there are any instances of this widget.
*/
private boolean hasInstances(Context context) {
AppWidgetManager manager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = manager.getAppWidgetIds(new ComponentName(context, getClass()));
return (appWidgetIds.length > 0);
}
/**
* Update all active widget instances by pushing changes
*/
private void performUpdate(Context context, DownloadService service, int[] appWidgetIds, boolean playing) {
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);
}
}
// Get Entry from current playing DownloadFile
MusicDirectory.Entry currentPlaying = null;
if (service == null) {
// Deserialize from playling list to setup
try {
PlayerQueue state = FileUtil.deserialize(context, DownloadServiceLifecycleSupport.FILENAME_DOWNLOADS_SER, PlayerQueue.class);
if (state != null && state.currentPlayingIndex != -1) {
currentPlaying = state.songs.get(state.currentPlayingIndex);
}
} catch (Exception e) {
Log.e(TAG, "Failed to grab current playing", e);
}
} else {
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 errorState = null;
// Show error message?
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_SHARED) ||
status.equals(Environment.MEDIA_UNMOUNTED)) {
errorState = res.getText(R.string.widget_sdcard_busy);
} else if (status.equals(Environment.MEDIA_REMOVED)) {
errorState = res.getText(R.string.widget_sdcard_missing);
} else if (currentPlaying == null) {
errorState = res.getText(R.string.widget_initial_text);
}
if (errorState != null) {
// Show error state to user
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);
}
} 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);
}
}
// Set correct drawable for pause state
if (playing) {
views.setImageViewResource(R.id.control_play, R.drawable.media_pause_dark);
} else {
views.setImageViewResource(R.id.control_play, R.drawable.media_start_dark);
}
// Set the cover art
try {
boolean large = false;
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) {
// Set default cover art
views.setImageViewResource(R.id.appwidget_coverart, R.drawable.appwidget_art_unknown);
} else {
bitmap = getRoundedCornerBitmap(bitmap);
views.setImageViewBitmap(R.id.appwidget_coverart, bitmap);
}
} catch (Exception x) {
Log.e(TAG, "Failed to load cover art", x);
views.setImageViewResource(R.id.appwidget_coverart, R.drawable.appwidget_art_unknown);
}
// Link actions buttons to intents
linkButtons(context, views);
pushUpdate(context, appWidgetIds, views);
}
/**
* Link up various button actions using {@link PendingIntent}.
*
* @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) {
private void linkButtons(Context context, RemoteViews views) {
Intent intent = new Intent(context, SubsonicFragmentActivity.class);
intent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD, true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

View File

@ -4,11 +4,12 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
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 final String TAG = A2dpIntentReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
@ -16,7 +17,7 @@ public class A2dpIntentReceiver extends BroadcastReceiver {
DownloadService downloadService = DownloadService.getInstance();
if (downloadService != null){
if (downloadService != null) {
Intent avrcpIntent = new Intent(PLAYSTATUS_RESPONSE);
@ -24,7 +25,7 @@ public class A2dpIntentReceiver extends BroadcastReceiver {
avrcpIntent.putExtra("position", (long) downloadService.getPlayerPosition());
avrcpIntent.putExtra("ListSize", (long) downloadService.getSongs().size());
switch (downloadService.getPlayerState()){
switch (downloadService.getPlayerState()) {
case STARTED:
avrcpIntent.putExtra("playing", true);
break;

View File

@ -20,7 +20,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.util.Log;
import net.nullsum.audinaut.domain.PlayerState;
import net.nullsum.audinaut.service.DownloadService;
@ -28,24 +27,23 @@ 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();
@Override
public void onReceive(Context context, Intent intent) {
DownloadService downloadService = DownloadService.getInstance();
// Don't do anything if downloadService is not started
if(downloadService == null) {
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

@ -22,10 +22,10 @@ import android.content.Intent;
import net.nullsum.audinaut.service.HeadphoneListenerService;
import net.nullsum.audinaut.util.Util;
public class BootReceiver extends BroadcastReceiver {
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(Util.shouldStartOnHeadphones(context)) {
if (Util.shouldStartOnHeadphones(context)) {
Intent serviceIntent = new Intent();
serviceIntent.setClassName(context.getPackageName(), HeadphoneListenerService.class.getName());
context.startService(serviceIntent);

View File

@ -18,19 +18,17 @@ package net.nullsum.audinaut.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
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();
@Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_HEADSET_PLUG.equals(intent.getAction())) {
if (Intent.ACTION_HEADSET_PLUG.equals(intent.getAction())) {
int headphoneState = intent.getIntExtra("state", -1);
if(headphoneState == 1 && Util.shouldStartOnHeadphones(context)) {
if (headphoneState == 1 && Util.shouldStartOnHeadphones(context)) {
Intent start = new Intent(context, DownloadService.class);
start.setAction(DownloadService.START_PLAY);
context.startService(start);

View File

@ -36,8 +36,8 @@ 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)) {
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;
}

View File

@ -19,17 +19,15 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
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();
@Override
public void onReceive(Context context, Intent intent) {
if(intent.hasExtra(Constants.TASKER_EXTRA_BUNDLE)) {
if (intent.hasExtra(Constants.TASKER_EXTRA_BUNDLE)) {
Bundle data = intent.getBundleExtra(Constants.TASKER_EXTRA_BUNDLE);
Boolean startShuffled = data.getBoolean(Constants.INTENT_EXTRA_NAME_SHUFFLE);

View File

@ -18,17 +18,6 @@
*/
package net.nullsum.audinaut.service;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.TimeUnit;
import okhttp3.Response;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
@ -36,41 +25,43 @@ import android.util.Log;
import net.nullsum.audinaut.domain.Artist;
import net.nullsum.audinaut.domain.Genre;
import net.nullsum.audinaut.domain.Indexes;
import net.nullsum.audinaut.domain.PlayerQueue;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.domain.MusicFolder;
import net.nullsum.audinaut.domain.Playlist;
import net.nullsum.audinaut.domain.SearchCritera;
import net.nullsum.audinaut.domain.SearchResult;
import net.nullsum.audinaut.domain.User;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.util.FileUtil;
import net.nullsum.audinaut.util.ProgressListener;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.util.SongDBHandler;
import net.nullsum.audinaut.util.TimeLimitedCache;
import net.nullsum.audinaut.util.FileUtil;
import net.nullsum.audinaut.util.Util;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import okhttp3.Response;
import static net.nullsum.audinaut.domain.MusicDirectory.Entry;
/**
* @author Sindre Mehus
*/
public class CachedMusicService implements MusicService {
public static final int CACHE_UPDATE_LIST = 1;
private static final int CACHE_UPDATE_METADATA = 2;
private static final String TAG = CachedMusicService.class.getSimpleName();
private static final int MUSIC_DIR_CACHE_SIZE = 20;
private static final int TTL_MUSIC_DIR = 5 * 60; // Five minutes
public static final int CACHE_UPDATE_LIST = 1;
public static final int CACHE_UPDATE_METADATA = 2;
private static final int CACHED_LAST_FM = 24 * 60;
private final RESTMusicService musicService;
private final TimeLimitedCache<Indexes> cachedIndexes = new TimeLimitedCache<Indexes>(60 * 60, TimeUnit.SECONDS);
private final TimeLimitedCache<List<Playlist>> cachedPlaylists = new TimeLimitedCache<List<Playlist>>(3600, TimeUnit.SECONDS);
private final TimeLimitedCache<List<MusicFolder>> cachedMusicFolders = new TimeLimitedCache<List<MusicFolder>>(10 * 3600, TimeUnit.SECONDS);
private final TimeLimitedCache<Indexes> cachedIndexes = new TimeLimitedCache<>(60 * 60);
private final TimeLimitedCache<List<Playlist>> cachedPlaylists = new TimeLimitedCache<>(3600);
private final TimeLimitedCache<List<MusicFolder>> cachedMusicFolders = new TimeLimitedCache<>(10 * 3600);
private String restUrl;
private String musicFolderId;
private boolean isTagBrowsing = false;
public CachedMusicService(RESTMusicService musicService) {
this.musicService = musicService;
@ -90,13 +81,13 @@ public class CachedMusicService implements MusicService {
}
List<MusicFolder> result = cachedMusicFolders.get();
if (result == null) {
if(!refresh) {
if (!refresh) {
result = FileUtil.deserialize(context, getCacheName(context, "musicFolders"), ArrayList.class);
}
if(result == null) {
if (result == null) {
result = musicService.getMusicFolders(refresh, context, progressListener);
FileUtil.serialize(context, new ArrayList<MusicFolder>(result), getCacheName(context, "musicFolders"));
FileUtil.serialize(context, new ArrayList<>(result), getCacheName(context, "musicFolders"));
}
MusicFolder.sort(result);
@ -114,13 +105,13 @@ public class CachedMusicService implements MusicService {
}
Indexes result = cachedIndexes.get();
if (result == null) {
String name = Util.isTagBrowsing(context, musicService.getInstance(context)) ? "artists" : "indexes";
String name = "artists";
name = getCacheName(context, name, musicFolderId);
if(!refresh) {
if (!refresh) {
result = FileUtil.deserialize(context, name, Indexes.class);
}
if(result == null) {
if (result == null) {
result = musicService.getIndexes(musicFolderId, refresh, context, progressListener);
FileUtil.serialize(context, result, name);
}
@ -133,7 +124,7 @@ public class CachedMusicService implements MusicService {
public MusicDirectory getMusicDirectory(final String id, final String name, final boolean refresh, final Context context, final ProgressListener progressListener) throws Exception {
MusicDirectory dir = null;
final MusicDirectory cached = FileUtil.deserialize(context, getCacheName(context, "directory", id), MusicDirectory.class);
if(!refresh && cached != null) {
if (!refresh && cached != null) {
dir = cached;
new SilentBackgroundTask<Void>(context) {
@ -153,11 +144,11 @@ public class CachedMusicService implements MusicService {
// Update which entries exist
@Override
public void done(Void result) {
if(progressListener != null) {
if(cached.updateEntriesList(context, musicService.getInstance(context), refreshed)) {
if (progressListener != null) {
if (cached.updateEntriesList(context, refreshed)) {
progressListener.updateCache(CACHE_UPDATE_LIST);
}
if(metadataUpdated) {
if (metadataUpdated) {
progressListener.updateCache(CACHE_UPDATE_METADATA);
}
}
@ -170,7 +161,7 @@ public class CachedMusicService implements MusicService {
}.execute();
}
if(dir == null) {
if (dir == null) {
dir = musicService.getMusicDirectory(id, name, refresh, context, progressListener);
updateAllSongs(context, dir);
FileUtil.serialize(context, dir, getCacheName(context, "directory", id));
@ -178,7 +169,7 @@ public class CachedMusicService implements MusicService {
// If a cached copy exists to check against, look for removes
deleteRemovedEntries(context, dir, cached);
}
dir.sortChildren(context, musicService.getInstance(context));
dir.sortChildren(context);
return dir;
}
@ -187,7 +178,7 @@ public class CachedMusicService implements MusicService {
public MusicDirectory getArtist(final String id, final String name, final boolean refresh, final Context context, final ProgressListener progressListener) throws Exception {
MusicDirectory dir = null;
final MusicDirectory cached = FileUtil.deserialize(context, getCacheName(context, "artist", id), MusicDirectory.class);
if(!refresh && cached != null) {
if (!refresh && cached != null) {
dir = cached;
new SilentBackgroundTask<Void>(context) {
@ -205,8 +196,8 @@ public class CachedMusicService implements MusicService {
// Update which entries exist
@Override
public void done(Void result) {
if(progressListener != null) {
if(cached.updateEntriesList(context, musicService.getInstance(context), refreshed)) {
if (progressListener != null) {
if (cached.updateEntriesList(context, refreshed)) {
progressListener.updateCache(CACHE_UPDATE_LIST);
}
}
@ -219,14 +210,14 @@ public class CachedMusicService implements MusicService {
}.execute();
}
if(dir == null) {
if (dir == null) {
dir = musicService.getArtist(id, name, refresh, context, progressListener);
FileUtil.serialize(context, dir, getCacheName(context, "artist", id));
// If a cached copy exists to check against, look for removes
deleteRemovedEntries(context, dir, cached);
}
dir.sortChildren(context, musicService.getInstance(context));
dir.sortChildren(context);
return dir;
}
@ -235,7 +226,7 @@ public class CachedMusicService implements MusicService {
public MusicDirectory getAlbum(final String id, final String name, final boolean refresh, final Context context, final ProgressListener progressListener) throws Exception {
MusicDirectory dir = null;
final MusicDirectory cached = FileUtil.deserialize(context, getCacheName(context, "album", id), MusicDirectory.class);
if(!refresh && cached != null) {
if (!refresh && cached != null) {
dir = cached;
new SilentBackgroundTask<Void>(context) {
@ -255,11 +246,11 @@ public class CachedMusicService implements MusicService {
// Update which entries exist
@Override
public void done(Void result) {
if(progressListener != null) {
if(cached.updateEntriesList(context, musicService.getInstance(context), refreshed)) {
if (progressListener != null) {
if (cached.updateEntriesList(context, refreshed)) {
progressListener.updateCache(CACHE_UPDATE_LIST);
}
if(metadataUpdated) {
if (metadataUpdated) {
progressListener.updateCache(CACHE_UPDATE_METADATA);
}
}
@ -272,7 +263,7 @@ public class CachedMusicService implements MusicService {
}.execute();
}
if(dir == null) {
if (dir == null) {
dir = musicService.getAlbum(id, name, refresh, context, progressListener);
updateAllSongs(context, dir);
FileUtil.serialize(context, dir, getCacheName(context, "album", id));
@ -280,7 +271,7 @@ public class CachedMusicService implements MusicService {
// If a cached copy exists to check against, look for removes
deleteRemovedEntries(context, dir, cached);
}
dir.sortChildren(context, musicService.getInstance(context));
dir.sortChildren(context);
return dir;
}
@ -294,16 +285,16 @@ public class CachedMusicService implements MusicService {
public MusicDirectory getPlaylist(boolean refresh, String id, String name, Context context, ProgressListener progressListener) throws Exception {
MusicDirectory dir = null;
MusicDirectory cachedPlaylist = FileUtil.deserialize(context, getCacheName(context, "playlist", id), MusicDirectory.class);
if(!refresh) {
if (!refresh) {
dir = cachedPlaylist;
}
if(dir == null) {
if (dir == null) {
dir = musicService.getPlaylist(refresh, id, name, context, progressListener);
updateAllSongs(context, dir);
FileUtil.serialize(context, dir, getCacheName(context, "playlist", id));
File playlistFile = FileUtil.getPlaylistFile(context, Util.getServerName(context, musicService.getInstance(context)), dir.getName());
if(cachedPlaylist == null || !playlistFile.exists() || !cachedPlaylist.getChildren().equals(dir.getChildren())) {
if (cachedPlaylist == null || !playlistFile.exists() || !cachedPlaylist.getChildren().equals(dir.getChildren())) {
FileUtil.writePlaylistFile(context, playlistFile, dir);
}
}
@ -315,13 +306,13 @@ public class CachedMusicService implements MusicService {
checkSettingsChanged(context);
List<Playlist> result = refresh ? null : cachedPlaylists.get();
if (result == null) {
if(!refresh) {
if (!refresh) {
result = FileUtil.deserialize(context, getCacheName(context, "playlist"), ArrayList.class);
}
if(result == null) {
if (result == null) {
result = musicService.getPlaylists(refresh, context, progressListener);
FileUtil.serialize(context, new ArrayList<Playlist>(result), getCacheName(context, "playlist"));
FileUtil.serialize(context, new ArrayList<>(result), getCacheName(context, "playlist"));
}
cachedPlaylists.set(result);
}
@ -381,9 +372,9 @@ public class CachedMusicService implements MusicService {
boolean supposedToUnpin = false;
// Remove in reverse order so indexes are still correct as we iterate through
for(ListIterator<Integer> iterator = toRemove.listIterator(toRemove.size()); iterator.hasPrevious(); ) {
for (ListIterator<Integer> iterator = toRemove.listIterator(toRemove.size()); iterator.hasPrevious(); ) {
int index = iterator.previous();
if(supposedToUnpin) {
if (supposedToUnpin) {
Entry entry = objects.get(index);
DownloadFile file = new DownloadFile(context, entry, true);
file.unpin();
@ -439,24 +430,17 @@ public class CachedMusicService implements MusicService {
String recentlyAddedFile = getCacheName(context, type);
ArrayList<String> recents = FileUtil.deserialize(context, recentlyAddedFile, ArrayList.class);
if (recents == null) {
recents = new ArrayList<String>();
recents = new ArrayList<>();
}
// Add any new items
final int instance = musicService.getInstance(context);
isTagBrowsing = Util.isTagBrowsing(context, instance);
for (final Entry album : dir.getChildren()) {
if (!recents.contains(album.getId())) {
recents.add(album.getId());
String cacheName, parent;
if (isTagBrowsing) {
cacheName = "artist";
parent = album.getArtistId();
} else {
cacheName = "directory";
parent = album.getParent();
}
cacheName = "artist";
parent = album.getArtistId();
// Add album to artist
if (parent != null) {
@ -492,7 +476,7 @@ public class CachedMusicService implements MusicService {
artist.setId(album.getId());
artist.setName(album.getTitle());
new IndexesUpdater(context, isTagBrowsing ? "artists" : "indexes") {
new IndexesUpdater(context) {
private boolean changed = false;
@Override
@ -530,17 +514,17 @@ public class CachedMusicService implements MusicService {
FileUtil.serialize(context, dir, getCacheName(context, type, Integer.toString(offset)));
return dir;
} catch(IOException e) {
} catch (IOException e) {
Log.w(TAG, "Failed to refresh album list: ", e);
if(refresh) {
if (refresh) {
throw e;
}
MusicDirectory dir = FileUtil.deserialize(context, getCacheName(context, type, Integer.toString(offset)), MusicDirectory.class);
if(dir == null) {
if (dir == null) {
// If we are at start and no cache, throw error higher
if(offset == 0) {
if (offset == 0) {
throw e;
} else {
// Otherwise just pretend we are at the end of the list
@ -558,17 +542,17 @@ public class CachedMusicService implements MusicService {
MusicDirectory dir = musicService.getAlbumList(type, extra, size, offset, refresh, context, progressListener);
FileUtil.serialize(context, dir, getCacheName(context, type + extra, Integer.toString(offset)));
return dir;
} catch(IOException e) {
} catch (IOException e) {
Log.w(TAG, "Failed to refresh album list: ", e);
if(refresh) {
if (refresh) {
throw e;
}
MusicDirectory dir = FileUtil.deserialize(context, getCacheName(context, type + extra, Integer.toString(offset)), MusicDirectory.class);
if(dir == null) {
if (dir == null) {
// If we are at start and no cache, throw error higher
if(offset == 0) {
if (offset == 0) {
throw e;
} else {
// Otherwise just pretend we are at the end of the list
@ -585,11 +569,6 @@ public class CachedMusicService implements MusicService {
return musicService.getSongList(type, size, offset, context, progressListener);
}
@Override
public MusicDirectory getRandomSongs(int size, String artistId, Context context, ProgressListener progressListener) throws Exception {
return musicService.getRandomSongs(size, artistId, context, progressListener);
}
@Override
public MusicDirectory getRandomSongs(int size, String folder, String genre, String startYear, String endYear, Context context, ProgressListener progressListener) throws Exception {
return musicService.getRandomSongs(size, folder, genre, startYear, endYear, context, progressListener);
@ -609,13 +588,13 @@ public class CachedMusicService implements MusicService {
public List<Genre> getGenres(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
List<Genre> result = null;
if(!refresh) {
if (!refresh) {
result = FileUtil.deserialize(context, getCacheName(context, "genre"), ArrayList.class);
}
if(result == null) {
if (result == null) {
result = musicService.getGenres(refresh, context, progressListener);
FileUtil.serialize(context, new ArrayList<Genre>(result), getCacheName(context, "genre"));
FileUtil.serialize(context, new ArrayList<>(result), getCacheName(context, "genre"));
}
return result;
@ -628,12 +607,12 @@ public class CachedMusicService implements MusicService {
FileUtil.serialize(context, dir, getCacheName(context, "genreSongs", Integer.toString(offset)));
return dir;
} catch(IOException e) {
} catch (IOException e) {
MusicDirectory dir = FileUtil.deserialize(context, getCacheName(context, "genreSongs", Integer.toString(offset)), MusicDirectory.class);
if(dir == null) {
if (dir == null) {
// If we are at start and no cache, throw error higher
if(offset == 0) {
if (offset == 0) {
throw e;
} else {
// Otherwise just pretend we are at the end of the list
@ -652,32 +631,17 @@ public class CachedMusicService implements MusicService {
try {
result = musicService.getUser(refresh, username, context, progressListener);
FileUtil.serialize(context, result, getCacheName(context, "user-" + username));
} catch(Exception e) {
} catch (Exception e) {
// Don't care
}
if(result == null && !refresh) {
if (result == null && !refresh) {
result = FileUtil.deserialize(context, getCacheName(context, "user-" + username), User.class);
}
return result;
}
@Override
public Bitmap getBitmap(String url, int size, Context context, ProgressListener progressListener, SilentBackgroundTask task) throws Exception {
return musicService.getBitmap(url, size, context, progressListener, task);
}
@Override
public void savePlayQueue(List<Entry> songs, Entry currentPlaying, int position, Context context, ProgressListener progressListener) throws Exception {
musicService.savePlayQueue(songs, currentPlaying, position, context, progressListener);
}
@Override
public PlayerQueue getPlayQueue(Context context, ProgressListener progressListener) throws Exception {
return musicService.getPlayQueue(context, progressListener);
}
@Override
public void setInstance(Integer instance) throws Exception {
musicService.setInstance(instance);
@ -687,57 +651,76 @@ public class CachedMusicService implements MusicService {
String s = musicService.getRestUrl(context, null, false) + id;
return name + "-" + s.hashCode() + ".ser";
}
private String getCacheName(Context context, String name) {
String s = musicService.getRestUrl(context, null, false);
return name + "-" + s.hashCode() + ".ser";
}
private void deleteRemovedEntries(Context context, MusicDirectory dir, MusicDirectory cached) {
if(cached != null) {
List<Entry> oldList = new ArrayList<Entry>();
if (cached != null) {
List<Entry> oldList = new ArrayList<>();
oldList.addAll(cached.getChildren());
// Remove all current items from old list
for(Entry entry: dir.getChildren()) {
oldList.remove(entry);
}
oldList.removeAll(dir.getChildren());
// Anything remaining has been removed from server
MediaStoreService store = new MediaStoreService(context);
for(Entry entry: oldList) {
for (Entry entry : oldList) {
File file = FileUtil.getEntryFile(context, entry);
FileUtil.recursiveDelete(file, store);
}
}
}
private void updateAllSongs(Context context, MusicDirectory dir) {
List<Entry> songs = dir.getSongs();
if (!songs.isEmpty()) {
SongDBHandler.getHandler(context).addSongs(musicService.getInstance(context), songs);
}
}
private void checkSettingsChanged(Context context) {
int instance = musicService.getInstance(context);
String newUrl = musicService.getRestUrl(context, null, false);
if (!Util.equals(newUrl, restUrl)) {
cachedMusicFolders.clear();
cachedIndexes.clear();
cachedPlaylists.clear();
restUrl = newUrl;
}
String newMusicFolderId = Util.getSelectedMusicFolderId(context, instance);
if (!Util.equals(newMusicFolderId, musicFolderId)) {
cachedIndexes.clear();
musicFolderId = newMusicFolderId;
}
}
private abstract class SerializeUpdater<T> {
final Context context;
final String cacheName;
final boolean singleUpdate;
public SerializeUpdater(Context context, String cacheName) {
this(context, cacheName, true);
}
public SerializeUpdater(Context context, String cacheName, boolean singleUpdate) {
public SerializeUpdater(Context context) {
this.context = context;
this.cacheName = getCacheName(context, cacheName);
this.singleUpdate = singleUpdate;
this.cacheName = getCacheName(context, "playlist");
this.singleUpdate = true;
}
public SerializeUpdater(Context context, String cacheName, String id) {
this(context, cacheName, id, true);
}
public SerializeUpdater(Context context, String cacheName, String id, boolean singleUpdate) {
this.context = context;
this.cacheName = getCacheName(context, cacheName, id);
this.singleUpdate = singleUpdate;
this.singleUpdate = true;
}
public ArrayList<T> getArrayList() {
return FileUtil.deserialize(context, cacheName, ArrayList.class);
}
public abstract boolean checkResult(T check);
public abstract void updateResult(List<T> objects, T result);
public void save(ArrayList<T> objects) {
FileUtil.serialize(context, objects, cacheName);
}
@ -746,47 +729,35 @@ public class CachedMusicService implements MusicService {
ArrayList<T> objects = getArrayList();
// Only execute if something to check against
if(objects != null) {
List<T> results = new ArrayList<T>();
for(T check: objects) {
if(checkResult(check)) {
if (objects != null) {
List<T> results = new ArrayList<>();
for (T check : objects) {
if (checkResult(check)) {
results.add(check);
if(singleUpdate) {
if (singleUpdate) {
break;
}
}
}
// Iterate through and update each object matched
for(T result: results) {
for (T result : results) {
updateResult(objects, result);
}
// Only reserialize if at least one match was found
if(results.size() > 0) {
if (results.size() > 0) {
save(objects);
}
}
}
}
private abstract class UserUpdater extends SerializeUpdater<User> {
String username;
public UserUpdater(Context context, String username) {
super(context, "users");
this.username = username;
}
@Override
public boolean checkResult(User check) {
return username.equals(check.getUsername());
}
}
private abstract class PlaylistUpdater extends SerializeUpdater<Playlist> {
String id;
final String id;
public PlaylistUpdater(Context context, String id) {
super(context, "playlist");
super(context);
this.id = id;
}
@ -795,199 +766,45 @@ public class CachedMusicService implements MusicService {
return id.equals(check.getId());
}
}
private abstract class MusicDirectoryUpdater extends SerializeUpdater<Entry> {
protected MusicDirectory musicDirectory;
MusicDirectory musicDirectory;
public MusicDirectoryUpdater(Context context, String cacheName, String id) {
super(context, cacheName, id, true);
}
public MusicDirectoryUpdater(Context context, String cacheName, String id, boolean singleUpdate) {
super(context, cacheName, id, singleUpdate);
super(context, cacheName, id);
}
@Override
public ArrayList<Entry> getArrayList() {
musicDirectory = FileUtil.deserialize(context, cacheName, MusicDirectory.class);
if(musicDirectory != null) {
if (musicDirectory != null) {
return new ArrayList<>(musicDirectory.getChildren());
} else {
return null;
}
}
public void save(ArrayList<Entry> objects) {
musicDirectory.replaceChildren(objects);
FileUtil.serialize(context, musicDirectory, cacheName);
}
}
private abstract class PlaylistDirectoryUpdater {
Context context;
public PlaylistDirectoryUpdater(Context context) {
this.context = context;
}
public abstract boolean checkResult(Entry check);
public abstract void updateResult(Entry result);
public void execute() {
List<Playlist> playlists = FileUtil.deserialize(context, getCacheName(context, "playlist"), ArrayList.class);
if(playlists == null) {
// No playlist list cache, nothing to update!
return;
}
for(Playlist playlist: playlists) {
new MusicDirectoryUpdater(context, "playlist", playlist.getId(), false) {
@Override
public boolean checkResult(Entry check) {
return PlaylistDirectoryUpdater.this.checkResult(check);
}
@Override
public void updateResult(List<Entry> objects, Entry result) {
PlaylistDirectoryUpdater.this.updateResult(result);
}
}.execute();
}
}
}
private abstract class GenericEntryUpdater {
Context context;
List<Entry> entries;
public GenericEntryUpdater(Context context, Entry entry) {
this.context = context;
this.entries = Arrays.asList(entry);
}
public GenericEntryUpdater(Context context, List<Entry> entries) {
this.context = context;
this.entries = entries;
}
public boolean checkResult(Entry entry, Entry check) {
return entry.getId().equals(check.getId());
}
public abstract void updateResult(Entry result);
public void execute() {
String cacheName, parent;
// Make sure it is up to date
isTagBrowsing = Util.isTagBrowsing(context, musicService.getInstance(context));
// Run through each entry, trying to update the directory it is in
final List<Entry> songs = new ArrayList<Entry>();
for(final Entry entry: entries) {
if(isTagBrowsing) {
// If starring album, needs to reference artist instead
if(entry.isDirectory()) {
if(entry.isAlbum()) {
cacheName = "artist";
parent = entry.getArtistId();
} else {
cacheName = "artists";
parent = null;
}
} else {
cacheName = "album";
parent = entry.getAlbumId();
}
} else {
if(entry.isDirectory() && !entry.isAlbum()) {
cacheName = "indexes";
parent = null;
} else {
cacheName = "directory";
parent = entry.getParent();
}
}
// Parent is only null when it is an artist
if(parent == null) {
new IndexesUpdater(context, cacheName) {
@Override
public boolean checkResult(Artist check) {
return GenericEntryUpdater.this.checkResult(entry, new Entry(check));
}
@Override
public void updateResult(List<Artist> objects, Artist result) {
// Don't try to put anything here, as the Entry update method will not be called since it's a artist!
}
}.execute();
} else {
new MusicDirectoryUpdater(context, cacheName, parent) {
@Override
public boolean checkResult(Entry check) {
return GenericEntryUpdater.this.checkResult(entry, check);
}
@Override
public void updateResult(List<Entry> objects, Entry result) {
GenericEntryUpdater.this.updateResult(result);
}
}.execute();
}
songs.add(entry);
}
// Only run through playlists once and check each song against it
if(songs.size() > 0) {
new PlaylistDirectoryUpdater(context) {
@Override
public boolean checkResult(Entry check) {
for(Entry entry: songs) {
if(GenericEntryUpdater.this.checkResult(entry, check)) {
return true;
}
}
return false;
}
@Override
public void updateResult(Entry result) {
GenericEntryUpdater.this.updateResult(result);
}
}.execute();
}
}
}
private class StarUpdater extends GenericEntryUpdater {
public StarUpdater(Context context, List<Entry> entries) {
super(context, entries);
}
@Override
public boolean checkResult(Entry entry, Entry check) {
if (!entry.getId().equals(check.getId())) {
return false;
}
return true;
}
@Override
public void updateResult(Entry result) {
}
};
private abstract class IndexesUpdater extends SerializeUpdater<Artist> {
Indexes indexes;
IndexesUpdater(Context context, String name) {
super(context, name, Util.getSelectedMusicFolderId(context, musicService.getInstance(context)));
IndexesUpdater(Context context) {
super(context, "artists", Util.getSelectedMusicFolderId(context, musicService.getInstance(context)));
}
@Override
public ArrayList<Artist> getArrayList() {
indexes = FileUtil.deserialize(context, cacheName, Indexes.class);
if(indexes == null) {
if (indexes == null) {
return null;
}
ArrayList<Artist> artists = new ArrayList<Artist>();
ArrayList<Artist> artists = new ArrayList<>();
artists.addAll(indexes.getArtists());
artists.addAll(indexes.getShortcuts());
return artists;
@ -999,34 +816,4 @@ public class CachedMusicService implements MusicService {
cachedIndexes.set(indexes);
}
}
protected void updateAllSongs(Context context, MusicDirectory dir) {
List<Entry> songs = dir.getSongs();
if(!songs.isEmpty()) {
SongDBHandler.getHandler(context).addSongs(musicService.getInstance(context), songs);
}
}
private void checkSettingsChanged(Context context) {
int instance = musicService.getInstance(context);
String newUrl = musicService.getRestUrl(context, null, false);
boolean newIsTagBrowsing = Util.isTagBrowsing(context, instance);
if (!Util.equals(newUrl, restUrl) || isTagBrowsing != newIsTagBrowsing) {
cachedMusicFolders.clear();
cachedIndexes.clear();
cachedPlaylists.clear();
restUrl = newUrl;
isTagBrowsing = newIsTagBrowsing;
}
String newMusicFolderId = Util.getSelectedMusicFolderId(context, instance);
if(!Util.equals(newMusicFolderId, musicFolderId)) {
cachedIndexes.clear();
musicFolderId = newMusicFolderId;
}
}
public RESTMusicService getMusicService() {
return musicService;
}
}

View File

@ -18,13 +18,6 @@
*/
package net.nullsum.audinaut.service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.PowerManager;
@ -38,6 +31,13 @@ import net.nullsum.audinaut.util.FileUtil;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.util.Util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import okhttp3.Response;
/**
@ -54,16 +54,15 @@ public class DownloadFile implements BufferFile {
private final File saveFile;
private final MediaStoreService mediaStoreService;
private final boolean save;
private final Long contentLength = null;
private DownloadTask downloadTask;
private boolean save;
private boolean failedDownload = false;
private int failed = 0;
private int bitRate;
private boolean isPlaying = false;
private boolean saveWhenDone = false;
private boolean completeWhenDone = false;
private Long contentLength = null;
private long currentSpeed = 0;
private boolean rateLimit = false;
public DownloadFile(Context context, MusicDirectory.Entry song, boolean save) {
@ -82,19 +81,16 @@ public class DownloadFile implements BufferFile {
public MusicDirectory.Entry getSong() {
return song;
}
public boolean isSong() {
return song.isSong();
}
public Context getContext() {
return context;
}
/**
* Returns the effective bit rate.
*/
public int getBitRate() {
if(!partialFile.exists()) {
if (!partialFile.exists()) {
bitRate = getActualBitrate();
}
if (bitRate > 0) {
@ -102,17 +98,18 @@ public class DownloadFile implements BufferFile {
}
return song.getBitRate() == null ? 160 : song.getBitRate();
}
private int getActualBitrate() {
int br = Util.getMaxBitrate(context);
if(br == 0 && song.getTranscodedSuffix() != null && "mp3".equals(song.getTranscodedSuffix().toLowerCase())) {
if(song.getBitRate() != null) {
if (br == 0 && song.getTranscodedSuffix() != null && "mp3".equals(song.getTranscodedSuffix().toLowerCase())) {
if (song.getBitRate() != null) {
br = Math.min(320, song.getBitRate());
} else {
br = 320;
}
} else if(song.getSuffix() != null && (song.getTranscodedSuffix() == null || song.getSuffix().equals(song.getTranscodedSuffix()))) {
} else if (song.getSuffix() != null && (song.getTranscodedSuffix() == null || song.getSuffix().equals(song.getTranscodedSuffix()))) {
// If just downsampling, don't try to upsample (ie: 128 kpbs -> 192 kpbs)
if(song.getBitRate() != null && (br == 0 || br > song.getBitRate())) {
if (song.getBitRate() != null && (br == 0 || br > song.getBitRate())) {
br = song.getBitRate();
}
}
@ -124,29 +121,16 @@ public class DownloadFile implements BufferFile {
return contentLength;
}
public long getCurrentSize() {
if(partialFile.exists()) {
return partialFile.length();
} else {
File file = getCompleteFile();
if(file.exists()) {
return file.length();
} else {
return 0L;
}
}
}
@Override
public long getEstimatedSize() {
if(contentLength != null) {
if (contentLength != null) {
return contentLength;
}
File file = getCompleteFile();
if(file.exists()) {
if (file.exists()) {
return file.length();
} else if(song.getDuration() == null) {
} else if (song.getDuration() == null) {
return 0;
} else {
int br = (getBitRate() * 1000) / 8;
@ -155,29 +139,16 @@ public class DownloadFile implements BufferFile {
}
}
public long getBytesPerSecond() {
return currentSpeed;
}
public synchronized void download() {
rateLimit = false;
preDownload();
downloadTask.execute();
}
public synchronized void downloadNow(MusicService musicService) {
rateLimit = true;
preDownload();
downloadTask.setMusicService(musicService);
try {
downloadTask.doInBackground();
} catch(InterruptedException e) {
// This should never be reached
}
}
private void preDownload() {
FileUtil.createDirectoryForParent(saveFile);
failedDownload = false;
if(!partialFile.exists()) {
if (!partialFile.exists()) {
bitRate = getActualBitrate();
}
downloadTask = new DownloadTask(context);
@ -211,9 +182,6 @@ public class DownloadFile implements BufferFile {
return saveFile;
}
public File getSaveFile() {
return saveFile;
}
public File getPartialFile() {
return partialFile;
@ -244,7 +212,7 @@ public class DownloadFile implements BufferFile {
@Override
public synchronized void onResume() {
if(!isWorkDone() && !isFailedMax() && !isDownloading() && !isDownloadCancelled()) {
if (!isWorkDone() && !isFailedMax() && !isDownloading() && !isDownloadCancelled()) {
download();
}
}
@ -264,6 +232,7 @@ public class DownloadFile implements BufferFile {
public boolean isFailed() {
return failedDownload;
}
public boolean isFailedMax() {
return failed > MAX_FAILURES;
}
@ -317,60 +286,52 @@ public class DownloadFile implements BufferFile {
}
}
public void renamePartial() {
Util.renameFile(partialFile, completeFile);
saveToStore();
}
public void setPlaying(boolean isPlaying) {
try {
if(saveWhenDone && !isPlaying) {
Util.renameFile(completeFile, saveFile);
renameInStore(completeFile, saveFile);
saveWhenDone = false;
} else if(completeWhenDone && !isPlaying) {
if(save) {
Util.renameFile(partialFile, saveFile);
saveToStore();
} else {
Util.renameFile(partialFile, completeFile);
saveToStore();
}
completeWhenDone = false;
if (saveWhenDone && !isPlaying) {
Util.renameFile(completeFile, saveFile);
renameInStore(completeFile, saveFile);
saveWhenDone = false;
} else if (completeWhenDone && !isPlaying) {
if (save) {
Util.renameFile(partialFile, saveFile);
saveToStore();
} else {
Util.renameFile(partialFile, completeFile);
saveToStore();
}
} catch(IOException ex) {
Log.w(TAG, "Failed to rename file " + completeFile + " to " + saveFile, ex);
completeWhenDone = false;
}
this.isPlaying = isPlaying;
}
public void renamePartial() {
try {
Util.renameFile(partialFile, completeFile);
saveToStore();
} catch(IOException ex) {
Log.w(TAG, "Failed to rename file " + partialFile + " to " + completeFile, ex);
}
}
public boolean getPlaying() {
return isPlaying;
}
private void deleteFromStore() {
try {
mediaStoreService.deleteFromMediaStore(this);
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Failed to remove from store", e);
}
}
private void saveToStore() {
if(!Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_HIDE_MEDIA, false)) {
if (!Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_HIDE_MEDIA, false)) {
try {
mediaStoreService.saveInMediaStore(this);
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Failed to save in media store", e);
}
}
}
private void renameInStore(File start, File end) {
try {
mediaStoreService.renameInMediaStore(start, end);
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Failed to rename in store", e);
}
}
@ -425,7 +386,7 @@ public class DownloadFile implements BufferFile {
}
if (completeFile.exists()) {
if (save) {
if(isPlaying) {
if (isPlaying) {
saveWhenDone = true;
} else {
Util.renameFile(completeFile, saveFile);
@ -438,7 +399,7 @@ public class DownloadFile implements BufferFile {
return null;
}
if(musicService == null) {
if (musicService == null) {
musicService = MusicServiceFactory.getMusicService(context);
}
@ -446,15 +407,14 @@ public class DownloadFile implements BufferFile {
boolean compare;
try {
compare = (bitRate == 0) || (song.getDuration() == 0) || (partialFile.length() == 0) || (bitRate * song.getDuration() * 1000 / 8) > partialFile.length();
} catch(Exception e) {
} catch (Exception e) {
compare = true;
}
if(compare) {
if (compare) {
// Attempt partial HTTP GET, appending to the file if it exists.
Response response = musicService.getDownloadInputStream(context, song, partialFile.length(), bitRate, DownloadTask.this);
if(response.header("Content-Length") != null) {
if (response.header("Content-Length") != null) {
Log.i(TAG, "Content Length: " + contentLength);
contentLength = contentLength;
}
boolean partial = response.code() == 206;
@ -472,17 +432,17 @@ public class DownloadFile implements BufferFile {
if (isCancelled()) {
throw new Exception("Download of '" + song + "' was cancelled");
} else if(partialFile.length() == 0) {
} else if (partialFile.length() == 0) {
throw new Exception("Download of '" + song + "' failed. File is 0 bytes long.");
}
downloadAndSaveCoverArt(musicService);
}
if(isPlaying) {
if (isPlaying) {
completeWhenDone = true;
} else {
if(save) {
if (save) {
Util.renameFile(partialFile, saveFile);
} else {
Util.renameFile(partialFile, completeFile);
@ -490,20 +450,20 @@ public class DownloadFile implements BufferFile {
DownloadFile.this.saveToStore();
}
} catch(InterruptedException x) {
} catch (InterruptedException x) {
throw x;
} catch(FileNotFoundException x) {
} catch (FileNotFoundException x) {
Util.delete(completeFile);
Util.delete(saveFile);
if(!isCancelled()) {
if (!isCancelled()) {
failed = MAX_FAILURES + 1;
failedDownload = true;
Log.w(TAG, "Failed to download '" + song + "'.", x);
}
} catch(IOException x) {
} catch (IOException x) {
Util.delete(completeFile);
Util.delete(saveFile);
if(!isCancelled()) {
if (!isCancelled()) {
failedDownload = true;
Log.w(TAG, "Failed to download '" + song + "'.", x);
}
@ -529,7 +489,7 @@ public class DownloadFile implements BufferFile {
// Only run these if not interrupted, ie: cancelled
DownloadService downloadService = DownloadService.getInstance();
if(downloadService != null && !isCancelled()) {
if (downloadService != null && !isCancelled()) {
new CacheCleaner(context, downloadService).cleanSpace();
checkDownloads();
}
@ -539,7 +499,7 @@ public class DownloadFile implements BufferFile {
private void checkDownloads() {
DownloadService downloadService = DownloadService.getInstance();
if(downloadService != null) {
if (downloadService != null) {
downloadService.checkDownloads();
}
}
@ -549,16 +509,12 @@ public class DownloadFile implements BufferFile {
return "DownloadTask (" + song + ")";
}
public void setMusicService(MusicService musicService) {
this.musicService = musicService;
}
private void downloadAndSaveCoverArt(MusicService musicService) throws Exception {
private void downloadAndSaveCoverArt(MusicService musicService) {
try {
if (song.getCoverArt() != null) {
// Check if album art already exists, don't want to needlessly load into memory
File albumArtFile = FileUtil.getAlbumArtFile(context, song);
if(!albumArtFile.exists()) {
if (!albumArtFile.exists()) {
musicService.getCoverArt(context, song, 0, null, null);
}
}
@ -602,23 +558,18 @@ public class DownloadFile implements BufferFile {
long now = System.currentTimeMillis();
if (now - lastLog > 3000L) { // Only every so often.
Log.i(TAG, "Downloaded " + Util.formatBytes(count) + " of " + song);
currentSpeed = lastCount / ((now - lastLog) / 1000L);
lastLog = now;
lastCount = 0;
// Re-establish every few seconds whether screen is on or not
if(rateLimit) {
if (rateLimit) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if(pm.isScreenOn()) {
activeLimit = true;
} else {
activeLimit = false;
}
activeLimit = pm.isScreenOn();
}
}
// If screen is on and rateLimit is true, stop downloading from exhausting bandwidth
if(activeLimit) {
if (activeLimit) {
Thread.sleep(10L);
}
}

View File

@ -18,12 +18,6 @@
*/
package net.nullsum.audinaut.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.atomic.AtomicBoolean;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@ -36,64 +30,60 @@ import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.domain.PlayerQueue;
import net.nullsum.audinaut.domain.PlayerState;
import net.nullsum.audinaut.util.CacheCleaner;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.FileUtil;
import net.nullsum.audinaut.util.Pair;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.util.SongDBHandler;
import net.nullsum.audinaut.util.Util;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
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 String TAG = DownloadServiceLifecycleSupport.class.getSimpleName();
private static final int DEBOUNCE_TIME = 200;
private final DownloadService downloadService;
private final AtomicBoolean setup = new AtomicBoolean(false);
private final ReentrantLock lock = new ReentrantLock();
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 boolean externalStorageAvailable = true;
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() {
private final 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);
}
eventHandler.post(() -> {
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);
}
});
}
@ -105,30 +95,27 @@ public class DownloadServiceLifecycleSupport {
}
public void onCreate() {
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
eventLooper = Looper.myLooper();
eventHandler = new Handler(eventLooper);
new Thread(() -> {
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);
}
setup.set(true);
} finally {
lock.unlock();
// Wait until PREPARING is done to mark lifecycle as ready to receive events
while (downloadService.getPlayerState() == PREPARING) {
Util.sleepQuietly(50L);
}
Looper.loop();
setup.set(true);
} finally {
lock.unlock();
}
Looper.loop();
}, "DownloadServiceLifecycleSupport").start();
// Stop when SD card is ejected.
@ -181,70 +168,67 @@ public class DownloadServiceLifecycleSupport {
if (intent != null) {
final String action = intent.getAction();
if(eventHandler == null) {
if (eventHandler == null) {
Util.sleepQuietly(100L);
}
if(eventHandler == null) {
if (eventHandler == null) {
return;
}
eventHandler.post(new Runnable() {
@Override
public void run() {
if(!setup.get()) {
lock.lock();
lock.unlock();
eventHandler.post(() -> {
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 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();
String endYear = intent.getStringExtra(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR);
if (endYear != null) {
editor.putString(Constants.PREFERENCES_KEY_SHUFFLE_END_YEAR, endYear);
}
} 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);
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);
}
}
});
@ -266,29 +250,23 @@ public class DownloadServiceLifecycleSupport {
}
public void serializeDownloadQueue() {
serializeDownloadQueue(true);
}
public void serializeDownloadQueue(final boolean serializeRemote) {
if(!setup.get()) {
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<>(downloadService.getSongs());
eventHandler.post(() -> {
if (lock.tryLock()) {
try {
serializeDownloadQueueNow(songs);
} finally {
lock.unlock();
}
}
});
}
public void serializeDownloadQueueNow(List<DownloadFile> songs, boolean serializeRemote) {
private void serializeDownloadQueueNow(List<DownloadFile> songs) {
final PlayerQueue state = new PlayerQueue();
for (DownloadFile downloadFile : songs) {
state.songs.add(downloadFile.getSong());
@ -300,11 +278,9 @@ public class DownloadServiceLifecycleSupport {
state.currentPlayingPosition = downloadService.getPlayerPosition();
DownloadFile currentPlaying = downloadService.getCurrentPlaying();
if(currentPlaying != null) {
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);
}
@ -321,24 +297,16 @@ public class DownloadServiceLifecycleSupport {
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()) {
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);
if(state != null) {
lastChange = state.changed;
}
}
public Date getLastChange() {
return lastChange;
}
public void handleKeyEvent(KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() > 0) {
private void handleKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() > 0) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
downloadService.fastForward();
@ -347,25 +315,25 @@ public class DownloadServiceLifecycleSupport {
downloadService.rewind();
break;
}
} else if(event.getAction() == KeyEvent.ACTION_UP) {
} 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)) {
if (lastPressTime < (System.currentTimeMillis() - 500)) {
lastPressTime = System.currentTimeMillis();
downloadService.togglePlayPause();
} else {
downloadService.next(false, true);
downloadService.next(true);
}
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
if(lastPressTime < (System.currentTimeMillis() - DEBOUNCE_TIME)) {
if (lastPressTime < (System.currentTimeMillis() - DEBOUNCE_TIME)) {
lastPressTime = System.currentTimeMillis();
downloadService.previous();
}
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
if(lastPressTime < (System.currentTimeMillis() - DEBOUNCE_TIME)) {
if (lastPressTime < (System.currentTimeMillis() - DEBOUNCE_TIME)) {
lastPressTime = System.currentTimeMillis();
downloadService.next();
}
@ -380,7 +348,7 @@ public class DownloadServiceLifecycleSupport {
downloadService.stop();
break;
case KeyEvent.KEYCODE_MEDIA_PLAY:
if(downloadService.getPlayerState() != PlayerState.STARTED) {
if (downloadService.getPlayerState() != PlayerState.STARTED) {
downloadService.start();
}
break;
@ -401,28 +369,25 @@ public class DownloadServiceLifecycleSupport {
@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);
eventHandler.post(() -> {
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;
case TelephonyManager.CALL_STATE_IDLE:
if (resumeAfterCall) {
resumeAfterCall = false;
if(downloadService.getPlayerState() == PlayerState.PAUSED_TEMP) {
downloadService.start();
}
}
break;
default:
break;
}
}
break;
default:
break;
}
});
}

View File

@ -44,7 +44,7 @@ public class HeadphoneListenerService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!Util.shouldStartOnHeadphones(this)) {
if (!Util.shouldStartOnHeadphones(this)) {
stopSelf();
}
@ -56,10 +56,10 @@ public class HeadphoneListenerService extends Service {
super.onDestroy();
try {
if(receiver != null) {
if (receiver != null) {
unregisterReceiver(receiver);
}
} catch(Exception e) {
} catch (Exception e) {
// Don't care
}
}

View File

@ -18,8 +18,6 @@
*/
package net.nullsum.audinaut.service;
import java.io.File;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
@ -27,9 +25,11 @@ import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.util.FileUtil;
import net.nullsum.audinaut.util.Util;
import java.io.File;
/**
* @author Sindre Mehus
@ -67,7 +67,7 @@ public class MediaStoreService {
if (song.getYear() != null) {
values.put(MediaStore.Audio.AudioColumns.YEAR, song.getYear());
}
if(song.getTranscodedContentType() != null) {
if (song.getTranscodedContentType() != null) {
values.put(MediaStore.MediaColumns.MIME_TYPE, song.getTranscodedContentType());
} else {
values.put(MediaStore.MediaColumns.MIME_TYPE, song.getContentType());

View File

@ -18,24 +18,23 @@
*/
package net.nullsum.audinaut.service;
import java.util.List;
import okhttp3.Response;
import android.content.Context;
import android.graphics.Bitmap;
import net.nullsum.audinaut.domain.Genre;
import net.nullsum.audinaut.domain.Indexes;
import net.nullsum.audinaut.domain.PlayerQueue;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.domain.MusicFolder;
import net.nullsum.audinaut.domain.Playlist;
import net.nullsum.audinaut.domain.SearchCritera;
import net.nullsum.audinaut.domain.SearchResult;
import net.nullsum.audinaut.domain.User;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.util.ProgressListener;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import java.util.List;
import okhttp3.Response;
/**
* @author Sindre Mehus
@ -78,7 +77,6 @@ public interface MusicService {
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 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;
@ -91,11 +89,5 @@ public interface MusicService {
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;
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;
void setInstance(Integer instance) throws Exception;
}

View File

@ -26,7 +26,7 @@ package net.nullsum.audinaut.service;
*/
public class OfflineException extends Exception {
public OfflineException(String message) {
super(message);
public OfflineException() {
super(OfflineMusicService.ERRORMSG);
}
}

View File

@ -18,30 +18,15 @@
*/
package net.nullsum.audinaut.service;
import java.io.File;
import java.io.Reader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.util.Log;
import okhttp3.Response;
import net.nullsum.audinaut.domain.Artist;
import net.nullsum.audinaut.domain.Genre;
import net.nullsum.audinaut.domain.Indexes;
import net.nullsum.audinaut.domain.MusicDirectory.Entry;
import net.nullsum.audinaut.domain.PlayerQueue;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.domain.MusicDirectory.Entry;
import net.nullsum.audinaut.domain.MusicFolder;
import net.nullsum.audinaut.domain.Playlist;
import net.nullsum.audinaut.domain.SearchCritera;
@ -49,21 +34,31 @@ import net.nullsum.audinaut.domain.SearchResult;
import net.nullsum.audinaut.domain.User;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.FileUtil;
import net.nullsum.audinaut.util.Pair;
import net.nullsum.audinaut.util.ProgressListener;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.util.SongDBHandler;
import net.nullsum.audinaut.util.Util;
import java.io.*;
import java.util.Comparator;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.SortedSet;
import okhttp3.Response;
/**
* @author Sindre Mehus
*/
public class OfflineMusicService implements MusicService {
public static final String ERRORMSG = "Not available in offline mode";
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
@ -73,7 +68,7 @@ public class OfflineMusicService implements MusicService {
@Override
public Indexes getIndexes(String musicFolderId, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
List<Artist> artists = new ArrayList<Artist>();
List<Artist> artists = new ArrayList<>();
List<Entry> entries = new ArrayList<>();
File root = FileUtil.getMusicDirectory(context);
for (File file : FileUtil.listFiles(root)) {
@ -83,31 +78,31 @@ public class OfflineMusicService implements MusicService {
artist.setIndex(file.getName().substring(0, 1));
artist.setName(file.getName());
artists.add(artist);
} else if(!file.getName().equals("albumart.jpg") && !file.getName().equals(".nomedia")) {
} else if (!file.getName().equals("albumart.jpg") && !file.getName().equals(".nomedia")) {
entries.add(createEntry(context, file));
}
}
Indexes indexes = new Indexes(0L, Collections.<Artist>emptyList(), artists, entries);
return indexes;
return new Indexes(Collections.emptyList(), artists, entries);
}
@Override
public MusicDirectory getMusicDirectory(String id, String artistName, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
return getMusicDirectory(id, artistName, refresh, context, progressListener, false);
return getMusicDirectory(id, context);
}
private MusicDirectory getMusicDirectory(String id, String artistName, boolean refresh, Context context, ProgressListener progressListener, boolean isPodcast) throws Exception {
private MusicDirectory getMusicDirectory(String id, Context context) 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<>();
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.addChild(createEntry(context, file, name, true));
}
}
result.sortChildren(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_CUSTOM_SORT_ENABLED, true));
@ -116,12 +111,12 @@ public class OfflineMusicService implements MusicService {
@Override
public MusicDirectory getArtist(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
throw new OfflineException();
}
@Override
public MusicDirectory getAlbum(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
throw new OfflineException();
}
private String getName(File file) {
@ -141,29 +136,24 @@ public class OfflineMusicService implements MusicService {
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;
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 + "/" , ""));
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)) {
if (artistFolder.getPath().equals(root)) {
entry.setArtist(albumFolder.getName());
} else {
entry.setArtist(artistFolder.getName());
@ -171,16 +161,16 @@ public class OfflineMusicService implements MusicService {
entry.setAlbum(albumFolder.getName());
int index = name.indexOf('-');
if(index != -1) {
if (index != -1) {
try {
entry.setTrack(Integer.parseInt(name.substring(0, index)));
title = title.substring(index + 1);
} catch(Exception e) {
} catch (Exception e) {
// Failed parseInt, just means track filled out
}
}
if(load) {
if (load) {
entry.loadMetadata(file);
}
}
@ -199,32 +189,32 @@ public class OfflineMusicService implements MusicService {
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) {
} 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);
throw new OfflineException();
}
@Override
public List<MusicFolder> getMusicFolders(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
throw new OfflineException();
}
@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>();
List<Artist> artists = new ArrayList<>();
List<Entry> albums = new ArrayList<>();
List<Entry> songs = new ArrayList<>();
File root = FileUtil.getMusicDirectory(context);
int closeness = 0;
int closeness;
for (File artistFile : FileUtil.listFiles(root)) {
String artistName = artistFile.getName();
if (artistFile.isDirectory()) {
if((closeness = matchCriteria(criteria, artistName)) > 0) {
if ((closeness = matchCriteria(criteria, artistName)) > 0) {
Artist artist = new Artist();
artist.setId(artistFile.getPath());
artist.setIndex(artistFile.getName().substring(0, 1));
@ -237,43 +227,31 @@ public class OfflineMusicService implements MusicService {
}
}
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(artists, (lhs, 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(albums, (lhs, 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;
}
Collections.sort(songs, (lhs, rhs) -> {
if (lhs.getCloseness() == rhs.getCloseness()) {
return 0;
} else if (lhs.getCloseness() > rhs.getCloseness()) {
return -1;
} else {
return 1;
}
});
@ -290,26 +268,25 @@ public class OfflineMusicService implements MusicService {
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()) {
for (File albumFile : FileUtil.listMediaFiles(file)) {
if (albumFile.isDirectory()) {
String albumName = getName(albumFile);
if((closeness = matchCriteria(criteria, albumName)) > 0) {
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)) {
for (File songFile : FileUtil.listMediaFiles(albumFile)) {
String songName = getName(songFile);
if(songName == null) {
if (songName == null) {
continue;
}
if(songFile.isDirectory()) {
if (songFile.isDirectory()) {
recursiveAlbumSearch(artistName, songFile, criteria, context, albums, songs);
}
else if((closeness = matchCriteria(criteria, songName)) > 0){
} else if ((closeness = matchCriteria(criteria, songName)) > 0) {
Entry song = createEntry(context, albumFile, songName);
song.setArtist(artistName);
song.setAlbum(albumName);
@ -317,10 +294,9 @@ public class OfflineMusicService implements MusicService {
songs.add(song);
}
}
}
else {
} else {
String songName = getName(albumFile);
if((closeness = matchCriteria(criteria, songName)) > 0) {
if ((closeness = matchCriteria(criteria, songName)) > 0) {
Entry song = createEntry(context, albumFile, songName);
song.setArtist(artistName);
song.setAlbum(songName);
@ -330,11 +306,12 @@ public class OfflineMusicService implements MusicService {
}
}
}
private int matchCriteria(SearchCritera criteria, String name) {
if (criteria.getPattern().matcher(name).matches()) {
return Util.getStringDistance(
criteria.getQuery().toLowerCase(),
name.toLowerCase());
criteria.getQuery().toLowerCase(),
name.toLowerCase());
} else {
return 0;
}
@ -342,16 +319,16 @@ public class OfflineMusicService implements MusicService {
@Override
public List<Playlist> getPlaylists(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
List<Playlist> playlists = new ArrayList<Playlist>();
List<Playlist> playlists = new ArrayList<>();
File root = FileUtil.getPlaylistDirectory(context);
String lastServer = null;
boolean removeServer = true;
for (File folder : FileUtil.listFiles(root)) {
if(folder.isDirectory()) {
if (folder.isDirectory()) {
String server = folder.getName();
SortedSet<File> fileList = FileUtil.listFiles(folder);
for(File file: fileList) {
if(FileUtil.isPlaylistFile(file)) {
for (File file : fileList) {
if (FileUtil.isPlaylistFile(file)) {
String id = file.getName();
String filename = FileUtil.getBaseName(id);
String name = server + ": " + filename;
@ -366,41 +343,41 @@ public class OfflineMusicService implements MusicService {
buffer = new BufferedReader(reader);
String line = buffer.readLine();
while( (line = buffer.readLine()) != null ){
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 (!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){
if (checkFile.exists() && entryName != null) {
songCount++;
}
}
playlist.setSongCount(Integer.toString(songCount));
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Failed to count songs in playlist", e);
} finally {
Util.close(buffer);
Util.close(reader);
}
if(songCount > 0) {
if (songCount > 0) {
playlists.add(playlist);
}
}
}
if(!server.equals(lastServer) && fileList.size() > 0) {
if(lastServer != null) {
if (!server.equals(lastServer) && fileList.size() > 0) {
if (lastServer != null) {
removeServer = false;
}
lastServer = server;
@ -409,14 +386,14 @@ public class OfflineMusicService implements MusicService {
// Delete legacy playlist files
try {
folder.delete();
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Failed to delete old playlist file: " + folder.getName());
}
}
}
if(removeServer) {
for(Playlist playlist: playlists) {
if (removeServer) {
for (Playlist playlist : playlists) {
playlist.setName(playlist.getName().substring(playlist.getId().length() + 2));
}
}
@ -434,7 +411,7 @@ public class OfflineMusicService implements MusicService {
BufferedReader buffer = null;
try {
int firstIndex = name.indexOf(id);
if(firstIndex != -1) {
if (firstIndex != -1) {
name = name.substring(id.length() + 2);
}
@ -444,23 +421,23 @@ public class OfflineMusicService implements MusicService {
MusicDirectory playlist = new MusicDirectory();
String line = buffer.readLine();
if(!"#EXTM3U".equals(line)) return playlist;
if (!"#EXTM3U".equals(line)) return playlist;
while( (line = buffer.readLine()) != null ){
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 (!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()));
+ ".complete." + FileUtil.getExtension(entryFile.getName()));
}
String entryName = getName(entryFile);
if(checkFile.exists() && entryName != null){
if (checkFile.exists() && entryName != null) {
playlist.addChild(createEntry(context, entryFile, entryName, false));
}
}
@ -474,68 +451,63 @@ public class OfflineMusicService implements MusicService {
@Override
public void createPlaylist(String id, String name, List<Entry> entries, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
throw new OfflineException();
}
@Override
public void deletePlaylist(String id, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
throw new OfflineException();
}
@Override
public void addToPlaylist(String id, List<Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
throw new OfflineException();
}
@Override
public void removeFromPlaylist(String id, List<Integer> toRemove, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
throw new OfflineException();
}
@Override
public void overwritePlaylist(String id, String name, int toRemove, List<Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
throw new OfflineException();
}
@Override
public void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
throw new OfflineException();
}
@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();
}
@Override
public MusicDirectory getAlbumList(String type, String extra, int size, int offset, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
throw new OfflineException();
}
@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);
throw new OfflineException();
}
@Override
public List<Genre> getGenres(boolean refresh, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
throw new OfflineException();
}
@Override
public MusicDirectory getSongsByGenre(String genre, int count, int offset, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
throw new OfflineException();
}
@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>();
List<File> children = new LinkedList<>();
listFilesRecursively(root, children);
MusicDirectory result = new MusicDirectory();
@ -552,27 +524,12 @@ public class OfflineMusicService implements MusicService {
@Override
public User getUser(boolean refresh, String username, Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
throw new OfflineException();
}
@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 PlayerQueue getPlayQueue(Context context, ProgressListener progressListener) throws Exception {
throw new OfflineException(ERRORMSG);
}
@Override
public void setInstance(Integer instance) throws Exception{
throw new OfflineException(ERRORMSG);
public void setInstance(Integer instance) throws Exception {
throw new OfflineException();
}
private void listFilesRecursively(File parent, List<File> children) {

View File

@ -18,28 +18,19 @@
*/
package net.nullsum.audinaut.service;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.FormBody;
import okhttp3.FormBody.Builder;
import okhttp3.RequestBody;
import okhttp3.Call;
import okhttp3.Credentials;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.util.Log;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.*;
import net.nullsum.audinaut.domain.Genre;
import net.nullsum.audinaut.domain.Indexes;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.domain.MusicFolder;
import net.nullsum.audinaut.domain.Playlist;
import net.nullsum.audinaut.domain.SearchCritera;
import net.nullsum.audinaut.domain.SearchResult;
import net.nullsum.audinaut.domain.User;
import net.nullsum.audinaut.fragments.MainFragment;
import net.nullsum.audinaut.service.parser.EntryListParser;
import net.nullsum.audinaut.service.parser.ErrorParser;
@ -47,29 +38,39 @@ import net.nullsum.audinaut.service.parser.GenreParser;
import net.nullsum.audinaut.service.parser.IndexesParser;
import net.nullsum.audinaut.service.parser.MusicDirectoryParser;
import net.nullsum.audinaut.service.parser.MusicFoldersParser;
import net.nullsum.audinaut.service.parser.PlayQueueParser;
import net.nullsum.audinaut.service.parser.PlaylistParser;
import net.nullsum.audinaut.service.parser.PlaylistsParser;
import net.nullsum.audinaut.service.parser.RandomSongsParser;
import net.nullsum.audinaut.service.parser.SearchResult2Parser;
import net.nullsum.audinaut.service.parser.UserParser;
import net.nullsum.audinaut.util.BackgroundTask;
import net.nullsum.audinaut.util.Pair;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.FileUtil;
import net.nullsum.audinaut.util.Pair;
import net.nullsum.audinaut.util.ProgressListener;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.util.SongDBHandler;
import net.nullsum.audinaut.util.Util;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.FormBody;
import okhttp3.FormBody.Builder;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* @author Sindre Mehus
*/
public class RESTMusicService implements MusicService {
private static OkHttpClient client = new OkHttpClient();
private static final String TAG = RESTMusicService.class.getSimpleName();
private static final OkHttpClient client = new OkHttpClient();
private Integer instance;
@Override
@ -77,8 +78,8 @@ public class RESTMusicService implements MusicService {
String url = getRestUrl(context, "ping");
Request request = new Request.Builder()
.url(url)
.build();
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
new ErrorParser(context, getInstance(context)).parse(response.body().byteStream());
@ -89,11 +90,11 @@ public class RESTMusicService implements MusicService {
String url = getRestUrl(context, "getMusicFolders");
Request request = new Request.Builder()
.url(url)
.build();
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
return new MusicFoldersParser(context, getInstance(context)).parse(response.body().byteStream(), progressListener);
return new MusicFoldersParser(context, getInstance(context)).parse(response.body().byteStream());
}
}
@ -101,7 +102,7 @@ public class RESTMusicService implements MusicService {
public Indexes getIndexes(String musicFolderId, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "getArtists");
Builder builder= new FormBody.Builder();
Builder builder = new FormBody.Builder();
if (musicFolderId != null) {
builder.add("musicFolderId", musicFolderId);
@ -110,9 +111,9 @@ public class RESTMusicService implements MusicService {
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
return new IndexesParser(context, getInstance(context)).parse(response.body().byteStream(), progressListener);
@ -123,22 +124,22 @@ public class RESTMusicService implements MusicService {
public MusicDirectory getMusicDirectory(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
SharedPreferences prefs = Util.getPreferences(context);
String cacheLocn = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null);
if(cacheLocn != null && id.indexOf(cacheLocn) != -1) {
String search = Util.parseOfflineIDSearch(context, id, cacheLocn);
if (cacheLocn != null && id.contains(cacheLocn)) {
String search = Util.parseOfflineIDSearch(id, cacheLocn);
SearchCritera critera = new SearchCritera(search, 1, 1, 0);
SearchResult result = search(critera, context, progressListener);
if(result.getArtists().size() == 1) {
if (result.getArtists().size() == 1) {
id = result.getArtists().get(0).getId();
} else if(result.getAlbums().size() == 1) {
} else if (result.getAlbums().size() == 1) {
id = result.getAlbums().get(0).getId();
}
}
MusicDirectory dir = null;
int index, start = 0;
while((index = id.indexOf(';', start)) != -1) {
MusicDirectory extra = getMusicDirectoryImpl(id.substring(start, index), name, refresh, context, progressListener);
if(dir == null) {
while ((index = id.indexOf(';', start)) != -1) {
MusicDirectory extra = getMusicDirectoryImpl(id.substring(start, index), name, context);
if (dir == null) {
dir = extra;
} else {
dir.addChildren(extra.getChildren());
@ -146,8 +147,8 @@ public class RESTMusicService implements MusicService {
start = index + 1;
}
MusicDirectory extra = getMusicDirectoryImpl(id.substring(start), name, refresh, context, progressListener);
if(dir == null) {
MusicDirectory extra = getMusicDirectoryImpl(id.substring(start), name, context);
if (dir == null) {
dir = extra;
} else {
dir.addChildren(extra.getChildren());
@ -156,20 +157,20 @@ public class RESTMusicService implements MusicService {
return dir;
}
private MusicDirectory getMusicDirectoryImpl(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
private MusicDirectory getMusicDirectoryImpl(String id, String name, Context context) throws Exception {
String url = getRestUrl(context, "getMusicDirectory");
RequestBody formBody = new FormBody.Builder()
.add("id", id)
.build();
.add("id", id)
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
return new MusicDirectoryParser(context, getInstance(context)).parse(name, response.body().byteStream(), progressListener);
return new MusicDirectoryParser(context, getInstance(context)).parse(name, response.body().byteStream());
}
}
@ -178,16 +179,16 @@ public class RESTMusicService implements MusicService {
String url = getRestUrl(context, "getArtist");
RequestBody formBody = new FormBody.Builder()
.add("id", id)
.build();
.add("id", id)
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
return new MusicDirectoryParser(context, getInstance(context)).parse(name, response.body().byteStream(), progressListener);
return new MusicDirectoryParser(context, getInstance(context)).parse(name, response.body().byteStream());
}
}
@ -196,16 +197,16 @@ public class RESTMusicService implements MusicService {
String url = getRestUrl(context, "getAlbum");
RequestBody formBody = new FormBody.Builder()
.add("id", id)
.build();
.add("id", id)
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
return new MusicDirectoryParser(context, getInstance(context)).parse(name, response.body().byteStream(), progressListener);
return new MusicDirectoryParser(context, getInstance(context)).parse(name, response.body().byteStream());
}
}
@ -213,7 +214,7 @@ public class RESTMusicService implements MusicService {
public SearchResult search(SearchCritera critera, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "search3");
Builder builder= new FormBody.Builder();
Builder builder = new FormBody.Builder();
builder.add("query", critera.getQuery());
builder.add("artistCount", Integer.toString(critera.getArtistCount()));
@ -223,12 +224,12 @@ public class RESTMusicService implements MusicService {
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
return new SearchResult2Parser(context, getInstance(context)).parse(response.body().byteStream(), progressListener);
return new SearchResult2Parser(context, getInstance(context)).parse(response.body().byteStream());
}
}
@ -237,16 +238,16 @@ public class RESTMusicService implements MusicService {
String url = getRestUrl(context, "getPlaylist");
RequestBody formBody = new FormBody.Builder()
.add("id", id)
.build();
.add("id", id)
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
return new PlaylistParser(context, getInstance(context)).parse(response.body().byteStream(), progressListener);
return new PlaylistParser(context, getInstance(context)).parse(response.body().byteStream());
}
}
@ -255,11 +256,11 @@ public class RESTMusicService implements MusicService {
String url = getRestUrl(context, "getPlaylists");
Request request = new Request.Builder()
.url(url)
.build();
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
return new PlaylistsParser(context, getInstance(context)).parse(response.body().byteStream(), progressListener);
return new PlaylistsParser(context, getInstance(context)).parse(response.body().byteStream());
}
}
@ -267,7 +268,7 @@ public class RESTMusicService implements MusicService {
public void createPlaylist(String id, String name, List<MusicDirectory.Entry> entries, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "createPlaylist");
Builder builder= new FormBody.Builder();
Builder builder = new FormBody.Builder();
if (id != null) {
builder.add("playlistId", id);
@ -284,9 +285,9 @@ public class RESTMusicService implements MusicService {
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
new ErrorParser(context, getInstance(context)).parse(response.body().byteStream());
@ -298,13 +299,13 @@ public class RESTMusicService implements MusicService {
String url = getRestUrl(context, "deletePlaylist");
RequestBody formBody = new FormBody.Builder()
.add("id", id)
.build();
.add("id", id)
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
new ErrorParser(context, getInstance(context)).parse(response.body().byteStream());
@ -315,18 +316,18 @@ public class RESTMusicService implements MusicService {
public void addToPlaylist(String id, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "updatePlaylist");
Builder builder= new FormBody.Builder();
Builder builder = new FormBody.Builder();
builder.add("playlistId", id);
for(MusicDirectory.Entry song: toAdd) {
for (MusicDirectory.Entry song : toAdd) {
builder.add("songIdToAdd", getOfflineSongId(song.getId(), context, progressListener));
}
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
new ErrorParser(context, getInstance(context)).parse(response.body().byteStream());
@ -337,19 +338,19 @@ public class RESTMusicService implements MusicService {
public void removeFromPlaylist(String id, List<Integer> toRemove, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "updatePlaylist");
Builder builder= new FormBody.Builder();
Builder builder = new FormBody.Builder();
builder.add("playlistId", id);
for(Integer song: toRemove) {
for (Integer song : toRemove) {
builder.add("songIndexToRemove", Integer.toString(song));
}
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
new ErrorParser(context, getInstance(context)).parse(response.body().byteStream());
@ -360,24 +361,24 @@ public class RESTMusicService implements MusicService {
public void overwritePlaylist(String id, String name, int toRemove, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "updatePlaylist");
Builder builder= new FormBody.Builder();
Builder builder = new FormBody.Builder();
builder.add("playlistId", id);
builder.add("name", name);
for(MusicDirectory.Entry song: toAdd) {
for (MusicDirectory.Entry song : toAdd) {
builder.add("songIdToAdd", getOfflineSongId(song.getId(), context, progressListener));
}
for(int i = 0; i < toRemove; i++) {
for (int i = 0; i < toRemove; i++) {
builder.add("songIndexToRemove", Integer.toString(i));
}
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
new ErrorParser(context, getInstance(context)).parse(response.body().byteStream());
@ -388,7 +389,7 @@ public class RESTMusicService implements MusicService {
public void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "updatePlaylist");
Builder builder= new FormBody.Builder();
Builder builder = new FormBody.Builder();
builder.add("playlistId", id);
builder.add("name", name);
builder.add("comment", comment);
@ -397,9 +398,9 @@ public class RESTMusicService implements MusicService {
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
new ErrorParser(context, getInstance(context)).parse(response.body().byteStream());
@ -410,16 +411,16 @@ public class RESTMusicService implements MusicService {
public MusicDirectory getAlbumList(String type, int size, int offset, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "getAlbumList2");
Builder builder= new FormBody.Builder();
Builder builder = new FormBody.Builder();
builder.add("type", type);
builder.add("size", Integer.toString(size));
builder.add("offset", Integer.toString(offset));
// Add folder if it was set and is non null
int instance = getInstance(context);
if(Util.getAlbumListsPerFolder(context, instance)) {
if (Util.getAlbumListsPerFolder(context, instance)) {
String folderId = Util.getSelectedMusicFolderId(context, instance);
if(folderId != null) {
if (folderId != null) {
builder.add("musicFolderId", folderId);
}
}
@ -427,12 +428,12 @@ public class RESTMusicService implements MusicService {
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
return new EntryListParser(context, getInstance(context)).parse(response.body().byteStream(), progressListener);
return new EntryListParser(context, getInstance(context)).parse(response.body().byteStream());
}
}
@ -440,15 +441,15 @@ public class RESTMusicService implements MusicService {
public MusicDirectory getAlbumList(String type, String extra, int size, int offset, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "getAlbumList2");
Builder builder= new FormBody.Builder();
Builder builder = new FormBody.Builder();
builder.add("size", Integer.toString(size));
builder.add("offset", Integer.toString(offset));
int instance = getInstance(context);
if("genres".equals(type)) {
if ("genres".equals(type)) {
builder.add("type", "byGenre");
builder.add("genre", extra);
} else if("years".equals(type)) {
} else if ("years".equals(type)) {
int decade = Integer.parseInt(extra);
builder.add("type", "byYear");
@ -457,9 +458,9 @@ public class RESTMusicService implements MusicService {
}
// Add folder if it was set and is non null
if(Util.getAlbumListsPerFolder(context, instance)) {
if (Util.getAlbumListsPerFolder(context, instance)) {
String folderId = Util.getSelectedMusicFolderId(context, instance);
if(folderId != null) {
if (folderId != null) {
builder.add("musicFolderId", folderId);
}
}
@ -467,23 +468,23 @@ public class RESTMusicService implements MusicService {
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
return new EntryListParser(context, getInstance(context)).parse(response.body().byteStream(), progressListener);
return new EntryListParser(context, getInstance(context)).parse(response.body().byteStream());
}
}
@Override
public MusicDirectory getSongList(String type, int size, int offset, Context context, ProgressListener progressListener) throws Exception {
Builder builder= new FormBody.Builder();
Builder builder = new FormBody.Builder();
builder.add("size", Integer.toString(size));
builder.add("offset", Integer.toString(offset));
String method;
switch(type) {
switch (type) {
case MainFragment.SONGS_NEWEST:
method = "getNewaddedSongs";
break;
@ -505,66 +506,43 @@ public class RESTMusicService implements MusicService {
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
return new EntryListParser(context, getInstance(context)).parse(response.body().byteStream(), progressListener);
}
}
@Override
public MusicDirectory getRandomSongs(int size, String artistId, Context context, ProgressListener progressListener) throws Exception {
Builder builder= new FormBody.Builder();
builder.add("id", artistId);
builder.add("count", Integer.toString(size));
String url = getRestUrl(context, "getSimilarSongs2");
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
return new RandomSongsParser(context, getInstance(context)).parse(response.body().byteStream(), progressListener);
return new EntryListParser(context, getInstance(context)).parse(response.body().byteStream());
}
}
@Override
public MusicDirectory getRandomSongs(int size, String musicFolderId, String genre, String startYear, String endYear, Context context, ProgressListener progressListener) throws Exception {
Builder builder= new FormBody.Builder();
Builder builder = new FormBody.Builder();
builder.add("size", Integer.toString(size));
if (musicFolderId != null && !"".equals(musicFolderId) && !Util.isTagBrowsing(context, getInstance(context))) {
builder.add("musicFolderId", musicFolderId);
}
if(genre != null && !"".equals(genre)) {
if (genre != null && !"".equals(genre)) {
builder.add("genre", genre);
}
if(startYear != null && !"".equals(startYear)) {
if (startYear != null && !"".equals(startYear)) {
// Check to make sure user isn't doing 2015 -> 2010 since Subsonic will return no results
if(endYear != null && !"".equals(endYear)) {
if (endYear != null && !"".equals(endYear)) {
try {
int startYearInt = Integer.parseInt(startYear);
int endYearInt = Integer.parseInt(endYear);
if(startYearInt > endYearInt) {
if (startYearInt > endYearInt) {
String tmp = startYear;
startYear = endYear;
endYear = tmp;
}
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Failed to convert start/end year into ints", e);
}
}
builder.add("fromYear", startYear);
}
if(endYear != null && !"".equals(endYear)) {
if (endYear != null && !"".equals(endYear)) {
builder.add("toYear", endYear);
}
@ -573,12 +551,12 @@ public class RESTMusicService implements MusicService {
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
return new RandomSongsParser(context, getInstance(context)).parse(response.body().byteStream(), progressListener);
return new RandomSongsParser(context, getInstance(context)).parse(response.body().byteStream());
}
}
@ -596,15 +574,15 @@ public class RESTMusicService implements MusicService {
String url = getRestUrl(context, "getCoverArt");
Builder builder= new FormBody.Builder();
Builder builder = new FormBody.Builder();
builder.add("id", entry.getCoverArt());
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
InputStream in = response.body().byteStream();
@ -612,7 +590,7 @@ public class RESTMusicService implements MusicService {
byte[] bytes = Util.toByteArray(in);
// Handle case where partial was downloaded before being cancelled
if(task != null && task.isCancelled()) {
if (task != null && task.isCancelled()) {
return null;
}
@ -625,7 +603,7 @@ public class RESTMusicService implements MusicService {
}
// Size == 0 -> only want to download
if(size == 0) {
if (size == 0) {
return null;
} else {
return FileUtil.getSampledBitmap(bytes, size);
@ -638,8 +616,8 @@ public class RESTMusicService implements MusicService {
public Response getDownloadInputStream(Context context, MusicDirectory.Entry song, long offset, int maxBitrate, SilentBackgroundTask task) throws Exception {
OkHttpClient eagerClient = client.newBuilder()
.readTimeout(30, TimeUnit.SECONDS)
.build();
.readTimeout(30, TimeUnit.SECONDS)
.build();
String url = getRestUrl(context, "stream");
@ -649,7 +627,7 @@ public class RESTMusicService implements MusicService {
RequestBody formBody = builder.build();
Request.Builder requestBuilder= new Request.Builder();
Request.Builder requestBuilder = new Request.Builder();
if (offset > 0) {
requestBuilder.header("Range", "bytes=" + offset + "-");
}
@ -659,8 +637,7 @@ public class RESTMusicService implements MusicService {
Request request = requestBuilder.build();
Response response = eagerClient.newCall(request).execute();
return response;
return eagerClient.newCall(request).execute();
}
@ -669,26 +646,26 @@ public class RESTMusicService implements MusicService {
String url = getRestUrl(context, "getGenres");
Request request = new Request.Builder()
.url(url)
.build();
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
return new GenreParser(context, getInstance(context)).parse(response.body().byteStream(), progressListener);
return new GenreParser(context, getInstance(context)).parse(response.body().byteStream());
}
}
@Override
public MusicDirectory getSongsByGenre(String genre, int count, int offset, Context context, ProgressListener progressListener) throws Exception {
Builder builder= new FormBody.Builder();
Builder builder = new FormBody.Builder();
builder.add("genre", genre);
builder.add("count", Integer.toString(count));
builder.add("offset", Integer.toString(offset));
// Add folder if it was set and is non null
int instance = getInstance(context);
if(Util.getAlbumListsPerFolder(context, instance)) {
if (Util.getAlbumListsPerFolder(context, instance)) {
String folderId = Util.getSelectedMusicFolderId(context, instance);
if(folderId != null) {
if (folderId != null) {
builder.add("musicFolderId", folderId);
}
}
@ -698,12 +675,12 @@ public class RESTMusicService implements MusicService {
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
return new RandomSongsParser(context, getInstance(context)).parse(response.body().byteStream(), progressListener);
return new RandomSongsParser(context, getInstance(context)).parse(response.body().byteStream());
}
}
@ -712,17 +689,17 @@ public class RESTMusicService implements MusicService {
String url = getRestUrl(context, "getUser");
RequestBody formBody = new FormBody.Builder()
.add("username", username)
.build();
.add("username", username)
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
List<User> users = new UserParser(context, getInstance(context)).parse(response.body().byteStream(), progressListener);
if(users.size() > 0) {
List<User> users = new UserParser(context, getInstance(context)).parse(response.body().byteStream());
if (users.size() > 0) {
// Should only have returned one anyways
return users.get(0);
} else {
@ -731,91 +708,15 @@ public class RESTMusicService implements MusicService {
}
}
@Override
public Bitmap getBitmap(String method, int size, Context context, ProgressListener progressListener, SilentBackgroundTask task) throws Exception {
// Synchronize on the url so that we don't download concurrently
synchronized (method) {
// Use cached file, if existing.
Bitmap bitmap = FileUtil.getMiscBitmap(context, method, size);
if(bitmap != null) {
return bitmap;
}
String url = getRestUrl(context, method);
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
InputStream in = response.body().byteStream();
byte[] bytes = Util.toByteArray(in);
if(task != null && task.isCancelled()) {
// Handle case where partial is downloaded and cancelled
return null;
}
OutputStream out = null;
try {
out = new FileOutputStream(FileUtil.getMiscFile(context, url));
out.write(bytes);
} finally {
Util.close(out);
}
return FileUtil.getSampledBitmap(bytes, size, false);
}
}
}
@Override
public void savePlayQueue(List<MusicDirectory.Entry> songs, MusicDirectory.Entry currentPlaying, int position, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "savePlayQueue");
Builder builder= new FormBody.Builder();
builder.add("current", currentPlaying.getId());
builder.add("position", Integer.toString(position));
for(MusicDirectory.Entry song: songs) {
builder.add("id", song.getId());
}
RequestBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
new ErrorParser(context, getInstance(context)).parse(response.body().byteStream());
}
}
@Override
public PlayerQueue getPlayQueue(Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "getPlayQueue");
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
return new PlayQueueParser(context, getInstance(context)).parse(response.body().byteStream(), progressListener);
}
}
private String getOfflineSongId(String id, Context context, ProgressListener progressListener) throws Exception {
SharedPreferences prefs = Util.getPreferences(context);
String cacheLocn = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null);
if(cacheLocn != null && id.indexOf(cacheLocn) != -1) {
if (cacheLocn != null && id.contains(cacheLocn)) {
Pair<Integer, String> cachedSongId = SongDBHandler.getHandler(context).getIdFromPath(Util.getRestUrlHash(context, getInstance(context)), id);
if(cachedSongId != null) {
if (cachedSongId != null) {
id = cachedSongId.getSecond();
} else {
String searchCriteria = Util.parseOfflineIDSearch(context, id, cacheLocn);
String searchCriteria = Util.parseOfflineIDSearch(id, cacheLocn);
SearchCritera critera = new SearchCritera(searchCriteria, 0, 0, 1);
SearchResult result = search(critera, context, progressListener);
if (result.getSongs().size() == 1) {
@ -828,12 +729,12 @@ public class RESTMusicService implements MusicService {
}
@Override
public void setInstance(Integer instance) throws Exception {
public void setInstance(Integer instance) throws Exception {
this.instance = instance;
}
public int getInstance(Context context) {
if(instance == null) {
if (instance == null) {
return Util.getActiveServer(context);
} else {
return instance;
@ -846,7 +747,7 @@ public class RESTMusicService implements MusicService {
}
public String getRestUrl(Context context, String method, boolean allowAltAddress) {
if(instance == null) {
if (instance == null) {
return Util.getRestUrl(context, method, allowAltAddress);
} else {
return Util.getRestUrl(context, method, instance, allowAltAddress);

View File

@ -18,41 +18,41 @@
*/
package net.nullsum.audinaut.service.parser;
import java.io.IOException;
import java.io.InputStream;
import org.xmlpull.v1.XmlPullParser;
import android.content.Context;
import android.util.Log;
import android.util.Xml;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.util.ProgressListener;
import net.nullsum.audinaut.util.Util;
import org.xmlpull.v1.XmlPullParser;
import java.io.IOException;
import java.io.InputStream;
/**
* @author Sindre Mehus
*/
public abstract class AbstractParser {
abstract class AbstractParser {
private static final String TAG = AbstractParser.class.getSimpleName();
private static final String SUBSONIC_RESPONSE = "subsonic-response";
private static final String SUBSONIC = "subsonic";
protected final Context context;
protected final int instance;
final Context context;
private final int instance;
private XmlPullParser parser;
private boolean rootElementFound;
public AbstractParser(Context context, int instance) {
AbstractParser(Context context, int instance) {
this.context = context;
this.instance = instance;
}
protected Context getContext() {
Context getContext() {
return context;
}
protected void handleError() throws Exception {
void handleError() throws Exception {
int code = getInteger("code");
String message;
switch (code) {
@ -69,7 +69,7 @@ public abstract class AbstractParser {
message = context.getResources().getString(R.string.parser_not_authenticated);
break;
case 41:
Util.setBlockTokenUse(context, instance, true);
Util.setBlockTokenUse(context, instance);
// Throw IOException so RESTMusicService knows to retry
throw new IOException();
@ -80,77 +80,56 @@ public abstract class AbstractParser {
message = get("message");
break;
}
throw new SubsonicRESTException(code, message);
throw new SubsonicRESTException(message);
}
protected void updateProgress(ProgressListener progressListener, int messageId) {
if (progressListener != null) {
progressListener.updateProgress(messageId);
}
}
protected void updateProgress(ProgressListener progressListener, String message) {
void updateProgress(ProgressListener progressListener, String message) {
if (progressListener != null) {
progressListener.updateProgress(message);
}
}
protected String getText() {
String getText() {
return parser.getText();
}
protected String get(String name) {
String get(String name) {
return parser.getAttributeValue(null, name);
}
protected boolean getBoolean(String name) {
return "true".equals(get(name));
boolean getBoolean() {
return "true".equals(get("isDir"));
}
protected Integer getInteger(String name) {
Integer getInteger(String name) {
String s = get(name);
try {
return (s == null || "".equals(s)) ? null : Integer.valueOf(s);
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Failed to parse " + s + " into integer");
return null;
}
}
protected Long getLong(String name) {
String s = get(name);
return s == null ? null : Long.valueOf(s);
}
protected Float getFloat(String name) {
String s = get(name);
return s == null ? null : Float.valueOf(s);
}
protected void init(InputStream inputStream) throws Exception {
void init(InputStream inputStream) throws Exception {
parser = Xml.newPullParser();
parser.setInput(inputStream, "UTF-8");
rootElementFound = false;
}
protected int nextParseEvent() throws Exception {
try {
return parser.next();
} catch(Exception e) {
throw e;
}
int nextParseEvent() throws Exception {
return parser.next();
}
protected String getElementName() {
String getElementName() {
String name = parser.getName();
if (SUBSONIC_RESPONSE.equals(name)) {
rootElementFound = true;
String version = get("version");
}
return name;
}
protected void validate() throws Exception {
void validate() throws Exception {
if (!rootElementFound) {
throw new Exception(context.getResources().getString(R.string.background_task_parse_error));
}

View File

@ -19,9 +19,9 @@
package net.nullsum.audinaut.service.parser;
import android.content.Context;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.util.ProgressListener;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;
@ -35,7 +35,7 @@ public class EntryListParser extends MusicDirectoryEntryParser {
super(context, instance);
}
public MusicDirectory parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
public MusicDirectory parse(InputStream inputStream) throws Exception {
init(inputStream);
MusicDirectory dir = new MusicDirectory();
@ -46,7 +46,7 @@ public class EntryListParser extends MusicDirectoryEntryParser {
String name = getElementName();
if ("album".equals(name)) {
MusicDirectory.Entry entry = parseEntry("");
if(get("isDir") == null) {
if (get("isDir") == null) {
entry.setDirectory(true);
}
dir.addChild(entry);

View File

@ -19,6 +19,7 @@
package net.nullsum.audinaut.service.parser;
import android.content.Context;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;

View File

@ -20,34 +20,29 @@ package net.nullsum.audinaut.service.parser;
import android.content.Context;
import android.text.Html;
import android.util.Log;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.Genre;
import net.nullsum.audinaut.util.ProgressListener;
import org.xmlpull.v1.XmlPullParser;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @author Joshua Bahnsen
*/
public class GenreParser extends AbstractParser {
private static final String TAG = GenreParser.class.getSimpleName();
public GenreParser(Context context, int instance) {
super(context, instance);
}
public List<Genre> parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
public List<Genre> parse(InputStream inputStream) throws Exception {
init(inputStream);
List<Genre> result = new ArrayList<Genre>();
List<Genre> result = new ArrayList<>();
Genre genre = null;
@ -80,12 +75,7 @@ public class GenreParser extends AbstractParser {
validate();
Collections.sort(result, new Comparator<Genre>() {
@Override
public int compare(Genre genre1, Genre genre2) {
return genre1.getName().compareTo(genre2.getName());
}
});
Collections.sort(result, (genre1, genre2) -> genre1.getName().compareTo(genre2.getName()));
return result;
}
}

View File

@ -18,25 +18,26 @@
*/
package net.nullsum.audinaut.service.parser;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import org.xmlpull.v1.XmlPullParser;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.Artist;
import net.nullsum.audinaut.domain.Indexes;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.util.ProgressListener;
import android.util.Log;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.ProgressListener;
import net.nullsum.audinaut.util.Util;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Sindre Mehus
*/
@ -51,15 +52,14 @@ public class IndexesParser extends MusicDirectoryEntryParser {
long t0 = System.currentTimeMillis();
init(inputStream);
List<Artist> artists = new ArrayList<Artist>();
List<Artist> shortcuts = new ArrayList<Artist>();
List<MusicDirectory.Entry> entries = new ArrayList<MusicDirectory.Entry>();
Long lastModified = null;
List<Artist> artists = new ArrayList<>();
List<Artist> shortcuts = new ArrayList<>();
List<MusicDirectory.Entry> entries = new ArrayList<>();
int eventType;
String index = "#";
String ignoredArticles = null;
boolean changed = false;
Map<String, Artist> artistList = new HashMap<String, Artist>();
Map<String, Artist> artistList = new HashMap<>();
do {
eventType = nextParseEvent();
@ -67,7 +67,6 @@ public class IndexesParser extends MusicDirectoryEntryParser {
String name = getElementName();
if ("indexes".equals(name) || "artists".equals(name)) {
changed = true;
lastModified = getLong("lastModified");
ignoredArticles = get("ignoredArticles");
} else if ("index".equals(name)) {
index = get("name");
@ -79,7 +78,7 @@ public class IndexesParser extends MusicDirectoryEntryParser {
artist.setIndex(index);
// Combine the id's for the two artists
if(artistList.containsKey(artist.getName())) {
if (artistList.containsKey(artist.getName())) {
Artist originalArtist = artistList.get(artist.getName());
originalArtist.setId(originalArtist.getId() + ";" + artist.getId());
} else {
@ -97,7 +96,7 @@ public class IndexesParser extends MusicDirectoryEntryParser {
shortcut.setName(get("name"));
shortcut.setIndex("*");
shortcuts.add(shortcut);
} else if("child".equals(name)) {
} else if ("child".equals(name)) {
MusicDirectory.Entry entry = parseEntry("");
entries.add(entry);
} else if ("error".equals(name)) {
@ -108,7 +107,7 @@ public class IndexesParser extends MusicDirectoryEntryParser {
validate();
if(ignoredArticles != null) {
if (ignoredArticles != null) {
SharedPreferences.Editor prefs = Util.getPreferences(context).edit();
prefs.putString(Constants.CACHE_KEY_IGNORE, ignoredArticles);
prefs.apply();
@ -124,6 +123,6 @@ public class IndexesParser extends MusicDirectoryEntryParser {
String msg = getContext().getResources().getString(R.string.parser_artist_count, artists.size());
updateProgress(progressListener, msg);
return new Indexes(lastModified == null ? 0L : lastModified, shortcuts, artists, entries);
return new Indexes(shortcuts, artists, entries);
}
}

View File

@ -25,21 +25,21 @@ import net.nullsum.audinaut.domain.MusicDirectory;
/**
* @author Sindre Mehus
*/
public class MusicDirectoryEntryParser extends AbstractParser {
public MusicDirectoryEntryParser(Context context, int instance) {
class MusicDirectoryEntryParser extends AbstractParser {
MusicDirectoryEntryParser(Context context, int instance) {
super(context, instance);
}
protected MusicDirectory.Entry parseEntry(String artist) {
MusicDirectory.Entry parseEntry(String artist) {
MusicDirectory.Entry entry = new MusicDirectory.Entry();
entry.setId(get("id"));
entry.setParent(get("parent"));
entry.setArtistId(get("artistId"));
entry.setTitle(get("title"));
if(entry.getTitle() == null) {
if (entry.getTitle() == null) {
entry.setTitle(get("name"));
}
entry.setDirectory(getBoolean("isDir"));
entry.setDirectory(getBoolean());
entry.setCoverArt(get("coverArt"));
entry.setArtist(get("artist"));
entry.setYear(getInteger("year"));
@ -53,27 +53,14 @@ public class MusicDirectoryEntryParser extends AbstractParser {
entry.setSuffix(get("suffix"));
entry.setTranscodedContentType(get("transcodedContentType"));
entry.setTranscodedSuffix(get("transcodedSuffix"));
entry.setSize(getLong("size"));
entry.setDuration(getInteger("duration"));
entry.setBitRate(getInteger("bitRate"));
entry.setPath(get("path"));
entry.setDiscNumber(getInteger("discNumber"));
String type = get("type");
} else if(!"".equals(artist)) {
} else if (!"".equals(artist)) {
entry.setPath(artist + "/" + entry.getTitle());
}
return 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);
return entry;
}
}

View File

@ -19,48 +19,42 @@
package net.nullsum.audinaut.service.parser;
import android.content.Context;
import android.util.Log;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.ProgressListener;
import net.nullsum.audinaut.util.Util;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import static net.nullsum.audinaut.domain.MusicDirectory.*;
import static net.nullsum.audinaut.domain.MusicDirectory.Entry;
/**
* @author Sindre Mehus
*/
public class MusicDirectoryParser extends MusicDirectoryEntryParser {
private static final String TAG = MusicDirectoryParser.class.getSimpleName();
public MusicDirectoryParser(Context context, int instance) {
super(context, instance);
}
public MusicDirectory parse(String artist, InputStream inputStream, ProgressListener progressListener) throws Exception {
public MusicDirectory parse(String artist, InputStream inputStream) throws Exception {
init(inputStream);
MusicDirectory dir = new MusicDirectory();
int eventType;
boolean isArtist = false;
Map<String, Entry> titleMap = new HashMap<String, Entry>();
Map<String, Entry> titleMap = new HashMap<>();
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());
// Only check for songs
if(!entry.isDirectory()) {
if (!entry.isDirectory()) {
// Check if duplicates
String disc = (entry.getDiscNumber() != null) ? Integer.toString(entry.getDiscNumber()) : "";
String track = (entry.getTrack() != null) ? Integer.toString(entry.getTrack()) : "";
@ -84,13 +78,9 @@ public class MusicDirectoryParser extends MusicDirectoryEntryParser {
} 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"));
}
dir.setParent(get("artistId"));
isArtist = true;
} else if("album".equals(name)) {
} else if ("album".equals(name)) {
Entry entry = parseEntry(artist);
entry.setDirectory(true);
dir.addChild(entry);

View File

@ -18,16 +18,15 @@
*/
package net.nullsum.audinaut.service.parser;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import net.nullsum.audinaut.domain.MusicFolder;
import org.xmlpull.v1.XmlPullParser;
import android.content.Context;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.MusicFolder;
import net.nullsum.audinaut.util.ProgressListener;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* @author Sindre Mehus
@ -38,10 +37,10 @@ public class MusicFoldersParser extends AbstractParser {
super(context, instance);
}
public List<MusicFolder> parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
public List<MusicFolder> parse(InputStream inputStream) throws Exception {
init(inputStream);
List<MusicFolder> result = new ArrayList<MusicFolder>();
List<MusicFolder> result = new ArrayList<>();
int eventType;
do {
eventType = nextParseEvent();

View File

@ -1,83 +0,0 @@
/*
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.service.parser;
import android.content.Context;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
import net.nullsum.audinaut.domain.MusicDirectory;
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();
public PlayQueueParser(Context context, int instance) {
super(context, instance);
}
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
state.songs.add(entry);
} 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;
}
validate();
return state;
}
}

View File

@ -19,9 +19,9 @@
package net.nullsum.audinaut.service.parser;
import android.content.Context;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.util.ProgressListener;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;
@ -35,7 +35,7 @@ public class PlaylistParser extends MusicDirectoryEntryParser {
super(context, instance);
}
public MusicDirectory parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
public MusicDirectory parse(InputStream inputStream) throws Exception {
init(inputStream);
MusicDirectory dir = new MusicDirectory();

View File

@ -21,7 +21,7 @@ package net.nullsum.audinaut.service.parser;
import android.content.Context;
import net.nullsum.audinaut.domain.Playlist;
import net.nullsum.audinaut.util.ProgressListener;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;
@ -37,10 +37,10 @@ public class PlaylistsParser extends AbstractParser {
super(context, instance);
}
public List<Playlist> parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
public List<Playlist> parse(InputStream inputStream) throws Exception {
init(inputStream);
List<Playlist> result = new ArrayList<Playlist>();
List<Playlist> result = new ArrayList<>();
int eventType;
do {
eventType = nextParseEvent();

View File

@ -19,9 +19,9 @@
package net.nullsum.audinaut.service.parser;
import android.content.Context;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.util.ProgressListener;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;
@ -35,7 +35,7 @@ public class RandomSongsParser extends MusicDirectoryEntryParser {
super(context, instance);
}
public MusicDirectory parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
public MusicDirectory parse(InputStream inputStream) throws Exception {
init(inputStream);
MusicDirectory dir = new MusicDirectory();

View File

@ -19,16 +19,16 @@
package net.nullsum.audinaut.service.parser;
import android.content.Context;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.Artist;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.domain.SearchResult;
import net.nullsum.audinaut.domain.Artist;
import net.nullsum.audinaut.util.ProgressListener;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;
import java.util.List;
import java.util.ArrayList;
import java.util.List;
/**
* @author Sindre Mehus
@ -39,12 +39,12 @@ public class SearchResult2Parser extends MusicDirectoryEntryParser {
super(context, instance);
}
public SearchResult parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
public SearchResult parse(InputStream inputStream) throws Exception {
init(inputStream);
List<Artist> artists = new ArrayList<Artist>();
List<MusicDirectory.Entry> albums = new ArrayList<MusicDirectory.Entry>();
List<MusicDirectory.Entry> songs = new ArrayList<MusicDirectory.Entry>();
List<Artist> artists = new ArrayList<>();
List<MusicDirectory.Entry> albums = new ArrayList<>();
List<MusicDirectory.Entry> songs = new ArrayList<>();
int eventType;
do {
eventType = nextParseEvent();

View File

@ -4,16 +4,10 @@ package net.nullsum.audinaut.service.parser;
* @author Sindre Mehus
* @version $Id$
*/
public class SubsonicRESTException extends Exception {
class SubsonicRESTException extends Exception {
private final int code;
public SubsonicRESTException(int code, String message) {
public SubsonicRESTException(String message) {
super(message);
this.code = code;
}
public int getCode() {
return code;
}
}

View File

@ -16,7 +16,12 @@
package net.nullsum.audinaut.service.parser;
import android.content.Context;
import android.util.Log;
import net.nullsum.audinaut.domain.MusicFolder;
import net.nullsum.audinaut.domain.User;
import net.nullsum.audinaut.domain.User.Setting;
import net.nullsum.audinaut.service.MusicService;
import net.nullsum.audinaut.service.MusicServiceFactory;
import org.xmlpull.v1.XmlPullParser;
@ -24,24 +29,15 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import net.nullsum.audinaut.domain.MusicFolder;
import net.nullsum.audinaut.domain.User;
import net.nullsum.audinaut.domain.User.MusicFolderSetting;
import net.nullsum.audinaut.domain.User.Setting;
import net.nullsum.audinaut.service.MusicService;
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();
public UserParser(Context context, int instance) {
super(context, instance);
}
public List<User> parse(InputStream inputStream, ProgressListener progressListener) throws Exception {
public List<User> parse(InputStream inputStream) throws Exception {
init(inputStream);
List<User> result = new ArrayList<User>();
List<User> result = new ArrayList<>();
List<MusicFolder> musicFolders = null;
User user = null;
int eventType;
@ -54,9 +50,7 @@ public class UserParser extends AbstractParser {
if ("user".equals(tagName)) {
user = new User();
user.setUsername(get("username"));
user.setEmail(get("email"));
for(String role: User.ROLES) {
for (String role : User.ROLES) {
parseSetting(user, role);
}
@ -64,22 +58,22 @@ public class UserParser extends AbstractParser {
} else if ("error".equals(tagName)) {
handleError();
}
} else if(eventType == XmlPullParser.TEXT) {
if("folder".equals(tagName)) {
} else if (eventType == XmlPullParser.TEXT) {
if ("folder".equals(tagName)) {
String id = getText();
if(musicFolders == null) {
if (musicFolders == null) {
musicFolders = getMusicFolders();
}
if(user != null) {
if(user.getMusicFolderSettings() == null) {
if (user != null) {
if (user.getMusicFolderSettings() == null) {
for (MusicFolder musicFolder : musicFolders) {
user.addMusicFolder(musicFolder);
}
}
for(Setting musicFolder: user.getMusicFolderSettings()) {
if(musicFolder.getName().equals(id)) {
for (Setting musicFolder : user.getMusicFolderSettings()) {
if (musicFolder.getName().equals(id)) {
musicFolder.setValue(true);
break;
}
@ -94,14 +88,14 @@ public class UserParser extends AbstractParser {
return result;
}
private List<MusicFolder> getMusicFolders() throws Exception{
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) {
if (value != null) {
user.addSetting(name, "true".equals(value));
}
}

View File

@ -21,24 +21,25 @@ package net.nullsum.audinaut.updates;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.SilentBackgroundTask;
import net.nullsum.audinaut.util.Util;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Scott
*/
public class Updater {
protected String TAG = Updater.class.getSimpleName();
protected int version;
protected Context context;
private final int version;
String TAG = Updater.class.getSimpleName();
private Context context;
public Updater(int version) {
// 5.2 should show as 520 instead of 52
if(version < 100) {
if (version < 100) {
version *= 10;
}
this.version = version;
@ -46,34 +47,41 @@ public class Updater {
public void checkUpdates(Context context) {
this.context = context;
List<Updater> updaters = new ArrayList<Updater>();
List<Updater> updaters = new ArrayList<>();
updaters.add(new UpdaterSongPress());
SharedPreferences prefs = Util.getPreferences(context);
int lastVersion = prefs.getInt(Constants.LAST_VERSION, 0);
if(lastVersion == 0) {
if (lastVersion == 0) {
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(Constants.LAST_VERSION, version);
editor.apply();
}
else if(version > lastVersion) {
} 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)) {
for (Updater updater : updaters) {
if (updater.shouldUpdate(lastVersion)) {
new BackgroundUpdate(context, updater).execute();
}
}
}
}
public String getName() {
private String getName() {
return this.TAG;
}
private boolean shouldUpdate(int version) {
return this.version > version;
}
void update(Context context) {
}
private class BackgroundUpdate extends SilentBackgroundTask<Void> {
private final Updater updater;
@ -86,17 +94,10 @@ public class Updater {
protected Void doInBackground() {
try {
updater.update(context);
} catch(Exception e) {
} 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) {
}
}

View File

@ -21,7 +21,7 @@ import android.content.SharedPreferences;
import net.nullsum.audinaut.util.Constants;
import net.nullsum.audinaut.util.Util;
public class UpdaterSongPress extends Updater {
class UpdaterSongPress extends Updater {
public UpdaterSongPress() {
super(521);
TAG = this.getClass().getSimpleName();
@ -33,7 +33,7 @@ public class UpdaterSongPress extends Updater {
boolean playNowAfter = prefs.getBoolean("playNowAfter", true);
// Migrate the old preference so behavior stays the same
if(playNowAfter == false) {
if (!playNowAfter) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(Constants.PREFERENCES_KEY_SONG_PRESS_ACTION, "single");
editor.apply();

View File

@ -18,85 +18,73 @@
*/
package net.nullsum.audinaut.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.view.ErrorDialog;
import org.xmlpull.v1.XmlPullParserException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* @author Sindre Mehus
*/
public abstract class BackgroundTask<T> implements ProgressListener {
static final BlockingQueue<BackgroundTask.Task> queue = new LinkedBlockingQueue<>(10);
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;
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) {
} catch (Exception e) {
// Not called from main thread
}
}
public BackgroundTask(Context context) {
final AtomicBoolean cancelled = new AtomicBoolean(false);
private final Context context;
private final Runnable onCompletionListener = null;
Task task;
BackgroundTask(Context context) {
this.context = context;
if(threads.size() < DEFAULT_CONCURRENCY) {
for(int i = threads.size(); i < DEFAULT_CONCURRENCY; i++) {
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) {
if (handler == null) {
try {
handler = new Handler(Looper.getMainLooper());
} catch(Exception e) {
} catch (Exception e) {
// Not called from main thread
}
}
}
public static void stopThreads() {
for(Thread thread: threads) {
thread.interrupt();
}
threads.clear();
queue.clear();
}
protected Activity getActivity() {
private Activity getActivity() {
return (context instanceof Activity) ? ((Activity) context) : null;
}
protected Context getContext() {
return context;
}
protected Handler getHandler() {
Handler getHandler() {
return handler;
}
@ -109,7 +97,7 @@ 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) {
if (activity != null) {
new ErrorDialog(activity, getErrorMessage(error), true);
}
}
@ -140,39 +128,27 @@ public abstract class BackgroundTask<T> implements ProgressListener {
}
public void cancel() {
if(cancelled.compareAndSet(false, true)) {
if(isRunning()) {
if(cancelListener != null) {
cancelListener.onCancel();
} else {
task.cancel();
}
if (cancelled.compareAndSet(false, true)) {
if (isRunning()) {
task.cancel();
}
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();
}
return task != null && task.isRunning();
}
@Override
public abstract void updateProgress(final String message);
@Override
public void updateProgress(int messageId) {
updateProgress(context.getResources().getString(messageId));
public void updateProgress() {
updateProgress(context.getResources().getString(R.string.settings_testing_connection));
}
@Override
@ -180,17 +156,13 @@ public abstract class BackgroundTask<T> implements ProgressListener {
}
public void setOnCompletionListener(Runnable onCompletionListener) {
this.onCompletionListener = onCompletionListener;
}
protected class Task {
class Task {
private final AtomicBoolean taskStart = new AtomicBoolean(false);
private Thread thread;
private AtomicBoolean taskStart = new AtomicBoolean(false);
private void execute() throws Exception {
// Don't run if cancelled already
if(isCancelled()) {
if (isCancelled()) {
return;
}
@ -199,60 +171,54 @@ public abstract class BackgroundTask<T> implements ProgressListener {
taskStart.set(true);
final T result = doInBackground();
if(isCancelled()) {
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(() -> {
if (!isCancelled()) {
try {
onDone(result);
} catch (Throwable t) {
if (!isCancelled()) {
try {
onError(t);
} catch (Exception e) {
// Don't care
}
}
}
taskStart.set(false);
}
taskStart.set(false);
});
} else {
taskStart.set(false);
}
} catch(InterruptedException interrupt) {
if(taskStart.get()) {
} catch (InterruptedException interrupt) {
if (taskStart.get()) {
// Don't exit root thread if task cancelled
throw interrupt;
}
} catch(final Throwable t) {
if(isCancelled()) {
} 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(() -> {
if (!isCancelled()) {
try {
onError(t);
} catch (Exception e) {
// Don't care
}
taskStart.set(false);
}
taskStart.set(false);
});
} else {
taskStart.set(false);
@ -263,28 +229,25 @@ public abstract class BackgroundTask<T> implements ProgressListener {
}
public void cancel() {
if(taskStart.compareAndSet(true, false)) {
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;
}
return Thread.interrupted() || BackgroundTask.this.isCancelled();
}
public void onDone(T result) {
done(result);
if(onCompletionListener != null) {
if (onCompletionListener != null) {
onCompletionListener.run();
}
}
public void onError(Throwable t) {
error(t);
}
@ -304,22 +267,18 @@ public abstract class BackgroundTask<T> implements ProgressListener {
@Override
public void run() {
Looper.prepare();
while(running) {
while (running) {
try {
Task task = queue.take();
task.execute();
} catch(InterruptedException stop) {
} catch (InterruptedException stop) {
Log.e(TAG, "Thread died");
running = false;
threads.remove(Thread.currentThread());
} catch(Throwable t) {
} catch (Throwable t) {
Log.e(TAG, "Unexpected crash in BackgroundTask thread", t);
}
}
}
}
public static interface OnCancelListener {
void onCancel();
}
}

View File

@ -19,10 +19,16 @@ import java.io.File;
public interface BufferFile {
File getFile();
Long getContentLength();
long getEstimatedSize();
boolean isWorkDone();
void onStart();
void onStop();
void onResume();
}

View File

@ -18,17 +18,8 @@ package net.nullsum.audinaut.util;
import java.io.File;
import java.net.Socket;
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;
public BufferProxy(Context context) {
super(context);
}
private BufferFile progress;
protected ProxyTask getTask(Socket client) {
return new BufferFileTask(client);
@ -51,11 +42,12 @@ public class BufferProxy extends FileProxy {
@Override
Long getContentLength() {
Long contentLength = progress.getContentLength();
if(contentLength == null && progress.isWorkDone()) {
if (contentLength == null && progress.isWorkDone()) {
contentLength = file.length();
}
return contentLength;
}
@Override
long getFileSize() {
return progress.getEstimatedSize();
@ -65,10 +57,12 @@ public class BufferProxy extends FileProxy {
public void onStart() {
progress.onStart();
}
@Override
public void onStop() {
progress.onStop();
}
@Override
public void onResume() {
progress.onResume();

View File

@ -1,22 +1,21 @@
package net.nullsum.audinaut.util;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import android.content.Context;
import android.util.Log;
import android.os.StatFs;
import android.util.Log;
import net.nullsum.audinaut.domain.Playlist;
import net.nullsum.audinaut.service.DownloadFile;
import net.nullsum.audinaut.service.DownloadService;
import net.nullsum.audinaut.service.MediaStoreService;
import java.util.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
/**
* @author Sindre Mehus
@ -41,9 +40,11 @@ public class CacheCleaner {
public void clean() {
new BackgroundCleanup(context).execute();
}
public void cleanSpace() {
new BackgroundSpaceCleanup(context).execute();
}
public void cleanPlaylists(List<Playlist> playlists) {
new BackgroundPlaylistsCleanup(context, playlists).execute();
}
@ -59,7 +60,7 @@ public class CacheCleaner {
}
private long getMinimumDelete(List<File> files, List<File> pinned) {
if(files.size() == 0) {
if (files.size() == 0) {
return 0L;
}
@ -75,8 +76,8 @@ public class CacheCleaner {
// 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();
long bytesTotalFs = stat.getBlockCountLong() * stat.getBlockSizeLong();
long bytesAvailableFs = stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
long bytesUsedFs = bytesTotalFs - bytesAvailableFs;
long minFsAvailability = bytesTotalFs - MIN_FREE_SPACE;
@ -99,7 +100,7 @@ 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)) {
@ -134,22 +135,19 @@ public class CacheCleaner {
}
private void sortByAscendingModificationTime(List<File> files) {
Collections.sort(files, new Comparator<File>() {
@Override
public int compare(File a, File b) {
if (a.lastModified() < b.lastModified()) {
return -1;
}
if (a.lastModified() > b.lastModified()) {
return 1;
}
return 0;
Collections.sort(files, (a, b) -> {
if (a.lastModified() < b.lastModified()) {
return -1;
}
if (a.lastModified() > b.lastModified()) {
return 1;
}
return 0;
});
}
private Set<File> findUndeletableFiles() {
Set<File> undeletable = new HashSet<File>(5);
Set<File> undeletable = new HashSet<>(5);
for (DownloadFile downloadFile : downloadService.getDownloads()) {
undeletable.add(downloadFile.getPartialFile());
@ -163,30 +161,30 @@ public class CacheCleaner {
private void cleanupCoverArt(Context context) {
File dir = FileUtil.getAlbumArtDirectory(context);
List<File> files = new ArrayList<File>();
List<File> files = new ArrayList<>();
long bytesUsed = 0L;
for(File file: dir.listFiles()) {
if(file.isFile()) {
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) {
if (bytesUsed < MAX_COVER_ART_SPACE) {
return;
}
sortByAscendingModificationTime(files);
long bytesDeleted = 0L;
for(File file: files) {
for (File file : files) {
// End as soon as the space used is below the threshold
if(bytesUsed < MAX_COVER_ART_SPACE) {
if (bytesUsed < MAX_COVER_ART_SPACE) {
break;
}
long bytes = file.length();
if(file.delete()) {
if (file.delete()) {
bytesUsed -= bytes;
bytesDeleted += bytes;
}
@ -208,9 +206,9 @@ public class CacheCleaner {
}
try {
List<File> files = new ArrayList<File>();
List<File> pinned = new ArrayList<File>();
List<File> dirs = new ArrayList<File>();
List<File> files = new ArrayList<>();
List<File> pinned = new ArrayList<>();
List<File> dirs = new ArrayList<>();
findCandidatesForDeletion(FileUtil.getMusicDirectory(context), files, pinned, dirs);
sortByAscendingModificationTime(files);
@ -243,13 +241,13 @@ public class CacheCleaner {
}
try {
List<File> files = new ArrayList<File>();
List<File> pinned = new ArrayList<File>();
List<File> dirs = new ArrayList<File>();
List<File> files = new ArrayList<>();
List<File> pinned = new ArrayList<>();
List<File> dirs = new ArrayList<>();
findCandidatesForDeletion(FileUtil.getMusicDirectory(context), files, pinned, dirs);
long bytesToDelete = getMinimumDelete(files, pinned);
if(bytesToDelete > 0L) {
if (bytesToDelete > 0L) {
sortByAscendingModificationTime(files);
Set<File> undeletable = findUndeletableFiles();
deleteFiles(files, undeletable, bytesToDelete, false);
@ -279,7 +277,7 @@ public class CacheCleaner {
playlistFiles.remove(FileUtil.getPlaylistFile(context, server, playlist.getName()));
}
for(File playlist : playlistFiles) {
for (File playlist : playlistFiles) {
playlist.delete();
}
} catch (RuntimeException x) {

View File

@ -37,7 +37,6 @@ public final class Constants {
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";
public static final String INTENT_EXTRA_NAME_QUERY = "subsonic.query";
public static final String INTENT_EXTRA_NAME_PLAYLIST_ID = "subsonic.playlist.id";
@ -48,18 +47,14 @@ public final class Constants {
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_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";
// Preferences keys.
public static final String PREFERENCES_KEY_SERVER_KEY = "server";
@ -76,7 +71,6 @@ public final class Constants {
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";
@ -98,7 +92,6 @@ public final class Constants {
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";
@ -106,7 +99,6 @@ public final class Constants {
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";
@ -115,8 +107,6 @@ public final class Constants {
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";
@ -126,7 +116,7 @@ public final class Constants {
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_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";
@ -134,12 +124,6 @@ public final class Constants {
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 CACHE_KEY_IGNORE = "ignoreArticles";
public static final String CACHE_AUDIO_SESSION_ID = "audioSessionId";
@ -152,21 +136,17 @@ public final class Constants {
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";
// 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";

View File

@ -2,10 +2,6 @@ package net.nullsum.audinaut.util;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import java.util.ArrayDeque;
import java.util.Deque;
import net.nullsum.audinaut.adapter.SectionAdapter;
import net.nullsum.audinaut.fragments.SubsonicFragment;
@ -14,14 +10,15 @@ import net.nullsum.audinaut.service.DownloadService;
import net.nullsum.audinaut.view.SongView;
import net.nullsum.audinaut.view.UpdateView;
import java.util.ArrayDeque;
import java.util.Deque;
public class DownloadFileItemHelperCallback extends ItemTouchHelper.SimpleCallback {
private static final String TAG = DownloadFileItemHelperCallback.class.getSimpleName();
private SubsonicFragment fragment;
private boolean mainList;
private final SubsonicFragment fragment;
private final boolean mainList;
private final 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);
@ -54,17 +51,18 @@ public class DownloadFileItemHelperCallback extends ItemTouchHelper.SimpleCallba
}
}
public DownloadService getDownloadService() {
private DownloadService getDownloadService() {
return fragment.getDownloadService();
}
public SectionAdapter getSectionAdapter() {
private SectionAdapter getSectionAdapter() {
return fragment.getCurrentAdapter();
}
private void updateDownloadService() {
if(pendingTask == null) {
if (pendingTask == null) {
final DownloadService downloadService = getDownloadService();
if(downloadService == null) {
if (downloadService == null) {
return;
}
@ -72,21 +70,21 @@ public class DownloadFileItemHelperCallback extends ItemTouchHelper.SimpleCallba
@Override
protected Void doInBackground() throws Throwable {
boolean running = true;
while(running) {
while (running) {
Object nextOperation = null;
synchronized (pendingOperations) {
if(!pendingOperations.isEmpty()) {
if (!pendingOperations.isEmpty()) {
nextOperation = pendingOperations.remove();
}
}
if(nextOperation != null) {
if(nextOperation instanceof Pair) {
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) {
} else if (nextOperation instanceof DownloadFile) {
DownloadFile downloadFile = (DownloadFile) nextOperation;
if(mainList) {
if (mainList) {
downloadService.remove(downloadFile);
} else {
downloadService.removeBackground(downloadFile);
@ -101,7 +99,7 @@ public class DownloadFileItemHelperCallback extends ItemTouchHelper.SimpleCallba
pendingTask = null;
// Start a task if this is non-empty. Means someone added while we were running operations
if(!pendingOperations.isEmpty()) {
if (!pendingOperations.isEmpty()) {
updateDownloadService();
}
}

View File

@ -21,48 +21,46 @@ import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.util.TypedValue;
import net.nullsum.audinaut.R;
import java.util.HashMap;
import java.util.Map;
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<>();
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) {
if (tintedDrawables.containsKey(R.drawable.ic_toggle_played)) {
return tintedDrawables.get(R.drawable.ic_toggle_played);
}
int color = getColorRes(context, colorAttr);
Drawable background = context.getResources().getDrawable(drawableRes);
int color = getColorRes(context, R.attr.colorAccent);
Drawable background = context.getResources().getDrawable(R.drawable.ic_toggle_played);
background.setColorFilter(color, PorterDuff.Mode.SRC_IN);
tintedDrawables.put(drawableRes, background);
tintedDrawables.put(R.drawable.ic_toggle_played, background);
return background;
}
public static Drawable getTintedDrawableFromColor(Context context, @DrawableRes int drawableRes, @ColorRes int colorRes) {
if(tintedDrawables.containsKey(drawableRes)) {
return tintedDrawables.get(drawableRes);
public static Drawable getTintedDrawableFromColor(Context context) {
if (tintedDrawables.containsKey(R.drawable.abc_spinner_mtrl_am_alpha)) {
return tintedDrawables.get(R.drawable.abc_spinner_mtrl_am_alpha);
}
int color = context.getResources().getColor(colorRes);
Drawable background = context.getResources().getDrawable(drawableRes);
int color = context.getResources().getColor(android.R.color.white);
Drawable background = context.getResources().getDrawable(R.drawable.abc_spinner_mtrl_am_alpha);
background.setColorFilter(color, PorterDuff.Mode.SRC_IN);
tintedDrawables.put(drawableRes, background);
tintedDrawables.put(R.drawable.abc_spinner_mtrl_am_alpha, background);
return background;
}
public static int getColorRes(Context context, @AttrRes int colorAttr) {
int color;
if(attrMap.containsKey(colorAttr)) {
if (attrMap.containsKey(colorAttr)) {
color = attrMap.get(colorAttr);
} else {
TypedValue typedValue = new TypedValue();
@ -74,8 +72,9 @@ public class DrawableTint {
return color;
}
public static int getDrawableRes(Context context, @AttrRes int drawableAttr) {
if(attrMap.containsKey(drawableAttr)) {
if (attrMap.containsKey(drawableAttr)) {
return attrMap.get(drawableAttr);
} else {
int[] attrs = new int[]{drawableAttr};
@ -86,14 +85,6 @@ public class DrawableTint {
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);
}
public static void wipeTintCache() {
attrMap.clear();

View File

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

View File

@ -15,6 +15,8 @@
package net.nullsum.audinaut.util;
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
@ -23,18 +25,9 @@ import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import android.content.Context;
import android.util.Log;
import net.nullsum.audinaut.util.ServerProxy;
public class FileProxy extends ServerProxy {
private static final String TAG = FileProxy.class.getSimpleName();
public FileProxy(Context context) {
super(context);
}
protected ProxyTask getTask(Socket client) {
return new StreamFileTask(client);
}
@ -48,7 +41,7 @@ public class FileProxy extends ServerProxy {
@Override
public boolean processRequest() {
if(!super.processRequest()) {
if (!super.processRequest()) {
return false;
}
@ -60,11 +53,7 @@ public class FileProxy extends ServerProxy {
}
// Make sure to not try to read past where the file is downloaded
if(cbSkip != 0 && cbSkip >= file.length()) {
return false;
}
return true;
return !(cbSkip != 0 && cbSkip >= file.length());
}
File getFile(String path) {
@ -74,6 +63,7 @@ public class FileProxy extends ServerProxy {
Long getContentLength() {
return file.length();
}
long getFileSize() {
return file.length();
}
@ -84,12 +74,12 @@ public class FileProxy extends ServerProxy {
// Create HTTP header
String headers;
if(cbSkip == 0) {
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) {
if (contentLength == null) {
headers += "*";
} else {
headers += contentLength;
@ -102,21 +92,21 @@ public class FileProxy extends ServerProxy {
String name = file.getPath();
int index = name.lastIndexOf('.');
String ext = "";
if(index != -1) {
if (index != -1) {
ext = name.substring(index + 1).toLowerCase();
}
if("mp3".equals(ext)) {
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) {
if (contentLength == null) {
fileSize = getFileSize();
} else {
fileSize = contentLength;
if(cbSkip > 0) {
if (cbSkip > 0) {
headers += "Content-Length: " + (fileSize - cbSkip) + "\r\n";
} else {
headers += "Content-Length: " + fileSize + "\r\n";
@ -132,7 +122,7 @@ public class FileProxy extends ServerProxy {
OutputStream output = null;
byte[] buff = new byte[64 * 1024];
try {
output = new BufferedOutputStream(client.getOutputStream(), 32*1024);
output = new BufferedOutputStream(client.getOutputStream(), 32 * 1024);
output.write(headers.getBytes());
// Make sure to have file lock
@ -165,7 +155,7 @@ public class FileProxy extends ServerProxy {
}
// Done regardless of whether or not it thinks it is
if(isWorkDone()) {
if (isWorkDone()) {
break;
}
@ -178,14 +168,12 @@ public class FileProxy extends ServerProxy {
// Release file lock, use of stream proxy means nothing else is using it
onStop();
}
catch (SocketException socketException) {
} 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) {
} catch (Exception e) {
Log.e(TAG, "Exception thrown from streaming task:");
Log.e(TAG, e.getClass().getName() + " : " + e.getLocalizedMessage());
}
@ -196,8 +184,7 @@ public class FileProxy extends ServerProxy {
output.close();
}
client.close();
}
catch (IOException e) {
} catch (IOException e) {
Log.e(TAG, "IOException while cleaning up streaming task:");
Log.e(TAG, e.getClass().getName() + " : " + e.getLocalizedMessage());
}
@ -206,12 +193,15 @@ public class FileProxy extends ServerProxy {
public void onStart() {
}
public void onStop() {
}
public void onResume() {
}
public boolean isWorkDone() {
return cbSkip >= file.length();
}

View File

@ -18,6 +18,26 @@
*/
package net.nullsum.audinaut.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import net.nullsum.audinaut.domain.Artist;
import net.nullsum.audinaut.domain.Genre;
import net.nullsum.audinaut.domain.Indexes;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.domain.MusicFolder;
import net.nullsum.audinaut.domain.Playlist;
import net.nullsum.audinaut.service.MediaStoreService;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
@ -28,34 +48,16 @@ import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import net.nullsum.audinaut.domain.Artist;
import net.nullsum.audinaut.domain.Genre;
import net.nullsum.audinaut.domain.Indexes;
import net.nullsum.audinaut.domain.Playlist;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.domain.MusicFolder;
import net.nullsum.audinaut.service.MediaStoreService;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
/**
* @author Sindre Mehus
*/
@ -65,10 +67,10 @@ public class FileUtil {
private static final String[] FILE_SYSTEM_UNSAFE = {"/", "\\", "..", ":", "\"", "?", "*", "<", ">", "|"};
private static final String[] FILE_SYSTEM_UNSAFE_DIR = {"\\", "..", ":", "\"", "?", "*", "<", ">", "|"};
private static final List<String> MUSIC_FILE_EXTENSIONS = Arrays.asList("mp3", "ogg", "aac", "flac", "m4a", "wav", "wma");
private static final List<String> PLAYLIST_FILE_EXTENSIONS = Arrays.asList("m3u");
private static final List<String> PLAYLIST_FILE_EXTENSIONS = Collections.singletonList("m3u");
private static final int MAX_FILENAME_LENGTH = 254 - ".complete.mp3".length();
private static File DEFAULT_MUSIC_DIR;
private static final Kryo kryo = new Kryo();
private static File DEFAULT_MUSIC_DIR;
private static HashMap<String, MusicDirectory.Entry> entryLookup;
static {
@ -80,27 +82,8 @@ public class FileUtil {
kryo.register(Genre.class);
}
public static File getAnySong(Context context) {
File dir = getMusicDirectory(context);
return getAnySong(context, dir);
}
private static File getAnySong(Context context, File dir) {
for(File file: dir.listFiles()) {
if(file.isDirectory()) {
return getAnySong(context, file);
}
String extension = getExtension(file.getName());
if(MUSIC_FILE_EXTENSIONS.contains(extension)) {
return file;
}
}
return null;
}
public static File getEntryFile(Context context, MusicDirectory.Entry entry) {
if(entry.isDirectory()) {
if (entry.isDirectory()) {
return getAlbumDirectory(context, entry);
} else {
return getSongFile(context, entry);
@ -120,7 +103,7 @@ public class FileUtil {
}
fileName.append(fileSystemSafe(song.getTitle()));
if(fileName.length() >= MAX_FILENAME_LENGTH) {
if (fileName.length() >= MAX_FILENAME_LENGTH) {
fileName.setLength(MAX_FILENAME_LENGTH);
}
@ -138,6 +121,7 @@ public class FileUtil {
File playlistDir = getPlaylistDirectory(context, server);
return new File(playlistDir, fileSystemSafe(name) + ".m3u");
}
public static void writePlaylistFile(Context context, File file, MusicDirectory playlist) throws IOException {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
@ -145,25 +129,27 @@ public class FileUtil {
fw.write("#EXTM3U\n");
for (MusicDirectory.Entry e : playlist.getChildren()) {
String filePath = FileUtil.getSongFile(context, e).getAbsolutePath();
if(! new File(filePath).exists()){
if (!new File(filePath).exists()) {
String ext = FileUtil.getExtension(filePath);
String base = FileUtil.getBaseName(filePath);
filePath = base + ".complete." + ext;
}
fw.write(filePath + "\n");
}
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Failed to save playlist: " + playlist.getName());
} finally {
bw.close();
fw.close();
}
}
public static File getPlaylistDirectory(Context context) {
File playlistDir = new File(getSubsonicDirectory(context), "playlists");
ensureDirectoryExistsAndIsReadWritable(playlistDir);
return playlistDir;
}
public static File getPlaylistDirectory(Context context, String server) {
File playlistDir = new File(getPlaylistDirectory(context), server);
ensureDirectoryExistsAndIsReadWritable(playlistDir);
@ -171,9 +157,9 @@ public class FileUtil {
}
public static File getAlbumArtFile(Context context, MusicDirectory.Entry entry) {
if(entry.getId().indexOf(ImageLoader.PLAYLIST_PREFIX) != -1) {
if (entry.getId().contains(ImageLoader.PLAYLIST_PREFIX)) {
File dir = getAlbumArtDirectory(context);
return new File(dir, Util.md5Hex(ImageLoader.PLAYLIST_PREFIX + entry.getTitle()) + ".jpeg");
return new File(dir, Util.md5Hex(ImageLoader.PLAYLIST_PREFIX + entry.getTitle()) + ".jpeg");
} else {
File albumDir = getAlbumDirectory(context, entry);
File artFile;
@ -191,10 +177,11 @@ public class FileUtil {
}
}
public static File getAlbumArtFile(File albumDir) {
private static File getAlbumArtFile(File albumDir) {
return new File(albumDir, Constants.ALBUM_ART_FILE);
}
public static File getHexAlbumArtFile(Context context, File albumDir) {
private static File getHexAlbumArtFile(Context context, File albumDir) {
return new File(getAlbumArtDirectory(context), Util.md5Hex(albumDir.getPath()) + ".jpeg");
}
@ -214,25 +201,7 @@ public class FileUtil {
return null;
}
public static File getMiscDirectory(Context context) {
File dir = new File(getSubsonicDirectory(context), "misc");
ensureDirectoryExistsAndIsReadWritable(dir);
ensureDirectoryExistsAndIsReadWritable(new File(dir, ".nomedia"));
return dir;
}
public static File getMiscFile(Context context, String url) {
return new File(getMiscDirectory(context), Util.md5Hex(url) + ".jpeg");
}
public static Bitmap getMiscBitmap(Context context, String url, int size) {
return null;
}
public static Bitmap getSampledBitmap(byte[] bytes, int size) {
return getSampledBitmap(bytes, size, true);
}
public static Bitmap getSampledBitmap(byte[] bytes, int size, boolean allowUnscaled) {
final BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opt);
@ -240,19 +209,17 @@ public class FileUtil {
opt.inSampleSize = Util.calculateInSampleSize(opt, size, Util.getScaledHeight(opt.outHeight, opt.outWidth, size));
opt.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opt);
if(bitmap == null) {
if (bitmap == null) {
return null;
} else {
return getScaledBitmap(bitmap, size, allowUnscaled);
return getScaledBitmap(bitmap, size);
}
}
public static Bitmap getScaledBitmap(Bitmap bitmap, int size) {
return getScaledBitmap(bitmap, size, true);
}
public static Bitmap getScaledBitmap(Bitmap bitmap, int size, boolean allowUnscaled) {
private static Bitmap getScaledBitmap(Bitmap bitmap, int size) {
// Don't waste time scaling if the difference is minor
// Large album arts still need to be scaled since displayed as is on now playing!
if(allowUnscaled && size < 400 && bitmap.getWidth() < (size * 1.1)) {
if (size < 400 && bitmap.getWidth() < (size * 1.1)) {
return bitmap;
} else {
return Bitmap.createScaledBitmap(bitmap, size, Util.getScaledHeight(bitmap, size), true);
@ -267,12 +234,11 @@ public class FileUtil {
}
public static File getArtistDirectory(Context context, Artist artist) {
File dir = new File(getMusicDirectory(context).getPath() + "/" + fileSystemSafe(artist.getName()));
return dir;
return new File(getMusicDirectory(context).getPath() + "/" + fileSystemSafe(artist.getName()));
}
public static File getArtistDirectory(Context context, MusicDirectory.Entry artist) {
File dir = new File(getMusicDirectory(context).getPath() + "/" + fileSystemSafe(artist.getTitle()));
return dir;
return new File(getMusicDirectory(context).getPath() + "/" + fileSystemSafe(artist.getTitle()));
}
public static File getAlbumDirectory(Context context, MusicDirectory.Entry entry) {
@ -280,26 +246,26 @@ public class FileUtil {
if (entry.getPath() != null) {
File f = new File(fileSystemSafeDir(entry.getPath()));
String folder = getMusicDirectory(context).getPath();
if(entry.isDirectory()) {
if (entry.isDirectory()) {
folder += "/" + f.getPath();
} else if(f.getParent() != null) {
} else if (f.getParent() != null) {
folder += "/" + f.getParent();
}
dir = new File(folder);
} else {
MusicDirectory.Entry firstSong;
if(!Util.isOffline(context)) {
if (!Util.isOffline(context)) {
firstSong = lookupChild(context, entry, false);
if(firstSong != null) {
if (firstSong != null) {
File songFile = FileUtil.getSongFile(context, firstSong);
dir = songFile.getParentFile();
}
}
if(dir == null) {
if (dir == null) {
String artist = fileSystemSafe(entry.getArtist());
String album = fileSystemSafe(entry.getAlbum());
if("unnamed".equals(album)) {
if ("unnamed".equals(album)) {
album = fileSystemSafe(entry.getTitle());
}
dir = new File(getMusicDirectory(context).getPath() + "/" + artist + "/" + album);
@ -310,30 +276,30 @@ public class FileUtil {
public static MusicDirectory.Entry lookupChild(Context context, MusicDirectory.Entry entry, boolean allowDir) {
// Initialize lookupMap if first time called
String lookupName = Util.getCacheName(context, "entryLookup");
if(entryLookup == null) {
String lookupName = Util.getCacheName(context);
if (entryLookup == null) {
entryLookup = deserialize(context, lookupName, HashMap.class);
// Create it if
if(entryLookup == null) {
entryLookup = new HashMap<String, MusicDirectory.Entry>();
if (entryLookup == null) {
entryLookup = new HashMap<>();
}
}
// Check if this lookup has already been done before
MusicDirectory.Entry child = entryLookup.get(entry.getId());
if(child != null) {
if (child != null) {
return child;
}
// Do a special lookup since 4.7+ doesn't match artist/album to entry.getPath
String s = Util.getRestUrl(context, null, false) + entry.getId();
String cacheName = (Util.isTagBrowsing(context) ? "album-" : "directory-") + s.hashCode() + ".ser";
String cacheName = "album-" + s.hashCode() + ".ser";
MusicDirectory entryDir = FileUtil.deserialize(context, cacheName, MusicDirectory.class);
if(entryDir != null) {
if (entryDir != null) {
List<MusicDirectory.Entry> songs = entryDir.getChildren(allowDir, true);
if(songs.size() > 0) {
if (songs.size() > 0) {
child = songs.get(0);
entryLookup.put(entry.getId(), child);
serialize(context, entryLookup, lookupName);
@ -353,22 +319,14 @@ public class FileUtil {
}
}
private static File createDirectory(Context context, String name) {
File dir = new File(getSubsonicDirectory(context), name);
if (!dir.exists() && !dir.mkdirs()) {
Log.e(TAG, "Failed to create " + name);
}
return dir;
}
public static File getSubsonicDirectory(Context context) {
return context.getExternalFilesDir(null);
}
public static File getDefaultMusicDirectory(Context context) {
if(DEFAULT_MUSIC_DIR == null) {
if (DEFAULT_MUSIC_DIR == null) {
File[] dirs;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
dirs = context.getExternalMediaDirs();
} else {
dirs = ContextCompat.getExternalFilesDirs(context, null);
@ -380,7 +338,7 @@ public class FileUtil {
Log.e(TAG, "Failed to create default dir " + DEFAULT_MUSIC_DIR);
// Some devices seem to have screwed up the new media directory API. Go figure. Try again with standard locations
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
dirs = ContextCompat.getExternalFilesDirs(context, null);
DEFAULT_MUSIC_DIR = new File(getBestDir(dirs), "music");
@ -395,13 +353,14 @@ public class FileUtil {
return DEFAULT_MUSIC_DIR;
}
private static File getBestDir(File[] dirs) {
// Past 5.0 we can query directly for SD Card
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
for(int i = 0; i < dirs.length; i++) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
for (File dir : dirs) {
try {
if (dirs[i] != null && Environment.isExternalStorageRemovable(dirs[i])) {
return dirs[i];
if (dir != null && Environment.isExternalStorageRemovable(dir)) {
return dir;
}
} catch (Exception e) {
Log.e(TAG, "Failed to check if is external", e);
@ -410,8 +369,8 @@ public class FileUtil {
}
// Before 5.0, we have to guess. Most of the time the SD card is last
for(int i = dirs.length - 1; i >= 0; i--) {
if(dirs[i] != null) {
for (int i = dirs.length - 1; i >= 0; i--) {
if (dirs[i] != null) {
return dirs[i];
}
}
@ -425,38 +384,43 @@ public class FileUtil {
File dir = new File(path);
return ensureDirectoryExistsAndIsReadWritable(dir) ? dir : getDefaultMusicDirectory(context);
}
public static boolean deleteMusicDirectory(Context context) {
public static void deleteMusicDirectory(Context context) {
File musicDirectory = FileUtil.getMusicDirectory(context);
MediaStoreService mediaStore = new MediaStoreService(context);
return recursiveDelete(musicDirectory, mediaStore);
recursiveDelete(musicDirectory, mediaStore);
}
public static void deleteSerializedCache(Context context) {
for(File file: context.getCacheDir().listFiles()) {
if(file.getName().indexOf(".ser") != -1) {
for (File file : context.getCacheDir().listFiles()) {
if (file.getName().contains(".ser")) {
file.delete();
}
}
}
public static boolean deleteArtworkCache(Context context) {
public static void deleteArtworkCache(Context context) {
File artDirectory = FileUtil.getAlbumArtDirectory(context);
return recursiveDelete(artDirectory);
recursiveDelete(artDirectory);
}
public static boolean recursiveDelete(File dir) {
return recursiveDelete(dir, null);
private static void recursiveDelete(File dir) {
recursiveDelete(dir, null);
}
public static boolean recursiveDelete(File dir, MediaStoreService mediaStore) {
if (dir != null && dir.exists()) {
File[] list = dir.listFiles();
if(list != null) {
for(File file: list) {
if(file.isDirectory()) {
if(!recursiveDelete(file, mediaStore)) {
if (list != null) {
for (File file : list) {
if (file.isDirectory()) {
if (!recursiveDelete(file, mediaStore)) {
return false;
}
} else if(file.exists()) {
if(!file.delete()) {
} else if (file.exists()) {
if (!file.delete()) {
return false;
} else if(mediaStore != null) {
} else if (mediaStore != null) {
mediaStore.deleteFromMediaStore(file);
}
}
@ -470,7 +434,7 @@ public class FileUtil {
public static void deleteEmptyDir(File dir) {
try {
File[] children = dir.listFiles();
if(children == null) {
if (children == null) {
return;
}
@ -484,33 +448,12 @@ public class FileUtil {
if (children.length == 0) {
Util.delete(dir);
}
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Error while trying to delete empty dir", e);
}
}
public static void unpinSong(Context context, File saveFile) {
// Don't try to unpin a song which isn't actually pinned
if(saveFile.getName().contains(".complete")) {
return;
}
// Unpin file, rename to .complete
File completeFile = new File(saveFile.getParent(), FileUtil.getBaseName(saveFile.getName()) +
".complete." + FileUtil.getExtension(saveFile.getName()));
if(!saveFile.renameTo(completeFile)) {
Log.w(TAG, "Failed to upin " + saveFile + " to " + completeFile);
} else {
try {
new MediaStoreService(context).renameInMediaStore(completeFile, saveFile);
} catch(Exception e) {
Log.w(TAG, "Failed to write to media store");
}
}
}
public static boolean ensureDirectoryExistsAndIsReadWritable(File dir) {
private static boolean ensureDirectoryExistsAndIsReadWritable(File dir) {
if (dir == null) {
return false;
}
@ -540,13 +483,14 @@ public class FileUtil {
}
return true;
}
public static boolean verifyCanWrite(File dir) {
if(ensureDirectoryExistsAndIsReadWritable(dir)) {
if (ensureDirectoryExistsAndIsReadWritable(dir)) {
try {
File tmp = new File(dir, "checkWrite");
tmp.createNewFile();
if(tmp.exists()) {
if(tmp.delete()) {
if (tmp.exists()) {
if (tmp.delete()) {
return true;
} else {
Log.w(TAG, "Failed to delete temp file, retrying");
@ -554,7 +498,7 @@ public class FileUtil {
// This should never be reached since this is a file Audinaut created!
Thread.sleep(100L);
tmp = new File(dir, "checkWrite");
if(tmp.delete()) {
if (tmp.delete()) {
return true;
} else {
Log.w(TAG, "Failed retry to delete temp file");
@ -565,7 +509,7 @@ public class FileUtil {
Log.w(TAG, "Temp file does not actually exist");
return false;
}
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Failed to create tmp file", e);
return false;
}
@ -575,12 +519,12 @@ public class FileUtil {
}
/**
* Makes a given filename safe by replacing special characters like slashes ("/" and "\")
* with dashes ("-").
*
* @param filename The filename in question.
* @return The filename with special characters replaced by hyphens.
*/
* Makes a given filename safe by replacing special characters like slashes ("/" and "\")
* with dashes ("-").
*
* @param filename The filename in question.
* @return The filename with special characters replaced by hyphens.
*/
private static String fileSystemSafe(String filename) {
if (filename == null || filename.trim().length() == 0) {
return "unnamed";
@ -618,10 +562,10 @@ public class FileUtil {
File[] files = dir.listFiles();
if (files == null) {
Log.w(TAG, "Failed to list children for " + dir.getPath());
return new TreeSet<File>();
return new TreeSet<>();
}
return new TreeSet<File>(Arrays.asList(files));
return new TreeSet<>(Arrays.asList(files));
}
public static SortedSet<File> listMediaFiles(File dir) {
@ -641,11 +585,6 @@ public class FileUtil {
return MUSIC_FILE_EXTENSIONS.contains(extension);
}
public static boolean isMusicFile(File file) {
String extension = getExtension(file.getName());
return MUSIC_FILE_EXTENSIONS.contains(extension);
}
public static boolean isPlaylistFile(File file) {
String extension = getExtension(file.getName());
return PLAYLIST_FILE_EXTENSIONS.contains(extension);
@ -675,32 +614,7 @@ public class FileUtil {
return index == -1 ? name : name.substring(0, index);
}
public static Long[] getUsedSize(Context context, File file) {
long number = 0L;
long permanent = 0L;
long size = 0L;
if(file.isFile()) {
if(isMediaFile(file)) {
if(file.getAbsolutePath().indexOf(".complete") == -1) {
permanent++;
}
return new Long[] {1L, permanent, file.length()};
} else {
return new Long[] {0L, 0L, 0L};
}
} else {
for (File child : FileUtil.listFiles(file)) {
Long[] pair = getUsedSize(context, child);
number += pair[0];
permanent += pair[1];
size += pair[2];
}
return new Long[] {number, permanent, size};
}
}
public static <T extends Serializable> boolean serialize(Context context, T obj, String fileName) {
public static <T extends Serializable> void serialize(Context context, T obj, String fileName) {
Output out = null;
try {
RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "rw");
@ -708,32 +622,26 @@ public class FileUtil {
synchronized (kryo) {
kryo.writeObject(out, obj);
}
return true;
} catch (Throwable x) {
Log.w(TAG, "Failed to serialize object to " + fileName);
return false;
} finally {
Util.close(out);
}
}
public static <T extends Serializable> T deserialize(Context context, String fileName, Class<T> tClass) {
return deserialize(context, fileName, tClass, 0);
}
public static <T extends Serializable> T deserialize(Context context, String fileName, Class<T> tClass, int hoursOld) {
Input in = null;
try {
File file = new File(context.getCacheDir(), fileName);
if(!file.exists()) {
if (!file.exists()) {
return null;
}
if(hoursOld != 0) {
if (0 != 0) {
Date fileDate = new Date(file.lastModified());
// Convert into hours
long age = (new Date().getTime() - fileDate.getTime()) / 1000 / 3600;
if(age > hoursOld) {
if (age > 0) {
return null;
}
}
@ -742,10 +650,9 @@ public class FileUtil {
in = new Input(new FileInputStream(randomFile.getFD()));
synchronized (kryo) {
T result = kryo.readObject(in, tClass);
return result;
return kryo.readObject(in, tClass);
}
} catch(FileNotFoundException e) {
} catch (FileNotFoundException e) {
// Different error message
Log.w(TAG, "No serialization for object from " + fileName);
return null;
@ -757,7 +664,7 @@ public class FileUtil {
}
}
public static <T extends Serializable> boolean serializeCompressed(Context context, T obj, String fileName) {
public static <T extends Serializable> void serializeCompressed(Context context, T obj, String fileName) {
Output out = null;
try {
RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "rw");
@ -765,10 +672,8 @@ public class FileUtil {
synchronized (kryo) {
kryo.writeObject(out, obj);
}
return true;
} catch (Throwable x) {
Log.w(TAG, "Failed to serialize compressed object to " + fileName);
return false;
} finally {
Util.close(out);
}
@ -781,10 +686,9 @@ public class FileUtil {
in = new Input(new InflaterInputStream(new FileInputStream(file.getFD())));
synchronized (kryo) {
T result = kryo.readObject(in, tClass);
return result;
return kryo.readObject(in, tClass);
}
} catch(FileNotFoundException e) {
} catch (FileNotFoundException e) {
// Different error message
Log.w(TAG, "No serialization compressed for object from " + fileName);
return null;

View File

@ -28,18 +28,15 @@ import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.util.LruCache;
import android.util.DisplayMetrics;
import android.util.Log;
import android.support.v4.util.LruCache;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.lang.ref.WeakReference;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.domain.MusicDirectory;
import net.nullsum.audinaut.domain.Playlist;
@ -54,20 +51,18 @@ 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 Context context;
private LruCache<String, Bitmap> cache;
private Handler handler;
private Bitmap nowPlaying;
private Bitmap nowPlayingSmall;
private static final String TAG = ImageLoader.class.getSimpleName();
private final static int[] COLORS = {0xFF33B5E5, 0xFFAA66CC, 0xFF99CC00, 0xFFFFBB33, 0xFFFF4444};
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 Context context;
private final Handler handler;
private LruCache<String, Bitmap> cache;
private Bitmap nowPlaying;
private Bitmap nowPlayingSmall;
private boolean clearingCache = false;
public ImageLoader(Context context) {
this.context = context;
@ -88,10 +83,10 @@ public class ImageLoader {
@Override
protected void entryRemoved(boolean evicted, String key, Bitmap oldBitmap, Bitmap newBitmap) {
if(evicted) {
if((oldBitmap != nowPlaying && oldBitmap != nowPlayingSmall) || clearingCache) {
if (evicted) {
if ((oldBitmap != nowPlaying && oldBitmap != nowPlayingSmall) || clearingCache) {
oldBitmap.recycle();
} else if(oldBitmap != newBitmap) {
} else if (oldBitmap != newBitmap) {
cache.put(key, oldBitmap);
}
}
@ -112,12 +107,14 @@ public class ImageLoader {
}
}.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) {
if (cache.maxSize() != cacheSize) {
Log.i(TAG, "Returned to full cache size");
cache.resize(cacheSize);
}
@ -130,7 +127,7 @@ public class ImageLoader {
private Bitmap getUnknownImage(MusicDirectory.Entry entry, int size) {
String key;
int color;
if(entry == null) {
if (entry == null) {
key = getKey("unknown", size);
color = COLORS[0];
@ -138,9 +135,9 @@ public class ImageLoader {
} else {
key = getKey(entry.getId() + "unknown", size);
String hash;
if(entry.getAlbum() != null) {
if (entry.getAlbum() != null) {
hash = entry.getAlbum();
} else if(entry.getArtist() != null) {
} else if (entry.getArtist() != null) {
hash = entry.getArtist();
} else {
hash = entry.getId();
@ -150,15 +147,17 @@ public class ImageLoader {
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) {
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);
@ -170,17 +169,17 @@ public class ImageLoader {
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) {
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) {
if (topText != null) {
canvas.drawText(topText, size * 0.05f, size * 0.6f, font);
}
if(bottomText != null) {
if (bottomText != null) {
canvas.drawText(bottomText, size * 0.05f, size * 0.8f, font);
}
}
@ -190,19 +189,19 @@ public class ImageLoader {
public Bitmap getCachedImage(Context context, MusicDirectory.Entry entry, boolean large) {
int size = large ? imageSizeLarge : imageSizeDefault;
if(entry == null || entry.getCoverArt() == null) {
if (entry == null || entry.getCoverArt() == null) {
return getUnknownImage(entry, size);
}
Bitmap bitmap = cache.get(getKey(entry.getCoverArt(), size));
if(bitmap == null || bitmap.isRecycled()) {
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()) {
if (bitmap != null && bitmap.isRecycled()) {
bitmap = null;
}
return bitmap;
@ -212,16 +211,17 @@ public class ImageLoader {
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);
if (entry != null && !entry.isAlbum() && !Util.isOffline(context)) {
SilentBackgroundTask task = new ArtistImageTask(view.getContext(), entry, size, large, view, crossfade);
task.execute();
return task;
} else if(entry != null && entry.getCoverArt() == null && entry.isDirectory() && !Util.isOffline(context)) {
} 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) {
if (firstChild != null) {
entry.setCoverArt(firstChild.getCoverArt());
}
}
@ -237,7 +237,7 @@ public class ImageLoader {
if (bitmap != null && !bitmap.isRecycled()) {
final Drawable drawable = Util.createDrawableFromBitmap(this.context, bitmap);
setImage(view, drawable, crossfade);
if(large) {
if (large) {
nowPlaying = bitmap;
}
return null;
@ -246,39 +246,15 @@ public class ImageLoader {
if (!large) {
setImage(view, null, false);
}
ImageTask task = new ViewImageTask(view.getContext(), entry, size, imageSizeLarge, large, view, crossfade);
ImageTask task = new ViewImageTask(view.getContext(), entry, size, 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;
}
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;
}
public SilentBackgroundTask loadImage(View view, Playlist playlist, boolean large, boolean crossfade) {
public SilentBackgroundTask loadImage(View view, Playlist playlist) {
MusicDirectory.Entry entry = new MusicDirectory.Entry();
String id;
if(Util.isOffline(context)) {
if (Util.isOffline(context)) {
id = PLAYLIST_PREFIX + playlist.getName();
entry.setTitle(playlist.getComment());
} else {
@ -290,7 +266,7 @@ public class ImageLoader {
// So this isn't treated as a artist
entry.setParent("");
return loadImage(view, entry, large, crossfade);
return loadImage(view, entry, false, true);
}
private String getKey(String coverArtId, int size) {
@ -308,31 +284,28 @@ public class ImageLoader {
Drawable existingDrawable = imageView.getDrawable();
if (existingDrawable == null) {
Bitmap emptyImage;
if(drawable.getIntrinsicWidth() > 0 && drawable.getIntrinsicHeight() > 0) {
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) {
} 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) {
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);
}
handler.postDelayed(() -> {
// Only execute if still on same transition drawable
if (imageView.getDrawable() == transitionDrawable) {
imageView.setImageDrawable(drawable);
}
}, 500L);
} else {
@ -345,19 +318,17 @@ public class ImageLoader {
}
public abstract class ImageTask extends SilentBackgroundTask<Void> {
final MusicDirectory.Entry mEntry;
private final Context mContext;
protected final MusicDirectory.Entry mEntry;
private final int mSize;
private final int mSaveSize;
private final boolean mIsNowPlaying;
protected Drawable mDrawable;
Drawable mDrawable;
public ImageTask(Context context, MusicDirectory.Entry entry, int size, int saveSize, boolean isNowPlaying) {
public ImageTask(Context context, MusicDirectory.Entry entry, int size, boolean isNowPlaying) {
super(context);
mContext = context;
mEntry = entry;
mSize = size;
mSaveSize = saveSize;
mIsNowPlaying = isNowPlaying;
}
@ -366,7 +337,7 @@ public class ImageLoader {
try {
MusicService musicService = MusicServiceFactory.getMusicService(mContext);
Bitmap bitmap = musicService.getCoverArt(mContext, mEntry, mSize, null, this);
if(bitmap != null) {
if (bitmap != null) {
String key = getKey(mEntry.getCoverArt(), mSize);
cache.put(key, bitmap);
// Make sure key is the most recently "used"
@ -389,11 +360,11 @@ public class ImageLoader {
}
private class ViewImageTask extends ImageTask {
protected boolean mCrossfade;
private View mView;
final boolean mCrossfade;
private final 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, boolean isNowPlaying, View view, boolean crossfade) {
super(context, entry, size, isNowPlaying);
mView = view;
mCrossfade = crossfade;
@ -409,20 +380,17 @@ public class ImageLoader {
private final Context mContext;
private final MusicDirectory.Entry mEntry;
private final int mSize;
private final int mSaveSize;
private final boolean mIsNowPlaying;
private final boolean mCrossfade;
private final View mView;
private Drawable mDrawable;
private boolean mCrossfade;
private View mView;
private SilentBackgroundTask subTask;
public ArtistImageTask(Context context, MusicDirectory.Entry entry, int size, int saveSize, boolean isNowPlaying, View view, boolean crossfade) {
public ArtistImageTask(Context context, MusicDirectory.Entry entry, int size, boolean isNowPlaying, View view, boolean crossfade) {
super(context);
mContext = context;
mEntry = entry;
mSize = size;
mSaveSize = saveSize;
mIsNowPlaying = isNowPlaying;
mView = view;
mCrossfade = crossfade;
@ -431,8 +399,6 @@ public class ImageLoader {
@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
if (mEntry != null && mEntry.getCoverArt() == null && mEntry.isDirectory() && !Util.isOffline(context)) {
// Try to lookup child cover art
@ -443,7 +409,7 @@ public class ImageLoader {
}
if (mEntry != null && mEntry.getCoverArt() != null) {
subTask = new ViewImageTask(mContext, mEntry, mSize, mSaveSize, mIsNowPlaying, mView, mCrossfade);
subTask = new ViewImageTask(mContext, mEntry, mSize, mIsNowPlaying, mView, mCrossfade);
} else {
// If entry is null as well, we need to just set as a blank image
Bitmap bitmap = getUnknownImage(mEntry, mSize);
@ -462,61 +428,11 @@ public class ImageLoader {
@Override
public void done(Void result) {
if(subTask != null) {
if (subTask != null) {
subTask.done(result);
} else if(mDrawable != null) {
} 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;
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);
mDrawable = Util.createDrawableFromBitmap(mContext, bitmap);
}
} catch (Throwable x) {
Log.e(TAG, "Failed to download from url " + mUrl, x);
cancelled.set(true);
}
return null;
}
@Override
protected void done(Void result) {
if(mDrawable != null) {
mView.setImageDrawable(mDrawable);
} else {
failedToDownload();
}
}
protected void failedToDownload() {
}
}
}

View File

@ -2,7 +2,6 @@ package net.nullsum.audinaut.util;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import net.nullsum.audinaut.activity.SubsonicActivity;
@ -13,14 +12,15 @@ 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;
public LoadingTask(Activity activity) {
super(activity);
tabActivity = activity;
this.cancellable = true;
}
public LoadingTask(Activity activity, final boolean cancellable) {
super(activity);
tabActivity = activity;
@ -29,16 +29,12 @@ public abstract class LoadingTask<T> extends BackgroundTask<T> {
@Override
public void execute() {
loading = ProgressDialog.show(tabActivity, "", "Loading. Please Wait...", true, cancellable, new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
cancel();
}
});
loading = ProgressDialog.show(tabActivity, "", "Loading. Please Wait...", true, cancellable, dialog -> cancel());
queue.offer(task = new Task() {
@Override
public void onDone(T result) {
if(loading.isShowing()) {
if (loading.isShowing()) {
loading.dismiss();
}
done(result);
@ -46,7 +42,7 @@ public abstract class LoadingTask<T> extends BackgroundTask<T> {
@Override
public void onError(Throwable t) {
if(loading.isShowing()) {
if (loading.isShowing()) {
loading.dismiss();
}
error(t);
@ -61,13 +57,8 @@ public abstract class LoadingTask<T> extends BackgroundTask<T> {
@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(() -> loading.setMessage(message));
}
}
}

View File

@ -16,12 +16,9 @@
package net.nullsum.audinaut.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import java.io.File;
import net.nullsum.audinaut.R;
import net.nullsum.audinaut.service.DownloadFile;
import net.nullsum.audinaut.view.AlbumView;
@ -30,21 +27,23 @@ import net.nullsum.audinaut.view.ArtistView;
import net.nullsum.audinaut.view.SongView;
import net.nullsum.audinaut.view.UpdateView;
import java.io.File;
public final class MenuUtil {
private final static String TAG = MenuUtil.class.getSimpleName();
public static void hideMenuItems(Context context, Menu menu, UpdateView updateView) {
if(!Util.isOffline(context)) {
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) {
if (updateView instanceof SongView) {
SongView songView = (SongView) updateView;
DownloadFile downloadFile = songView.getDownloadFile();
try {
if(downloadFile != null) {
if(downloadFile.isWorkDone()) {
if (downloadFile != null) {
if (downloadFile.isWorkDone()) {
// Remove permanent cache menu if already perma cached
if(downloadFile.isSaved()) {
if (downloadFile.isSaved()) {
menu.setGroupVisible(R.id.hide_pin, false);
}
@ -55,26 +54,26 @@ public final class MenuUtil {
menu.setGroupVisible(R.id.hide_delete, false);
}
}
} catch(Exception e) {
} 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) {
else if (updateView instanceof AlbumView || updateView instanceof ArtistView || updateView instanceof ArtistEntryView) {
File folder = null;
if(updateView instanceof AlbumView) {
if (updateView instanceof AlbumView) {
folder = ((AlbumView) updateView).getFile();
} else if(updateView instanceof ArtistView) {
} else if (updateView instanceof ArtistView) {
folder = ((ArtistView) updateView).getFile();
} else if(updateView instanceof ArtistEntryView) {
} else if (updateView instanceof ArtistEntryView) {
folder = ((ArtistEntryView) updateView).getFile();
}
try {
if(folder != null && !folder.exists()) {
if (folder != null && !folder.exists()) {
menu.setGroupVisible(R.id.hide_delete, false);
}
} catch(Exception e) {
} catch (Exception e) {
Log.w(TAG, "Failed to lookup album directory info", e);
}
}

View File

@ -43,14 +43,11 @@ import net.nullsum.audinaut.service.DownloadService;
import static android.content.Context.NOTIFICATION_SERVICE;
public final class Notifications {
private static final int NOTIFICATION_ID_PLAYING = 100;
private static final int NOTIFICATION_ID_DOWNLOADING = 102;
private static final String CHANNEL_PLAYING_ID = "playback_controls";
private static final String CHANNEL_DOWNLOADING_ID = "media_download";
private static final String TAG = Notifications.class.getSimpleName();
public static final int NOTIFICATION_ID_PLAYING = 100;
public static final int NOTIFICATION_ID_DOWNLOADING = 102;
public static final String CHANNEL_PLAYING_ID = "playback_controls";
public static final String CHANNEL_DOWNLOADING_ID = "media_download";
private static boolean playShowing = false;
private static boolean downloadShowing = false;
private static boolean downloadForeground = false;
@ -71,7 +68,7 @@ public final class Notifications {
final boolean playing = downloadService.getPlayerState() == PlayerState.STARTED;
RemoteViews expandedContentView = new RemoteViews(context.getPackageName(), R.layout.notification_expanded);
setupViews(expandedContentView ,context, song, true, playing);
setupViews(expandedContentView, context, song, true, playing);
RemoteViews smallContentView = new RemoteViews(context.getPackageName(), R.layout.notification);
setupViews(smallContentView, context, song, false, playing);
@ -93,29 +90,23 @@ public final class Notifications {
.setPriority(NotificationCompat.PRIORITY_LOW).build();
playShowing = true;
if(downloadForeground && downloadShowing) {
if (downloadForeground && downloadShowing) {
downloadForeground = false;
handler.post(new Runnable() {
@Override
public void run() {
downloadService.stopForeground(true);
showDownloadingNotification(context, downloadService, handler, downloadService.getCurrentDownloading(), downloadService.getBackgroundDownloads().size());
downloadService.startForeground(NOTIFICATION_ID_PLAYING, notification);
}
handler.post(() -> {
downloadService.stopForeground(true);
showDownloadingNotification(context, downloadService, handler, downloadService.getCurrentDownloading(), downloadService.getBackgroundDownloads().size());
downloadService.startForeground(NOTIFICATION_ID_PLAYING, notification);
});
} else {
handler.post(new Runnable() {
@Override
public void run() {
if (playing) {
downloadService.startForeground(NOTIFICATION_ID_PLAYING, notification);
} else {
playShowing = false;
persistentPlayingShowing = true;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
downloadService.stopForeground(false);
notificationManager.notify(NOTIFICATION_ID_PLAYING, notification);
}
handler.post(() -> {
if (playing) {
downloadService.startForeground(NOTIFICATION_ID_PLAYING, notification);
} else {
playShowing = false;
persistentPlayingShowing = true;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
downloadService.stopForeground(false);
notificationManager.notify(NOTIFICATION_ID_PLAYING, notification);
}
});
}
@ -134,7 +125,7 @@ public final class Notifications {
try {
ImageLoader imageLoader = SubsonicActivity.getStaticImageLoader(context);
Bitmap bitmap = null;
if(imageLoader != null) {
if (imageLoader != null) {
bitmap = imageLoader.getCachedImage(context, song, false);
}
if (bitmap == null) {
@ -155,8 +146,8 @@ public final class Notifications {
rv.setTextViewText(R.id.notification_album, album);
boolean persistent = Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_PERSISTENT_NOTIFICATION, false);
if(persistent) {
if(expanded) {
if (persistent) {
if (expanded) {
rv.setImageViewResource(R.id.control_pause, playing ? R.drawable.notification_pause : R.drawable.notification_start);
rv.setImageViewResource(R.id.control_previous, R.drawable.notification_backward);
@ -174,8 +165,8 @@ public final class Notifications {
// Create actions for media buttons
PendingIntent pendingIntent;
int previous = 0, pause = 0, next = 0, close = 0, rewind = 0, fastForward = 0;
if(persistent && !expanded) {
int previous = 0, pause, next, close = 0, rewind = 0, fastForward = 0;
if (persistent && !expanded) {
pause = R.id.control_previous;
next = R.id.control_pause;
close = R.id.control_next;
@ -185,27 +176,27 @@ public final class Notifications {
next = R.id.control_next;
}
if(persistent && close == 0 && expanded) {
if (persistent && close == 0 && expanded) {
close = R.id.notification_close;
rv.setViewVisibility(close, View.VISIBLE);
}
if(previous > 0) {
if (previous > 0) {
Intent prevIntent = new Intent("KEYCODE_MEDIA_PREVIOUS");
prevIntent.setComponent(new ComponentName(context, DownloadService.class));
prevIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS));
pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
rv.setOnClickPendingIntent(previous, pendingIntent);
}
if(rewind > 0) {
if (rewind > 0) {
Intent rewindIntent = new Intent("KEYCODE_MEDIA_REWIND");
rewindIntent.setComponent(new ComponentName(context, DownloadService.class));
rewindIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_REWIND));
pendingIntent = PendingIntent.getService(context, 0, rewindIntent, 0);
rv.setOnClickPendingIntent(rewind, pendingIntent);
}
if(pause > 0) {
if(playing) {
if (pause > 0) {
if (playing) {
Intent pauseIntent = new Intent("KEYCODE_MEDIA_PLAY_PAUSE");
pauseIntent.setComponent(new ComponentName(context, DownloadService.class));
pauseIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
@ -219,21 +210,21 @@ public final class Notifications {
rv.setOnClickPendingIntent(pause, pendingIntent);
}
}
if(next > 0) {
if (next > 0) {
Intent nextIntent = new Intent("KEYCODE_MEDIA_NEXT");
nextIntent.setComponent(new ComponentName(context, DownloadService.class));
nextIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
pendingIntent = PendingIntent.getService(context, 0, nextIntent, 0);
rv.setOnClickPendingIntent(next, pendingIntent);
}
if(fastForward > 0) {
if (fastForward > 0) {
Intent fastForwardIntent = new Intent("KEYCODE_MEDIA_FAST_FORWARD");
fastForwardIntent.setComponent(new ComponentName(context, DownloadService.class));
fastForwardIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_FAST_FORWARD));
pendingIntent = PendingIntent.getService(context, 0, fastForwardIntent, 0);
rv.setOnClickPendingIntent(fastForward, pendingIntent);
}
if(close > 0) {
if (close > 0) {
Intent prevIntent = new Intent("KEYCODE_MEDIA_STOP");
prevIntent.setComponent(new ComponentName(context, DownloadService.class));
prevIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_STOP));
@ -246,21 +237,18 @@ public final class Notifications {
playShowing = false;
// Remove notification and remove the service from the foreground
handler.post(new Runnable() {
@Override
public void run() {
downloadService.stopForeground(true);
handler.post(() -> {
downloadService.stopForeground(true);
if(persistentPlayingShowing) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID_PLAYING);
persistentPlayingShowing = false;
}
if (persistentPlayingShowing) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID_PLAYING);
persistentPlayingShowing = false;
}
});
// Get downloadNotification in foreground if playing
if(downloadShowing) {
if (downloadShowing) {
showDownloadingNotification(context, downloadService, handler, downloadService.getCurrentDownloading(), downloadService.getBackgroundDownloads().size());
}
@ -274,7 +262,7 @@ public final class Notifications {
PendingIntent cancelPI = PendingIntent.getService(context, 0, cancelIntent, 0);
String currentDownloading, currentSize;
if(file != null) {
if (file != null) {
currentDownloading = file.getSong().getTitle();
currentSize = Util.formatLocalizedBytes(file.getEstimatedSize(), context);
} else {
@ -307,37 +295,28 @@ public final class Notifications {
cancelPI)
.setContentIntent(PendingIntent.getActivity(context, 2, notificationIntent, 0))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(context.getResources().getString(R.string.download_downloading_summary_expanded, currentDownloading, currentSize)))
.bigText(context.getResources().getString(R.string.download_downloading_summary_expanded, currentDownloading, currentSize)))
.setProgress(10, 5, true).build();
downloadShowing = true;
if(playShowing) {
if (playShowing) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID_DOWNLOADING, notification);
} else {
downloadForeground = true;
handler.post(new Runnable() {
@Override
public void run() {
downloadService.startForeground(NOTIFICATION_ID_DOWNLOADING, notification);
}
});
handler.post(() -> downloadService.startForeground(NOTIFICATION_ID_DOWNLOADING, notification));
}
}
public static void hideDownloadingNotification(final Context context, final DownloadService downloadService, Handler handler) {
downloadShowing = false;
if(playShowing) {
if (playShowing) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID_DOWNLOADING);
} else {
downloadForeground = false;
handler.post(new Runnable() {
@Override
public void run() {
downloadService.stopForeground(true);
}
});
handler.post(() -> downloadService.stopForeground(true));
}
}
}

View File

@ -25,11 +25,8 @@ import java.io.Serializable;
*/
public class Pair<S, T> implements Serializable {
private S first;
private T second;
public Pair() {
}
private final S first;
private final T second;
public Pair(S first, T second) {
this.first = first;
@ -40,15 +37,8 @@ public class Pair<S, T> implements Serializable {
return first;
}
public void setFirst(S first) {
this.first = first;
}
public T getSecond() {
return second;
}
public void setSecond(T second) {
this.second = second;
}
}

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