Search code examples
androidxamarin.androidadbrootkiosk-mode

How to write a * .kl file and copy to / system on a rooted system


I have the following problem.

I need to lock my tablet for a specific app. I am using my application in KioskMode, however I need to block some buttons, "Switch_app", "Volume_UP", "Volume_DOWN", etc.

I was able to block these buttons by accessing the ES File Explorer and changing the file manually, saving and restarting the tablet.

However, I would like to change this file progammatically.

I've tried the following:

        {
            using (StreamReader sr = new StreamReader("/system/usr/keylayout/Generic.kl"))
            {
                string line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.Contains("VOLUME"))
                    {
                        line = $"# {line}";

                    }
                    text += line + "\n";
                    System.Console.WriteLine(text);
                }
            }
            CreateFile();

            TransferFile();
        };


    void CreateFile()
    {


        string sdCard = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
        string path = Path.Combine(sdCard, "MyFolder/Generic.kl");
        // This text is added only once to the file.
        if (!System.IO.File.Exists(path))
        {
            // Create a file to write to.
            System.IO.File.WriteAllText(path, text);
        }
    }

And to transfer the created file to / system / usr / Keylayout I use this:

Java.Lang.Runtime.GetRuntime().Exec("su -c mount -o rw,remount,rw /system");
Java.Lang.Runtime.GetRuntime().Exec("su -c rm system/usr/keylayout/Generic.kl");
Java.Lang.Runtime.GetRuntime().Exec("su -c mv /storage/emulated/0/MyFolder/Generic.kl system/usr/keylayout/Generic.kl");

When I use these commands, the file is copied, but when I restart the tablet no more physical buttons work. So I believe it's some problem related to deleting the old file and adding the new one.

Any help will be very welcome as well as ideas.

Thank you all.


Solution

  • There is no need to worry about ownership, permissions, etc. if you would use sed's in place editing:

    GetRuntime().Exec("su 0 mount -o remount,rw /system");
    GetRuntime().Exec("su 0 sed -i 's/^[^#]*VOLUME_DOWN/# &/' /system/usr/keylayout/Generic.kl");
    GetRuntime().Exec("su 0 sed -i 's/^[^#]*VOLUME_UP/# &/' /system/usr/keylayout/Generic.kl");
    GetRuntime().Exec("su 0 sed -i 's/^[^#]*APP_SWITCH/# &/' /system/usr/keylayout/Generic.kl");
    

    You still need to reboot though.