Pavle_nis

OTG Enabler

Feb 8th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. package ru.freshmobile.otgenabler;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Switch;
  7. import android.widget.Toast;
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.DataOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13.  
  14. public class MainActivity extends Activity {
  15.  
  16.     private Process suProcess;
  17.     private Switch otgSwitch;
  18.     private int state;
  19.  
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_main);
  24.         otgSwitch = (Switch) findViewById(R.id.switch1);
  25.         getRoot();
  26.         String result = execCommand("id\n", true);
  27.         if (result == null || !result.contains("uid=0")) {
  28.             Toast.makeText(this, "Error, app requires root access", Toast.LENGTH_LONG).show();
  29.             finish();
  30.         }
  31.     }
  32.  
  33.     @Override
  34.     protected void onResume() {
  35.         super.onResume();
  36.         getState();
  37.     }
  38.  
  39.     public void switchOtgClick(View v) {
  40.         state = 1 - state;
  41.         String cmd = "echo \"" + state + "\" >> /sys/kernel/debug/regulator/8226_smbbp_otg/enable\n";
  42.         execCommand(cmd, false);
  43.     }
  44.  
  45.     private void getState() {
  46.         String cmd = "cat /sys/kernel/debug/regulator/8226_smbbp_otg/enable\n";
  47.         state = Integer.parseInt(execCommand(cmd, true));
  48.         boolean isEnabled = (state == 1);
  49.         otgSwitch.setChecked(isEnabled);
  50.     }
  51.  
  52.     private void getRoot() {
  53.         try {
  54.             suProcess = Runtime.getRuntime().exec("su");
  55.         } catch (IOException e) {
  56.         }
  57.     }
  58.  
  59.     private String execCommand(String cmd, boolean isOutputNeeded) {
  60.         String result = null;
  61.         try {
  62.             DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
  63.             BufferedReader osRes = new BufferedReader(new InputStreamReader(suProcess.getInputStream()));
  64.             if (null != os && null != osRes) {
  65.                 os.writeBytes(cmd);
  66.                 os.flush();
  67.                 if (isOutputNeeded) {
  68.                     result = osRes.readLine();
  69.                 }
  70.             }
  71.         } catch (IOException ioe) {
  72.             Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
  73.             finish();
  74.         }
  75.         catch(NullPointerException npe){
  76.             Toast.makeText(this, "Error, app requires root access", Toast.LENGTH_LONG).show();
  77.             finish();
  78.         }
  79.         return result;
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment