First MVP

This commit is contained in:
Jonas Zeunert
2024-05-13 11:50:50 +02:00
parent 8335829ef9
commit 5600a0d4d9
2 changed files with 61 additions and 24 deletions

View File

@@ -17,57 +17,77 @@ import org.rhetenor.clipcleaner.ui.theme.ClipCleanerTheme
import android.net.Uri
class MainActivity : ComponentActivity() {
private var trackerParameterNames: Set<String> = emptySet()
private fun populateTrackerList() {
this.trackerParameterNames = this.resources?.getStringArray(R.array.tracker_parameter_names)!!.toSet()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
populateTrackerList()
enableEdgeToEdge()
setContent {
ClipCleanerTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Greeting(
name = "Android",
modifier = Modifier.padding(innerPadding)
name = "Android",
modifier = Modifier.padding(innerPadding)
)
}
}
}
this.intent?.let {
when (it.action) {
Intent.ACTION_SEND -> {
if (it.type == "text/plain") {
// it.get
val cleanedIntent = cleanIntent(it)
startActivity(cleanedIntent)
}
this.intent?.let {intent ->
handleIntent(intent)
}
}
private fun handleIntent(intent: Intent) {
when (intent.action) {
Intent.ACTION_SEND -> {
if (intent.type == "text/plain") {
val cleanedIntent = cleanIntent(intent)
startActivity(cleanedIntent)
}
}
}
}
}
fun cleanIntent(intent: Intent): Intent {
intent.setComponent(null)
private fun cleanIntent(intent: Intent): Intent {
intent.setComponent(null)
intent.clipData?.also {data ->
var item = data.getItemAt(0)
Uri.parse(item.toString()).also { uri ->
val cleaned = removeTracker(uri)
intent.clipData = ClipData.newPlainText(data.description.label, cleaned)
intent.clipData?.also {data ->
val item = data.getItemAt(0) // todo error handling
Uri.parse(item.text.toString()).also { uri -> // todo error handling
val cleanedData = removeTracker(uri)
val cleanedIntent = Intent(Intent.ACTION_SEND)
cleanedIntent.setType("text/plain")
cleanedIntent.clipData = ClipData.newPlainText(data.description.label, cleanedData)
cleanedIntent.putExtra(Intent.EXTRA_TEXT, cleanedData)
return cleanedIntent
}
}
return intent
}
return intent
}
private fun removeTracker(uri: Uri): String {
val trackers = uri.queryParameterNames intersect this.trackerParameterNames
fun removeTracker(uri: Uri): String {
uri.getQueryParameter("si").also {
val filteredQuery = uri.query?.split("&")?.filter { q ->
val parameterName = q.split("=").first()
parameterName !in trackers
}?.joinToString("&")!! // ToDo Error handling
return uri.toString().replace(Regex("\\?.*#?"), filteredQuery)
}
return "asdf"
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(

View File

@@ -0,0 +1,17 @@
package org.rhetenor.clipcleaner
import android.content.ClipData
import android.content.Intent
import org.junit.Test
import org.junit.Assert.*
/**
* Android. Damn stubbing *rolleyes*
*/
class CleanTest {
@Test
fun cleaning_correct() {
}
}