1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.R
.string
.*;
5 import static ro
.ieval
.fonbot
.Utils
.toNonNull
;
7 import java
.util
.Arrays
;
8 import org
.eclipse
.jdt
.annotation
.Nullable
;
10 import ro
.ieval
.fonbot
.FonBotMainService
.Binder
;
11 import ro
.ieval
.fonbot
.Utils
.OngoingEvent
;
13 import android
.app
.ListActivity
;
14 import android
.content
.BroadcastReceiver
;
15 import android
.content
.ComponentName
;
16 import android
.content
.Context
;
17 import android
.content
.Intent
;
18 import android
.content
.IntentFilter
;
19 import android
.content
.SharedPreferences
;
20 import android
.os
.Bundle
;
21 import android
.os
.IBinder
;
22 import android
.preference
.PreferenceManager
;
23 import android
.support
.v4
.content
.LocalBroadcastManager
;
24 import android
.view
.LayoutInflater
;
25 import android
.view
.Menu
;
26 import android
.view
.MenuItem
;
27 import android
.view
.View
;
28 import android
.view
.View
.OnClickListener
;
29 import android
.view
.ViewGroup
;
30 import android
.widget
.Button
;
31 import android
.widget
.LinearLayout
;
32 import android
.widget
.TextView
;
35 * Copyright © 2013 Marius Gavrilescu
37 * This file is part of FonBot.
39 * FonBot is free software: you can redistribute it and/or modify
40 * it under the terms of the GNU General Public License as published by
41 * the Free Software Foundation, either version 3 of the License, or
42 * (at your option) any later version.
44 * FonBot is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
49 * You should have received a copy of the GNU General Public License
50 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
54 * Main Activity. Shows the login status and a list of ongoing events.
56 * @author Marius Gavrilescu <marius@ieval.ro>
58 public final class FonBotMainActivity
extends ListActivity
{
60 * Adapter for the ongoing event list. Shows a list of ongoing events with cancel buttons.
62 * @author Marius Gavrilescu
64 private static final class ArrayAdapter
extends android
.widget
.ArrayAdapter
<OngoingEvent
>{
66 * Constructs an ArrayAdapter with a given list of events
68 * @param context Context instance
69 * @param objects the list of events
71 public ArrayAdapter(final Context context
, final OngoingEvent
[] objects
) {
72 super(context
, 0, 0, objects
);
76 public View
getView(final int position
, final @Nullable View convertView
, final @Nullable ViewGroup parent
) {
77 final LayoutInflater inflater
=(LayoutInflater
) getContext().getSystemService(LAYOUT_INFLATER_SERVICE
);
80 if(convertView
instanceof LinearLayout
){
83 listItem
=inflater
.inflate(R
.layout
.ongoing_list_item
, parent
, false);
85 final Button button
=(Button
) listItem
.findViewById(R
.id
.ongoingItemButton
);
86 final TextView textView
=(TextView
) listItem
.findViewById(R
.id
.ongoingItemTextView
);
87 final OngoingEvent event
=getItem(position
);
89 textView
.setText(event
.resource
);
90 button
.setOnClickListener(new OnClickListener() {
92 public void onClick(final @Nullable View v
) {
93 Heavy
.cancelOngoing(toNonNull(getContext()), event
);
102 * ServiceConnection for getting the ongoing event list from {@link FonBotMainService}
104 * @author Marius Gavrilescu <marius@ieval.ro>
106 private final class ServiceConnection
implements android
.content
.ServiceConnection
{
107 /** Binder got from onServiceConnected */
108 private Binder binder
;
111 public void onServiceDisconnected(final @Nullable ComponentName name
) {
116 public void onServiceConnected(final @Nullable ComponentName name
, final @Nullable IBinder service
) {
119 binder
=(Binder
) service
;
124 * Updates the list of ongoing events from the service.
126 public void refreshAdapter(){
129 setListAdapter(new ArrayAdapter(FonBotMainActivity
.this,
130 toNonNull(binder
.getService().getOngoingEvents().toArray(new OngoingEvent
[0]))));
135 * BroadcastReceiver that refreshes the ongoing event list.
137 * @author Marius Gavrilescu <marius@ieval.ro>
139 private final class OngoingUpdateReceiver
extends BroadcastReceiver
{
141 public void onReceive(final @Nullable Context context
, final @Nullable Intent intent
) {
142 connection
.refreshAdapter();
147 * BroadcastReceiver that updates {@link #resultTextView}
149 * @author Marius Gavrilescu <marius@ieval.ro>
151 private final class LoginReceiver
extends BroadcastReceiver
{
153 public void onReceive(@Nullable final Context context
, @Nullable final Intent intent
) {
156 resultTextView
.setText(intent
.getStringExtra(SendHttpMessageAsyncTask
.EXTRA_RESPONSE_MESSAGE
));
157 final int responseCode
=intent
.getIntExtra(SendHttpMessageAsyncTask
.EXTRA_RESPONSE_CODE
, 0);
158 if(responseCode
>=200&&responseCode
<300)
159 resultTextView
.setCompoundDrawablesWithIntrinsicBounds(0, R
.drawable
.tick
, 0, 0);
161 resultTextView
.setCompoundDrawablesWithIntrinsicBounds(0, R
.drawable
.cross
, 0, 0);
166 * The one instance of {@link OngoingUpdateReceiver}
168 private final BroadcastReceiver ongoingUpdateReceiver
=new OngoingUpdateReceiver();
170 * IntentFilter for {@link #ongoingUpdateReceiver}
172 private static final IntentFilter ONGOING_UPDATE_FILTER
=new IntentFilter(FonBotMainService
.ACTION_ONGOING_UPDATE
);
174 * The one instance of {@link LoginReceiver}
176 private final BroadcastReceiver loginReceiver
=new LoginReceiver();
178 * The broadcast sent by {@link SendHttpMessageAsyncTask} when logging in.
180 private static final String LOGIN_BROADCAST
="ro.ieval.fonbot.LOGIN_RESULT";
182 * IntentFilter for {@link #loginReceiver}
184 private static final IntentFilter LOGIN_FILTER
=new IntentFilter(LOGIN_BROADCAST
);
186 * The one instance of {@link ServiceConnection}
188 private final ServiceConnection connection
=new ServiceConnection();
191 * TextView that tells the user whether logging in failed or succeded.
193 private TextView resultTextView
;
196 protected void onCreate(@Nullable final Bundle icicle
) {
197 super.onCreate(icicle
);
199 setContentView(R
.layout
.main
);
201 resultTextView
=(TextView
) findViewById(R
.id
.resultTextView
);
205 protected void onStart() {
207 final SharedPreferences sp
=PreferenceManager
.getDefaultSharedPreferences(this);
208 resultTextView
.setText(logging_in
);
209 new SendHttpMessageAsyncTask(toNonNull(Arrays
.asList(
210 new Header("X-Action","LOGIN")
211 )),LOGIN_BROADCAST
,this).execute(
212 sp
.getString("username", null),
213 sp
.getString("password", null)
215 connection
.refreshAdapter();
219 protected void onResume() {
221 registerReceiver(loginReceiver
, LOGIN_FILTER
);
222 LocalBroadcastManager
.getInstance(this).registerReceiver(ongoingUpdateReceiver
, ONGOING_UPDATE_FILTER
);
223 bindService(new Intent(this, FonBotMainService
.class), connection
, 0);
227 protected void onPause() {
229 unregisterReceiver(loginReceiver
);
230 LocalBroadcastManager
.getInstance(this).unregisterReceiver(ongoingUpdateReceiver
);
231 unbindService(connection
);
235 public boolean onCreateOptionsMenu(@Nullable final Menu menu
) {
236 getMenuInflater().inflate(R
.menu
.main
, menu
);
241 public boolean onOptionsItemSelected(@Nullable final MenuItem item
) {
243 return super.onOptionsItemSelected(item
);
244 switch(item
.getItemId()){
246 startActivity(new Intent(this, FonBotPreferenceActivity
.class));
249 startActivity(new Intent(this, FonBotLocalActivity
.class));
252 startActivity(new Intent(this, FonBotHelpActivity
.class));
255 return super.onOptionsItemSelected(item
);