4

I got this exception when I only show and dismiss any MaterialAlertDialog or DialogFragment, this exception causes that my app sometimes can't receive any response from my web service using Retrofit, I produce this exception when I enter and exit between Activities quickly. This is the stack trace:

2021-08-16 11:54:24.585 W/System.err: java.lang.SecurityException: mx.madison.madisonservice was not granted  this permission: android.permission.WRITE_SETTINGS.
2021-08-16 11:54:24.586 W/System.err:     at android.os.Parcel.createException(Parcel.java:2071)
2021-08-16 11:54:24.586 W/System.err:     at android.os.Parcel.readException(Parcel.java:2039)
2021-08-16 11:54:24.586 W/System.err:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
2021-08-16 11:54:24.586 W/System.err:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
2021-08-16 11:54:24.586 W/System.err:     at android.content.ContentProviderProxy.call(ContentProviderNative.java:658)
2021-08-16 11:54:24.586 W/System.err:     at android.provider.Settings$NameValueCache.putStringForUser(Settings.java:2295)
2021-08-16 11:54:24.586 W/System.err:     at android.provider.Settings$System.putStringForUser(Settings.java:2719)
2021-08-16 11:54:24.586 W/System.err:     at android.provider.Settings$System.putIntForUser(Settings.java:2827)
2021-08-16 11:54:24.586 W/System.err:     at android.provider.Settings$System.putInt(Settings.java:2820)
2021-08-16 11:54:24.586 W/System.err:     at android.app.Dialog.show(Dialog.java:355)
2021-08-16 11:54:24.586 W/System.err:     at mx.madison.madisonservice.presenter.TablePlanPresenter.showSimbology(TablePlanPresenter.kt:93)
2021-08-16 11:54:24.586 W/System.err:     at mx.madison.madisonservice.activities.TablePlanActivity.updateInfo(TablePlanActivity.kt:216)
2021-08-16 11:54:24.586 W/System.err:     at mx.madison.madisonservice.activities.TablePlanActivity.onOptionsItemSelected(TablePlanActivity.kt:198)
2021-08-16 11:54:24.586 W/System.err:     at android.app.Activity.onMenuItemSelected(Activity.java:4208)
2021-08-16 11:54:24.586 W/System.err:     at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:340)
2021-08-16 11:54:24.586 W/System.err:     at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:228)
2021-08-16 11:54:24.586 W/System.err:     at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
2021-08-16 11:54:24.586 W/System.err:     at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
2021-08-16 11:54:24.586 W/System.err:     at androidx.appcompat.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:65)
2021-08-16 11:54:24.586 W/System.err:     at androidx.appcompat.widget.Toolbar$1.onMenuItemClick(Toolbar.java:207)
2021-08-16 11:54:24.586 W/System.err:     at androidx.appcompat.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:779)
2021-08-16 11:54:24.586 W/System.err:     at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
2021-08-16 11:54:24.586 W/System.err:     at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
2021-08-16 11:54:24.586 W/System.err:     at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
2021-08-16 11:54:24.586 W/System.err:     at androidx.appcompat.view.menu.MenuPopup.onItemClick(MenuPopup.java:128)
2021-08-16 11:54:24.586 W/System.err:     at android.widget.AdapterView.performItemClick(AdapterView.java:330)
2021-08-16 11:54:24.586 W/System.err:     at android.widget.AbsListView.performItemClick(AbsListView.java:1190)
2021-08-16 11:54:24.586 W/System.err:     at android.widget.AbsListView$PerformClick.run(AbsListView.java:3198)
2021-08-16 11:54:24.586 W/System.err:     at android.widget.AbsListView$3.run(AbsListView.java:4116)
2021-08-16 11:54:24.586 W/System.err:     at android.os.Handler.handleCallback(Handler.java:883)
2021-08-16 11:54:24.586 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:100)
2021-08-16 11:54:24.586 W/System.err:     at android.os.Looper.loop(Looper.java:214)
2021-08-16 11:54:24.586 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7405)
2021-08-16 11:54:24.586 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2021-08-16 11:54:24.587 W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502)
2021-08-16 11:54:24.587 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
2021-08-16 11:54:24.587 W/System.err: Caused by: android.os.RemoteException: Remote stack trace:
2021-08-16 11:54:24.587 W/System.err:     at android.provider.Settings.isCallingPackageAllowedToPerformAppOpsProtectedOperation(Settings.java:15704)
2021-08-16 11:54:24.587 W/System.err:     at android.provider.Settings.checkAndNoteWriteSettingsOperation(Settings.java:15584)
2021-08-16 11:54:24.587 W/System.err:     at com.android.providers.settings.SettingsProvider.mutateSystemSetting(SettingsProvider.java:1699)
2021-08-16 11:54:24.587 W/System.err:     at com.android.providers.settings.SettingsProvider.insertSystemSetting(SettingsProvider.java:1674)
2021-08-16 11:54:24.587 W/System.err:     at com.android.providers.settings.SettingsProvider.call(SettingsProvider.java:443)

