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