NekoX/TMessagesProj/src/main/java/org/telegram/messenger/VideoEncodingService.java

116 lines
4.8 KiB
Java
Raw Normal View History

/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
*/
2015-09-24 22:52:02 +02:00
package org.telegram.messenger;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
2019-05-14 14:08:05 +02:00
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
public class VideoEncodingService extends Service implements NotificationCenter.NotificationCenterDelegate {
2016-10-11 13:57:01 +02:00
private NotificationCompat.Builder builder;
private String path;
private int currentProgress;
2018-07-30 04:07:02 +02:00
private int currentAccount;
public VideoEncodingService() {
super();
2018-07-30 04:07:02 +02:00
NotificationCenter.getGlobalInstance().addObserver(this, NotificationCenter.stopEncodingService);
}
public IBinder onBind(Intent arg2) {
return null;
}
public void onDestroy() {
2019-09-10 12:56:11 +02:00
super.onDestroy();
try {
stopForeground(true);
} catch (Throwable ignore) {
}
2019-05-14 14:08:05 +02:00
NotificationManagerCompat.from(ApplicationLoader.applicationContext).cancel(4);
2018-07-30 04:07:02 +02:00
NotificationCenter.getGlobalInstance().removeObserver(this, NotificationCenter.stopEncodingService);
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.FileUploadProgressChanged);
if (BuildVars.LOGS_ENABLED) {
FileLog.d("destroy video service");
}
}
@Override
2018-07-30 04:07:02 +02:00
public void didReceivedNotification(int id, int account, Object... args) {
if (id == NotificationCenter.FileUploadProgressChanged) {
2018-07-30 04:07:02 +02:00
String fileName = (String) args[0];
if (account == currentAccount && path != null && path.equals(fileName)) {
2020-01-23 07:15:40 +01:00
Long loadedSize = (Long) args[1];
Long totalSize = (Long) args[2];
float progress = Math.min(1f, loadedSize / (float) totalSize);
Boolean enc = (Boolean) args[3];
2018-07-30 04:07:02 +02:00
currentProgress = (int) (progress * 100);
builder.setProgress(100, currentProgress, currentProgress == 0);
2017-03-31 01:58:05 +02:00
try {
NotificationManagerCompat.from(ApplicationLoader.applicationContext).notify(4, builder.build());
} catch (Throwable e) {
FileLog.e(e);
}
}
} else if (id == NotificationCenter.stopEncodingService) {
2018-07-30 04:07:02 +02:00
String filepath = (String) args[0];
account = (Integer) args[1];
if (account == currentAccount && (filepath == null || filepath.equals(path))) {
stopSelf();
}
}
}
public int onStartCommand(Intent intent, int flags, int startId) {
path = intent.getStringExtra("path");
2018-07-30 04:07:02 +02:00
int oldAccount = currentAccount;
currentAccount = intent.getIntExtra("currentAccount", UserConfig.selectedAccount);
2021-04-09 15:17:32 +02:00
if (!UserConfig.isValidAccount(currentAccount)) {
stopSelf();
return Service.START_NOT_STICKY;
}
2018-07-30 04:07:02 +02:00
if (oldAccount != currentAccount) {
NotificationCenter.getInstance(oldAccount).removeObserver(this, NotificationCenter.FileUploadProgressChanged);
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.FileUploadProgressChanged);
}
2016-10-11 13:57:01 +02:00
boolean isGif = intent.getBooleanExtra("gif", false);
if (path == null) {
stopSelf();
return Service.START_NOT_STICKY;
}
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("start video service");
}
if (builder == null) {
2018-08-27 10:33:11 +02:00
NotificationsController.checkOtherNotificationsChannel();
builder = new NotificationCompat.Builder(ApplicationLoader.applicationContext);
builder.setSmallIcon(android.R.drawable.stat_sys_upload);
builder.setWhen(System.currentTimeMillis());
2018-07-30 04:07:02 +02:00
builder.setChannelId(NotificationsController.OTHER_NOTIFICATIONS_CHANNEL);
builder.setContentTitle(LocaleController.getString("AppName", R.string.AppName));
2016-10-11 13:57:01 +02:00
if (isGif) {
builder.setTicker(LocaleController.getString("SendingGif", R.string.SendingGif));
builder.setContentText(LocaleController.getString("SendingGif", R.string.SendingGif));
} else {
builder.setTicker(LocaleController.getString("SendingVideo", R.string.SendingVideo));
builder.setContentText(LocaleController.getString("SendingVideo", R.string.SendingVideo));
}
}
currentProgress = 0;
builder.setProgress(100, currentProgress, currentProgress == 0);
startForeground(4, builder.build());
NotificationManagerCompat.from(ApplicationLoader.applicationContext).notify(4, builder.build());
return Service.START_NOT_STICKY;
}
}