From 9d531d717af377fb304527723be52f7562cc16c3 Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Mon, 16 Jul 2012 15:00:52 +0200 Subject: [PATCH] Add some code to pop up an inputbox on android. --- .../turboviking/libnative/NativeActivity.java | 57 +++++++++++++++++++ base/PCMain.cpp | 3 +- ui/screen.cpp | 7 ++- ui/screen.h | 3 + 4 files changed, 67 insertions(+), 3 deletions(-) diff --git a/android/src/com/turboviking/libnative/NativeActivity.java b/android/src/com/turboviking/libnative/NativeActivity.java index 6b88954992..be456068c0 100644 --- a/android/src/com/turboviking/libnative/NativeActivity.java +++ b/android/src/com/turboviking/libnative/NativeActivity.java @@ -9,7 +9,9 @@ import java.util.UUID; import android.annotation.SuppressLint; import android.app.Activity; import android.app.ActivityManager; +import android.app.AlertDialog; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; @@ -26,10 +28,14 @@ import android.os.Environment; import android.util.DisplayMetrics; import android.util.Log; import android.view.Display; +import android.view.Gravity; import android.view.KeyEvent; import android.view.View; +import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; +import android.widget.EditText; +import android.widget.FrameLayout; import android.widget.Toast; class Installation { @@ -84,6 +90,8 @@ public class NativeActivity extends Activity { public static String installID; + private EditText editText; + String getApplicationLibraryDir(ApplicationInfo application) { String libdir = null; try { @@ -168,6 +176,14 @@ public class NativeActivity extends Activity { if (!useOpenSL) audioPlayer = new NativeAudioPlayer(); lightsOut(); + + /* + editText = new EditText(this); + editText.setText("Hello world"); + + addContentView(editText, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + */ + inputBox("Please ener a s", "", "Save"); } @SuppressLint("NewApi") @@ -281,6 +297,45 @@ public class NativeActivity extends Activity { super.onConfigurationChanged(newConfig); } + public static boolean inputBoxCancelled; + @SuppressWarnings("deprecation") + public String inputBox(String title, String defaultText, String defaultAction) { + final FrameLayout fl = new FrameLayout(this); + final EditText input = new EditText(this); + input.setGravity(Gravity.CENTER); + + inputBoxCancelled = false; + FrameLayout.LayoutParams editBoxLayout = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT); + editBoxLayout.setMargins(2, 20, 2, 20); + fl.addView(input, editBoxLayout); + + input.setText(defaultText); + input.selectAll(); + + AlertDialog dlg = new AlertDialog.Builder(this) + .setView(fl) + .setTitle(title) + .setPositiveButton(defaultAction, new DialogInterface.OnClickListener(){ + @Override + public void onClick(DialogInterface d, int which) { + d.dismiss(); + } + }) + .setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ + @Override + public void onClick(DialogInterface d, int which) { + d.cancel(); + NativeActivity.inputBoxCancelled = false; + } + }).create(); + dlg.setCancelable(false); + dlg.show(); + if (inputBoxCancelled) + return null; + // Toast.makeText(this, "Value: " + input.getText().toString(), Toast.LENGTH_LONG).show(); + return input.getText().toString(); + } + public void processCommand(String command, String params) { if (command.equals("launchBrowser")) { Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(params)); @@ -304,6 +359,8 @@ public class NativeActivity extends Activity { } else if (command.equals("showkeyboard")) { //InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); //inputMethodManager.toggleSoftInputFromWindow(this, InputMethodManager.SHOW_FORCED, 0); + } else if (command.equals("gettext")) { + inputBox("Enter text", params, "OK"); } else { Log.e(TAG, "Unsupported command " + command + " , param: " + params); } diff --git a/base/PCMain.cpp b/base/PCMain.cpp index 7540cf1d22..8739633a48 100644 --- a/base/PCMain.cpp +++ b/base/PCMain.cpp @@ -25,9 +25,10 @@ #include "base/NativeApp.h" #include "net/resolve.h" - // Simple implementations of System functions + + void SystemToast(const char *text) { #ifdef _WIN32 MessageBox(0, text, "Toast!", MB_ICONINFORMATION); diff --git a/ui/screen.cpp b/ui/screen.cpp index 581c3eabd9..548ae4fb58 100644 --- a/ui/screen.cpp +++ b/ui/screen.cpp @@ -21,9 +21,11 @@ void ScreenManager::switchScreen(Screen *screen) { // will only become apparent if the dialog is closed. The previous screen will stick around // until that switch. if (nextScreen_ != 0) { - FLOG("WTF? Already had nextScreen_"); + FLOG("WTF? Already had a nextScreen_"); + } + if (screen != currentScreen_) { + nextScreen_ = screen; } - nextScreen_ = screen; } void ScreenManager::update(const InputState &input) { @@ -47,6 +49,7 @@ void ScreenManager::update(const InputState &input) { void ScreenManager::render() { if (dialog_.size()) { dialog_.back()->render(); + return; } if (currentScreen_) { currentScreen_->render(); diff --git a/ui/screen.h b/ui/screen.h index 5dcd8a841b..cb168ec1d2 100644 --- a/ui/screen.h +++ b/ui/screen.h @@ -14,6 +14,7 @@ #pragma once #include +#include "base/basictypes.h" #include "base/display.h" struct InputState; @@ -25,6 +26,8 @@ public: virtual void update(const InputState &input) = 0; virtual void render() {} virtual void dialogFinished(const Screen &dialog) {} +private: + DISALLOW_COPY_AND_ASSIGN(Screen); }; class Transition {