The line that produce the exception is the dismiss dialog call

fun showSimbology() {
        view?.getCtx()?.let {
            val alert = MaterialAlertDialogBuilder(it)
                .setIcon(R.drawable.ic_tables)
                .setTitle("Simbología de mesas")
                .setView(R.layout.simbology_dialog)
                .setNegativeButton("Cerrar") { dialog, _ -> dialog.dismiss() }
                .create()

            alert.show()
        }
    }

When I enter to any activity, my app call to my web service, I think that the real problem is the enqueue calls that not finished, The strange is that this exception only is produced when any Dialog is open and is closed, and quickly go to another activity. Any suggestion? Regards!

leoLR
  • 462
  • 2
  • 6
  • 21
  • 1
    Are you running this on an emulator or a physical device? Which Android version is this? Have you tested this on more than one device? – Mike M. Aug 16 '21 at 17:09
  • 1
    I tested on physical device: Device: Lenovo Tab E8 (The error is produced in those devices) Android version: 10 Also tested on Lenovo Tab P10 Android version: 10 In this device runs well – leoLR Aug 16 '21 at 17:13
  • 2
    That looks like the manufacturer has modified the `Dialog` class to incorporate some sort of security or permissions check. There's no reason for a regular `Dialog` to be accessing `Settings.System`, and I can't find any version that even imports the classes necessary to do that. I'm not sure that there's much you could do about it programmatically, though someone else might have some thoughts. There might be some user setting that could affect that, but I'm not familiar with that manufacturer or model. – Mike M. Aug 16 '21 at 17:17
  • 1
    So, I could try request this permission? I read that this is a especial system permission. But I cannot find some example for request this :( – leoLR Aug 16 '21 at 17:23
  • 2
    Yeah, I think you can still get that one, but you have to jump through an extra hoop: https://stackoverflow.com/a/32083622. That might be a bit of overkill, though, if there's only one device with the issue. – Mike M. Aug 16 '21 at 17:26
  • 2
    Also with Lenovo Tab M8 with android 10. Seems to be a Lenovo bug, you can ignore these warnings. – fireb86 Mar 14 '22 at 16:24

1 Answers1

4

It looks that's solved!, thanks @Mike M. This is my code for request WRITE_SETTINGS permission: On Manifest file add:

<uses-permission android:name="android.permission.WRITE_SETTINGS"
        tools:ignore="ProtectedPermissions" /> 

And I add this function in my code for request the permission:

 fun checkSystemWriteSettings(ctx: Context, onGranted: (Boolean) -> Unit) {
            if (!Settings.System.canWrite(ctx)) {
                val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + ctx.packageName))
                ctx.startActivity(intent)
            } else {
                onGranted(true)
            }
        }
leoLR
  • 462
  • 2
  • 6
  • 21