342606a506b8186d75c4badeec4068eaa65fb251
[fonbot.git] / src / ro / ieval / fonbot / FonBotMainActivity.java
1 package ro.ieval.fonbot;
2
3 import static ro.ieval.fonbot.R.string.logging_in;
4 import static ro.ieval.fonbot.Utils.toNonNull;
5
6 import java.io.InputStream;
7 import org.eclipse.jdt.annotation.Nullable;
8
9 import ro.ieval.fonbot.FonBotMainService.Binder;
10 import ro.ieval.fonbot.HttpCallExecutableRunnable.ResultCallback;
11 import ro.ieval.fonbot.Utils.OngoingEvent;
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;
18 import android.os.Bundle;
19 import android.os.Handler;
20 import android.os.IBinder;
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 /**
145 * Implementation of <code>ResultCallback</code> that updates the {@link #resultTextView}
146 *
147 * @author Marius Gavrilescu
148 */
149 public final class UpdateResultCallback implements ResultCallback{
150 @Override
151 public void onResult(final int responseCode, final String responseMessage, final InputStream inputStream) {
152 if(responseCode>=200 && responseCode<300)
153 updateResultTextView(responseMessage, false);
154 else
155 updateResultTextView(responseMessage, true);
156 }
157
158 @Override
159 public void onError(final String error) {
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);
176 else
177 resultTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.tick, 0, 0);
178 }
179 });
180 }
181 }
182
183 /**
184 * The one instance of {@link OngoingUpdateReceiver}
185 */
186 private final BroadcastReceiver ongoingUpdateReceiver=new OngoingUpdateReceiver();
187 /**
188 * IntentFilter for {@link #ongoingUpdateReceiver}
189 */
190 private static final IntentFilter ONGOING_UPDATE_FILTER=new IntentFilter(FonBotMainService.ACTION_ONGOING_UPDATE);
191 /**
192 * The one instance of {@link UpdateResultCallback}
193 */
194 private final UpdateResultCallback updateResultCallback=new UpdateResultCallback();
195 /**
196 * The one instance of {@link ServiceConnection}
197 */
198 private final ServiceConnection connection=new ServiceConnection();
199
200 /**
201 * TextView that tells the user whether logging in failed or succeded.
202 */
203 private TextView resultTextView;
204 /** Handler instance */
205 private Handler handler;
206
207 @Override
208 protected void onCreate(@Nullable final Bundle icicle) {
209 super.onCreate(icicle);
210
211 setContentView(R.layout.main);
212 handler=new Handler();
213 resultTextView=(TextView) findViewById(R.id.resultTextView);
214 }
215
216 @Override
217 protected void onStart() {
218 super.onStart();
219 resultTextView.setText(logging_in);
220 new HttpCallExecutableRunnable("/ok", null,
221 toNonNull(getApplicationContext()), toNonNull(updateResultCallback)).execute();
222 connection.refreshAdapter();
223 }
224
225 @Override
226 protected void onResume() {
227 super.onResume();
228 LocalBroadcastManager.getInstance(this).registerReceiver(ongoingUpdateReceiver, ONGOING_UPDATE_FILTER);
229 bindService(new Intent(this, FonBotMainService.class), connection, 0);
230 }
231
232 @Override
233 protected void onPause() {
234 super.onPause();
235 LocalBroadcastManager.getInstance(this).unregisterReceiver(ongoingUpdateReceiver);
236 unbindService(connection);
237 }
238
239 @Override
240 public boolean onCreateOptionsMenu(@Nullable final Menu menu) {
241 getMenuInflater().inflate(R.menu.main, menu);
242 return true;
243 }
244
245 @Override
246 public boolean onOptionsItemSelected(@Nullable final MenuItem item) {
247 if(item==null)
248 return super.onOptionsItemSelected(item);
249 switch(item.getItemId()){
250 case R.id.prefsItem:
251 startActivity(new Intent(this, FonBotPreferenceActivity.class));
252 return true;
253 case R.id.localItem:
254 startActivity(new Intent(this, FonBotLocalActivity.class));
255 return true;
256 case R.id.helpItem:
257 startActivity(new Intent(this, FonBotHelpActivity.class));
258 return true;
259 default:
260 return super.onOptionsItemSelected(item);
261 }
262 }
263 }
This page took 0.028287 seconds and 3 git commands to generate.