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,8 +17,17 @@ import org.rhetenor.clipcleaner.ui.theme.ClipCleanerTheme
import android.net.Uri import android.net.Uri
class MainActivity : ComponentActivity() { 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?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
populateTrackerList()
enableEdgeToEdge() enableEdgeToEdge()
setContent { setContent {
ClipCleanerTheme { ClipCleanerTheme {
@@ -31,43 +40,54 @@ class MainActivity : ComponentActivity() {
} }
} }
this.intent?.let { this.intent?.let {intent ->
when (it.action) { handleIntent(intent)
}
}
private fun handleIntent(intent: Intent) {
when (intent.action) {
Intent.ACTION_SEND -> { Intent.ACTION_SEND -> {
if (it.type == "text/plain") { if (intent.type == "text/plain") {
// it.get val cleanedIntent = cleanIntent(intent)
val cleanedIntent = cleanIntent(it)
startActivity(cleanedIntent) startActivity(cleanedIntent)
} }
} }
} }
} }
}
}
fun cleanIntent(intent: Intent): Intent { private fun cleanIntent(intent: Intent): Intent {
intent.setComponent(null) intent.setComponent(null)
intent.clipData?.also {data -> intent.clipData?.also {data ->
var item = data.getItemAt(0) val item = data.getItemAt(0) // todo error handling
Uri.parse(item.toString()).also { uri -> Uri.parse(item.text.toString()).also { uri -> // todo error handling
val cleaned = removeTracker(uri) val cleanedData = removeTracker(uri)
intent.clipData = ClipData.newPlainText(data.description.label, cleaned) 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
}
fun removeTracker(uri: Uri): String {
uri.getQueryParameter("si").also {
} }
return "asdf"
private fun removeTracker(uri: Uri): String {
val trackers = uri.queryParameterNames intersect this.trackerParameterNames
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)
}
} }
@Composable @Composable
fun Greeting(name: String, modifier: Modifier = Modifier) { fun Greeting(name: String, modifier: Modifier = Modifier) {
Text( 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() {
}
}