mirror of
https://gitlab.com/ultrasonic/ultrasonic.git
synced 2025-06-13 22:12:10 +03:00
Fixed DownloadServiceImpl to start as background service
Removed notification related settings for API26
This commit is contained in:
parent
50fde988a8
commit
02da3e79ba
@ -206,6 +206,7 @@ public class BookmarkActivity extends SubsonicTabActivity
|
|||||||
if (!getSelectedSongs(albumListView).isEmpty())
|
if (!getSelectedSongs(albumListView).isEmpty())
|
||||||
{
|
{
|
||||||
int position = songs.get(0).getBookmarkPosition();
|
int position = songs.get(0).getBookmarkPosition();
|
||||||
|
if (getDownloadService() == null) return;
|
||||||
getDownloadService().restore(songs, 0, position, true, true);
|
getDownloadService().restore(songs, 0, position, true, true);
|
||||||
selectAll(false, false);
|
selectAll(false, false);
|
||||||
}
|
}
|
||||||
|
@ -97,11 +97,8 @@ public class SubsonicTabActivity extends ResultActivity implements OnClickListen
|
|||||||
applyTheme();
|
applyTheme();
|
||||||
super.onCreate(bundle);
|
super.onCreate(bundle);
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
// This should always succeed as it is called when Ultrasonic is in the foreground
|
||||||
startForegroundService(new Intent(this, DownloadServiceImpl.class));
|
startService(new Intent(this, DownloadServiceImpl.class));
|
||||||
} else {
|
|
||||||
startService(new Intent(this, DownloadServiceImpl.class));
|
|
||||||
}
|
|
||||||
setVolumeControlStream(AudioManager.STREAM_MUSIC);
|
setVolumeControlStream(AudioManager.STREAM_MUSIC);
|
||||||
|
|
||||||
if (bundle != null)
|
if (bundle != null)
|
||||||
@ -778,11 +775,16 @@ public class SubsonicTabActivity extends ResultActivity implements OnClickListen
|
|||||||
}
|
}
|
||||||
|
|
||||||
Log.w(TAG, "DownloadService not running. Attempting to start it.");
|
Log.w(TAG, "DownloadService not running. Attempting to start it.");
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
||||||
startForegroundService(new Intent(this, DownloadServiceImpl.class));
|
try
|
||||||
} else {
|
{
|
||||||
startService(new Intent(this, DownloadServiceImpl.class));
|
startService(new Intent(this, DownloadServiceImpl.class));
|
||||||
}
|
}
|
||||||
|
catch (IllegalStateException exception)
|
||||||
|
{
|
||||||
|
Log.w(TAG, "getDownloadService couldn't start DownloadServiceImpl because the application was in the background.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
Util.sleepQuietly(50L);
|
Util.sleepQuietly(50L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,6 +108,13 @@ public class SettingsFragment extends PreferenceFragment
|
|||||||
setupClearSearchPreference();
|
setupClearSearchPreference();
|
||||||
setupGaplessControlSettingsV14();
|
setupGaplessControlSettingsV14();
|
||||||
setupFeatureFlagsPreferences();
|
setupFeatureFlagsPreferences();
|
||||||
|
|
||||||
|
// After API26 foreground services must be used for music playback, and they must have a notification
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
PreferenceCategory notificationsCategory = (PreferenceCategory) findPreference(Constants.PREFERENCES_KEY_CATEGORY_NOTIFICATIONS);
|
||||||
|
notificationsCategory.removePreference(findPreference(Constants.PREFERENCES_KEY_SHOW_NOTIFICATION));
|
||||||
|
notificationsCategory.removePreference(findPreference(Constants.PREFERENCES_KEY_ALWAYS_SHOW_NOTIFICATION));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -58,19 +58,20 @@ public class MediaButtonIntentReceiver extends BroadcastReceiver
|
|||||||
|
|
||||||
Intent serviceIntent = new Intent(context, DownloadServiceImpl.class);
|
Intent serviceIntent = new Intent(context, DownloadServiceImpl.class);
|
||||||
serviceIntent.putExtra(Intent.EXTRA_KEY_EVENT, event);
|
serviceIntent.putExtra(Intent.EXTRA_KEY_EVENT, event);
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
||||||
context.startForegroundService(serviceIntent);
|
|
||||||
} else {
|
|
||||||
context.startService(serviceIntent);
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
context.startService(serviceIntent);
|
||||||
|
|
||||||
if (isOrderedBroadcast())
|
if (isOrderedBroadcast())
|
||||||
{
|
{
|
||||||
abortBroadcast();
|
abortBroadcast();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (IllegalStateException exception)
|
||||||
|
{
|
||||||
|
Log.w(TAG, "MediaButtonIntentReceiver couldn't start DownloadServiceImpl because the application was in the background.");
|
||||||
|
}
|
||||||
catch (Exception x)
|
catch (Exception x)
|
||||||
{
|
{
|
||||||
// Ignored.
|
// Ignored.
|
||||||
|
@ -276,13 +276,17 @@ public class DownloadServiceImpl extends Service implements DownloadService
|
|||||||
|
|
||||||
// We should use a single notification builder, otherwise the notification may not be updated
|
// We should use a single notification builder, otherwise the notification may not be updated
|
||||||
notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
|
notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
|
||||||
|
|
||||||
|
Log.i(TAG, "DownloadServiceImpl created");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId)
|
public int onStartCommand(Intent intent, int flags, int startId)
|
||||||
{
|
{
|
||||||
super.onStartCommand(intent, flags, startId);
|
super.onStartCommand(intent, flags, startId);
|
||||||
|
|
||||||
lifecycleSupport.onStart(intent);
|
lifecycleSupport.onStart(intent);
|
||||||
|
Log.i(TAG, "DownloadServiceImpl started with intent");
|
||||||
return START_NOT_STICKY;
|
return START_NOT_STICKY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -338,6 +342,8 @@ public class DownloadServiceImpl extends Service implements DownloadService
|
|||||||
catch (Throwable ignored)
|
catch (Throwable ignored)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Log.i(TAG, "DownloadServiceImpl stopped");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DownloadService getInstance()
|
public static DownloadService getInstance()
|
||||||
@ -2144,44 +2150,47 @@ public class DownloadServiceImpl extends Service implements DownloadService
|
|||||||
bigView.setImageViewResource(R.id.control_play, R.drawable.media_pause_normal_dark);
|
bigView.setImageViewResource(R.id.control_play, R.drawable.media_pause_normal_dark);
|
||||||
}
|
}
|
||||||
|
|
||||||
final Entry song = currentPlaying.getSong();
|
if (currentPlaying != null) {
|
||||||
final String title = song.getTitle();
|
final Entry song = currentPlaying.getSong();
|
||||||
final String text = song.getArtist();
|
final String title = song.getTitle();
|
||||||
final String album = song.getAlbum();
|
final String text = song.getArtist();
|
||||||
final int rating = song.getUserRating() == null ? 0 : song.getUserRating();
|
final String album = song.getAlbum();
|
||||||
final int imageSize = Util.getNotificationImageSize(this);
|
final int rating = song.getUserRating() == null ? 0 : song.getUserRating();
|
||||||
|
final int imageSize = Util.getNotificationImageSize(this);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final Bitmap nowPlayingImage = FileUtil.getAlbumArtBitmap(this, currentPlaying.getSong(), imageSize, true);
|
final Bitmap nowPlayingImage = FileUtil.getAlbumArtBitmap(this, currentPlaying.getSong(), imageSize, true);
|
||||||
if (nowPlayingImage == null) {
|
if (nowPlayingImage == null) {
|
||||||
contentView.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
|
contentView.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
|
||||||
bigView.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
|
bigView.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
|
||||||
} else {
|
} else {
|
||||||
contentView.setImageViewBitmap(R.id.notification_image, nowPlayingImage);
|
contentView.setImageViewBitmap(R.id.notification_image, nowPlayingImage);
|
||||||
bigView.setImageViewBitmap(R.id.notification_image, nowPlayingImage);
|
bigView.setImageViewBitmap(R.id.notification_image, nowPlayingImage);
|
||||||
}
|
}
|
||||||
} catch (Exception x) {
|
} catch (Exception x) {
|
||||||
Log.w(TAG, "Failed to get notification cover art", x);
|
Log.w(TAG, "Failed to get notification cover art", x);
|
||||||
contentView.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
|
contentView.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
|
||||||
bigView.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
|
bigView.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
|
||||||
}
|
}
|
||||||
|
|
||||||
contentView.setTextViewText(R.id.trackname, title);
|
|
||||||
bigView.setTextViewText(R.id.trackname, title);
|
|
||||||
contentView.setTextViewText(R.id.artist, text);
|
|
||||||
bigView.setTextViewText(R.id.artist, text);
|
|
||||||
contentView.setTextViewText(R.id.album, album);
|
|
||||||
bigView.setTextViewText(R.id.album, album);
|
|
||||||
|
|
||||||
boolean useFiveStarRating = KoinJavaComponent.get(FeatureStorage.class).isFeatureEnabled(Feature.FIVE_STAR_RATING);
|
contentView.setTextViewText(R.id.trackname, title);
|
||||||
if (!useFiveStarRating) bigView.setViewVisibility(R.id.notification_rating, View.INVISIBLE);
|
bigView.setTextViewText(R.id.trackname, title);
|
||||||
else
|
contentView.setTextViewText(R.id.artist, text);
|
||||||
{
|
bigView.setTextViewText(R.id.artist, text);
|
||||||
bigView.setImageViewResource(R.id.notification_five_star_1, rating > 0 ? R.drawable.ic_star_full_dark : R.drawable.ic_star_hollow_dark);
|
contentView.setTextViewText(R.id.album, album);
|
||||||
bigView.setImageViewResource(R.id.notification_five_star_2, rating > 1 ? R.drawable.ic_star_full_dark : R.drawable.ic_star_hollow_dark);
|
bigView.setTextViewText(R.id.album, album);
|
||||||
bigView.setImageViewResource(R.id.notification_five_star_3, rating > 2 ? R.drawable.ic_star_full_dark : R.drawable.ic_star_hollow_dark);
|
|
||||||
bigView.setImageViewResource(R.id.notification_five_star_4, rating > 3 ? R.drawable.ic_star_full_dark : R.drawable.ic_star_hollow_dark);
|
boolean useFiveStarRating = KoinJavaComponent.get(FeatureStorage.class).isFeatureEnabled(Feature.FIVE_STAR_RATING);
|
||||||
bigView.setImageViewResource(R.id.notification_five_star_5, rating > 4 ? R.drawable.ic_star_full_dark : R.drawable.ic_star_hollow_dark);
|
if (!useFiveStarRating)
|
||||||
|
bigView.setViewVisibility(R.id.notification_rating, View.INVISIBLE);
|
||||||
|
else {
|
||||||
|
bigView.setImageViewResource(R.id.notification_five_star_1, rating > 0 ? R.drawable.ic_star_full_dark : R.drawable.ic_star_hollow_dark);
|
||||||
|
bigView.setImageViewResource(R.id.notification_five_star_2, rating > 1 ? R.drawable.ic_star_full_dark : R.drawable.ic_star_hollow_dark);
|
||||||
|
bigView.setImageViewResource(R.id.notification_five_star_3, rating > 2 ? R.drawable.ic_star_full_dark : R.drawable.ic_star_hollow_dark);
|
||||||
|
bigView.setImageViewResource(R.id.notification_five_star_4, rating > 3 ? R.drawable.ic_star_full_dark : R.drawable.ic_star_hollow_dark);
|
||||||
|
bigView.setImageViewResource(R.id.notification_five_star_5, rating > 4 ? R.drawable.ic_star_full_dark : R.drawable.ic_star_hollow_dark);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Notification notification = notificationBuilder.build();
|
Notification notification = notificationBuilder.build();
|
||||||
|
@ -132,6 +132,7 @@ public final class Constants
|
|||||||
public static final String PREFERENCES_KEY_IMAGE_LOADER_CONCURRENCY = "imageLoaderConcurrency";
|
public static final String PREFERENCES_KEY_IMAGE_LOADER_CONCURRENCY = "imageLoaderConcurrency";
|
||||||
public static final String PREFERENCES_KEY_FF_IMAGE_LOADER = "ff_new_image_loader";
|
public static final String PREFERENCES_KEY_FF_IMAGE_LOADER = "ff_new_image_loader";
|
||||||
public static final String PREFERENCES_KEY_USE_FIVE_STAR_RATING = "use_five_star_rating";
|
public static final String PREFERENCES_KEY_USE_FIVE_STAR_RATING = "use_five_star_rating";
|
||||||
|
public static final String PREFERENCES_KEY_CATEGORY_NOTIFICATIONS = "notificationsCategory";
|
||||||
|
|
||||||
// Number of free trial days for non-licensed servers.
|
// Number of free trial days for non-licensed servers.
|
||||||
public static final int FREE_TRIAL_DAYS = 30;
|
public static final int FREE_TRIAL_DAYS = 30;
|
||||||
|
@ -36,6 +36,7 @@ import android.net.ConnectivityManager;
|
|||||||
import android.net.NetworkInfo;
|
import android.net.NetworkInfo;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
@ -155,12 +156,16 @@ public class Util extends DownloadActivity
|
|||||||
|
|
||||||
public static boolean isNotificationEnabled(Context context)
|
public static boolean isNotificationEnabled(Context context)
|
||||||
{
|
{
|
||||||
|
// After API26 foreground services must be used for music playback, and they must have a notification
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) return true;
|
||||||
SharedPreferences preferences = getPreferences(context);
|
SharedPreferences preferences = getPreferences(context);
|
||||||
return preferences.getBoolean(Constants.PREFERENCES_KEY_SHOW_NOTIFICATION, false);
|
return preferences.getBoolean(Constants.PREFERENCES_KEY_SHOW_NOTIFICATION, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isNotificationAlwaysEnabled(Context context)
|
public static boolean isNotificationAlwaysEnabled(Context context)
|
||||||
{
|
{
|
||||||
|
// After API26 foreground services must be used for music playback, and they must have a notification
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) return true;
|
||||||
SharedPreferences preferences = getPreferences(context);
|
SharedPreferences preferences = getPreferences(context);
|
||||||
return preferences.getBoolean(Constants.PREFERENCES_KEY_ALWAYS_SHOW_NOTIFICATION, false);
|
return preferences.getBoolean(Constants.PREFERENCES_KEY_ALWAYS_SHOW_NOTIFICATION, false);
|
||||||
}
|
}
|
||||||
|
@ -107,7 +107,9 @@
|
|||||||
a:summary="@string/settings.playback.resume_play_on_headphones_plug.summary"
|
a:summary="@string/settings.playback.resume_play_on_headphones_plug.summary"
|
||||||
/>
|
/>
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
<PreferenceCategory a:title="@string/settings.notifications_title">
|
<PreferenceCategory
|
||||||
|
a:title="@string/settings.notifications_title"
|
||||||
|
a:key="notificationsCategory">
|
||||||
<CheckBoxPreference
|
<CheckBoxPreference
|
||||||
a:defaultValue="true"
|
a:defaultValue="true"
|
||||||
a:key="showNowPlaying"
|
a:key="showNowPlaying"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user