]>
Commit | Line | Data |
---|---|---|
1 | package ro.ieval.fonbot; | |
2 | ||
3 | import static ro.ieval.fonbot.R.string.*; | |
4 | ||
5 | import static ro.ieval.fonbot.Utils.toNonNull; | |
6 | ||
7 | import org.eclipse.jdt.annotation.Nullable; | |
8 | ||
9 | import android.app.Activity; | |
10 | import android.os.Bundle; | |
11 | import android.view.View; | |
12 | import android.view.View.OnClickListener; | |
13 | import android.widget.Button; | |
14 | import android.widget.LinearLayout; | |
15 | import android.widget.TextView; | |
16 | ||
17 | /* | |
18 | * Copyright © 2013 Marius Gavrilescu | |
19 | * | |
20 | * This file is part of FonBot. | |
21 | * | |
22 | * FonBot is free software: you can redistribute it and/or modify | |
23 | * it under the terms of the GNU General Public License as published by | |
24 | * the Free Software Foundation, either version 3 of the License, or | |
25 | * (at your option) any later version. | |
26 | * | |
27 | * FonBot is distributed in the hope that it will be useful, | |
28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
30 | * GNU General Public License for more details. | |
31 | * | |
32 | * You should have received a copy of the GNU General Public License | |
33 | * along with FonBot. If not, see <http://www.gnu.org/licenses/>. | |
34 | */ | |
35 | ||
36 | /** | |
37 | * A dialog created by the {@link Utils.Command#DIALOG DIALOG} command. Includes a message and a list of buttons, all user-defined | |
38 | * | |
39 | * @author Marius Gavrilescu <marius@ieval.ro> | |
40 | */ | |
41 | public final class DialogActivity extends Activity { | |
42 | /** Extra: string array of dialog buttons */ | |
43 | public static final String EXTRA_BUTTONS = "buttons"; | |
44 | /** Extra: dialog message */ | |
45 | public static final String EXTRA_MESSAGE = "message"; | |
46 | /** Extra: reply Address */ | |
47 | public static final String EXTRA_REPLYTO = "replyto"; | |
48 | ||
49 | /** true if the dialog is finishing because the user tapped a button, false otherwise */ | |
50 | private boolean gotAnswer=false; | |
51 | /** Reply address */ | |
52 | private Address address; | |
53 | ||
54 | @Override | |
55 | protected void onCreate(@Nullable final Bundle icicle) { | |
56 | super.onCreate(icicle); | |
57 | address=new Address(toNonNull(getIntent().getStringExtra(EXTRA_REPLYTO))); | |
58 | final LinearLayout layout=new LinearLayout(this); | |
59 | layout.setOrientation(LinearLayout.VERTICAL); | |
60 | if(getIntent().hasExtra(EXTRA_MESSAGE)){ | |
61 | final String text=getIntent().getStringExtra(EXTRA_MESSAGE); | |
62 | final TextView tv=new TextView(this); | |
63 | tv.setText(text); | |
64 | layout.addView(tv); | |
65 | } | |
66 | final String[] buttonLabels=getIntent().getStringArrayExtra(EXTRA_BUTTONS); | |
67 | ||
68 | final OnClickListener buttonListener=new OnClickListener() { | |
69 | @Override | |
70 | public void onClick(@Nullable final View v) { | |
71 | if(v==null) | |
72 | return; | |
73 | ||
74 | Utils.sendMessage(DialogActivity.this, toNonNull(address), | |
75 | toNonNull(getString(user_tapped_fmt, ((Button)v).getText()))); | |
76 | gotAnswer=true; | |
77 | finish(); | |
78 | } | |
79 | }; | |
80 | for(final String label : buttonLabels){ | |
81 | final Button b=new Button(this);//NOPMD buttons must be different | |
82 | b.setText(label); | |
83 | b.setOnClickListener(buttonListener); | |
84 | layout.addView(b); | |
85 | } | |
86 | setContentView(layout); | |
87 | } | |
88 | ||
89 | @Override | |
90 | protected void onStop() { | |
91 | super.onStop(); | |
92 | if(!gotAnswer){ | |
93 | Utils.sendMessage(this, toNonNull(address), | |
94 | toNonNull(getString(user_navigated_away_from_dialog))); | |
95 | gotAnswer=true; | |
96 | } | |
97 | finish(); | |
98 | } | |
99 | ||
100 | @Override | |
101 | public void onBackPressed() { | |
102 | if(!gotAnswer){ | |
103 | Utils.sendMessage(this, toNonNull(address), | |
104 | toNonNull(getString(user_canceled_dialog))); | |
105 | gotAnswer=true; | |
106 | } | |
107 | finish(); | |
108 | } | |
109 | } |