Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Chainfire committed Oct 29, 2012
1 parent edc7bcd commit c96d343
Show file tree
Hide file tree
Showing 13 changed files with 379 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libsuperuser_example/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
33 changes: 33 additions & 0 deletions libsuperuser_example/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>libsuperuser_example</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
35 changes: 35 additions & 0 deletions libsuperuser_example/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="eu.chainfire.libsuperuser_example"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
android:name="eu.chainfire.libsuperuser.Application"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>

<service android:name=".BackgroundIntentService" />"
</application>

</manifest>
20 changes: 20 additions & 0 deletions libsuperuser_example/proguard-project.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
15 changes: 15 additions & 0 deletions libsuperuser_example/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-4
android.library.reference.1=../libsuperuser
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions libsuperuser_example/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<EditText
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inputType="textMultiLine" >

<requestFocus />
</EditText>

</LinearLayout>
3 changes: 3 additions & 0 deletions libsuperuser_example/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">libsuperuser_example</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (C) 2012 Jorrit "Chainfire" Jongma
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package eu.chainfire.libsuperuser_example;

import eu.chainfire.libsuperuser.Application;
import eu.chainfire.libsuperuser.Shell;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

/**
* Example IntentService based class, to execute tasks in a background
* thread. This would typically be used by BroadcastReceivers and other
* fire-and-forget type calls.
*
* For most background calls that would occur when the UI is visible, in
* response to some user action and/or something you are waiting for,
* you would typically use an AsyncTask instead of a service like this.
* (See MainActivity.java for that example)
*
* Note that the IntentService's onHandleIntent call runs in a background
* thread, while a normal service's calls would run in the main thread,
* unless you put in the extra work. This is an important distinction
* that is often overlooked by beginners.
*
* This service starts running when needed, and stops running when the
* task is done, automagically.
*
* Please also see BootCompleteReceiver.java, and AndroidManifest.xml for
* how and when this service is instantiated.
*
* This code leaves some room for extension - if you really wanted to
* respond only to a single event that always does the same, this code
* could have been a lot shorter.
*/
public class BackgroundIntentService extends IntentService {
// you could provide more options here, should you need them
public static final String ACTION_BOOT_COMPLETE = "boot_complete";

public static void performAction(Context context, String action) {
performAction(context, action, null);
}

public static void performAction(Context context, String action, Bundle extras) {
// this is utility call to easy starting the service and performing a task
// pass parameters in an bundle to be added to the intent as extras
// See BootCompleteReceiver.java

if ((context == null) || (action == null) || action.equals("")) return;

Intent svc = new Intent(context, BackgroundIntentService.class);
svc.setAction(action);
if (extras != null) svc.putExtras(extras);
context.startService(svc);
}

public BackgroundIntentService() {
// If you forget this one, the app will crash
super("BackgroundIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
if ((action == null) || (action.equals(""))) return;

if (action.equals(ACTION_BOOT_COMPLETE)) {
onBootComplete();
}
// you can define more options here... pass parameters through the "extra" values
}

protected void onBootComplete() {
// We are running in a background thread here!

// This would crash (when debugging) if it was called from the main thread:
Shell.SU.run("ls -l /");

// Let's toast that we're done, using the work-arounds and utility function in
// out Application class. Without those modifications there would be a very high
// chance of crashing the app in various Android versions. The modifications are
// simple and easily ported to your own Application class, if you can't use the
// one from libsuperuser.
Application.toast(this, "This toast will self-destruct in five seconds");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2012 Jorrit "Chainfire" Jongma
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package eu.chainfire.libsuperuser_example;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
* Example BootCompleteReceiver that starts MyIntentService
* (please see MyIntentService.java) to handle the task
* in a background thread
*/
public class BootCompleteReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// What many beginners don't realize is that BroadcastReceivers like these
// usually run in the application's main thread, and can thus generate
// ANRs. This is increasingly likely with the BOOT_COMPLETED receiver, as
// the system is likely very busy when this receiver is called.

// In this example we are starting our MyIntentService to actually do the
// work we want to happen, not only because "su" should specifically NEVER
// be called from a BroadcastReceiver, but also because you should be doing
// this even if you aren't calling "su". It's a good practise, and using
// IntentService is really easy.

BackgroundIntentService.performAction(context, BackgroundIntentService.ACTION_BOOT_COMPLETE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (C) 2012 Jorrit "Chainfire" Jongma
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package eu.chainfire.libsuperuser_example;

import java.util.List;

import eu.chainfire.libsuperuser.Shell;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.widget.EditText;

public class MainActivity extends Activity {
private class Startup extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog = null;
private Context context = null;
private boolean suAvailable = false;
private String suVersion = null;
private String suVersionInternal = null;
private List<String> suResult = null;

public Startup setContext(Context context) {
this.context = context;
return this;
}

@Override
protected void onPreExecute() {
// We're creating a progress dialog here because we want the user to wait.
// If in your app your user can just continue on with clicking other things,
// don't do the dialog thing.

dialog = new ProgressDialog(context);
dialog.setTitle("Some title");
dialog.setMessage("Doing something interesting ...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}

@Override
protected Void doInBackground(Void... params) {
// Let's do some SU stuff
suAvailable = Shell.SU.available();
if (suAvailable) {
suVersion = Shell.SU.version(false);
suVersionInternal = Shell.SU.version(true);
suResult = Shell.SU.run(new String[] {
"id",
"ls -l /"
});
}

// This is just so you see we had a progress dialog,
// don't do this in production code
try { Thread.sleep(5000); } catch(Exception e) { }

return null;
}

@Override
protected void onPostExecute(Void result) {
dialog.dismiss();

// output
StringBuilder sb = (new StringBuilder()).
append("Root? ").append(suAvailable ? "Yes" : "No").append((char)10).
append("Version: ").append(suVersion == null ? "N/A" : suVersion).append((char)10).
append("Version (internal): ").append(suVersionInternal == null ? "N/A" : suVersionInternal).append((char)10).
append((char)10);
if (suResult != null) {
for (String line : suResult) {
sb.append(line).append((char)10);
}
}
((EditText)findViewById(R.id.text)).setText(sb.toString());
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Let's do some background stuff
(new Startup()).setContext(this).execute();
}
}

0 comments on commit c96d343

Please sign in to comment.