2

I am trying to develop a fullscreen mode app. Now I have set my app as immersive mode that hides every time the status bar and navigation bar, and only shows them when user made a swipe gesture in top/bottom edge of the screen

I think is not possible to disable permanently the status bar and navigation bar but it would be a great solution if I hide as immersive mode and disable all three buttons of navigation bar (Home button, back button and task manager button) although they are showing

NOTE: I am developing this app only for Android P and I only need a solution for this version.

To set app in immersive mode I have added to my AndroidManifest.xml

<intent-filter>
   <action android:name="android.intent.action.MAIN" />
   <category android:name="android.intent.category.HOME"/>
   <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

And in my MainActivity.kt:

window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_INMERSIVE_STICKY)

I have been trying to access to UI listener to avoid gestures but failed

window.decorView.setOnSystemUiVisibilityChangeListener {
  //For example hide action
  actionBar.hide()
}

and I don't know how to access to navigation bar buttons and disable their actions, also I want to avoid to swipe from top edge and show notifications manager view.

Please some help and suggestions? Thanks in advance!

UPDATE 09/18/19

For this I have implemented a kiosk mode in my application.

I made a Launcher Activity that configure all needed for the application and if everything goes well the app run but if not, I show a message warning.

There are two pre-conditions very important for that:

1. Device shouldn't have any account in the device (Google account, Samsung account, etc)

2. The app should be owner and admin of the device (I will explain later)

STEPS

1. Implement receiver in AndroidManifest

<receiver
                android:name=".security.CustomDeviceAdminReceiver"
                android:label="@string/app_name"
                android:permission="android.permission.BIND_DEVICE_ADMIN">
                <meta-data
                    android:name="android.app.device_admin"
                    android:resource="@xml/device_admin_receiver" />
                <intent-filter>
                    <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
                </intent-filter>
            </receiver>

2. Implement CustomDeviceAdminReceiver

    class CustomDeviceAdminReceiver: DeviceAdminReceiver() {
        companion object {
            fun getComponentName(context: Context): ComponentName {
                return ComponentName(context.applicationContext, CustomDeviceAdminReceiver::class.java)
            }
        }
    }

3. Create xml folder and a xml file inside it called device_admin_receiver

<?xml version="1.0" encoding="utf-8"?>
<device-admin>
    <uses-policies>
        <limit-password />
        <watch-login />
        <reset-password />
        <force-lock />
        <wipe-data />
        <expire-password />
        <encrypted-storage />
        <disable-camera />
    </uses-policies>
</device-admin>

4. In Launcher Activity do this:


class LauncherActivity : BaseActivity() {

        private var adminComponentName: ComponentName
        private var devicePolicyManager: DevicePolicyManager

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.launcher_activity)

            adminComponentName = CustomDeviceAdminReceiver.getComponentName(this)
            devicePolicyManager = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
        }

        override fun onResume() {
            super.onResume()
            initKioskMode()
        }

        private fun initKioskMode() {
            setUserRestriction(UserManager.DISALLOW_SAFE_BOOT)
            setUserRestriction(UserManager.DISALLOW_FACTORY_RESET)
            setUserRestriction(UserManager.DISALLOW_ADD_USER)
            setUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA)

            setKeyGuardEnabled()
        }

        private fun setUserRestriction(restriction: String) = devicePolicyManager.addUserRestriction(adminComponentName, restriction)

        private fun setKeyGuardEnabled() {
            devicePolicyManager.setKeyguardDisabled(adminComponentName, false)
            setLockTask()
        }

        private fun setLockTask() {
            devicePolicyManager.setLockTaskPackages(adminComponentName, arrayOf(packageName))
            startLockTask()
            startActivity(Intent(this, MainActivity::class.java))
        }

        private fun checkAppDeviceOwnerStatus() {
            if (isAppDeviceOwner() && isAppDeviceActiveAdmin()) initKioskMode()
            else {
                stopLockTask()
                showWarningAdvices()
            }
        }

        private fun isAppDeviceOwner() = devicePolicyManager.isDeviceOwnerApp(packageName)

        private fun isAppDeviceActiveAdmin() = devicePolicyManager.isAdminActive(adminComponentName)

        private fun showWarningAdvices() {
            adminWarningTitle.visibility = View.VISIBLE
            adminWarningMessage.visibility = View.VISIBLE
        }
}

5. Made my Activity as Main and Launcher

<activity
        android:name=".screen.main.LauncherActivity"
        android:screenOrientation="portrait">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER"/>
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

6. And finally you have to set up your app as device owner and admin device app

This is made by command line with this ->

adb shell dpm set-device-owner packageName/path to Admin device file

PD: To delete owner profile from device to have to run in terminal:

adb shell dpm remove-active-admin packageName/path to Admin device file 

Aaaaaand that's all!!

Sergio
  • 725
  • 1
  • 7
  • 20

0 Answers0