aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/cgSearchHandler.java
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2011-09-16 14:36:28 +0200
committerSamuel Tardieu <sam@rfc1149.net>2011-09-16 14:36:28 +0200
commit579ef7a535489d4aa632db11667a3b01deb6cafd (patch)
tree55810021c02ac7d80d3a9702ef0b59e4af154b9c /main/src/cgeo/geocaching/cgSearchHandler.java
parent96ea21fd50334479c262da692038965d0e4d596a (diff)
downloadcgeo-579ef7a535489d4aa632db11667a3b01deb6cafd.zip
cgeo-579ef7a535489d4aa632db11667a3b01deb6cafd.tar.gz
cgeo-579ef7a535489d4aa632db11667a3b01deb6cafd.tar.bz2
Move sources into the main directory
This prepares the inclusion of tests into the same repository.
Diffstat (limited to 'main/src/cgeo/geocaching/cgSearchHandler.java')
-rw-r--r--main/src/cgeo/geocaching/cgSearchHandler.java105
1 files changed, 105 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/cgSearchHandler.java b/main/src/cgeo/geocaching/cgSearchHandler.java
new file mode 100644
index 0000000..4fc6200
--- /dev/null
+++ b/main/src/cgeo/geocaching/cgSearchHandler.java
@@ -0,0 +1,105 @@
+package cgeo.geocaching;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
+import android.content.res.Resources;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.os.Handler;
+import android.os.Message;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.EditText;
+import android.widget.ImageView;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+public class cgSearchHandler extends Handler {
+ private Activity activity = null;
+ private Resources res = null;
+ private cgSearchThread recaptchaThread = null;
+ private ImageView imageView = null;
+ private Bitmap img = null;
+
+ private Handler imgHandler = new Handler() {
+ @Override
+ public void handleMessage(Message msg) {
+ try {
+ if (img != null && imageView != null) {
+ imageView.setImageBitmap(img);
+ }
+ } catch (Exception e) {
+ // nothing
+ }
+ }
+ };
+
+ public cgSearchHandler(Activity activityIn, Resources resIn, cgSearchThread recaptchaThreadIn) {
+ activity = activityIn;
+ res = resIn;
+ recaptchaThread = recaptchaThreadIn;
+ }
+
+ @Override
+ public void handleMessage(Message msg) {
+ try {
+ if (msg.what == 1) {
+ final AlertDialog.Builder dlg = new AlertDialog.Builder(activity);
+ final LayoutInflater inflater = activity.getLayoutInflater();
+ final View view = inflater.inflate(R.layout.recaptcha_dialog, null);
+
+ imageView = (ImageView) view.findViewById(R.id.image);
+
+ (new getCaptcha(new URL("http://www.google.com/recaptcha/api/image?c=" + recaptchaThread.getChallenge()))).start();
+
+ dlg.setTitle(res.getString(R.string.caches_recaptcha_title));
+ dlg.setView(view);
+ dlg.setNeutralButton(res.getString(R.string.caches_recaptcha_continue), new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int id) {
+ final String text = ((EditText) view.findViewById(R.id.text)).getText().toString();
+
+ recaptchaThread.setText(text);
+
+ dialog.cancel();
+ }
+ });
+
+ dlg.create().show();
+ }
+ } catch (Exception e) {
+ // nothing
+ }
+ }
+
+ private class getCaptcha extends Thread {
+ private URL uri = null;
+
+ public getCaptcha(URL uriIn) {
+ uri = uriIn;
+ }
+
+ @Override
+ public void run() {
+ try {
+ HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
+ connection.setDoInput(true);
+ connection.connect();
+
+ InputStream is = connection.getInputStream();
+
+ img = BitmapFactory.decodeStream(is);
+
+ is.close();
+
+ imgHandler.sendEmptyMessage(0);
+ } catch (IOException e) {
+ Log.e(cgSettings.tag, "Failed to download reCAPTCHA image");
+ }
+ }
+ }
+}