Clover Coverage Report - RaspberryBusMalaysiaActivity Coverage Report
Coverage timestamp: mar dic 23 2014 15:39:35 EST
../../../img/srcFileCovDistChart9.png 36% of files have more coverage
135   339   48   5,87
44   279   0,36   11,5
23     2,09  
2    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  RouteActivity       Line # 42 113 42 88,2% 0.88235295
  RouteActivity.RouteListAdapter       Line # 53 22 6 93,8% 0.9375
 
No Tests
 
1    /*
2    Copyright (C) 2012,2013 Sweetie Piggy Apps <sweetiepiggyapps@gmail.com>
3   
4    This file is part of Raspberry Bus Malaysia.
5   
6    Raspberry Bus Malaysia is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10   
11    Raspberry Bus Malaysia is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14    GNU General Public License for more details.
15   
16    You should have received a copy of the GNU General Public License
17    along with Raspberry Bus Malaysia; if not, see <http://www.gnu.org/licenses/>.
18    */
19   
20    package com.sweetiepiggy.raspberrybusmalaysia;
21   
22    import android.app.Activity;
23    import android.content.Context;
24    import android.content.Intent;
25    import android.database.Cursor;
26    import android.os.Bundle;
27    import android.view.View;
28    import android.widget.AdapterView;
29    import android.widget.AdapterView.OnItemClickListener;
30    import android.widget.AdapterView.OnItemSelectedListener;
31    import android.widget.Button;
32    import android.widget.ImageView;
33    import android.widget.LinearLayout;
34    import android.widget.ListView;
35    import android.widget.RadioButton;
36    import android.widget.RatingBar;
37    import android.widget.ResourceCursorAdapter;
38    import android.widget.SimpleCursorAdapter;
39    import android.widget.Spinner;
40    import android.widget.TextView;
41   
 
42    public class RouteActivity extends Activity
43    {
44    private String m_from_city = "";
45    private String m_to_city = "";
46    private boolean mHideSearch = false;
47   
48    private DbAdapter mDbHelper;
49   
50    private static final int ACTIVITY_FROM = 0;
51    private static final int ACTIVITY_TO = 1;
52   
 
53    private class RouteListAdapter extends ResourceCursorAdapter
54    {
 
55  23 toggle public RouteListAdapter(Context context, Cursor c)
56    {
57  23 super(context, R.layout.route_row, c);
58    }
59   
 
60  56 toggle @Override
61    public void bindView(View view, Context context, Cursor c)
62    {
63  56 TextView company_v = (TextView) view.findViewById(R.id.company);
64  56 TextView avg_v = (TextView) view.findViewById(R.id.avg);
65  56 TextView count_v = (TextView) view.findViewById(R.id.count);
66  56 RatingBar overall_v = (RatingBar) view.findViewById(R.id.overall_bar);
67   
68  56 final int row_id = c.getInt(c.getColumnIndex(DbAdapter.KEY_ROWID));
69  56 String agent = c.getString(c.getColumnIndex(DbAdapter.KEY_AGENT));
70  56 String operator = c.getString(c.getColumnIndex(DbAdapter.KEY_OPERATOR));
71  56 String avg = format_time(c.getInt(c.getColumnIndex(DbAdapter.AVG_TIME)));
72  56 String count = c.getString(c.getColumnIndex(DbAdapter.NUM_TRIPS));
73  56 float avg_overall = c.getFloat(c.getColumnIndex(DbAdapter.AVG_OVERALL));
74   
75  56 String company = ((RadioButton) findViewById(R.id.operator_radio)).isChecked() ?
76    operator : agent;
77   
78  56 if (company.length() > 15) {
79  2 company = company.replace(' ', '\n');
80  54 } else if (company.length() == 0) {
81  8 company = getResources().getString(R.string.unknown);
82    }
83   
84  56 if (count == null) {
85  0 count = "0";
86    }
87   
88  56 company_v.setText(company);
89  56 avg_v.setText(avg);
90  56 count_v.setText(count + " " + getResources().getString(R.string.reviews));
91  56 overall_v.setRating(avg_overall);
92    }
93    }
94   
95    /** Called when the activity is first created. */
 
96  1 toggle @Override
97    public void onCreate(Bundle savedInstanceState)
98    {
99  1 super.onCreate(savedInstanceState);
100  1 setContentView(R.layout.route);
101   
102  1 ((RadioButton) findViewById(R.id.agent_radio)).setChecked(true);
103   
104  1 mDbHelper = new DbAdapter();
105  1 mDbHelper.open(this);
106  1 init_from_map_button();
107  1 init_from_spinner(null);
108  1 init_shade();
109    }
110   
 
111  1 toggle @Override
112    protected void onDestroy() {
113  1 if (mDbHelper != null) {
114  1 mDbHelper.close();
115    }
116  1 super.onDestroy();
117    }
118   
 
119  1 toggle private void init_from_map_button()
120    {
121  1 Button from_map_button = (Button) findViewById(R.id.from_map_button);
122  1 from_map_button.setOnClickListener(new View.OnClickListener() {
 
123  2 toggle public void onClick(View v) {
124  2 Intent intent = new Intent(getApplicationContext(), RbmMapActivity.class);
125  2 Bundle b = new Bundle();
126  2 b.putBoolean("set_result", true);
127  2 b.putBoolean("valid_from", true);
128  2 intent.putExtras(b);
129  2 startActivityForResult(intent, ACTIVITY_FROM);
130    }
131    });
132    }
133   
 
134  4 toggle private void init_to_map_button(final String from_city)
135    {
136  4 Button to_map_button = (Button) findViewById(R.id.to_map_button);
137  4 to_map_button.setOnClickListener(new View.OnClickListener() {
 
138  2 toggle public void onClick(View v) {
139  2 Intent intent = new Intent(getApplicationContext(), RbmMapActivity.class);
140  2 Bundle b = new Bundle();
141  2 b.putBoolean("set_result", true);
142  2 b.putBoolean("valid_to", true);
143  2 b.putString("from_city", from_city);
144  2 intent.putExtras(b);
145  2 startActivityForResult(intent, ACTIVITY_TO);
146    }
147    });
148    }
149   
 
150  3 toggle private void init_from_spinner(String set_from_city)
151    {
152  3 Cursor c = mDbHelper.fetch_from_cities();
153  3 startManagingCursor(c);
154  3 SimpleCursorAdapter from_cities = new SimpleCursorAdapter(this,
155    android.R.layout.simple_spinner_item,
156    c, new String[] {DbAdapter.KEY_FROM_CITY},
157    new int[] {android.R.id.text1});
158  3 from_cities.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
159  3 Spinner from_spinner = (Spinner) findViewById(R.id.from_spinner);
160  3 from_spinner.setAdapter(from_cities);
161   
162  3 if (set_from_city == null) {
163  1 set_from_city = mDbHelper.get_most_freq_from_city();
164    }
165  3 spinner_set_selection(from_spinner, set_from_city);
166   
167  3 from_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
 
168  14 toggle @Override
169    public void onItemSelected(AdapterView<?> parent,
170    View selected_item, int pos,
171    long id)
172    {
173  14 String new_from_city = ((Cursor)parent.getItemAtPosition(pos)).getString(1);
174  14 if (!m_from_city.equals(new_from_city)) {
175  4 m_from_city = new_from_city;
176  4 init_to_map_button(m_from_city);
177  4 init_to_spinner(m_from_city, null);
178    }
179    }
180   
 
181  0 toggle @Override
182    public void onNothingSelected(AdapterView<?> parentView)
183    {
184    }
185   
186    });
187    }
188   
 
189  1 toggle private void init_shade()
190    {
191  1 ImageView shade_v = (ImageView) findViewById(R.id.shade);
192  1 shade_v.setOnClickListener(new View.OnClickListener() {
 
193  2 toggle public void onClick(View v) {
194  2 mHideSearch = !mHideSearch;
195  2 ((ImageView) v).setImageResource(mHideSearch ? android.R.drawable.arrow_down_float :
196    android.R.drawable.arrow_up_float);
197  2 ((LinearLayout) findViewById(R.id.search_layout)).setVisibility(mHideSearch ?
198    View.GONE : View.VISIBLE);
199   
200  2 TextView route_v = (TextView) findViewById(R.id.route);
201  2 route_v.setText(m_from_city + " -> " + m_to_city);
202  2 route_v.setVisibility(mHideSearch ?
203    View.VISIBLE : View.GONE);
204    }
205    });
206    }
207   
 
