Skip to content

Commit

Permalink
Fix copy address button
Browse files Browse the repository at this point in the history
  • Loading branch information
greenart7c3 committed Jun 26, 2024
1 parent b67581d commit e82d0a1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {
minSdk = 26
targetSdk = 34
versionCode = 23
versionName = "0.2.8"
versionName = "0.2.9"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
15 changes: 12 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,35 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<queries>
<package android:name="org.torproject.android"/>
<package android:name="org.torproject.android" />

<intent>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="nostrsigner" />
</intent>
</queries>

<application
android:requestLegacyExternalStorage="true"
android:allowBackup="false"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:localeConfig="@xml/locales_config"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Citrine"
android:usesCleartextTraffic="true"
android:localeConfig="@xml/locales_config"
tools:targetApi="tiramisu">
<receiver
android:name=".service.ClipboardReceiver"
android:enabled="true"
android:exported="true">
</receiver>

<service
android:name=".service.WebSocketServerService"
Expand All @@ -58,6 +66,7 @@
android:theme="@style/Theme.Citrine">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.greenart7c3.citrine.service

import android.content.BroadcastReceiver
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.widget.Toast

class ClipboardReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent?) {
if (intent != null && intent.hasExtra("url")) {
val url = intent.getStringExtra("url")

// Copy the URL to the clipboard
val clipboard: ClipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("Copied URL", url)
clipboard.setPrimaryClip(clip)

// Show a toast message
Toast.makeText(context, "URL copied to clipboard", Toast.LENGTH_SHORT).show()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.app.TaskStackBuilder
import android.content.BroadcastReceiver
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
Expand Down Expand Up @@ -44,14 +39,6 @@ class WebSocketServerService : Service() {
fun getService(): WebSocketServerService = this@WebSocketServerService
}

private val brCopy: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("WebSocket Server Address", "ws://localhost:${webSocketServer.port()}")
clipboard.setPrimaryClip(clip)
}
}

@OptIn(DelicateCoroutinesApi::class)
private fun eventsToDelete(database: AppDatabase) {
GlobalScope.launch(Dispatchers.IO) {
Expand Down Expand Up @@ -119,14 +106,6 @@ class WebSocketServerService : Service() {
300000,
)

val intentFilter = IntentFilter("com.greenart7c3.citrine.ACTION_COPY")

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(brCopy, intentFilter, RECEIVER_NOT_EXPORTED)
} else {
registerReceiver(brCopy, intentFilter)
}

// Start the WebSocket server
webSocketServer = CustomWebSocketServer(
host = Settings.host,
Expand All @@ -143,7 +122,6 @@ class WebSocketServerService : Service() {
timer?.cancel()
timer = null
EventSubscription.closeAll()
unregisterReceiver(brCopy)
webSocketServer.stop()
super.onDestroy()
}
Expand All @@ -159,8 +137,15 @@ class WebSocketServerService : Service() {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)

val copy = Intent("com.greenart7c3.citrine.ACTION_COPY")
val piCopy = PendingIntent.getBroadcast(this, 0, copy, PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE)
val copyIntent = Intent(this, ClipboardReceiver::class.java)
copyIntent.putExtra("url", "ws://localhost:${webSocketServer.port()}")

val copyPendingIntent = PendingIntent.getBroadcast(
this,
0,
copyIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE,
)

val resultIntent = Intent(this, MainActivity::class.java)

Expand All @@ -175,7 +160,7 @@ class WebSocketServerService : Service() {
.setContentTitle("Relay running at ws://localhost:${webSocketServer.port()}")
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.addAction(R.drawable.ic_launcher_background, "Copy Address", piCopy)
.addAction(R.drawable.ic_launcher_background, "Copy Address", copyPendingIntent)
.setContentIntent(resultPendingIntent)

return notificationBuilder.build()
Expand Down

0 comments on commit e82d0a1

Please sign in to comment.