From 0a4a26be91b3b5b9da7d36f80846f55e90d42c41 Mon Sep 17 00:00:00 2001 From: Denis Date: Tue, 25 Nov 2014 17:28:33 +0200 Subject: [PATCH] Refactored downloads activity to be able to use update functionality in dashboard. --- OsmAnd/res/layout/dash_updates_fragment.xml | 6 + .../plus/dashboard/DashFavoritesFragment.java | 13 +- .../plus/dashboard/DashPluginsFragment.java | 3 - .../plus/dashboard/DashUpdatesFragment.java | 48 +++++++ .../plus/dashboard/DashboardActivity.java | 21 ++- .../plus/download/BaseDownloadActivity.java | 133 ++++++++++++++++++ .../plus/download/DownloadActivity.java | 92 +----------- .../plus/download/DownloadIndexesThread.java | 4 +- 8 files changed, 218 insertions(+), 102 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java diff --git a/OsmAnd/res/layout/dash_updates_fragment.xml b/OsmAnd/res/layout/dash_updates_fragment.xml index 0334c02e46..bd0eae9de4 100644 --- a/OsmAnd/res/layout/dash_updates_fragment.xml +++ b/OsmAnd/res/layout/dash_updates_fragment.xml @@ -8,6 +8,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content"> + + \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java b/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java index 920120f102..a1e306fefe 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java @@ -63,6 +63,12 @@ public class DashFavoritesFragment extends DashBaseFragment { View mainView = getView(); final FavouritesDbHelper helper = getMyApplication().getFavorites(); final List points = new ArrayList(helper.getFavouritePoints()); + if (points.size() == 0){ + (mainView.findViewById(R.id.main_fav)).setVisibility(View.GONE); + return; + } else { + (mainView.findViewById(R.id.main_fav)).setVisibility(View.VISIBLE); + } Collections.sort(points, new Comparator() { @Override public int compare(FavouritePoint point, FavouritePoint point2) { @@ -76,12 +82,7 @@ public class DashFavoritesFragment extends DashBaseFragment { }); LinearLayout favorites = (LinearLayout) mainView.findViewById(R.id.favorites); favorites.removeAllViews(); - if (points.size() == 0){ - (mainView.findViewById(R.id.main_fav)).setVisibility(View.GONE); - return; - } else { - (mainView.findViewById(R.id.main_fav)).setVisibility(View.VISIBLE); - } + if (points.size() > 3){ while (points.size() != 3){ diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashPluginsFragment.java b/OsmAnd/src/net/osmand/plus/dashboard/DashPluginsFragment.java index 10b57d41ca..a853e357a1 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashPluginsFragment.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashPluginsFragment.java @@ -72,8 +72,5 @@ public class DashPluginsFragment extends DashBaseFragment { view.setLayoutParams(lp); layout.addView(view); } - - - } } diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashUpdatesFragment.java b/OsmAnd/src/net/osmand/plus/dashboard/DashUpdatesFragment.java index 11ee470320..cc24de5ea6 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashUpdatesFragment.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashUpdatesFragment.java @@ -5,7 +5,15 @@ import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.LinearLayout; +import android.widget.TextView; import net.osmand.plus.R; +import net.osmand.plus.download.BaseDownloadActivity; +import net.osmand.plus.download.DownloadEntry; +import net.osmand.plus.download.IndexItem; + +import java.util.ArrayList; +import java.util.List; /** * Created by Denis on 21.11.2014. @@ -21,4 +29,44 @@ public class DashUpdatesFragment extends DashBaseFragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } + + public void updatedDownloadsList(List list) { + View mainView = getView(); + if (mainView == null) { + return; + } + if (list.size() > 0) { + mainView.setVisibility(View.VISIBLE); + } else { + mainView.setVisibility(View.GONE); + return; + } + LinearLayout updates = (LinearLayout) mainView.findViewById(R.id.updates_items); + updates.removeAllViews(); + + for (int i = 0; i < list.size(); i++) { + final IndexItem item = list.get(i); + if (i > 2) { + break; + } + LayoutInflater inflater = getActivity().getLayoutInflater(); + View view = inflater.inflate(R.layout.dash_updates_item, null, false); + String eName = item.getVisibleDescription(getMyApplication()) + "\n" + + item.getVisibleName(getMyApplication(), getMyApplication().getResourceManager().getOsmandRegions()); + String d = item.getDate(getMyApplication().getResourceManager().getDateFormat()) + "\n" + item.getSizeDescription(getMyApplication()); + ((TextView) view.findViewById(R.id.map_name)).setText(eName); + ((TextView) view.findViewById(R.id.map_descr)).setText(d); + (view.findViewById(R.id.btn_download)).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + List download = item.createDownloadEntry(getMyApplication(), item.getType(), new ArrayList()); + getDownloadActivity().getEntriesToDownload().put(item, download); + } + }); + } + } + + private BaseDownloadActivity getDownloadActivity(){ + return (BaseDownloadActivity)getActivity(); + } } diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashboardActivity.java b/OsmAnd/src/net/osmand/plus/dashboard/DashboardActivity.java index 9f4f676e84..1b2f8a6c17 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashboardActivity.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashboardActivity.java @@ -22,6 +22,9 @@ import net.osmand.plus.activities.MainMenuActivity; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.TipsAndTricksActivity; import net.osmand.plus.base.FavoriteImageDrawable; +import net.osmand.plus.download.BaseDownloadActivity; +import net.osmand.plus.download.DownloadActivityType; +import net.osmand.plus.download.DownloadIndexesThread; import net.osmand.util.MapUtils; import java.util.*; @@ -29,9 +32,11 @@ import java.util.*; /** * Created by Denis on 05.11.2014. */ -public class DashboardActivity extends SherlockFragmentActivity { +public class DashboardActivity extends BaseDownloadActivity { public static final boolean TIPS_AND_TRICKS = false; + public static DownloadIndexesThread downloadListIndexThread; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -41,6 +46,16 @@ public class DashboardActivity extends SherlockFragmentActivity { getSupportActionBar().setBackgroundDrawable(color); getSupportActionBar().setIcon(android.R.color.transparent); + if(downloadListIndexThread == null) { + downloadListIndexThread = new DownloadIndexesThread(this); + } + if (downloadListIndexThread.getCachedIndexFiles() != null && downloadListIndexThread.isDownloadedFromInternet()) { + downloadListIndexThread.runCategorization(DownloadActivityType.NORMAL_FILE); + } else { + downloadListIndexThread.runReloadIndexFiles(); + } + downloadListIndexThread.setUiActivity(this); + android.support.v4.app.FragmentManager manager = getSupportFragmentManager(); android.support.v4.app.FragmentTransaction fragmentTransaction = manager.beginTransaction(); @@ -92,10 +107,6 @@ public class DashboardActivity extends SherlockFragmentActivity { return true; } - private OsmandApplication getMyApplication() { - return (OsmandApplication) getApplication(); - } - @Override public boolean onCreateOptionsMenu(Menu menu) { return true; diff --git a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java new file mode 100644 index 0000000000..584dba9511 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java @@ -0,0 +1,133 @@ +package net.osmand.plus.download; + +import android.app.AlertDialog; +import android.content.DialogInterface; +import com.actionbarsherlock.app.SherlockFragmentActivity; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandSettings; +import net.osmand.plus.R; +import net.osmand.plus.Version; + +import java.text.MessageFormat; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by Denis on 25.11.2014. + */ +public class BaseDownloadActivity extends SherlockFragmentActivity { + protected DownloadActivityType type = DownloadActivityType.NORMAL_FILE; + protected OsmandSettings settings; + public static DownloadIndexesThread downloadListIndexThread; + + public static final int MAXIMUM_AVAILABLE_FREE_DOWNLOADS = 10; + + public void updateDownloadList(List list){ + + } + + public void updateProgress(boolean updateOnlyProgress) { + + } + + public DownloadActivityType getDownloadType(){ + return type; + } + + public Map> getEntriesToDownload() { + if(downloadListIndexThread == null) { + return new LinkedHashMap>(); + } + return downloadListIndexThread.getEntriesToDownload(); + } + + public void downloadedIndexes() { + + } + + public void updateDownloadButton(boolean scroll) { + + } + + public void downloadListUpdated(){ + + } + + public OsmandApplication getMyApplication(){ + return (OsmandApplication)getApplication(); + } + + public void categorizationFinished(List filtered, List cats){ + + } + + public void downloadFilesPreCheckSpace() { + double sz = 0; + List list = downloadListIndexThread.flattenDownloadEntries(); + for (DownloadEntry es : list) { + sz += es.sizeMB; + } + // get availabile space + double asz = downloadListIndexThread.getAvailableSpace(); + if (asz != -1 && asz > 0 && sz / asz > 0.4) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setMessage(MessageFormat.format(getString(R.string.download_files_question_space), list.size(), sz, asz)); + builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + downloadListIndexThread.runDownloadFiles(); + } + }); + builder.setNegativeButton(R.string.default_buttons_no, null); + builder.show(); + } else { + downloadListIndexThread.runDownloadFiles(); + } + } + + protected void downloadFilesCheckFreeVersion() { + if (Version.isFreeVersion(getMyApplication()) ) { + int total = settings.NUMBER_OF_FREE_DOWNLOADS.get(); + boolean wiki = false; + for (IndexItem es : DownloadActivity.downloadListIndexThread.getEntriesToDownload().keySet()) { + if (es.getBasename() != null && es.getBasename().contains("_wiki")) { + wiki = true; + break; + } else if (DownloadActivityType.isCountedInDownloads(es.getType())) { + total++; + } + } + if (total > MAXIMUM_AVAILABLE_FREE_DOWNLOADS || wiki) { + String msgTx = getString(R.string.free_version_message, MAXIMUM_AVAILABLE_FREE_DOWNLOADS + ""); + AlertDialog.Builder msg = new AlertDialog.Builder(this); + msg.setTitle(R.string.free_version_title); + msg.setMessage(msgTx); + msg.setPositiveButton(R.string.default_buttons_ok, null); + msg.show(); + } else { + downloadFilesCheckInternet(); + } + } else { + downloadFilesCheckInternet(); + } + } + + protected void downloadFilesCheckInternet() { + if(!getMyApplication().getSettings().isWifiConnected()) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setMessage(getString(R.string.download_using_mobile_internet)); + builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + downloadFilesPreCheckSpace(); + } + }); + builder.setNegativeButton(R.string.default_buttons_no, null); + builder.show(); + } else { + downloadFilesPreCheckSpace(); + } + } +} + diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 3f0e10aada..7c5d98020b 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -35,15 +35,10 @@ import java.util.*; /** * Created by Denis on 08.09.2014. */ -public class DownloadActivity extends SherlockFragmentActivity { +public class DownloadActivity extends BaseDownloadActivity { private TabHost tabHost; private FavouritesActivity.TabsAdapter mTabsAdapter; - public static DownloadIndexesThread downloadListIndexThread; - private DownloadActivityType type = DownloadActivityType.NORMAL_FILE; - public static final int MAXIMUM_AVAILABLE_FREE_DOWNLOADS = 10; - - private OsmandSettings settings; private View progressView; private ProgressBar indeterminateProgressBar; @@ -241,8 +236,6 @@ public class DownloadActivity extends SherlockFragmentActivity { return localIndexInfos; } - public DownloadActivityType getDownloadType() { return type;} - public void setType(DownloadActivityType type) { this.type = type;} public void changeType(final DownloadActivityType tp) { @@ -253,90 +246,12 @@ public class DownloadActivity extends SherlockFragmentActivity { } } - public void downloadFilesPreCheckSpace() { - double sz = 0; - List list = downloadListIndexThread.flattenDownloadEntries(); - for (DownloadEntry es : list) { - sz += es.sizeMB; - } - // get availabile space - double asz = downloadListIndexThread.getAvailableSpace(); - if (asz != -1 && asz > 0 && sz / asz > 0.4) { - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage(MessageFormat.format(getString(R.string.download_files_question_space), list.size(), sz, asz)); - builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - downloadListIndexThread.runDownloadFiles(); - } - }); - builder.setNegativeButton(R.string.default_buttons_no, null); - builder.show(); - } else { - downloadListIndexThread.runDownloadFiles(); - } - - } - - public Map> getEntriesToDownload() { - if(downloadListIndexThread == null) { - return new LinkedHashMap>(); - } - return downloadListIndexThread.getEntriesToDownload(); - } - @Override public void onPause() { super.onPause(); (getMyApplication()).setDownloadActivity(null); } - protected void downloadFilesCheckFreeVersion() { - if (Version.isFreeVersion(getMyApplication()) ) { - int total = settings.NUMBER_OF_FREE_DOWNLOADS.get(); - boolean wiki = false; - for (IndexItem es : DownloadActivity.downloadListIndexThread.getEntriesToDownload().keySet()) { - if (es.getBasename() != null && es.getBasename().contains("_wiki")) { - wiki = true; - break; - } else if (DownloadActivityType.isCountedInDownloads(es.getType())) { - total++; - } - } - if (total > MAXIMUM_AVAILABLE_FREE_DOWNLOADS || wiki) { - String msgTx = getString(R.string.free_version_message, MAXIMUM_AVAILABLE_FREE_DOWNLOADS + ""); - AlertDialog.Builder msg = new AlertDialog.Builder(this); - msg.setTitle(R.string.free_version_title); - msg.setMessage(msgTx); - msg.setPositiveButton(R.string.default_buttons_ok, null); - msg.show(); - } else { - downloadFilesCheckInternet(); - } - } else { - downloadFilesCheckInternet(); - } - } - - protected void downloadFilesCheckInternet() { - if(!getMyApplication().getSettings().isWifiConnected()) { - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage(getString(R.string.download_using_mobile_internet)); - builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - downloadFilesPreCheckSpace(); - } - }); - builder.setNegativeButton(R.string.default_buttons_no, null); - builder.show(); - } else { - downloadFilesPreCheckSpace(); - } - } - - public OsmandApplication getMyApplication(){ return (OsmandApplication)getApplication();} - public void showDialogOfFreeDownloadsIfNeeded() { if (Version.isFreeVersion(getMyApplication())) { AlertDialog.Builder msg = new AlertDialog.Builder(this); @@ -361,6 +276,7 @@ public class DownloadActivity extends SherlockFragmentActivity { } } + @Override public void updateProgress(boolean updateOnlyProgress) { BasicProgressAsyncTask basicProgressAsyncTask = DownloadActivity.downloadListIndexThread.getCurrentRunningTask(); //needed when rotation is performed and progress can be null @@ -411,6 +327,7 @@ public class DownloadActivity extends SherlockFragmentActivity { bld.show(); } + @Override public void updateDownloadList(List list){ for(WeakReference ref : fragList) { Fragment f = ref.get(); @@ -422,6 +339,7 @@ public class DownloadActivity extends SherlockFragmentActivity { } } + @Override public void categorizationFinished(List filtered, List cats){ for(WeakReference ref : fragList) { Fragment f = ref.get(); @@ -444,6 +362,7 @@ public class DownloadActivity extends SherlockFragmentActivity { } } + @Override public void downloadedIndexes(){ for(WeakReference ref : fragList) { Fragment f = ref.get(); @@ -464,6 +383,7 @@ public class DownloadActivity extends SherlockFragmentActivity { } + @Override public void updateDownloadButton(boolean scroll) { // View view = getView(); // if (view == null || getExpandableListView() == null){ diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index 7490993b42..548b8e9637 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -43,7 +43,7 @@ import android.view.View; import android.widget.Toast; public class DownloadIndexesThread { - private DownloadActivity uiActivity = null; + private BaseDownloadActivity uiActivity = null; private IndexFileList indexFiles = null; private Map> entriesToDownload = new ConcurrentHashMap>(); private Set currentDownloads = new HashSet(); @@ -69,7 +69,7 @@ public class DownloadIndexesThread { indexFiles = null; } - public void setUiActivity(DownloadActivity uiActivity) { + public void setUiActivity(BaseDownloadActivity uiActivity) { this.uiActivity = uiActivity; }