summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/dialog/GpodnetSetHostnameDialog.java
blob: a9c596d2ea5c675054bf095a0f8d1a9b23b75fd6 (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
package de.danoeh.antennapod.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.InputType;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.gpoddernet.GpodnetService;
import de.danoeh.antennapod.preferences.GpodnetPreferences;

/**
 * Creates a dialog that lets the user change the hostname for the gpodder.net service.
 */
public class GpodnetSetHostnameDialog {
    private static final String TAG = "GpodnetSetHostnameDialog";

    public static AlertDialog createDialog(final Context context) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        final EditText et = new EditText(context);
        et.setText(GpodnetPreferences.getHostname());
        et.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
        dialog.setTitle(R.string.pref_gpodnet_sethostname_title)
                .setView(setupContentView(context, et))
                .setPositiveButton(R.string.confirm_label, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        final Editable e = et.getText();
                        if (e != null) {
                            GpodnetPreferences.setHostname(e.toString());
                        }
                        dialog.dismiss();
                    }
                })
                .setNegativeButton(R.string.cancel_label, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                })
                .setNeutralButton(R.string.pref_gpodnet_sethostname_use_default_host, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        GpodnetPreferences.setHostname(GpodnetService.DEFAULT_BASE_HOST);
                        dialog.dismiss();
                    }
                })
                .setCancelable(true);
        return dialog.show();
    }

    private static View setupContentView(Context context, EditText et) {
        LinearLayout ll = new LinearLayout(context);
        ll.addView(et);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) et.getLayoutParams();
        if (params != null) {
            params.setMargins(8, 8, 8, 8);
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        }
        return ll;
    }
}