Skip to content

Commit

Permalink
Add subject and content template options
Browse files Browse the repository at this point in the history
  • Loading branch information
jinweijie committed Nov 15, 2024
1 parent 1f10321 commit 6e2ad07
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId = "com.jinweijie.notifyme"
minSdk = 24
targetSdk = 34
versionCode = 140
versionName = "1.4.0"
versionCode = 150
versionName = "1.5.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/java/com/jinweijie/notifyme/CallReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,8 @@ class CallReceiver : BroadcastReceiver() {
Log.d("CallReceiver", "Incoming call from $displayMessage")
Toast.makeText(context, "Incoming call from $displayMessage", Toast.LENGTH_LONG).show()

val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
val currentTime = sdf.format(Date())

// Post the data
Utils.sendNotification(incomingNumber!!, "Call from $displayMessage @ $currentTime", "Call", context)
Utils.sendNotification(incomingNumber!!, displayMessage, "Call", context)
} else {
Log.d("CallReceiver", "Incoming call number is null or empty.")
}
Expand Down
57 changes: 57 additions & 0 deletions app/src/main/java/com/jinweijie/notifyme/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class MainActivity : ComponentActivity() {

private lateinit var sharedPreferences: SharedPreferences

// General configuration UI elements
private lateinit var etSmsSubjectTemplate: EditText
private lateinit var etSmsContentTemplate: EditText
private lateinit var etPhoneSubjectTemplate: EditText
private lateinit var etPhoneContentTemplate: EditText
private lateinit var btnSaveGeneral: Button
private lateinit var btnUseDefaultGeneral: Button

// Bark UI elements
private lateinit var cbEnableBark: CheckBox
private lateinit var layoutBarkContent: LinearLayout
Expand Down Expand Up @@ -71,6 +79,14 @@ class MainActivity : ComponentActivity() {
private fun initializeUi() {
setContentView(R.layout.activity_main)

// Initialize General Configuration UI elements
etSmsSubjectTemplate = findViewById<EditText>(R.id.et_sms_subject_template)
etSmsContentTemplate = findViewById<EditText>(R.id.et_sms_content_template)
etPhoneSubjectTemplate = findViewById<EditText>(R.id.et_phone_subject_template)
etPhoneContentTemplate = findViewById<EditText>(R.id.et_phone_content_template)
btnSaveGeneral = findViewById<Button>(R.id.btn_save_general)
btnUseDefaultGeneral = findViewById<Button>(R.id.btn_use_default_general)

// Initialize Bark UI elements
cbEnableBark = findViewById(R.id.cb_enable_bark)
layoutBarkContent = findViewById(R.id.layout_bark_content)
Expand Down Expand Up @@ -117,6 +133,23 @@ class MainActivity : ComponentActivity() {
// Load and display the current settings
loadSavedSettings()

// Set click listener on the Save button for General configuration
btnSaveGeneral.setOnClickListener {
saveGeneralSettings()

Toast.makeText(this, "General settings saved.", Toast.LENGTH_LONG).show()
}
// Set click listener on the Use Default button for General configuration
btnUseDefaultGeneral.setOnClickListener {
etSmsSubjectTemplate.setText("[{{TYPE}}] {{SENDER}}")
etSmsContentTemplate.setText("{{MESSAGE}}")
etPhoneSubjectTemplate.setText("[{{TYPE}}] {{SENDER}}")
etPhoneContentTemplate.setText("Call from {{MESSAGE}} @ {{TIME}}")

saveGeneralSettings()
Toast.makeText(this, "Default templates applied and saved.", Toast.LENGTH_LONG).show()
}

// Set click listener on the Save button for Bark configuration
btnSaveBark.setOnClickListener {
saveBarkSettings()
Expand Down Expand Up @@ -194,6 +227,18 @@ class MainActivity : ComponentActivity() {
}
}

private fun saveGeneralSettings() {
val smsSubjectTemplate = etSmsSubjectTemplate.text.toString().trim()
val smsContentTemplate = etSmsContentTemplate.text.toString().trim()
val phoneSubjectTemplate = etPhoneSubjectTemplate.text.toString().trim()
val phoneContentTemplate = etPhoneContentTemplate.text.toString().trim()

saveConfig("sms_subject_template", smsSubjectTemplate)
saveConfig("sms_content_template", smsContentTemplate)
saveConfig("phone_subject_template", phoneSubjectTemplate)
saveConfig("phone_content_template", phoneContentTemplate)
}

private fun saveBarkSettings() {
val newEndpoint = etEndpoint.text.toString().trim()
if (newEndpoint.isNotEmpty()) {
Expand Down Expand Up @@ -253,6 +298,18 @@ class MainActivity : ComponentActivity() {
}

private fun loadSavedSettings() {

// Load General settings
val smsSubjectTemplate = sharedPreferences.getString("sms_subject_template", "[{{TYPE}}] {{SENDER}}")
val smsContentTemplate = sharedPreferences.getString("sms_content_template", "{{MESSAGE}}")
val phoneSubjectTemplate = sharedPreferences.getString("phone_subject_template", "[{{TYPE}}] {{SENDER}}")
val phoneContentTemplate = sharedPreferences.getString("phone_content_template", "Call from {{MESSAGE}} @ {{TIME}}")

etSmsSubjectTemplate.setText(smsSubjectTemplate)
etSmsContentTemplate.setText(smsContentTemplate)
etPhoneSubjectTemplate.setText(phoneSubjectTemplate)
etPhoneContentTemplate.setText(phoneContentTemplate)

// Load Bark settings
val isBarkEnabled = sharedPreferences.getBoolean("enable_bark", false)
cbEnableBark.isChecked = isBarkEnabled
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/jinweijie/notifyme/SmsReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import android.content.Context
import android.content.Intent
import android.telephony.SmsMessage
import android.widget.Toast
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class SmsReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Expand Down
48 changes: 40 additions & 8 deletions app/src/main/java/com/jinweijie/notifyme/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,49 @@ object Utils {
return
}

var subject = sender
var content = message
val sharedPreferences = context.getSharedPreferences("AppConfig", Context.MODE_PRIVATE)
if(type == "SMS"){
val smsSubjectTemplate = sharedPreferences?.getString("sms_subject_template", "[{{TYPE}}] {{SENDER}}")
val smsContentTemplate = sharedPreferences?.getString("sms_content_template", "{{MESSAGE}}")

val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
val currentTime = sdf.format(Date())

subject = smsSubjectTemplate?.replace("{{SENDER}}", sender)
?.replace("{{TIME}}", currentTime)
?.replace("{{TYPE}}", type)!!

content = smsContentTemplate?.replace("{{MESSAGE}}", message)
?.replace("{{TIME}}", currentTime)
?.replace("{{TYPE}}", type)!!
}
else if(type == "Call"){
val phoneSubjectTemplate = sharedPreferences.getString("phone_subject_template", "[{{TYPE}}] {{SENDER}}")
val phoneContentTemplate = sharedPreferences.getString("phone_content_template", "Call from {{MESSAGE}} @ {{TIME}}")

val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
val currentTime = sdf.format(Date())

subject = phoneSubjectTemplate?.replace("{{SENDER}}", sender)
?.replace("{{TIME}}", currentTime)
?.replace("{{TYPE}}", type)!!
content = phoneContentTemplate?.replace("{{MESSAGE}}", message)
?.replace("{{TIME}}", currentTime)
?.replace("{{TYPE}}", type)!!
}

val isBarkEnabled = sharedPreferences.getBoolean("enable_bark", false)
val isEmailEnabled = sharedPreferences.getBoolean("enable_email", false)
val isWebhookEnabled = sharedPreferences.getBoolean("enable_webhook", false)
val isHttpEnabled = sharedPreferences.getBoolean("enable_http", false)

if(isBarkEnabled)
sendBark(sender, message, type, context)
sendBark(subject, content, type, context)

if(isEmailEnabled)
sendEmail(sender, message, type, context)
sendEmail(subject, content, type, context)

if(isWebhookEnabled)
sendWebhook(sender, message, type, context)
Expand All @@ -51,7 +83,7 @@ object Utils {
sendHttp(sender, message, type, context)
}

fun sendBark(sender: String, message: String, type: String, context: Context?) {
fun sendBark(subject: String, content: String, type: String, context: Context?) {
// Run network operation in a background thread
Thread {
try {
Expand Down Expand Up @@ -79,8 +111,8 @@ object Utils {
// Prepare the payload for the Bark server
val jsonPayload = """
{
"title": "${sender ?: "Unknown Sender"}",
"body": "${message ?: "No message content"}",
"title": "${subject ?: "Unknown Sender"}",
"body": "${content ?: "No message content"}",
"category": "$type",
"device_key": "$deviceKey"
}
Expand Down Expand Up @@ -134,7 +166,7 @@ object Utils {
}.start() // Starting the thread
}

fun sendEmail(mailSubject: String, message: String, type: String, context: Context?) {
fun sendEmail(mailSubject: String, content: String, type: String, context: Context?) {
if (context == null) {
Log.e("sendEmail", "Context is null")
return
Expand Down Expand Up @@ -183,8 +215,8 @@ object Utils {
val mimeMessage = MimeMessage(session).apply {
setFrom(InternetAddress(username))
setRecipient(Message.RecipientType.TO, InternetAddress(recipient))
subject = "[$type] $mailSubject"
setText(message)
subject = mailSubject
setText(content)
}

// Send the email
Expand Down
90 changes: 90 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,96 @@
android:orientation="vertical"
android:padding="16dp">

<!-- General Configuration Section -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="24dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="General Configuration"
android:textSize="18sp"
android:textStyle="bold"
android:paddingBottom="8dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SMS Subject Template"
android:textSize="14sp"
android:paddingTop="8dp" />

<EditText
android:id="@+id/et_sms_subject_template"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="SMS Subject Template"
android:inputType="textMultiLine"
android:minLines="1" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SMS Content Template"
android:textSize="14sp"
android:paddingTop="8dp" />

<EditText
android:id="@+id/et_sms_content_template"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="SMS Content Template"
android:inputType="textMultiLine"
android:minLines="2" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Subject Template"
android:textSize="14sp"
android:paddingTop="8dp" />

<EditText
android:id="@+id/et_phone_subject_template"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Subject Template"
android:inputType="textMultiLine"
android:minLines="1" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Content Template"
android:textSize="14sp"
android:paddingTop="8dp" />

<EditText
android:id="@+id/et_phone_content_template"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Content Template"
android:inputType="textMultiLine"
android:minLines="2" />

<Button
android:id="@+id/btn_save_general"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Save General Settings" />

<Button
android:id="@+id/btn_use_default_general"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Use Default Templates" />

</LinearLayout>

<!-- Bark Configuration Section -->
<LinearLayout
android:layout_width="match_parent"
Expand Down

0 comments on commit 6e2ad07

Please sign in to comment.