Skip to content

Commit

Permalink
🔒 Fix permission
Browse files Browse the repository at this point in the history
  • Loading branch information
yhs0602 committed Oct 18, 2020
1 parent 4608894 commit 9d65dea
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
implementation "androidx.activity:activity:1.1.0"
implementation "androidx.activity:activity-ktx:1.2.0-beta01"
implementation 'androidx.fragment:fragment-ktx:1.3.0-beta01'
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@

<application
android:debuggable="false"
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/Theme.AppCompat">
<activity android:name=".ChooseDeckActivity"
android:screenOrientation="landscape"></activity>
<activity android:name=".ChooseGuestActivity"
Expand Down
51 changes: 48 additions & 3 deletions app/src/main/java/sma/rhythmtapper/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
package sma.rhythmtapper

import android.app.Activity
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File

class MainActivity : Activity() {
class MainActivity : AppCompatActivity() {
val requestPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
// Permission is granted. Continue the action or workflow in your
// app.
} else {
// Explain to the user that the feature is unavailable because the
// features requires a permission that the user has denied. At the
// same time, respect the user's decision. Don't link to system
// settings in an effort to convince the user to change their
// decision.
}
}

val sdcardFile = Environment.getExternalStorageDirectory()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
main_btn_start.setOnClickListener {
var text = etPath.text.toString()
if (text.isEmpty()) text = "/sdcard/Android/data/com.nomansland.tempestwave/files/"
if (text.isEmpty()) text = File(sdcardFile, "Android/data/com.nomansland.tempestwave/files/").canonicalPath
val i = Intent(this@MainActivity, DifficultySelectionActivity::class.java)
i.putExtra("path", text)
this@MainActivity.startActivity(i)
Expand All @@ -24,7 +47,29 @@ class MainActivity : Activity() {
val i = Intent(this@MainActivity, AboutActivity::class.java)
this@MainActivity.startActivity(i)
}
when {
ContextCompat.checkSelfPermission(
applicationContext,
Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED -> {
// You can use the API that requires the permission.
}
else -> {
// You can directly ask for the permission.
// The registered ActivityResultCallback gets the result of this request.
requestPermissionLauncher.launch(
Manifest.permission.READ_EXTERNAL_STORAGE)
}
}

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
if (!Environment.isExternalStorageManager()) {
startActivity(Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION))
}
} else {
// TODO("VERSION.SDK_INT < R")

}
// requestPermission
}
}

0 comments on commit 9d65dea

Please sign in to comment.