Nicolas Breuninger
Nicolas Breuninger

Reputation: 263

How can I lockdown a Flutter App to Kiosk App?

I recently started to move an Android App to Flutter. I enjoy the way Flutter Apps are coded. After 2 days the Flutter App had the same functionality as the Android App. The only feature missing is the lockdown. The Android App is running on corporate-owned devices and is device owner. The lockdown is achieved by the following methods:

    private void setDefaultCosuPolicies(boolean active) {
    // set user restrictions
    setUserRestriction(UserManager.DISALLOW_SAFE_BOOT, active);
    setUserRestriction(UserManager.DISALLOW_FACTORY_RESET, active);
    setUserRestriction(UserManager.DISALLOW_ADD_USER, active);
    setUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, active);
    setUserRestriction(UserManager.DISALLOW_ADJUST_VOLUME, active);

    // disable keyguard and status bar
    mDevicePolicyManager.setKeyguardDisabled(mAdminName, active);
    mDevicePolicyManager.setStatusBarDisabled(mAdminName, active);

    // enable STAY_ON_WHILE_PLUGGED_IN
    enableStayOnWhilePluggedIn(false);

    // set this Activity as a lock task package

    mDevicePolicyManager.setLockTaskPackages(mAdminName,
            active ? new String[]{getPackageName()} : new String[]{});

    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
    intentFilter.addCategory(Intent.CATEGORY_HOME);
    intentFilter.addCategory(Intent.CATEGORY_DEFAULT);

    if (active) {
      // set Cosu activity as home intent receiver so that it is started
      // on reboot
      mDevicePolicyManager.addPersistentPreferredActivity(
              mAdminName, intentFilter, new ComponentName(
                      getPackageName(), MainActivity.class.getName()));
    } else {
      mDevicePolicyManager.clearPackagePersistentPreferredActivities(
              mAdminName, getPackageName());
    }
  }

  private void setUserRestriction(String restriction, boolean disallow) {
    if (disallow) {
      mDevicePolicyManager.addUserRestriction(mAdminName,
              restriction);
    } else {
      mDevicePolicyManager.clearUserRestriction(mAdminName,
              restriction);
    }
  }

  private void enableStayOnWhilePluggedIn(boolean enabled) {
    if (enabled) {
      mDevicePolicyManager.setGlobalSetting(
              mAdminName,
              Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
              Battery_PLUGGED_ANY);
    } else {
      mDevicePolicyManager.setGlobalSetting(
              mAdminName,
              Settings.Global.STAY_ON_WHILE_PLUGGED_IN, DONT_STAY_ON);
    }
  }

In the MainActivitys onCreate function setDefaultCosuPolicies(true) was called. I copied these functions to the flutter app. Added the AdminReceiver and configured everything in the AndroidManifest. The first attempt didn't work so I tried to call the setDefaultCosuPolicies function through a MethodChannel. This also did not work.

Does anyone have any idea how to activate the kiosk functionality?

Thanks in advance

Upvotes: 18

Views: 3921

Answers (1)

Tasnuva Tavasum oshin
Tasnuva Tavasum oshin

Reputation: 4750

Add this Package in Project :

  kiosk_mode: ^0.2.1

Example:

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:kiosk_mode/kiosk_mode.dart';

void main() {
  runApp(const App());
}

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) => MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: const Center(child: _Home()),
        ),
      );
}

class _Home extends StatefulWidget {
  const _Home({
    Key? key,
  }) : super(key: key);

  @override
  State<_Home> createState() => _HomeState();
}

class _HomeState extends State<_Home> {
  late final Stream<KioskMode> _currentMode = watchKioskMode();

  @override
  Widget build(BuildContext context) => Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (Platform.isAndroid) ...[
            const MaterialButton(
              onPressed: startKioskMode,
              child: Text('Start Kiosk Mode'),
            ),
            const MaterialButton(
              onPressed: stopKioskMode,
              child: Text('Stop Kiosk Mode'),
            ),
          ],
          MaterialButton(
            onPressed: () => getKioskMode().then(
              (value) => ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(content: Text('Kiosk mode: $value')),
              ),
            ),
            child: const Text('Check mode'),
          ),
          StreamBuilder<KioskMode>(
            stream: _currentMode,
            builder: (context, snapshot) => Text(
              'Current mode: ${snapshot.data}',
            ),
          ),
        ],
      );
}

Upvotes: 1

Related Questions