-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathActivityHome.java
655 lines (593 loc) · 26.1 KB
/
ActivityHome.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
package com.orimex.orimex;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.InputType;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SearchView;
import android.widget.Spinner;
import android.widget.TextView;
import com.orimex.orimex.fragment.DetailViewFragment;
import com.orimex.orimex.fragment.HomeFragment;
import com.orimex.orimex.fragment.NavigationDrawerFragment;
import com.orimex.orimex.fragment.ResultListFragment;
import com.orimex.orimex.fragment.SubCategoryFragment;
import com.orimex.orimex.model.District;
import com.orimex.orimex.model.Item;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.List;
import static com.orimex.orimex.util.Constants.JF_AREA;
import static com.orimex.orimex.util.Constants.JF_CATEGORY_ID;
import static com.orimex.orimex.util.Constants.JF_ENTRY;
import static com.orimex.orimex.util.Constants.JF_ID;
import static com.orimex.orimex.util.Constants.JF_TITLE;
import static com.orimex.orimex.util.Constants.KEY_DISTRICT_2;
import static com.orimex.orimex.util.Constants.isResultListFragmentOpened;
import static com.orimex.orimex.util.UtilMethods.hideSoftKeyboard;
import static com.orimex.orimex.util.UtilMethods.loadJSONFromAsset;
import static com.orimex.orimex.util.UtilMethods.isDeviceCallSupported;
import static com.orimex.orimex.util.UtilMethods.showHotLineCallDialog;
import static com.orimex.orimex.util.UtilMethods.showSoftKeyboard;
/**
* Created by Mi sesión on 15/11/2016.
*/
public class ActivityHome extends AppCompatActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks,
HomeFragment.CategorySelectionCallbacks,
SubCategoryFragment.SubCategorySelectionCallbacks,
ResultListFragment.ResultListCallbacks,
SearchView.OnQueryTextListener, AdapterView.OnItemClickListener {
public static int selectedDistrictId = 1;
public static String selectedArea = "All Area";
private int navigationDepth = 0;
private SearchView mSearchView;
private List<String> suggestions;
private String mCurrentSearchText;
private List<District> districtList;
private List<String> areaList;
private NavigationDrawerFragment mNavigationDrawerFragment;
private FragmentManager fragmentManager;
private CharSequence mTitle;
private String subCategoryTitle = null;
private String resultListTitle = null;
private String detailViewTitle = null;
private String searchQueryTitle = null;
private boolean isPopupWindowNeedToUpdate = false;
private boolean isResultListNeedToUpdate = false;
private RelativeLayout filterLayout;
private Spinner spinnerDistrict;
private Spinner spinnerArea;
private TextView buttonApply;
private ListView suggestionListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
mSearchView = (SearchView) findViewById(R.id.searchView);
mSearchView.setSearchableInfo(searchableInfo);
mSearchView.setOnQueryTextListener(this);
mSearchView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
mSearchView.setQueryHint(Html.fromHtml("<small><small>" + getString(R.string.search_hint) + "</small></small>"));
int searchPlateId = mSearchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
View searchPlate = mSearchView.findViewById(searchPlateId);
searchPlate.setBackgroundColor(Color.TRANSPARENT);
int submitAreaId = mSearchView.getContext().getResources().getIdentifier("android:id/submit_area", null, null);
View submitArea = mSearchView.findViewById(submitAreaId);
submitArea.setBackgroundColor(Color.TRANSPARENT);
int searchImgIcon = getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView searchImgView = (ImageView) mSearchView.findViewById(searchImgIcon);
searchImgView.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
searchImgView.setVisibility(View.GONE);
int closeButtonId = mSearchView.getContext().getResources().getIdentifier("android:id/search_close_btn", null, null);
ImageView closeButton = (ImageView) mSearchView.findViewById(closeButtonId);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mSearchView.setQuery(null, false);
hideSoftKeyboard(ActivityHome.this);
}
});
suggestionListView = (ListView) findViewById(R.id.suggestionListView);
suggestionListView.setOnItemClickListener(this);
findViewById(R.id.blankLayout).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isSearchFilerShowing()) {
findViewById(R.id.blankLayout).setVisibility(View.GONE);
filterLayout.setVisibility(View.GONE);
return;
}
}
});
initSearchFilter();
onNavigationDrawerItemSelected(0);
}
@Override
public void onBackPressed() {
if (isSearchFilerShowing()) {
findViewById(R.id.blankLayout).setVisibility(View.GONE);
filterLayout.setVisibility(View.GONE);
return;
}
if (mSearchView.getVisibility() == View.VISIBLE) {
hideSearchView();
return;
}
switch (navigationDepth) {
case 1:
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, HomeFragment.newInstance(0))
.commit();
if (navigationDepth > 0) {
navigationDepth--;
}
isResultListFragmentOpened = false;
getSupportActionBar().setTitle(getTitle());
break;
case 2:
if (TextUtils.isEmpty(SubCategoryFragment.catId)) {
SubCategoryFragment.catId = "1";
}
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, SubCategoryFragment.newInstance(SubCategoryFragment.catId))
.commit();
if (navigationDepth > 0)
navigationDepth--;
if (subCategoryTitle != null)
getSupportActionBar().setTitle(subCategoryTitle);
break;
case 3:
if (TextUtils.isEmpty(ResultListFragment.catId)) {
ResultListFragment.catId = "1";
}
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, ResultListFragment.newInstance(ResultListFragment.catId))
.commit();
if (navigationDepth > 0)
navigationDepth--;
if (resultListTitle != null)
getSupportActionBar().setTitle(resultListTitle);
break;
case 4:
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, HomeFragment.newInstance(0))
.commit();
navigationDepth = 0;
getSupportActionBar().setTitle(getTitle());
isResultListFragmentOpened = false;
break;
case 5:
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, ResultListFragment.newInstance("", ResultListFragment.searchTerm))
.commit();
if (navigationDepth > 0)
navigationDepth--;
getSupportActionBar().setTitle(ResultListFragment.searchTerm);
break;
default:
super.onBackPressed();
}
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
mSearchView.setQuery(query, false);
mSearchView.clearFocus();
}
}
@Override
public void onNavigationDrawerItemSelected(int position) {
if (mSearchView.getVisibility() == View.VISIBLE) {
hideSearchView();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, HomeFragment.newInstance(position))
.commit();
navigationDepth = 0;
isResultListFragmentOpened = false;
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
if (navigationDepth == 0)
actionBar.setTitle(getTitle());
else if (navigationDepth == 1)
actionBar.setTitle(subCategoryTitle);
else if (navigationDepth == 2)
actionBar.setTitle(resultListTitle);
else if (navigationDepth == 3)
actionBar.setTitle(detailViewTitle);
else if (navigationDepth == 4)
actionBar.setTitle(searchQueryTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (isSearchFilerShowing()) {
findViewById(R.id.blankLayout).setVisibility(View.GONE);
filterLayout.setVisibility(View.GONE);
return false;
}
if (!mNavigationDrawerFragment.isDrawerOpen()) {
getMenuInflater().inflate(R.menu.menu, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mSearchView.isFocused()) {
mSearchView.setFocusable(false);
}
hideSoftKeyboard(ActivityHome.this);
int id = item.getItemId();
switch (id) {
case R.id.action_search:
if (!isSearchFilerShowing() && !mNavigationDrawerFragment.isDrawerOpen())
showHideSearchView();
break;
case R.id.action_call:
hideSearchView();
if (isDeviceCallSupported(this)) {
showHotLineCallDialog(this, getResources().getString(R.string.hotline_call_heading),
getResources().getString(R.string.hotline_call_text),
getResources().getString(R.string.yes_string),
getResources().getString(R.string.no_string));
}
break;
case R.id.home:
return super.onOptionsItemSelected(item);
case R.id.action_filter:
if (!mNavigationDrawerFragment.isDrawerOpen()) {
hideSearchView();
if (isSearchFilerShowing()) {
findViewById(R.id.blankLayout).setVisibility(View.GONE);
filterLayout.setVisibility(View.GONE);
} else {
showSearchFilter();
}
}
break;
}
return super.onOptionsItemSelected(item);
}
private void showHideSearchView() {
if (mSearchView.getVisibility() == View.VISIBLE) {
mSearchView.setFocusable(false);
mSearchView.setVisibility(View.GONE);
hideSoftKeyboard(this);
} else {
mSearchView.setVisibility(View.VISIBLE);
mSearchView.setQuery("", false);
mSearchView.setFocusable(true);
mSearchView.requestFocus();
showSoftKeyboard(this);
}
}
private void hideSearchView() {
if (mSearchView.getVisibility() == View.VISIBLE) {
mSearchView.setVisibility(View.GONE);
}
hideSuggestionList();
}
private void hideSuggestionList() {
if (suggestionListView != null && suggestionListView.getVisibility() == View.VISIBLE) {
suggestionListView.setAdapter(null);
suggestionListView.setVisibility(View.GONE);
}
}
private void initSearchFilter() {
filterLayout = (RelativeLayout) findViewById(R.id.filterLayout);
spinnerDistrict = (Spinner) findViewById(R.id.spinnerDistrict);
spinnerArea = (Spinner) findViewById(R.id.spinnerArea);
buttonApply = (TextView) findViewById(R.id.buttonApply);
filterLayout.setVisibility(View.GONE);
buttonApply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
findViewById(R.id.blankLayout).setVisibility(View.GONE);
filterLayout.setVisibility(View.GONE);
if (isResultListFragmentOpened && isResultListNeedToUpdate) {
ResultListFragment.locationChangeListener.onLocationChange();
isResultListNeedToUpdate = false;
}
}
});
}
private void showSearchFilter() {
findViewById(R.id.blankLayout).setVisibility(View.VISIBLE);
filterLayout.setVisibility(View.VISIBLE);
if (spinnerDistrict.getAdapter() != null) {
return;
}
if (districtList.size() > 0) {
String[] district = new String[districtList.size()];
for (int i = 0; i < districtList.size(); i++) {
district[i] = districtList.get(i).getTitle();
}
ArrayAdapter<String> adapterCity = new ArrayAdapter(this, R.layout.list_item_spinner, district);
adapterCity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDistrict.setAdapter(adapterCity);
spinnerDistrict.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedDistrictId = Integer.parseInt(districtList.get(position).getId());
isPopupWindowNeedToUpdate = true;
if (isResultListFragmentOpened) {
isResultListNeedToUpdate = true;
}
initArea(selectedDistrictId + "");
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
private boolean isSearchFilerShowing() {
if (filterLayout != null && filterLayout.getVisibility() == View.VISIBLE)
return true;
else
return false;
}
private void setAreaSpinner() {
if (areaList.size() > 0) {
ArrayAdapter<String> areaAdapter = new ArrayAdapter(this, R.layout.list_item_spinner, areaList);
areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerArea.setAdapter(areaAdapter);
spinnerArea.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
isResultListNeedToUpdate = true;
selectedArea = areaList.get(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
@Override
public void onCategorySelected(String catID, String title) {
hideSearchView();
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, SubCategoryFragment.newInstance(catID))
.commit();
navigationDepth++;
getSupportActionBar().setTitle(title);
subCategoryTitle = title;
}
@Override
public void onSubCategorySelected(String catID, String title) {
hideSearchView();
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, ResultListFragment.newInstance(catID))
.commit();
navigationDepth++;
getSupportActionBar().setTitle(title);
resultListTitle = title;
}
@Override
public void onResultItemSelected(Item itemDetails) {
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, DetailViewFragment.newInstance(itemDetails))
.commit();
hideSearchView();
navigationDepth++;
getSupportActionBar().setTitle(itemDetails.getTitle());
detailViewTitle = itemDetails.getTitle();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
hideSearchView();
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, ResultListFragment.newInstance("", suggestions.get(position)))
.commit();
navigationDepth = 4;
getSupportActionBar().setTitle(suggestions.get(position));
searchQueryTitle = suggestions.get(position);
}
@Override
public boolean onQueryTextSubmit(String query) {
mSearchView.clearFocus();
hideSearchView();
hideSoftKeyboard(this);
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, ResultListFragment.newInstance("", query))
.commit();
navigationDepth = 4;
getSupportActionBar().setTitle(query);
searchQueryTitle = query;
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText.length() == 0) {
hideSuggestionList();
hideSoftKeyboard(this);
mSearchView.clearFocus();
mCurrentSearchText = null;
return false;
}
if (!TextUtils.isEmpty(newText)) {
getSearchSuggestions(newText);
mCurrentSearchText = newText;
return true;
}
return false;
}
// Search
private void showSuggestions() {
if (suggestions != null && suggestions.size() > 0 && !TextUtils.isEmpty(mCurrentSearchText)) {
if (suggestionListView.getVisibility() == View.GONE) {
suggestionListView.setVisibility(View.VISIBLE);
}
suggestionListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, suggestions) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
if (suggestions != null && suggestions.size() > 0 && !TextUtils.isEmpty(mCurrentSearchText)) {
String text = suggestions.get(position);
int startIndex = -1;
int endIndex = -1;
if (!TextUtils.isEmpty(mCurrentSearchText)) {
startIndex = text.toLowerCase().indexOf(mCurrentSearchText.toLowerCase());
endIndex = (startIndex + mCurrentSearchText.length() < text.length()) ?
startIndex + mCurrentSearchText.length() : text.length();
}
if (startIndex >= 0 && endIndex >= 0) {
SpannableStringBuilder sb = new SpannableStringBuilder(suggestions.get(position));
ForegroundColorSpan fcs = new ForegroundColorSpan(getResources().getColor(R.color.green_text_color));
sb.setSpan(fcs, startIndex, endIndex, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
sb.setSpan(bss, startIndex, endIndex, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(sb);
} else {
textView.setText(suggestions.get(position));
}
}
return view;
}
});
}
}
private void getSearchSuggestions(String query) {
String jsonString = loadJSONFromAsset(this, "get_suggestion_list");
try {
JSONArray jsonArray = new JSONArray(jsonString);
int arraySize = jsonArray.length();
if (arraySize > 0) {
suggestions = new ArrayList<String>();
for (int i = 0; i < arraySize; i++) {
suggestions.add(jsonArray.getString(i));
}
} else {
suggestions = null;
}
if (suggestions != null && suggestions.size() > 0) {
runOnUiThread(new Runnable() {
@Override
public void run() {
showSuggestions();
}
});
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
hideSuggestionList();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
if (districtList == null)
initDistrictFilter();
}
private void initDistrictFilter() {
String jsonString = loadJSONFromAsset(this, "get_district");
try {
final JSONArray jsonArray = new JSONArray(jsonString);
districtList = new ArrayList<District>();
for (int i = 0; i < jsonArray.length(); i++) {
districtList.add(new District(jsonArray.getJSONObject(i).getJSONObject(KEY_DISTRICT_2).getString(JF_ID),
jsonArray.getJSONObject(i).getJSONObject(KEY_DISTRICT_2).getString(JF_TITLE)));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void initArea(String id) {
String jsonString = loadJSONFromAsset(this, "get_area");
try {
JSONArray jsonArray = new JSONArray(jsonString);
areaList = new ArrayList<String>();
areaList.add("All Area");
for (int i = 0; i < jsonArray.length(); i++) {
areaList.add(jsonArray.getJSONObject(i).getJSONObject(JF_ENTRY).getString(JF_AREA));
}
if (isPopupWindowNeedToUpdate) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
setAreaSpinner();
isPopupWindowNeedToUpdate = false;
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return super.dispatchTouchEvent(ev);
}
}