[Atrix 4G Android Development] samcripp:
SUNClass

** as promised soon enough the Fruit Cake Maker or FCM app will be out **

and i will begin also creating some tutorials and open sourcing some stuff to get you guys up to speed with some parts of android development.

Here is a small token of appreciation for your patience.

The SUNClass is very simple to use and it will solve the head ache a lot of devs have when they attempt to create an app for their rooted device, specifically apps that require to be ROOT. Also as a small bonus it has a small Toast Msg method.

(I spent a great deal of time documenting this small class)

Quick explanation on how to use it.

From your main activity, you want to create an instance and set a context

// create an instance of the class
SUNClass fromMain = new SUNClass(this);

("this" passes the main activity as the context [require by the toast class])

From inside another class (will still require Main to pass a context)

SUNClass fromClass = new SUNClass(false);

(inside of classes there is no context unless you call it from main, but since we can just set the context on create by creating an instance of the class there we can ignore it on another class)

Now that main has created an instance of the class, we can finally ask for ROOT permissions

// request root
fromMain.RequestSU();

(This is the code that will get you the pop up from superuser, any command after this one will be as ROOT)

Now we just have to send commands.

// Sending a command
fromMain.CmdSU("dd if=/dev/block/mmcblk0p11 of=/sdcard-ext/boot.img", "Making a quick img from the boot partition.");

(The first line in parentheses is the command, and the second one is a quick toast msg! Is good practice to let anyone know what you are doing to their phone. CARRIERIQ anyone?)

Last but not least you can just sent Toast msgs if you feel like it.

// sending a toast msg
fromMain.toaster("silly goof!");

Sorry is taking so long to get FCM to you guys, but im trying to create an adaptable app that will work with many phones. Atrix only at first tho.

Anyways, download the class here. I will upload to github later.

License is as follows.

Use it for anything you want with or without my permission, for money or free applications. But, I must receive credit for it, and you may not remove the ledger.

// Update 12/3/11 8:22pm
// samuel rivera (samcripp) @zombisam
// SU and Notification Class for android apps.
// v 0.5
// notes: toast working to perfection, added a set of class constructors for main, and outside main activities.
//
package com.samcripp.fcm;

import java.io.DataOutputStream;
import java.io.IOException;
import android.content.Context;
import android.widget.Toast;

public class SUNClass {
// variables
Context context;
// class constructorS
public SUNClass(Context ct){
context = ct;
}
public SUNClass(boolean bool){

}
// create a process
Process user;
// The RequestSU method
public void RequestSU(){
// Request the superuser permission
try {
// make process su
user = Runtime.getRuntime().exec("su\n");
} catch (IOException e) {
toaster("Device is not rooted.");
}
}

// Sending custom commands (the command, the msg to display, default: this)
public void CmdSU(String cmd,String tMSG){
try {
// create a shell
DataOutputStream suShell = new DataOutputStream(user.getOutputStream());
// write to the shell
suShell.writeBytes(cmd + "\n");
// wait for the process to finish
user.waitFor();
// closing the shell
suShell.writeBytes("exit\n");
// flush
suShell.flush();
// toast messages
toaster(tMSG);
} catch (IOException e) {
toaster("Device is not rooted.");
} catch (InterruptedException e) {
e.printStackTrace();
toaster("Device is not rooted.");
}
}
// simple method for displaying toast msgs (msg to display, default:this)
public void toaster(String qMSG){
// displays the toast msg
Toast.makeText(context, qMSG, Toast.LENGTH_SHORT).show();
}
}