|
| 1 | +/* |
| 2 | + * Léon - The URL Cleaner |
| 3 | + * Copyright (C) 2024 Sven Jacobs |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.svenjacobs.app.leon |
| 20 | + |
| 21 | +import android.content.Intent |
| 22 | +import android.os.Build |
| 23 | +import android.os.Bundle |
| 24 | +import androidx.activity.ComponentActivity |
| 25 | +import androidx.annotation.RequiresApi |
| 26 | +import com.svenjacobs.app.leon.core.domain.CleanerService |
| 27 | +import kotlinx.coroutines.runBlocking |
| 28 | + |
| 29 | +@RequiresApi(Build.VERSION_CODES.M) |
| 30 | +class ProcessTextActivity : ComponentActivity() { |
| 31 | + |
| 32 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 33 | + super.onCreate(savedInstanceState) |
| 34 | + |
| 35 | + val text = intent.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT)?.toString() |
| 36 | + val readonly = intent.getBooleanExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, false) |
| 37 | + |
| 38 | + when { |
| 39 | + // If readonly, delegate to MainActivity |
| 40 | + readonly -> startActivity( |
| 41 | + Intent(this, MainActivity::class.java).apply { |
| 42 | + action = Intent.ACTION_SEND |
| 43 | + type = "text/plain" |
| 44 | + putExtra(Intent.EXTRA_TEXT, text) |
| 45 | + }, |
| 46 | + ) |
| 47 | + |
| 48 | + text.isNullOrBlank() -> setResult(RESULT_CANCELED) |
| 49 | + |
| 50 | + // Needs to run with runBlocking or else setResult() won't work |
| 51 | + else -> runBlocking { |
| 52 | + val result = CleanerService().clean(text) |
| 53 | + |
| 54 | + setResult( |
| 55 | + RESULT_OK, |
| 56 | + Intent().apply { |
| 57 | + putExtra(Intent.EXTRA_PROCESS_TEXT, result.cleanedText) |
| 58 | + }, |
| 59 | + ) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + finish() |
| 64 | + } |
| 65 | +} |
0 commit comments