Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,80 +1,55 @@
package com.tencent.cloud.tuikit.flutter.rtcconferencetuikit.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.Manifest;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;

public class ForegroundService extends Service {
private static final String TAG = "KeepAliveService";
private static final String TITLE = "title";
private static final String DESCRIPTION = "description";
import com.tencent.qcloud.tuicore.util.TUIBuild;
import com.trtc.tuikit.common.foregroundservice.AudioForegroundService;
import com.trtc.tuikit.common.foregroundservice.MediaForegroundService;
import com.trtc.tuikit.common.system.ContextProvider;

private static final int NOTIFICATION_ID = 1001;
/**
* App are kept alive to ensure that process are not killed by the system in the background.
* You can delete them if they are not needed.
*/
public class KeepAliveService extends Service {
private static final String TAG = "KeepAliveService";

public static void startForegroundService(Context context) {
Intent intent = new Intent(context, ForegroundService.class);
intent.putExtra(TITLE, "rtc_conference_tuikit");
intent.putExtra(DESCRIPTION, "rtc_conference_tuikit running");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
String appName = context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
String notificationText = "";
if (hasRecordAudioPermission()) {
Log.i(TAG, "startForegroundService audio");
AudioForegroundService.start(context, appName, notificationText, 0);
} else {
context.startService(intent);
Log.i(TAG, "startForegroundService media");
MediaForegroundService.start(context, appName, notificationText, 0);
}
}

public static void stopForegroundService(Context context) {
Log.d(TAG, "stop keep alive service");
Intent intent = new Intent(context, ForegroundService.class);
context.stopService(intent);
if (hasRecordAudioPermission()) {
Log.i(TAG, "stopForegroundService audio");
AudioForegroundService.stop(context);
} else {
Log.i(TAG, "stopForegroundService media");
MediaForegroundService.stop(context);
}
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null) {
return START_NOT_STICKY;
}
String title = intent.getStringExtra(TITLE);
String description = intent.getStringExtra(DESCRIPTION);
if (TextUtils.isEmpty(title) || TextUtils.isEmpty(description)) {
Log.e(TAG, "on start command wrong params");
return START_NOT_STICKY;
}
Notification notification = createForegroundNotification(title, description);
startForeground(NOTIFICATION_ID, notification);
return START_NOT_STICKY;
}

private Notification createForegroundNotification(String title, String description) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String notificationChannelId = "notification_channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelName = "RTC Room Foreground Service";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel =
new NotificationChannel(notificationChannelId, channelName, importance);
notificationChannel.setDescription("Channel description");
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
}

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, notificationChannelId);
builder.setSmallIcon(android.R.mipmap.sym_def_app_icon);
builder.setContentTitle(title);
builder.setContentText(description);
builder.setWhen(System.currentTimeMillis());
return builder.build();
}

@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
Expand All @@ -85,4 +60,12 @@ public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
stopSelf();
}

private static boolean hasRecordAudioPermission() {
if (TUIBuild.getVersionInt() < Build.VERSION_CODES.M) {
return true;
}
return ContextCompat.checkSelfPermission(ContextProvider.getApplicationContext(), Manifest.permission.RECORD_AUDIO)
== PackageManager.PERMISSION_GRANTED;
}
}