0

I want to write a simple android application that listens for a command such as long press menu button or press home button 3 times and then changes value in /sys/class/mdnie/mdnie/negative to 1 or 0. I have no idea where to begin, I know modifying this value requires root access and I can successfully do this by echo > 1 /sys/class/mdnie/mdnie/negative

Any guidance is appreciated, I need this for a friend who is visually impaired. This application will toggle negative colors for himon some samsung devices and he would like to press the home key 3 times to toggle the negative colors on and off from anywhere on the device.

  • Possible duplicate of http://stackoverflow.com/questions/18086097/where-to-put-code-for-a-global-hardware-key-listener – Piovezan Oct 28 '13 at 17:49
  • Thanks for the reference, the rom we are using allows for launching an app via hardkeys, so hardkey feature aside how would I write a simple app the looks at value at /sys/class/mdnie/mdnie/negative and changes it to a 1 or 0 based on what it already has. This app needs no user interface. – user2929128 Oct 28 '13 at 18:09

1 Answers1

0

It shouldn't be difficult. It would be an activity with no associated display that toggles the value and finishes immediately afterwards. The code would look like this (not it is untested code - if you face issues, post them as new StackOverflow questions different from this one):

public class ToggleNegativeColorsActivity extends Activity {

    private static final String FILEPATH = "/sys/class/mdnie/mdnie/negative";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super(savedInstanceState);

        try {
            String value = readFileAsString(FILEPATH);
            if ("1".equals(value.trim())) {
                writeStringToFile(FILEPATH, "0");
            } else {
                writeStringToFile(FILEPATH, "1");
            }
        catch (IOException e) {
            e.printStackTrace();
        }

        finish();
    }

    // Grabbed from http://stackoverflow.com/questions/1656797/how-to-read-a-file-into-string-in-java
    private String readFileAsString(String filePath) throws IOException {
        StringBuffer fileData = new StringBuffer();
        BufferedReader reader = new BufferedReader(
            new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
        }
        reader.close();
        return fileData.toString();
    }

    // Grabbed from http://stackoverflow.com/questions/1053467/how-do-i-save-a-string-to-a-text-file-using-java
    private void writeStringToFile(String filePath, String value) throws IOException {
        PrintWriter out = new PrintWriter(filePath);
        out.print(value);
        out.close();
    }
}
Piovezan
  • 3,070
  • 1
  • 24
  • 37