Skip to content

Commit fb6c05b

Browse files
committed
Add logtail event for logs
1 parent d755ce2 commit fb6c05b

File tree

8 files changed

+219
-33
lines changed

8 files changed

+219
-33
lines changed

android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
9898

9999
private fun sendMessage(message: Message, sentIntent: PendingIntent, deliveredIntent: PendingIntent) {
100100
Timber.d("sending SMS for message with ID [${message.id}]")
101-
SmsManagerService().sendMessage(this.applicationContext, message, sentIntent, deliveredIntent)
101+
try {
102+
SmsManagerService().sendMessage(this.applicationContext, message, sentIntent, deliveredIntent)
103+
} catch (e: Exception) {
104+
Timber.e(e)
105+
Timber.d("could not send SMS for message with ID [${message.id}]")
106+
return
107+
}
102108
Timber.d("sent SMS for message with ID [${message.id}]")
103109
}
104110

android/app/src/main/java/com/httpsms/LoginActivity.kt

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package com.httpsms
22

3+
import android.Manifest
4+
import android.annotation.SuppressLint
5+
import android.content.Context
6+
import android.content.pm.PackageManager
37
import android.os.Bundle
8+
import android.telephony.PhoneNumberUtils
9+
import android.telephony.TelephonyManager
410
import android.view.View
511
import androidx.appcompat.app.AppCompatActivity
12+
import androidx.core.app.ActivityCompat
613
import androidx.lifecycle.MutableLiveData
714
import com.google.android.material.button.MaterialButton
815
import com.google.android.material.progressindicator.LinearProgressIndicator
@@ -16,14 +23,53 @@ class LoginActivity : AppCompatActivity() {
1623
redirectToMain()
1724
setContentView(R.layout.activity_login)
1825
registerListeners()
26+
setPhoneNumber()
1927
}
2028

2129
private fun registerListeners() {
2230
loginButton().setOnClickListener { onLoginClick() }
2331
}
2432

