]> iEval git - fonbot.git/blame - src/ro/ieval/fonbot/FonBotMainActivity.java
Handle HTTP 401 and beautify HTTP messages
[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
2e5049c9 6import java.io.InputStream;
8dfb76c9
MG
7import org.eclipse.jdt.annotation.Nullable;
8
9import ro.ieval.fonbot.FonBotMainService.Binder;
2e5049c9 10import ro.ieval.fonbot.HttpCallExecutableRunnable.ResultCallback;
8dfb76c9 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 18import android.os.Bundle;
b5986f47 19import android.os.Handler;
8dfb76c9 20import android.os.IBinder;
8dfb76c9
MG
21import android.support.v4.content.LocalBroadcastManager;
22import android.view.LayoutInflater;
23import android.view.Menu;
24import android.view.MenuItem;
25import android.view.View;
26import android.view.View.OnClickListener;
27import android.view.ViewGroup;
28import android.widget.Button;
29import android.widget.LinearLayout;
30import 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 */
56public 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 /**
b5986f47 145 * Implementation of <code>ResultCallback</code> that updates the {@link #resultTextView}
2e5049c9
MG
146 *
147 * @author Marius Gavrilescu
148 */
b5986f47 149 public final class UpdateResultCallback implements ResultCallback{
2e5049c9
MG
150 @Override
151 public void onResult(final int responseCode, final String responseMessage, final InputStream inputStream) {
b5986f47
MG
152 if(responseCode>=200 && responseCode<300)
153 updateResultTextView(responseMessage, false);
154 else
155 updateResultTextView(responseMessage, true);
2e5049c9
MG
156 }
157
158 @Override
159 public void onError(final String error) {
b5986f47
MG
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 });
2e5049c9
MG
180 }
181 }
182
8dfb76c9
MG
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 /**
b5986f47 192 * The one instance of {@link UpdateResultCallback}
8dfb76c9 193 */
b5986f47 194 private final UpdateResultCallback updateResultCallback=new UpdateResultCallback();
8dfb76c9
MG
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;
b5986f47
MG
204 /** Handler instance */
205 private Handler handler;
8dfb76c9
MG
206
207 @Override
208 protected void onCreate(@Nullable final Bundle icicle) {
209 super.onCreate(icicle);
210
211 setContentView(R.layout.main);
b5986f47 212 handler=new Handler();
8dfb76c9
MG
213 resultTextView=(TextView) findViewById(R.id.resultTextView);
214 }
215
216 @Override
217 protected void onStart() {
218 super.onStart();
8dfb76c9 219 resultTextView.setText(logging_in);
4d0abc18 220 resultTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
2e5049c9 221 new HttpCallExecutableRunnable("/ok", null,
b26f32d6 222 toNonNull(getApplicationContext()), toNonNull(updateResultCallback), false).execute();
8dfb76c9
MG
223 connection.refreshAdapter();
224 }
225
226 @Override
227 protected void onResume() {
228 super.onResume();
8dfb76c9
MG
229 LocalBroadcastManager.getInstance(this).registerReceiver(ongoingUpdateReceiver, ONGOING_UPDATE_FILTER);
230 bindService(new Intent(this, FonBotMainService.class), connection, 0);
231 }
232
233 @Override
234 protected void onPause() {
235 super.onPause();
8dfb76c9
MG
236 LocalBroadcastManager.getInstance(this).unregisterReceiver(ongoingUpdateReceiver);
237 unbindService(connection);
238 }
239
240 @Override
241 public boolean onCreateOptionsMenu(@Nullable final Menu menu) {
242 getMenuInflater().inflate(R.menu.main, menu);
243 return true;
244 }
245
246 @Override
247 public boolean onOptionsItemSelected(@Nullable final MenuItem item) {
248 if(item==null)
249 return super.onOptionsItemSelected(item);
250 switch(item.getItemId()){
251 case R.id.prefsItem:
252 startActivity(new Intent(this, FonBotPreferenceActivity.class));
253 return true;
254 case R.id.localItem:
255 startActivity(new Intent(this, FonBotLocalActivity.class));
256 return true;
257 case R.id.helpItem:
258 startActivity(new Intent(this, FonBotHelpActivity.class));
259 return true;
260 default:
261 return super.onOptionsItemSelected(item);
262 }
263 }
4d0abc18 264}
This page took 0.048427 seconds and 4 git commands to generate.