1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.R
.string
.logging_in
;
4 import static ro
.ieval
.fonbot
.Utils
.toNonNull
;
6 import java
.io
.InputStream
;
7 import org
.eclipse
.jdt
.annotation
.Nullable
;
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
;
33 * Copyright © 2013 Marius Gavrilescu
35 * This file is part of FonBot.
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.
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.
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/>.
52 * Main Activity. Shows the login status and a list of ongoing events.
54 * @author Marius Gavrilescu <marius@ieval.ro>
56 public final class FonBotMainActivity
extends ListActivity
{
58 * Adapter for the ongoing event list. Shows a list of ongoing events with cancel buttons.
60 * @author Marius Gavrilescu
62 private static final class ArrayAdapter
extends android
.widget
.ArrayAdapter
<OngoingEvent
>{
64 * Constructs an ArrayAdapter with a given list of events
66 * @param context Context instance
67 * @param objects the list of events
69 public ArrayAdapter(final Context context
, final OngoingEvent
[] objects
) {
70 super(context
, 0, 0, objects
);
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
);
78 if(convertView
instanceof LinearLayout
){
81 listItem
=inflater
.inflate(R
.layout
.ongoing_list_item
, parent
, false);
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
);
87 textView
.setText(event
.resource
);
88 button
.setOnClickListener(new OnClickListener() {
90 public void onClick(final @Nullable View v
) {
91 Heavy
.cancelOngoing(toNonNull(getContext()), event
);
100 * ServiceConnection for getting the ongoing event list from {@link FonBotMainService}
102 * @author Marius Gavrilescu <marius@ieval.ro>
104 private final class ServiceConnection
implements android
.content
.ServiceConnection
{
105 /** Binder got from onServiceConnected */
106 private Binder binder
;
109 public void onServiceDisconnected(final @Nullable ComponentName name
) {
114 public void onServiceConnected(final @Nullable ComponentName name
, final @Nullable IBinder service
) {
117 binder
=(Binder
) service
;
122 * Updates the list of ongoing events from the service.
124 public void refreshAdapter(){
127 setListAdapter(new ArrayAdapter(FonBotMainActivity
.this,
128 toNonNull(binder
.getService().getOngoingEvents().toArray(new OngoingEvent
[0]))));
133 * BroadcastReceiver that refreshes the ongoing event list.
135 * @author Marius Gavrilescu <marius@ieval.ro>
137 private final class OngoingUpdateReceiver
extends BroadcastReceiver
{
139 public void onReceive(final @Nullable Context context
, final @Nullable Intent intent
) {
140 connection
.refreshAdapter();
145 * Implementation of <code>ResultCallback</code> that updates the {@link #resultTextView}
147 * @author Marius Gavrilescu
149 public final class UpdateResultCallback
implements ResultCallback
{
151 public void onResult(final int responseCode
, final String responseMessage
, final InputStream inputStream
) {
152 if(responseCode
>=200 && responseCode
<300)
153 updateResultTextView(responseMessage
, false);
155 updateResultTextView(responseMessage
, true);
159 public void onError(final String error
) {
160 updateResultTextView(error
, true);
164 * Update the {@link #resultTextView}.
166 * @param message new text for the TextView
167 * @param error true if the text is an error message, false otherwise
169 private void updateResultTextView(final String message
, final boolean error
){
170 handler
.post(new Runnable() {
173 resultTextView
.setText(message
);
175 resultTextView
.setCompoundDrawablesWithIntrinsicBounds(0, R
.drawable
.cross
, 0, 0);
177 resultTextView
.setCompoundDrawablesWithIntrinsicBounds(0, R
.drawable
.tick
, 0, 0);
184 * The one instance of {@link OngoingUpdateReceiver}
186 private final BroadcastReceiver ongoingUpdateReceiver
=new OngoingUpdateReceiver();
188 * IntentFilter for {@link #ongoingUpdateReceiver}
190 private static final IntentFilter ONGOING_UPDATE_FILTER
=new IntentFilter(FonBotMainService
.ACTION_ONGOING_UPDATE
);
192 * The one instance of {@link UpdateResultCallback}
194 private final UpdateResultCallback updateResultCallback
=new UpdateResultCallback();
196 * The one instance of {@link ServiceConnection}
198 private final ServiceConnection connection
=new ServiceConnection();
201 * TextView that tells the user whether logging in failed or succeded.
203 private TextView resultTextView
;
204 /** Handler instance */
205 private Handler handler
;
208 protected void onCreate(@Nullable final Bundle icicle
) {
209 super.onCreate(icicle
);
211 setContentView(R
.layout
.main
);
212 handler
=new Handler();
213 resultTextView
=(TextView
) findViewById(R
.id
.resultTextView
);
217 protected void onStart() {
219 resultTextView
.setText(logging_in
);
220 new HttpCallExecutableRunnable("/ok", null,
221 toNonNull(getApplicationContext()), toNonNull(updateResultCallback
), false).execute();
222 connection
.refreshAdapter();
226 protected void onResume() {
228 LocalBroadcastManager
.getInstance(this).registerReceiver(ongoingUpdateReceiver
, ONGOING_UPDATE_FILTER
);
229 bindService(new Intent(this, FonBotMainService
.class), connection
, 0);
233 protected void onPause() {
235 LocalBroadcastManager
.getInstance(this).unregisterReceiver(ongoingUpdateReceiver
);
236 unbindService(connection
);
240 public boolean onCreateOptionsMenu(@Nullable final Menu menu
) {
241 getMenuInflater().inflate(R
.menu
.main
, menu
);
246 public boolean onOptionsItemSelected(@Nullable final MenuItem item
) {
248 return super.onOptionsItemSelected(item
);
249 switch(item
.getItemId()){
251 startActivity(new Intent(this, FonBotPreferenceActivity
.class));
254 startActivity(new Intent(this, FonBotLocalActivity
.class));
257 startActivity(new Intent(this, FonBotHelpActivity
.class));
260 return super.onOptionsItemSelected(item
);