Restricting the operation of some Android buttons

0

I've been trying for a few days, some alternatives to locking the buttons on a tablet, and then I need them to work again. I'll show you some ways I've done and which one is working the best possible way so far.

This was the first attempt, however after a few days using it began to appear permission error to Read the file inside the system / usr folder, being that at the beginning it worked normally.

string sdCard = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
            string path = Path.Combine(sdCard, "MyFolder/Generic.kl");

            using (StreamReader sr = new StreamReader(path))
            using (StreamReader sr = new StreamReader("/system/usr/keylayout/gpio-keys.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.Contains("HOME")) || (line.Contains("POWER")))
                    {
                        line = $"# {line}";
                    }
                    if (line.Contains("HOME"))
                    {
                        line = $"# {line}";
                    }
                    if (line.Contains("SWITCH"))
                    {
                        line = $"# {line}";
                    }
                    if (line.Contains("VOLUME"))
                    {
                        line = $"# {line}";
                    }
                    text += line + "\n";
                }
            }
            Java.Lang.Runtime.GetRuntime().Exec("su -c rm /storage/emulated/0/MyFolder/Generic.kl");

            CreateFile();

            TransferFile();




        void CreateFile()
    {
        string sdCard = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
        string path = Path.Combine(sdCard, "backups/gpio-keys.kll");
        // 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);
        }
    }





        void TransferFile()
    {
        bool rootaccess = ExecuteAsRootBase.canRunRootCommands();
        //button.Text = rootaccess.ToString();
        if (rootaccess)
        {
            Java.Lang.Runtime.GetRuntime().Exec("su -c mount -o rw,remount,rw /system");
            Java.Lang.Runtime.GetRuntime().Exec("su -c rm system/usr/keylayout/gpio-keys.kl");
            Java.Lang.Runtime.GetRuntime().Exec("su -c mv /storage/emulated/0/backups/gpio-keys.kl system/usr/keylayout/");
            Java.Lang.Runtime.GetRuntime().Exec("su -c chmod 644 /system/usr/keylayout/gpio-keys.kl");
            Java.Lang.Runtime.GetRuntime().Exec("su -c chown system.system /system/usr/keylayout/gpio-keys.kl");
        }
    }

As I said earlier, this solution worked normally for a period of time, then denied access appeared. To leave the buttons working I used the following command, with the same logic:

if (line.Contains("HOME"))
{
    line = line.Replace("# ", "");
}

Another solution that I have adopted that is also very functional is this:

Java.Lang.Runtime.GetRuntime().Exec("su -c sed -i 's/^[^#]*VOLUME_DOWN/# &/' /system/usr/keylayout/Generic.kl");
Java.Lang.Runtime.GetRuntime().Exec("su -c sed -i 's/^[^#]*VOLUME_UP/# &/' /system/usr/keylayout/Generic.kl");

However, I could not get the "#" out of the front of the line when I put it in the same command. Does anyone understand sed command in adb shell to help me rewrite the line the way it "uncomment" this particular line?

    
asked by anonymous 20.10.2017 / 14:13

0 answers