diff --git a/src/org/kiwix/kiwixmobile/ZimFileSelectActivity.java b/src/org/kiwix/kiwixmobile/ZimFileSelectActivity.java index d352f65e9..1da319bb1 100644 --- a/src/org/kiwix/kiwixmobile/ZimFileSelectActivity.java +++ b/src/org/kiwix/kiwixmobile/ZimFileSelectActivity.java @@ -179,7 +179,6 @@ public class ZimFileSelectActivity extends FragmentActivity } mCursorAdapter.notifyDataSetChanged(); - } // Get the data of our cursor and wrap it all in our ArrayAdapter. @@ -190,14 +189,25 @@ public class ZimFileSelectActivity extends FragmentActivity for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { // Check if file exists - if (new File(cursor.getString(2)).exists()) { - files.add(new DataModel(cursor.getString(1), cursor.getString(2))); - } else { - Log.e("kiwix", cursor.getString(2) + "does not exist"); + //if (new File(cursor.getString(2)).exists()) { + files.add(new DataModel(cursor.getString(1), cursor.getString(2))); + //} else { + Log.e("kiwix", cursor.getString(2) + " does not exist"); + //} + } + + files = new FileWriter(ZimFileSelectActivity.this, files).getDataModelList(); + + for (int i = 0; i < files.size(); i++) { + + if (!new File(files.get(i).getPath()).exists()) { + files.remove(i); } } + files = sortDataModel(files); mFiles = files; + return new RescanDataAdapter(ZimFileSelectActivity.this, 0, files); } @@ -212,7 +222,7 @@ public class ZimFileSelectActivity extends FragmentActivity for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { files.add(cursor.getString(2)); } - // updateMediaStore(files); + updateMediaStore(files); } @@ -251,6 +261,19 @@ public class ZimFileSelectActivity extends FragmentActivity mNeedsUpdate = false; } + public ArrayList sortDataModel(ArrayList data) { + + // Sorting the data in alphabetical order + Collections.sort(data, new Comparator() { + @Override + public int compare(DataModel a, DataModel b) { + return a.getTitle().compareToIgnoreCase(b.getTitle()); + } + }); + + return data; + } + @Override public void onLoaderReset(Loader cursorLoader) { mCursorAdapter.swapCursor(null); @@ -327,6 +350,87 @@ public class ZimFileSelectActivity extends FragmentActivity finishResult(file); } + // This items class stores the Data for the ArrayAdapter. + // We Have to implement Parcelable, so we can store ArrayLists with this generic type in the Bundle + // of onSaveInstanceState() and retrieve it later on in onRestoreInstanceState() + public static class DataModel implements Parcelable { + + // Interface that must be implemented and provided as a public CREATOR field. + // It generates instances of our Parcelable class from a Parcel. + public Parcelable.Creator CREATOR = new Parcelable.Creator() { + + @Override + public DataModel createFromParcel(Parcel source) { + return new DataModel(source); + } + + @Override + public boolean equals(Object o) { + return super.equals(o); + } + + @Override + public int hashCode() { + return super.hashCode(); + } + + @Override + public DataModel[] newArray(int size) { + return new DataModel[size]; + } + }; + + private String mTitle; + + private String mPath; + + public DataModel(String title, String path) { + mTitle = title; + mPath = path; + } + + // This constructor will be called when this class is generated by a Parcel. + // We have to read the previously written Data in this Parcel. + public DataModel(Parcel parcel) { + String[] data = new String[2]; + parcel.readStringArray(data); + mTitle = data[0]; + mTitle = data[1]; + } + + public String getTitle() { + return mTitle; + } + + public String getPath() { + return mPath; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + // Write the data to the Parcel, so we can restore this Data later on. + // It will be restored by the DataModel(Parcel parcel) constructor. + dest.writeArray(new String[]{mTitle, mPath}); + } + + // Override equals(Object) so we can compare objects. Specifically, so List#contains() works. + @Override + public boolean equals(Object object) { + boolean isEqual = false; + + if (object != null && object instanceof DataModel) { + isEqual = (this.mPath.equals(((DataModel) object).mPath)); + } + + return isEqual; + } + } + // This AsyncTask will scan the file system for files with the Extension ".zim" or ".zimaa" private class RescanFileSystem extends AsyncTask { @@ -355,6 +459,8 @@ public class ZimFileSelectActivity extends FragmentActivity mProgressBarMessage.setVisibility(View.GONE); mProgressBar.setVisibility(View.GONE); + new FileWriter(ZimFileSelectActivity.this).saveArray(mFiles); + super.onPostExecute(result); } @@ -423,13 +529,7 @@ public class ZimFileSelectActivity extends FragmentActivity data.add(new DataModel(getTitleFromFilePath(file), file)); } - // Sorting the data in alphabetical order - Collections.sort(data, new Comparator() { - @Override - public int compare(DataModel a, DataModel b) { - return a.getTitle().compareToIgnoreCase(b.getTitle()); - } - }); + data = sortDataModel(data); return data; } @@ -440,65 +540,6 @@ public class ZimFileSelectActivity extends FragmentActivity } } - // This items class stores the Data for the ArrayAdapter. - // We Have to implement Parcelable, so we can store ArrayLists with this generic type in the Bundle - // of onSaveInstanceState() and retrieve it later on in onRestoreInstanceState() - private class DataModel implements Parcelable { - - // Interface that must be implemented and provided as a public CREATOR field. - // It generates instances of your Parcelable class from a Parcel. - public Parcelable.Creator CREATOR = new Parcelable.Creator() { - - @Override - public DataModel createFromParcel(Parcel source) { - return new DataModel(source); - } - - @Override - public DataModel[] newArray(int size) { - return new DataModel[size]; - } - }; - - private String mTitle; - - private String mPath; - - private DataModel(String title, String path) { - mTitle = title; - mPath = path; - } - - // This constructor will be called when this class is generated by a Parcel. - // We have to read the previously written Data in this Parcel. - public DataModel(Parcel parcel) { - String[] data = new String[2]; - parcel.readStringArray(data); - mTitle = data[0]; - mTitle = data[1]; - } - - public String getTitle() { - return mTitle; - } - - public String getPath() { - return mPath; - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - // Write the data to the Parcel, so we can restore this Data later on. - // It will be restored by the DataModel(Parcel parcel) constructor. - dest.writeArray(new String[]{mTitle, mPath}); - } - } - // The Adapter for the ListView for when the ListView is populated with the rescanned files private class RescanDataAdapter extends ArrayAdapter {