Skip to content

Commit c37e484

Browse files
committed
Fix permissions checker
1 parent 2f3169c commit c37e484

3 files changed

Lines changed: 91 additions & 80 deletions

File tree

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

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ class MainActivity : AppCompatActivity() {
3434

3535
createChannel()
3636

37-
requestPermission(this, Manifest.permission.SEND_SMS)
38-
requestPermission(this, Manifest.permission.RECEIVE_SMS)
39-
requestPermission(this, READ_PHONE_NUMBERS)
40-
requestPermission(this, Manifest.permission.READ_PHONE_STATE)
41-
requestPermission(this, Manifest.permission.RECEIVE_SMS)
37+
requestPermissions(this)
4238

4339
setOwner(getPhoneNumber(this))
4440
setActiveStatus(this)
@@ -121,8 +117,35 @@ class MainActivity : AppCompatActivity() {
121117
val switch = findViewById<SwitchMaterial>(R.id.cardSwitch)
122118
switch.isChecked = Settings.getActiveStatus(context)
123119
switch.setOnCheckedChangeListener{
124-
_, isChecked -> Settings.setActiveStatusAsync(context, isChecked)
120+
_, isChecked ->
121+
run {
122+
if (isChecked && !hasAllPermissions(context)) {
123+
Toast.makeText(context, "PERMISSIONS_NOT_GRANTED", Toast.LENGTH_SHORT).show()
124+
} else {
125+
Settings.setActiveStatusAsync(context, isChecked)
126+
}
127+
}
128+
}
129+
}
130+
131+
private fun hasAllPermissions(context: Context): Boolean {
132+
if (ActivityCompat.checkSelfPermission(
133+
context,
134+
Manifest.permission.SEND_SMS
135+
) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
136+
context,
137+
READ_PHONE_NUMBERS
138+
) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
139+
context,
140+
Manifest.permission.RECEIVE_SMS
141+
) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
142+
context,
143+
Manifest.permission.READ_PHONE_STATE
144+
) == PackageManager.PERMISSION_GRANTED
145+
) {
146+
return true
125147
}
148+
return false
126149
}
127150

128151
private fun setOwner(phoneNumber: String) {
@@ -157,41 +180,35 @@ class MainActivity : AppCompatActivity() {
157180
Manifest.permission.READ_PHONE_STATE
158181
) != PackageManager.PERMISSION_GRANTED
159182
) {
160-
return "NO_PHONE_NUMBER"
183+
return Settings.getOwnerOrDefault(this)
161184
}
162185

163186
if (telephonyManager.line1Number != null) {
164187
Settings.setOwnerAsync(context, telephonyManager.line1Number)
165188
}
166189

167-
return telephonyManager.line1Number ?: "NO_PHONE_NUMBER"
190+
return telephonyManager.line1Number ?: Settings.getOwnerOrDefault(this)
168191
}
169192

