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