aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2014-06-22 11:51:47 -0700
committerScott Jackson <daneren2005@gmail.com>2014-06-22 11:51:47 -0700
commit109132de514923ae57574ba40642133d425b0344 (patch)
tree19de4b6366f75be3940a90f2ec7d4d28dc8eaf9d
parent5b7a4c3e458f78dfee22b930c18779e61f16b57e (diff)
downloaddsub-109132de514923ae57574ba40642133d425b0344.tar.gz
dsub-109132de514923ae57574ba40642133d425b0344.tar.bz2
dsub-109132de514923ae57574ba40642133d425b0344.zip
Moved notifications stuff to it's own class
-rw-r--r--src/github/daneren2005/dsub/service/DownloadService.java23
-rw-r--r--src/github/daneren2005/dsub/util/Constants.java4
-rw-r--r--src/github/daneren2005/dsub/util/Notifications.java281
-rw-r--r--src/github/daneren2005/dsub/util/Util.java229
4 files changed, 293 insertions, 244 deletions
diff --git a/src/github/daneren2005/dsub/service/DownloadService.java b/src/github/daneren2005/dsub/service/DownloadService.java
index aa7e8bbf..99056ea2 100644
--- a/src/github/daneren2005/dsub/service/DownloadService.java
+++ b/src/github/daneren2005/dsub/service/DownloadService.java
@@ -39,6 +39,7 @@ import github.daneren2005.dsub.domain.PodcastEpisode;
import github.daneren2005.dsub.domain.RemoteControlState;
import github.daneren2005.dsub.domain.RepeatMode;
import github.daneren2005.dsub.receiver.MediaButtonIntentReceiver;
+import github.daneren2005.dsub.util.Notifications;
import github.daneren2005.dsub.util.SilentBackgroundTask;
import github.daneren2005.dsub.util.Constants;
import github.daneren2005.dsub.util.MediaRouteManager;
@@ -275,8 +276,8 @@ public class DownloadService extends Service {
proxy = null;
}
mediaRouter.destroy();
- Util.hidePlayingNotification(this, this, handler);
- Util.hideDownloadingNotification(this);
+ Notifications.hidePlayingNotification(this, this, handler);
+ Notifications.hideDownloadingNotification(this);
}
public static DownloadService getInstance() {
@@ -522,7 +523,7 @@ public class DownloadService extends Service {
}
backgroundDownloadList.clear();
revision++;
- Util.hideDownloadingNotification(this);
+ Notifications.hideDownloadingNotification(this);
}
public synchronized void clearIncomplete() {
@@ -665,7 +666,7 @@ public class DownloadService extends Service {
mRemoteControl.updateMetadata(this, currentPlaying.getSong());
} else {
Util.broadcastNewTrackInfo(this, null);
- Util.hidePlayingNotification(this, this, handler);
+ Notifications.hidePlayingNotification(this, this, handler);
}
}
@@ -765,7 +766,7 @@ public class DownloadService extends Service {
reset();
if(index >= size && size != 0) {
setCurrentPlaying(0, false);
- Util.hidePlayingNotification(this, this, handler);
+ Notifications.hidePlayingNotification(this, this, handler);
} else {
setCurrentPlaying(null, false);
}
@@ -1044,16 +1045,16 @@ public class DownloadService extends Service {
}
if (show) {
- Util.showPlayingNotification(this, this, handler, currentPlaying.getSong());
+ Notifications.showPlayingNotification(this, this, handler, currentPlaying.getSong());
} else if (pause) {
SharedPreferences prefs = Util.getPreferences(this);
if(prefs.getBoolean(Constants.PREFERENCES_KEY_PERSISTENT_NOTIFICATION, false)) {
- Util.showPlayingNotification(this, this, handler, currentPlaying.getSong());
+ Notifications.showPlayingNotification(this, this, handler, currentPlaying.getSong());
} else {
- Util.hidePlayingNotification(this, this, handler);
+ Notifications.hidePlayingNotification(this, this, handler);
}
} else if(hide) {
- Util.hidePlayingNotification(this, this, handler);
+ Notifications.hidePlayingNotification(this, this, handler);
}
if(mRemoteControl != null) {
mRemoteControl.setPlaybackState(playerState.getRemoteControlClientPlayState());
@@ -1668,10 +1669,10 @@ public class DownloadService extends Service {
}
if(!backgroundDownloadList.isEmpty()) {
- Util.showDownloadingNotification(this, currentDownloading, backgroundDownloadList.size());
+ Notifications.showDownloadingNotification(this, currentDownloading, backgroundDownloadList.size());
downloadOngoing = true;
} else if(backgroundDownloadList.isEmpty() && downloadOngoing) {
- Util.hideDownloadingNotification(this);
+ Notifications.hideDownloadingNotification(this);
downloadOngoing = false;
}
diff --git a/src/github/daneren2005/dsub/util/Constants.java b/src/github/daneren2005/dsub/util/Constants.java
index ea2b5498..0c263d3a 100644
--- a/src/github/daneren2005/dsub/util/Constants.java
+++ b/src/github/daneren2005/dsub/util/Constants.java
@@ -62,10 +62,6 @@ public final class Constants {
public static final String INTENT_EXTRA_FRAGMENT_TYPE = "fragmentType";
public static final String INTENT_EXTRA_REFRESH_LISTINGS = "refreshListings";
- // Notification IDs.
- public static final int NOTIFICATION_ID_PLAYING = 100;
- public static final int NOTIFICATION_ID_DOWNLOADING = 102;
-
// Preferences keys.
public static final String PREFERENCES_KEY_SERVER_KEY = "server";
public static final String PREFERENCES_KEY_SERVER_COUNT = "serverCount";
diff --git a/src/github/daneren2005/dsub/util/Notifications.java b/src/github/daneren2005/dsub/util/Notifications.java
new file mode 100644
index 00000000..e77fed5d
--- /dev/null
+++ b/src/github/daneren2005/dsub/util/Notifications.java
@@ -0,0 +1,281 @@
+/*
+ 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 github.daneren2005.dsub.util;
+
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.graphics.Bitmap;
+import android.os.Build;
+import android.os.Handler;
+import android.support.v4.app.NotificationCompat;
+import android.util.Log;
+import android.view.KeyEvent;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.LinearLayout;
+import android.widget.RemoteViews;
+import android.widget.TextView;
+
+import github.daneren2005.dsub.R;
+import github.daneren2005.dsub.activity.SubsonicActivity;
+import github.daneren2005.dsub.activity.SubsonicFragmentActivity;
+import github.daneren2005.dsub.domain.MusicDirectory;
+import github.daneren2005.dsub.domain.PlayerState;
+import github.daneren2005.dsub.provider.DSubWidgetProvider;
+import github.daneren2005.dsub.service.DownloadFile;
+import github.daneren2005.dsub.service.DownloadService;
+
+public final class Notifications {
+ private static final String TAG = Notifications.class.getSimpleName();
+
+ // Notification IDs.
+ public static final int NOTIFICATION_ID_PLAYING = 100;
+ public static final int NOTIFICATION_ID_DOWNLOADING = 102;
+
+ private final static Pair<Integer, Integer> NOTIFICATION_TEXT_COLORS = new Pair<Integer, Integer>();
+
+ public static void showPlayingNotification(final Context context, final DownloadService downloadService, Handler handler, MusicDirectory.Entry song) {
+ // Set the icon, scrolling text and timestamp
+ final Notification notification = new Notification(R.drawable.stat_notify_playing, song.getTitle(), System.currentTimeMillis());
+ notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
+
+ boolean playing = downloadService.getPlayerState() == PlayerState.STARTED;
+ boolean remote = downloadService.isRemoteEnabled();
+ if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.JELLY_BEAN){
+ RemoteViews expandedContentView = new RemoteViews(context.getPackageName(), R.layout.notification_expanded);
+ setupViews(expandedContentView,context,song, playing, remote);
+ notification.bigContentView = expandedContentView;
+ }
+
+ RemoteViews smallContentView = new RemoteViews(context.getPackageName(), R.layout.notification);
+ setupViews(smallContentView, context, song, playing, remote);
+ notification.contentView = smallContentView;
+
+ Intent notificationIntent = new Intent(context, SubsonicFragmentActivity.class);
+ notificationIntent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD, true);
+ notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
+ notification.contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
+
+ handler.post(new Runnable() {
+ @Override
+ public void run() {
+ downloadService.startForeground(NOTIFICATION_ID_PLAYING, notification);
+ }
+ });
+
+ // Update widget
+ DSubWidgetProvider.notifyInstances(context, downloadService, playing);
+ }
+
+ private static void setupViews(RemoteViews rv, Context context, MusicDirectory.Entry song, boolean playing, boolean remote){
+
+ // Use the same text for the ticker and the expanded notification
+ String title = song.getTitle();
+ String arist = song.getArtist();
+ String album = song.getAlbum();
+
+ // Set the album art.
+ try {
+ ImageLoader imageLoader = SubsonicActivity.getStaticImageLoader(context);
+ Bitmap bitmap = null;
+ if(imageLoader != null) {
+ bitmap = imageLoader.getCachedImage(context, song, false);
+ }
+ if (bitmap == null) {
+ // set default album art
+ rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
+ } else {
+ rv.setImageViewBitmap(R.id.notification_image, bitmap);
+ }
+ } catch (Exception x) {
+ Log.w(TAG, "Failed to get notification cover art", x);
+ rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
+ }
+
+ // set the text for the notifications
+ rv.setTextViewText(R.id.notification_title, title);
+ rv.setTextViewText(R.id.notification_artist, arist);
+ rv.setTextViewText(R.id.notification_album, album);
+
+ /*Pair<Integer, Integer> colors = getNotificationTextColors(context);
+ if (colors.getFirst() != null) {
+ rv.setTextColor(R.id.notification_title, colors.getFirst());
+ }
+ if (colors.getSecond() != null) {
+ rv.setTextColor(R.id.notification_artist, colors.getSecond());
+ }*/
+
+ if(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_PERSISTENT_NOTIFICATION, false)) {
+ rv.setImageViewResource(R.id.control_previous, playing ? R.drawable.notification_pause : R.drawable.notification_play);
+ rv.setImageViewResource(R.id.control_pause, R.drawable.notification_next);
+ rv.setImageViewResource(R.id.control_next, R.drawable.notification_close);
+ }
+
+ // Create actions for media buttons
+ PendingIntent pendingIntent;
+ int previous = 0, pause = 0, next = 0, close = 0;
+ if(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_PERSISTENT_NOTIFICATION, false)) {
+ pause = R.id.control_previous;
+ next = R.id.control_pause;
+ close = R.id.control_next;
+ } else {
+ previous = R.id.control_previous;
+ pause = R.id.control_pause;
+ next = R.id.control_next;
+ }
+
+ if(remote && close == 0) {
+ close = R.id.notification_close;
+ rv.setViewVisibility(close, View.VISIBLE);
+ }
+
+ 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(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));
+ pendingIntent = PendingIntent.getService(context, 0, pauseIntent, 0);
+ rv.setOnClickPendingIntent(pause, pendingIntent);
+ } else {
+ Intent prevIntent = new Intent("KEYCODE_MEDIA_START");
+ prevIntent.setComponent(new ComponentName(context, DownloadService.class));
+ prevIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY));
+ pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
+ rv.setOnClickPendingIntent(pause, pendingIntent);
+ }
+ }
+ 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(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));
+ pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
+ rv.setOnClickPendingIntent(close, pendingIntent);
+ }
+ }
+
+ public static void hidePlayingNotification(final Context context, final DownloadService downloadService, Handler handler) {
+ // Remove notification and remove the service from the foreground
+ handler.post(new Runnable() {
+ @Override
+ public void run() {
+ downloadService.stopForeground(true);
+ }
+ });
+
+ // Update widget
+ DSubWidgetProvider.notifyInstances(context, downloadService, false);
+ }
+
+ public static void showDownloadingNotification(final Context context, DownloadFile file, int size) {
+ Intent cancelIntent = new Intent(context, DownloadService.class);
+ cancelIntent.setAction(DownloadService.CANCEL_DOWNLOADS);
+ PendingIntent cancelPI = PendingIntent.getService(context, 0, cancelIntent, 0);
+
+ String currentDownloading, currentSize;
+ if(file != null) {
+ currentDownloading = file.getSong().getTitle();
+ currentSize = Util.formatBytes(file.getEstimatedSize());
+ } else {
+ currentDownloading = "none";
+ currentSize = "0";
+ }
+
+ NotificationCompat.Builder builder;
+ builder = new NotificationCompat.Builder(context)
+ .setSmallIcon(android.R.drawable.stat_sys_download)
+ .setContentTitle(context.getResources().getString(R.string.download_downloading_title, size))
+ .setContentText(context.getResources().getString(R.string.download_downloading_summary, currentDownloading))
+ .setStyle(new NotificationCompat.BigTextStyle()
+ .bigText(context.getResources().getString(R.string.download_downloading_summary_expanded, currentDownloading, currentSize)))
+ .setProgress(10, 5, true)
+ .setOngoing(true)
+ .addAction(R.drawable.notification_close,
+ context.getResources().getString(R.string.common_cancel),
+ cancelPI);
+
+ Intent notificationIntent = new Intent(context, SubsonicFragmentActivity.class);
+ notificationIntent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD, true);
+ notificationIntent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD_VIEW, true);
+ notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
+ builder.setContentIntent(PendingIntent.getActivity(context, 1, notificationIntent, 0));
+
+ NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+ notificationManager.notify(NOTIFICATION_ID_DOWNLOADING, builder.build());
+
+ }
+ public static void hideDownloadingNotification(final Context context) {
+ NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+ notificationManager.cancel(NOTIFICATION_ID_DOWNLOADING);
+ }
+
+ /**
+ * Resolves the default text color for notifications.
+ *
+ * Based on http://stackoverflow.com/questions/4867338/custom-notification-layouts-and-text-colors/7320604#7320604
+ */
+ private static Pair<Integer, Integer> getNotificationTextColors(Context context) {
+ if (NOTIFICATION_TEXT_COLORS.getFirst() == null && NOTIFICATION_TEXT_COLORS.getSecond() == null) {
+ try {
+ Notification notification = new Notification();
+ String title = "title";
+ String content = "content";
+ notification.setLatestEventInfo(context, title, content, null);
+ LinearLayout group = new LinearLayout(context);
+ ViewGroup event = (ViewGroup) notification.contentView.apply(context, group);
+ findNotificationTextColors(event, title, content);
+ group.removeAllViews();
+ } catch (Exception x) {
+ Log.w(TAG, "Failed to resolve notification text colors.", x);
+ }
+ }
+ return NOTIFICATION_TEXT_COLORS;
+ }
+
+ private static void findNotificationTextColors(ViewGroup group, String title, String content) {
+ for (int i = 0; i < group.getChildCount(); i++) {
+ if (group.getChildAt(i) instanceof TextView) {
+ TextView textView = (TextView) group.getChildAt(i);
+ String text = textView.getText().toString();
+ if (title.equals(text)) {
+ NOTIFICATION_TEXT_COLORS.setFirst(textView.getTextColors().getDefaultColor());
+ }
+ else if (content.equals(text)) {
+ NOTIFICATION_TEXT_COLORS.setSecond(textView.getTextColors().getDefaultColor());
+ }
+ }
+ else if (group.getChildAt(i) instanceof ViewGroup)
+ findNotificationTextColors((ViewGroup) group.getChildAt(i), title, content);
+ }
+ }
+}
diff --git a/src/github/daneren2005/dsub/util/Util.java b/src/github/daneren2005/dsub/util/Util.java
index 6fe4b6bc..5e93ee55 100644
--- a/src/github/daneren2005/dsub/util/Util.java
+++ b/src/github/daneren2005/dsub/util/Util.java
@@ -118,7 +118,6 @@ public final class Util {
// Used by hexEncode()
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
- private final static Pair<Integer, Integer> NOTIFICATION_TEXT_COLORS = new Pair<Integer, Integer>();
private static Toast toast;
private Util() {
@@ -978,194 +977,6 @@ public final class Util {
.show();
}
- public static void showPlayingNotification(final Context context, final DownloadService downloadService, Handler handler, MusicDirectory.Entry song) {
- // Set the icon, scrolling text and timestamp
- final Notification notification = new Notification(R.drawable.stat_notify_playing, song.getTitle(), System.currentTimeMillis());
- notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
-
- boolean playing = downloadService.getPlayerState() == PlayerState.STARTED;
- boolean remote = downloadService.isRemoteEnabled();
- if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.JELLY_BEAN){
- RemoteViews expandedContentView = new RemoteViews(context.getPackageName(), R.layout.notification_expanded);
- setupViews(expandedContentView,context,song, playing, remote);
- notification.bigContentView = expandedContentView;
- }
-
- RemoteViews smallContentView = new RemoteViews(context.getPackageName(), R.layout.notification);
- setupViews(smallContentView, context, song, playing, remote);
- notification.contentView = smallContentView;
-
- Intent notificationIntent = new Intent(context, SubsonicFragmentActivity.class);
- notificationIntent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD, true);
- notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
- notification.contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
-
- handler.post(new Runnable() {
- @Override
- public void run() {
- downloadService.startForeground(Constants.NOTIFICATION_ID_PLAYING, notification);
- }
- });
-
- // Update widget
- DSubWidgetProvider.notifyInstances(context, downloadService, playing);
- }
-
- private static void setupViews(RemoteViews rv, Context context, MusicDirectory.Entry song, boolean playing, boolean remote){
-
- // Use the same text for the ticker and the expanded notification
- String title = song.getTitle();
- String arist = song.getArtist();
- String album = song.getAlbum();
-
- // Set the album art.
- try {
- ImageLoader imageLoader = SubsonicActivity.getStaticImageLoader(context);
- Bitmap bitmap = null;
- if(imageLoader != null) {
- bitmap = imageLoader.getCachedImage(context, song, false);
- }
- if (bitmap == null) {
- // set default album art
- rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
- } else {
- rv.setImageViewBitmap(R.id.notification_image, bitmap);
- }
- } catch (Exception x) {
- Log.w(TAG, "Failed to get notification cover art", x);
- rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
- }
-
- // set the text for the notifications
- rv.setTextViewText(R.id.notification_title, title);
- rv.setTextViewText(R.id.notification_artist, arist);
- rv.setTextViewText(R.id.notification_album, album);
-
- Pair<Integer, Integer> colors = getNotificationTextColors(context);
- if (colors.getFirst() != null) {
- rv.setTextColor(R.id.notification_title, colors.getFirst());
- }
- if (colors.getSecond() != null) {
- rv.setTextColor(R.id.notification_artist, colors.getSecond());
- }
-
- if(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_PERSISTENT_NOTIFICATION, false)) {
- rv.setImageViewResource(R.id.control_previous, playing ? R.drawable.notification_pause : R.drawable.notification_play);
- rv.setImageViewResource(R.id.control_pause, R.drawable.notification_next);
- rv.setImageViewResource(R.id.control_next, R.drawable.notification_close);
- }
-
- // Create actions for media buttons
- PendingIntent pendingIntent;
- int previous = 0, pause = 0, next = 0, close = 0;
- if(Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_PERSISTENT_NOTIFICATION, false)) {
- pause = R.id.control_previous;
- next = R.id.control_pause;
- close = R.id.control_next;
- } else {
- previous = R.id.control_previous;
- pause = R.id.control_pause;
- next = R.id.control_next;
- }
-
- if(remote && close == 0) {
- close = R.id.notification_close;
- rv.setViewVisibility(close, View.VISIBLE);
- }
-
- 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(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));
- pendingIntent = PendingIntent.getService(context, 0, pauseIntent, 0);
- rv.setOnClickPendingIntent(pause, pendingIntent);
- } else {
- Intent prevIntent = new Intent("KEYCODE_MEDIA_START");
- prevIntent.setComponent(new ComponentName(context, DownloadService.class));
- prevIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY));
- pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
- rv.setOnClickPendingIntent(pause, pendingIntent);
- }
- }
- 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(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));
- pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
- rv.setOnClickPendingIntent(close, pendingIntent);
- }
- }
-
- public static void hidePlayingNotification(final Context context, final DownloadService downloadService, Handler handler) {
- // Remove notification and remove the service from the foreground
- handler.post(new Runnable() {
- @Override
- public void run() {
- downloadService.stopForeground(true);
- }
- });
-
- // Update widget
- DSubWidgetProvider.notifyInstances(context, downloadService, false);
- }
-
- public static void showDownloadingNotification(final Context context, DownloadFile file, int size) {
- Intent cancelIntent = new Intent(context, DownloadService.class);
- cancelIntent.setAction(DownloadService.CANCEL_DOWNLOADS);
- PendingIntent cancelPI = PendingIntent.getService(context, 0, cancelIntent, 0);
-
- String currentDownloading, currentSize;
- if(file != null) {
- currentDownloading = file.getSong().getTitle();
- currentSize = Util.formatBytes(file.getEstimatedSize());
- } else {
- currentDownloading = "none";
- currentSize = "0";
- }
-
- NotificationCompat.Builder builder;
- builder = new NotificationCompat.Builder(context)
- .setSmallIcon(android.R.drawable.stat_sys_download)
- .setContentTitle(context.getResources().getString(R.string.download_downloading_title, size))
- .setContentText(context.getResources().getString(R.string.download_downloading_summary, currentDownloading))
- .setStyle(new NotificationCompat.BigTextStyle()
- .bigText(context.getResources().getString(R.string.download_downloading_summary_expanded, currentDownloading, currentSize)))
- .setProgress(10, 5, true)
- .setOngoing(true)
- .addAction(R.drawable.notification_close,
- context.getResources().getString(R.string.common_cancel),
- cancelPI);
-
- Intent notificationIntent = new Intent(context, SubsonicFragmentActivity.class);
- notificationIntent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD, true);
- notificationIntent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD_VIEW, true);
- notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
- builder.setContentIntent(PendingIntent.getActivity(context, 1, notificationIntent, 0));
-
- NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(Constants.NOTIFICATION_ID_DOWNLOADING, builder.build());
-
- }
- public static void hideDownloadingNotification(final Context context) {
- NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.cancel(Constants.NOTIFICATION_ID_DOWNLOADING);
- }
-
public static void sleepQuietly(long millis) {
try {
Thread.sleep(millis);
@@ -1381,46 +1192,6 @@ public final class Util {
intent.putExtra("coverart", "");
}
}
-
- /**
- * Resolves the default text color for notifications.
- *
- * Based on http://stackoverflow.com/questions/4867338/custom-notification-layouts-and-text-colors/7320604#7320604
- */
- private static Pair<Integer, Integer> getNotificationTextColors(Context context) {
- if (NOTIFICATION_TEXT_COLORS.getFirst() == null && NOTIFICATION_TEXT_COLORS.getSecond() == null) {
- try {
- Notification notification = new Notification();
- String title = "title";
- String content = "content";
- notification.setLatestEventInfo(context, title, content, null);
- LinearLayout group = new LinearLayout(context);
- ViewGroup event = (ViewGroup) notification.contentView.apply(context, group);
- findNotificationTextColors(event, title, content);
- group.removeAllViews();
- } catch (Exception x) {
- Log.w(TAG, "Failed to resolve notification text colors.", x);
- }
- }
- return NOTIFICATION_TEXT_COLORS;
- }
-
- private static void findNotificationTextColors(ViewGroup group, String title, String content) {
- for (int i = 0; i < group.getChildCount(); i++) {
- if (group.getChildAt(i) instanceof TextView) {
- TextView textView = (TextView) group.getChildAt(i);
- String text = textView.getText().toString();
- if (title.equals(text)) {
- NOTIFICATION_TEXT_COLORS.setFirst(textView.getTextColors().getDefaultColor());
- }
- else if (content.equals(text)) {
- NOTIFICATION_TEXT_COLORS.setSecond(textView.getTextColors().getDefaultColor());
- }
- }
- else if (group.getChildAt(i) instanceof ViewGroup)
- findNotificationTextColors((ViewGroup) group.getChildAt(i), title, content);
- }
- }
public static WifiManager.WifiLock createWifiLock(Context context, String tag) {
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);