Notification!

This commit is contained in:
Tobias Fella 2021-08-06 23:23:54 +02:00
parent 76b1d5c30d
commit f4a754ad1c
11 changed files with 249 additions and 50 deletions

View File

@ -7,7 +7,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.kde.kasts"
android:versionName="0.0.1"
android:versionCode="1628239323"
android:versionCode="1628285196"
android:installLocation="auto">
<supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/>
@ -16,7 +16,7 @@
<application android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="Kasts" android:icon="@mipmap/ic_launcher">
<activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation"
android:name="org.kde.kasts.Activity"
android:name="org.kde.kasts.KastsActivity"
android:label="Kasts"
android:windowSoftInputMode="adjustResize"
android:launchMode="singleTop">
@ -57,9 +57,6 @@
<!-- auto screen scale factor -->
<meta-data android:name="android.app.auto_screen_scale_factor" android:value="true"/>
</activity>
<service android:name="org.kde.kasts.MediaService" >
<meta-data android:name="android.app.background_running" android:value="true"/>
</service>
</application>
</manifest>

View File

@ -29,6 +29,7 @@ def timestamp = (int)(new Date().getTime()/1000)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
implementation 'com.android.support:support-v4:28.0.0'
implementation 'androidx.core:core:1.3.1'
}
android {

View File

@ -1 +1,2 @@
android.useAndroidX=true
android.enableJetifier=true

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M6,18l8.5,-6L6,6v12zM16,6v12h2V6h-2z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M6,19h4L10,5L6,5v14zM14,5v14h4L18,5h-4z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M8,5v14l11,-7z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M6,6h2v12L6,18zM9.5,12l8.5,6L18,6z"/>
</vector>

View File

@ -1,27 +0,0 @@
// SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
// SPDX-License-Identifier: LGPL-2.0-or-later
package org.kde.kasts;
import org.qtproject.qt5.android.bindings.QtActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract;
import android.os.Bundle;
import android.util.Log;
import java.io.*;
import java.util.*;
public class Activity extends QtActivity
{
private static final String TAG = "org.kde.kasts";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}

View File

@ -0,0 +1,194 @@
// SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
// SPDX-License-Identifier: LGPL-2.0-or-later
package org.kde.kasts;
import org.qtproject.qt5.android.bindings.QtActivity;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract;
import android.os.Bundle;
import android.util.Log;
import android.app.NotificationManager;
import android.app.NotificationChannel;
import android.app.Service;
import android.content.Context;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.support.v4.media.MediaMetadataCompat;
import androidx.core.app.NotificationCompat;
import androidx.media.app.NotificationCompat.MediaStyle;
import androidx.core.content.ContextCompat;
import java.io.*;
import java.util.*;
public class KastsActivity extends QtActivity
{
private static final String TAG = "org.kde.kasts.mediasession";
class MediaData {
public String title;
public long length;
public long position;
// add more variables here
}
static MediaData mediaData;
private static MediaSessionCompat mSession;
private static PlaybackStateCompat.Builder mPBuilder;
private static KastsActivity activity;
void updateNotification() {
// TODO: Change all of these variables to the values in mediaData
// add other required values
MediaMetadataCompat.Builder metadata = new MediaMetadataCompat.Builder();
metadata.putString(MediaMetadataCompat.METADATA_KEY_TITLE, "The title");
metadata.putString(MediaMetadataCompat.METADATA_KEY_AUTHOR, "Author");
metadata.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, "Author");
metadata.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, "The album");
metadata.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, 1000000);
//TODO Image
mSession.setMetadata(metadata.build());
mPBuilder.setState(PlaybackStateCompat.STATE_PLAYING, 100000, 1.0f);
Intent iPlay = new Intent(this, MediaSessionCallback.class);
iPlay.setAction("ACTION_PLAY");
PendingIntent piPlay = PendingIntent.getBroadcast(this, 0, iPlay, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action.Builder aPlay = new NotificationCompat.Action.Builder(
R.drawable.ic_play_white, "Play", piPlay);
Intent iPause = new Intent(this, MediaSessionCallback.class);
iPause.setAction("ACTION_PAUSE");
PendingIntent piPause = PendingIntent.getBroadcast(this, 0, iPause, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action.Builder aPause = new NotificationCompat.Action.Builder(
R.drawable.ic_pause_white, "Pause", piPause);
Intent iPrevious = new Intent(this, MediaSessionCallback.class);
iPrevious.setAction("ACTION_PREVIOUS");
PendingIntent piPrevious = PendingIntent.getBroadcast(this, 0, iPrevious, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action.Builder aPrevious = new NotificationCompat.Action.Builder(
R.drawable.ic_previous_white, "Previous", piPrevious);
Intent iNext = new Intent(this, MediaSessionCallback.class);
iNext.setAction("ACTION_NEXT");
PendingIntent piNext = PendingIntent.getBroadcast(this, 0, iNext, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action.Builder aNext = new NotificationCompat.Action.Builder(
R.drawable.ic_next_white, "Next", piNext);
Intent iOpenActivity = new Intent(this, KastsActivity.class);
iOpenActivity.putExtra("deviceId", "device");
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "media_control");
notification
.setAutoCancel(false)
.setShowWhen(false)
.setVisibility(androidx.core.app.NotificationCompat.VISIBILITY_PUBLIC)
.setSubText("foobar")
.setContentTitle("Foo's title")
.setSmallIcon(this.getApplicationInfo().icon)
.setChannelId("org.kde.neochat.channel")
.setContentText("some random text");
notification.addAction(aPrevious.build());
notification.addAction(aPause.build());
notification.addAction(aNext.build());
mSession.setPlaybackState(mPBuilder.build());
MediaStyle mediaStyle = new MediaStyle();
mediaStyle.setMediaSession(mSession.getSessionToken());
mediaStyle.setShowActionsInCompactView(0, 1, 2);
notification.setStyle(mediaStyle);
notification.setGroup("MprisMediaSession");
mSession.setActive(true);
NotificationManager nm = ContextCompat.getSystemService(this, NotificationManager.class);
NotificationChannel channel = new NotificationChannel("org.kde.neochat.channel", "KastsChannel", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("The notification channel");
channel.enableLights(false);
channel.enableVibration(false);
nm.createNotificationChannel(channel);
nm.notify(0x487671, notification.build());
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSession = new MediaSessionCompat(this, TAG);
mSession.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mSession.setCallback(new MediaSessionCallback(this));
mPBuilder = new PlaybackStateCompat.Builder();
activity = this;
mediaData = new MediaData();
updateNotification();
}
@Override
public void onDestroy() {
super.onDestroy();
mSession.release();
}
public static void setSessionState(int state)
{
//TODO: set state in mediadata
activity.updateNotification();
}
private class MediaSessionCallback extends MediaSessionCompat.Callback {
private Context mContext;
public MediaSessionCallback(Context context) {
super();
mContext = context;
}
@Override
public void onPlay() {
super.onPlay();
mSession.setActive(true);
//Update variables of mediaData;
activity.updateNotification();
//JNI to audiomanager play
//setPlaybackState for mSession
}
@Override
public void onPause() {
super.onPause();
//JNI to audiomanager pause
//setPlaybackState for mSession
}
@Override
public void onStop() {
super.onStop();
//JNI call to audiomanager stop
mSession.setActive(false);
}
}
}

View File

@ -74,7 +74,7 @@ AudioManager::AudioManager(QObject *parent)
connect(this, &AudioManager::logError, &ErrorLogModel::instance(), &ErrorLogModel::monitorErrorMessages);
MediaSessionClient mClient(this);
new MediaSessionClient(this);
// Check if an entry was playing when the program was shut down and restore it
if (DataManager::instance().lastPlayingEntry() != QStringLiteral("none")) {

View File

@ -21,29 +21,26 @@ void MediaSessionClient::setState()
{
qDebug() << m_audioPlayer->playbackState();
switch(m_audioPlayer->playbackState()) {
case QMediaPlayer::StoppedState : {
QAndroidJniObject::callStaticMethod<jint>
("org/kde/kasts/MediaService"
case QMediaPlayer::StoppedState :
QAndroidJniObject::callStaticMethod<void>
("org/kde/kasts/KastsActivity"
, "setSessionState"
, "(I)I"
, "(I)V"
, 2);
break;
}
case QMediaPlayer::PausedState : {
QAndroidJniObject::callStaticMethod<jint>
("org/kde/kasts/MediaService"
break;
case QMediaPlayer::PausedState :
QAndroidJniObject::callStaticMethod<void>
("org/kde/kasts/KastsActivity"
, "setSessionState"
, "(I)I"
, "(I)V"
, 1);
break;
}
case QMediaPlayer::PlayingState : {
QAndroidJniObject::callStaticMethod<jint>
("org/kde/kasts/MediaService"
break;
case QMediaPlayer::PlayingState :
QAndroidJniObject::callStaticMethod<void>
("org/kde/kasts/KastsActivity"
, "setSessionState"
, "(I)I"
, "(I)V"
, 0);
break;
}
}
}