Android Open Source - alogger Super User






From Project

Back to project page alogger.

License

The source code is released under:

GNU General Public License

If you think the Android project alogger listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jug6ernaut.android.utilites;
/*from   w  ww  . j  a v a2 s .co  m*/
/****************************************************************************
 * SuperUser class... should speak for itself. These are the functions I    *
 * personally use as base for most things SuperUser.                        *
 *                                                                          *
 * I know some parts look overkill, but tested on a wide range of devices,  *
 * doing things this way works around a large number of issues.             *
 *                                                                          *
 * Doesn't include an easy way to keep a su shell open in the background    *
 * and dynamically read and write to it. However if you're looking for that,*
 * executeSU(String[] commands) should be a good starting point.            *
 *                                                                          *
 *  - Chainfire                                                             *
 *                                                                          *
 *  Copyright (c) Chainfire, license public domain                          *
 ****************************************************************************/

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SuperUser {
  // change this
  private static final String LOG_TAG = "SuperUser"; 
  
  private static int haveSUresult = 0;
  
  private static void log(String text) {
    // using LOG_TAG twice so its easier to use in @#$&)%^&@( DDMS
    android.util.Log.d(LOG_TAG, "[" + LOG_TAG + "] " + text);
  }
  
  public static String getAppPid(String appName){
    List<String> x = executeSU("ps | grep " + appName + " | awk '{print $2}'"); 
    return (x.size()>0)?x.get(0):null;
  }
  
  public static List<String> getChildPids(int parentPid){
    List<String> children = executeSU("ps | grep " + parentPid + " | awk '{print $2}'");
    Collections.synchronizedList(children);
    for(String c : children){
      if(c.equals(parentPid)){
        children.remove(c);
      }
    }
    return children;
  }
  
  public static boolean haveSU() {
    if (haveSUresult == 0) {      
      List<String> result = executeSU("ls /");
      if (result == null) {
        haveSUresult = 1;
        return false;
      } else {
        haveSUresult = 2;
        return true;
      }
    } else {
      if (haveSUresult == 1) {
        return false;
      } else {
        return true;
      }
    }
  }
  
  
  
  static SuperUser instance = null;
  public static SuperUser getInstance(){
    
    if(instance == null)instance = new SuperUser();
    
    return instance;    
  }
  
  public void restart(){
    if(process!=null){
      try {
        os.write("exit\n".getBytes());
        os.flush();
        process.waitFor();
      } catch (IOException e) {
        e.printStackTrace();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      process = null;
    }
    
    try {
      // open su, attach stdin, stdout and stderr
      process = Runtime.getRuntime().exec("su");
      os = new DataOutputStream(process.getOutputStream());
      osRes = new DataInputStream(process.getInputStream());
      osError = new DataInputStream(process.getErrorStream());
    }catch(Exception e){
      e.printStackTrace();
    }    
  }
  
  public Process process;
  DataOutputStream os = null;
  DataInputStream osRes = null;
  DataInputStream osError = null;
  
  private SuperUser(){
    restart();
  }
  
  public void raw(String command){
    try {
      os.write((command + "\n").getBytes());
      os.flush();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  
  public List<String> execute(String command) {
    return executeSU(new String[] { command });
  }
  
  public List<String> execute(String[] commands) throws InterruptedException {
    List<String> res = new ArrayList<String>();  
    // execute commands and log them
    try{
          for (String single : commands) {
            log("[SU+] " + single);
            os.write((single + "\n").getBytes());
               //os.flush();
          }
          // wait until su exits
          process.waitFor();
          if (process.exitValue() == 255) {
            // su (probably) denied
            return null;
          }
          
          // log output and error streams
          while (osError.available() > 0) {
            String read = osError.readLine();
            log("[SU*] " + read);
            if(read.length()>0)
            res.add(read);
          }
             while (osRes.available() > 0) {
               String read = osRes.readLine();
            log("[SU-] " + read);
               res.add(read);
             }
        } catch (IOException e) {
          // su (probably) not found
          return null;
        }
        
        return res;
      }
  
  public static List<String> executeSU(String command) {
    return executeSU(new String[] { command });
  }
  
  public static List<String> executeSU(String[] commands) {
    List<String> res = new ArrayList<String>();
    
    Process process;
    try {
      // open su, attach stdin, stdout and stderr
      process = Runtime.getRuntime().exec("su");
      DataOutputStream os = new DataOutputStream(process.getOutputStream());
      DataInputStream osRes = new DataInputStream(process.getInputStream());
      DataInputStream osError = new DataInputStream(process.getErrorStream());
      
      // execute commands and log them
      for (String single : commands) {
        log("[SU+] " + single);
        os.writeBytes(single + "\n");
           os.flush();
      }
      
      // exit
      os.writeBytes("exit\n");
      os.flush();
      
      // wait until su exits
      process.waitFor();
      if (process.exitValue() == 255) {
        // su (probably) denied
        return null;
      }
      
      // log output and error streams
      while (osError.available() > 0) {
        String read = osError.readLine();
        log("[SU*] " + read);
        if(read.length()>0)
        res.add(read);
      }
         while (osRes.available() > 0) {
           String read = osRes.readLine();
        log("[SU-] " + read);
           res.add(read);
         }
    } catch (IOException e) {
      // su (probably) not found
      return null;
    } catch (InterruptedException e) {
      // should probably reraise InterruptedException instead
      return null;
    }
    
    return res;
  }
  
  public static void executeSUreboot() {
    // yes, seriously.
    executeSU(new String[] {
        "reboot -f",
        "reboot",
        "reboot normal",
        "toolbox reboot",
        "busybox reboot -f",
        "busybox reboot",
        "busybox reboot normal"        
    });
  }
}




Java Source Code List

.FileDialog.java
com.jug6ernaut.android.actiondialog.ActionDialog.java
com.jug6ernaut.android.logging.ALogger.java
com.jug6ernaut.android.logging.BriefLogFormatter.java
com.jug6ernaut.android.logging.DebugLogger.java
com.jug6ernaut.android.logging.JSONFormatter.java
com.jug6ernaut.android.logging.JSONLogReader.java
com.jug6ernaut.android.logging.JSONLogWriter.java
com.jug6ernaut.android.logging.LogAdapter.java
com.jug6ernaut.android.logging.LogEntry.java
com.jug6ernaut.android.logging.LogFragment.java
com.jug6ernaut.android.logging.Logger.java
com.jug6ernaut.android.logging.ProdLogger.java
com.jug6ernaut.android.utilites.AnalysisDir.java
com.jug6ernaut.android.utilites.Assets.java
com.jug6ernaut.android.utilites.BaseAsyncTask.java
com.jug6ernaut.android.utilites.ByteUtils.java
com.jug6ernaut.android.utilites.CopyUtils.java
com.jug6ernaut.android.utilites.DeviceWake.java
com.jug6ernaut.android.utilites.DisplayUtils.java
com.jug6ernaut.android.utilites.Eula.java
com.jug6ernaut.android.utilites.FileDownloader.java
com.jug6ernaut.android.utilites.ImagesUtils.java
com.jug6ernaut.android.utilites.PInfo.java
com.jug6ernaut.android.utilites.PackageInfo.java
com.jug6ernaut.android.utilites.ReflectionUtils.java
com.jug6ernaut.android.utilites.SecurePreferences.java
com.jug6ernaut.android.utilites.SeekBarPreference.java
com.jug6ernaut.android.utilites.SimpleCrypto.java
com.jug6ernaut.android.utilites.SuperUser.java
com.jug6ernaut.android.utilites.UserUtils.java
com.jug6ernaut.android.utilites.WakelockHelper.java
com.jug6ernaut.android.utilites.lazyloader.FileCache.java
com.jug6ernaut.android.utilites.lazyloader.ImageLoader.java
com.jug6ernaut.android.utilites.lazyloader.MemoryCache.java
com.jug6ernaut.android.utilites.lazyloader.Utils.java
com.jug6ernaut.android.utilites.time.Preconditions.java
com.jug6ernaut.android.utilites.time.Stopwatch.java
com.jug6ernaut.android.utilites.time.Ticker.java
com.jug6ernaut.android.utilites.time.TimeUtils.java
com.jug6ernaut.billing.Base64DecoderException.java
com.jug6ernaut.billing.Base64.java
com.jug6ernaut.billing.IabException.java
com.jug6ernaut.billing.IabHelper.java
com.jug6ernaut.billing.IabResult.java
com.jug6ernaut.billing.Inventory.java
com.jug6ernaut.billing.Purchase.java
com.jug6ernaut.billing.Security.java
com.jug6ernaut.billing.SkuDetails.java