Permalink
Browse files

Update intent sample to demonstrate reuse of existing windows

  • Loading branch information...
1 parent c6d8325 commit b68ff3f2d6eb06581378ed9629073740c11752ed @steven676 steven676 committed May 15, 2012
@@ -33,4 +33,10 @@
android:layout_height="wrap_content"
android:text="@string/run_script" />
+ <Button
+ android:id="@+id/runScriptSaveWindow"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/run_script_save_window" />
+
</LinearLayout>
@@ -4,6 +4,8 @@
<string name="open_new_window">Open New Window</string>
<string name="script_label">Script:</string>
<string name="run_script">Run Script</string>
+ <string name="run_script_save_window">Run Script and Save Window</string>
+ <string name="run_script_existing_window">Run Script in Saved Window</string>
<string name="help">This sample shows how to send intents to Android Terminal Emulator.</string>
<!-- Do not translate: -->
@@ -10,6 +10,9 @@
public class IntentSampleActivity extends Activity
{
+ private String mHandle;
+ private static final int REQUEST_WINDOW_HANDLE = 1;
+
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
@@ -19,6 +22,7 @@ public void onCreate(Bundle savedInstanceState)
addClickListener(R.id.openNewWindow, new OnClickListener() {
@Override
public void onClick(View v) {
+ // Intent for opening a new window without providing script
Intent intent =
new Intent("jackpal.androidterm.OPEN_NEW_WINDOW");
intent.addCategory(Intent.CATEGORY_DEFAULT);
@@ -30,16 +34,54 @@ public void onClick(View v) {
addClickListener(R.id.runScript, new OnClickListener() {
@Override
public void onClick(View v) {
+ /* Intent for opening a new window and running the provided
+ script -- you must declare the permission
+ jackpal.androidterm.permission.RUN_SCRIPT in your manifest
+ to use */
Intent intent =
new Intent("jackpal.androidterm.RUN_SCRIPT");
intent.addCategory(Intent.CATEGORY_DEFAULT);
String command = script.getText().toString();
intent.putExtra("jackpal.androidterm.iInitialCommand", command);
startActivity(intent);
}});
+ addClickListener(R.id.runScriptSaveWindow, new OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ /* Intent for running a script in a previously opened window,
+ if it still exists
+ This will open another window if it doesn't find a match */
+ Intent intent =
+ new Intent("jackpal.androidterm.RUN_SCRIPT");
+ intent.addCategory(Intent.CATEGORY_DEFAULT);
+ String command = script.getText().toString();
+ intent.putExtra("jackpal.androidterm.iInitialCommand", command);
+ if (mHandle != null) {
+ // Identify the targeted window by its handle
+ intent.putExtra("jackpal.androidterm.window_handle",
+ mHandle);
+ }
+ /* The handle for the targeted window -- whether newly opened
+ or reused -- is returned to us via onActivityResult()
+ You can compare it against an existing saved handle to
+ determine whether or not a new window was opened */
+ startActivityForResult(intent, REQUEST_WINDOW_HANDLE);
+ }});
}
private void addClickListener(int buttonId, OnClickListener onClickListener) {
((Button) findViewById(buttonId)).setOnClickListener(onClickListener);
}
+
+ protected void onActivityResult(int request, int result, Intent data) {
+ if (result != RESULT_OK) {
+ return;
+ }
+
+ if (request == REQUEST_WINDOW_HANDLE && data != null) {
+ mHandle = data.getStringExtra("jackpal.androidterm.window_handle");
+ ((Button) findViewById(R.id.runScriptSaveWindow)).setText(
+ R.string.run_script_existing_window);
+ }
+ }
}

0 comments on commit b68ff3f

Please sign in to comment.