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: 8277e13d5ded657ef727945f5e738d6a139c992d (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
package de.luhmer.owncloudnewsreader.view;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import org.greenrobot.eventbus.Subscribe;

import java.util.Locale;

import de.luhmer.owncloudnewsreader.notification.NextcloudNotificationManager;

public class PodcastNotification {

    private static final String TAG = PodcastNotification.class.getCanonicalName();
    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 final Context mContext;
    private final NotificationManager notificationManager;
    private NotificationCompat.Builder notificationBuilder;
    private final String CHANNEL_ID = "Podcast Notification";

    private MediaSessionCompat mSession;
    private @PlaybackStateCompat.State int lastStatus = PlaybackStateCompat.STATE_NONE;

    public final static int NOTIFICATION_ID = 1111;

    public PodcastNotification(Context context, MediaSessionCompat session) {
        this.mContext = context;
        this.mSession = session;
        this.notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        this.notificationBuilder = NextcloudNotificationManager.buildPodcastNotification(mContext, CHANNEL_ID, mSession);

        //EventBus.getDefault().register(this);
    }

    @Subscribe
    public void updateStateOfNotification(@PlaybackStateCompat.State int status, long currentPosition, long totalDuration) {
        if(mSession == null) {
            Log.v(TAG, "Session null.. ignore UpdatePodcastStatusEvent");
            return;
        }

        if (status != lastStatus) {
            lastStatus = status;


            /*
            if(podcast.isPlaying()) {
                //Prevent the Podcast Player from getting killed because of low memory
                //For more info see: http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)
                ((PodcastPlaybackService)mContext).startForeground(NOTIFICATION_ID, notificationBuilder.build());

                notificationBuilder.setOngoing(true); // Non cancelable (sort above the others)
            } else {
                ((PodcastPlaybackService)mContext).stopForeground(false);

                notificationBuilder.setOngoing(false); // cancelable
            }
            */

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

            notificationBuilder = NextcloudNotificationManager.buildPodcastNotification(mContext, CHANNEL_ID, mSession);

            //int drawableId = podcast.isPlaying() ? android.R.drawable.ic_media_pause : android.R.drawable.ic_media_play;
            //String actionText = podcast.isPlaying() ? "Pause" : "Play";
            //notificationBuilder.addAction(new NotificationCompat.Action(drawableId, actionText, intent));

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


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

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



        double progressDouble = ((double)currentPosition / (double)totalDuration) * 100d;
        int progress = ((int) progressDouble);

        notificationBuilder
                .setContentText(fromText + " - " + toText)
                .setProgress(100, progress, status == PlaybackStateCompat.STATE_CONNECTING); // TODO IMPLEMENT THIS!!!!

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

    public void cancel()
    {
        if(notificationManager != null) {
            notificationManager.cancel(NOTIFICATION_ID);
        }
        /*
        if(mSession != null) {
            mSession.setActive(false);
        }
        */
    }

    public void createPodcastNotification() {
        /*
        MediaItem 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, podcastItem.author)
                .putString(MediaMetadataCompat.METADATA_KEY_TITLE, podcastItem.title)
                .build());
        */

        /*
        mSession.setMetadata(new MediaMetadataCompat.Builder()
                .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, "NA")
                .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, "")
                .putString(MediaMetadataCompat.METADATA_KEY_TITLE, "NA")
                //.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());
        */

        this.notificationBuilder = NextcloudNotificationManager.buildPodcastNotification(mContext, CHANNEL_ID, mSession);
        notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
    }

    public Notification getNotification() {
        return notificationBuilder.build();
    }
}