How to restrict the operation of some Android buttons

Asked

Viewed 106 times

0

I’ve been trying a few days, some alternatives to lock the buttons of a tablet and then I need them back to work. I’ll show you some ways I’ve done and which one is operating in the best way possible so far.

This was the first attempt, but after a few days using started to appear permission error to read the file inside the system/usr folder, and at first 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 mentioned earlier, this solution worked normally for a period of time, then access was denied. 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 adopted that is also being very functional is the one that follows:

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 haven’t been able to get the "# " out of the line when I put it on the same command. Does anyone understand of sed command in adb shell to help me rewrite the line in a way that "discovers" this specific line?

  • I believe that I myself have found the solution, I myself can answer my question?

  • Of course can.

  • I ended up not putting the answer because it is not good practice. You find it interesting that I remove the question?

  • Answering the question itself is a normal and accepted practice, see the link I indicated in the previous comment. What matters to the community is to have questions with answers. Since you found the solution, the ideal would be for you to answer them. Answer, delete or leave the question, at your discretion.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.