diff --git a/app/build.gradle b/app/build.gradle index 643e826..00c0db2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,8 +12,8 @@ android { defaultConfig { applicationId "cn.salesuite.saf.kotlin" - minSdkVersion cfg.minSdk - targetSdkVersion cfg.targetSdk + minSdkVersion 30 + targetSdkVersion 33 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 83cbfca..23ff730 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,7 @@ @@ -64,8 +65,13 @@ - - + + + + + + + diff --git a/app/src/main/java/cn/salesuite/saf/kotlin/activity/FilePrinterActivity.kt b/app/src/main/java/cn/salesuite/saf/kotlin/activity/FilePrinterActivity.kt index a95cf72..abd7f36 100644 --- a/app/src/main/java/cn/salesuite/saf/kotlin/activity/FilePrinterActivity.kt +++ b/app/src/main/java/cn/salesuite/saf/kotlin/activity/FilePrinterActivity.kt @@ -2,8 +2,13 @@ package cn.salesuite.saf.kotlin.activity import android.Manifest import android.app.Activity +import android.content.Context +import android.content.Intent import android.content.pm.PackageManager +import android.os.Build import android.os.Bundle +import android.os.Environment +import android.provider.Settings import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import cn.salesuite.saf.kotlin.domain.User @@ -11,6 +16,7 @@ import com.safframework.log.L import com.safframework.log.printer.FilePrinter import com.safframework.log.printer.file.FileBuilder + /** * * @FileName: @@ -22,16 +28,29 @@ import com.safframework.log.printer.file.FileBuilder class FilePrinterActivity : Activity() { private lateinit var filePrinter:FilePrinter + private val REQUEST_CODE = 0 + + public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) - if (permissionCheck != PackageManager.PERMISSION_GRANTED) { - ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 0) - } else { - filePrinter = FileBuilder().folderPath("/storage/emulated/0/logs").build() - L.addPrinter(filePrinter); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {// Android 13 及以上 + + if (!Environment.isExternalStorageManager()) { + val intent = Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION) + startActivityForResult(intent, REQUEST_CODE) + } else { + filePrinter = FileBuilder().folderPath("/storage/emulated/0/logs").build() + L.addPrinter(filePrinter) + } + } else { // Android 12 及以下 + if (hasStoragePermissions(this)) { + ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_CODE) + } else { + filePrinter = FileBuilder().folderPath("/storage/emulated/0/logs").build() + L.addPrinter(filePrinter) + } } L.i("111321frehtyjuyikuloil'0[xwrgrtehcytbk8ynfggrgr4hytj") @@ -56,10 +75,38 @@ class FilePrinterActivity : Activity() { L.json(map3) } + /** + * 权限检查 + * @param context + * @return + */ + private fun hasStoragePermissions(context: Context): Boolean { + //版本判断,如果比android 13 就走正常的权限获取 + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { + val readPermission = ContextCompat.checkSelfPermission( + context, + Manifest.permission.READ_EXTERNAL_STORAGE + ) + val writePermission = ContextCompat.checkSelfPermission( + context, + Manifest.permission.WRITE_EXTERNAL_STORAGE + ) + return readPermission == PackageManager.PERMISSION_GRANTED && writePermission == PackageManager.PERMISSION_GRANTED + } else { + val audioPermission = + ContextCompat.checkSelfPermission(context, Manifest.permission.READ_MEDIA_AUDIO) + val imagePermission = + ContextCompat.checkSelfPermission(context, Manifest.permission.READ_MEDIA_IMAGES) + val videoPermission = + ContextCompat.checkSelfPermission(context, Manifest.permission.READ_MEDIA_VIDEO) + return audioPermission == PackageManager.PERMISSION_GRANTED && imagePermission == PackageManager.PERMISSION_GRANTED && videoPermission == PackageManager.PERMISSION_GRANTED + } + } + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { if (requestCode==0) { - filePrinter = FileBuilder().folderPath("/storage/emulated/0/logs").build() L.addPrinter(filePrinter) } diff --git a/build.gradle b/build.gradle index ce63b44..35ba774 100644 --- a/build.gradle +++ b/build.gradle @@ -1,10 +1,10 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. ext { configuration = [ - buildToolsVersion: "29.0.2", - compileVersion : 29, + buildToolsVersion: "30.0.3", + compileVersion : 33, minSdk : 15, - targetSdk : 29 + targetSdk : 33 ] libraries = [ diff --git a/gradle.properties b/gradle.properties index c79c847..af6dcbe 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,4 +15,5 @@ org.gradle.jvmargs=-Xmx1536m # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true -android.useAndroidX=true \ No newline at end of file +android.useAndroidX=true +android.enableJetifier=true \ No newline at end of file