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
.util
.Collections
;
8 import org
.eclipse
.jdt
.annotation
.Nullable
;
10 import ro
.ieval
.fonbot
.FonBotMainService
.Binder
;
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
.IBinder
;
20 import android
.support
.v4
.content
.LocalBroadcastManager
;
21 import android
.view
.LayoutInflater
;
22 import android
.view
.Menu
;
23 import android
.view
.MenuItem
;
24 import android
.view
.View
;
25 import android
.view
.View
.OnClickListener
;
26 import android
.view
.ViewGroup
;
27 import android
.widget
.Button
;
28 import android
.widget
.LinearLayout
;
29 import android
.widget
.TextView
;
32 * Copyright © 2013 Marius Gavrilescu
34 * This file is part of FonBot.
36 * FonBot is free software: you can redistribute it and/or modify
37 * it under the terms of the GNU General Public License as published by
38 * the Free Software Foundation, either version 3 of the License, or
39 * (at your option) any later version.
41 * FonBot is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Public License for more details.
46 * You should have received a copy of the GNU General Public License
47 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
51 * Main Activity. Shows the login status and a list of ongoing events.
53 * @author Marius Gavrilescu <marius@ieval.ro>
55 public final class FonBotMainActivity
extends ListActivity
{
57 * Adapter for the ongoing event list. Shows a list of ongoing events with cancel buttons.
59 * @author Marius Gavrilescu
61 private static final class ArrayAdapter
extends android
.widget
.ArrayAdapter
<OngoingEvent
>{
63 * Constructs an ArrayAdapter with a given list of events
65 * @param context Context instance
66 * @param objects the list of events
68 public ArrayAdapter(final Context context
, final OngoingEvent
[] objects
) {
69 super(context
, 0, 0, objects
);
73 public View
getView(final int position
, final @Nullable View convertView
, final @Nullable ViewGroup parent
) {
74 final LayoutInflater inflater
=(LayoutInflater
) getContext().getSystemService(LAYOUT_INFLATER_SERVICE
);
77 if(convertView
instanceof LinearLayout
){
80 listItem
=inflater
.inflate(R
.layout
.ongoing_list_item
, parent
, false);
82 final Button button
=(Button
) listItem
.findViewById(R
.id
.ongoingItemButton
);
83 final TextView textView
=(TextView
) listItem
.findViewById(R
.id
.ongoingItemTextView
);
84 final OngoingEvent event
=getItem(position
);
86 textView
.setText(event
.resource
);
87 button
.setOnClickListener(new OnClickListener() {
89 public void onClick(final @Nullable View v
) {
90 Heavy
.cancelOngoing(toNonNull(getContext()), event
);
99 * ServiceConnection for getting the ongoing event list from {@link FonBotMainService}
101 * @author Marius Gavrilescu <marius@ieval.ro>
103 private final class ServiceConnection
implements android
.content
.ServiceConnection
{
104 /** Binder got from onServiceConnected */
105 private Binder binder
;
108 public void onServiceDisconnected(final @Nullable ComponentName name
) {
113 public void onServiceConnected(final @Nullable ComponentName name
, final @Nullable IBinder service
) {
116 binder
=(Binder
) service
;
121 * Updates the list of ongoing events from the service.
123 public void refreshAdapter(){
126 setListAdapter(new ArrayAdapter(FonBotMainActivity
.this,
127 toNonNull(binder
.getService().getOngoingEvents().toArray(new OngoingEvent
[0]))));
132 * BroadcastReceiver that refreshes the ongoing event list.
134 * @author Marius Gavrilescu <marius@ieval.ro>
136 private final class OngoingUpdateReceiver
extends BroadcastReceiver
{
138 public void onReceive(final @Nullable Context context
, final @Nullable Intent intent
) {
139 connection
.refreshAdapter();
144 * BroadcastReceiver that updates {@link #resultTextView}
146 * @author Marius Gavrilescu <marius@ieval.ro>
148 private final class LoginReceiver
extends BroadcastReceiver
{
150 public void onReceive(@Nullable final Context context
, @Nullable final Intent intent
) {
153 resultTextView
.setText(intent
.getStringExtra(SendHttpMessageAsyncTask
.EXTRA_RESPONSE_MESSAGE
));
154 final int responseCode
=intent
.getIntExtra(SendHttpMessageAsyncTask
.EXTRA_RESPONSE_CODE
, 0);
155 if(responseCode
>=200&&responseCode
<300)
156 resultTextView
.setCompoundDrawablesWithIntrinsicBounds(0, R
.drawable
.tick
, 0, 0);
158 resultTextView
.setCompoundDrawablesWithIntrinsicBounds(0, R
.drawable
.cross
, 0, 0);
163 * The one instance of {@link OngoingUpdateReceiver}
165 private final BroadcastReceiver ongoingUpdateReceiver
=new OngoingUpdateReceiver();
167 * IntentFilter for {@link #ongoingUpdateReceiver}
169 private static final IntentFilter ONGOING_UPDATE_FILTER
=new IntentFilter(FonBotMainService
.ACTION_ONGOING_UPDATE
);
171 * The one instance of {@link LoginReceiver}
173 private final BroadcastReceiver loginReceiver
=new LoginReceiver();
175 * The broadcast sent by {@link SendHttpMessageAsyncTask} when logging in.
177 private static final String LOGIN_BROADCAST
="ro.ieval.fonbot.LOGIN_RESULT";
179 * IntentFilter for {@link #loginReceiver}
181 private static final IntentFilter LOGIN_FILTER
=new IntentFilter(LOGIN_BROADCAST
);
183 * The one instance of {@link ServiceConnection}
185 private final ServiceConnection connection
=new ServiceConnection();
188 * TextView that tells the user whether logging in failed or succeded.
190 private TextView resultTextView
;
193 protected void onCreate(@Nullable final Bundle icicle
) {
194 super.onCreate(icicle
);
196 setContentView(R
.layout
.main
);
198 resultTextView
=(TextView
) findViewById(R
.id
.resultTextView
);
202 protected void onStart() {
204 resultTextView
.setText(logging_in
);
205 new SendHttpMessageAsyncTask("/ok", toNonNull(Collections
.<Header
>emptySet()),
206 LOGIN_BROADCAST
,this).execute();
207 connection
.refreshAdapter();
211 protected void onResume() {
213 registerReceiver(loginReceiver
, LOGIN_FILTER
);
214 LocalBroadcastManager
.getInstance(this).registerReceiver(ongoingUpdateReceiver
, ONGOING_UPDATE_FILTER
);
215 bindService(new Intent(this, FonBotMainService
.class), connection
, 0);
219 protected void onPause() {
221 unregisterReceiver(loginReceiver
);
222 LocalBroadcastManager
.getInstance(this).unregisterReceiver(ongoingUpdateReceiver
);
223 unbindService(connection
);
227 public boolean onCreateOptionsMenu(@Nullable final Menu menu
) {
228 getMenuInflater().inflate(R
.menu
.main
, menu
);
233 public boolean onOptionsItemSelected(@Nullable final MenuItem item
) {
235 return super.onOptionsItemSelected(item
);
236 switch(item
.getItemId()){
238 startActivity(new Intent(this, FonBotPreferenceActivity
.class));
241 startActivity(new Intent(this, FonBotLocalActivity
.class));
244 startActivity(new Intent(this, FonBotHelpActivity
.class));
247 return super.onOptionsItemSelected(item
);