@@ -4,6 +4,8 @@ import android.annotation.SuppressLint
44import android.app.Notification
55import android.app.PendingIntent
66import android.content.BroadcastReceiver
7+ import android.content.ClipData
8+ import android.content.ClipboardManager
79import android.content.ContentValues
810import android.content.Context
911import android.content.Intent
@@ -17,11 +19,14 @@ import android.net.wifi.p2p.WifiP2pGroup
1719import android.net.wifi.p2p.WifiP2pInfo
1820import android.net.wifi.p2p.WifiP2pManager
1921import android.os.Environment
22+ import android.os.Handler
2023import android.os.IBinder
24+ import android.os.Looper
2125import android.provider.MediaStore
2226import android.text.format.Formatter
2327import android.util.Log
2428import android.webkit.MimeTypeMap
29+ import android.widget.Toast
2530import androidx.annotation.DrawableRes
2631import androidx.core.app.NotificationCompat
2732import androidx.core.app.NotificationManagerCompat
@@ -43,6 +48,7 @@ import kotlinx.coroutines.Job
4348import kotlinx.coroutines.SupervisorJob
4449import kotlinx.coroutines.async
4550import kotlinx.coroutines.coroutineScope
51+ import kotlinx.coroutines.delay
4652import kotlinx.coroutines.launch
4753import kotlinx.coroutines.selects.select
4854import kotlinx.coroutines.suspendCancellableCoroutine
@@ -66,6 +72,7 @@ import moe.reimu.catshare.utils.connectSuspend
6672import moe.reimu.catshare.utils.registerInternalBroadcastReceiver
6773import moe.reimu.catshare.utils.removeGroupSuspend
6874import moe.reimu.catshare.utils.requestGroupInfo
75+ import moe.reimu.catshare.utils.sendStatusIgnoreException
6976import okhttp3.ConnectionPool
7077import org.json.JSONObject
7178import java.io.File
@@ -223,12 +230,17 @@ class P2pReceiverService : BaseP2pService() {
223230 fileName : String ,
224231 fileCount : Int ,
225232 totalSize : Long ,
226- thumbnail : Bitmap ?
233+ thumbnail : Bitmap ? ,
234+ textContent : String?
227235 ): Notification {
228236 val fmtSize = Formatter .formatShortFileSize(this , totalSize)
229- val contentText = resources.getQuantityString(
230- R .plurals.noti_request_desc, fileCount, fileCount, fmtSize
231- )
237+ val contentText = if (textContent == null ) {
238+ resources.getQuantityString(
239+ R .plurals.noti_request_desc, fileCount, fileCount, fmtSize
240+ )
241+ } else {
242+ resources.getString(R .string.noti_request_desc_text)
243+ }
232244
233245 val dismissIntent = PendingIntent .getBroadcast(
234246 this ,
@@ -253,6 +265,9 @@ class P2pReceiverService : BaseP2pService() {
253265 if (thumbnail != null ) {
254266 n.setStyle(NotificationCompat .BigPictureStyle ().bigPicture(thumbnail))
255267 }
268+ if (textContent != null ) {
269+ n.setStyle(NotificationCompat .BigTextStyle ().bigText(textContent))
270+ }
256271
257272 return n.build()
258273 }
@@ -375,8 +390,10 @@ class P2pReceiverService : BaseP2pService() {
375390 }
376391 }
377392
378- val p2pConfig =
379- WifiP2pConfig .Builder ().setNetworkName(p2pInfo.ssid).setPassphrase(p2pInfo.psk).build()
393+ val p2pConfig = WifiP2pConfig .Builder ()
394+ .setNetworkName(p2pInfo.ssid)
395+ .setPassphrase(p2pInfo.psk)
396+ .build()
380397
381398 try {
382399 p2pFuture = CompletableDeferred ()
@@ -411,6 +428,11 @@ class P2pReceiverService : BaseP2pService() {
411428 val fileName = sendRequestPayload.getString(" fileName" )
412429 val totalSize = sendRequestPayload.getLong(" totalSize" )
413430 val fileCount = sendRequestPayload.getInt(" fileCount" )
431+ val textContent = if (sendRequestPayload.has(" catShareText" )) {
432+ sendRequestPayload.getString(" catShareText" )
433+ } else {
434+ null
435+ }
414436
415437 val thumbPath = sendRequestPayload.optString(" thumbnail" )
416438 val bigPicture = if (thumbPath.isNotEmpty()) {
@@ -423,7 +445,13 @@ class P2pReceiverService : BaseP2pService() {
423445
424446 updateNotification(
425447 createAskingNotification(
426- localTaskId, senderName, fileName, fileCount, totalSize, bigPicture
448+ localTaskId,
449+ senderName,
450+ fileName,
451+ fileCount,
452+ totalSize,
453+ bigPicture,
454+ textContent
427455 )
428456 )
429457
@@ -432,20 +460,26 @@ class P2pReceiverService : BaseP2pService() {
432460 }
433461
434462 if (userResponse != true ) {
435- wsSession.send(
436- Frame .Text (
437- WebSocketMessage .makeStatus(
438- 99 ,
439- taskId,
440- 3 ,
441- " user refuse"
442- ).toText()
443- )
463+ wsSession.sendStatusIgnoreException(
464+ 99 ,
465+ taskId,
466+ 3 ,
467+ " user refuse"
444468 )
445- wsSession.flush()
446469 throw CancelledByUserException ()
447470 }
448471
472+ if (textContent != null ) {
473+ val cm = getSystemService(ClipboardManager ::class .java)
474+ cm.setPrimaryClip(ClipData .newPlainText(" Shared Text" , textContent))
475+
476+ showTextCopiedToast()
477+
478+ wsSession.sendStatusIgnoreException(99 , taskId, 1 , " ok" )
479+ delay(1000 )
480+ return @async
481+ }
482+
449483 updateNotification(
450484 createProgressNotification(
451485 localTaskId, senderName, totalSize, null
@@ -479,14 +513,8 @@ class P2pReceiverService : BaseP2pService() {
479513 senderName, files, files.size != fileCount
480514 )
481515 )
482-
483- try {
484- val st = WebSocketMessage .makeStatus(99 , taskId, 1 , " " )
485- wsSession.send(Frame .Text (st.toText()))
486- wsSession.flush()
487- } catch (e: Throwable ) {
488- // ignore
489- }
516+ wsSession.sendStatusIgnoreException(99 , taskId, 1 , " ok" )
517+ delay(1000 )
490518 } else {
491519 throw IllegalStateException (" Failed to receive any file" )
492520 }
@@ -673,6 +701,16 @@ class P2pReceiverService : BaseP2pService() {
673701 }
674702 }
675703
704+ private fun showTextCopiedToast () {
705+ Handler (Looper .getMainLooper()).post {
706+ Toast .makeText(
707+ this @P2pReceiverService,
708+ R .string.msg_copied_to_clipboard,
709+ Toast .LENGTH_SHORT
710+ ).show()
711+ }
712+ }
713+
676714 companion object {
677715 fun getIntent (context : Context , p2pInfo : P2pInfo ): Intent {
678716 return Intent (context, P2pReceiverService ::class .java).apply {
0 commit comments