Skip to content

Commit e5415eb

Browse files
Teja Vojjalaopticod
authored andcommitted
Search functionality and few bug fixes (#156)
* fixed app crash in phones with api level<19 due to object.equals * added header and footer views before setting adapter * added search functionality for list views
1 parent 303c642 commit e5415eb

12 files changed

Lines changed: 300 additions & 12 deletions

File tree

source-code/app/src/main/java/org/buildmlearn/toolkit/activity/HomeActivity.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import org.buildmlearn.toolkit.R;
1313
import org.buildmlearn.toolkit.constant.Constants;
14+
import org.buildmlearn.toolkit.fragment.LoadApkFragment;
15+
import org.buildmlearn.toolkit.fragment.LoadProjectFragment;
1416
import org.buildmlearn.toolkit.fragment.NavigationDrawerFragment;
1517
import org.buildmlearn.toolkit.fragment.SettingsFragment;
1618
import org.buildmlearn.toolkit.model.Section;
@@ -58,14 +60,36 @@ protected void onCreate(Bundle savedInstanceState) {
5860
}
5961
}
6062
}
61-
6263
/**
6364
* {@inheritDoc}
6465
*/
6566
@Override
6667
public void onNavigationDrawerItemSelected(int position) {
68+
if(position==-1)
69+
{
70+
if(currentSection!=null)
71+
{
72+
if(currentSection.toString().equals("OPEN_PROJECT"))
73+
{
74+
LoadProjectFragment f = (LoadProjectFragment)getFragmentManager().findFragmentByTag(currentSection.getViewName());
75+
if(f!=null)
76+
f.closeSearch();
77+
}
78+
else if(currentSection.toString().equals("OPEN_APK"))
79+
{
80+
LoadApkFragment f = (LoadApkFragment)getFragmentManager().findFragmentByTag(currentSection.getViewName());
81+
if(f!=null)
82+
f.closeSearch();
83+
}
84+
}
85+
return ;
86+
}
6787
Section[] menuItem = Section.values();
6888
Section selectedMenuItem = menuItem[position];
89+
if(getSupportActionBar()!=null) {
90+
getSupportActionBar().setDisplayShowHomeEnabled(true);
91+
getSupportActionBar().setDisplayShowCustomEnabled(false);
92+
}
6993
if (selectedMenuItem.getType() == Section.ACTIVITY) {
7094
Class<?> c;
7195
if (selectedMenuItem.getViewName() != null) {
@@ -107,6 +131,7 @@ public void onNavigationDrawerItemSelected(int position) {
107131

108132
@Override
109133
public void onBackPressed() {
134+
110135
if (mNavigationDrawerFragment.isDrawerOpen()) {
111136
mNavigationDrawerFragment.closeDrawer();
112137
return;

source-code/app/src/main/java/org/buildmlearn/toolkit/activity/TemplateEditor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,4 +877,13 @@ private void setAdapterMeta(BaseAdapter adapter) {
877877
templateMetaList.setAdapter(adapter);
878878
}
879879

880+
@Override
881+
protected void onStop() {
882+
super.onStop();
883+
if(mApkGenerationDialog!=null) {
884+
mApkGenerationDialog.dismiss();
885+
mApkGenerationDialog=null;
886+
}
887+
}
888+
880889
}

source-code/app/src/main/java/org/buildmlearn/toolkit/fragment/LoadApkFragment.java

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,30 @@
22

33
import android.app.Activity;
44
import android.app.Fragment;
5+
import android.content.Context;
56
import android.content.Intent;
7+
import android.content.pm.PackageInfo;
68
import android.content.res.ColorStateList;
79
import android.graphics.drawable.ColorDrawable;
810
import android.net.Uri;
911
import android.os.Build;
1012
import android.os.Bundle;
1113
import android.support.v4.content.ContextCompat;
12-
import android.content.pm.PackageInfo;
14+
import android.support.v7.app.ActionBar;
1315
import android.support.v7.app.AppCompatActivity;
16+
import android.text.Editable;
17+
import android.text.TextWatcher;
1418
import android.util.Log;
19+
import android.view.KeyEvent;
1520
import android.view.LayoutInflater;
1621
import android.view.Menu;
1722
import android.view.MenuItem;
1823
import android.view.View;
1924
import android.view.ViewGroup;
25+
import android.view.inputmethod.InputMethodManager;
2026
import android.widget.AbsListView;
2127
import android.widget.AdapterView;
28+
import android.widget.EditText;
2229
import android.widget.Toast;
2330

2431
import com.afollestad.materialdialogs.DialogAction;
@@ -50,9 +57,11 @@ public class LoadApkFragment extends Fragment implements AbsListView.OnItemClick
5057
private SavedApiAdapter mAdapter;
5158
private ToolkitApplication mToolkit;
5259
private Activity activity;
53-
private ArrayList<SavedApi> savedApis;
60+
private ArrayList<SavedApi> savedApis,allsavedApis;
5461
private View selectedView = null;
62+
private EditText editSearch;
5563

64+
private boolean isSearchOpened=false;
5665
private int selectedPosition = -1;
5766

5867
/**
@@ -65,6 +74,7 @@ public void onCreate(Bundle savedInstanceState) {
6574
mToolkit = (ToolkitApplication) getActivity().getApplicationContext();
6675
activity = getActivity();
6776
savedApis = new ArrayList<>();
77+
allsavedApis = new ArrayList<>();
6878

6979
String path = mToolkit.getSavedDir();
7080
if (mToolkit.checkExternalStorage()) {
@@ -85,6 +95,7 @@ public void onCreate(Bundle savedInstanceState) {
8595
PackageInfo info = getActivity().getPackageManager().getPackageArchiveInfo(apkFile.getAbsolutePath(),0);
8696
if(info!=null&&info.packageName!=null&&info.packageName.startsWith("org.buildmlearn.")) {
8797
savedApis.add(new SavedApi(apkFile, apkFile.getName(), apkFile.lastModified(), apkFile.getAbsolutePath()));
98+
allsavedApis.add(new SavedApi(apkFile, apkFile.getName(), apkFile.lastModified(), apkFile.getAbsolutePath()));
8899
}
89100
}
90101

@@ -94,7 +105,15 @@ public int compare(SavedApi f1, SavedApi f2) {
94105
}
95106
});
96107

108+
Collections.sort(allsavedApis, new Comparator<SavedApi>() {
109+
public int compare(SavedApi f1, SavedApi f2) {
110+
return Long.valueOf(f1.getFile().lastModified()).compareTo(f2.getFile().lastModified());
111+
}
112+
});
113+
97114
Collections.reverse(savedApis);
115+
Collections.reverse(allsavedApis);
116+
98117
}
99118

100119
/**
@@ -170,7 +189,11 @@ private void setEmptyText() {
170189
public void onResume() {
171190
if (mAdapter != null) {
172191

192+
String specificApis="";
193+
if(isSearchOpened)
194+
specificApis=editSearch.getText().toString();
173195
savedApis.clear();
196+
allsavedApis.clear();
174197
String path = mToolkit.getApkDir();
175198

176199
if (mToolkit.checkExternalStorage()) {
@@ -190,7 +213,9 @@ public void onResume() {
190213
File apkFile = new File(aFile.getAbsolutePath());
191214
PackageInfo info = getActivity().getPackageManager().getPackageArchiveInfo(apkFile.getAbsolutePath(),0);
192215
if(info!=null&&info.packageName!=null&&info.packageName.startsWith("org.buildmlearn.")) {
193-
savedApis.add(new SavedApi(apkFile, apkFile.getName(), apkFile.lastModified(), apkFile.getAbsolutePath()));
216+
if(apkFile.getName().startsWith(specificApis))
217+
savedApis.add(new SavedApi(apkFile, apkFile.getName(), apkFile.lastModified(), apkFile.getAbsolutePath()));
218+
allsavedApis.add(new SavedApi(apkFile, apkFile.getName(), apkFile.lastModified(), apkFile.getAbsolutePath()));
194219
}
195220
}
196221
}
@@ -201,7 +226,14 @@ public int compare(SavedApi f1, SavedApi f2) {
201226
}
202227
});
203228

229+
Collections.sort(allsavedApis, new Comparator<SavedApi>() {
230+
public int compare(SavedApi f1, SavedApi f2) {
231+
return Long.valueOf(f1.getFile().lastModified()).compareTo(f2.getFile().lastModified());
232+
}
233+
});
234+
204235
Collections.reverse(savedApis);
236+
Collections.reverse(allsavedApis);
205237
mAdapter.notifyDataSetChanged();
206238
}
207239
super.onResume();
@@ -259,13 +291,16 @@ public void onPrepareOptionsMenu(Menu menu) {
259291
if (showTemplateSelectedMenu) {
260292
activity.getMenuInflater().inflate(R.menu.menu_apk_selected, menu);
261293
}
294+
else{
295+
activity.getMenuInflater().inflate(R.menu.menu_apk_not_selected,menu);
296+
}
262297
}
263298

264299
/**
265300
* {@inheritDoc}
266301
*/
267302
@Override
268-
public boolean onOptionsItemSelected(MenuItem item) {
303+
public boolean onOptionsItemSelected(final MenuItem item) {
269304
int id = item.getItemId();
270305

271306
switch (id) {
@@ -298,6 +333,59 @@ public void onClick(View v) {
298333
sendIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
299334
startActivity(Intent.createChooser(sendIntent, null));
300335
break;
336+
case R.id.action_search:
337+
338+
isSearchOpened=true;
339+
ActionBar actionBar=((AppCompatActivity)getActivity()).getSupportActionBar();
340+
actionBar.setDisplayShowCustomEnabled(true);
341+
item.setVisible(false);
342+
actionBar.setCustomView(R.layout.search_bar);
343+
actionBar.setDisplayShowTitleEnabled(false);
344+
editSearch = (EditText)actionBar.getCustomView().findViewById(R.id.editSearch);
345+
editSearch.setHint("Enter name of Apk");
346+
editSearch.addTextChangedListener(new TextWatcher() {
347+
@Override
348+
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
349+
350+
}
351+
352+
@Override
353+
public void onTextChanged(CharSequence s, int start, int before, int count) {
354+
355+
}
356+
357+
@Override
358+
public void afterTextChanged(Editable s) {
359+
String text = s.toString().trim();
360+
savedApis.clear();
361+
SavedApi tempApi;
362+
for (int i = 0; i < allsavedApis.size(); i++) {
363+
if (allsavedApis.get(i).getName().startsWith(text)) {
364+
tempApi = new SavedApi(allsavedApis.get(i).getFile(), allsavedApis.get(i).getName(), allsavedApis.get(i).getUnformattedDate(), allsavedApis.get(i).getFullPath());
365+
savedApis.add(tempApi);
366+
}
367+
}
368+
mAdapter.notifyDataSetChanged();
369+
setEmptyText();
370+
}
371+
});
372+
editSearch.setOnKeyListener(new View.OnKeyListener() {
373+
@Override
374+
public boolean onKey(View v, int keyCode, KeyEvent event) {
375+
if (keyCode == KeyEvent.KEYCODE_BACK) {
376+
editSearch.onKeyPreIme(keyCode, event);
377+
if (isSearchOpened) {
378+
closeSearch();
379+
}
380+
return true;
381+
}
382+
return false;
383+
}
384+
});
385+
editSearch.requestFocus();
386+
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
387+
imm.showSoftInput(editSearch, InputMethodManager.SHOW_IMPLICIT);
388+
break;
301389
default: //do nothing
302390
break;
303391
}
@@ -330,5 +418,23 @@ private void restoreSelectedView() {
330418
}
331419
restoreColorScheme();
332420
}
421+
422+
public void closeSearch()
423+
{
424+
if (isSearchOpened) {
425+
editSearch.setText("");
426+
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
427+
restoreColorScheme();
428+
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
429+
imm.hideSoftInputFromWindow(editSearch.getWindowToken(), 0);
430+
isSearchOpened = false;
431+
actionBar.setDisplayShowHomeEnabled(true);
432+
actionBar.setDisplayHomeAsUpEnabled(true);
433+
actionBar.setDisplayShowCustomEnabled(false);
434+
actionBar.setDisplayShowTitleEnabled(true);
435+
}
436+
437+
}
438+
333439
}
334440

0 commit comments

Comments
 (0)