Advertisement
Pavle_nis

autootg

Feb 9th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.73 KB | None | 0 0
  1. public class AutoOTG extends AppCompatActivity
  2. {
  3.     private boolean isCableConnected = false;
  4.     private boolean cableStateChanged = false;
  5.     private boolean popupWasShown = false;
  6.  
  7.     private boolean mServiceRunning = false;
  8.     private boolean mServiceWasRunning = false;
  9.  
  10.     private ImageView mConnectedAnimation = null;
  11.  
  12.     public static final String IS_CONNECTED_KEY = "isConnectedValue";
  13.  
  14.     private int SETTINGS_ACTION = 1;
  15.  
  16.     private Button button;
  17.  
  18.     private Process suProcess;
  19.     private int state;
  20.     private String cmd;
  21.  
  22.     public void updateState()
  23.     {
  24.         boolean currentState = OtgService.getUsbDeviceCount() > 0;
  25.         if (isCableConnected != currentState)
  26.         {
  27.             cableStateWasChanged(currentState);
  28.         }
  29.     }
  30.  
  31.     public void startOtgService()
  32.     {
  33.  
  34.         //Manually get init state
  35.         updateState();
  36.  
  37.  
  38.         mServiceRunning = true;
  39.         startService(new Intent(AutoOTG.this, OtgService.class));
  40.  
  41.         OtgService.setOnStatusChangedListener(new OtgService.OnStatusChangedListener()
  42.         {
  43.             @Override
  44.             public void onStatusChanged(boolean status)
  45.             {
  46.                 cableStateWasChanged(status);
  47.             }
  48.         });
  49.     }
  50.  
  51.     public void stopOtgService()
  52.     {
  53.         mServiceRunning = false;
  54.         stopService(new Intent(AutoOTG.this, OtgService.class));
  55.  
  56.     }
  57.  
  58.     private void loadAnimation()
  59.     {
  60.         mConnectedAnimation = new ImageView(this);
  61.         mConnectedAnimation.setLayoutParams( new ViewGroup.LayoutParams(512, 512 ) );
  62.  
  63.         Glide.with(this).load("file:///android_asset/connected.gif").into(mConnectedAnimation);
  64.     }
  65.  
  66.  
  67.  
  68.     @Override
  69.     protected void onCreate(Bundle savedInstanceState)
  70.     {
  71.         SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
  72.         boolean keep_screen = sharedPrefs.getBoolean("Keep screen on",false);
  73.         String theme = sharedPrefs.getString("Choose Theme","Light");
  74.  
  75.         if(keep_screen)
  76.         {
  77.             getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  78.         }
  79.         if(theme != null && theme.equals("Light"))
  80.         {
  81.             setTheme(R.style.AppTheme);
  82.             //setTheme(R.style.Theme_light);
  83.             //Toast.makeText(AutoOTG.this,"Light theme",Toast.LENGTH_SHORT).show();
  84.         }
  85.         else
  86.         {
  87.             setTheme(R.style.AppTheme_AppBarOverlay);
  88.             //Toast.makeText(AutoOTG.this,"Dark theme",Toast.LENGTH_SHORT).show();
  89.         }
  90.  
  91.         super.onCreate(savedInstanceState);
  92.  
  93.         loadAnimation();
  94.  
  95.         setContentView(R.layout.activity_auto_otg);
  96.  
  97.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  98.         setSupportActionBar(toolbar);
  99.         getSupportActionBar().setTitle("Auto OTG");
  100.  
  101.         if(getSupportActionBar() != null)
  102.         {
  103.             getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  104.             getSupportActionBar().setDisplayShowHomeEnabled(true);
  105.         }
  106.         toolbar.setNavigationOnClickListener(new View.OnClickListener() {
  107.             @Override
  108.             public void onClick(View view) {
  109.                 Intent intent = new Intent(AutoOTG.this,OTGEnabler.class);
  110.                 startActivity(intent);
  111.                 //finish();
  112.             }
  113.         });
  114.  
  115.         getRoot();
  116.         startOtgService();
  117.         button = (Button) findViewById(R.id.button_autootg);
  118.  
  119.         //Get current state
  120.         if (mServiceRunning)
  121.         {
  122.             updateState();
  123.         }
  124.  
  125.  
  126.         String result = execCommand("id\n", true);
  127.         if (result == null || !result.contains("uid=0")) {
  128.             Toast.makeText(this, "Error, app requires root access", Toast.LENGTH_LONG).show();
  129.             finish();
  130.         }
  131.  
  132.  
  133.     }
  134.  
  135.     private void getRoot()
  136.     {
  137.         try
  138.         {
  139.             suProcess = Runtime.getRuntime().exec("su");
  140.         }
  141.         catch (IOException e) {
  142.         }
  143.     }
  144.  
  145.     private void getState()
  146.     {
  147.         SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
  148.         boolean auto_otg = sharedPrefs.getBoolean("Enable Auto OTG",false);
  149.  
  150.         cmd = "cat /sys/kernel/debug/regulator/8226_smbbp_otg/enable\n";
  151.         state = Integer.parseInt(execCommand(cmd, true));
  152.         boolean isEnabled = (state == 1);
  153. //        otgSwitch.setChecked(isEnabled);
  154.         if(auto_otg)
  155.         {
  156.             if (isCableConnected)
  157.             {
  158.                 button.setText("OTG Enabled");
  159.                 button.setBackgroundColor(Color.GREEN);
  160.                 state = 1 - state;
  161.                 String cmd = "echo \"" + state + "\" >> /sys/kernel/debug/regulator/8226_smbbp_otg/enable\n";
  162.                 execCommand(cmd, false);
  163.             }
  164.             else
  165.             {
  166.                 button.setText("OTG Disabled");
  167.                 button.setBackgroundColor(Color.RED);
  168.                 state = 1 - state;
  169.                 String cmd = "echo \"" + state + "\" >> /sys/kernel/debug/regulator/8226_smbbp_otg/enable\n";
  170.                 execCommand(cmd, false);
  171.             }
  172.         }
  173.     }
  174.  
  175.     private String execCommand(String cmd, boolean isOutputNeeded) {
  176.         String result = null;
  177.         try {
  178.             DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
  179.             BufferedReader osRes = new BufferedReader(new InputStreamReader(suProcess.getInputStream()));
  180.             if (null != os && null != osRes) {
  181.                 os.writeBytes(cmd);
  182.                 os.flush();
  183.                 if (isOutputNeeded) {
  184.                     result = osRes.readLine();
  185.                 }
  186.             }
  187.         } catch (IOException ioe) {
  188.             Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
  189.             finish();
  190.         }
  191.         catch(NullPointerException npe){
  192.             Toast.makeText(this, "Error, app requires root access", Toast.LENGTH_LONG).show();
  193.             finish();
  194.         }
  195.         return result;
  196.     }
  197.  
  198.     @Override
  199.     public boolean onCreateOptionsMenu(Menu menu)
  200.     {
  201.         // Inflate the menu; this adds items to the action bar if it is present.
  202.         getMenuInflater().inflate(R.menu.menu_devices__list, menu);
  203.         return true;
  204.     }
  205.  
  206.     @Override
  207.     public boolean onPrepareOptionsMenu(Menu menu)
  208.     {
  209.         Log.e("###", "Prepare called");
  210.  
  211.         MenuItem item = menu.findItem(R.id.action_cable);
  212.         if (!isCableConnected)
  213.         {
  214.             item.setVisible(true);
  215.         }
  216.         else
  217.         {
  218.             item.setVisible(false);
  219.         }
  220.  
  221.         return super.onPrepareOptionsMenu(menu);
  222.     }
  223.  
  224.  
  225.     @Override
  226.     public boolean onOptionsItemSelected(MenuItem item)
  227.     {
  228.         // Handle action bar item clicks here. The action bar will
  229.         // automatically handle clicks on the Home/Up button, so long
  230.         // as you specify a parent activity in AndroidManifest.xml.
  231.         int id = item.getItemId();
  232.  
  233.         //noinspection SimplifiableIfStatement
  234.         if (id == R.id.action_settings)
  235.         {
  236.             startActivityForResult(new Intent(this,
  237.                     Settings.class), SETTINGS_ACTION);
  238.         }
  239.         else if(id == R.id.about)
  240.         {
  241.             Intent intent = new Intent(AutoOTG.this,About.class);
  242.             startActivity(intent);
  243.         }
  244.         else if(id == R.id.settings)
  245.         {
  246.             startActivityForResult(new Intent(this,
  247.                     Settings.class), SETTINGS_ACTION);
  248.         }
  249.  
  250.         else if(id ==  R.id.action_cable)
  251.         {
  252.             popupWasShown = true;
  253.             invalidateOptionsMenu();
  254.             // You can change the popup title, message and buttons here
  255.             AlertDialog.Builder builder = new AlertDialog.Builder(this);
  256.             builder.setTitle(isCableConnected ? "OTG cable connected" : "OTG cable disconnected");
  257.             builder.setMessage("Please connect OTG cable !");
  258.             builder.setPositiveButton("OK", null);
  259.             builder.setNegativeButton(null, null);
  260.             builder.create().show();
  261.         }
  262.  
  263.  
  264.         return super.onOptionsItemSelected(item);
  265.     }
  266.  
  267.     private void cableStateWasChanged(boolean isConnected)
  268.     {
  269.  
  270.         SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
  271.         boolean show_popup = sharedPrefs.getBoolean("Show Popup Message",false);
  272.         boolean type = sharedPrefs.getBoolean("Show cable notification",false);
  273.         String notification = sharedPrefs.getString("Select type of notification","Toast");
  274.         boolean state_changed = sharedPrefs.getBoolean("Start app on cable state change",false);
  275.  
  276.         if(type)
  277.         {
  278.             if (notification != null && notification.equals("Toast"))
  279.             {
  280.                 if (isConnected)
  281.                     Toast.makeText(AutoOTG.this, "OTG Cable Connected", Toast.LENGTH_SHORT).show();
  282.                 else
  283.                     Toast.makeText(AutoOTG.this, "OTG Cable Disconnected", Toast.LENGTH_SHORT).show();
  284.             }
  285.             else if (notification != null && notification.equals("Notification bar"))
  286.             {
  287.  
  288.             }
  289.             else if (notification != null && notification.equals("Vibrate"))
  290.             {
  291.                 // Get instance of Vibrator from current Context
  292.                 Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
  293.                 if (isConnected)
  294.                 {
  295.                     // Vibrate for 400 milliseconds
  296.                     v.vibrate(400);
  297.                 }
  298.                 else
  299.                 {
  300.                     // Vibrate for 400 milliseconds
  301.                     v.vibrate(400);
  302.                 }
  303.             }
  304.         }
  305.  
  306.         //Menu
  307.         isCableConnected = isConnected;
  308.         cableStateChanged = true;
  309.         popupWasShown = false;
  310.         invalidateOptionsMenu();
  311.  
  312.             //Popup
  313.             if (mLastDialog != null)
  314.                 mLastDialog.dismiss();
  315.             mLastDialog = null;
  316.  
  317.             mLastDialog = new AlertDialog.Builder(this).create();
  318.  
  319.         if(show_popup)
  320.         {
  321.             mLastDialog.setTitle("Otg state changed");
  322.             if (isConnected)
  323.             {
  324.                 mLastDialog.setMessage("OTG cable connected");
  325.                 //startOtgService();
  326.             }
  327.             else
  328.             {
  329.                 mLastDialog.setMessage("OTG cable disconnected");
  330.                 //stopOtgService();
  331.  
  332.                 mLastDialog.setView(mConnectedAnimation);
  333.                 if (mConnectedAnimation.getParent() != null)
  334.                     ((ViewGroup) mConnectedAnimation.getParent()).removeView(mConnectedAnimation);
  335.             }
  336.  
  337.             mLastDialog.setOnDismissListener(new DialogInterface.OnDismissListener()
  338.             {
  339.                 @Override
  340.                 public void onDismiss(DialogInterface dialog)
  341.                 {
  342.                     mLastDialog = null;
  343.                 }
  344.             });
  345.  
  346.             if(state_changed)
  347.             {
  348.                 mLastDialog.show();
  349.             }
  350.         }
  351.     }
  352.  
  353.     AlertDialog mLastDialog = null;
  354.    
  355.     @Override
  356.     protected void onPause()
  357.     {
  358.         Log.e("###", "Paused");
  359.         super.onPause();
  360.  
  361.         SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
  362.         boolean auto_otg = sharedPrefs.getBoolean("Enable Auto OTG",false);
  363.  
  364.         OtgService.clearListener();
  365.  
  366.         if(auto_otg)
  367.         {
  368.             if (isCableConnected)
  369.             {
  370.                 state = 1;
  371.                 String cmd = "echo \"" + state + "\" >> /sys/kernel/debug/regulator/8226_smbbp_otg/enable\n";
  372.                 execCommand(cmd, false);
  373.             } else
  374.             {
  375.                 state = 0;
  376.                 String cmd = "echo \"" + state + "\" >> /sys/kernel/debug/regulator/8226_smbbp_otg/enable\n";
  377.                 execCommand(cmd, false);
  378.             }
  379.         }
  380.     }
  381.  
  382.     @Override
  383.     protected void onResume()
  384.     {
  385.         Log.e("###", "Resumed");
  386.  
  387.         super.onResume();
  388.  
  389.         SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
  390.         boolean auto_otg = sharedPrefs.getBoolean("Enable Auto OTG",false);
  391.  
  392.         if (mServiceRunning)
  393.             updateState();
  394.  
  395.         OtgService.setOnStatusChangedListener(new OtgService.OnStatusChangedListener()
  396.         {
  397.             @Override
  398.             public void onStatusChanged(boolean status)
  399.             {
  400.                 cableStateWasChanged(status);
  401.             }
  402.         });
  403.  
  404.         if(auto_otg)
  405.         getState();
  406.     }
  407.  
  408.     @Override
  409.     protected void onDestroy()
  410.     {
  411.         super.onDestroy();
  412.  
  413.         OtgService.clearListener();
  414.     }
  415. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement