Continue refactoring

This commit is contained in:
Victor Shcherb 2015-10-18 17:13:21 +02:00
parent e5b451193a
commit 266e62c459
8 changed files with 154 additions and 165 deletions

View file

@ -80,8 +80,6 @@ public class BaseDownloadActivity extends ActionBarProgressActivity {
public void downloadedIndexes() { public void downloadedIndexes() {
} }
public void updateFragments() {
}
public void downloadListUpdated() { public void downloadListUpdated() {
} }
@ -155,7 +153,7 @@ public class BaseDownloadActivity extends ActionBarProgressActivity {
private void downloadFileCheck_Final_Run(IndexItem[] items) { private void downloadFileCheck_Final_Run(IndexItem[] items) {
downloadListIndexThread.runDownloadFiles(items); downloadListIndexThread.runDownloadFiles(items);
updateFragments(); updateProgress(false);
} }

View file

@ -119,7 +119,7 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
getMyApplication().getAppCustomization().resumeActivity(DownloadActivity.class, this); getMyApplication().getAppCustomization().resumeActivity(DownloadActivity.class, this);
updateFragments(); updateProgress(false);
} }
@ -157,8 +157,22 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism
final int countedDownloads = downloadListIndexThread.getCountedDownloads(); final int countedDownloads = downloadListIndexThread.getCountedDownloads();
visibleBanner.updateProgress(countedDownloads, basicProgressAsyncTask); visibleBanner.updateProgress(countedDownloads, basicProgressAsyncTask);
} }
if (!updateOnlyProgress) { if(activeDownloads != null) {
updateFragments(); if(updateOnlyProgress) {
activeDownloads.notifyDataSetChanged();
} else {
activeDownloads.notifyDataSetInvalidated();
}
}
// FIXME
//((DownloadActivity) getActivity()).updateDescriptionTextWithSize(getView());
for (WeakReference<Fragment> ref : fragSet) {
Fragment f = ref.get();
notifyUpdateDataSetChanged(f);
if(f instanceof RegionItemsFragment) {
Fragment innerFragment = ((RegionItemsFragment)f).getChildFragmentManager().findFragmentById(R.id.fragmentContainer);
notifyUpdateDataSetChanged(innerFragment);
}
} }
} }
@ -209,22 +223,6 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism
this.activeDownloads = activeDownloads; this.activeDownloads = activeDownloads;
} }
@Override
public void updateFragments() {
if(activeDownloads != null) {
activeDownloads.refresh();
}
// FIXME
//((DownloadActivity) getActivity()).updateDescriptionTextWithSize(getView());
for (WeakReference<Fragment> ref : fragSet) {
Fragment f = ref.get();
notifyUpdateDataSetChanged(f);
if(f instanceof RegionItemsFragment) {
Fragment innerFragment = ((RegionItemsFragment)f).getChildFragmentManager().findFragmentById(R.id.fragmentContainer);
notifyUpdateDataSetChanged(innerFragment);
}
}
}
private void notifyUpdateDataSetChanged(Fragment f) { private void notifyUpdateDataSetChanged(Fragment f) {

View file

@ -1,31 +0,0 @@
package net.osmand.plus.download;
import java.io.File;
public class DownloadEntry {
public long dateModified;
public double sizeMB;
public File targetFile;
public boolean zipStream;
public boolean unzipFolder;
public File fileToDownload;
public String baseName;
public String urlToDownload;
public boolean isAsset;
public String assetName;
public DownloadActivityType type;
public DownloadEntry() {
}
public DownloadEntry(String assetName, String fileName, long dateModified) {
this.dateModified = dateModified;
targetFile = new File(fileName);
this.assetName = assetName;
isAsset = true;
}
}

View file

@ -200,7 +200,7 @@ public class DownloadFileHelper {
return ctx.getSettings().isWifiConnected(); return ctx.getSettings().isWifiConnected();
} }
public boolean downloadFile(DownloadEntry de, IProgress progress, public boolean downloadFile(IndexItem.DownloadEntry de, IProgress progress,
List<File> toReIndex, DownloadFileShowWarning showWarningCallback, boolean forceWifi) throws InterruptedException { List<File> toReIndex, DownloadFileShowWarning showWarningCallback, boolean forceWifi) throws InterruptedException {
try { try {
final List<InputStream> downloadInputStreams = new ArrayList<InputStream>(); final List<InputStream> downloadInputStreams = new ArrayList<InputStream>();
@ -235,7 +235,7 @@ public class DownloadFileHelper {
} }
} }
private void copyVoiceConfig(DownloadEntry de) { private void copyVoiceConfig(IndexItem.DownloadEntry de) {
File f = ctx.getAppPath("/voice/" + de.baseName + "/_config.p"); File f = ctx.getAppPath("/voice/" + de.baseName + "/_config.p");
if (f.exists()) try { if (f.exists()) try {
InputStream is = ctx.getAssets().open("voice/" + de.baseName + "/config.p"); InputStream is = ctx.getAssets().open("voice/" + de.baseName + "/config.p");
@ -252,7 +252,7 @@ public class DownloadFileHelper {
} }
} }
private void unzipFile(DownloadEntry de, IProgress progress, List<InputStream> is) throws IOException { private void unzipFile(IndexItem.DownloadEntry de, IProgress progress, List<InputStream> is) throws IOException {
CountingMultiInputStream fin = new CountingMultiInputStream(is); CountingMultiInputStream fin = new CountingMultiInputStream(is);
int len = (int) fin.available(); int len = (int) fin.available();
int mb = (int) (len / (1024f*1024f)); int mb = (int) (len / (1024f*1024f));
@ -308,7 +308,7 @@ public class DownloadFileHelper {
fin.close(); fin.close();
} }
private void copyFile(DownloadEntry de, IProgress progress, private void copyFile(IndexItem.DownloadEntry de, IProgress progress,
CountingMultiInputStream countIS, int length, InputStream toRead, File targetFile) CountingMultiInputStream countIS, int length, InputStream toRead, File targetFile)
throws IOException { throws IOException {
targetFile.getParentFile().mkdirs(); targetFile.getParentFile().mkdirs();

View file

@ -89,10 +89,44 @@ public class DownloadIndexesThread {
} }
/// UI notifications methods
public void setUiActivity(BaseDownloadActivity uiActivity) { public void setUiActivity(BaseDownloadActivity uiActivity) {
this.uiActivity = uiActivity; this.uiActivity = uiActivity;
} }
@UiThread
private void updateProgressUI(boolean onlyProgress) {
if (uiActivity != null) {
uiActivity.updateProgress(onlyProgress);
}
}
@UiThread
private void onCategorizationFinished() {
if (uiActivity != null) {
uiActivity.onCategorizationFinished();
}
}
@UiThread
private void updateDownloadList() {
if (uiActivity != null) {
uiActivity.updateDownloadList();
}
}
@UiThread
private void notifyFilesToUpdateChanged() {
List<IndexItem> filtered = getCachedIndexFiles();
if (filtered != null) {
updateDownloadList();
}
}
// PUBLIC API
public List<IndexItem> getCurrentDownloadingItems() { public List<IndexItem> getCurrentDownloadingItems() {
List<IndexItem> res = new ArrayList<IndexItem>(); List<IndexItem> res = new ArrayList<IndexItem>();
IndexItem ii = currentDownloadingItem; IndexItem ii = currentDownloadingItem;
@ -309,9 +343,9 @@ public class DownloadIndexesThread {
public void cancelDownload(IndexItem item) { public void cancelDownload(IndexItem item) {
if(currentDownloadingItem == item) { if(currentDownloadingItem == item) {
downloadFileHelper.setInterruptDownloading(true);; downloadFileHelper.setInterruptDownloading(true);;
} else {
indexItemDownloading.remove(item);
} }
// TODO Auto-generated method stub
} }
private <P> void execute(BasicProgressAsyncTask<?, P, ?, ?> task, P... indexItems) { private <P> void execute(BasicProgressAsyncTask<?, P, ?, ?> task, P... indexItems) {
@ -336,15 +370,6 @@ public class DownloadIndexesThread {
} }
} }
@UiThread
private void notifyFilesToUpdateChanged() {
List<IndexItem> filtered = getCachedIndexFiles();
if (filtered != null) {
if (uiActivity != null) {
uiActivity.updateDownloadList();
}
}
}
public boolean checkIfItemOutdated(IndexItem item) { public boolean checkIfItemOutdated(IndexItem item) {
boolean outdated = false; boolean outdated = false;
@ -385,8 +410,6 @@ public class DownloadIndexesThread {
} else { } else {
oldItemSize = app.getAppPath(item.getTargetFileName()).length(); oldItemSize = app.getAppPath(item.getTargetFileName()).length();
} }
if (itemSize != oldItemSize) { if (itemSize != oldItemSize) {
outdated = true; outdated = true;
} }
@ -395,38 +418,6 @@ public class DownloadIndexesThread {
return outdated; return outdated;
} }
private void updateFilesToUpdate() {
List<IndexItem> stillUpdate = new ArrayList<IndexItem>();
for (IndexItem item : itemsToUpdate) {
String sfName = item.getTargetFileName();
java.text.DateFormat format = app.getResourceManager().getDateFormat();
String date = item.getDate(format);
String indexactivateddate = indexActivatedFileNames.get(sfName);
String indexfilesdate = indexFileNames.get(sfName);
if (date != null &&
!date.equals(indexactivateddate) &&
!date.equals(indexfilesdate) &&
indexActivatedFileNames.containsKey(sfName)) {
stillUpdate.add(item);
}
}
itemsToUpdate = stillUpdate;
if (uiActivity != null) {
uiActivity.updateDownloadList();
}
}
public boolean isDownloadRunning() {
for (int i = 0; i < currentRunningTask.size(); i++) {
if (currentRunningTask.get(i) instanceof DownloadIndexesAsyncTask) {
return true;
}
}
return false;
}
public IndexItem getCurrentDownloadingItem() { public IndexItem getCurrentDownloadingItem() {
return currentDownloadingItem; return currentDownloadingItem;
} }
@ -435,7 +426,6 @@ public class DownloadIndexesThread {
return currentDownloadingItemProgress; return currentDownloadingItemProgress;
} }
// FIXME deprecated
public BasicProgressAsyncTask<?, ?, ?, ?> getCurrentRunningTask() { public BasicProgressAsyncTask<?, ?, ?, ?> getCurrentRunningTask() {
for (int i = 0; i < currentRunningTask.size(); ) { for (int i = 0; i < currentRunningTask.size(); ) {
if (currentRunningTask.get(i).getStatus() == Status.FINISHED) { if (currentRunningTask.get(i).getStatus() == Status.FINISHED) {
@ -524,7 +514,6 @@ public class DownloadIndexesThread {
protected void onPostExecute(DownloadIndexes result) { protected void onPostExecute(DownloadIndexes result) {
indexes = result; indexes = result;
notifyFilesToUpdateChanged();
if (result.indexFiles != null) { if (result.indexFiles != null) {
if (result.indexFiles.isIncreasedMapVersion()) { if (result.indexFiles.isIncreasedMapVersion()) {
showWarnDialog(); showWarnDialog();
@ -533,10 +522,9 @@ public class DownloadIndexesThread {
AccessibleToast.makeText(ctx, R.string.list_index_files_was_not_loaded, Toast.LENGTH_LONG).show(); AccessibleToast.makeText(ctx, R.string.list_index_files_was_not_loaded, Toast.LENGTH_LONG).show();
} }
currentRunningTask.remove(this); currentRunningTask.remove(this);
if (uiActivity != null) { notifyFilesToUpdateChanged();
uiActivity.updateProgress(false); updateProgressUI(false);
uiActivity.onCategorizationFinished(); onCategorizationFinished();
}
} }
private void showWarnDialog() { private void showWarnDialog() {
@ -564,13 +552,29 @@ public class DownloadIndexesThread {
@Override @Override
protected void updateProgress(boolean updateOnlyProgress, Void tag) { protected void updateProgress(boolean updateOnlyProgress, Void tag) {
if (uiActivity != null) { updateProgressUI(true);
uiActivity.updateProgress(updateOnlyProgress);
}
} }
} }
//FIXME
private void updateFilesToUpdate() {
List<IndexItem> stillUpdate = new ArrayList<IndexItem>();
for (IndexItem item : itemsToUpdate) {
String sfName = item.getTargetFileName();
java.text.DateFormat format = app.getResourceManager().getDateFormat();
String date = item.getDate(format);
String indexactivateddate = indexActivatedFileNames.get(sfName);
String indexfilesdate = indexFileNames.get(sfName);
if (date != null &&
!date.equals(indexactivateddate) &&
!date.equals(indexfilesdate) &&
indexActivatedFileNames.containsKey(sfName)) {
stillUpdate.add(item);
}
}
itemsToUpdate = stillUpdate;
}
public class DownloadIndexesAsyncTask extends BasicProgressAsyncTask<IndexItem, IndexItem, Object, String> implements DownloadFileShowWarning { public class DownloadIndexesAsyncTask extends BasicProgressAsyncTask<IndexItem, IndexItem, Object, String> implements DownloadFileShowWarning {
@ -594,8 +598,7 @@ public class DownloadIndexesThread {
protected void onProgressUpdate(Object... values) { protected void onProgressUpdate(Object... values) {
for (Object o : values) { for (Object o : values) {
if (o instanceof IndexItem) { if (o instanceof IndexItem) {
if (uiActivity != null) { updateProgressUI(false);
uiActivity.updateFragments();
IndexItem item = (IndexItem) o; IndexItem item = (IndexItem) o;
String name = item.getBasename(); String name = item.getBasename();
long count = dbHelper.getCount(name, DatabaseHelper.DOWNLOAD_ENTRY) + 1; long count = dbHelper.getCount(name, DatabaseHelper.DOWNLOAD_ENTRY) + 1;
@ -605,7 +608,6 @@ public class DownloadIndexesThread {
} else { } else {
dbHelper.update(entry, DatabaseHelper.DOWNLOAD_ENTRY); dbHelper.update(entry, DatabaseHelper.DOWNLOAD_ENTRY);
} }
}
} else if (o instanceof String) { } else if (o instanceof String) {
String message = (String) o; String message = (String) o;
if (!message.equals("I/O error occurred : Interrupted")) { if (!message.equals("I/O error occurred : Interrupted")) {
@ -613,11 +615,8 @@ public class DownloadIndexesThread {
!message.equals(uiActivity.getString(R.string.shared_string_download_successful))) { !message.equals(uiActivity.getString(R.string.shared_string_download_successful))) {
AccessibleToast.makeText(ctx, message, Toast.LENGTH_LONG).show(); AccessibleToast.makeText(ctx, message, Toast.LENGTH_LONG).show();
} }
} else {
if (uiActivity != null) {
uiActivity.updateProgress(true);
}
} }
updateProgressUI(false);
} }
} }
super.onProgressUpdate(values); super.onProgressUpdate(values);
@ -647,16 +646,16 @@ public class DownloadIndexesThread {
if (mainView != null) { if (mainView != null) {
mainView.setKeepScreenOn(false); mainView.setKeepScreenOn(false);
} }
uiActivity.downloadedIndexes();
} }
currentRunningTask.remove(this); currentRunningTask.remove(this);
if (uiActivity != null) { notifyFilesToUpdateChanged();
uiActivity.updateProgress(false);
}
updateFilesToUpdate(); updateFilesToUpdate();
updateProgressUI(false);
} }
@Override @Override
protected String doInBackground(IndexItem... filesToDownload) { protected String doInBackground(IndexItem... filesToDownload) {
try { try {
@ -777,7 +776,7 @@ public class DownloadIndexesThread {
public boolean downloadFile(IndexItem item, List<File> filesToReindex, boolean forceWifi) public boolean downloadFile(IndexItem item, List<File> filesToReindex, boolean forceWifi)
throws InterruptedException { throws InterruptedException {
downloadFileHelper.setInterruptDownloading(false); downloadFileHelper.setInterruptDownloading(false);
DownloadEntry de = item.createDownloadEntry(app); IndexItem.DownloadEntry de = item.createDownloadEntry(app);
boolean res = false; boolean res = false;
if(de == null) { if(de == null) {
return res; return res;
@ -804,7 +803,7 @@ public class DownloadIndexesThread {
@Override @Override
protected void updateProgress(boolean updateOnlyProgress, IndexItem tag) { protected void updateProgress(boolean updateOnlyProgress, IndexItem tag) {
currentDownloadingItemProgress = getProgressPercentage(); currentDownloadingItemProgress = getProgressPercentage();
uiActivity.updateProgress(true); updateProgressUI(true);
} }
} }

View file

@ -162,6 +162,34 @@ public class IndexItem implements Comparable<IndexItem>/*, Parcelable*/ {
return format.format(new Date(timestamp)); return format.format(new Date(timestamp));
} }
public static class DownloadEntry {
public long dateModified;
public double sizeMB;
public File targetFile;
public boolean zipStream;
public boolean unzipFolder;
public File fileToDownload;
public String baseName;
public String urlToDownload;
public boolean isAsset;
public String assetName;
public DownloadActivityType type;
public DownloadEntry() {
}
public DownloadEntry(String assetName, String fileName, long dateModified) {
this.dateModified = dateModified;
targetFile = new File(fileName);
this.assetName = assetName;
isAsset = true;
}
}
// @Override // @Override
// public String toString() { // public String toString() {

View file

@ -34,8 +34,12 @@ public class ActiveDownloadsDialogFragment extends DialogFragment {
return builder.create(); return builder.create();
} }
public void refresh() { public void notifyDataSetInvalidated() {
adapter.updateData(); adapter.refreshAllData();
}
public void notifyDataSetChanged() {
adapter.notifyDataSetChanged();
} }
public void onDetach() { public void onDetach() {
@ -59,10 +63,10 @@ public class ActiveDownloadsDialogFragment extends DialogFragment {
deleteDrawable = context.getMyApplication().getIconsCache() deleteDrawable = context.getMyApplication().getIconsCache()
.getPaintedContentIcon(R.drawable.ic_action_remove_dark, .getPaintedContentIcon(R.drawable.ic_action_remove_dark,
context.getResources().getColor(R.color.dash_search_icon_dark)); context.getResources().getColor(R.color.dash_search_icon_dark));
updateData(); refreshAllData();
} }
public void updateData() { public void refreshAllData() {
clear(); clear();
addAll(context.getDownloadThread().getCurrentDownloadingItems()); addAll(context.getDownloadThread().getCurrentDownloadingItems());
} }
@ -127,7 +131,7 @@ public class ActiveDownloadsDialogFragment extends DialogFragment {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
context.getDownloadThread().cancelDownload(item); context.getDownloadThread().cancelDownload(item);
adapter.updateData(); adapter.refreshAllData();
} }
}); });
progressBar.setIndeterminate(isIndeterminate); progressBar.setIndeterminate(isIndeterminate);

View file

@ -6,24 +6,17 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.WorldRegion; import net.osmand.plus.WorldRegion;
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.activities.OsmandExpandableListFragment;
import net.osmand.plus.base.BasicProgressAsyncTask;
import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.download.DownloadActivityType;
import net.osmand.plus.download.DownloadEntry;
import net.osmand.plus.download.IndexItem; import net.osmand.plus.download.IndexItem;
import org.apache.commons.logging.Log;
import android.app.Dialog; import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;