Skip to content

Commit c4ffde9

Browse files
committed
WIP ask for permission on runtime
1 parent 6536e31 commit c4ffde9

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed

app/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ dependencies {
4949
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
5050
implementation 'com.github.zagum:Android-SwitchIcon:1.4.0'
5151
implementation 'androidx.fragment:fragment-ktx:1.8.6'
52+
implementation 'com.github.cesarferreira:MaterialComponent.Banner:0.14.0'
5253

5354
}

app/src/main/AndroidManifest.xml

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
33

4+
<uses-feature
5+
android:name="android.hardware.telephony"
6+
android:required="false" />
7+
48
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
59
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
610
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />

app/src/main/java/com/saulnunez/help/HelpMain.kt

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
package com.saulnunez.help
22

3+
import android.content.pm.PackageManager
34
import androidx.appcompat.app.AppCompatActivity
45
import android.os.Bundle
6+
import androidx.activity.result.contract.ActivityResultContracts
7+
import androidx.core.content.ContextCompat
58
import androidx.fragment.app.commit
69
import androidx.fragment.app.replace
710
import com.saulnunez.help.databinding.ActivityHelpMainBinding
811

912
class HelpMain : AppCompatActivity() {
13+
private val requestedPermissions = listOf(
14+
android.Manifest.permission.ACCESS_FINE_LOCATION,
15+
android.Manifest.permission.SEND_SMS)
16+
1017
private lateinit var binding: ActivityHelpMainBinding
1118
override fun onCreate(savedInstanceState: Bundle?) {
1219
super.onCreate(savedInstanceState)
1320

1421
binding = ActivityHelpMainBinding.inflate(layoutInflater)
1522
setContentView(binding.root)
1623

24+
setSupportActionBar(binding.topAppBar)
25+
1726
binding.bottomNavigation.setOnItemSelectedListener {
1827
when(it.itemId){
1928
R.id.sound_page -> {
@@ -34,6 +43,36 @@ class HelpMain : AppCompatActivity() {
3443
}
3544
}
3645

37-
setSupportActionBar(binding.topAppBar)
46+
val requestPermissionLauncher =
47+
registerForActivityResult(
48+
ActivityResultContracts.RequestMultiplePermissions()
49+
) { map: Map<String, @JvmSuppressWildcards Boolean> ->
50+
if (map.containsValue(false)) {
51+
// Explain to the user that the feature is unavailable because the
52+
// feature requires a permission that the user has denied. At the
53+
// same time, respect the user's decision. Don't link to system
54+
// settings in an effort to convince the user to change their
55+
// decision.
56+
}
57+
}
58+
59+
if(hasAllPermissions()){
60+
binding.banner.dismiss()
61+
}else{
62+
binding.banner.show()
63+
}
64+
65+
binding.banner.setLeftButtonAction { binding.banner.dismiss() }
66+
binding.banner.setRightButtonAction {
67+
requestPermissionLauncher.launch(
68+
requestedPermissions.toTypedArray()
69+
)
70+
binding.banner.dismiss()
71+
}
72+
}
73+
74+
private fun hasAllPermissions(): Boolean {
75+
return requestedPermissions
76+
.all { ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED }
3877
}
3978
}

app/src/main/res/layout/activity_help_main.xml

+16-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,24 @@
99
<com.google.android.material.appbar.MaterialToolbar
1010
android:id="@+id/topAppBar"
1111
android:layout_width="match_parent"
12-
android:layout_height="?attr/actionBarSize"
12+
android:layout_height="wrap_content"
13+
android:background="?attr/colorSurface"
1314
app:layout_constraintTop_toTopOf="parent"
1415
app:layout_constraintEnd_toEndOf="parent"
1516
app:layout_constraintStart_toStartOf="parent" />
1617

18+
<com.cesar.materialcomponents.Banner
19+
android:id="@+id/banner"
20+
android:layout_width="match_parent"
21+
android:layout_height="wrap_content"
22+
app:layout_constraintTop_toBottomOf="@id/topAppBar"
23+
app:bannerBackgroundColor="?attr/colorSurface"
24+
app:buttonsTextColor="@color/md_theme_light_primary"
25+
app:contentText="@string/not_all_permissions_set"
26+
app:contentTextColor="@color/md_theme_light_onBackground"
27+
app:leftButtonText="@string/ignore"
28+
app:rightButtonText="@string/fix" />
29+
1730
<androidx.fragment.app.FragmentContainerView
1831
android:id="@+id/fragment_container_view"
1932
android:name="com.saulnunez.help.SoundFragment"
@@ -22,11 +35,11 @@
2235
app:layout_constraintBottom_toBottomOf="@id/bottom_navigation"
2336
app:layout_constraintEnd_toEndOf="parent"
2437
app:layout_constraintStart_toStartOf="parent"
25-
app:layout_constraintTop_toBottomOf="@id/topAppBar" />
38+
app:layout_constraintTop_toBottomOf="@id/banner" />
2639

2740
<com.google.android.material.bottomnavigation.BottomNavigationView
2841
android:id="@+id/bottom_navigation"
29-
android:layout_width="0dp"
42+
android:layout_width="match_parent"
3043
android:layout_height="wrap_content"
3144
app:layout_constraintBottom_toBottomOf="parent"
3245
app:layout_constraintEnd_toEndOf="parent"

app/src/main/res/values/strings.xml

+3
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@
1616
<string name="sixty_minutes">60 minutes</string>
1717
<string name="ninety_minutes">90 minutes</string>
1818
<string name="settings">Settings</string>
19+
<string name="not_all_permissions_set">Some permissions have not been granted. Without them Help will not be able to provide full functionality.</string>
20+
<string name="fix">Fix</string>
21+
<string name="ignore">Ignore</string>
1922
</resources>

0 commit comments

Comments
 (0)