|
| 1 | +package org.telegram.messenger.forkgram |
| 2 | + |
| 3 | +import android.content.ComponentName |
| 4 | +import android.content.Context |
| 5 | +import android.content.Intent |
| 6 | +import android.content.ServiceConnection |
| 7 | +import android.os.IBinder |
| 8 | +import android.os.Looper |
| 9 | +import dev.davidv.translator.ITranslationCallback |
| 10 | +import dev.davidv.translator.ITranslationService |
| 11 | +import dev.davidv.translator.TranslationError |
| 12 | +import org.telegram.messenger.ApplicationLoader |
| 13 | +import org.telegram.messenger.MessagesController |
| 14 | +import java.util.concurrent.CountDownLatch |
| 15 | +import java.util.concurrent.TimeUnit |
| 16 | +import java.util.concurrent.atomic.AtomicReference |
| 17 | + |
| 18 | +object ForkOfflineTranslate { |
| 19 | + |
| 20 | + const val PACKAGE = "dev.davidv.translator" |
| 21 | + const val FDROID_URL = "https://f-droid.org/packages/$PACKAGE/" |
| 22 | + private const val ACTION = "dev.davidv.translator.ITranslationService" |
| 23 | + private const val BIND_TIMEOUT_MS = 5000L |
| 24 | + private const val CALL_TIMEOUT_MS = 60000L |
| 25 | + |
| 26 | + const val PROVIDER_DEFAULT = 0 |
| 27 | + const val PROVIDER_ALTERNATIVE = 1 |
| 28 | + const val PROVIDER_OFFLINE = 2 |
| 29 | + |
| 30 | + @JvmStatic |
| 31 | + fun provider(): Int = |
| 32 | + MessagesController.getGlobalMainSettings().getInt("translationProvider", PROVIDER_DEFAULT) |
| 33 | + |
| 34 | + @JvmStatic |
| 35 | + fun method(serverMethod: String?): String? = when (provider()) { |
| 36 | + PROVIDER_ALTERNATIVE -> "alternative" |
| 37 | + PROVIDER_OFFLINE -> "offline" |
| 38 | + else -> serverMethod |
| 39 | + } |
| 40 | + |
| 41 | + private val bindLock = Any() |
| 42 | + @Volatile private var service: ITranslationService? = null |
| 43 | + private var connection: ServiceConnection? = null |
| 44 | + private var bindLatch: CountDownLatch? = null |
| 45 | + |
| 46 | + @JvmStatic |
| 47 | + fun isAvailable(): Boolean { |
| 48 | + val context = ApplicationLoader.applicationContext ?: return false |
| 49 | + val intent = Intent(ACTION).setPackage(PACKAGE) |
| 50 | + return context.packageManager.resolveService(intent, 0) != null |
| 51 | + } |
| 52 | + |
| 53 | + @JvmStatic |
| 54 | + fun translate(text: String, fromLanguage: String?, toLanguage: String): String? { |
| 55 | + if (Looper.myLooper() == Looper.getMainLooper()) { |
| 56 | + return null |
| 57 | + } |
| 58 | + val bound = ensureService() ?: return null |
| 59 | + val resultRef = AtomicReference<String?>(null) |
| 60 | + val latch = CountDownLatch(1) |
| 61 | + val callback = object : ITranslationCallback.Stub() { |
| 62 | + override fun onTranslationResult(translatedText: String?) { |
| 63 | + resultRef.set(translatedText) |
| 64 | + latch.countDown() |
| 65 | + } |
| 66 | + |
| 67 | + override fun onTranslationError(error: TranslationError?) { |
| 68 | + latch.countDown() |
| 69 | + } |
| 70 | + } |
| 71 | + try { |
| 72 | + bound.translate(text, fromLanguage ?: "", toLanguage, callback) |
| 73 | + } catch (e: Exception) { |
| 74 | + service = null |
| 75 | + return null |
| 76 | + } |
| 77 | + latch.await(CALL_TIMEOUT_MS, TimeUnit.MILLISECONDS) |
| 78 | + return resultRef.get() |
| 79 | + } |
| 80 | + |
| 81 | + private fun ensureService(): ITranslationService? { |
| 82 | + service?.let { return it } |
| 83 | + val context = ApplicationLoader.applicationContext ?: return null |
| 84 | + synchronized(bindLock) { |
| 85 | + service?.let { return it } |
| 86 | + var latch = bindLatch |
| 87 | + if (connection == null) { |
| 88 | + latch = CountDownLatch(1) |
| 89 | + val awaiting = latch |
| 90 | + val conn = object : ServiceConnection { |
| 91 | + override fun onServiceConnected(name: ComponentName?, binder: IBinder?) { |
| 92 | + service = ITranslationService.Stub.asInterface(binder) |
| 93 | + awaiting.countDown() |
| 94 | + } |
| 95 | + |
| 96 | + override fun onServiceDisconnected(name: ComponentName?) { |
| 97 | + service = null |
| 98 | + } |
| 99 | + } |
| 100 | + val intent = Intent(ACTION).setPackage(PACKAGE) |
| 101 | + val ok = try { |
| 102 | + context.bindService(intent, conn, Context.BIND_AUTO_CREATE) |
| 103 | + } catch (e: Exception) { |
| 104 | + false |
| 105 | + } |
| 106 | + if (!ok) { |
| 107 | + try { |
| 108 | + context.unbindService(conn) |
| 109 | + } catch (ignore: Exception) { |
| 110 | + } |
| 111 | + return null |
| 112 | + } |
| 113 | + connection = conn |
| 114 | + bindLatch = latch |
| 115 | + } |
| 116 | + latch?.await(BIND_TIMEOUT_MS, TimeUnit.MILLISECONDS) |
| 117 | + return service |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments