]>
Commit | Line | Data |
---|---|---|
8dfb76c9 MG |
1 | package ro.ieval.fonbot; |
2 | ||
d13f1533 | 3 | import static ro.ieval.fonbot.R.string.logging_in; |
8dfb76c9 MG |
4 | import static ro.ieval.fonbot.Utils.toNonNull; |
5 | ||
2e5049c9 | 6 | import java.io.InputStream; |
8dfb76c9 MG |
7 | import org.eclipse.jdt.annotation.Nullable; |
8 | ||
9 | import ro.ieval.fonbot.FonBotMainService.Binder; | |
2e5049c9 | 10 | import ro.ieval.fonbot.HttpCallExecutableRunnable.ResultCallback; |
8dfb76c9 | 11 | import ro.ieval.fonbot.Utils.OngoingEvent; |
8dfb76c9 MG |
12 | import android.app.ListActivity; |
13 | import android.content.BroadcastReceiver; | |
14 | import android.content.ComponentName; | |
15 | import android.content.Context; | |
16 | import android.content.Intent; | |
17 | import android.content.IntentFilter; | |
8dfb76c9 | 18 | import android.os.Bundle; |
b5986f47 | 19 | import android.os.Handler; |
8dfb76c9 | 20 | import android.os.IBinder; |
8dfb76c9 MG |
21 | import android.support.v4.content.LocalBroadcastManager; |
22 | import android.view.LayoutInflater; | |
23 | import android.view.Menu; | |
24 | import android.view.MenuItem; | |
25 | import android.view.View; | |
26 | import android.view.View.OnClickListener; | |
27 | import android.view.ViewGroup; | |
28 | import android.widget.Button; | |
29 | import android.widget.LinearLayout; | |
30 | import android.widget.TextView; | |
31 | ||
32 | /* | |
33 | * Copyright © 2013 Marius Gavrilescu | |
34 | * | |
35 | * This file is part of FonBot. | |
36 | * | |
37 | * FonBot is free software: you can redistribute it and/or modify | |
38 | * it under the terms of the GNU General Public License as published by | |
39 | * the Free Software Foundation, either version 3 of the License, or | |
40 | * (at your option) any later version. | |
41 | * | |
42 | * FonBot is distributed in the hope that it will be useful, | |
43 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
44 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
45 | * GNU General Public License for more details. | |
46 | * | |
47 | * You should have received a copy of the GNU General Public License | |
48 | * along with FonBot. If not, see <http://www.gnu.org/licenses/>. | |
49 | */ | |
50 | ||
51 | /** | |
52 | * Main Activity. Shows the login status and a list of ongoing events. | |
53 | * | |
54 | * @author Marius Gavrilescu <marius@ieval.ro> | |
55 | */ | |
56 | public final class FonBotMainActivity extends ListActivity { | |
57 | /** | |
58 | * Adapter for the ongoing event list. Shows a list of ongoing events with cancel buttons. | |
59 | * | |
60 | * @author Marius Gavrilescu | |
61 | */ | |
62 | private static final class ArrayAdapter extends android.widget.ArrayAdapter<OngoingEvent>{ | |
63 | /** | |
64 | * Constructs an ArrayAdapter with a given list of events | |
65 | * | |
66 | * @param context Context instance | |
67 | * @param objects the list of events | |
68 | */ | |
69 | public ArrayAdapter(final Context context, final OngoingEvent[] objects) { | |
70 | super(context, 0, 0, objects); | |
71 | } | |
72 | ||
73 | @Override | |
74 | public View getView(final int position, final @Nullable View convertView, final @Nullable ViewGroup parent) { | |
75 | final LayoutInflater inflater=(LayoutInflater) getContext().getSystemService(LAYOUT_INFLATER_SERVICE); | |
76 | final View listItem; | |
77 | ||
78 | if(convertView instanceof LinearLayout){ | |
79 | listItem=convertView; | |
80 | } else | |
81 | listItem=inflater.inflate(R.layout.ongoing_list_item, parent, false); | |
82 | ||
83 | final Button button=(Button) listItem.findViewById(R.id.ongoingItemButton); | |
84 | final TextView textView=(TextView) listItem.findViewById(R.id.ongoingItemTextView); | |
85 | final OngoingEvent event=getItem(position); | |
86 | ||
87 | textView.setText(event.resource); | |
88 | button.setOnClickListener(new OnClickListener() { | |
89 | @Override | |
90 | public void onClick(final @Nullable View v) { | |
91 | Heavy.cancelOngoing(toNonNull(getContext()), event); | |
92 | } | |
93 | }); | |
94 | ||
95 | return listItem; | |
96 | } | |
97 | } | |
98 | ||
99 | /** | |
100 | * ServiceConnection for getting the ongoing event list from {@link FonBotMainService} | |
101 | * | |
102 | * @author Marius Gavrilescu <marius@ieval.ro> | |
103 | */ | |
104 | private final class ServiceConnection implements android.content.ServiceConnection{ | |
105 | /** Binder got from onServiceConnected */ | |
106 | private Binder binder; | |
107 | ||
108 | @Override | |
109 | public void onServiceDisconnected(final @Nullable ComponentName name) { | |
110 | // do nothing | |
111 | } | |
112 | ||
113 | @Override | |
114 | public void onServiceConnected(final @Nullable ComponentName name, final @Nullable IBinder service) { | |
115 | if(service==null) | |
116 | return; | |
117 | binder=(Binder) service; | |
118 | refreshAdapter(); | |
119 | } | |
120 | ||
121 | /** | |
122 | * Updates the list of ongoing events from the service. | |
123 | */ | |
124 | public void refreshAdapter(){ | |
125 | if(binder==null) | |
126 | return; | |
127 | setListAdapter(new ArrayAdapter(FonBotMainActivity.this, | |
128 | toNonNull(binder.getService().getOngoingEvents().toArray(new OngoingEvent[0])))); | |
129 | } | |
130 | } | |
131 | ||
132 | /** | |
133 | * BroadcastReceiver that refreshes the ongoing event list. | |
134 | * | |
135 | * @author Marius Gavrilescu <marius@ieval.ro> | |
136 | */ | |
137 | private final class OngoingUpdateReceiver extends BroadcastReceiver{ | |
138 | @Override | |
139 | public void onReceive(final @Nullable Context context, final @Nullable Intent intent) { | |
140 | connection.refreshAdapter(); | |
141 | } | |
142 | } | |
143 | ||
144 | /** | |
b5986f47 | 145 | * Implementation of <code>ResultCallback</code> that updates the {@link #resultTextView} |
2e5049c9 MG |
146 | * |
147 | * @author Marius Gavrilescu | |
148 | */ | |
b5986f47 | 149 | public final class UpdateResultCallback implements ResultCallback{ |
2e5049c9 MG |
150 | @Override |
151 | public void onResult(final int responseCode, final String responseMessage, final InputStream inputStream) { | |
b5986f47 MG |
152 | if(responseCode>=200 && responseCode<300) |
153 | updateResultTextView(responseMessage, false); | |
154 | else | |
155 | updateResultTextView(responseMessage, true); | |
2e5049c9 MG |
156 | } |
157 | ||
158 | @Override | |
159 | public void onError(final String error) { | |
b5986f47 MG |
160 | updateResultTextView(error, true); |
161 | } | |
162 | ||
163 | /** | |
164 | * Update the {@link #resultTextView}. | |
165 | * | |
166 | * @param message new text for the TextView | |
167 | * @param error true if the text is an error message, false otherwise | |
168 | */ | |
169 | private void updateResultTextView(final String message, final boolean error){ | |
170 | handler.post(new Runnable() { | |
171 | @Override | |
172 | public void run() { | |
173 | resultTextView.setText(message); | |
174 | if(error) | |
175 | resultTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.cross, 0, 0); | |
c7b1fdf5 | 176 | else { |
b5986f47 | 177 | resultTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.tick, 0, 0); |
c7b1fdf5 MG |
178 | startService(new Intent(FonBotMainActivity.this, FonBotMainService.class)); |
179 | } | |
b5986f47 MG |
180 | } |
181 | }); | |
2e5049c9 MG |
182 | } |
183 | } | |
184 | ||
8dfb76c9 MG |
185 | /** |
186 | * The one instance of {@link OngoingUpdateReceiver} | |
187 | */ | |
188 | private final BroadcastReceiver ongoingUpdateReceiver=new OngoingUpdateReceiver(); | |
189 | /** | |
190 | * IntentFilter for {@link #ongoingUpdateReceiver} | |
191 | */ | |
192 | private static final IntentFilter ONGOING_UPDATE_FILTER=new IntentFilter(FonBotMainService.ACTION_ONGOING_UPDATE); | |
193 | /** | |
b5986f47 | 194 | * The one instance of {@link UpdateResultCallback} |
8dfb76c9 | 195 | */ |
b5986f47 | 196 | private final UpdateResultCallback updateResultCallback=new UpdateResultCallback(); |
8dfb76c9 MG |
197 | /** |
198 | * The one instance of {@link ServiceConnection} | |
199 | */ | |
200 | private final ServiceConnection connection=new ServiceConnection(); | |
201 | ||
202 | /** | |
203 | * TextView that tells the user whether logging in failed or succeded. | |
204 | */ | |
205 | private TextView resultTextView; | |
b5986f47 MG |
206 | /** Handler instance */ |
207 | private Handler handler; | |
8dfb76c9 MG |
208 | |
209 | @Override | |
210 | protected void onCreate(@Nullable final Bundle icicle) { | |
211 | super.onCreate(icicle); | |
212 | ||
213 | setContentView(R.layout.main); | |
b5986f47 | 214 | handler=new Handler(); |
8dfb76c9 MG |
215 | resultTextView=(TextView) findViewById(R.id.resultTextView); |
216 | } | |
217 | ||
218 | @Override | |
219 | protected void onStart() { | |
220 | super.onStart(); | |
8dfb76c9 | 221 | resultTextView.setText(logging_in); |
4d0abc18 | 222 | resultTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); |
2e5049c9 | 223 | new HttpCallExecutableRunnable("/ok", null, |
b26f32d6 | 224 | toNonNull(getApplicationContext()), toNonNull(updateResultCallback), false).execute(); |
8dfb76c9 MG |
225 | connection.refreshAdapter(); |
226 | } | |
227 | ||
228 | @Override | |
229 | protected void onResume() { | |
230 | super.onResume(); | |
8dfb76c9 MG |
231 | LocalBroadcastManager.getInstance(this).registerReceiver(ongoingUpdateReceiver, ONGOING_UPDATE_FILTER); |
232 | bindService(new Intent(this, FonBotMainService.class), connection, 0); | |
233 | } | |
234 | ||
235 | @Override | |
236 | protected void onPause() { | |
237 | super.onPause(); | |
8dfb76c9 MG |
238 | LocalBroadcastManager.getInstance(this).unregisterReceiver(ongoingUpdateReceiver); |
239 | unbindService(connection); | |
240 | } | |
241 | ||
242 | @Override | |
243 | public boolean onCreateOptionsMenu(@Nullable final Menu menu) { | |
244 | getMenuInflater().inflate(R.menu.main, menu); | |
245 | return true; | |
246 | } | |
247 | ||
248 | @Override | |
249 | public boolean onOptionsItemSelected(@Nullable final MenuItem item) { | |
250 | if(item==null) | |
251 | return super.onOptionsItemSelected(item); | |
252 | switch(item.getItemId()){ | |
253 | case R.id.prefsItem: | |
254 | startActivity(new Intent(this, FonBotPreferenceActivity.class)); | |
255 | return true; | |
256 | case R.id.localItem: | |
257 | startActivity(new Intent(this, FonBotLocalActivity.class)); | |
258 | return true; | |
259 | case R.id.helpItem: | |
260 | startActivity(new Intent(this, FonBotHelpActivity.class)); | |
261 | return true; | |
262 | default: | |
263 | return super.onOptionsItemSelected(item); | |
264 | } | |
265 | } | |
4d0abc18 | 266 | } |