33+
private fun setPhoneNumber() {
34+
val phoneNumber = getPhoneNumber(this)
35+
if(phoneNumber == null) {
36+
Timber.d("cannot get phone due to no permissions")
37+
return
38+
}
39+
40+
val phoneInput = findViewById<TextInputEditText>(R.id.loginPhoneNumberInput)
41+
phoneInput.setText(phoneNumber)
42+
Timber.d("phone number [$phoneNumber] set successfully")
43+
}
44+
45+
@SuppressLint("HardwareIds")
46+
private fun getPhoneNumber(context: Context): String? {
47+
val telephonyManager = this.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
48+
if (ActivityCompat.checkSelfPermission(
49+
this,
50+
Manifest.permission.READ_SMS
51+
) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(
52+
this,
53+
Manifest.permission.READ_PHONE_NUMBERS
54+
) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(
55+
this,
56+
Manifest.permission.READ_PHONE_STATE
57+
) != PackageManager.PERMISSION_GRANTED
58+
) {
59+
Timber.e("cannot get owner because permissions are not granted")
60+
return Settings.getOwner(this)
61+
}
62+
63+
if (telephonyManager.line1Number != null && telephonyManager.line1Number != "") {
64+
Settings.setOwnerAsync(context, telephonyManager.line1Number)
65+
}
66+
67+
return telephonyManager.line1Number
68+
}
69+
70+
2571
private fun onLoginClick() {
26-
Timber.e("login button clicked")
72+
Timber.d("login button clicked")
2773
loginButton().isEnabled = false
2874
val progressBar = findViewById<LinearProgressIndicator>(R.id.loginProgressIndicator)
2975
progressBar.visibility = View.VISIBLE
@@ -34,18 +80,48 @@ class LoginActivity : AppCompatActivity() {
3480
val apiKey = findViewById<TextInputEditText>(R.id.loginApiKeyTextInput)
3581
apiKey.isEnabled = false
3682

83+
val phoneNumberLayout = findViewById<TextInputLayout>(R.id.loginPhoneNumberLayout)
84+
phoneNumberLayout.error = null
85+
86+
val phoneNumber = findViewById<TextInputEditText>(R.id.loginPhoneNumberInput)
87+
phoneNumber.isEnabled = false
88+
89+
val resetView = fun () {
90+
apiKey.isEnabled = true
91+
progressBar.visibility = View.INVISIBLE
92+
phoneNumber.isEnabled = true
93+
loginButton().isEnabled = true
94+
}
95+
96+
if (
97+
!PhoneNumberUtils.isWellFormedSmsAddress(phoneNumber.text.toString()) ||
98+
!PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber.text.toString())
99+
) {
100+
Timber.e("phone number [$phoneNumber] is not valid")
101+
resetView()
102+
phoneNumberLayout.error = "Invalid E.164 phone number"
103+
return
104+
}
105+
37106
val liveData = MutableLiveData<String?>()
38107
liveData.observe(this) { authResult ->
39108
run {
40109
progressBar.visibility = View.INVISIBLE
41110
if (authResult != null) {
42-
apiKey.isEnabled = true
43-
loginButton().isEnabled = true
111+
resetView()
44112
apiKeyLayout.error = authResult
45113
return@run
46114
}
47-
Timber.d("login successfully redirecting to main view")
115+
48116
Settings.setApiKeyAsync(this, apiKey.text.toString())
117+
118+
val e164PhoneNumber = PhoneNumberUtils.formatNumberToE164(
119+
phoneNumber.text.toString(),
120+
this.resources.configuration.locales.get(0).country
121+
)
122+
Settings.setOwnerAsync(this, e164PhoneNumber)
123+
124+
Timber.d("login successfully redirecting to main view")
49125
redirectToMain()
50126
}
51127
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.httpsms
2+
3+
import android.os.Build
4+
import com.beust.klaxon.Klaxon
5+
import okhttp3.MediaType.Companion.toMediaType
6+
import okhttp3.OkHttpClient
7+
import okhttp3.Request
8+
import okhttp3.RequestBody.Companion.toRequestBody
9+
import timber.log.Timber
10+
import java.time.ZoneOffset
11+
import java.time.ZonedDateTime
12+
import java.time.format.DateTimeFormatter
13+
14+
class LogtailTree: Timber.DebugTree() {
15+
private val client = OkHttpClient()
16+
private val jsonMediaType = "application/json; charset=utf-8".toMediaType()
17+
18+
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
19+
val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z")
20+
21+
val logEntry = LogEntry(
22+
priority,
23+
severity(priority),
24+
tag,
25+
message,
26+
Build.MODEL,
27+
Build.BRAND,
28+
Build.DEVICE,
29+
Build.VERSION.SDK_INT,
30+
ZonedDateTime.now(ZoneOffset.UTC).format(formatter),
31+
t
32+
)
33+
34+
val request: Request = Request.Builder()
35+
.url("https://in.logtail.com")
36+
.post(Klaxon().toJsonString(logEntry).toRequestBody(jsonMediaType))
37+
.header("Authorization", "Bearer m7ZoA8u5KRYNe6RnEdWeZqsZ")
38+
.build()
39+
40+
41+
42+
Thread {
43+
client.newCall(request).execute()
44+
}.start()
45+
}
46+
47+
private fun severity(priority: Int): String {
48+
return when(priority) {
49+
3 -> "DEBUG"
50+
4 -> "INFO"
51+
5 -> "WARNING"
52+
6 -> "ERROR"
53+
7 -> "ASSERT"
54+
else -> "VERBOSE"
55+
}
56+
}
57+
58+
class LogEntry(
59+
val priority: Int,
60+
val severity: String,
61+
val tag: String?,
62+
val message: String,
63+
val model: String,
64+
val brand: String,
65+
val device: String,
66+
val version: Int,
67+
val timestamp: String,
68+
val throwable: Throwable?)
69+
}

android/app/src/main/java/com/httpsms/MainActivity.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class MainActivity : AppCompatActivity() {
109109
private fun initTimber() {
110110
if (BuildConfig.DEBUG) {
111111
Timber.plant(Timber.DebugTree())
112+
Timber.plant(LogtailTree())
112113
}
113114
}
114115

@@ -119,6 +120,7 @@ class MainActivity : AppCompatActivity() {
119120
private fun onLogoutClick() {
120121
Timber.d("logout button clicked")
121122
Settings.setApiKeyAsync(this, null)
123+
Settings.setOwnerAsync(this, null)
122124
Settings.setFcmTokenLastUpdateTimestampAsync(this, 0)
123125
redirectToLogin()
124126
}
@@ -203,12 +205,12 @@ class MainActivity : AppCompatActivity() {
203205
return Settings.getOwnerOrDefault(this)
204206
}
205207

206-
if (telephonyManager.line1Number != null) {
208+
if (telephonyManager.line1Number != null && telephonyManager.line1Number != "") {
207209
Timber.d("line 1 number fetched [${telephonyManager.line1Number}]")
208210
Settings.setOwnerAsync(context, telephonyManager.line1Number)
209211
}
210212

211-
return telephonyManager.line1Number ?: Settings.getOwnerOrDefault(this)
213+
return Settings.getOwnerOrDefault(this)
212214
}
213215

214216
private fun requestPermissions(context:Context) {

android/app/src/main/java/com/httpsms/Settings.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ object Settings {
6060
}
6161

6262

63-
fun setOwnerAsync(context: Context, owner: String) {
63+
fun setOwnerAsync(context: Context, owner: String?) {
6464
Timber.d(Settings::setOwnerAsync.name)
6565

6666
PreferenceManager.getDefaultSharedPreferences(context)

android/app/src/main/java/com/httpsms/SmsManagerService.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.httpsms
22

33
import android.app.PendingIntent
44
import android.content.Context
5+
import android.os.Build
56
import android.telephony.SmsManager
67

78
class SmsManagerService {
@@ -11,7 +12,16 @@ class SmsManagerService {
1112
}
1213

1314
fun sendMessage(context: Context, message: Message, sentIntent:PendingIntent, deliveryIntent: PendingIntent) {
14-
val smsManager: SmsManager = context.getSystemService(SmsManager::class.java)
15-
smsManager.sendTextMessage(message.contact, message.owner, message.content, sentIntent, deliveryIntent)
15+
getSmsManager(context)
16+
.sendTextMessage(message.contact, message.owner, message.content, sentIntent, deliveryIntent)
17+
}
18+
19+
@Suppress("DEPRECATION")
20+
private fun getSmsManager(context: Context): SmsManager {
21+
return if (Build.VERSION.SDK_INT < 31) {
22+
SmsManager.getDefault()
23+
} else {
24+
context.getSystemService(SmsManager::class.java)
25+
}
1626
}
1727
}

android/app/src/main/res/layout/activity_login.xml

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,35 @@
3535
app:layout_constraintHorizontal_bias="0.498"
3636
app:layout_constraintStart_toStartOf="parent" />
3737

38+
<com.google.android.material.textfield.TextInputLayout
39+
android:id="@+id/loginPhoneNumberLayout"
40+
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
41+
android:layout_width="match_parent"
42+
android:layout_height="wrap_content"
43+
android:layout_marginBottom="224dp"
44+
android:hint="@string/phone_number"
45+
app:errorEnabled="true"
46+
app:placeholderText="@string/login_phone_number_hint"
47+
app:layout_constraintBottom_toBottomOf="parent"
48+
app:layout_constraintTop_toBottomOf="@+id/loginApiKeyTextInputLayout"
49+
app:layout_constraintVertical_bias="0.137"
50+
tools:layout_editor_absoluteX="16dp">
51+
52+
<com.google.android.material.textfield.TextInputEditText
53+
android:id="@+id/loginPhoneNumberInput"
54+
android:layout_width="match_parent"
55+
android:layout_height="wrap_content"
56+
android:inputType="textMultiLine"
57+
tools:ignore="TextContrastCheck" />
58+
59+
</com.google.android.material.textfield.TextInputLayout>
60+
3861
<com.google.android.material.textfield.TextInputLayout
3962
android:id="@+id/loginApiKeyTextInputLayout"
4063
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
4164
android:layout_width="match_parent"
4265
android:layout_height="wrap_content"
4366
app:errorEnabled="true"
44-
app:errorContentDescription="How are you"
4567
android:hint="@string/text_area_api_key"
4668
app:layout_constraintBottom_toBottomOf="parent"
4769
app:layout_constraintTop_toTopOf="parent"
@@ -57,6 +79,7 @@
5779
</com.google.android.material.textfield.TextInputLayout>
5880

5981
<LinearLayout
82+
android:id="@+id/linearLayout"
6083
android:layout_width="wrap_content"
6184
android:layout_height="wrap_content"
6285
android:layout_weight="50"
@@ -65,35 +88,34 @@
6588
app:layout_constraintStart_toStartOf="parent"
6689
app:layout_constraintTop_toBottomOf="@+id/loginApiKeyTextInputLayout">
6790

68-
<com.google.android.material.button.MaterialButton
69-
android:id="@+id/loginButton"
70-
style="@style/Widget.MaterialComponents.Button.Icon"
71-
android:layout_width="wrap_content"
72-
android:layout_height="wrap_content"
73-
android:layout_marginTop="16dp"
74-
android:backgroundTint="#2196f3"
75-
android:drawableTint="@color/white"
76-
android:padding="10dp"
77-
android:text="@string/sign_in_button"
78-
android:textColor="@color/white"
79-
android:textSize="16sp"
80-
app:icon="@drawable/ic_login"
81-
app:iconTint="@color/white"
82-
app:layout_constraintEnd_toEndOf="parent"
83-
app:layout_constraintStart_toStartOf="parent"
84-
app:layout_constraintTop_toBottomOf="@+id/loginApiKeyTextInputLayout"
85-
tools:ignore="TextContrastCheck" />
86-
8791
<com.google.android.material.progressindicator.LinearProgressIndicator
8892
android:id="@+id/loginProgressIndicator"
8993
android:layout_width="match_parent"
90-
android:visibility="invisible"
9194
android:layout_height="wrap_content"
92-
android:indeterminate="true"
9395
android:layout_marginTop="4dp"
96+
android:indeterminate="true"
97+
android:visibility="invisible"
9498
app:indicatorColor="@color/pink_500"
9599
app:layout_constraintTop_toBottomOf="@+id/loginButton"
96100
tools:layout_editor_absoluteX="16dp" />
97101
</LinearLayout>
98102

103+
<com.google.android.material.button.MaterialButton
104+
android:id="@+id/loginButton"
105+
style="@style/Widget.MaterialComponents.Button.Icon"
106+
android:layout_width="wrap_content"
107+
android:layout_height="wrap_content"
108+
android:backgroundTint="#2196f3"
109+
android:drawableTint="@color/white"
110+
android:padding="10dp"
111+
android:text="@string/sign_in_button"
112+
android:textColor="@color/white"
113+
android:textSize="16sp"
114+
app:icon="@drawable/ic_login"
115+
app:iconTint="@color/white"
116+
app:layout_constraintEnd_toEndOf="parent"
117+
app:layout_constraintStart_toStartOf="parent"
118+
app:layout_constraintTop_toBottomOf="@+id/loginPhoneNumberLayout"
119+
tools:ignore="TextContrastCheck" />
120+
99121
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<resources>
22
<string name="app_name">HttpSMS</string>
33
<string name="nextRefreshTime">23/06/2022 18:58:30</string>
4-
<string name="phone_number">+1 800 555 0100</string>
54
<string name="menuIconDescription">Menu Icon</string>
65
<string name="menuIconName">More</string>
76
<string name="notification_channel_default">com.httpsms.notification.default</string>
@@ -10,4 +9,6 @@
109
<string name="img_http_sms_logo">HTTP Sms Logo</string>
1110
<string name="get_your_api_key">Open\nhttpsms.com/settings\nto get your API key</string>
1211
<string name="main_log_out">Log Out</string>
12+
<string name="login_phone_number_hint">International phone number in E.164 format</string>
13+
<string name="phone_number">Phone Number</string>
1314
</resources>

0 commit comments

Comments
 (0)