Skip to content

Commit 4c7412d

Browse files
committed
Updated android app
1 parent 3e7deef commit 4c7412d

11 files changed

Lines changed: 220 additions & 4 deletions

File tree

android/app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<uses-permission android:name="android.permission.SEND_SMS" />
1111
<uses-permission android:name="android.permission.RECEIVE_SMS" />
1212
<uses-permission android:name="android.permission.INTERNET" />
13+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
1314
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
1415

1516

@@ -25,13 +26,17 @@
2526
tools:targetApi="31">
2627
<activity
2728
android:name=".MainActivity"
28-
android:exported="true">
29+
android:screenOrientation="portrait"
30+
android:exported="true"
31+
tools:ignore="LockedOrientationActivity">
2932
<intent-filter>
3033
<action android:name="android.intent.action.MAIN" />
3134

3235
<category android:name="android.intent.category.LAUNCHER" />
3336
</intent-filter>
3437
</activity>
38+
<activity android:name=".LoginActivity" android:screenOrientation="portrait"
39+
tools:ignore="LockedOrientationActivity" />
3540

3641
<service
3742
android:name=".MyFirebaseMessagingService"

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import android.content.Context
55
import android.content.Intent
66
import android.content.IntentFilter
77
import android.util.Log
8-
import androidx.preference.PreferenceManager
98
import androidx.work.OneTimeWorkRequest
109
import androidx.work.WorkManager
1110
import androidx.work.Worker
@@ -50,8 +49,14 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
5049
.enqueue(work)
5150
// [END dispatch_job]
5251
}
53-
private fun sendRegistrationToServer(token: String?) {
52+
private fun sendRegistrationToServer(token: String) {
5453
Log.d(TAG, "sendRegistrationTokenToServer($token)")
54+
Settings.setApiKeyAsync(this, token)
55+
56+
if (Settings.isLoggedIn(this)) {
57+
Log.d(TAG, "updating phone with new fcm token")
58+
HttpSmsApiService().updatePhone(Settings.getPhoneOrDefault(this), token)
59+
}
5560
}
5661

5762
companion object {

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,32 @@ class HttpSmsApiService {
116116
Log.i(TAG, "[$event] event sent successfully for message with ID [$messageId]" )
117117
}
118118

119+
120+
fun updatePhone(phoneNumber: String, fcmToken: String) {
121+
val client = OkHttpClient()
122+
123+
val body = """
124+
{
125+
"fcm_token": "$fcmToken",
126+
"phone_number": "$phoneNumber"
127+
}
128+
""".trimIndent()
129+
130+
val request: Request = Request.Builder()
131+
.url(baseURL.resolve("/v1/phones").toURL())
132+
.put(body.toRequestBody(jsonMediaType))
133+
.build()
134+
135+
val response = client.newCall(request).execute()
136+
if (!response.isSuccessful) {
137+
Log.e(TAG, "error response [${response.body?.string()}] with code [${response.code}] while sending fcm token [${body}]")
138+
return
139+
}
140+
141+
response.close()
142+
Log.i(TAG, "fcm token sent successfully for phone [$phoneNumber]" )
143+
}
144+
119145
companion object {
120146
private val TAG = HttpSmsApiService::class.simpleName
121147
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.httpsms
2+
3+
import android.os.Bundle
4+
import android.util.Log
5+
import androidx.appcompat.app.AppCompatActivity
6+
7+
class LoginActivity : AppCompatActivity() {
8+
companion object {
9+
private val TAG = LoginActivity::class.simpleName
10+
}
11+
12+
override fun onCreate(savedInstanceState: Bundle?) {
13+
super.onCreate(savedInstanceState)
14+
Log.d(TAG, "inside on create method")
15+
setContentView(R.layout.activity_login)
16+
}
17+
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import android.annotation.SuppressLint
66
import android.app.NotificationChannel
77
import android.app.NotificationManager
88
import android.content.Context
9+
import android.content.Intent
910
import android.content.pm.PackageManager
1011
import android.os.Bundle
1112
import android.telephony.PhoneNumberUtils
1213
import android.telephony.TelephonyManager
14+
import android.util.Log
1315
import android.widget.TextView
1416
import android.widget.Toast
1517
import androidx.activity.result.contract.ActivityResultContracts
@@ -26,6 +28,11 @@ class MainActivity : AppCompatActivity() {
2628

2729
override fun onCreate(savedInstanceState: Bundle?) {
2830
super.onCreate(savedInstanceState)
31+
32+
Log.d(TAG, "on create")
33+
34+
redirectToLogin()
35+
2936
setContentView(R.layout.activity_main)
3037

3138
createChannel()
@@ -40,6 +47,20 @@ class MainActivity : AppCompatActivity() {
4047
setActiveStatus(this)
4148
}
4249

50+
override fun onResume() {
51+
super.onResume()
52+
Log.d(TAG, "on activity resume")
53+
redirectToLogin()
54+
}
55+
56+
private fun redirectToLogin() {
57+
if (Settings.isLoggedIn(this)) {
58+
return
59+
}
60+
val switchActivityIntent = Intent(this, LoginActivity::class.java)
61+
startActivity(switchActivityIntent)
62+
}
63+
4364
private fun setActiveStatus(context: Context) {
4465
val switch = findViewById<SwitchMaterial>(R.id.cardSwitch)
4566
switch.isChecked = Settings.getActiveStatus(context)

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ object Settings {
99

1010
private const val SETTINGS_OWNER = "SETTINGS_OWNER"
1111
private const val SETTINGS_ACTIVE = "SETTINGS_ACTIVE_STATUS"
12+
private const val SETTINGS_API_KEY = "SETTINGS_API_KEY"
13+
private const val SETTINGS_FCM_TOKEN = "SETTINGS_FCM_TOKEN"
1214

1315
fun getOwner(context: Context): String? {
1416
Log.d(TAG, Settings::getOwner.name)
@@ -26,6 +28,10 @@ object Settings {
2628
return owner
2729
}
2830

31+
fun getPhoneOrDefault(context: Context): String {
32+
return getOwner(context) ?: return DEFAULT_PHONE_NUMBER
33+
}
34+
2935
fun setOwnerAsync(context: Context, owner: String) {
3036
Log.d(TAG, Settings::getOwner.name)
3137

@@ -55,5 +61,49 @@ object Settings {
5561
.apply()
5662
}
5763

64+
fun isLoggedIn(context: Context): Boolean {
65+
return getApiKey(context) != null
66+
}
67+
68+
fun getApiKey(context: Context): String?{
69+
Log.d(TAG, Settings::getApiKey.name)
70+
71+
val activeStatus = PreferenceManager
72+
.getDefaultSharedPreferences(context)
73+
.getString(this.SETTINGS_API_KEY,null)
74+
75+
Log.d(TAG, "API_KEY: [$activeStatus]")
76+
return activeStatus
77+
}
78+
79+
fun setApiKeyAsync(context: Context, apiKey: String) {
80+
Log.d(TAG, Settings::setApiKeyAsync.name)
81+
82+
PreferenceManager.getDefaultSharedPreferences(context)
83+
.edit()
84+
.putString(this.SETTINGS_API_KEY, apiKey)
85+
.apply()
86+
}
87+
88+
fun getFcmToken(context: Context): String?{
89+
Log.d(TAG, Settings::getFcmToken.name)
90+
91+
val activeStatus = PreferenceManager
92+
.getDefaultSharedPreferences(context)
93+
.getString(this.SETTINGS_FCM_TOKEN,null)
94+
95+
Log.d(TAG, "API_KEY: [$activeStatus]")
96+
return activeStatus
97+
}
98+
99+
fun setFcmToken(context: Context, apiKey: String) {
100+
Log.d(TAG, Settings::setApiKeyAsync.name)
101+
102+
PreferenceManager.getDefaultSharedPreferences(context)
103+
.edit()
104+
.putString(this.SETTINGS_FCM_TOKEN, apiKey)
105+
.apply()
106+
}
107+
58108
private val TAG = Settings::class.simpleName
59-
}
109+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="124dp"
3+
android:height="124dp"
4+
android:viewportWidth="124"
5+
android:viewportHeight="124">
6+
<path
7+
android:pathData="M55.75,27.16C52.53,21.61 44.47,21.61 41.25,27.16L6.13,87.53C2.91,93.07 6.94,100 13.38,100H40.8C38.04,97.59 37.02,93.43 39.11,89.86L65.7,44.27L55.75,27.16Z"
8+
android:fillColor="#80EEC0"
9+
android:fillType="evenOdd"/>
10+
<path
11+
android:pathData="M78,40.4C80.67,35.87 87.33,35.87 90,40.4L119.06,89.8C121.73,94.33 118.39,100 113.06,100H54.94C49.61,100 46.27,94.33 48.94,89.8L78,40.4Z"
12+
android:fillColor="#00DC82"/>
13+
</vector>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24">
6+
<path
7+
android:pathData="M10,17V14H3V10H10V7L15,12L10,17M10,2H19A2,2 0,0 1,21 4V20A2,2 0,0 1,19 22H10A2,2 0,0 1,8 20V18H10V20H19V4H10V6H8V4A2,2 0,0 1,10 2Z"
8+
android:fillColor="#ffffff"/>
9+
</vector>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:paddingLeft="16dp"
7+
android:paddingRight="16dp"
8+
android:layout_height="match_parent">
9+
10+
<com.google.android.material.textfield.TextInputLayout
11+
android:id="@+id/textField"
12+
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
13+
android:layout_width="match_parent"
14+
android:layout_height="wrap_content"
15+
android:hint="@string/text_area_api_key"
16+
app:layout_constraintBottom_toBottomOf="parent"
17+
app:layout_constraintTop_toTopOf="parent">
18+
19+
<com.google.android.material.textfield.TextInputEditText
20+
android:layout_width="match_parent"
21+
android:layout_height="wrap_content"
22+
android:inputType="textMultiLine"
23+
tools:ignore="TextContrastCheck" />
24+
25+
</com.google.android.material.textfield.TextInputLayout>
26+
27+
<com.google.android.material.button.MaterialButton
28+
android:id="@+id/button"
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:layout_marginTop="40dp"
32+
android:textSize="16sp"
33+
android:padding="10dp"
34+
android:textColor="@color/white"
35+
android:backgroundTint="#2196f3"
36+
android:text="@string/sign_in_button"
37+
app:layout_constraintEnd_toEndOf="parent"
38+
app:layout_constraintStart_toStartOf="parent"
39+
android:drawableTint="@color/white"
40+
app:icon="@drawable/ic_login"
41+
app:iconTint="@color/white"
42+
style="@style/Widget.MaterialComponents.Button.Icon"
43+
app:layout_constraintTop_toBottomOf="@+id/textField" />
44+
45+
<ImageView
46+
android:id="@+id/imageView"
47+
android:layout_width="wrap_content"
48+
android:layout_height="wrap_content"
49+
android:layout_marginTop="24dp"
50+
android:contentDescription="@string/img_http_sms_logo"
51+
app:layout_constraintEnd_toEndOf="parent"
52+
app:layout_constraintHorizontal_bias="0.498"
53+
app:layout_constraintStart_toStartOf="parent"
54+
app:layout_constraintTop_toTopOf="parent"
55+
app:srcCompat="@drawable/ic_colored_logo"
56+
tools:ignore="ImageContrastCheck" />
57+
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,14 @@
6060

6161
</com.google.android.material.card.MaterialCardView>
6262

63+
<Button
64+
android:id="@+id/sign_in_button"
65+
android:layout_width="wrap_content"
66+
android:layout_height="wrap_content"
67+
android:layout_marginTop="32dp"
68+
android:text="@string/sign_in_button"
69+
app:layout_constraintEnd_toEndOf="parent"
70+
app:layout_constraintStart_toStartOf="parent"
71+
app:layout_constraintTop_toBottomOf="@+id/card" />
72+
6373
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)