Android 12 SCHEDULE_EXACT_ALARM permission not applied in new process

Viewed 767

Hy guys

Situation

I have been updated MyApp to target Android 12 and ran into some odd behavior with the new SCHEDULE_EXACT_ALARM permission.

I have following problem with the new introduced permission

In my AndroidManifest.xml I declared the permission as following:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.myfirm.myapp">
          .... more permissions
          <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
          <application>
          ....
          </application>

           <service
            android:name="com.myfirm..services.KernelStartedService"
            android:enabled="true"
            android:exported="true"
            android:process=":KernelStartedService">
            <intent-filter>
                <action android:name="com.myfirm.service.kernel.api.ANDROID_KERNEL_SERVICE_BINDING_ACTION" />
            </intent-filter>
        </service>

</manifest>

As you can see in the declaration the value of the process attributes starts with a colon (:)
this means a new process, private to the application, is created when it's needed and the service runs in that process.

Before Starting or binding this service I invoke a test if the permission is given with this helper function:

private fun canExactAlarmsBeScheduled(): Boolean {
        val alarmManager = this.getSystemService(ALARM_SERVICE) as AlarmManager
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            alarmManager.canScheduleExactAlarms()
        } else {
            true // below API it can always be scheduled
        }
    }

In the onStart Method of the first Activity I then do a test if the permission is givien, if not, I provoke showing a dialog to the user that he needs to give this permission to my app:

override fun onStart() {

            // non relevant code here
            if (canExactAlarmsBeScheduled()) {
                initApplication() // here the service will be started/ bound
            } else {
                // start System Settings to enable alarms, coming back to Myapp will
                // provoke an onStart again
                AlertDialog.Builder(this)
                    .setMessage(getString(R.string.bootstrap_need_permission_to_schedule_alarms,getString(R.string.app_name)))
                    .setPositiveButton(getString(R.string.dialog_ok)) { _: DialogInterface, _: Int ->
                        intent =  Intent(ACTION_REQUEST_SCHEDULE_EXACT_ALARM).apply {
                            data = Uri.fromParts(PACKAGE_SCHEME, packageName, null)
                            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        }
                        startActivity(intent)
                    }
                    .show()
            }
        }
}

This above is working well for the MyApp.

Problem:

I do ensure that only if the Permission of Scheduling exact alarms is given, the above mentioned Service "KernelStartedService" will be started in a new process. The already granted permission is not taken by the new process in which the service is running in

If later on in classes that are running in this dedicated Process are testing alarmManager.canScheduleExactAlarms(). Then it ALWAYS is returning false!

Question:

How can I achieve to take over the permission SCHEDULE_EXACT_ALARM also for the process called ":KernelStartedService"? The other permissions like...

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

...are all respected also for this new process. Why is the declaration

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/> 

not respected in the separate process ":KernelStartedService"

As far as I see, this is a bug of Android 12 (API31 -App) when it creates the Process not applying this permission to the new process.

Expected behavior

I would expect that if the permissions are granted for the app-process, and afterwards I start a service in separate process, that all permissions would be applied to the new created process.

Thanks in advance for any help!

0 Answers
Related