aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/cgSearchHandler.java
blob: 6d38ea184fd19841bf96e9131454a60242887f5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cgeo.geocaching;

import cgeo.geocaching.utils.Log;

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.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("Failed to download reCAPTCHA image");
            }
        }
    }
}