简体   繁体   中英

Avoid “su” toast on shell command execute in Android?

I'm developing a simple app that injects lines on build.prop by executing a shell command. My main problem is that every time I check a toggle that create the function a toast displaying the shell string appear. Is there any way to avoid this? also, if you have any suggestion to clean a bit the code would be appreciated! (First app for me).

Code:

public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

  // fragment not when container null
  if (container == null) {
     return null;
  }
  // inflate view from layout
  View v = (LinearLayout)inflater.inflate(R.layout.performance,container,false);

    final CheckBox hwdebug = (CheckBox) v.findViewById(R.id.hwDebug);   
    final String[] mountrw = {"su","-c","mount -o remount,rw /system"};
    final String[] enhwdebug1 = {"su","-c","sed -i '/debug.sf.hw=*/d' /system/build.prop"};
    final String[] enhwdebug2 = {"su","-c","echo '## Rendering GPU Enabled ##' >> /system/build.prop"};
    final String[] enhwdebug3 = {"su","-c","echo debug.sf.hw=1 >> /system/build.prop"};
    final String[] dishwdebug1 = {"su","-c","sed -i '/debug.sf.hw=1/d' /system/build.prop"};
    final String[] dishwdebug2 = {"su","-c","sed -i '/## Rendering GPU Enabled ##/d' /system/build.prop"};

final SharedPreferences hwdebugpref = this.getActivity().getSharedPreferences("hwdebugck",0);

    // GPU Rendering Checkbox
    boolean hwdebugck = hwdebugpref.getBoolean("hwdebugck", false);
    if (hwdebugck) { 
        hwdebug.setChecked(true);
    } else {
        hwdebug.setChecked(false);
    }
    hwdebug.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {    
        if((hwdebug.isChecked())) {
            SharedPreferences.Editor editor = hwdebugpref.edit();
          editor.putBoolean("hwdebugck", true); // value to store
          editor.commit();   
            ArrayList<String[]> enhwdebug = new ArrayList<String[]>();
            enhwdebug.add(mountrw);
            enhwdebug.add(enhwdebug1);
            enhwdebug.add(enhwdebug2);
            enhwdebug.add(enhwdebug3);
                for(String[] cmd:enhwdebug){
                    try {
                        Runtime.getRuntime().exec(cmd);
                        } catch (IOException e) {
                            e.fillInStackTrace(); 
                        } 
                }
        } else {
          SharedPreferences.Editor editor = hwdebugpref.edit();
          editor.putBoolean("hwdebugck", false); // value to store
          editor.commit();
            ArrayList<String[]> diswdebug = new ArrayList<String[]>();
            diswdebug.add(mountrw);
            diswdebug.add(dishwdebug1);
            diswdebug.add(dishwdebug2);
                for(String[] cmd:diswdebug){
                    try {
                        Runtime.getRuntime().exec(cmd);              
                        } catch (IOException e) {
                            e.fillInStackTrace(); 
                        } 
                } 
                }
    }
});

So, my main problem is that su -c show that annoying toast. I tried to pass it to busybox or toolbox but without success since they need to be ran with su .

Thank you!

It is possible , by having the same thread that makes the call to root commands stay and let it always be the only one that handles them.

This way, the toast will only appear the first time you use root operations.

Also, on the end user side, some apps (like super-su) allow to avoid the toast, even per app.

Ok first to answer your question the answer is yes and no.

Easy Answer: No its not possible using one of the current SU managers like SuperUser or SuperSU you cant. The toast is a safety mechanism. Both apps features to remove the toast for specific apps, but you as a dev can not control this.

Hard Answer: Yes it is possible, but it would require you compiling your own su binary and using it in place of the su binary already installed. You would need to remove the code that references the current manager (Which ever source you compiled from). Would be recommend to add checks so that ONLY your app can run that binary. This can lead to security risks though and is probably is not a good idea.

I did not look through your code very much but i would say one thing do NOT under any circumstance run SU commands on the UI thread. This is only asking for problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM