Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AutoOTG extends AppCompatActivity
- {
- private boolean isCableConnected = false;
- private boolean cableStateChanged = false;
- private boolean popupWasShown = false;
- private boolean mServiceRunning = false;
- private boolean mServiceWasRunning = false;
- private ImageView mConnectedAnimation = null;
- public static final String IS_CONNECTED_KEY = "isConnectedValue";
- private int SETTINGS_ACTION = 1;
- private Button button;
- private Process suProcess;
- private int state;
- private String cmd;
- public void updateState()
- {
- boolean currentState = OtgService.getUsbDeviceCount() > 0;
- if (isCableConnected != currentState)
- {
- cableStateWasChanged(currentState);
- }
- }
- public void startOtgService()
- {
- //Manually get init state
- updateState();
- mServiceRunning = true;
- startService(new Intent(AutoOTG.this, OtgService.class));
- OtgService.setOnStatusChangedListener(new OtgService.OnStatusChangedListener()
- {
- @Override
- public void onStatusChanged(boolean status)
- {
- cableStateWasChanged(status);
- }
- });
- }
- public void stopOtgService()
- {
- mServiceRunning = false;
- stopService(new Intent(AutoOTG.this, OtgService.class));
- }
- private void loadAnimation()
- {
- mConnectedAnimation = new ImageView(this);
- mConnectedAnimation.setLayoutParams( new ViewGroup.LayoutParams(512, 512 ) );
- Glide.with(this).load("file:///android_asset/connected.gif").into(mConnectedAnimation);
- }
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
- boolean keep_screen = sharedPrefs.getBoolean("Keep screen on",false);
- String theme = sharedPrefs.getString("Choose Theme","Light");
- if(keep_screen)
- {
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- }
- if(theme != null && theme.equals("Light"))
- {
- setTheme(R.style.AppTheme);
- //setTheme(R.style.Theme_light);
- //Toast.makeText(AutoOTG.this,"Light theme",Toast.LENGTH_SHORT).show();
- }
- else
- {
- setTheme(R.style.AppTheme_AppBarOverlay);
- //Toast.makeText(AutoOTG.this,"Dark theme",Toast.LENGTH_SHORT).show();
- }
- super.onCreate(savedInstanceState);
- loadAnimation();
- setContentView(R.layout.activity_auto_otg);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- getSupportActionBar().setTitle("Auto OTG");
- if(getSupportActionBar() != null)
- {
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- getSupportActionBar().setDisplayShowHomeEnabled(true);
- }
- toolbar.setNavigationOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent(AutoOTG.this,OTGEnabler.class);
- startActivity(intent);
- //finish();
- }
- });
- getRoot();
- startOtgService();
- button = (Button) findViewById(R.id.button_autootg);
- //Get current state
- if (mServiceRunning)
- {
- updateState();
- }
- String result = execCommand("id\n", true);
- if (result == null || !result.contains("uid=0")) {
- Toast.makeText(this, "Error, app requires root access", Toast.LENGTH_LONG).show();
- finish();
- }
- }
- private void getRoot()
- {
- try
- {
- suProcess = Runtime.getRuntime().exec("su");
- }
- catch (IOException e) {
- }
- }
- private void getState()
- {
- SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
- boolean auto_otg = sharedPrefs.getBoolean("Enable Auto OTG",false);
- cmd = "cat /sys/kernel/debug/regulator/8226_smbbp_otg/enable\n";
- state = Integer.parseInt(execCommand(cmd, true));
- boolean isEnabled = (state == 1);
- // otgSwitch.setChecked(isEnabled);
- if(auto_otg)
- {
- if (isCableConnected)
- {
- button.setText("OTG Enabled");
- button.setBackgroundColor(Color.GREEN);
- state = 1 - state;
- String cmd = "echo \"" + state + "\" >> /sys/kernel/debug/regulator/8226_smbbp_otg/enable\n";
- execCommand(cmd, false);
- }
- else
- {
- button.setText("OTG Disabled");
- button.setBackgroundColor(Color.RED);
- state = 1 - state;
- String cmd = "echo \"" + state + "\" >> /sys/kernel/debug/regulator/8226_smbbp_otg/enable\n";
- execCommand(cmd, false);
- }
- }
- }
- private String execCommand(String cmd, boolean isOutputNeeded) {
- String result = null;
- try {
- DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
- BufferedReader osRes = new BufferedReader(new InputStreamReader(suProcess.getInputStream()));
- if (null != os && null != osRes) {
- os.writeBytes(cmd);
- os.flush();
- if (isOutputNeeded) {
- result = osRes.readLine();
- }
- }
- } catch (IOException ioe) {
- Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
- finish();
- }
- catch(NullPointerException npe){
- Toast.makeText(this, "Error, app requires root access", Toast.LENGTH_LONG).show();
- finish();
- }
- return result;
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_devices__list, menu);
- return true;
- }
- @Override
- public boolean onPrepareOptionsMenu(Menu menu)
- {
- Log.e("###", "Prepare called");
- MenuItem item = menu.findItem(R.id.action_cable);
- if (!isCableConnected)
- {
- item.setVisible(true);
- }
- else
- {
- item.setVisible(false);
- }
- return super.onPrepareOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item)
- {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings)
- {
- startActivityForResult(new Intent(this,
- Settings.class), SETTINGS_ACTION);
- }
- else if(id == R.id.about)
- {
- Intent intent = new Intent(AutoOTG.this,About.class);
- startActivity(intent);
- }
- else if(id == R.id.settings)
- {
- startActivityForResult(new Intent(this,
- Settings.class), SETTINGS_ACTION);
- }
- else if(id == R.id.action_cable)
- {
- popupWasShown = true;
- invalidateOptionsMenu();
- // You can change the popup title, message and buttons here
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle(isCableConnected ? "OTG cable connected" : "OTG cable disconnected");
- builder.setMessage("Please connect OTG cable !");
- builder.setPositiveButton("OK", null);
- builder.setNegativeButton(null, null);
- builder.create().show();
- }
- return super.onOptionsItemSelected(item);
- }
- private void cableStateWasChanged(boolean isConnected)
- {
- SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
- boolean show_popup = sharedPrefs.getBoolean("Show Popup Message",false);
- boolean type = sharedPrefs.getBoolean("Show cable notification",false);
- String notification = sharedPrefs.getString("Select type of notification","Toast");
- boolean state_changed = sharedPrefs.getBoolean("Start app on cable state change",false);
- if(type)
- {
- if (notification != null && notification.equals("Toast"))
- {
- if (isConnected)
- Toast.makeText(AutoOTG.this, "OTG Cable Connected", Toast.LENGTH_SHORT).show();
- else
- Toast.makeText(AutoOTG.this, "OTG Cable Disconnected", Toast.LENGTH_SHORT).show();
- }
- else if (notification != null && notification.equals("Notification bar"))
- {
- }
- else if (notification != null && notification.equals("Vibrate"))
- {
- // Get instance of Vibrator from current Context
- Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
- if (isConnected)
- {
- // Vibrate for 400 milliseconds
- v.vibrate(400);
- }
- else
- {
- // Vibrate for 400 milliseconds
- v.vibrate(400);
- }
- }
- }
- //Menu
- isCableConnected = isConnected;
- cableStateChanged = true;
- popupWasShown = false;
- invalidateOptionsMenu();
- //Popup
- if (mLastDialog != null)
- mLastDialog.dismiss();
- mLastDialog = null;
- mLastDialog = new AlertDialog.Builder(this).create();
- if(show_popup)
- {
- mLastDialog.setTitle("Otg state changed");
- if (isConnected)
- {
- mLastDialog.setMessage("OTG cable connected");
- //startOtgService();
- }
- else
- {
- mLastDialog.setMessage("OTG cable disconnected");
- //stopOtgService();
- mLastDialog.setView(mConnectedAnimation);
- if (mConnectedAnimation.getParent() != null)
- ((ViewGroup) mConnectedAnimation.getParent()).removeView(mConnectedAnimation);
- }
- mLastDialog.setOnDismissListener(new DialogInterface.OnDismissListener()
- {
- @Override
- public void onDismiss(DialogInterface dialog)
- {
- mLastDialog = null;
- }
- });
- if(state_changed)
- {
- mLastDialog.show();
- }
- }
- }
- AlertDialog mLastDialog = null;
- @Override
- protected void onPause()
- {
- Log.e("###", "Paused");
- super.onPause();
- SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
- boolean auto_otg = sharedPrefs.getBoolean("Enable Auto OTG",false);
- OtgService.clearListener();
- if(auto_otg)
- {
- if (isCableConnected)
- {
- state = 1;
- String cmd = "echo \"" + state + "\" >> /sys/kernel/debug/regulator/8226_smbbp_otg/enable\n";
- execCommand(cmd, false);
- } else
- {
- state = 0;
- String cmd = "echo \"" + state + "\" >> /sys/kernel/debug/regulator/8226_smbbp_otg/enable\n";
- execCommand(cmd, false);
- }
- }
- }
- @Override
- protected void onResume()
- {
- Log.e("###", "Resumed");
- super.onResume();
- SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
- boolean auto_otg = sharedPrefs.getBoolean("Enable Auto OTG",false);
- if (mServiceRunning)
- updateState();
- OtgService.setOnStatusChangedListener(new OtgService.OnStatusChangedListener()
- {
- @Override
- public void onStatusChanged(boolean status)
- {
- cableStateWasChanged(status);
- }
- });
- if(auto_otg)
- getState();
- }
- @Override
- protected void onDestroy()
- {
- super.onDestroy();
- OtgService.clearListener();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement