diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e5a96f6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +*.iml +.idea +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..e6b32bc --- /dev/null +++ b/build.gradle @@ -0,0 +1,27 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + + repositories { + google() + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:3.0.1' + + + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + +allprojects { + repositories { + google() + jcenter() + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} diff --git a/example/.gitignore b/example/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/example/.gitignore @@ -0,0 +1 @@ +/build diff --git a/example/build.gradle b/example/build.gradle new file mode 100644 index 0000000..618876e --- /dev/null +++ b/example/build.gradle @@ -0,0 +1,29 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 27 + defaultConfig { + applicationId "com.topjohnwu.libsuexample" + minSdkVersion 16 + targetSdkVersion 27 + versionCode 1 + versionName "1.0" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation project(':superuser') +} diff --git a/example/proguard-rules.pro b/example/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/example/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# 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 *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/example/src/main/AndroidManifest.xml b/example/src/main/AndroidManifest.xml new file mode 100644 index 0000000..3b5b598 --- /dev/null +++ b/example/src/main/AndroidManifest.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example/src/main/java/com/topjohnwu/libsuexample/ExampleApp.java b/example/src/main/java/com/topjohnwu/libsuexample/ExampleApp.java new file mode 100644 index 0000000..eabc0b4 --- /dev/null +++ b/example/src/main/java/com/topjohnwu/libsuexample/ExampleApp.java @@ -0,0 +1,39 @@ +package com.topjohnwu.libsuexample; + +import android.app.Application; + +import com.topjohnwu.superuser.Shell; +import com.topjohnwu.superuser.ShellContainer; + +/** + * The {@link Application} of the Example app. + *

+ * We implement {@link ShellContainer} to the {@link Application} of + * the app, which means that we would love our root shell to live as long as the + * application itself. + */ +public class ExampleApp extends Application implements ShellContainer { + + /** + * A shell instance living alongside the lifecycle of {@link ExampleApp} + */ + public Shell mShell; + + @Override + public void onCreate() { + super.onCreate(); + // Enable verbose logging flags + Shell.addFlags(Shell.FLAG_VERBOSE_LOGGING); + Shell.setGlobalContainer(this); + } + + @Override + public Shell getShell() { + return mShell; + } + + @Override + public void setShell(Shell shell) { + mShell = shell; + } +} diff --git a/example/src/main/java/com/topjohnwu/libsuexample/MainActivity.java b/example/src/main/java/com/topjohnwu/libsuexample/MainActivity.java new file mode 100644 index 0000000..9ffe7e0 --- /dev/null +++ b/example/src/main/java/com/topjohnwu/libsuexample/MainActivity.java @@ -0,0 +1,75 @@ +package com.topjohnwu.libsuexample; + +import android.app.Activity; +import android.os.Bundle; +import android.text.TextUtils; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ScrollView; +import android.widget.TextView; + +import com.topjohnwu.superuser.NoShellException; +import com.topjohnwu.superuser.Shell; +import com.topjohnwu.superuser.ShellCallback; +import com.topjohnwu.superuser.ShellCallbackVector; + +import java.util.List; + +public class MainActivity extends Activity { + + private TextView console; + private Button cmd, script, clear; + private EditText input; + private ScrollView sv; + private List callback; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + console = findViewById(R.id.console); + input = findViewById(R.id.cmd_input); + sv = findViewById(R.id.sv); + + cmd = findViewById(R.id.run_cmd); + script = findViewById(R.id.run_script); + clear = findViewById(R.id.clear); + + // Run the shell command in the input box + cmd.setOnClickListener(v -> { + Shell.su(callback, input.getText().toString()); + input.setText(""); + }); + + // Load a script from raw resources + script.setOnClickListener(v -> { + try { + Shell.getShell().loadInputStream(callback, callback, + getResources().openRawResource(R.raw.script)); + } catch (NoShellException e) { + e.printStackTrace(); + } + }); + + clear.setOnClickListener(v -> { + callback.clear(); + console.setText(""); + }); + + // We create a ShellCallback to update the UI with the Shell output + callback = new ShellCallback() { + StringBuilder builder = new StringBuilder(); + @Override + public void onShellOutput(String e) { + builder.append(e).append('\n'); + console.setText(builder); + sv.postDelayed(() -> sv.fullScroll(ScrollView.FOCUS_DOWN), 10); + } + + @Override + public void clear() { + builder = new StringBuilder(); + } + }; + } +} diff --git a/example/src/main/res/drawable-v24/ic_launcher_foreground.xml b/example/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..c7bd21d --- /dev/null +++ b/example/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/example/src/main/res/drawable/ic_launcher_background.xml b/example/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..d5fccc5 --- /dev/null +++ b/example/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/example/src/main/res/layout/activity_main.xml b/example/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..8d222fd --- /dev/null +++ b/example/src/main/res/layout/activity_main.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + +