-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: keep inserted text when going to fullscreen app from translation…
… dialog
- Loading branch information
Showing
5 changed files
with
117 additions
and
118 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
app/src/main/java/com/bnyro/translate/ui/TranslationActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright (c) 2024 You Apps | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.bnyro.translate.ui | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.os.Bundle | ||
import android.os.Parcelable | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.bnyro.translate.db.obj.Language | ||
import com.bnyro.translate.ext.hexToColor | ||
import com.bnyro.translate.ext.parcelable | ||
import com.bnyro.translate.ui.models.TranslationModel | ||
import com.bnyro.translate.ui.theme.TranslateYouTheme | ||
import com.bnyro.translate.util.ImageHelper | ||
import com.bnyro.translate.util.LocaleHelper | ||
import com.bnyro.translate.util.Preferences | ||
|
||
open class TranslationActivity: ComponentActivity() { | ||
lateinit var translationModel: TranslationModel | ||
var themeMode by mutableStateOf(Preferences.getThemeMode()) | ||
var accentColor by mutableStateOf(Preferences.getAccentColor()) | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
LocaleHelper.updateLanguage(this) | ||
|
||
translationModel = ViewModelProvider(this)[TranslationModel::class.java] | ||
handleIntentData() | ||
|
||
super.onCreate(savedInstanceState) | ||
} | ||
|
||
fun showContent(content: @Composable () -> Unit) { | ||
setContent { | ||
TranslateYouTheme(themeMode, accentColor?.hexToColor()) { | ||
content() | ||
} | ||
} | ||
} | ||
|
||
@SuppressLint("InlinedApi") | ||
private fun getIntentText(): String? { | ||
return intent.getCharSequenceExtra(Intent.EXTRA_TEXT)?.toString() | ||
?: intent.takeIf { Build.VERSION.SDK_INT > Build.VERSION_CODES.M } | ||
?.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT)?.toString() | ||
?: intent.getCharSequenceExtra(Intent.ACTION_SEND)?.toString() | ||
} | ||
|
||
override fun onNewIntent(intent: Intent?) { | ||
super.onNewIntent(intent) | ||
this.intent = intent | ||
handleIntentData() | ||
} | ||
|
||
private fun handleIntentData() { | ||
getIntentText()?.let { | ||
translationModel.insertedText = it | ||
translationModel.translateNow(this) | ||
} | ||
// open links from Google Translate | ||
if (intent.data?.host == "translate.google.com") { | ||
val source = intent.data?.getQueryParameter("sl").orEmpty() | ||
val target = intent.data?.getQueryParameter("tl").orEmpty() | ||
translationModel.sourceLanguage = Language(source, source) | ||
translationModel.targetLanguage = Language(target, target) | ||
translationModel.insertedText = intent.data?.getQueryParameter("text").orEmpty() | ||
translationModel.translateNow(this) | ||
} | ||
if (intent.type?.startsWith("image/") != true) return | ||
|
||
(intent.parcelable<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let { | ||
ImageHelper.getImage(this, it)?.let { bm -> | ||
translationModel.processImage(this, bm) | ||
} | ||
} | ||
} | ||
|
||
override fun onStop() { | ||
translationModel.saveSelectedLanguages() | ||
super.onStop() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters