2021-06-24 23:10:45 +00:00
|
|
|
package im.cwtch.flwtch
|
|
|
|
|
|
|
|
import android.app.*
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.graphics.BitmapFactory
|
|
|
|
import android.graphics.Color
|
|
|
|
import android.os.Build
|
|
|
|
import android.util.Log
|
|
|
|
import androidx.annotation.RequiresApi
|
|
|
|
import androidx.core.app.NotificationCompat
|
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
|
|
|
import androidx.work.*
|
|
|
|
import cwtch.Cwtch
|
|
|
|
import io.flutter.FlutterInjector
|
|
|
|
import org.json.JSONObject
|
|
|
|
|
2021-09-27 19:53:21 +00:00
|
|
|
import java.nio.file.Files
|
|
|
|
import java.nio.file.Paths
|
|
|
|
import java.nio.file.StandardCopyOption
|
|
|
|
import android.net.Uri
|
2021-06-24 23:10:45 +00:00
|
|
|
|
|
|
|
class FlwtchWorker(context: Context, parameters: WorkerParameters) :
|
|
|
|
CoroutineWorker(context, parameters) {
|
|
|
|
private val notificationManager =
|
|
|
|
context.getSystemService(Context.NOTIFICATION_SERVICE) as
|
|
|
|
NotificationManager
|
|
|
|
|
|
|
|
private var notificationID: MutableMap<String, Int> = mutableMapOf()
|
|
|
|
private var notificationIDnext: Int = 1
|
|
|
|
|
|
|
|
override suspend fun doWork(): Result {
|
|
|
|
val method = inputData.getString(KEY_METHOD)
|
|
|
|
?: return Result.failure()
|
|
|
|
val args = inputData.getString(KEY_ARGS)
|
|
|
|
?: return Result.failure()
|
|
|
|
// Mark the Worker as important
|
|
|
|
val progress = "Cwtch is keeping Tor running in the background"//todo:translate
|
|
|
|
setForeground(createForegroundInfo(progress))
|
|
|
|
return handleCwtch(method, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getNotificationID(profile: String, contact: String): Int {
|
|
|
|
val k = "$profile $contact"
|
|
|
|
if (!notificationID.containsKey(k)) {
|
|
|
|
notificationID[k] = notificationIDnext++
|
|
|
|
}
|
|
|
|
return notificationID[k] ?: -1
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun handleCwtch(method: String, args: String): Result {
|
|
|
|
val a = JSONObject(args)
|
|
|
|
when (method) {
|
|
|
|
"Start" -> {
|
|
|
|
Log.i("FlwtchWorker.kt", "handleAppInfo Start")
|
|
|
|
val appDir = (a.get("appDir") as? String) ?: ""
|
|
|
|
val torPath = (a.get("torPath") as? String) ?: "tor"
|
|
|
|
Log.i("FlwtchWorker.kt", "appDir: '$appDir' torPath: '$torPath'")
|
|
|
|
|
|
|
|
if (Cwtch.startCwtch(appDir, torPath) != 0.toLong()) return Result.failure()
|
|
|
|
|
|
|
|
Log.i("FlwtchWorker.kt", "startCwtch success, starting coroutine AppbusEvent loop...")
|
2021-09-30 00:16:00 +00:00
|
|
|
val downloadIDs = mutableMapOf<String, Int>()
|
2021-06-24 23:10:45 +00:00
|
|
|
while(true) {
|
|
|
|
val evt = MainActivity.AppbusEvent(Cwtch.getAppBusEvent())
|
|
|
|
if (evt.EventType == "NewMessageFromPeer" || evt.EventType == "NewMessageFromGroup") {
|
|
|
|
val data = JSONObject(evt.Data)
|
2021-06-29 18:49:29 +00:00
|
|
|
val handle = if (evt.EventType == "NewMessageFromPeer") data.getString("RemotePeer") else data.getString("GroupID");
|
2021-06-25 00:59:54 +00:00
|
|
|
if (data["RemotePeer"] != data["ProfileOnion"]) {
|
|
|
|
val channelId =
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2021-06-29 18:49:29 +00:00
|
|
|
createMessageNotificationChannel(handle, handle)
|
2021-06-25 00:59:54 +00:00
|
|
|
} else {
|
|
|
|
// If earlier version channel ID is not used
|
|
|
|
// https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context)
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
|
|
|
val loader = FlutterInjector.instance().flutterLoader()
|
|
|
|
val key = loader.getLookupKeyForAsset("assets/" + data.getString("Picture"))//"assets/profiles/001-centaur.png")
|
|
|
|
val fh = applicationContext.assets.open(key)
|
|
|
|
|
2021-06-29 18:49:29 +00:00
|
|
|
|
2021-06-25 00:59:54 +00:00
|
|
|
val clickIntent = Intent(applicationContext, MainActivity::class.java).also { intent ->
|
|
|
|
intent.action = Intent.ACTION_RUN
|
|
|
|
intent.putExtra("EventType", "NotificationClicked")
|
|
|
|
intent.putExtra("ProfileOnion", data.getString("ProfileOnion"))
|
2021-06-29 18:49:29 +00:00
|
|
|
intent.putExtra("Handle", handle)
|
2021-06-25 00:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val newNotification = NotificationCompat.Builder(applicationContext, channelId)
|
|
|
|
.setContentTitle(data.getString("Nick"))
|
|
|
|
.setContentText("New message")//todo: translate
|
|
|
|
.setLargeIcon(BitmapFactory.decodeStream(fh))
|
2021-07-15 20:03:57 +00:00
|
|
|
.setSmallIcon(R.mipmap.knott_transparent)
|
2021-06-25 00:59:54 +00:00
|
|
|
.setContentIntent(PendingIntent.getActivity(applicationContext, 1, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT))
|
|
|
|
.setAutoCancel(true)
|
|
|
|
.build()
|
2021-06-29 18:49:29 +00:00
|
|
|
notificationManager.notify(getNotificationID(data.getString("ProfileOnion"), handle), newNotification)
|
2021-06-24 23:10:45 +00:00
|
|
|
}
|
2021-09-30 00:16:00 +00:00
|
|
|
} else if (evt.EventType == "FileDownloadProgressUpdate") {
|
|
|
|
try {
|
|
|
|
val data = JSONObject(evt.Data);
|
|
|
|
val fileKey = data.getString("FileKey");
|
|
|
|
val title = data.getString("NameSuggestion");
|
|
|
|
val progress = data.getString("Progress").toInt();
|
|
|
|
val progressMax = data.getString("FileSizeInChunks").toInt();
|
|
|
|
if (!downloadIDs.containsKey(fileKey)) {
|
|
|
|
downloadIDs.put(fileKey, downloadIDs.count());
|
|
|
|
}
|
|
|
|
var dlID = downloadIDs.get(fileKey);
|
|
|
|
if (dlID == null) {
|
|
|
|
dlID = 0;
|
|
|
|
}
|
|
|
|
val channelId =
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
createDownloadNotificationChannel(fileKey, fileKey)
|
|
|
|
} else {
|
|
|
|
// If earlier version channel ID is not used
|
|
|
|
// https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context)
|
|
|
|
""
|
|
|
|
};
|
|
|
|
val newNotification = NotificationCompat.Builder(applicationContext, channelId)
|
|
|
|
.setOngoing(true)
|
|
|
|
.setContentTitle("Downloading")//todo: translate
|
|
|
|
.setContentText(title)
|
|
|
|
.setSmallIcon(android.R.drawable.stat_sys_download)
|
|
|
|
.setProgress(progressMax, progress, false)
|
|
|
|
.setSound(null)
|
|
|
|
//.setSilent(true)
|
|
|
|
.build();
|
|
|
|
notificationManager.notify(dlID, newNotification);
|
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.i("FlwtchWorker->FileDownloadProgressUpdate", e.toString() + " :: " + e.getStackTrace());
|
|
|
|
}
|
2021-09-27 19:53:21 +00:00
|
|
|
} else if (evt.EventType == "FileDownloaded") {
|
|
|
|
Log.i("FlwtchWorker", "file downloaded!");
|
|
|
|
val data = JSONObject(evt.Data);
|
|
|
|
val tempFile = data.getString("TempFile");
|
2021-09-30 00:16:00 +00:00
|
|
|
val fileKey = data.getString("FileKey");
|
2021-09-27 19:53:21 +00:00
|
|
|
if (tempFile != "") {
|
|
|
|
val filePath = data.getString("FilePath");
|
|
|
|
Log.i("FlwtchWorker", "moving "+tempFile+" to "+filePath);
|
|
|
|
val sourcePath = Paths.get(tempFile);
|
|
|
|
val targetUri = Uri.parse(filePath);
|
|
|
|
val os = this.applicationContext.getContentResolver().openOutputStream(targetUri);
|
|
|
|
val bytesWritten = Files.copy(sourcePath, os);
|
|
|
|
Log.i("FlwtchWorker", "copied " + bytesWritten.toString() + " bytes");
|
|
|
|
if (bytesWritten != 0L) {
|
|
|
|
os?.flush();
|
|
|
|
os?.close();
|
|
|
|
Files.delete(sourcePath);
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 00:16:00 +00:00
|
|
|
if (downloadIDs.containsKey(fileKey)) {
|
|
|
|
notificationManager.cancel(downloadIDs.get(fileKey)?:0);
|
|
|
|
}
|
2021-06-24 23:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Intent().also { intent ->
|
|
|
|
intent.action = "im.cwtch.flwtch.broadcast.SERVICE_EVENT_BUS"
|
|
|
|
intent.putExtra("EventType", evt.EventType)
|
|
|
|
intent.putExtra("Data", evt.Data)
|
|
|
|
intent.putExtra("EventID", evt.EventID)
|
|
|
|
LocalBroadcastManager.getInstance(applicationContext).sendBroadcast(intent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"ReconnectCwtchForeground" -> {
|
|
|
|
Cwtch.reconnectCwtchForeground()
|
|
|
|
}
|
|
|
|
"CreateProfile" -> {
|
|
|
|
val nick = (a.get("nick") as? String) ?: ""
|
|
|
|
val pass = (a.get("pass") as? String) ?: ""
|
|
|
|
Cwtch.createProfile(nick, pass)
|
|
|
|
}
|
|
|
|
"LoadProfiles" -> {
|
|
|
|
val pass = (a.get("pass") as? String) ?: ""
|
|
|
|
Cwtch.loadProfiles(pass)
|
|
|
|
}
|
|
|
|
"GetMessage" -> {
|
|
|
|
val profile = (a.get("profile") as? String) ?: ""
|
|
|
|
val handle = (a.get("contact") as? String) ?: ""
|
|
|
|
val indexI = a.getInt("index")
|
|
|
|
return Result.success(Data.Builder().putString("result", Cwtch.getMessage(profile, handle, indexI.toLong())).build())
|
|
|
|
}
|
2021-07-05 19:31:16 +00:00
|
|
|
"GetMessageByContentHash" -> {
|
|
|
|
val profile = (a.get("profile") as? String) ?: ""
|
|
|
|
val handle = (a.get("contact") as? String) ?: ""
|
|
|
|
val contentHash = (a.get("contentHash") as? String) ?: ""
|
2021-07-07 17:05:25 +00:00
|
|
|
return Result.success(Data.Builder().putString("result", Cwtch.getMessagesByContentHash(profile, handle, contentHash)).build())
|
2021-07-05 19:31:16 +00:00
|
|
|
}
|
2021-06-24 23:10:45 +00:00
|
|
|
"UpdateMessageFlags" -> {
|
|
|
|
val profile = (a.get("profile") as? String) ?: ""
|
|
|
|
val handle = (a.get("contact") as? String) ?: ""
|
|
|
|
val midx = (a.get("midx") as? Long) ?: 0
|
|
|
|
val flags = (a.get("flags") as? Long) ?: 0
|
|
|
|
Cwtch.updateMessageFlags(profile, handle, midx, flags)
|
|
|
|
}
|
|
|
|
"AcceptContact" -> {
|
|
|
|
val profile = (a.get("ProfileOnion") as? String) ?: ""
|
|
|
|
val handle = (a.get("handle") as? String) ?: ""
|
|
|
|
Cwtch.acceptContact(profile, handle)
|
|
|
|
}
|
|
|
|
"BlockContact" -> {
|
|
|
|
val profile = (a.get("ProfileOnion") as? String) ?: ""
|
|
|
|
val handle = (a.get("handle") as? String) ?: ""
|
|
|
|
Cwtch.blockContact(profile, handle)
|
|
|
|
}
|
|
|
|
"SendMessage" -> {
|
|
|
|
val profile = (a.get("ProfileOnion") as? String) ?: ""
|
|
|
|
val handle = (a.get("handle") as? String) ?: ""
|
|
|
|
val message = (a.get("message") as? String) ?: ""
|
|
|
|
Cwtch.sendMessage(profile, handle, message)
|
|
|
|
}
|
|
|
|
"SendInvitation" -> {
|
|
|
|
val profile = (a.get("ProfileOnion") as? String) ?: ""
|
|
|
|
val handle = (a.get("handle") as? String) ?: ""
|
|
|
|
val target = (a.get("target") as? String) ?: ""
|
|
|
|
Cwtch.sendInvitation(profile, handle, target)
|
|
|
|
}
|
2021-09-27 19:53:21 +00:00
|
|
|
"ShareFile" -> {
|
|
|
|
val profile = (a.get("ProfileOnion") as? String) ?: ""
|
|
|
|
val handle = (a.get("handle") as? String) ?: ""
|
|
|
|
val filepath = (a.get("filepath") as? String) ?: ""
|
|
|
|
Cwtch.shareFile(profile, handle, filepath)
|
|
|
|
}
|
|
|
|
"DownloadFile" -> {
|
|
|
|
val profile = (a.get("ProfileOnion") as? String) ?: ""
|
|
|
|
val handle = (a.get("handle") as? String) ?: ""
|
|
|
|
val filepath = (a.get("filepath") as? String) ?: ""
|
|
|
|
val manifestpath = (a.get("manifestpath") as? String) ?: ""
|
|
|
|
val filekey = (a.get("filekey") as? String) ?: ""
|
|
|
|
Log.i("FlwtchWorker::DownloadFile", "DownloadFile("+filepath+", "+manifestpath+")")
|
|
|
|
Cwtch.downloadFile(profile, handle, filepath, manifestpath, filekey)
|
|
|
|
}
|
2021-09-29 20:31:01 +00:00
|
|
|
"CheckDownloadStatus" -> {
|
|
|
|
val profile = (a.get("ProfileOnion") as? String) ?: ""
|
|
|
|
val fileKey = (a.get("fileKey") as? String) ?: ""
|
|
|
|
Cwtch.checkDownloadStatus(profile, fileKey)
|
|
|
|
}
|
2021-06-24 23:10:45 +00:00
|
|
|
"SendProfileEvent" -> {
|
|
|
|
val onion = (a.get("onion") as? String) ?: ""
|
|
|
|
val jsonEvent = (a.get("jsonEvent") as? String) ?: ""
|
|
|
|
Cwtch.sendProfileEvent(onion, jsonEvent)
|
|
|
|
}
|
|
|
|
"SendAppEvent" -> {
|
|
|
|
val jsonEvent = (a.get("jsonEvent") as? String) ?: ""
|
|
|
|
Cwtch.sendAppEvent(jsonEvent)
|
|
|
|
}
|
|
|
|
"ResetTor" -> {
|
|
|
|
Cwtch.resetTor()
|
|
|
|
}
|
|
|
|
"ImportBundle" -> {
|
|
|
|
val profile = (a.get("ProfileOnion") as? String) ?: ""
|
|
|
|
val bundle = (a.get("bundle") as? String) ?: ""
|
|
|
|
Cwtch.importBundle(profile, bundle)
|
|
|
|
}
|
|
|
|
"SetGroupAttribute" -> {
|
|
|
|
val profile = (a.get("ProfileOnion") as? String) ?: ""
|
|
|
|
val groupHandle = (a.get("groupHandle") as? String) ?: ""
|
|
|
|
val key = (a.get("key") as? String) ?: ""
|
|
|
|
val value = (a.get("value") as? String) ?: ""
|
|
|
|
Cwtch.setGroupAttribute(profile, groupHandle, key, value)
|
|
|
|
}
|
|
|
|
"CreateGroup" -> {
|
|
|
|
val profile = (a.get("ProfileOnion") as? String) ?: ""
|
|
|
|
val server = (a.get("server") as? String) ?: ""
|
2021-06-30 00:17:42 +00:00
|
|
|
val groupName = (a.get("groupName") as? String) ?: ""
|
2021-06-24 23:10:45 +00:00
|
|
|
Cwtch.createGroup(profile, server, groupName)
|
|
|
|
}
|
|
|
|
"DeleteProfile" -> {
|
|
|
|
val profile = (a.get("ProfileOnion") as? String) ?: ""
|
|
|
|
val pass = (a.get("pass") as? String) ?: ""
|
|
|
|
Cwtch.deleteProfile(profile, pass)
|
|
|
|
}
|
2021-08-27 20:46:13 +00:00
|
|
|
"ArchiveConversation" -> {
|
2021-06-24 23:10:45 +00:00
|
|
|
val profile = (a.get("ProfileOnion") as? String) ?: ""
|
2021-08-27 20:46:13 +00:00
|
|
|
val contactHandle = (a.get("handle") as? String) ?: ""
|
|
|
|
Cwtch.archiveConversation(profile, contactHandle)
|
2021-06-24 23:10:45 +00:00
|
|
|
}
|
2021-08-27 20:46:13 +00:00
|
|
|
"DeleteContact" -> {
|
2021-06-24 23:10:45 +00:00
|
|
|
val profile = (a.get("ProfileOnion") as? String) ?: ""
|
2021-08-27 20:46:13 +00:00
|
|
|
val handle = (a.get("handle") as? String) ?: ""
|
2021-08-27 21:25:08 +00:00
|
|
|
Cwtch.deleteContact(profile, handle)
|
2021-06-24 23:10:45 +00:00
|
|
|
}
|
|
|
|
"RejectInvite" -> {
|
|
|
|
val profile = (a.get("ProfileOnion") as? String) ?: ""
|
|
|
|
val groupHandle = (a.get("groupHandle") as? String) ?: ""
|
|
|
|
Cwtch.rejectInvite(profile, groupHandle)
|
|
|
|
}
|
|
|
|
"Shutdown" -> {
|
|
|
|
Cwtch.shutdownCwtch();
|
|
|
|
return Result.success()
|
|
|
|
}
|
|
|
|
else -> return Result.failure()
|
|
|
|
}
|
|
|
|
return Result.success()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates an instance of ForegroundInfo which can be used to update the
|
|
|
|
// ongoing notification.
|
|
|
|
private fun createForegroundInfo(progress: String): ForegroundInfo {
|
|
|
|
val id = "flwtch"
|
|
|
|
val title = "Flwtch"
|
|
|
|
val cancel = "Shut down"//todo: translate
|
|
|
|
val channelId =
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
createForegroundNotificationChannel(id, id)
|
|
|
|
} else {
|
|
|
|
// If earlier version channel ID is not used
|
|
|
|
// https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context)
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
2021-06-25 00:59:54 +00:00
|
|
|
val cancelIntent = Intent(applicationContext, MainActivity::class.java).also { intent ->
|
|
|
|
intent.action = Intent.ACTION_RUN
|
|
|
|
intent.putExtra("EventType", "ShutdownClicked")
|
|
|
|
}
|
2021-06-24 23:10:45 +00:00
|
|
|
|
|
|
|
val notification = NotificationCompat.Builder(applicationContext, channelId)
|
|
|
|
.setContentTitle(title)
|
|
|
|
.setTicker(title)
|
|
|
|
.setContentText(progress)
|
2021-07-15 20:03:57 +00:00
|
|
|
.setSmallIcon(R.mipmap.knott_transparent)
|
2021-06-24 23:10:45 +00:00
|
|
|
.setOngoing(true)
|
|
|
|
// Add the cancel action to the notification which can
|
|
|
|
// be used to cancel the worker
|
2021-07-02 23:27:09 +00:00
|
|
|
.addAction(android.R.drawable.ic_delete, cancel, PendingIntent.getActivity(applicationContext, 2, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT))
|
2021-06-24 23:10:45 +00:00
|
|
|
.build()
|
|
|
|
|
|
|
|
return ForegroundInfo(101, notification)
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequiresApi(Build.VERSION_CODES.O)
|
|
|
|
private fun createForegroundNotificationChannel(channelId: String, channelName: String): String{
|
|
|
|
val chan = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE)
|
|
|
|
chan.lightColor = Color.MAGENTA
|
|
|
|
chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
|
|
|
|
notificationManager.createNotificationChannel(chan)
|
|
|
|
return channelId
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequiresApi(Build.VERSION_CODES.O)
|
|
|
|
private fun createMessageNotificationChannel(channelId: String, channelName: String): String{
|
|
|
|
val chan = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH)
|
|
|
|
chan.lightColor = Color.MAGENTA
|
|
|
|
chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
|
|
|
|
notificationManager.createNotificationChannel(chan)
|
|
|
|
return channelId
|
|
|
|
}
|
|
|
|
|
2021-09-30 00:16:00 +00:00
|
|
|
@RequiresApi(Build.VERSION_CODES.O)
|
|
|
|
private fun createDownloadNotificationChannel(channelId: String, channelName: String): String{
|
|
|
|
val chan = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW)
|
|
|
|
chan.lightColor = Color.MAGENTA
|
|
|
|
chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
|
|
|
|
notificationManager.createNotificationChannel(chan)
|
|
|
|
return channelId
|
|
|
|
}
|
|
|
|
|
2021-06-24 23:10:45 +00:00
|
|
|
companion object {
|
|
|
|
const val KEY_METHOD = "KEY_METHOD"
|
|
|
|
const val KEY_ARGS = "KEY_ARGS"
|
|
|
|
}
|
|
|
|
}
|