mirror of
https://codeberg.org/timelimit/timelimit-android.git
synced 2025-10-05 19:42:20 +02:00
Explain permissions before launching the system settings
This commit is contained in:
parent
18b31f2a45
commit
788d03249b
2 changed files with 152 additions and 47 deletions
|
@ -55,6 +55,7 @@ import io.timelimit.android.ui.lock.LockActivity
|
|||
import io.timelimit.android.ui.manage.device.manage.permission.AdbDeviceAdminDialogFragment
|
||||
import io.timelimit.android.ui.manage.device.manage.permission.AdbUsageStatsDialogFragment
|
||||
import io.timelimit.android.ui.manage.device.manage.permission.InformAboutDeviceOwnerDialogFragment
|
||||
import io.timelimit.android.ui.manage.device.manage.permission.PermissionInfoConfirmDialog
|
||||
import io.timelimit.android.ui.manipulation.AnnoyActivity
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.channels.consumeEach
|
||||
|
@ -648,7 +649,10 @@ class AndroidIntegration(context: Context): PlatformIntegration(maximumProtectio
|
|||
val protectionLevel = getCurrentProtectionLevel()
|
||||
|
||||
if (protectionLevel == ProtectionLevel.None) {
|
||||
if (
|
||||
if (confirmationLevel == SystemPermissionConfirmationLevel.None) {
|
||||
PermissionInfoConfirmDialog.newInstance(SystemPermission.DeviceAdmin)
|
||||
.show(activity.supportFragmentManager)
|
||||
} else if (
|
||||
InformAboutDeviceOwnerDialogFragment.shouldShow &&
|
||||
confirmationLevel != SystemPermissionConfirmationLevel.Suggestion
|
||||
) {
|
||||
|
@ -680,30 +684,95 @@ class AndroidIntegration(context: Context): PlatformIntegration(maximumProtectio
|
|||
true
|
||||
}
|
||||
SystemPermission.UsageStats -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
// According to user reports, some devices open the wrong screen
|
||||
// with the Settings.ACTION_USAGE_ACCESS_SETTINGS
|
||||
// but using an activity launcher to open this intent works for them.
|
||||
// This intent works at regular android too, so try this first
|
||||
// and use the "correct" one as fallback.
|
||||
|
||||
try {
|
||||
activity.startActivity(
|
||||
Intent()
|
||||
.setClassName("com.android.settings", "com.android.settings.Settings\$UsageAccessSettingsActivity")
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
)
|
||||
if (
|
||||
foregroundAppHelper.getPermissionStatus() == RuntimePermissionStatus.NotGranted &&
|
||||
confirmationLevel == SystemPermissionConfirmationLevel.None
|
||||
) {
|
||||
PermissionInfoConfirmDialog.newInstance(SystemPermission.UsageStats)
|
||||
.show(activity.supportFragmentManager)
|
||||
|
||||
true
|
||||
} catch (ex: Exception) {
|
||||
} else {
|
||||
// According to user reports, some devices open the wrong screen
|
||||
// with the Settings.ACTION_USAGE_ACCESS_SETTINGS
|
||||
// but using an activity launcher to open this intent works for them.
|
||||
// This intent works at regular android too, so try this first
|
||||
// and use the "correct" one as fallback.
|
||||
|
||||
try {
|
||||
activity.startActivity(
|
||||
Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
|
||||
Intent()
|
||||
.setClassName(
|
||||
"com.android.settings",
|
||||
"com.android.settings.Settings\$UsageAccessSettingsActivity"
|
||||
)
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
)
|
||||
|
||||
true
|
||||
} catch (ex: Exception) {
|
||||
AdbUsageStatsDialogFragment().show(activity.supportFragmentManager)
|
||||
try {
|
||||
activity.startActivity(
|
||||
Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
)
|
||||
|
||||
true
|
||||
} catch (ex: Exception) {
|
||||
AdbUsageStatsDialogFragment().show(activity.supportFragmentManager)
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(context, R.string.error_general, Toast.LENGTH_SHORT).show()
|
||||
|
||||
false
|
||||
}
|
||||
SystemPermission.Notification -> if (
|
||||
getNotificationAccessPermissionStatus() == NewPermissionStatus.NotGranted &&
|
||||
confirmationLevel == SystemPermissionConfirmationLevel.None
|
||||
) {
|
||||
PermissionInfoConfirmDialog.newInstance(SystemPermission.Notification)
|
||||
.show(activity.supportFragmentManager)
|
||||
|
||||
true
|
||||
} else {
|
||||
try {
|
||||
activity.startActivity(
|
||||
Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
)
|
||||
|
||||
true
|
||||
} catch (ex: Exception) {
|
||||
Toast.makeText(context, R.string.error_general, Toast.LENGTH_SHORT).show()
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
SystemPermission.Overlay -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
if (
|
||||
overlay.getOverlayPermissionStatus() == RuntimePermissionStatus.NotGranted &&
|
||||
confirmationLevel == SystemPermissionConfirmationLevel.None
|
||||
) {
|
||||
PermissionInfoConfirmDialog.newInstance(SystemPermission.Overlay)
|
||||
.show(activity.supportFragmentManager)
|
||||
|
||||
true
|
||||
} else {
|
||||
try {
|
||||
activity.startActivity(
|
||||
Intent(
|
||||
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
|
||||
Uri.parse("package:" + context!!.packageName)
|
||||
)
|
||||
)
|
||||
|
||||
true
|
||||
} catch (ex: Exception) {
|
||||
Toast.makeText(context, R.string.error_general, Toast.LENGTH_SHORT).show()
|
||||
|
||||
false
|
||||
}
|
||||
|
@ -713,25 +782,19 @@ class AndroidIntegration(context: Context): PlatformIntegration(maximumProtectio
|
|||
|
||||
false
|
||||
}
|
||||
SystemPermission.Notification -> try {
|
||||
activity.startActivity(
|
||||
Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
)
|
||||
SystemPermission.AccessibilityService -> if (
|
||||
!isAccessibilityServiceEnabled() &&
|
||||
confirmationLevel == SystemPermissionConfirmationLevel.None
|
||||
) {
|
||||
PermissionInfoConfirmDialog.newInstance(SystemPermission.AccessibilityService)
|
||||
.show(activity.supportFragmentManager)
|
||||
|
||||
true
|
||||
} catch (ex: Exception) {
|
||||
Toast.makeText(context, R.string.error_general, Toast.LENGTH_SHORT).show()
|
||||
|
||||
false
|
||||
}
|
||||
SystemPermission.Overlay -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
} else {
|
||||
try {
|
||||
activity.startActivity(
|
||||
Intent(
|
||||
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
|
||||
Uri.parse("package:" + context!!.packageName)
|
||||
)
|
||||
Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
)
|
||||
|
||||
true
|
||||
|
@ -740,22 +803,6 @@ class AndroidIntegration(context: Context): PlatformIntegration(maximumProtectio
|
|||
|
||||
false
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(context, R.string.error_general, Toast.LENGTH_SHORT).show()
|
||||
|
||||
false
|
||||
}
|
||||
SystemPermission.AccessibilityService -> try {
|
||||
activity.startActivity(
|
||||
Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
)
|
||||
|
||||
true
|
||||
} catch (ex: Exception) {
|
||||
Toast.makeText(context, R.string.error_general, Toast.LENGTH_SHORT).show()
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* TimeLimit Copyright <C> 2019 - 2021 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package io.timelimit.android.ui.manage.device.manage.permission
|
||||
|
||||
import android.app.Dialog
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import androidx.fragment.app.FragmentManager
|
||||
import io.timelimit.android.R
|
||||
import io.timelimit.android.extensions.showSafe
|
||||
import io.timelimit.android.integration.platform.SystemPermission
|
||||
import io.timelimit.android.integration.platform.SystemPermissionConfirmationLevel
|
||||
import io.timelimit.android.logic.DefaultAppLogic
|
||||
|
||||
class PermissionInfoConfirmDialog: DialogFragment() {
|
||||
companion object {
|
||||
private const val DIALOG_TAG = "PermissionInfoConfirmDialog"
|
||||
private const val EXTRA_PERMISSION = "permission"
|
||||
|
||||
fun newInstance(permission: SystemPermission) = PermissionInfoConfirmDialog().apply {
|
||||
arguments = Bundle().apply {
|
||||
putSerializable(EXTRA_PERMISSION, permission)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
val permission = requireArguments().getSerializable(EXTRA_PERMISSION) as SystemPermission
|
||||
val strings = PermissionInfoStrings.getFor(permission)
|
||||
|
||||
return AlertDialog.Builder(requireContext(), theme)
|
||||
.setTitle(strings.title)
|
||||
.setMessage(strings.text)
|
||||
.setNegativeButton(R.string.generic_cancel, null)
|
||||
.setPositiveButton(R.string.wiazrd_next) { _, _ ->
|
||||
DefaultAppLogic.with(requireContext()).platformIntegration.openSystemPermissionScren(
|
||||
requireActivity(), permission, SystemPermissionConfirmationLevel.PermissionInfo
|
||||
)
|
||||
}
|
||||
.create()
|
||||
}
|
||||
|
||||
fun show(fragmentManager: FragmentManager) = showSafe(fragmentManager, DIALOG_TAG)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue