From 0bb562b43db5a2a7a67b4be31f83d0113cec8e62 Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Tue, 3 Oct 2023 10:06:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E5=9C=A8=E9=80=89?= =?UTF-8?q?=E6=8B=A9Web=E5=AE=A2=E6=88=B7=E7=AB=AF=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E5=BC=B9=E5=87=BA=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E6=89=80=E6=9C=89=E6=96=87=E4=BB=B6=E8=AE=BF?= =?UTF-8?q?=E9=97=AE=E7=9A=84=E6=9D=83=E9=99=90=20#332?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sms/forwarder/fragment/ServerFragment.kt | 65 ++++++++++++------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/idormy/sms/forwarder/fragment/ServerFragment.kt b/app/src/main/java/com/idormy/sms/forwarder/fragment/ServerFragment.kt index 7ee890e6..e90b03b6 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/fragment/ServerFragment.kt +++ b/app/src/main/java/com/idormy/sms/forwarder/fragment/ServerFragment.kt @@ -1,5 +1,6 @@ package com.idormy.sms.forwarder.fragment +import android.annotation.SuppressLint import android.content.Intent import android.os.Environment import android.os.Handler @@ -308,30 +309,48 @@ class ServerFragment : BaseFragment(), View.OnClickListe XToastUtils.info(String.format(getString(R.string.copied_to_clipboard), url)) } R.id.btn_path_picker -> { - val downloadPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).path - val dirList = listSubDir(downloadPath) - if (dirList.isEmpty()) { - XToastUtils.error(String.format(getString(R.string.download_first), downloadPath)) - return - } - MaterialDialog.Builder(requireContext()).title(getString(R.string.select_web_client_directory)).content(String.format(getString(R.string.root_directory), downloadPath)).items(dirList).itemsCallbackSingleChoice(0) { _: MaterialDialog?, _: View?, _: Int, text: CharSequence -> - val webPath = "$downloadPath/$text" - binding!!.etWebPath.setText(webPath) - HttpServerUtils.serverWebPath = webPath - - XToastUtils.info(getString(R.string.restarting_httpserver)) - Intent(appContext, HttpService::class.java).also { - if (ServiceUtils.isServiceRunning("com.idormy.sms.forwarder.service.HttpService")) { - appContext?.stopService(it) - Thread.sleep(500) - appContext?.startService(it) - } else { - appContext?.startService(it) + // 申请储存权限 + XXPermissions.with(this) + .permission(Permission.MANAGE_EXTERNAL_STORAGE).request(object : OnPermissionCallback { + @SuppressLint("SetTextI18n") + override fun onGranted(permissions: List, all: Boolean) { + val downloadPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).path + val dirList = listSubDir(downloadPath) + if (dirList.isEmpty()) { + XToastUtils.error(String.format(getString(R.string.download_first), downloadPath)) + return + } + MaterialDialog.Builder(requireContext()).title(getString(R.string.select_web_client_directory)).content(String.format(getString(R.string.root_directory), downloadPath)).items(dirList).itemsCallbackSingleChoice(0) { _: MaterialDialog?, _: View?, _: Int, text: CharSequence -> + val webPath = "$downloadPath/$text" + binding!!.etWebPath.setText(webPath) + HttpServerUtils.serverWebPath = webPath + + XToastUtils.info(getString(R.string.restarting_httpserver)) + Intent(appContext, HttpService::class.java).also { + if (ServiceUtils.isServiceRunning("com.idormy.sms.forwarder.service.HttpService")) { + appContext?.stopService(it) + Thread.sleep(500) + appContext?.startService(it) + } else { + appContext?.startService(it) + } + } + refreshButtonText() + true // allow selection + }.positiveText(R.string.select).negativeText(R.string.cancel).show() } - } - refreshButtonText() - true // allow selection - }.positiveText(R.string.select).negativeText(R.string.cancel).show() + + override fun onDenied(permissions: List, never: Boolean) { + if (never) { + XToastUtils.error(R.string.toast_denied_never) + // 如果是被永久拒绝就跳转到应用权限系统设置页面 + XXPermissions.startPermissionActivity(requireContext(), permissions) + } else { + XToastUtils.error(R.string.toast_denied) + } + binding!!.etWebPath.setText(getString(R.string.storage_permission_tips)) + } + }) } else -> {} }