diff --git a/libsuperuser_example/AndroidManifest.xml b/libsuperuser_example/AndroidManifest.xml index dd08b9b..4eb3884 100644 --- a/libsuperuser_example/AndroidManifest.xml +++ b/libsuperuser_example/AndroidManifest.xml @@ -22,6 +22,7 @@ + @@ -33,4 +34,4 @@ " - \ No newline at end of file + diff --git a/libsuperuser_example/src/eu/chainfire/libsuperuser_example/InteractiveActivity.java b/libsuperuser_example/src/eu/chainfire/libsuperuser_example/InteractiveActivity.java new file mode 100644 index 0000000..6b93579 --- /dev/null +++ b/libsuperuser_example/src/eu/chainfire/libsuperuser_example/InteractiveActivity.java @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2012 Jorrit "Chainfire" Jongma + * Copyright (C) 2013 Kevin Cernekee + * + * 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.ArrayList; +import java.util.List; + +import eu.chainfire.libsuperuser.Shell; + +import android.os.Bundle; +import android.app.Activity; +import android.app.ProgressDialog; +import android.content.Intent; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +public class InteractiveActivity extends Activity { + + private static Shell.Interactive rootSession; + + private void updateResultStatus(boolean suAvailable, List suResult) { + StringBuilder sb = (new StringBuilder()). + append("Root? ").append(suAvailable ? "Yes" : "No").append((char)10). + append((char)10); + if (suResult != null) { + for (String line : suResult) { + sb.append(line).append((char)10); + } + } + ((TextView)findViewById(R.id.text)).setText(sb.toString()); + } + + private void reportError(String error) { + List errorInfo = new ArrayList(); + errorInfo.add(error); + updateResultStatus(false, errorInfo); + rootSession = null; + } + + private void sendRootCommand() { + rootSession.addCommand(new String[] { "id", "date", "ls -l /" }, 0, + new Shell.OnCommandResultListener() { + public void onCommandResult(int commandCode, int exitCode, List output) { + if (exitCode < 0) { + reportError("Error executing commands: exitCode " + exitCode); + } else { + updateResultStatus(true, output); + } + } + }); + } + + private void openRootShell() { + if (rootSession != null) { + sendRootCommand(); + } else { + // 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. + final ProgressDialog dialog = new ProgressDialog(this); + dialog.setTitle("Please wait"); + dialog.setMessage("Requesting root privilege..."); + dialog.setIndeterminate(true); + dialog.setCancelable(false); + dialog.show(); + + // start the shell in the background and keep it alive as long as the app is running + rootSession = new Shell.Builder(). + useSU(). + setWantSTDERR(true). + setWatchdogTimeout(5). + setMinimalLogging(true). + open(new Shell.OnCommandResultListener() { + + // Callback to report whether the shell was successfully started up + @Override + public void onCommandResult(int commandCode, int exitCode, List output) { + // note: this will FC if you rotate the phone while the dialog is up + dialog.dismiss(); + + if (exitCode != Shell.OnCommandResultListener.SHELL_RUNNING) { + reportError("Error opening root shell: exitCode " + exitCode); + } else { + // Shell is up: send our first request + sendRootCommand(); + } + } + }); + } + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + // mode switch button + Button button = (Button)findViewById(R.id.switch_button); + button.setText(R.string.disable_interactive_mode); + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + startActivity(new Intent(v.getContext(), MainActivity.class)); + finish(); + } + }); + + // refresh button + ((Button)findViewById(R.id.refresh_button)). + setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + openRootShell(); + } + }); + + openRootShell(); + } +}