Skip to content

Commit 27e9ba1

Browse files
authored
Merge pull request #3 from JunkFood02/dev
Match & paste for url in clipboard
2 parents c5d3406 + 7c5b166 commit 27e9ba1

32 files changed

+101
-52
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
def versionMajor = 0
77
def versionMinor = 0
88
def versionPatch = 3
9-
def versionBuild = 1
9+
def versionBuild = 2
1010

1111
android {
1212
compileSdk 32

app/src/main/java/com/junkfood/seal/BaseApplication.kt

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.junkfood.seal
22

33
import android.annotation.SuppressLint
44
import android.app.Application
5+
import android.content.ClipboardManager
56
import android.content.Context
67
import android.os.Build
78
import android.os.Environment
@@ -26,6 +27,7 @@ class BaseApplication : Application() {
2627
super.onCreate()
2728

2829
DynamicColors.applyToActivitiesIfAvailable(this)
30+
clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
2931
try {
3032
YoutubeDL.getInstance().init(this)
3133
FFmpeg.getInstance().init(this)
@@ -63,13 +65,9 @@ class BaseApplication : Application() {
6365
}
6466

6567

66-
fun printErrorLog(th: Throwable) {
67-
68-
}
69-
70-
7168
companion object {
7269
private const val TAG = "BaseApplication"
70+
lateinit var clipboard: ClipboardManager
7371
lateinit var downloadDir: String
7472
fun updateDownloadDir() {
7573
downloadDir = File(
@@ -79,25 +77,26 @@ class BaseApplication : Application() {
7977
}
8078

8179
fun createLogFileOnDevice(th: Throwable) {
82-
with(context.getExternalFilesDir(null)){
83-
if (this?.canWrite() == true) {
84-
val timeNow: String =
85-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
86-
LocalDateTime.now()
87-
.format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"))
88-
} else {
89-
SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(Date())
80+
with(context.getExternalFilesDir(null)) {
81+
if (this?.canWrite() == true) {
82+
val timeNow: String =
83+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
84+
LocalDateTime.now()
85+
.format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"))
86+
} else {
87+
SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(Date())
88+
}
89+
with(File(this, "log$timeNow.txt")) {
90+
if (!exists())
91+
createNewFile()
92+
val logWriter = FileWriter(this, true)
93+
val out = BufferedWriter(logWriter)
94+
out.append(th.stackTraceToString())
95+
out.close()
9096
}
91-
with(File(this, "log$timeNow.txt")) {
92-
if (!exists())
93-
createNewFile()
94-
val logWriter = FileWriter(this, true)
95-
val out = BufferedWriter(logWriter)
96-
out.append(th.stackTraceToString())
97-
out.close()
9897
}
9998
}
100-
}}
99+
}
101100

102101
@SuppressLint("StaticFieldLeak")
103102
lateinit var context: Context

app/src/main/java/com/junkfood/seal/ui/home/HomeFragment.kt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.junkfood.seal.ui.home
22

33
import android.Manifest
4+
import android.content.ClipDescription
45
import android.content.Intent
56
import android.os.*
67
import android.text.Editable
@@ -21,6 +22,7 @@ import com.junkfood.seal.R
2122
import com.junkfood.seal.databinding.FragmentHomeBinding
2223
import com.junkfood.seal.util.DownloadUtil
2324
import java.io.File
25+
import java.util.regex.Pattern
2426

2527

2628
class HomeFragment : Fragment() {
@@ -110,15 +112,40 @@ class HomeFragment : Fragment() {
110112
}
111113
with(binding) {
112114
inputTextUrl.editText?.setText(homeViewModel.url.value)
113-
114115
inputTextUrl.editText?.addTextChangedListener(object : TextWatcher {
115116
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
116117
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
117118
override fun afterTextChanged(p0: Editable?) {
118119
homeViewModel.url.value = p0.toString()
119120
}
120121
})
121-
122+
pasteButton.setOnClickListener {
123+
if (BaseApplication.clipboard.hasPrimaryClip()) {
124+
if (BaseApplication.clipboard.primaryClipDescription?.hasMimeType(
125+
ClipDescription.MIMETYPE_TEXT_PLAIN
126+
) == true
127+
) {
128+
val item = BaseApplication.clipboard.primaryClip?.getItemAt(0)?.text
129+
?: return@setOnClickListener
130+
val pattern =
131+
Pattern.compile("(http|https)://[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-.,@?^=%&:/~+#]*[\\w\\-@?^=%&/~+#])?")
132+
with(pattern.matcher(item)) {
133+
if (find()) {
134+
inputTextUrl.editText?.setText(group())
135+
Toast.makeText(
136+
context,
137+
getString(R.string.paste_msg),
138+
Toast.LENGTH_SHORT
139+
)
140+
.show()
141+
return@setOnClickListener
142+
}
143+
}
144+
}
145+
}
146+
Toast.makeText(context, getString(R.string.paste_fail_msg), Toast.LENGTH_SHORT)
147+
.show()
148+
}
122149
downloadButton.setOnClickListener {
123150
activityResultLauncher.launch(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE))
124151
}
211 Bytes
Loading
199 Bytes
Loading
215 Bytes
Loading
299 Bytes
Loading
323 Bytes
Loading
154 Bytes
Loading
179 Bytes
Loading

0 commit comments

Comments
 (0)