Upgrade to the latest server protocol.
[fonbot.git] / src / ro / ieval / fonbot / FonBotMainActivity.java
CommitLineData
8dfb76c9
MG
1package ro.ieval.fonbot;
2
d13f1533 3import static ro.ieval.fonbot.R.string.logging_in;
8dfb76c9
MG
4import static ro.ieval.fonbot.Utils.toNonNull;
5
d13f1533
MG
6import java.util.Collections;
7
8dfb76c9
MG
8import org.eclipse.jdt.annotation.Nullable;
9
10import ro.ieval.fonbot.FonBotMainService.Binder;
11import ro.ieval.fonbot.Utils.OngoingEvent;
8dfb76c9
MG
12import android.app.ListActivity;
13import android.content.BroadcastReceiver;
14import android.content.ComponentName;
15import android.content.Context;
16import android.content.Intent;
17import android.content.IntentFilter;
8dfb76c9
MG
18import android.os.Bundle;
19import android.os.IBinder;
8dfb76c9
MG
20import android.support.v4.content.LocalBroadcastManager;
21import android.view.LayoutInflater;
22import android.view.Menu;
23import android.view.MenuItem;
24import android.view.View;
25import android.view.View.OnClickListener;
26import android.view.ViewGroup;
27import android.widget.Button;
28import android.widget.LinearLayout;
29import android.widget.TextView;
30
31/*
32 * Copyright © 2013 Marius Gavrilescu
33 *
34 * This file is part of FonBot.
35 *
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.
40 *
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.
45 *
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/>.
48 */
49
50/**
51 * Main Activity. Shows the login status and a list of ongoing events.
52 *
53 * @author Marius Gavrilescu <marius@ieval.ro>
54 */
55public final class FonBotMainActivity extends ListActivity {
56 /**
57 * Adapter for the ongoing event list. Shows a list of ongoing events with cancel buttons.
58 *
59 * @author Marius Gavrilescu
60 */
61 private static final class ArrayAdapter extends android.widget.ArrayAdapter<OngoingEvent>{
62 /**
63 * Constructs an ArrayAdapter with a given list of events
64 *
65 * @param context Context instance
66 * @param objects the list of events
67 */
68 public ArrayAdapter(final Context context, final OngoingEvent[] objects) {
69 super(context, 0, 0, objects);
70 }
71
72 @Override
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);
75 final View listItem;
76
77 if(convertView instanceof LinearLayout){
78 listItem=convertView;
79 } else
80 listItem=inflater.inflate(R.layout.ongoing_list_item, parent, false);
81
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);
85
86 textView.setText(event.resource);
87 button.setOnClickListener(new OnClickListener() {
88 @Override
89 public void onClick(final @Nullable View v) {
90 Heavy.cancelOngoing(toNonNull(getContext()), event);
91 }
92 });
93
94 return listItem;
95 }
96 }
97
98 /**
99 * ServiceConnection for getting the ongoing event list from {@link FonBotMainService}
100 *
101 * @author Marius Gavrilescu <marius@ieval.ro>
102 */
103 private final class ServiceConnection implements android.content.ServiceConnection{
104 /** Binder got from onServiceConnected */
105 private Binder binder;
106
107 @Override
108 public void onServiceDisconnected(final @Nullable ComponentName name) {
109 // do nothing
110 }
111
112 @Override
113 public void onServiceConnected(final @Nullable ComponentName name, final @Nullable IBinder service) {
114 if(service==null)
115 return;
116 binder=(Binder) service;
117 refreshAdapter();
118 }
119
120 /**
121 * Updates the list of ongoing events from the service.
122 */
123 public void refreshAdapter(){
124 if(binder==null)
125 return;
126 setListAdapter(new ArrayAdapter(FonBotMainActivity.this,
127 toNonNull(binder.getService().getOngoingEvents().toArray(new OngoingEvent[0]))));
128 }
129 }
130
131 /**
132 * BroadcastReceiver that refreshes the ongoing event list.
133 *
134 * @author Marius Gavrilescu <marius@ieval.ro>
135 */
136 private final class OngoingUpdateReceiver extends BroadcastReceiver{
137 @Override
138 public void onReceive(final @Nullable Context context, final @Nullable Intent intent) {
139 connection.refreshAdapter();
140 }
141 }
142
143 /**
144 * BroadcastReceiver that updates {@link #resultTextView}
145 *
146 * @author Marius Gavrilescu <marius@ieval.ro>
147 */
148 private final class LoginReceiver extends BroadcastReceiver {
149 @Override
150 public void onReceive(@Nullable final Context context, @Nullable final Intent intent) {
151 if(intent==null)
152 return;
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);
157 else
158 resultTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.cross, 0, 0);
159 }
160 }
161
162 /**
163 * The one instance of {@link OngoingUpdateReceiver}
164 */
165 private final BroadcastReceiver ongoingUpdateReceiver=new OngoingUpdateReceiver();
166 /**
167 * IntentFilter for {@link #ongoingUpdateReceiver}
168 */
169 private static final IntentFilter ONGOING_UPDATE_FILTER=new IntentFilter(FonBotMainService.ACTION_ONGOING_UPDATE);
170 /**
171 * The one instance of {@link LoginReceiver}
172 */
173 private final BroadcastReceiver loginReceiver=new LoginReceiver();
174 /**
175 * The broadcast sent by {@link SendHttpMessageAsyncTask} when logging in.
176 */
177 private static final String LOGIN_BROADCAST="ro.ieval.fonbot.LOGIN_RESULT";
178 /**
179 * IntentFilter for {@link #loginReceiver}
180 */
181 private static final IntentFilter LOGIN_FILTER=new IntentFilter(LOGIN_BROADCAST);
182 /**
183 * The one instance of {@link ServiceConnection}
184 */
185 private final ServiceConnection connection=new ServiceConnection();
186
187 /**
188 * TextView that tells the user whether logging in failed or succeded.
189 */
190 private TextView resultTextView;
191
192 @Override
193 protected void onCreate(@Nullable final Bundle icicle) {
194 super.onCreate(icicle);
195
196 setContentView(R.layout.main);
197
198 resultTextView=(TextView) findViewById(R.id.resultTextView);
199 }
200
201 @Override
202 protected void onStart() {
203 super.onStart();
8dfb76c9 204 resultTextView.setText(logging_in);
d13f1533
MG
205 new SendHttpMessageAsyncTask("/ok", toNonNull(Collections.<Header>emptySet()),
206 LOGIN_BROADCAST,this).execute();
8dfb76c9
MG
207 connection.refreshAdapter();
208 }
209
210 @Override
211 protected void onResume() {
212 super.onResume();
213 registerReceiver(loginReceiver, LOGIN_FILTER);
214 LocalBroadcastManager.getInstance(this).registerReceiver(ongoingUpdateReceiver, ONGOING_UPDATE_FILTER);
215 bindService(new Intent(this, FonBotMainService.class), connection, 0);
216 }
217
218 @Override
219 protected void onPause() {
220 super.onPause();
221 unregisterReceiver(loginReceiver);
222 LocalBroadcastManager.getInstance(this).unregisterReceiver(ongoingUpdateReceiver);
223 unbindService(connection);
224 }
225
226 @Override
227 public boolean onCreateOptionsMenu(@Nullable final Menu menu) {
228 getMenuInflater().inflate(R.menu.main, menu);
229 return true;
230 }
231
232 @Override
233 public boolean onOptionsItemSelected(@Nullable final MenuItem item) {
234 if(item==null)
235 return super.onOptionsItemSelected(item);
236 switch(item.getItemId()){
237 case R.id.prefsItem:
238 startActivity(new Intent(this, FonBotPreferenceActivity.class));
239 return true;
240 case R.id.localItem:
241 startActivity(new Intent(this, FonBotLocalActivity.class));
242 return true;
243 case R.id.helpItem:
244 startActivity(new Intent(this, FonBotHelpActivity.class));
245 return true;
246 default:
247 return super.onOptionsItemSelected(item);
248 }
249 }
250}
This page took 0.023441 seconds and 4 git commands to generate.