Skip to content

Commit

Permalink
fix:permission
Browse files Browse the repository at this point in the history
  • Loading branch information
licoba committed May 16, 2023
1 parent ea17b25 commit 0f4b845
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 97 deletions.
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
tools:ignore="MissingLeanbackLauncher">

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<uses-permission android:name="com.fingerprints.service.ACCESS_FINGERPRINT_MANAGER" />
<uses-permission android:name="com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY" />



<!--Android 13版本适配,细化存储权限-->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.example.dibage.accountb.entitys.Photo
import com.example.dibage.accountb.utils.PhotoUtils
import com.example.dibage.accountb.utils.SimpleUtils
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.kongzue.dialogx.dialogs.PopMenu
import com.kongzue.dialogx.interfaces.OnIconChangeCallBack
Expand Down Expand Up @@ -130,8 +131,13 @@ class AddPhotoActivity() : AppCompatActivity(), View.OnClickListener {
onMenuItemClickListener =
OnMenuItemClickListener { dialog, text, index ->
when (index) {
0 -> { checkPermission(IMAGE) }
1 -> { checkPermission(CAMERA) }
0 -> {
checkPermission(IMAGE)
}

1 -> {
checkPermission(CAMERA)
}

}
false
Expand Down Expand Up @@ -226,43 +232,34 @@ class AddPhotoActivity() : AppCompatActivity(), View.OnClickListener {
*/
private fun checkPermission(condition: Int) {
if (condition == IMAGE) {
if (ContextCompat.checkSelfPermission(
(context)!!,
Manifest.permission.WRITE_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED
) { //权限没有被授予
ActivityCompat.requestPermissions(
this@AddPhotoActivity, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
IMAGE
)
} else { //权限被授予
//Toasty.success(context,"已经拥有权限").show();
val intent = Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
)
startActivityForResult(intent, IMAGE)
//直接操作
}
val permissions = arrayOf(Permission.READ_MEDIA_IMAGES)
XXPermissions.with((context)!!).permission(permissions)
.request(object : OnPermissionCallback {
override fun onGranted(permissions: List<String>, allGranted: Boolean) {
if (allGranted) openGallery()

}

override fun onDenied(permissions: List<String>, doNotAskAgain: Boolean) {
Toasty.info((context)!!, "获取权限失败,请授予相册访问权限")
XXPermissions.startPermissionActivity((context)!!, permissions)
}
})

} else if (condition == CAMERA) {
val permissions =
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
arrayOf( Manifest.permission.CAMERA)
XXPermissions.with((context)!!).permission(*permissions)
.request(object : OnPermissionCallback {
override fun onGranted(permissions: List<String>, allGranted: Boolean) {
if (allGranted == true) {
startCamera()
}
if (allGranted) startCamera()

}

override fun onDenied(permissions: List<String>, doNotAskAgain: Boolean) {
if (doNotAskAgain) {
Toasty.info((context)!!, "被永久拒绝授权,请手动授予权限")
// 如果是被永久拒绝就跳转到应用权限系统设置页面
XXPermissions.startPermissionActivity((context)!!, permissions)
} else {
Toasty.info((context)!!, "获取权限失败")
}
Toasty.info((context)!!, "获取权限失败,请手动授予权限")
// 如果是被永久拒绝就跳转到应用权限系统设置页面
XXPermissions.startPermissionActivity((context)!!, permissions)
}
})
}
Expand All @@ -284,6 +281,16 @@ class AddPhotoActivity() : AppCompatActivity(), View.OnClickListener {
startActivityForResult(intent, CAMERA) //开启相机
}


private fun openGallery() {
Toasty.info(context!!, "打开相册").show()
val intent = Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
)
startActivityForResult(intent, IMAGE)
}

//权限申请回调
override fun onRequestPermissionsResult(
requestCode: Int,
Expand All @@ -305,6 +312,7 @@ class AddPhotoActivity() : AppCompatActivity(), View.OnClickListener {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}


//处理相册回调
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Expand Down
106 changes: 50 additions & 56 deletions app/src/main/java/com/example/dibage/accountb/activity/MoreActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class MoreActivity : AppCompatActivity(), View.OnClickListener {

private var toolbar: Toolbar? = null
private var switch_finger: Switch? = null
private var mFingerprintIdentify: FingerprintIdentify? = null
lateinit var mFingerprintIdentify: FingerprintIdentify
private var ll_settingpassword: LinearLayout? = null
private var ll_developing: LinearLayout? = null
private var ll_create_backup: LinearLayout? = null
Expand All @@ -76,64 +76,15 @@ class MoreActivity : AppCompatActivity(), View.OnClickListener {
super.onCreate(savedInstanceState)
binding = ActivityMoreBinding.inflate(layoutInflater)
setContentView(binding.root)
mFingerprintIdentify = FingerprintIdentify(this)
mFingerprintIdentify.setSupportAndroidL(true); // support android L
mFingerprintIdentify.init(); // init
initFBI()
iniData()
initView()
initEvent()
}

inner class MyCheckListener : CompoundButton.OnCheckedChangeListener {
override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
if (isChecked) { //从关到开
// Toasty.success(context,"开").show();
if (!mFingerprintIdentify!!.isHardwareEnable) {
Toasty.info(this@MoreActivity, "您的设备不支持指纹识别,无法使用此功能").show()
} else if (!mFingerprintIdentify!!.isRegisteredFingerprint) {
Toasty.info(this@MoreActivity, "请在您的设备中录入指纹后再使用此功能").show()
} else {
Toasty.success(this@MoreActivity, "指纹解锁已启用", Toast.LENGTH_SHORT, false)
.show()
SPUtils.put(this@MoreActivity, "finger_state", true)
}
} else if (!isChecked) { //从开到关
//Toasty.success(context,"关").show();
popWindowTip = object : PopWindowTip(this@MoreActivity) {
override fun clickCancel() {
switch_finger!!.setOnCheckedChangeListener(null)
switch_finger!!.isChecked = true
switch_finger!!.setOnCheckedChangeListener(MyCheckListener())
}

override fun dismissTodo() {
if (SPUtils.get(this@MoreActivity, "finger_state", false) as Boolean) {
switch_finger!!.setOnCheckedChangeListener(null)
switch_finger!!.isChecked = true
switch_finger!!.setOnCheckedChangeListener(MyCheckListener())
}
}

override fun clickConfirm() {
if (SPUtils.get(this@MoreActivity, "finger_state", false) as Boolean) {
Toasty.success(
this@MoreActivity,
"指纹解锁已关闭",
Toast.LENGTH_SHORT,
false
).show()
SPUtils.put(this@MoreActivity, "finger_state", false)
// switch_finger.setOnCheckedChangeListener(null);
// switch_finger.setChecked(false);
// switch_finger.setOnCheckedChangeListener(new MyCheckListener());
}
}
}
popWindowTip!!.setTitleAndContent("指纹解锁", "确定关闭指纹解锁功能?")
popWindowTip!!.setOutside(false)
popWindowTip!!.update()
}
}
}

private fun initView() {
UIUtils.setToolbar(this@MoreActivity, toolbar, "功能与设置")
if (is_setting_pwd) {
Expand Down Expand Up @@ -162,8 +113,6 @@ class MoreActivity : AppCompatActivity(), View.OnClickListener {
ll_developing!!.setOnClickListener(this)
ll_create_backup!!.setOnClickListener(this)
ll_recovery!!.setOnClickListener(this)
mFingerprintIdentify = FingerprintIdentify(this)
switch_finger!!.setOnCheckedChangeListener(MyCheckListener())
ll_course!!.setOnClickListener(this)
ll_support!!.setOnClickListener(this)
ll_openource!!.setOnClickListener(this)
Expand All @@ -185,11 +134,56 @@ class MoreActivity : AppCompatActivity(), View.OnClickListener {
), BlendModeCompat.SRC_ATOP
)

binding.llFinger.setOnClickListener {
val isChecked = binding.switchFinger.isChecked
if (!isChecked) { //从关到开
if (!mFingerprintIdentify.isHardwareEnable) {
Toasty.info(this@MoreActivity, "您的设备不支持指纹识别,无法使用此功能").show()
} else if (!mFingerprintIdentify.isRegisteredFingerprint) {
Toasty.info(this@MoreActivity, "请在您的设备中录入指纹后再使用此功能").show()
} else {
Toasty.success(this@MoreActivity, "指纹解锁已启用", Toast.LENGTH_SHORT, false)
.show()
SPUtils.put(this@MoreActivity, "finger_state", true)
binding.switchFinger.isChecked = true
}
} else { //从开到关
popWindowTip = object : PopWindowTip(this@MoreActivity) {
override fun clickCancel() {
switch_finger!!.isChecked = true
}

override fun dismissTodo() {
if (SPUtils.get(this@MoreActivity, "finger_state", false) as Boolean) {
switch_finger!!.isChecked = true
}
}

override fun clickConfirm() {
if (SPUtils.get(this@MoreActivity, "finger_state", false) as Boolean) {
Toasty.success(
this@MoreActivity,
"指纹解锁已关闭",
Toast.LENGTH_SHORT,
false
).show()
SPUtils.put(this@MoreActivity, "finger_state", false)
switch_finger!!.isChecked = false;
}
}
}
popWindowTip!!.setTitleAndContent("指纹解锁", "确定关闭指纹解锁功能?")
popWindowTip!!.setOutside(false)
popWindowTip!!.update()
}
}


}

private fun initFBI() {
toolbar = findViewById(R.id.toolbar)
switch_finger = findViewById(R.id.switch_finger)
switch_finger = binding.switchFinger
ll_settingpassword = findViewById(R.id.ll_settingpassword)
tv_set_pwd = findViewById(R.id.tv_set_pwd)
btnShare = findViewById(R.id.btnShare)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected void onCreate(Bundle savedInstanceState) {
if (finger_state && is_setting_pwd) {
tv_tip.setText("请验证指纹或输入密码");
mFingerprintIdentify = new FingerprintIdentify(this); // 构造对象
mFingerprintIdentify.init();
mFingerprintIdentify.startIdentify(3, new mFingerprintIdentifyListener());// 开始验证指纹识别
} else if ((!finger_state) && is_setting_pwd) {
tv_tip.setText("请输入密码解锁");
Expand Down
Loading

0 comments on commit 0f4b845

Please sign in to comment.