From e2bed2a6af730e98884ad3a017d40de1d5331bce Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Wed, 23 Sep 2015 16:20:48 +0300 Subject: [PATCH] Download maps hierarchy work in progress. --- .../plus/download/BaseDownloadActivity.java | 45 +-- .../plus/download/DownloadActivity.java | 15 + .../plus/download/DownloadIndexesThread.java | 286 ++++++++++-------- .../net/osmand/plus/download/IndexItem.java | 30 +- .../plus/download/IndexItemCategory.java | 24 +- .../download/NewLocalIndexesFragment.java | 113 ------- .../IndexItemCategoryWithSubcat.java | 261 ++++++++++++++++ .../MapsInCategoryFragment.java | 86 ++++++ .../NewLocalIndexesFragment.java | 159 ++++++++++ .../src/net/osmand/plus/helpers/HasName.java | 5 + 10 files changed, 739 insertions(+), 285 deletions(-) delete mode 100644 OsmAnd/src/net/osmand/plus/download/NewLocalIndexesFragment.java create mode 100644 OsmAnd/src/net/osmand/plus/download/newimplementation/IndexItemCategoryWithSubcat.java create mode 100644 OsmAnd/src/net/osmand/plus/download/newimplementation/MapsInCategoryFragment.java create mode 100644 OsmAnd/src/net/osmand/plus/download/newimplementation/NewLocalIndexesFragment.java create mode 100644 OsmAnd/src/net/osmand/plus/helpers/HasName.java diff --git a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java index 12bbb5e693..8a08b95ab8 100644 --- a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java @@ -1,19 +1,5 @@ package net.osmand.plus.download; -import java.lang.ref.WeakReference; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import net.osmand.access.AccessibleToast; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.OsmandSettings; -import net.osmand.plus.R; -import net.osmand.plus.Version; -import net.osmand.plus.activities.ActionBarProgressActivity; -import net.osmand.plus.base.BasicProgressAsyncTask; import android.app.AlertDialog; import android.content.ActivityNotFoundException; import android.content.DialogInterface; @@ -23,6 +9,22 @@ import android.os.Bundle; import android.support.v4.app.Fragment; import android.widget.Toast; +import net.osmand.access.AccessibleToast; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandSettings; +import net.osmand.plus.R; +import net.osmand.plus.Version; +import net.osmand.plus.activities.ActionBarProgressActivity; +import net.osmand.plus.base.BasicProgressAsyncTask; +import net.osmand.plus.download.newimplementation.IndexItemCategoryWithSubcat; + +import java.lang.ref.WeakReference; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + /** * Created by Denis * on 25.11.2014. @@ -31,8 +33,8 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { protected DownloadActivityType type = DownloadActivityType.NORMAL_FILE; protected OsmandSettings settings; public static DownloadIndexesThread downloadListIndexThread; - protected List> fragList = new ArrayList>(); - protected List downloadQueue = new ArrayList(); + protected List> fragList = new ArrayList<>(); + protected List downloadQueue = new ArrayList<>(); public static final int MAXIMUM_AVAILABLE_FREE_DOWNLOADS = 10; @@ -63,7 +65,7 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { super.onResume(); downloadListIndexThread.setUiActivity(this); } - + @Override protected void onPause() { super.onPause(); @@ -85,7 +87,7 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { public Map> getEntriesToDownload() { if (downloadListIndexThread == null) { - return new LinkedHashMap>(); + return new LinkedHashMap<>(); } return downloadListIndexThread.getEntriesToDownload(); } @@ -110,6 +112,11 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { } + public void onCategorizationFinished(List filtered, + List cats) { + + } + public boolean startDownload(IndexItem item) { if (downloadListIndexThread.getCurrentRunningTask() != null && getEntriesToDownload().get(item) == null) { downloadQueue.add(item); @@ -267,7 +274,7 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { } else { msg.setNeutralButton(R.string.shared_string_ok, null); } - + msg.show(); } } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 51cbfeb58b..c2b410041a 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -32,6 +32,8 @@ import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.activities.TabActivity; import net.osmand.plus.base.BasicProgressAsyncTask; +import net.osmand.plus.download.newimplementation.IndexItemCategoryWithSubcat; +import net.osmand.plus.download.newimplementation.NewLocalIndexesFragment; import net.osmand.plus.srtmplugin.SRTMPlugin; import net.osmand.plus.views.controls.PagerSlidingTabStrip; @@ -303,6 +305,19 @@ public class DownloadActivity extends BaseDownloadActivity { } } + @Override + public void onCategorizationFinished(List filtered, + List cats) { + for (WeakReference ref : fragList) { + Fragment f = ref.get(); + if (f instanceof NewLocalIndexesFragment) { + if (!f.isDetached()) { + ((NewLocalIndexesFragment) f).onCategorizationFinished(filtered, cats); + } + } + } + } + public void downloadListUpdated() { for (WeakReference ref : fragList) { Fragment f = ref.get(); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index fb2c63b242..12e910c117 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -1,6 +1,37 @@ package net.osmand.plus.download; +import android.annotation.SuppressLint; +import android.app.AlertDialog; +import android.content.ActivityNotFoundException; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.net.Uri; +import android.os.AsyncTask; +import android.os.AsyncTask.Status; +import android.os.Build; +import android.os.StatFs; +import android.view.View; +import android.widget.Toast; + +import net.osmand.IndexConstants; +import net.osmand.PlatformUtil; +import net.osmand.access.AccessibleToast; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandSettings.OsmandPreference; +import net.osmand.plus.R; +import net.osmand.plus.Version; +import net.osmand.plus.base.BasicProgressAsyncTask; +import net.osmand.plus.download.DownloadFileHelper.DownloadFileShowWarning; +import net.osmand.plus.download.DownloadOsmandIndexesHelper.AssetIndexItem; +import net.osmand.plus.download.newimplementation.IndexItemCategoryWithSubcat; +import net.osmand.plus.helpers.DatabaseHelper; +import net.osmand.plus.resources.ResourceManager; +import net.osmand.util.Algorithms; + +import org.apache.commons.logging.Log; + import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -16,36 +47,6 @@ import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import net.osmand.IndexConstants; -import net.osmand.PlatformUtil; -import net.osmand.access.AccessibleToast; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.OsmandSettings.OsmandPreference; -import net.osmand.plus.R; -import net.osmand.plus.Version; -import net.osmand.plus.base.BasicProgressAsyncTask; -import net.osmand.plus.download.DownloadFileHelper.DownloadFileShowWarning; -import net.osmand.plus.download.DownloadOsmandIndexesHelper.AssetIndexItem; -import net.osmand.plus.helpers.DatabaseHelper; -import net.osmand.plus.resources.ResourceManager; -import net.osmand.util.Algorithms; - -import org.apache.commons.logging.Log; - -import android.annotation.SuppressLint; -import android.app.AlertDialog; -import android.content.ActivityNotFoundException; -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.net.Uri; -import android.os.AsyncTask; -import android.os.AsyncTask.Status; -import android.os.Build; -import android.os.StatFs; -import android.view.View; -import android.widget.Toast; - @SuppressLint("NewApi") public class DownloadIndexesThread { private BaseDownloadActivity uiActivity = null; @@ -56,7 +57,7 @@ public class DownloadIndexesThread { private OsmandApplication app; private final static Log log = PlatformUtil.getLog(DownloadIndexesThread.class); private DownloadFileHelper downloadFileHelper; - private List > currentRunningTask = Collections.synchronizedList(new ArrayList>()); + private List> currentRunningTask = Collections.synchronizedList(new ArrayList>()); private Map indexFileNames = new LinkedHashMap(); private Map indexActivatedFileNames = new LinkedHashMap(); private java.text.DateFormat dateFormat; @@ -72,22 +73,22 @@ public class DownloadIndexesThread { dbHelper = new DatabaseHelper(app); } - public DatabaseHelper getDbHelper(){ + public DatabaseHelper getDbHelper() { return dbHelper; } public void clear() { indexFiles = null; } - + public void setUiActivity(BaseDownloadActivity uiActivity) { this.uiActivity = uiActivity; } public List flattenDownloadEntries() { List res = new ArrayList(); - for(List ens : getEntriesToDownload().values()) { - if(ens != null) { + for (List ens : getEntriesToDownload().values()) { + if (ens != null) { res.addAll(ens); } } @@ -98,19 +99,19 @@ public class DownloadIndexesThread { return indexFiles != null ? indexFiles.getIndexFiles() : null; } - + public IndexFileList getIndexFiles() { return indexFiles; } - - public Map getIndexFileNames(){ + + public Map getIndexFileNames() { return indexFileNames; } - public Map getIndexActivatedFileNames(){ + public Map getIndexActivatedFileNames() { return indexActivatedFileNames; } - + public void updateLoadedFiles() { Map indexActivatedFileNames = app.getResourceManager().getIndexFileNames(); DownloadIndexFragment.listWithAlternatives(dateFormat, app.getAppPath(""), IndexConstants.EXTRA_EXT, @@ -126,7 +127,7 @@ public class DownloadIndexesThread { this.indexActivatedFileNames = indexActivatedFileNames; //updateFilesToDownload(); } - + public Map getDownloadedIndexFileNames() { return indexFileNames; } @@ -148,11 +149,11 @@ public class DownloadIndexesThread { super(ctx); downloads = app.getSettings().NUMBER_OF_FREE_DOWNLOADS; } - + @Override public void setInterrupted(boolean interrupted) { super.setInterrupted(interrupted); - if(interrupted) { + if (interrupted) { downloadFileHelper.setInterruptDownloading(true); } } @@ -164,10 +165,10 @@ public class DownloadIndexesThread { if (uiActivity != null) { uiActivity.downloadListUpdated(); uiActivity.updateDownloadButton(); - DownloadEntry item = (DownloadEntry)o; + DownloadEntry item = (DownloadEntry) o; String name = item.item.getBasename(); long count = dbHelper.getCount(name, DatabaseHelper.DOWNLOAD_ENTRY) + 1; - DatabaseHelper.HistoryDownloadEntry entry = new DatabaseHelper.HistoryDownloadEntry(name,count); + DatabaseHelper.HistoryDownloadEntry entry = new DatabaseHelper.HistoryDownloadEntry(name, count); if (count == 1) { dbHelper.add(entry, DatabaseHelper.DOWNLOAD_ENTRY); } else { @@ -179,14 +180,14 @@ public class DownloadIndexesThread { if (uiActivity != null) { uiActivity.downloadListUpdated(); uiActivity.updateDownloadButton(); - IndexItem item = (IndexItem)o; + IndexItem item = (IndexItem) o; long count = dbHelper.getCount(item.getBasename(), DatabaseHelper.DOWNLOAD_ENTRY) + 1; dbHelper.add(new DatabaseHelper.HistoryDownloadEntry(item.getBasename(), count), DatabaseHelper.DOWNLOAD_ENTRY); } } else if (o instanceof String) { String message = (String) o; - if(!message.equals("I/O error occurred : Interrupted")){ + if (!message.equals("I/O error occurred : Interrupted")) { AccessibleToast.makeText(ctx, message, Toast.LENGTH_LONG).show(); } } @@ -196,7 +197,7 @@ public class DownloadIndexesThread { @Override protected void onPreExecute() { - currentRunningTask.add( this); + currentRunningTask.add(this); super.onPreExecute(); if (uiActivity != null) { downloadFileHelper.setInterruptDownloading(false); @@ -222,13 +223,12 @@ public class DownloadIndexesThread { uiActivity.downloadedIndexes(); } currentRunningTask.remove(this); - if(uiActivity != null) { + if (uiActivity != null) { uiActivity.updateProgress(false); } updateFilesToUpdate(); } - - + @Override protected String doInBackground(IndexItem... filesToDownload) { @@ -237,39 +237,40 @@ public class DownloadIndexesThread { boolean forceWifi = downloadFileHelper.isWifiConnected(); currentDownloads = new HashSet(); String breakDownloadMessage = null; - downloadCycle : while(!entriesToDownload.isEmpty() ) { - + downloadCycle: + while (!entriesToDownload.isEmpty()) { + Iterator>> it = entriesToDownload.entrySet().iterator(); IndexItem file = null; List list = null; - while(it.hasNext()) { + while (it.hasNext()) { Entry> n = it.next(); - if(!currentDownloads.containsAll(n.getValue())) { + if (!currentDownloads.containsAll(n.getValue())) { file = n.getKey(); list = n.getValue(); break; } } - if(file == null) { + if (file == null) { break downloadCycle; } if (list != null) { boolean success = false; for (DownloadEntry entry : list) { - if(currentDownloads.contains(entry)) { + if (currentDownloads.contains(entry)) { continue; } currentDownloads.add(entry); double asz = getAvailableSpace(); // validate interrupted - if(downloadFileHelper.isInterruptDownloading()) { + if (downloadFileHelper.isInterruptDownloading()) { break downloadCycle; } // validate enough space if (asz != -1 && entry.sizeMB > asz) { - breakDownloadMessage = app.getString(R.string.download_files_not_enough_space, entry.sizeMB, asz); + breakDownloadMessage = app.getString(R.string.download_files_not_enough_space, entry.sizeMB, asz); break downloadCycle; - } + } if (exceedsFreelimit(entry)) { breakDownloadMessage = app.getString(R.string.free_version_message, DownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS + ""); @@ -288,15 +289,15 @@ public class DownloadIndexesThread { publishProgress(entry); } } - if(success) { + if (success) { entriesToDownload.remove(file); } } - + } String warn = reindexFiles(filesToReindex); - if(breakDownloadMessage != null) { - if(warn != null) { + if (breakDownloadMessage != null) { + if (warn != null) { warn = breakDownloadMessage + "\n" + warn; } else { warn = breakDownloadMessage; @@ -331,10 +332,10 @@ public class DownloadIndexesThread { warnings = manager.indexingMaps(this); } List wns = manager.indexAdditionalMaps(this); - if(wns != null) { + if (wns != null) { warnings.addAll(wns); } - + if (!warnings.isEmpty()) { return warnings.get(0); } @@ -366,7 +367,7 @@ public class DownloadIndexesThread { if (ctx != null) { ResourceManager.copyAssets(ctx.getAssets(), de.assetName, de.targetFile); boolean changedDate = de.targetFile.setLastModified(de.dateModified); - if(!changedDate) { + if (!changedDate) { log.error("Set last timestamp is not supported"); } res = true; @@ -382,28 +383,28 @@ public class DownloadIndexesThread { @Override protected void updateProgress(boolean updateOnlyProgress) { - if(uiActivity != null) { + if (uiActivity != null) { uiActivity.updateProgress(updateOnlyProgress); } } } - + private boolean checkRunning() { - if(getCurrentRunningTask() != null) { + if (getCurrentRunningTask() != null) { AccessibleToast.makeText(app, R.string.wait_current_task_finished, Toast.LENGTH_SHORT).show(); return true; } return false; } - - public void runReloadIndexFiles() { - checkRunning(); - final BasicProgressAsyncTask inst = new BasicProgressAsyncTask(ctx) { - @Override - protected IndexFileList doInBackground(Void... params) { - return DownloadOsmandIndexesHelper.getIndexesList(ctx); - }; + public void runReloadIndexFiles() { + runReloadIndexFiles(false); + } + + public void runReloadIndexFiles(final boolean withSubcats) { + checkRunning(); + final BasicProgressAsyncTask inst + = new BasicProgressAsyncTask(ctx) { @Override protected void onPreExecute() { @@ -412,13 +413,18 @@ public class DownloadIndexesThread { this.message = ctx.getString(R.string.downloading_list_indexes); } + @Override + protected IndexFileList doInBackground(Void... params) { + return DownloadOsmandIndexesHelper.getIndexesList(ctx); + } + protected void onPostExecute(IndexFileList result) { indexFiles = result; if (indexFiles != null && uiActivity != null) { prepareFilesToUpdate(); boolean basemapExists = uiActivity.getMyApplication().getResourceManager().containsBasemap(); IndexItem basemap = indexFiles.getBasemap(); - if (basemap != null ) { + if (basemap != null) { String dt = uiActivity.getMyApplication().getResourceManager().getIndexFileNames().get(basemap.getTargetFileName()); if (!basemapExists || !Algorithms.objectEquals(dt, basemap.getDate(dateFormat))) { List downloadEntry = basemap @@ -427,8 +433,8 @@ public class DownloadIndexesThread { uiActivity.getEntriesToDownload().put(basemap, downloadEntry); AccessibleToast.makeText(uiActivity, R.string.basemap_was_selected_to_download, Toast.LENGTH_LONG).show(); - if (uiActivity instanceof DownloadActivity){ - ((DownloadActivity)uiActivity).updateDownloadButton(); + if (uiActivity instanceof DownloadActivity) { + ((DownloadActivity) uiActivity).updateDownloadButton(); } } } @@ -441,7 +447,7 @@ public class DownloadIndexesThread { currentRunningTask.remove(this); if (uiActivity != null) { uiActivity.updateProgress(false); - runCategorization(uiActivity.getDownloadType()); + runCategorization(uiActivity.getDownloadType(), withSubcats); } } @@ -474,24 +480,26 @@ public class DownloadIndexesThread { uiActivity.updateProgress(updateOnlyProgress); } - }; + } + + ; }; - execute(inst, new Void[0]); + execute(inst); } - - public void runDownloadFiles(){ - if(checkRunning()) { + + public void runDownloadFiles() { + if (checkRunning()) { return; } DownloadIndexesAsyncTask task = new DownloadIndexesAsyncTask(ctx); - execute(task, new IndexItem[0]); + execute(task); } - - private

void execute(BasicProgressAsyncTask task, P... indexItems) { - if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) { - task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, indexItems); + + private

void execute(BasicProgressAsyncTask task, P... indexItems) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, indexItems); } else { task.execute(indexItems); } @@ -502,26 +510,40 @@ public class DownloadIndexesThread { } public void runCategorization(final DownloadActivityType type) { - final BasicProgressAsyncTask> inst = new BasicProgressAsyncTask>(ctx) { + runCategorization(type, false); + } + + public void runCategorization(final DownloadActivityType type, final boolean withSubcats) { + final BasicProgressAsyncTask> inst + = new BasicProgressAsyncTask>(ctx) { private List cats; + private List catsWithSubcats; + @Override protected void onPreExecute() { super.onPreExecute(); currentRunningTask.add(this); this.message = ctx.getString(R.string.downloading_list_indexes); - if(uiActivity != null) { + if (uiActivity != null) { uiActivity.updateProgress(false); } } - + @Override protected List doInBackground(Void... params) { final List filtered = getFilteredByType(); - cats = IndexItemCategory.categorizeIndexItems(app, filtered); + if (withSubcats) { + catsWithSubcats = + IndexItemCategoryWithSubcat.categorizeIndexItems(app, filtered); + } else { + cats = IndexItemCategory.categorizeIndexItems(app, filtered); + } updateLoadedFiles(); return filtered; - }; - + } + + ; + public List getFilteredByType() { final List filtered = new ArrayList(); List cachedIndexFiles = getCachedIndexFiles(); @@ -534,28 +556,32 @@ public class DownloadIndexesThread { } return filtered; } - + @Override protected void onPostExecute(List filtered) { prepareFilesToUpdate(); currentRunningTask.remove(this); - if(uiActivity != null) { - uiActivity.categorizationFinished(filtered, cats); + if (uiActivity != null) { + if (withSubcats) { + uiActivity.onCategorizationFinished(filtered, catsWithSubcats); + } else { + uiActivity.categorizationFinished(filtered, cats); + } uiActivity.updateProgress(false); } } @Override protected void updateProgress(boolean updateOnlyProgress) { - if(uiActivity != null) { + if (uiActivity != null) { uiActivity.updateProgress(updateOnlyProgress); } - - }; - + + } + }; - execute(inst, new Void[0]); + execute(inst); } private void prepareFilesToUpdate() { @@ -565,11 +591,11 @@ public class DownloadIndexesThread { for (IndexItem item : filtered) { boolean outdated = checkIfItemOutdated(item); //include only activated files here - if(outdated && indexActivatedFileNames.containsKey(item.getTargetFileName())) { + if (outdated && indexActivatedFileNames.containsKey(item.getTargetFileName())) { itemsToUpdate.add(item); } } - if (uiActivity != null){ + if (uiActivity != null) { uiActivity.updateDownloadList(itemsToUpdate); } } @@ -588,17 +614,17 @@ public class DownloadIndexesThread { if ((item.getType() == DownloadActivityType.NORMAL_FILE && !item.extra) || item.getType() == DownloadActivityType.ROADS_FILE || item.getType() == DownloadActivityType.WIKIPEDIA_FILE || - item.getType() == DownloadActivityType.SRTM_COUNTRY_FILE){ + item.getType() == DownloadActivityType.SRTM_COUNTRY_FILE) { outdated = true; } else { long itemSize = item.getContentSize(); long oldItemSize = 0; - if(item.getType() == DownloadActivityType.VOICE_FILE) { - if(item instanceof AssetIndexItem) { + if (item.getType() == DownloadActivityType.VOICE_FILE) { + if (item instanceof AssetIndexItem) { File file = new File(((AssetIndexItem) item).getDestFile()); oldItemSize = file.length(); } else { - File fl = new File(item.getType().getDownloadFolder(app, item), sfName +"/_config.p"); + File fl = new File(item.getType().getDownloadFolder(app, item), sfName + "/_config.p"); if (fl.exists()) { oldItemSize = fl.length(); try { @@ -612,11 +638,11 @@ public class DownloadIndexesThread { } } } else { - oldItemSize = app.getAppPath(item.getTargetFileName()).length(); + oldItemSize = app.getAppPath(item.getTargetFileName()).length(); } - - - if (itemSize != oldItemSize){ + + + if (itemSize != oldItemSize) { outdated = true; } } @@ -624,7 +650,7 @@ public class DownloadIndexesThread { return outdated; } - private void updateFilesToUpdate(){ + private void updateFilesToUpdate() { List stillUpdate = new ArrayList(); for (IndexItem item : itemsToUpdate) { String sfName = item.getTargetFileName(); @@ -640,7 +666,7 @@ public class DownloadIndexesThread { } } itemsToUpdate = stillUpdate; - if (uiActivity != null){ + if (uiActivity != null) { uiActivity.updateDownloadList(itemsToUpdate); } } @@ -655,16 +681,16 @@ public class DownloadIndexesThread { } return false; } - + public BasicProgressAsyncTask getCurrentRunningTask() { - for(int i = 0; i< currentRunningTask.size(); ) { - if(currentRunningTask.get(i).getStatus() == Status.FINISHED) { + for (int i = 0; i < currentRunningTask.size(); ) { + if (currentRunningTask.get(i).getStatus() == Status.FINISHED) { currentRunningTask.remove(i); } else { i++; } } - if(currentRunningTask.size() > 0) { + if (currentRunningTask.size() > 0) { return currentRunningTask.get(0); } return null; @@ -684,30 +710,30 @@ public class DownloadIndexesThread { int i = 0; Collection> vs = getEntriesToDownload().values(); for (List v : vs) { - for(DownloadEntry e : v) { - if(!currentDownloads.contains(e)) { + for (DownloadEntry e : v) { + if (!currentDownloads.contains(e)) { i++; } } } - if(!currentDownloads.isEmpty()) { + if (!currentDownloads.isEmpty()) { i++; } return i; } - + public int getCountedDownloads() { int i = 0; Collection> vs = getEntriesToDownload().values(); for (List v : vs) { - for(DownloadEntry e : v) { - if(DownloadActivityType.isCountedInDownloads(e.item)) { + for (DownloadEntry e : v) { + if (DownloadActivityType.isCountedInDownloads(e.item)) { i++; } } } return i; } - - + + } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItem.java b/OsmAnd/src/net/osmand/plus/download/IndexItem.java index ffb22b5453..ef2acfb2ed 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItem.java @@ -1,22 +1,24 @@ package net.osmand.plus.download; +import android.content.Context; +import android.support.annotation.NonNull; + +import net.osmand.IndexConstants; +import net.osmand.PlatformUtil; +import net.osmand.map.OsmandRegions; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.helpers.HasName; + +import org.apache.commons.logging.Log; + import java.io.File; import java.io.IOException; import java.util.Date; import java.util.List; import java.util.Map; -import net.osmand.IndexConstants; -import net.osmand.PlatformUtil; -import net.osmand.map.OsmandRegions; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.R; - -import org.apache.commons.logging.Log; - -import android.content.Context; - -public class IndexItem implements Comparable { +public class IndexItem implements Comparable, HasName { private static final Log log = PlatformUtil.getLog(IndexItem.class); String description; @@ -110,7 +112,7 @@ public class IndexItem implements Comparable { } @Override - public int compareTo(IndexItem another) { + public int compareTo(@NonNull IndexItem another) { if(another == null) { return -1; } @@ -141,4 +143,8 @@ public class IndexItem implements Comparable { return format.format(new Date(timestamp)); } + @Override + public String getName() { + return getBasename(); + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItemCategory.java b/OsmAnd/src/net/osmand/plus/download/IndexItemCategory.java index d2418f09f5..c0417eb3a9 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItemCategory.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItemCategory.java @@ -1,5 +1,14 @@ package net.osmand.plus.download; +import android.support.annotation.NonNull; + +import net.osmand.Collator; +import net.osmand.OsmAndCollator; +import net.osmand.map.OsmandRegions; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.Version; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -8,16 +17,9 @@ import java.util.List; import java.util.Map; import java.util.TreeMap; -import net.osmand.Collator; -import net.osmand.OsmAndCollator; -import net.osmand.map.OsmandRegions; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.R; -import net.osmand.plus.Version; - public class IndexItemCategory implements Comparable { public final String name; - public final List items = new ArrayList(); + public final List items = new ArrayList<>(); private final int order; public IndexItemCategory(String name, int order) { @@ -26,14 +28,14 @@ public class IndexItemCategory implements Comparable { } @Override - public int compareTo(IndexItemCategory another) { + public int compareTo(@NonNull IndexItemCategory another) { return order < another.order ? -1 : 1; } public static List categorizeIndexItems(final OsmandApplication ctx, Collection indexItems) { boolean skipWiki = Version.isFreeVersion(ctx); - final Map cats = new TreeMap(); + final Map cats = new TreeMap<>(); for (IndexItem i : indexItems) { int nameId = R.string.index_name_other; int order = 0; @@ -110,7 +112,7 @@ public class IndexItemCategory implements Comparable { } cats.get(name).items.add(i); } - ArrayList r = new ArrayList(cats.values()); + ArrayList r = new ArrayList<>(cats.values()); final Collator collator = OsmAndCollator.primaryCollator(); for(IndexItemCategory ct : r) { final OsmandRegions osmandRegions = ctx.getResourceManager().getOsmandRegions(); diff --git a/OsmAnd/src/net/osmand/plus/download/NewLocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/NewLocalIndexesFragment.java deleted file mode 100644 index d3d522ab25..0000000000 --- a/OsmAnd/src/net/osmand/plus/download/NewLocalIndexesFragment.java +++ /dev/null @@ -1,113 +0,0 @@ -package net.osmand.plus.download; - -import android.app.Activity; -import android.app.AlertDialog.Builder; -import android.content.Context; -import android.content.DialogInterface; -import android.content.res.TypedArray; -import android.graphics.PorterDuff; -import android.graphics.Typeface; -import android.graphics.drawable.Drawable; -import android.os.AsyncTask; -import android.os.Bundle; -import android.os.StatFs; -import android.support.v4.view.MenuItemCompat; -import android.support.v7.app.ActionBar; -import android.support.v7.view.ActionMode; -import android.support.v7.widget.PopupMenu; -import android.text.method.LinkMovementMethod; -import android.view.ContextMenu; -import android.view.ContextMenu.ContextMenuInfo; -import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.SubMenu; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.BaseAdapter; -import android.widget.CheckBox; -import android.widget.EditText; -import android.widget.ExpandableListView; -import android.widget.ExpandableListView.ExpandableListContextMenuInfo; -import android.widget.ImageButton; -import android.widget.ImageView; -import android.widget.ListView; -import android.widget.ProgressBar; -import android.widget.TextView; -import android.widget.Toast; - -import net.osmand.IProgress; -import net.osmand.IndexConstants; -import net.osmand.access.AccessibleToast; -import net.osmand.plus.ContextMenuAdapter; -import net.osmand.plus.ContextMenuAdapter.OnContextMenuClick; -import net.osmand.plus.IconsCache; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.OsmandPlugin; -import net.osmand.plus.R; -import net.osmand.plus.activities.LocalIndexHelper; -import net.osmand.plus.activities.LocalIndexHelper.LocalIndexType; -import net.osmand.plus.activities.LocalIndexInfo; -import net.osmand.plus.activities.OsmAndListFragment; -import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; -import net.osmand.plus.activities.OsmandExpandableListFragment; -import net.osmand.plus.dialogs.DirectionsDialogs; -import net.osmand.plus.helpers.FileNameTranslationHelper; -import net.osmand.plus.resources.IncrementalChangesManager; -import net.osmand.plus.resources.IncrementalChangesManager.IncrementalUpdate; -import net.osmand.plus.resources.IncrementalChangesManager.IncrementalUpdateList; -import net.osmand.util.Algorithms; - -import java.io.File; -import java.text.Collator; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; - - -public class NewLocalIndexesFragment extends OsmAndListFragment { - private static final MessageFormat formatGb = new MessageFormat("{0, number,#.##} GB", Locale.US); - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.local_index_fragment, container, false); - - getDownloadActivity().setSupportProgressBarIndeterminateVisibility(false); - - ListView listView = (ListView)view.findViewById(android.R.id.list); -// listAdapter = new LocalIndexesAdapter(getActivity()); -// listView.setAdapter(listAdapter); -// setListView(listView); - TextView descriptionText = (TextView) view.findViewById(R.id.memory_size); - ProgressBar sizeProgress = (ProgressBar) view.findViewById(R.id.memory_progress); - File dir = getMyApplication().getAppPath("").getParentFile(); - String size = formatGb.format(new Object[]{0}); - int percent = 0; - if(dir.canRead()){ - StatFs statFs = new StatFs(dir.getAbsolutePath()); - //noinspection deprecation - size = formatGb.format(new Object[]{(float) (statFs.getAvailableBlocks()) * statFs.getBlockSize() / (1 << 30) }); - //noinspection deprecation - percent = (int) (statFs.getAvailableBlocks() * 100 / statFs.getBlockCount()); - } - sizeProgress.setProgress(percent); - String text = getString(R.string.free, size); - descriptionText.setText(text); - descriptionText.setMovementMethod(LinkMovementMethod.getInstance()); - return view; - } - - private DownloadActivity getDownloadActivity() { - return (DownloadActivity) getActivity(); - } - -} diff --git a/OsmAnd/src/net/osmand/plus/download/newimplementation/IndexItemCategoryWithSubcat.java b/OsmAnd/src/net/osmand/plus/download/newimplementation/IndexItemCategoryWithSubcat.java new file mode 100644 index 0000000000..099257a15a --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/download/newimplementation/IndexItemCategoryWithSubcat.java @@ -0,0 +1,261 @@ +package net.osmand.plus.download.newimplementation; + +import android.os.Parcel; +import android.os.Parcelable; +import android.support.annotation.NonNull; + +import net.osmand.Collator; +import net.osmand.OsmAndCollator; +import net.osmand.PlatformUtil; +import net.osmand.map.OsmandRegions; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.Version; +import net.osmand.plus.download.IndexItem; +import net.osmand.plus.helpers.HasName; + +import org.apache.commons.logging.Log; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +public class IndexItemCategoryWithSubcat implements Comparable, + Parcelable, HasName { + private static final Log LOG = PlatformUtil.getLog(IndexItemCategoryWithSubcat.class); + + public final List items; + public final List subcats; + public final CategoryStaticData categoryStaticData; + + public IndexItemCategoryWithSubcat(CategoryStaticData categoryStaticData) { + this.categoryStaticData = categoryStaticData; + items = new ArrayList<>(); + subcats = new ArrayList<>(); + } + + @Override + public int compareTo(@NonNull IndexItemCategoryWithSubcat another) { + final int BEFORE = -1; + final int EQUAL = 0; + final int AFTER = 1; + + if (this == another) return EQUAL; + + if (this.categoryStaticData.getOrder() < another.categoryStaticData.getOrder()) + return BEFORE; + if (this.categoryStaticData.getOrder() > another.categoryStaticData.getOrder()) + return AFTER; + + return EQUAL; + } + + public static List categorizeIndexItems(final OsmandApplication ctx, + Collection indexItems) { + boolean skipWiki = Version.isFreeVersion(ctx); + final Map cats = new TreeMap<>(); + ArrayList mainList = new ArrayList<>(); + for (IndexItem i : indexItems) { + String lowerCase = i.getFileName().toLowerCase(); + CategoryStaticData categoryStaticData; + if (lowerCase.endsWith(".voice.zip")) { + categoryStaticData = CategoryStaticData.NAME_VOICE; + } else if (lowerCase.contains(".ttsvoice.zip")) { + categoryStaticData = CategoryStaticData.TTS_VOICE; + } else if (lowerCase.contains("_wiki_")) { + if (skipWiki) { + continue; + } + categoryStaticData = CategoryStaticData.WIKI; + } else if (lowerCase.startsWith("us") || + (lowerCase.contains("united states") && lowerCase.startsWith("north-america"))) { + categoryStaticData = CategoryStaticData.US; + } else if (lowerCase.startsWith("canada")) { + categoryStaticData = CategoryStaticData.CANADA; + } else if (lowerCase.contains("openmaps")) { + categoryStaticData = CategoryStaticData.OPENMAPS; + } else if (lowerCase.contains("northamerica") || lowerCase.contains("north-america")) { + categoryStaticData = CategoryStaticData.NORTH_AMERICA; + } else if (lowerCase.contains("centralamerica") || lowerCase.contains("central-america") + || lowerCase.contains("caribbean")) { + categoryStaticData = CategoryStaticData.CENTRAL_AMERICA; + } else if (lowerCase.contains("southamerica") || lowerCase.contains("south-america")) { + categoryStaticData = CategoryStaticData.SOUTH_AMERICA; + } else if (lowerCase.contains("germany")) { + categoryStaticData = CategoryStaticData.GERMANY; + } else if (lowerCase.startsWith("france_")) { + categoryStaticData = CategoryStaticData.FRANCE; + } else if (lowerCase.startsWith("italy_")) { + categoryStaticData = CategoryStaticData.ITALY; + } else if (lowerCase.startsWith("gb_") || lowerCase.startsWith("british")) { + categoryStaticData = CategoryStaticData.GB; + } else if (lowerCase.contains("netherlands")) { + categoryStaticData = CategoryStaticData.NETHERLANDS; + } else if (lowerCase.contains("russia")) { + categoryStaticData = CategoryStaticData.RUSSIA; + } else if (lowerCase.contains("europe")) { + categoryStaticData = CategoryStaticData.EUROPE; + } else if (lowerCase.contains("africa") && !lowerCase.contains("_wiki_")) { + categoryStaticData = CategoryStaticData.AFRICA; + } else if (lowerCase.contains("_asia") || lowerCase.startsWith("asia")) { + categoryStaticData = CategoryStaticData.ASIA; + } else if (lowerCase.contains("oceania") || lowerCase.contains("australia")) { + categoryStaticData = CategoryStaticData.OCEANIA; + } else if (lowerCase.contains("tour")) { + categoryStaticData = CategoryStaticData.TOURS; + } else { + categoryStaticData = CategoryStaticData.WORLD_WIDE_AND_TOPIC; + } + String name = ctx.getString(categoryStaticData.getNameId()); + categoryStaticData.setName(name); + final IndexItemCategoryWithSubcat category = + new IndexItemCategoryWithSubcat(categoryStaticData); + if (!cats.containsKey(name)) { + cats.put(name, category); + LOG.debug("category=" + category.categoryStaticData); + if (!categoryStaticData.hasParent()) { + mainList.add(category); + } else { + final CategoryStaticData parent = categoryStaticData.getParent(); + if (cats.get(parent.getName()) == null) { + cats.put(parent.getName(), new IndexItemCategoryWithSubcat(parent)); + } else { + cats.get(parent.getName()).subcats.add(category); + } + } + } + cats.get(name).items.add(i); + } + final Collator collator = OsmAndCollator.primaryCollator(); + for (IndexItemCategoryWithSubcat ct : mainList) { + final OsmandRegions osmandRegions = ctx.getResourceManager().getOsmandRegions(); + Collections.sort(ct.items, new Comparator() { + @Override + public int compare(IndexItem lhs, IndexItem rhs) { + return collator.compare(lhs.getVisibleName(ctx, osmandRegions), + rhs.getVisibleName(ctx, osmandRegions)); + } + }); + } + Collections.sort(mainList); + return mainList; + } + + @Override + public String getName() { + return categoryStaticData.getName(); + } + + public enum CategoryStaticData { + WORLD_WIDE_AND_TOPIC(R.string.index_name_other, 0), + NAME_VOICE(R.string.index_name_voice, 1), + TTS_VOICE(R.string.index_name_tts_voice, 2), + WIKI(R.string.index_name_wiki, 10), + OPENMAPS(R.string.index_name_openmaps, 90), + NORTH_AMERICA(R.string.index_name_north_america, 30), + US(R.string.index_name_us, 31, NORTH_AMERICA), + CANADA(R.string.index_name_canada, 32, NORTH_AMERICA), + CENTRAL_AMERICA(R.string.index_name_central_america, 40), + SOUTH_AMERICA(R.string.index_name_south_america, 45), + RUSSIA(R.string.index_name_russia, 25), + EUROPE(R.string.index_name_europe, 15), + GERMANY(R.string.index_name_germany, 16, EUROPE), + FRANCE(R.string.index_name_france, 17, EUROPE), + ITALY(R.string.index_name_italy, 18, EUROPE), + GB(R.string.index_name_gb, 19, EUROPE), + NETHERLANDS(R.string.index_name_netherlands, 20, EUROPE), + AFRICA(R.string.index_name_africa, 80), + ASIA(R.string.index_name_asia, 50), + OCEANIA(R.string.index_name_oceania, 70), + TOURS(R.string.index_tours, 0); + + private final int nameId; + private final int order; + private final CategoryStaticData parent; + private String name; + + CategoryStaticData(int nameId, int order) { + this.nameId = nameId; + this.order = order; + parent = null; + } + + CategoryStaticData(int nameId, int order, CategoryStaticData parent) { + this.nameId = nameId; + this.order = order; + this.parent = parent; + } + + public int getNameId() { + return nameId; + } + + public int getOrder() { + return order; + } + + public CategoryStaticData getParent() { + return parent; + } + + public boolean hasParent() { + return parent != null; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "CategoryStaticData{" + + "nameId=" + nameId + + ", order=" + order + + ", parent=" + parent + + ", name='" + name + '\'' + + '}'; + } + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeList(this.items); + dest.writeList(this.subcats); + dest.writeInt(this.categoryStaticData.ordinal()); + dest.writeString(this.categoryStaticData.getName()); + } + + protected IndexItemCategoryWithSubcat(Parcel in) { + this.items = new ArrayList(); + in.readList(this.items, List.class.getClassLoader()); + this.subcats = new ArrayList(); + in.readList(this.subcats, List.class.getClassLoader()); + int tmpCategoryStaticData = in.readInt(); + this.categoryStaticData = CategoryStaticData.values()[tmpCategoryStaticData]; + this.categoryStaticData.setName(in.readString()); + } + + public static final Parcelable.Creator CREATOR = + new Parcelable.Creator() { + public IndexItemCategoryWithSubcat createFromParcel(Parcel source) { + return new IndexItemCategoryWithSubcat(source); + } + + public IndexItemCategoryWithSubcat[] newArray(int size) { + return new IndexItemCategoryWithSubcat[size]; + } + }; +} diff --git a/OsmAnd/src/net/osmand/plus/download/newimplementation/MapsInCategoryFragment.java b/OsmAnd/src/net/osmand/plus/download/newimplementation/MapsInCategoryFragment.java new file mode 100644 index 0000000000..b7d7ace463 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/download/newimplementation/MapsInCategoryFragment.java @@ -0,0 +1,86 @@ +package net.osmand.plus.download.newimplementation; + +import android.content.Context; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.app.DialogFragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.TextView; + +import net.osmand.plus.R; +import net.osmand.plus.helpers.HasName; + +public class MapsInCategoryFragment extends DialogFragment { + public static final String TAG = "MapsInCategoryFragment"; + private static final String CATEGORY = "category"; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.maps_in_category_fragment, container, false); + + IndexItemCategoryWithSubcat category = getArguments().getParcelable(CATEGORY); + assert category != null; + ListView listView = (ListView) view.findViewById(android.R.id.list); + final MapFilesAdapter mAdapter = new MapFilesAdapter(getActivity()); + listView.setAdapter(mAdapter); + mAdapter.add(new Divider("maps")); + mAdapter.addAll(category.items); + mAdapter.add(new Divider("subcategories")); + mAdapter.addAll(category.subcats); + + return view; + } + + public static MapsInCategoryFragment createInstance( + @NonNull IndexItemCategoryWithSubcat category) { + Bundle bundle = new Bundle(); + bundle.putParcelable(CATEGORY, category); + MapsInCategoryFragment fragment = new MapsInCategoryFragment(); + fragment.setArguments(bundle); + return fragment; + } + + private static class MapFilesAdapter extends ArrayAdapter { + + public MapFilesAdapter(Context context) { + super(context, R.layout.simple_list_menu_item); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + ViewHolder viewHolder; + if (convertView == null) { + convertView = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.simple_list_menu_item, parent, false); + viewHolder = new ViewHolder(); + viewHolder.textView = (TextView) convertView.findViewById(R.id.title); + convertView.setTag(viewHolder); + } else { + viewHolder = (ViewHolder) convertView.getTag(); + } + viewHolder.textView.setText(getItem(position).getName()); + return convertView; + } + + private static class ViewHolder { + TextView textView; + } + } + + public static class Divider implements HasName { + private final String text; + + public Divider(String text) { + this.text = text; + } + + @Override + public String getName() { + return text; + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/download/newimplementation/NewLocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/newimplementation/NewLocalIndexesFragment.java new file mode 100644 index 0000000000..4f02c22e91 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/download/newimplementation/NewLocalIndexesFragment.java @@ -0,0 +1,159 @@ +package net.osmand.plus.download.newimplementation; + +import android.content.Context; +import android.os.Bundle; +import android.os.StatFs; +import android.support.v4.view.MenuItemCompat; +import android.text.Html; +import android.text.method.LinkMovementMethod; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.ProgressBar; +import android.widget.TextView; + +import net.osmand.PlatformUtil; +import net.osmand.plus.R; +import net.osmand.plus.activities.OsmAndListFragment; +import net.osmand.plus.download.BaseDownloadActivity; +import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.IndexItem; + +import org.apache.commons.logging.Log; + +import java.io.File; +import java.text.MessageFormat; +import java.util.List; +import java.util.Locale; + + +public class NewLocalIndexesFragment extends OsmAndListFragment { + private static final Log LOG = PlatformUtil.getLog(NewLocalIndexesFragment.class); + private static final MessageFormat formatGb = new MessageFormat("{0, number,#.##} GB", Locale.US); + + public static final int RELOAD_ID = 0; + private CategoriesAdapter mAdapter; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setHasOptionsMenu(true); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.local_index_fragment, container, false); + + getDownloadActivity().setSupportProgressBarIndeterminateVisibility(false); + + ProgressBar sizeProgress = (ProgressBar) view.findViewById(R.id.memory_progress); + File dir = getMyApplication().getAppPath("").getParentFile(); + String size = formatGb.format(new Object[]{0}); + int percent = 0; + if (dir.canRead()) { + StatFs statFs = new StatFs(dir.getAbsolutePath()); + //noinspection deprecation + size = formatGb.format(new Object[]{(float) (statFs.getAvailableBlocks()) * statFs.getBlockSize() / (1 << 30)}); + //noinspection deprecation + percent = statFs.getAvailableBlocks() * 100 / statFs.getBlockCount(); + } + sizeProgress.setProgress(percent); + String text = getString(R.string.free, size); + + TextView descriptionText = (TextView) view.findViewById(R.id.memory_size); + descriptionText.setText(Html.fromHtml(text)); + descriptionText.setMovementMethod(LinkMovementMethod.getInstance()); + + TextView downloadsLeftTextView = (TextView) view.findViewById(R.id.downloadsLeftTextView); + downloadsLeftTextView.setText(getString(R.string.downloads_left_template, + BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS + - getMyApplication().getSettings().NUMBER_OF_FREE_DOWNLOADS.get())); + TextView freeVersionDescriptionTextView = + (TextView) view.findViewById(R.id.freeVersionDescriptionTextView); + freeVersionDescriptionTextView.setText(getString(R.string.free_version_message, + BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS)); + + + ListView listView = (ListView) view.findViewById(android.R.id.list); + mAdapter = new CategoriesAdapter(getActivity()); + listView.setAdapter(mAdapter); + listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + LOG.debug("onItemClick()"); + + } + }); +// listAdapter = new LocalIndexesAdapter(getActivity()); +// listView.setAdapter(listAdapter); +// setListView(listView); + return view; + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + MenuItem item = menu.add(0, RELOAD_ID, 0, R.string.shared_string_refresh); + item.setIcon(R.drawable.ic_action_refresh_dark); + MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == RELOAD_ID) { + // re-create the thread + DownloadActivity.downloadListIndexThread.runReloadIndexFiles(true); + return true; + } + return super.onOptionsItemSelected(item); + } + + @Override + public void onListItemClick(ListView l, View v, int position, long id) { + MapsInCategoryFragment.createInstance(mAdapter.getItem(position)) + .show(getChildFragmentManager(), MapsInCategoryFragment.TAG); + LOG.debug("onListItemClick()"); + super.onListItemClick(l, v, position, id); + } + + private DownloadActivity getDownloadActivity() { + return (DownloadActivity) getActivity(); + } + + public void onCategorizationFinished(List filtered, List cats) { + LOG.debug("cats=" + cats); + mAdapter.clear(); + mAdapter.addAll(cats); + } + + private static class CategoriesAdapter extends ArrayAdapter { + public CategoriesAdapter(Context context) { + super(context, R.layout.simple_list_menu_item); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + ViewHolder viewHolder; + if (convertView == null) { + convertView = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.simple_list_menu_item, parent, false); + viewHolder = new ViewHolder(); + viewHolder.textView = (TextView) convertView.findViewById(R.id.title); + convertView.setTag(viewHolder); + } else { + viewHolder = (ViewHolder) convertView.getTag(); + } + viewHolder.textView.setText(getItem(position).categoryStaticData.getName()); + return convertView; + } + + private static class ViewHolder { + TextView textView; + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/helpers/HasName.java b/OsmAnd/src/net/osmand/plus/helpers/HasName.java new file mode 100644 index 0000000000..8f58c96568 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/helpers/HasName.java @@ -0,0 +1,5 @@ +package net.osmand.plus.helpers; + +public interface HasName { + String getName(); +}