aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/connector/gc/RecaptchaHandler.java
blob: 5bcfd1747b4a7fc5d4036941bef79b92c0be7f26 (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
107
108
109
110
111
112
113
114
115
116
117
package cgeo.geocaching.connector.gc;

import butterknife.ButterKnife;

import cgeo.geocaching.R;
import cgeo.geocaching.loaders.RecaptchaReceiver;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.RxUtils;

import org.apache.commons.io.IOUtils;

import rx.Observable;
import rx.android.observables.AndroidObservable;
import rx.functions.Action1;
import rx.functions.Func0;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

import java.io.InputStream;

public class RecaptchaHandler extends Handler {
    final public static int SHOW_CAPTCHA = 1;

    final private Activity activity;
    final private RecaptchaReceiver recaptchaReceiver;

    public RecaptchaHandler(final Activity activity, final RecaptchaReceiver recaptchaReceiver) {
        this.activity = activity;
        this.recaptchaReceiver = recaptchaReceiver;
    }

    private void loadChallenge(final ImageView imageView, final View reloadButton, final boolean needsFetch) {
        final Observable<Bitmap> captcha = Observable.defer(new Func0<Observable<? extends Bitmap>>() {
            @Override
            public Observable<? extends Bitmap> call() {
                if (needsFetch) {
                    recaptchaReceiver.fetchChallenge();
                }
                final String url = "http://www.google.com/recaptcha/api/image?c=" + recaptchaReceiver.getChallenge();
                final InputStream is = Network.getResponseStream(Network.getRequest(url));
                if (is != null) {
                    try {
                        final Bitmap img = BitmapFactory.decodeStream(is);
                        return Observable.from(img);
                    } catch (final Exception e) {
                        Log.e("RecaptchaHandler.getCaptcha", e);
                        return Observable.error(e);
                    } finally {
                        IOUtils.closeQuietly(is);
                    }
                }
                return Observable.empty();
            }
        });
        AndroidObservable.bindActivity(activity, captcha).subscribeOn(RxUtils.networkScheduler).subscribe(new Action1<Bitmap>() {
            @Override
            public void call(final Bitmap bitmap) {
                imageView.setImageBitmap(bitmap);
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(final Throwable throwable) {
                // Do nothing
            }
        });
        reloadButton.setEnabled(true);
    }

    @SuppressLint("InflateParams")
    @Override
    public void handleMessage(final Message msg) {
        if (msg.what == SHOW_CAPTCHA) {
            final AlertDialog.Builder dlg = new AlertDialog.Builder(activity);
            final View view = activity.getLayoutInflater().inflate(R.layout.recaptcha_dialog, null, false);

            final ImageView imageView = ButterKnife.findById(view, R.id.image);

            final ImageButton reloadButton = ButterKnife.findById(view, R.id.button_recaptcha_refresh);
            reloadButton.setEnabled(false);
            reloadButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    loadChallenge(imageView, reloadButton, true);
                }
            });

            loadChallenge(imageView, reloadButton, false);

            dlg.setTitle(activity.getString(R.string.caches_recaptcha_title));
            dlg.setView(view);
            dlg.setNeutralButton(activity.getString(R.string.caches_recaptcha_continue), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(final DialogInterface dialog, final int id) {
                    final EditText editText = ButterKnife.findById(view, R.id.text);
                    final String text = editText.getText().toString();
                    recaptchaReceiver.setText(text);
                    dialog.cancel();
                }
            });

            dlg.create().show();
        }
    }

}