170-
private fun requestPermission(context: Context, permission: String) {
171-
// Register the permissions callback, which handles the user's response to the
172-
// system permissions dialog. Save the return value, an instance of
173-
// ActivityResultLauncher. You can use either a val, as shown in this snippet,
174-
// or a late init var in your onAttach() or onCreate() method.
175-
val requestPermissionLauncher =
176-
registerForActivityResult(
177-
ActivityResultContracts.RequestPermission()
178-
) { isGranted: Boolean ->
179-
if (isGranted) {
180-
val toast = Toast.makeText(context, "Granted", Toast.LENGTH_SHORT)
181-
toast.show()
182-
} else {
183-
val toast = Toast.makeText(context, "NOT Granted", Toast.LENGTH_LONG)
184-
toast.show()
185-
}
193+
private fun requestPermissions(context:Context) {
194+
if(!Settings.isLoggedIn(context)) {
195+
return
196+
}
197+
198+
val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
199+
permissions.entries.forEach {
200+
Timber.d("${it.key} = ${it.value}")
201+
setOwner(getPhoneNumber(context))
186202
}
187-
if (ActivityCompat.checkSelfPermission(
188-
context,
189-
permission
190-
) != PackageManager.PERMISSION_GRANTED
191-
) {
192-
// You can directly ask for the permission.
193-
// The registered ActivityResultCallback gets the result of this request.
194-
requestPermissionLauncher.launch(permission)
195203
}
204+
205+
requestPermissionLauncher.launch(
206+
arrayOf(
207+
Manifest.permission.SEND_SMS,
208+
Manifest.permission.RECEIVE_SMS,
209+
READ_PHONE_NUMBERS,
210+
Manifest.permission.READ_PHONE_STATE
211+
)
212+
)
196213
}
197214
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ object Settings {
2525
return null
2626
}
2727

28-
Timber.d("owner: [$owner]")
28+
Timber.d("SETTINGS_OWNER: [$owner]")
2929
return owner
3030
}
3131

@@ -44,7 +44,7 @@ object Settings {
4444
.getDefaultSharedPreferences(context)
4545
.getLong(this.SETTINGS_FCM_TOKEN_UPDATE_TIMESTAMP,0)
4646

47-
Timber.d("active status: [$timestamp]")
47+
Timber.d("SETTINGS_FCM_TOKEN_UPDATE_TIMESTAMP: [$timestamp]")
4848
return timestamp
4949
}
5050

@@ -75,7 +75,7 @@ object Settings {
7575
.getDefaultSharedPreferences(context)
7676
.getBoolean(this.SETTINGS_ACTIVE,false)
7777

78-
Timber.d("active status: [$activeStatus]")
78+
Timber.d("SETTINGS_ACTIVE: [$activeStatus]")
7979
return activeStatus
8080
}
8181

@@ -99,7 +99,7 @@ object Settings {
9999
.getDefaultSharedPreferences(context)
100100
.getString(this.SETTINGS_API_KEY,null)
101101

102-
Timber.d("API_KEY: [$apiKey]")
102+
Timber.d("SETTINGS_API_KEY: [$apiKey]")
103103
return apiKey
104104
}
105105

@@ -123,7 +123,7 @@ object Settings {
123123
.getDefaultSharedPreferences(context)
124124
.getString(this.SETTINGS_FCM_TOKEN,null)
125125

126-
Timber.d("FCM_TOKEN: [$activeStatus]")
126+
Timber.d("SETTINGS_FCM_TOKEN: [$activeStatus]")
127127
return activeStatus
128128
}
129129

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

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,63 +21,57 @@
2121
tools:ignore="ImageContrastCheck" />
2222

2323
<com.google.android.material.card.MaterialCardView
24-
android:id="@+id/card"
24+
android:id="@+id/mainPhoneCard"
2525
android:layout_width="match_parent"
2626
android:layout_height="wrap_content"
2727
android:elevation="16dp"
28-
app:cardCornerRadius="16dp"
2928
app:layout_constraintEnd_toEndOf="parent"
3029
app:layout_constraintStart_toStartOf="parent"
31-
app:layout_constraintTop_toTopOf="parent">
32-
33-
</com.google.android.material.card.MaterialCardView>
34-
35-
<LinearLayout
36-
android:layout_width="match_parent"
37-
android:layout_height="wrap_content"
38-
android:layout_marginTop="32dp"
39-
android:orientation="vertical"
40-
android:padding="16dp"
41-
android:background="#1E1E1E"
42-
app:layout_constraintTop_toBottomOf="@+id/imageView"
43-
tools:layout_editor_absoluteX="0dp">
44-
45-
<!-- Title, secondary and supporting text -->
30+
app:layout_constraintTop_toBottomOf="@+id/imageView">
4631

4732
<LinearLayout
4833
android:layout_width="match_parent"
49-
android:layout_height="match_parent"
50-
android:orientation="horizontal">
34+
android:layout_height="wrap_content"
35+
android:orientation="vertical"
36+
android:padding="16dp">
5137

52-
<TextView
53-
android:id="@+id/cardPhoneNumber"
54-
android:layout_width="wrap_content"
55-
android:layout_height="match_parent"
56-
android:text="@string/phone_number"
57-
android:textAppearance="?attr/textAppearanceTitleMedium"
58-
android:textColor="?android:attr/textColorPrimary"
59-
android:textSize="28sp" />
38+
<!-- Title, secondary and supporting text -->
6039

61-
<com.google.android.material.switchmaterial.SwitchMaterial
62-
android:id="@+id/cardSwitch"
40+
<LinearLayout
6341
android:layout_width="match_parent"
64-
android:layout_height="wrap_content"
65-
android:minHeight="48dp"
66-
tools:ignore="TouchTargetSizeCheck" />
67-
</LinearLayout>
42+
android:layout_height="match_parent"
43+
android:orientation="horizontal">
6844

45+
<TextView
46+
android:id="@+id/cardPhoneNumber"
47+
android:layout_width="wrap_content"
48+
android:layout_height="match_parent"
49+
android:text="@string/phone_number"
50+
android:textAppearance="?attr/textAppearanceTitleMedium"
51+
android:textColor="?android:attr/textColorPrimary"
52+
android:textSize="28sp" />
6953

70-
<TextView
71-
android:id="@+id/cardRefreshTime"
72-
android:layout_width="wrap_content"
73-
android:layout_height="wrap_content"
74-
android:layout_marginTop="8dp"
75-
android:text="@string/nextRefreshTime"
76-
android:textAppearance="?attr/textAppearanceBodyMedium"
77-
android:textColor="?android:attr/textColorSecondary"
78-
android:textSize="16sp" />
54+
<com.google.android.material.switchmaterial.SwitchMaterial
55+
android:id="@+id/cardSwitch"
56+
android:layout_width="match_parent"
57+
android:layout_height="wrap_content"
58+
android:minHeight="48dp"
59+
tools:ignore="TouchTargetSizeCheck" />
60+
</LinearLayout>
61+
62+
63+
<TextView
64+
android:id="@+id/cardRefreshTime"
65+
android:layout_width="wrap_content"
66+
android:layout_height="wrap_content"
67+
android:layout_marginTop="8dp"
68+
android:text="@string/nextRefreshTime"
69+
android:textAppearance="?attr/textAppearanceBodyMedium"
70+
android:textColor="?android:attr/textColorSecondary"
71+
android:textSize="16sp" />
7972

80-
</LinearLayout>
73+
</LinearLayout>
74+
</com.google.android.material.card.MaterialCardView>
8175

8276
<com.google.android.material.button.MaterialButton
8377
android:id="@+id/mainLogoutButton"

0 commit comments

Comments
 (0)