Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

클립보드로 이동 추가 #154

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions app/src/main/java/xyz/quaver/pupil/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import android.view.View
import android.view.animation.DecelerateInterpolator
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.GravityCompat
import androidx.core.view.ViewCompat
Expand All @@ -59,6 +59,7 @@ import xyz.quaver.pupil.ui.dialog.DownloadLocationDialogFragment
import xyz.quaver.pupil.ui.dialog.GalleryDialog
import xyz.quaver.pupil.ui.view.MainView
import xyz.quaver.pupil.ui.view.ProgressCard
import xyz.quaver.pupil.util.ClipboardHelper
import xyz.quaver.pupil.util.ItemClickSupport
import xyz.quaver.pupil.util.Preferences
import xyz.quaver.pupil.util.checkUpdate
Expand Down Expand Up @@ -112,12 +113,16 @@ class MainActivity :

private lateinit var binding: MainActivityBinding

private val requestNotificationPermssionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
private val requestNotificationPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (!isGranted) {
showNotificationPermissionExplanationDialog(this)
}
}

private val clipboardHelper by lazy {
ClipboardHelper(this)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = MainActivityBinding.inflate(layoutInflater)
Expand All @@ -135,7 +140,7 @@ class MainActivity :
}
}

requestNotificationPermission(this, requestNotificationPermssionLauncher, false) {}
requestNotificationPermission(this, requestNotificationPermissionLauncher, false) {}

if (Preferences["download_folder", ""].isEmpty())
DownloadLocationDialogFragment().show(supportFragmentManager, "Download Location Dialog")
Expand Down Expand Up @@ -163,6 +168,11 @@ class MainActivity :
checkUpdate(this)
}

override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
clipboardHelper.updateFocus(focus = hasFocus)
}

@OptIn(ExperimentalStdlibApi::class)
override fun onBackPressed() {
when {
Expand Down Expand Up @@ -317,6 +327,13 @@ class MainActivity :
inputType = InputType.TYPE_CLASS_NUMBER
}

// 생성될때 clipboard에 복사된 값이 숫자 7자리인지 확인하여
// 7자리라면 세팅해줌
clipboardHelper.checkClipboardOnFocus { galleryId ->
editText.setText(galleryId.toString())
Toast.makeText(this@MainActivity,String.format(getString(R.string.dialog_find_message,galleryId)),Toast.LENGTH_SHORT).show()
}

AlertDialog.Builder(context).apply {
setView(editText)
setTitle(R.string.main_open_gallery_by_id)
Expand Down Expand Up @@ -407,7 +424,7 @@ class MainActivity :

requestNotificationPermission(
this@MainActivity,
requestNotificationPermssionLauncher
requestNotificationPermissionLauncher
) {
if (DownloadManager.getInstance(context).isDownloading(galleryID)) { //download in progress
DownloadService.cancel(this@MainActivity, galleryID)
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/java/xyz/quaver/pupil/util/ClipboardManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package xyz.quaver.pupil.util

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Build

class ClipboardHelper(
context: Context
) {
private val clipboardManager: ClipboardManager by lazy {
context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
}

private var isFocus: Boolean = false

// Focus를 체크하는 이유는 안드로이드10 이상에서 background에서 접근하려하면 에러나기때문
fun updateFocus(focus: Boolean) {
this.isFocus = focus
}

fun checkClipboardOnFocus(completion: (Int) -> Unit) {
if (isFocus) {
clipboardManager
.primaryClip
?.getItemAt(0)
?.text
?.toString()
?.let { clipData ->
if (clipData.length == 7) {
runCatching {
clipData.toInt()
}.onSuccess { galleryID ->
completion(galleryID)
}
}
}
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,5 @@
<string name="unaccessible_download_folder">アンドロイド11以上では外部からのアプリ内部空間接近が不可能です。ダウンロードフォルダを変更しますか?</string>
<string name="settings_networking">ネットワーク</string>
<string name="settings_recover_downloads">ダウンロードデータベースを再構築</string>
<string name="dialog_find_message">コピーされたID: %dを見つけました</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-ko/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,5 @@
<string name="unaccessible_download_folder">안드로이드 11 이상에서는 외부에서 현재 다운로드 폴더에 접근할 수 없습니다. 변경하시겠습니까?</string>
<string name="settings_networking">네트워크</string>
<string name="settings_recover_downloads">다운로드 데이터베이스 복구</string>
<string name="dialog_find_message">복사된 ID : %d을 찾았습니다</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,7 @@
<string name="import_old_galleries_notification_done">Importing completed</string>
<string name="settings_lock_fingerprint_prompt_subtitle">Ah Shit, Here we go again</string>

<!-- DIALOG FIND ID -->
<string name="dialog_find_message">Found copied ID: %d</string>

</resources>