208  6 toggle private void init_to_spinner(final String from_city, String set_to_city)
209    {
210  6 Cursor c = mDbHelper.fetch_to_cities(from_city);
211  6 startManagingCursor(c);
212  6 SimpleCursorAdapter to_cities = new SimpleCursorAdapter(getApplicationContext(),
213    android.R.layout.simple_spinner_item,
214    c, new String[] {DbAdapter.KEY_TO_CITY},
215    new int[] {android.R.id.text1});
216  6 to_cities.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
217  6 Spinner to_spinner = (Spinner) findViewById(R.id.to_spinner);
218  6 to_spinner.setAdapter(to_cities);
219   
220  6 if (set_to_city == null) {
221  4 set_to_city = mDbHelper.get_most_freq_to_city(from_city);
222    }
223  6 spinner_set_selection(to_spinner, set_to_city);
224   
225  6 to_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
 
226  16 toggle @Override
227    public void onItemSelected(AdapterView<?> parent,
228    View selected_item, int pos,
229    long id) {
230  16 m_to_city = ((Cursor) parent.getItemAtPosition(pos)).getString(1);
231  16 print_rows(from_city, m_to_city);
232    }
 
233  0 toggle @Override
234    public void onNothingSelected(AdapterView<?> parentView) {
235    }
236   
237    });
238    }
239   
 
240  9 toggle private void spinner_set_selection(Spinner spinner, String value)
241    {
242  9 boolean done = false;
243  24 for (int i = 0; i < spinner.getCount() && !done; ++i) {
244  15 if (((Cursor)spinner.getItemAtPosition(i)).getString(1).equals(value)) {
245  9 spinner.setSelection(i);
246  9 done = true;
247    }
248    }
249    }
250   
 
251  7 toggle public void on_radio_button_clicked(View v)
252    {
253  7 boolean checked = ((RadioButton) v).isChecked();
254   
255  7 if (checked) {
256  7 print_rows(m_from_city, m_to_city);
257    }
258    }
259   
 
260  23 toggle private void print_rows(String from_city, String to_city)
261    {
262  23 Cursor c = ((RadioButton) findViewById(R.id.operator_radio)).isChecked() ?
263    mDbHelper.fetch_avg_by_operator(from_city, to_city) :
264    mDbHelper.fetch_avg_by_agent(from_city, to_city);
265  23 startManagingCursor(c);
266  23 ListView lv = (ListView) findViewById(R.id.results_list);
267  23 lv.setAdapter(new RouteListAdapter(this, c));
268  23 lv.setOnItemClickListener(new OnItemClickListener() {
 
269  3 toggle public void onItemClick(AdapterView<?> parent, View v,
270    int pos, long id) {
271  3 String company = ((RadioButton) findViewById(R.id.operator_radio)).isChecked() ?
272    mDbHelper.getCompanyByOperator(id) : mDbHelper.getCompanyByAgent(id);
273  3 Intent intent = new Intent(getApplicationContext(),
274    CompanyResultActivity.class);
275  3 Bundle b = new Bundle();
276  3 b.putString("company", company);
277  3 b.putBoolean("is_operator", ((RadioButton) findViewById(R.id.operator_radio)).isChecked());
278  3 intent.putExtras(b);
279  3 startActivity(intent);
280    }
281    });
282    }
283   
 
284  56 toggle private String format_time(int time)
285    {
286  56 String negative = "";
287  56 if (time < 0) {
288  0 negative = "-";
289  0 time *= -1;
290    }
291   
292  56 int hr = time / 3600;
293  56 time -= hr * 3600;
294  56 int min = time / 60;
295  56 return String.format("%s%d%s %02d%s", negative, hr, getResources().getString(R.string.hour_abbr),
296    min, getResources().getString(R.string.minute_abbr));
297    }
298   
299    /* TODO: move format_time() and format_time_min() to their own class */
 
300  0 toggle private String format_time_min(int time)
301    {
302  0 String negative = "";
303  0 if (time < 0) {
304  0 negative = "-";
305  0 time *= -1;
306    }
307   
308  0 int min = time / 60;
309  0 return String.format("%s%d%s", negative, min, getResources().getString(R.string.minute_abbr));
310    }
311   
 
312  4 toggle @Override
313    protected void onActivityResult(int request_code, int result_code, Intent data)
314    {
315  4 super.onActivityResult(request_code, result_code, data);
316   
317  4 switch (request_code) {
318  2 case ACTIVITY_FROM:
319  2 if (result_code == RESULT_OK) {
320  2 Bundle b = data.getExtras();
321  2 if (b != null) {
322  2 String city = b.getString("city");
323  2 init_from_spinner(city);
324    }
325    }
326  2 break;
327  2 case ACTIVITY_TO:
328  2 if (result_code == RESULT_OK) {
329  2 Bundle b = data.getExtras();
330  2 if (b != null) {
331  2 String city = b.getString("city");
332  2 init_to_spinner(m_from_city, city);
333    }
334    }
335  2 break;
336    }
337    }
338    }
339