Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PodcastNotification.java « view « owncloudnewsreader « luhmer « de « java « main « src « News-Android-App - github.com/nextcloud/news-android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d7aa25863c68702ea88b2867580105d0cdf1b914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package de.luhmer.owncloudnewsreader.view;

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.media.AudioManager;
import android.media.session.MediaSessionManager;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import de.greenrobot.event.EventBus;
import de.luhmer.owncloudnewsreader.NewsReaderListActivity;
import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.events.podcast.TogglePlayerStateEvent;
import de.luhmer.owncloudnewsreader.events.podcast.UpdatePodcastStatusEvent;
import de.luhmer.owncloudnewsreader.events.podcast.broadcastreceiver.PodcastNotificationToggle;
import de.luhmer.owncloudnewsreader.model.PodcastItem;
import de.luhmer.owncloudnewsreader.services.PodcastPlaybackService;

public class PodcastNotification {

    public static final String ACTION_PLAY = "action_play";
    public static final String ACTION_PAUSE = "action_pause";
    //public static final String ACTION_NEXT = "action_next";
    //public static final String ACTION_PREVIOUS = "action_previous";
    //public static final String ACTION_STOP = "action_stop";

    private Context mContext;
    private NotificationManager notificationManager;
    private EventBus eventBus;
    private NotificationCompat.Builder notificationBuilder;
    private PendingIntent resultPendingIntent;

    private MediaSessionManager mManager;
    public MediaSessionCompat mSession;
    private MediaControllerCompat mController;

    private final static int NOTIFICATION_ID = 1111;

    public PodcastNotification(Context context) {
        this.mContext = context;
        this.notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        eventBus = EventBus.getDefault();
        eventBus.register(this);
    }

    public void unbind() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mSession.release();
        }
    }

    private void createNewNotificationBuilder() {
        // Creates an explicit intent for an ResultActivity to receive.
        Intent resultIntent = new Intent(mContext, NewsReaderListActivity.class);
        // Because clicking the notification opens a new ("special") activity, there's
        // no need to create an artificial back stack.
        resultPendingIntent =
                PendingIntent.getActivity(
                        mContext,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        // Create the final Notification object.
        notificationBuilder = new NotificationCompat.Builder(mContext)
                .setSmallIcon(R.drawable.ic_notification)
                .setAutoCancel(true)
                .setOngoing(true)
                .setContentIntent(resultPendingIntent);
    }


    int lastDrawableId = -1;

    public void onEvent(UpdatePodcastStatusEvent podcast) {
        if(!podcast.isFileLoaded())
            return;

        int drawableId = podcast.isPlaying() ? android.R.drawable.ic_media_pause : android.R.drawable.ic_media_play;
        String actionText = podcast.isPlaying() ? "Pause" : "Play";


        if(lastDrawableId != drawableId) {
            lastDrawableId = drawableId;

            createNewNotificationBuilder();
            notificationBuilder.setContentTitle(podcast.getTitle());
            notificationBuilder.addAction(drawableId, actionText, PendingIntent.getBroadcast(mContext, 0, new Intent(mContext,
                            PodcastNotificationToggle.class),
                    PendingIntent.FLAG_ONE_SHOT));


            //Lock screen notification
            /*
            mSession.setMetadata(new MediaMetadataCompat.Builder()
                    .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, podcast.getMax())
                    .build());
                    */

            if(podcast.isPlaying()) {
                mSession.setPlaybackState(new PlaybackStateCompat.Builder()
                        .setState(PlaybackStateCompat.STATE_PLAYING, podcast.getCurrent(), 1.0f)
                        .setActions(PlaybackStateCompat.ACTION_PAUSE).build());
            } else {
                mSession.setPlaybackState(new PlaybackStateCompat.Builder()
                        .setState(PlaybackStateCompat.STATE_PAUSED, podcast.getCurrent(), 0.0f)
                        .setActions(PlaybackStateCompat.ACTION_PLAY).build());
            }
        }


        int hours = (int)( podcast.getCurrent() / (1000*60*60));
        int minutes = (int)(podcast.getCurrent() % (1000*60*60)) / (1000*60);
        int seconds = (int) ((podcast.getCurrent() % (1000*60*60)) % (1000*60) / 1000);
        minutes += hours * 60;
        String fromText = (String.format("%02d:%02d", minutes, seconds));

        hours = (int)( podcast.getMax() / (1000*60*60));
        minutes = (int)(podcast.getMax() % (1000*60*60)) / (1000*60);
        seconds = (int) ((podcast.getMax() % (1000*60*60)) % (1000*60) / 1000);
        minutes += hours * 60;
        String toText = (String.format("%02d:%02d", minutes, seconds));



        double progressDouble = ((double)podcast.getCurrent() / (double)podcast.getMax()) * 100d;
        int progress = ((int) progressDouble);


        notificationBuilder
                .setContentText(fromText + " - " + toText)
                .setProgress(100, progress, podcast.isPreparingFile());

        notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
    }

    public void cancel()
    {
        notificationManager.cancel(NOTIFICATION_ID);

        mSession.setActive(false);
    }

    public void podcastChanged() {
        initMediaSessions();
    }

    private void initMediaSessions() {
        String packageName = PodcastNotificationToggle.class.getPackage().getName();
        ComponentName receiver = new ComponentName(packageName, PodcastNotificationToggle.class.getName());
        mSession = new MediaSessionCompat(mContext, "PlayerService", receiver, null);
        mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
                MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        mSession.setPlaybackState(new PlaybackStateCompat.Builder()
                .setState(PlaybackStateCompat.STATE_PAUSED, 0, 0)
                .setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE).build());


        PodcastItem podcastItem = ((PodcastPlaybackService)mContext).getCurrentlyPlayingPodcast();

        String favIconUrl = podcastItem.favIcon;
        DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder().
                showImageOnLoading(R.drawable.default_feed_icon_light).
                showImageForEmptyUri(R.drawable.default_feed_icon_light).
                showImageOnFail(R.drawable.default_feed_icon_light).
                build();
        Bitmap bmpAlbumArt = ImageLoader.getInstance().loadImageSync(favIconUrl, displayImageOptions);



        mSession.setMetadata(new MediaMetadataCompat.Builder()
                .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, "Test")
                .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, "Test")
                .putString(MediaMetadataCompat.METADATA_KEY_TITLE, "Test")
                        //.putString(MediaMetadataCompat.METADATA_KEY_TITLE, podcastItem.title)
                .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, 100)
                .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bmpAlbumArt)
                /* .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART,
                        BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher)) */
                .build());

        mSession.setCallback(new MediaSessionCallback());


        AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        audioManager.requestAudioFocus(new AudioManager.OnAudioFocusChangeListener() {
            @Override
            public void onAudioFocusChange(int focusChange) {
                // Ignore
            }
        }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

        MediaControllerCompat controller = mSession.getController();


        mSession.setActive(true);
    }


    private final class MediaSessionCallback extends MediaSessionCompat.Callback {
        @Override
        public void onPlay() {
            EventBus.getDefault().post(new TogglePlayerStateEvent());
        }

        @Override
        public void onPause() {
            EventBus.getDefault().post(new TogglePlayerStateEvent());
        }
    }

}