Skip to content

Commit d376e17

Browse files
committed
Added sample android app
1 parent ec3f677 commit d376e17

7 files changed

Lines changed: 132 additions & 12 deletions

File tree

android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ android {
3232
}
3333

3434
dependencies {
35-
3635
implementation 'androidx.core:core-ktx:1.8.0'
36+
implementation "androidx.cardview:cardview:1.0.0"
3737
implementation 'androidx.appcompat:appcompat:1.4.2'
3838
implementation 'com.google.android.material:material:1.6.1'
3939
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

android/app/src/main/AndroidManifest.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
xmlns:tools="http://schemas.android.com/tools"
44
package="com.httpsms">
55

6+
7+
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
8+
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
9+
<uses-permission android:name="android.permission.READ_SMS" />
10+
<uses-permission android:name="android.permission.SEND_SMS" />
11+
612
<application
713
android:allowBackup="true"
814
android:dataExtractionRules="@xml/data_extraction_rules"
@@ -24,4 +30,4 @@
2430
</activity>
2531
</application>
2632

27-
</manifest>
33+
</manifest>
Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,76 @@
11
package com.httpsms
22

3-
import androidx.appcompat.app.AppCompatActivity
3+
import android.Manifest
4+
import android.annotation.SuppressLint
5+
import android.content.Context
6+
import android.content.pm.PackageManager
47
import android.os.Bundle
8+
import android.telephony.PhoneNumberUtils
9+
import android.telephony.TelephonyManager
10+
import android.util.Log
11+
import android.view.Menu
12+
import android.view.MenuItem
13+
import android.widget.TextView
14+
import android.widget.Toast
15+
import androidx.activity.result.contract.ActivityResultContracts
16+
import androidx.appcompat.app.AppCompatActivity
17+
import androidx.core.app.ActivityCompat
18+
import java.util.*
19+
520

621
class MainActivity : AppCompatActivity() {
22+
private val logTag = MainActivity::class.simpleName ?: "MainActivity"
23+
724
override fun onCreate(savedInstanceState: Bundle?) {
825
super.onCreate(savedInstanceState)
926
setContentView(R.layout.activity_main)
27+
28+
val phoneNumber = getPhoneNumber(this)
29+
30+
val titleText = findViewById<TextView>(R.id.cardPhoneNumber)
31+
titleText.text = PhoneNumberUtils.formatNumber(phoneNumber, Locale.getDefault().country)
32+
}
33+
34+
@SuppressLint("HardwareIds")
35+
private fun getPhoneNumber(context: Context): String {
36+
// Register the permissions callback, which handles the user's response to the
37+
// system permissions dialog. Save the return value, an instance of
38+
// ActivityResultLauncher. You can use either a val, as shown in this snippet,
39+
// or a late init var in your onAttach() or onCreate() method.
40+
val requestPermissionLauncher =
41+
registerForActivityResult(
42+
ActivityResultContracts.RequestPermission()
43+
) { isGranted: Boolean ->
44+
if (isGranted) {
45+
val toast = Toast.makeText(context, "Granted", Toast.LENGTH_SHORT)
46+
toast.show()
47+
} else {
48+
val toast = Toast.makeText(context, "NOT Granted", Toast.LENGTH_LONG)
49+
toast.show()
50+
}
51+
}
52+
53+
val telephonyManager = this.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
54+
if (ActivityCompat.checkSelfPermission(
55+
this,
56+
Manifest.permission.READ_SMS
57+
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
58+
this,
59+
Manifest.permission.READ_PHONE_NUMBERS
60+
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
61+
this,
62+
Manifest.permission.READ_PHONE_STATE
63+
) != PackageManager.PERMISSION_GRANTED
64+
) {
65+
// You can directly ask for the permission.
66+
// The registered ActivityResultCallback gets the result of this request.
67+
requestPermissionLauncher.launch(Manifest.permission.READ_SMS)
68+
requestPermissionLauncher.launch(Manifest.permission.READ_PHONE_NUMBERS)
69+
requestPermissionLauncher.launch(Manifest.permission.READ_PHONE_STATE)
70+
}
71+
72+
val phoneNumber = telephonyManager.line1Number ?: "NO_PHONE_NUMBER"
73+
Log.d(logTag, phoneNumber)
74+
return phoneNumber
1075
}
1176
}

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

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,58 @@
66
android:layout_height="match_parent"
77
tools:context=".MainActivity">
88

9-
<TextView
10-
android:layout_width="71dp"
11-
android:layout_height="17dp"
12-
android:text="Hello World!"
13-
app:layout_constraintBottom_toBottomOf="parent"
9+
<com.google.android.material.card.MaterialCardView
10+
android:id="@+id/card"
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"
13+
android:layout_margin="8dp"
1414
app:layout_constraintEnd_toEndOf="parent"
1515
app:layout_constraintStart_toStartOf="parent"
16-
app:layout_constraintTop_toTopOf="parent" />
16+
app:layout_constraintTop_toTopOf="parent">
17+
18+
<LinearLayout
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:orientation="vertical"
22+
android:padding="16dp">
23+
24+
<!-- Title, secondary and supporting text -->
25+
<LinearLayout
26+
android:layout_width="match_parent"
27+
android:layout_height="match_parent"
28+
android:orientation="horizontal">
29+
30+
<TextView
31+
android:id="@+id/cardPhoneNumber"
32+
android:layout_width="wrap_content"
33+
android:layout_height="match_parent"
34+
android:text="@string/phone_number"
35+
android:textAppearance="?attr/textAppearanceTitleMedium"
36+
android:textColor="?android:attr/textColorPrimary"
37+
android:textSize="28sp" />
38+
39+
<com.google.android.material.switchmaterial.SwitchMaterial
40+
android:id="@+id/cardSwitch"
41+
android:layout_width="match_parent"
42+
android:layout_height="wrap_content"
43+
android:minHeight="48dp"
44+
android:text=""
45+
tools:ignore="TouchTargetSizeCheck" />
46+
</LinearLayout>
47+
48+
49+
<TextView
50+
android:id="@+id/cardRefreshTime"
51+
android:layout_width="wrap_content"
52+
android:layout_height="wrap_content"
53+
android:layout_marginTop="8dp"
54+
android:text="@string/nextRefreshTime"
55+
android:textAppearance="?attr/textAppearanceBodyMedium"
56+
android:textColor="?android:attr/textColorSecondary"
57+
android:textSize="16sp" />
58+
59+
</LinearLayout>
60+
61+
</com.google.android.material.card.MaterialCardView>
1762

1863
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<resources xmlns:tools="http://schemas.android.com/tools">
22
<!-- Base application theme. -->
3-
<style name="Theme.HttpSMS" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
3+
<style name="Theme.HttpSMS" parent="Theme.MaterialComponents.DayNight.NoActionBar">
44
<!-- Primary brand color. -->
55
<item name="colorPrimary">@color/purple_200</item>
66
<item name="colorPrimaryVariant">@color/purple_700</item>
@@ -10,7 +10,7 @@
1010
<item name="colorSecondaryVariant">@color/teal_200</item>
1111
<item name="colorOnSecondary">@color/black</item>
1212
<!-- Status bar color. -->
13-
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
13+
<item name="android:statusBarColor" tools:targetApi="l">#121212</item>
1414
<!-- Customize your theme here. -->
1515
</style>
1616
</resources>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
<resources>
22
<string name="app_name">HttpSMS</string>
3+
<string name="nextRefreshTime">23/06/2022 18:58:30</string>
4+
<string name="phone_number">+1 800 555 0100</string>
5+
<string name="menuIconDescription">Menu Icon</string>
6+
<string name="menuIconName">More</string>
37
</resources>

android/app/src/main/res/values/themes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<resources xmlns:tools="http://schemas.android.com/tools">
22
<!-- Base application theme. -->
3-
<style name="Theme.HttpSMS" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
3+
<style name="Theme.HttpSMS" parent="Theme.MaterialComponents.DayNight.NoActionBar">
44
<!-- Primary brand color. -->
55
<item name="colorPrimary">@color/purple_500</item>
66
<item name="colorPrimaryVariant">@color/purple_700</item>

0 commit comments

Comments
 (0)