diff --git a/OsmAnd-java/src/net/osmand/map/OsmandRegions.java b/OsmAnd-java/src/net/osmand/map/OsmandRegions.java index cd764b4f2b..9f0c35bc89 100644 --- a/OsmAnd-java/src/net/osmand/map/OsmandRegions.java +++ b/OsmAnd-java/src/net/osmand/map/OsmandRegions.java @@ -403,7 +403,7 @@ public class OsmandRegions { if(locPrefix == null || locName == null) { throw new IllegalStateException("There is no prefix registered for " + fullName + " (" + parentFullName + ") "); } - fullNamesToLocaleNames.put(fullName, locPrefix + " " + locName); + fullNamesToLocaleNames.put(fullName, /*locPrefix + " " +*/ locName); String index = fullNamesToLowercaseIndex.get(fullName); String prindex = fullNamesToLowercaseIndex.get(parentFullName); fullNamesToLowercaseIndex.put(fullName, index + " " + prindex); diff --git a/OsmAnd/res/layout/download_search_items_fragment.xml b/OsmAnd/res/layout/download_search_items_fragment.xml new file mode 100644 index 0000000000..5c9432e349 --- /dev/null +++ b/OsmAnd/res/layout/download_search_items_fragment.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/download_search_items_list.xml b/OsmAnd/res/layout/download_search_items_list.xml new file mode 100644 index 0000000000..7af692c926 --- /dev/null +++ b/OsmAnd/res/layout/download_search_items_list.xml @@ -0,0 +1,11 @@ + + + diff --git a/OsmAnd/src/net/osmand/plus/WorldRegion.java b/OsmAnd/src/net/osmand/plus/WorldRegion.java index b39d6aa8b1..78f1af9d1a 100644 --- a/OsmAnd/src/net/osmand/plus/WorldRegion.java +++ b/OsmAnd/src/net/osmand/plus/WorldRegion.java @@ -107,7 +107,7 @@ public class WorldRegion implements Serializable { public void initWorld() { regionId = ""; downloadsIdPrefix = "world_"; - name = null; + name = ""; superregion = null; } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 4e1a359cc5..2849564a03 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -12,6 +12,7 @@ import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentTransaction; import android.support.v4.app.ListFragment; import android.support.v4.view.ViewPager; import android.view.MenuItem; @@ -39,6 +40,7 @@ import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.activities.TabActivity; import net.osmand.plus.base.BasicProgressAsyncTask; import net.osmand.plus.download.items.RegionDialogFragment; +import net.osmand.plus.download.items.SearchDialogFragment; import net.osmand.plus.download.items.WorldItemsFragment; import net.osmand.plus.srtmplugin.SRTMPlugin; import net.osmand.plus.views.controls.PagerSlidingTabStrip; @@ -282,6 +284,10 @@ public class DownloadActivity extends BaseDownloadActivity implements RegionDial if (f.isAdded()) { ((WorldItemsFragment) f).onCategorizationFinished(); } + } else if (f instanceof SearchDialogFragment) { + if (f.isAdded()) { + ((SearchDialogFragment) f).onCategorizationFinished(); + } } } } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java.orig b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java.orig new file mode 100644 index 0000000000..cdd9609cc0 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java.orig @@ -0,0 +1,745 @@ +package net.osmand.plus.download; + +import android.app.AlertDialog; +import android.app.AlertDialog.Builder; +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.Bundle; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.ListFragment; +import android.support.v4.view.ViewPager; +import android.view.MenuItem; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.BaseAdapter; +import android.widget.BaseExpandableListAdapter; +import android.widget.Button; +import android.widget.ExpandableListAdapter; +import android.widget.ListAdapter; +import android.widget.ProgressBar; +import android.widget.TextView; + +import net.osmand.IndexConstants; +import net.osmand.PlatformUtil; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandPlugin; +import net.osmand.plus.OsmandSettings; +import net.osmand.plus.R; +import net.osmand.plus.Version; +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.activities.TabActivity; +import net.osmand.plus.base.BasicProgressAsyncTask; +<<<<<<< HEAD +import net.osmand.plus.download.items.SearchDialogFragment; +======= +import net.osmand.plus.download.items.RegionDialogFragment; +>>>>>>> 123c270bc595f997b400d0de3681e6ab22f427cf +import net.osmand.plus.download.items.WorldItemsFragment; +import net.osmand.plus.srtmplugin.SRTMPlugin; +import net.osmand.plus.views.controls.PagerSlidingTabStrip; + +import org.apache.commons.logging.Log; + +import java.io.File; +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + + +public class DownloadActivity extends BaseDownloadActivity implements RegionDialogFragment.DialogDismissListener { + private List localIndexInfos = new ArrayList(); + + private String initialFilter = ""; + private boolean singleTab; + + List mTabs = new ArrayList(); + + public static final String FILTER_KEY = "filter"; + public static final String FILTER_CAT = "filter_cat"; + + public static final String TAB_TO_OPEN = "Tab_to_open"; + public static final String LOCAL_TAB = "local"; + public static final String DOWNLOAD_TAB = "download"; + public static final String UPDATES_TAB = "updates"; + public static final String SINGLE_TAB = "SINGLE_TAB"; + private List downloadTypes = new ArrayList(); + private BannerAndDownloadFreeVersion visibleBanner; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + getMyApplication().applyTheme(this); + super.onCreate(savedInstanceState); + updateDownloads(); + + setContentView(R.layout.download); + singleTab = getIntent() != null && getIntent().getBooleanExtra(SINGLE_TAB, false); + int currentTab = 0; + String tab = getIntent() == null || getIntent().getExtras() == null ? null : getIntent().getExtras().getString(TAB_TO_OPEN); + if (tab != null) { + if (tab.equals(DOWNLOAD_TAB)) { + currentTab = 1; + } else if (tab.equals(UPDATES_TAB)) { + currentTab = 2; + } + } +// if (singleTab) { +// ViewPager viewPager = (ViewPager) findViewById(R.id.pager); +// viewPager.setVisibility(View.GONE); +// Fragment f = currentTab == 0 ? new LocalIndexesFragment() : +// (currentTab == 1? new DownloadIndexFragment() : new UpdatesIndexFragment()); +// String tag = currentTab == 0 ? LOCAL_TAB : +// (currentTab == 1 ? DOWNLOAD_TAB : UPDATES_TAB); +// findViewById(R.id.layout).setVisibility(View.VISIBLE); +// android.support.v4.app.FragmentManager manager = getSupportFragmentManager(); +// if (manager.findFragmentByTag(tag) == null){ +// getSupportFragmentManager().beginTransaction().add(R.id.layout, f, tag).commit(); +// } +// } else { + ViewPager viewPager = (ViewPager) findViewById(R.id.pager); + PagerSlidingTabStrip mSlidingTabLayout = (PagerSlidingTabStrip) findViewById(R.id.sliding_tabs); + + mTabs.add(new TabActivity.TabItem(R.string.download_tab_local, + getString(R.string.download_tab_local), LocalIndexesFragment.class)); + mTabs.add(new TabActivity.TabItem(R.string.download_tab_downloads, + getString(R.string.download_tab_downloads), DownloadIndexFragment.class)); + mTabs.add(new TabActivity.TabItem(R.string.download_tab_updates, + getString(R.string.download_tab_updates), UpdatesIndexFragment.class)); + +// mTabs.add(new TabActivity.TabItem(R.string.download_tab_local, +// getString(R.string.download_tab_local), NewLocalIndexesFragment.class)); + mTabs.add(new TabActivity.TabItem(R.string.download_tab_downloads, + getString(R.string.download_tab_downloads), WorldItemsFragment.class)); + + viewPager.setAdapter(new TabActivity.OsmandFragmentPagerAdapter(getSupportFragmentManager(), mTabs)); + mSlidingTabLayout.setViewPager(viewPager); + + viewPager.setCurrentItem(currentTab); +// } + + settings = ((OsmandApplication) getApplication()).getSettings(); + + findViewById(R.id.downloadButton).setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View v) { + downloadFilesCheckFreeVersion(); + } + + }); + findViewById(R.id.wikiButton).setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View v) { + downloadWikiFiles(); + } + }); + + findViewById(R.id.CancelAll).setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View v) { + getEntriesToDownload().clear(); + updateDownloadButton(); + for (WeakReference ref : fragSet) { + Fragment f = ref.get(); + if (f != null) { + if (f instanceof OsmAndListFragment) { + if (f.isAdded() && ((OsmAndListFragment) f).getListAdapter() instanceof ArrayAdapter) { + ((ArrayAdapter) ((OsmAndListFragment) f).getListAdapter()).notifyDataSetChanged(); + } + } else if (f.isAdded() && f instanceof OsmandExpandableListFragment && + ((OsmandExpandableListFragment) f).getAdapter() instanceof BaseExpandableListAdapter) { + ((BaseExpandableListAdapter) ((OsmandExpandableListFragment) f).getAdapter()).notifyDataSetChanged(); + } + } + } + } + }); + + downloadTypes = createDownloadTypes(); + final Intent intent = getIntent(); + if (intent != null && intent.getExtras() != null) { + final String filter = intent.getExtras().getString(FILTER_KEY); + if (filter != null) { + initialFilter = filter; + } + + final String filterCat = intent.getExtras().getString(FILTER_CAT); + if (filterCat != null) { + DownloadActivityType type = DownloadActivityType.getIndexType(filterCat.toLowerCase()); + if (type != null) { + downloadTypes.remove(type); + downloadTypes.add(0, type); + } + } + } + changeType(downloadTypes.get(0)); + } + + + public Map getIndexActivatedFileNames() { + return downloadListIndexThread != null ? downloadListIndexThread.getIndexActivatedFileNames() : null; + } + + public String getInitialFilter() { + return initialFilter; + } + + @Override + protected void onResume() { + super.onResume(); + getMyApplication().getAppCustomization().resumeActivity(DownloadActivity.class, this); + initFreeVersionBanner(findViewById(R.id.mainLayout)); + } + + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + int itemId = item.getItemId(); + switch (itemId) { + case android.R.id.home: + finish(); + return true; + + } + return false; + } + + public void setLocalIndexInfos(List list) { + this.localIndexInfos = list; + } + + public List getLocalIndexInfos() { + return localIndexInfos; + } + + public void changeType(final DownloadActivityType tp) { + //invalidateOptionsMenu(); + if (downloadListIndexThread != null && type != tp) { + type = tp; + downloadListIndexThread.runCategorization(tp); + } + } + + @Override + public void onPause() { + super.onPause(); + getMyApplication().getAppCustomization().pauseActivity(DownloadActivity.class); + } + + @Override + public void updateProgress(boolean updateOnlyProgress) { + BasicProgressAsyncTask basicProgressAsyncTask = + DownloadActivity.downloadListIndexThread.getCurrentRunningTask(); + if (visibleBanner != null) { + final int countedDownloads = DownloadActivity.downloadListIndexThread.getCountedDownloads(); + visibleBanner.updateProgress(countedDownloads, basicProgressAsyncTask); + } + if (!updateOnlyProgress) { + updateDownloadButton(); + } + } + + @Override + public void updateDownloadList(List list) { + for (WeakReference ref : fragSet) { + Fragment f = ref.get(); + if (f instanceof UpdatesIndexFragment) { + if (f.isAdded()) { + ((UpdatesIndexFragment) f).updateItemsList(list); + } + } + } + } + + @Override + public void categorizationFinished(List filtered, List cats) { + for (WeakReference ref : fragSet) { + Fragment f = ref.get(); + if (f instanceof DownloadIndexFragment) { + if (f.isAdded()) { + ((DownloadIndexFragment) f).categorizationFinished(filtered, cats); + } + } + } + } + + @Override + public void onCategorizationFinished() { + for (WeakReference ref : fragSet) { + Fragment f = ref.get(); + if (f instanceof WorldItemsFragment) { + if (f.isAdded()) { + ((WorldItemsFragment) f).onCategorizationFinished(); + } + } else if (f instanceof SearchDialogFragment) { + if (f.isAdded()) { + ((SearchDialogFragment) f).onCategorizationFinished(); + } + } + } + } + + public void downloadListUpdated() { + for (WeakReference ref : fragSet) { + Fragment f = ref.get(); + if (f instanceof DownloadIndexFragment) { + if (f.isAdded()) { + ((DownloadIndexAdapter) ((DownloadIndexFragment) f).getExpandableListAdapter()) + .notifyDataSetInvalidated(); + } + } + } + } + + @Override + public boolean startDownload(IndexItem item) { + final boolean b = super.startDownload(item); + visibleBanner.initFreeVersionBanner(); + return b; + } + + @Override + public void downloadedIndexes() { + for (WeakReference ref : fragSet) { + Fragment f = ref.get(); + if (f instanceof LocalIndexesFragment) { + if (f.isAdded()) { + ((LocalIndexesFragment) f).reloadData(); + } + } else if (f instanceof DownloadIndexFragment) { + if (f.isAdded()) { + DownloadIndexAdapter adapter = ((DownloadIndexAdapter) + ((DownloadIndexFragment) f).getExpandableListAdapter()); + if (adapter != null) { + adapter.setLoadedFiles(getIndexActivatedFileNames(), getIndexFileNames()); + + } + } + } + } + + } + + protected void downloadWikiFiles() { + if (Version.isFreeVersion(getMyApplication())) { + new InstallPaidVersionDialogFragment().show(getSupportFragmentManager(), + InstallPaidVersionDialogFragment.TAG); + } else { + Builder bld = new AlertDialog.Builder(this); + final List wi = getWikipediaItems(); + long size = 0; + for (IndexItem i : wi) { + size += i.getSize(); + } + bld.setMessage(getString(R.string.download_wikipedia_files, (size >> 20))); + bld.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + for (IndexItem i : wi) { + addToDownload(i); + } + updateDownloadButton(); + checkOldWikiFiles(); + } + }); + bld.setNegativeButton(R.string.shared_string_cancel, null); + if (wi.size() > 0) { + bld.show(); + } + } + } + + protected void checkOldWikiFiles() { + Map fileNames = getMyApplication().getResourceManager().getIndexFileNames(); + final Set wiki = new HashSet(); + for (String s : fileNames.keySet()) { + if (s.contains("_wiki")) { + wiki.add(s); + } + } + if (wiki.size() > 0) { + Builder bld = new AlertDialog.Builder(this); + bld.setMessage(R.string.archive_wikipedia_data); + bld.setNegativeButton(R.string.shared_string_cancel, null); + bld.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + for (String w : wiki) { + File fl = getMyApplication().getAppPath(w); + File nf = new File(fl.getParentFile(), IndexConstants.BACKUP_INDEX_DIR + "/" + fl.getName()); + boolean res = fl.renameTo(nf); + if (!res) { + System.err.println("Renaming from " + fl.getAbsolutePath() + " to " + nf.getAbsolutePath() + " failed"); + } + } + } + }); + bld.show(); + } + } + + + @Override + public void updateDownloadButton() { +// View view = getView(); +// if (view == null || getExpandableListView() == null){ +// return; +// } +// int x = getExpandableListView().getScrollX(); +// int y = getExpandableListView().getScrollY(); + if (getEntriesToDownload().isEmpty()) { + findViewById(R.id.DownloadLayout).setVisibility(View.GONE); + } else { + BasicProgressAsyncTask task = DownloadActivity.downloadListIndexThread.getCurrentRunningTask(); + boolean running = task instanceof DownloadIndexesThread.DownloadIndexesAsyncTask; + ((Button) findViewById(R.id.downloadButton)).setEnabled(!running); + String text; + int downloads = DownloadActivity.downloadListIndexThread.getDownloads(); + if (!running) { + text = getString(R.string.shared_string_download) + " (" + downloads + ")"; //$NON-NLS-1$ + } else { + text = getString(R.string.shared_string_downloading) + " (" + downloads + ")"; //$NON-NLS-1$ + } + findViewById(R.id.DownloadLayout).setVisibility(View.VISIBLE); + if (Version.isFreeVersion(getMyApplication())) { + int left = DownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS - settings.NUMBER_OF_FREE_DOWNLOADS.get() - downloads; + boolean excessLimit = left < 0; + if (left < 0) + left = 0; + if (getDownloadType() == DownloadActivityType.NORMAL_FILE || getDownloadType() == DownloadActivityType.ROADS_FILE) { + text += " (" + (excessLimit ? "! " : "") + getString(R.string.files_limit, left).toLowerCase() + ")"; + } + } + ((Button) findViewById(R.id.downloadButton)).setText(text); + List wikipediaItems = getWikipediaItems(); + findViewById(R.id.wikiButton).setVisibility(wikipediaItems.size() == 0 ? View.GONE : View.VISIBLE); + } + + for (WeakReference ref : fragSet) { + Fragment f = ref.get(); + if (f != null) + if (f.isAdded()) { + if (f instanceof OsmandExpandableListFragment) { + ExpandableListAdapter ad = ((OsmandExpandableListFragment) f).getExpandableListView() + .getExpandableListAdapter(); + if (ad instanceof OsmandBaseExpandableListAdapter) { + ((OsmandBaseExpandableListAdapter) ad).notifyDataSetChanged(); + } + } else if (f instanceof ListFragment) { + ListAdapter la = ((ListFragment) f).getListAdapter(); + if (la instanceof BaseAdapter) { + ((BaseAdapter) la).notifyDataSetChanged(); + } + } + } + } +// if (scroll) { +// getExpandableListView().scrollTo(x, y); +// } + } + + + private List getWikipediaItems() { + Set wikipediaItems = new HashSet(); + Map indexed = getMyApplication().getResourceManager().getIndexFileNames(); + for (IndexItem i : getEntriesToDownload().keySet()) { + if (i.getType() == DownloadActivityType.NORMAL_FILE) { + boolean fit = true; + fit = fit && i.getFileName().contains("obf"); + fit = fit && !i.getFileName().contains("world"); + String fname = i.getBasename(); + if (fit && !indexed.containsKey(fname + ".wiki.obf")) { + wikipediaItems.add(fname); + } + } + } + for (IndexItem i : getEntriesToDownload().keySet()) { + if (i.getType() == DownloadActivityType.WIKIPEDIA_FILE) { + wikipediaItems.remove(i.getBasename()); + } + } + List res = new ArrayList(); + IndexFileList list = downloadListIndexThread.getIndexFiles(); + if (list != null) { + List indexFiles = list.getIndexFiles(); + for (IndexItem i : indexFiles) { + if (i.getType() == DownloadActivityType.WIKIPEDIA_FILE && + wikipediaItems.contains(i.getBasename())) { + res.add(i); + } + } + } + return res; + } + + + public List getDownloadTypes() { + return downloadTypes; + } + + public List createDownloadTypes() { + List items = new ArrayList(); + items.add(DownloadActivityType.NORMAL_FILE); + if (!Version.isFreeVersion(getMyApplication())) { + items.add(DownloadActivityType.WIKIPEDIA_FILE); + } + items.add(DownloadActivityType.VOICE_FILE); + items.add(DownloadActivityType.ROADS_FILE); + if (OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) != null) { + items.add(DownloadActivityType.HILLSHADE_FILE); + items.add(DownloadActivityType.SRTM_COUNTRY_FILE); + } + + getMyApplication().getAppCustomization().getDownloadTypes(items); + return items; + } + + public boolean isLightActionBar() { + return ((OsmandApplication) getApplication()).getSettings().isLightActionBar(); + } + + + public Map getIndexFileNames() { + return downloadListIndexThread != null ? downloadListIndexThread.getIndexFileNames() : null; + } + + public List getIndexFiles() { + return downloadListIndexThread != null ? downloadListIndexThread.getCachedIndexFiles() : null; + } + + public void showDialogToDownloadMaps(Collection maps) { + int count = 0; + int sz = 0; + String s = ""; + for (IndexItem i : DownloadActivity.downloadListIndexThread.getCachedIndexFiles()) { + for (String map : maps) { + if ((i.getFileName().equals(map + ".obf.zip") || i.getFileName().equals(map + "_" + IndexConstants.BINARY_MAP_VERSION + ".obf.zip")) + && i.getType() == DownloadActivityType.NORMAL_FILE) { + final List de = i.createDownloadEntry(getMyApplication(), i.getType(), new ArrayList(1)); + for (DownloadEntry d : de) { + count++; + sz += d.sizeMB; + } + if (s.length() > 0) { + s += ", "; + } + s += i.getVisibleName(getMyApplication(), getMyApplication().getResourceManager().getOsmandRegions()); + getEntriesToDownload().put(i, de); + } + } + } + if (count > 0) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setMessage(getString(R.string.download_additional_maps, s, sz)); + builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + downloadFilesCheckInternet(); + } + }); + builder.setNegativeButton(R.string.shared_string_no, new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + getEntriesToDownload().clear(); + } + }); + builder.setOnCancelListener(new DialogInterface.OnCancelListener() { + + @Override + public void onCancel(DialogInterface dialog) { + getEntriesToDownload().clear(); + } + }); + builder.show(); + + } + } + + + public void initFreeVersionBanner(View header) { + visibleBanner = new BannerAndDownloadFreeVersion(header, this); + updateProgress(true); + } + + + public void showDialog(FragmentActivity activity, DialogFragment fragment) { + fragment.show(activity.getSupportFragmentManager(), "dialog"); + } + + @Override + public void onDialogDismissed() { + initFreeVersionBanner(findViewById(R.id.mainLayout)); + } + + private static class ToggleCollapseFreeVersionBanner implements View.OnClickListener { + private final View freeVersionDescriptionTextView; + private final View buttonsLinearLayout; + + private ToggleCollapseFreeVersionBanner(View freeVersionDescriptionTextView, + View buttonsLinearLayout) { + this.freeVersionDescriptionTextView = freeVersionDescriptionTextView; + this.buttonsLinearLayout = buttonsLinearLayout; + } + + @Override + public void onClick(View v) { + if (freeVersionDescriptionTextView.getVisibility() == View.VISIBLE) { + freeVersionDescriptionTextView.setVisibility(View.GONE); + buttonsLinearLayout.setVisibility(View.GONE); + } else { + freeVersionDescriptionTextView.setVisibility(View.VISIBLE); + buttonsLinearLayout.setVisibility(View.VISIBLE); + } + } + } + + public static class BannerAndDownloadFreeVersion { + private final View freeVersionBanner; + private final View downloadProgressLayout; + private final ProgressBar progressBar; + private final TextView leftTextView; + private final TextView rightTextView; + private final ProgressBar downloadsLeftProgressBar; + private final View buttonsLinearLayout; + private final TextView freeVersionDescriptionTextView; + private final TextView downloadsLeftTextView; + private final View laterButton; + + private final Context ctx; + private final OsmandApplication application; + private final boolean shouldShowFreeVersionBanner; + private final View freeVersionBannerTitle; + + public BannerAndDownloadFreeVersion(View view, Context ctx) { + this.ctx = ctx; + application = (OsmandApplication) ctx.getApplicationContext(); + freeVersionBanner = view.findViewById(R.id.freeVersionBanner); + downloadProgressLayout = view.findViewById(R.id.downloadProgressLayout); + progressBar = (ProgressBar) view.findViewById(R.id.progressBar); + leftTextView = (TextView) view.findViewById(R.id.leftTextView); + rightTextView = (TextView) view.findViewById(R.id.rightTextView); + downloadsLeftTextView = (TextView) freeVersionBanner.findViewById(R.id.downloadsLeftTextView); + downloadsLeftProgressBar = (ProgressBar) freeVersionBanner.findViewById(R.id.downloadsLeftProgressBar); + buttonsLinearLayout = freeVersionBanner.findViewById(R.id.buttonsLinearLayout); + freeVersionDescriptionTextView = (TextView) freeVersionBanner + .findViewById(R.id.freeVersionDescriptionTextView); + laterButton = freeVersionBanner.findViewById(R.id.laterButton); + freeVersionBannerTitle = freeVersionBanner.findViewById(R.id.freeVersionBannerTitle); + + shouldShowFreeVersionBanner = Version.isFreeVersion(application) + || application.getSettings().SHOULD_SHOW_FREE_VERSION_BANNER.get(); + + initFreeVersionBanner(); + updateFreeVersionBanner(); + } + + public void updateProgress(int countedDownloads, + BasicProgressAsyncTask basicProgressAsyncTask) { + final boolean isFinished = basicProgressAsyncTask == null + || basicProgressAsyncTask.getStatus() == AsyncTask.Status.FINISHED; + if (isFinished) { + downloadProgressLayout.setVisibility(View.GONE); + updateFreeVersionBanner(); + } else { + boolean indeterminate = basicProgressAsyncTask.isIndeterminate(); + String message = basicProgressAsyncTask.getDescription(); + int percent = basicProgressAsyncTask.getProgressPercentage(); + + setMinimizedFreeVersionBanner(true); + updateAvailableDownloads(countedDownloads); + downloadProgressLayout.setVisibility(View.VISIBLE); + progressBar.setIndeterminate(indeterminate); + if (indeterminate) { + leftTextView.setText(message); + } else { + // TODO if only 1 map, show map name + progressBar.setProgress(percent); +// final String format = ctx.getString(R.string.downloading_number_of_files); + leftTextView.setText(message); + rightTextView.setText(percent + "%"); + } + } + + } + + private void initFreeVersionBanner() { + if (!shouldShowFreeVersionBanner) { + freeVersionBanner.setVisibility(View.GONE); + return; + } + freeVersionBanner.setVisibility(View.VISIBLE); + downloadsLeftProgressBar.setMax(BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS); + freeVersionDescriptionTextView.setText(ctx.getString(R.string.free_version_message, + BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS)); + freeVersionBanner.findViewById(R.id.getFullVersionButton).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + BaseDownloadActivity context = (BaseDownloadActivity) v.getContext(); + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Version.marketPrefix(context + .getMyApplication()) + "net.osmand.plus")); + try { + context.startActivity(intent); + } catch (ActivityNotFoundException e) { + } + } + }); + laterButton.setOnClickListener( + new ToggleCollapseFreeVersionBanner(freeVersionDescriptionTextView, buttonsLinearLayout)); + } + + private void updateFreeVersionBanner() { + if (!shouldShowFreeVersionBanner) return; + setMinimizedFreeVersionBanner(false); + OsmandSettings settings = application.getSettings(); + final Integer mapsDownloaded = settings.NUMBER_OF_FREE_DOWNLOADS.get(); + downloadsLeftProgressBar.setProgress(mapsDownloaded); + int downloadsLeft = BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS + - mapsDownloaded; + downloadsLeft = Math.max(downloadsLeft, 0); + if (downloadsLeft <= 0) { + laterButton.setVisibility(View.GONE); + } + downloadsLeftTextView.setText(ctx.getString(R.string.downloads_left_template, downloadsLeft)); + // TODO review logic + freeVersionBanner.setOnClickListener(new ToggleCollapseFreeVersionBanner(freeVersionDescriptionTextView, + buttonsLinearLayout)); + } + + private void updateAvailableDownloads(int activeTasks) { + OsmandSettings settings = application.getSettings(); + final Integer mapsDownloaded = settings.NUMBER_OF_FREE_DOWNLOADS.get() + activeTasks; + downloadsLeftProgressBar.setProgress(mapsDownloaded); + } + + private void setMinimizedFreeVersionBanner(boolean minimize) { + if (minimize) { + freeVersionDescriptionTextView.setVisibility(View.GONE); + buttonsLinearLayout.setVisibility(View.GONE); + freeVersionBannerTitle.setVisibility(View.GONE); + } else { + freeVersionDescriptionTextView.setVisibility(View.VISIBLE); + buttonsLinearLayout.setVisibility(View.VISIBLE); + freeVersionBannerTitle.setVisibility(View.VISIBLE); + } + } + } + +} diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index 838a3a5297..99cb1afc17 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -186,54 +186,10 @@ public class DownloadIndexesThread { voiceRecItems.clear(); voiceTTSItems.clear(); - List mergedRegions = app.getWorldRegion().getFlattenedSubregions(); - mergedRegions.add(app.getWorldRegion()); - boolean voiceFilesProcessed = false; - for (WorldRegion region : mergedRegions) { - String downloadsIdPrefix = region.getDownloadsIdPrefix(); - - Map regionResources = new HashMap<>(); - - Set typesSet = new TreeSet<>(new Comparator() { - @Override - public int compare(DownloadActivityType dat1, DownloadActivityType dat2) { - return dat1.getTag().compareTo(dat2.getTag()); - } - }); - - for (IndexItem resource : resourcesInRepository) { - - if (!voiceFilesProcessed) { - if (resource.getSimplifiedFileName().endsWith(".voice.zip")) { - voiceRecItems.add(resource); - continue; - } else if (resource.getSimplifiedFileName().contains(".ttsvoice.zip")) { - voiceTTSItems.add(resource); - continue; - } - } - - if (!resource.getSimplifiedFileName().startsWith(downloadsIdPrefix)) { - continue; - } - - typesSet.add(resource.getType()); - regionResources.put(resource.getSimplifiedFileName(), resource); - } - - voiceFilesProcessed = true; - - if (region.getSuperregion() != null && region.getSuperregion().getSuperregion() != app.getWorldRegion()) { - if (region.getSuperregion().getResourceTypes() == null) { - region.getSuperregion().setResourceTypes(typesSet); - } else { - region.getSuperregion().getResourceTypes().addAll(typesSet); - } - } - - region.setResourceTypes(typesSet); - resourcesByRegions.put(region, regionResources); + for (WorldRegion region : app.getWorldRegion().getFlattenedSubregions()) { + processRegion(resourcesInRepository, false, region); } + processRegion(resourcesInRepository, true, app.getWorldRegion()); final Collator collator = OsmAndCollator.primaryCollator(); final OsmandRegions osmandRegions = app.getRegions(); @@ -257,6 +213,50 @@ public class DownloadIndexesThread { return true; } + private void processRegion(List resourcesInRepository, boolean processVoiceFiles, WorldRegion region) { + String downloadsIdPrefix = region.getDownloadsIdPrefix(); + + Map regionResources = new HashMap<>(); + + Set typesSet = new TreeSet<>(new Comparator() { + @Override + public int compare(DownloadActivityType dat1, DownloadActivityType dat2) { + return dat1.getTag().compareTo(dat2.getTag()); + } + }); + + for (IndexItem resource : resourcesInRepository) { + + if (processVoiceFiles) { + if (resource.getSimplifiedFileName().endsWith(".voice.zip")) { + voiceRecItems.add(resource); + continue; + } else if (resource.getSimplifiedFileName().contains(".ttsvoice.zip")) { + voiceTTSItems.add(resource); + continue; + } + } + + if (!resource.getSimplifiedFileName().startsWith(downloadsIdPrefix)) { + continue; + } + + typesSet.add(resource.getType()); + regionResources.put(resource.getSimplifiedFileName(), resource); + } + + if (region.getSuperregion() != null && region.getSuperregion().getSuperregion() != app.getWorldRegion()) { + if (region.getSuperregion().getResourceTypes() == null) { + region.getSuperregion().setResourceTypes(typesSet); + } else { + region.getSuperregion().getResourceTypes().addAll(typesSet); + } + } + + region.setResourceTypes(typesSet); + resourcesByRegions.put(region, regionResources); + } + public class DownloadIndexesAsyncTask extends BasicProgressAsyncTask implements DownloadFileShowWarning { private OsmandPreference downloads; diff --git a/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java index 91446e3094..695e6989e9 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java @@ -25,7 +25,7 @@ public class RegionDialogFragment extends DialogFragment{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - boolean isLightTheme = ((OsmandApplication) getActivity().getApplication()) + boolean isLightTheme = getMyApplication() .getSettings().OSMAND_THEME.get() == OsmandSettings.OSMAND_LIGHT_THEME; int themeId = isLightTheme ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme; setStyle(STYLE_NO_FRAME, themeId); @@ -85,6 +85,10 @@ public class RegionDialogFragment extends DialogFragment{ return (OsmandApplication) getActivity().getApplication(); } + private DownloadActivity getDownloadActivity() { + return (DownloadActivity) getActivity(); + } + public void onRegionSelected(String regionId) { final RegionDialogFragment regionDialogFragment = createInstance(regionId); regionDialogFragment.setOnDismissListener(listener); diff --git a/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java.orig b/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java.orig new file mode 100644 index 0000000000..6ae8693a22 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java.orig @@ -0,0 +1,135 @@ +package net.osmand.plus.download.items; + +<<<<<<< HEAD +======= +import android.content.DialogInterface; +>>>>>>> 123c270bc595f997b400d0de3681e6ab22f427cf +import android.os.Bundle; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.Fragment; +import android.support.v7.widget.Toolbar; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandSettings; +import net.osmand.plus.R; +import net.osmand.plus.WorldRegion; +import net.osmand.plus.download.DownloadActivity; + +<<<<<<< HEAD +public class RegionDialogFragment extends DialogFragment { +======= +public class RegionDialogFragment extends DialogFragment{ +>>>>>>> 123c270bc595f997b400d0de3681e6ab22f427cf + public static final String TAG = "RegionDialogFragment"; + private static final String REGION_ID_DLG_KEY = "world_region_dialog_key"; + private String regionId; + private DialogDismissListener dialogDismissListener; + private DialogDismissListener listener; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + boolean isLightTheme = getMyApplication() + .getSettings().OSMAND_THEME.get() == OsmandSettings.OSMAND_LIGHT_THEME; + int themeId = isLightTheme ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme; + setStyle(STYLE_NO_FRAME, themeId); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + final View view = inflater.inflate(R.layout.maps_in_category_fragment, container, false); + + if (savedInstanceState != null) { + regionId = savedInstanceState.getString(REGION_ID_DLG_KEY); + } + if (regionId == null) { + regionId = getArguments().getString(REGION_ID_DLG_KEY); + } + if (regionId == null) + regionId = ""; + + Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar); + toolbar.setNavigationIcon(getMyApplication().getIconsCache().getIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha)); + toolbar.setNavigationOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + dismiss(); + } + }); + + if (regionId.length() > 0) { + Fragment fragment = getChildFragmentManager().findFragmentById(R.id.fragmentContainer); + if (fragment == null) { + getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer, + RegionItemsFragment.createInstance(regionId)).commit(); + } + WorldRegion region = getMyApplication().getWorldRegion().getRegionById(regionId); + if (region != null) { + toolbar.setTitle(region.getName()); + } + } +<<<<<<< HEAD + getDownloadActivity().initFreeVersionBanner(view); +======= + ((DownloadActivity) getActivity()).initFreeVersionBanner(view); + listener = new DialogDismissListener() { + @Override + public void onDialogDismissed() { + ((DownloadActivity) getActivity()).initFreeVersionBanner(view); + } + }; +>>>>>>> 123c270bc595f997b400d0de3681e6ab22f427cf + return view; + } + + @Override + public void onSaveInstanceState(Bundle outState) { + outState.putString(REGION_ID_DLG_KEY, regionId); + super.onSaveInstanceState(outState); + } + + private OsmandApplication getMyApplication() { + return (OsmandApplication) getActivity().getApplication(); + } + + private DownloadActivity getDownloadActivity() { + return (DownloadActivity) getActivity(); + } + + public void onRegionSelected(String regionId) { +<<<<<<< HEAD + getDownloadActivity().showDialog(getActivity(), createInstance(regionId)); +======= + final RegionDialogFragment regionDialogFragment = createInstance(regionId); + regionDialogFragment.setOnDismissListener(listener); + ((DownloadActivity) getActivity()).showDialog(getActivity(), regionDialogFragment); +>>>>>>> 123c270bc595f997b400d0de3681e6ab22f427cf + } + + public static RegionDialogFragment createInstance(String regionId) { + Bundle bundle = new Bundle(); + bundle.putString(REGION_ID_DLG_KEY, regionId); + RegionDialogFragment fragment = new RegionDialogFragment(); + fragment.setArguments(bundle); + return fragment; + } + + @Override + public void onDismiss(DialogInterface dialog) { + super.onDismiss(dialog); + if (dialogDismissListener != null) + dialogDismissListener.onDialogDismissed(); + } + + public void setOnDismissListener(DialogDismissListener listener) { + this.dialogDismissListener = listener; + } + + public interface DialogDismissListener { + void onDialogDismissed(); + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/items/SearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/items/SearchDialogFragment.java index 99ea08cba6..9eb9554ed4 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/SearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/SearchDialogFragment.java @@ -5,8 +5,13 @@ import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.RelativeLayout; +import android.widget.SearchView; +import android.widget.RelativeLayout.LayoutParams; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; @@ -18,12 +23,12 @@ public class SearchDialogFragment extends DialogFragment { public static final String TAG = "SearchDialogFragment"; private static final String SEARCH_TEXT_DLG_KEY = "search_text_dlg_key"; private String searchText; + SearchView search; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - boolean isLightTheme = ((OsmandApplication) getActivity().getApplication()) - .getSettings().OSMAND_THEME.get() == OsmandSettings.OSMAND_LIGHT_THEME; + boolean isLightTheme = getMyApplication().getSettings().OSMAND_THEME.get() == OsmandSettings.OSMAND_LIGHT_THEME; int themeId = isLightTheme ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme; setStyle(STYLE_NO_FRAME, themeId); } @@ -51,13 +56,44 @@ public class SearchDialogFragment extends DialogFragment { } }); + search = new SearchView(getActivity()); + RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( + LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); + params.setMargins(0, 0, 0, 0); + search.setQueryHint("Enter map name"); + search.setLayoutParams(params); + toolbar.addView(search); + + search.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() { + @Override + public void onFocusChange(View v, boolean hasFocus) { + } + }); + + search.setOnQueryTextListener(new SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + return false; + } + + @Override + public boolean onQueryTextChange(String newText) { + Fragment f = getChildFragmentManager().findFragmentByTag(SearchItemsFragment.TAG); + if (f != null) { + ((SearchItemsFragment) f).updateSearchText(newText); + return true; + } + return false; + } + }); + Fragment fragment = getChildFragmentManager().findFragmentById(R.id.fragmentContainer); if (fragment == null) { getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer, - SearchItemsFragment.createInstance(searchText)).commit(); + SearchItemsFragment.createInstance(searchText), SearchItemsFragment.TAG).commit(); } - ((DownloadActivity) getActivity()).initFreeVersionBanner(view); + getDownloadActivity().initFreeVersionBanner(view); return view; } @@ -67,10 +103,28 @@ public class SearchDialogFragment extends DialogFragment { super.onSaveInstanceState(outState); } + @Override + public void onResume() { + super.onResume(); + //search.setIconifiedByDefault(false); + search.setIconified(false); + } + private OsmandApplication getMyApplication() { return (OsmandApplication) getActivity().getApplication(); } + private DownloadActivity getDownloadActivity() { + return (DownloadActivity) getActivity(); + } + + public void onCategorizationFinished() { + Fragment f = getChildFragmentManager().findFragmentByTag(SearchItemsFragment.TAG); + if (f != null) { + ((SearchItemsFragment) f).onCategorizationFinished(); + } + } + public static SearchDialogFragment createInstance(String searchText) { Bundle bundle = new Bundle(); bundle.putString(SEARCH_TEXT_DLG_KEY, searchText); diff --git a/OsmAnd/src/net/osmand/plus/download/items/SearchItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/items/SearchItemsFragment.java index fb17e1f890..873bc1acd4 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/SearchItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/SearchItemsFragment.java @@ -13,6 +13,8 @@ import android.widget.Filter; import android.widget.Filterable; import android.widget.ListView; +import net.osmand.Collator; +import net.osmand.OsmAndCollator; import net.osmand.PlatformUtil; import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; @@ -29,8 +31,11 @@ import net.osmand.plus.srtmplugin.SRTMPlugin; import org.apache.commons.logging.Log; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.LinkedList; import java.util.List; +import java.util.Objects; public class SearchItemsFragment extends Fragment { public static final String TAG = "SearchItemsFragment"; @@ -49,7 +54,7 @@ public class SearchItemsFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.download_items_fragment, container, false); + View view = inflater.inflate(R.layout.download_search_items_fragment, container, false); if (savedInstanceState != null) { searchText = savedInstanceState.getString(SEARCH_TEXT_KEY); @@ -73,7 +78,6 @@ public class SearchItemsFragment extends Fragment { }); fillSearchItemsAdapter(); - listAdapter.notifyDataSetChanged(); return view; } @@ -113,8 +117,25 @@ public class SearchItemsFragment extends Fragment { private void fillSearchItemsAdapter() { if (listAdapter != null) { listAdapter.clear(); - listAdapter.addWorldRegions(getMyApplication().getWorldRegion().getFlattenedSubregions()); - listAdapter.addIndexItems(getDownloadActivity().getIndexFiles()); + List flattenedList = getMyApplication().getWorldRegion().getFlattenedSubregions(); + List indexItems = getDownloadActivity().getIndexFiles(); + if (flattenedList != null && flattenedList.size() > 0 && + indexItems != null && indexItems.size() > 0) { + listAdapter.addWorldRegions(flattenedList); + listAdapter.addIndexItems(indexItems); + listAdapter.notifyDataSetChanged(); + } + } + } + + public void onCategorizationFinished() { + fillSearchItemsAdapter(); + } + + public void updateSearchText(String searchText) { + this.searchText = searchText; + if(listAdapter != null){ + listAdapter.getFilter().filter(searchText); } } @@ -229,7 +250,7 @@ public class SearchItemsFragment extends Fragment { @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); - if (constraint == null || constraint.length() == 0) { + if (constraint == null || constraint.length() < 2) { results.values = new ArrayList<>(); results.count = 0; } else { @@ -251,22 +272,43 @@ public class SearchItemsFragment extends Fragment { Context c = getDownloadActivity(); for (WorldRegion region : worldRegions) { - String indexLC = region.getName(); + String indexLC = region.getName().toLowerCase(); if (isMatch(conds, false, indexLC)) { filter.add(region); } } for (IndexItem item : indexItems) { - String indexLC = osmandRegions.getDownloadNameIndexLowercase(item.getBasename()); - if (indexLC == null) { - indexLC = item.getVisibleName(c, osmandRegions).toLowerCase(); - } + String indexLC = item.getVisibleName(c, osmandRegions).toLowerCase(); if (isMatch(conds, false, indexLC)) { filter.add(item); } } + final Collator collator = OsmAndCollator.primaryCollator(); + Collections.sort(filter, new Comparator() { + @Override + public int compare(Object obj1, Object obj2) { + + String str1; + String str2; + + if (obj1 instanceof WorldRegion) { + str1 = ((WorldRegion) obj1).getName(); + } else { + str1 = ((IndexItem) obj1).getVisibleName(getMyApplication(), osmandRegions); + } + + if (obj2 instanceof WorldRegion) { + str2 = ((WorldRegion) obj2).getName(); + } else { + str2 = ((IndexItem) obj2).getVisibleName(getMyApplication(), osmandRegions); + } + + return collator.compare(str1, str2); + } + }); + results.values = filter; results.count = filter.size(); } @@ -296,6 +338,7 @@ public class SearchItemsFragment extends Fragment { @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { + items.clear(); List values = (List) results.values; if (values != null && !values.isEmpty()) { items.addAll(values); diff --git a/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java index d2163e8481..09587ba521 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java @@ -42,6 +42,7 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { private static final Log LOG = PlatformUtil.getLog(WorldItemsFragment.class); public static final int RELOAD_ID = 0; + public static final int SEARCH_ID = 1; private WorldItemsAdapter listAdapter; @@ -131,10 +132,10 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { return true; } else if (groupPosition == voicePromptsIndex) { if (childPosition == voicePromptsItemsRecordedSubIndex) { - ((DownloadActivity)getActivity()).showDialog(getActivity(), + getDownloadActivity().showDialog(getActivity(), VoiceDialogFragment.createInstance(VoicePromptsType.RECORDED)); } else { - ((DownloadActivity) getActivity()).showDialog(getActivity(), + getDownloadActivity().showDialog(getActivity(), VoiceDialogFragment.createInstance(VoicePromptsType.TTS)); } } @@ -143,19 +144,28 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { @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); + MenuItem itemReload = menu.add(0, RELOAD_ID, 0, R.string.shared_string_refresh); + itemReload.setIcon(R.drawable.ic_action_refresh_dark); + MenuItemCompat.setShowAsAction(itemReload, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); + + MenuItem itemSearch = menu.add(0, SEARCH_ID, 1, R.string.shared_string_search); + itemSearch.setIcon(R.drawable.ic_action_search_dark); + MenuItemCompat.setShowAsAction(itemSearch, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); } @Override public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == RELOAD_ID) { - // re-create the thread - DownloadActivity.downloadListIndexThread.runReloadIndexFiles(); - return true; + switch (item.getItemId()) { + case RELOAD_ID: + // re-create the thread + DownloadActivity.downloadListIndexThread.runReloadIndexFiles(); + return true; + case SEARCH_ID: + getDownloadActivity().showDialog(getActivity(), SearchDialogFragment.createInstance("")); + return true; + default: + return super.onOptionsItemSelected(item); } - return super.onOptionsItemSelected(item); } private DownloadActivity getDownloadActivity() { diff --git a/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java.orig b/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java.orig new file mode 100644 index 0000000000..7637360b27 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java.orig @@ -0,0 +1,366 @@ +package net.osmand.plus.download.items; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import net.osmand.PlatformUtil; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandPlugin; +import net.osmand.plus.R; +import net.osmand.plus.Version; +import net.osmand.plus.WorldRegion; +import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; +import net.osmand.plus.activities.OsmandExpandableListFragment; +import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.items.ItemsListBuilder.VoicePromptsType; +import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin; +import net.osmand.plus.srtmplugin.SRTMPlugin; + +import org.apache.commons.logging.Log; + +import android.content.Context; +import android.content.res.Resources; +import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.support.v4.view.MenuItemCompat; +import android.util.TypedValue; +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.ExpandableListView; +import android.widget.TextView; + +public class WorldItemsFragment extends OsmandExpandableListFragment { + public static final String TAG = "WorldItemsFragment"; + private static final Log LOG = PlatformUtil.getLog(WorldItemsFragment.class); + + public static final int RELOAD_ID = 0; + public static final int SEARCH_ID = 1; + + private WorldItemsAdapter listAdapter; + + private int worldRegionsIndex = -1; + private int worldMapsIndex = -1; + private int voicePromptsIndex = -1; + + private int voicePromptsItemsRecordedSubIndex = -1; + private int voicePromptsItemsTTSSubIndex = -1; + + @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.download_index_fragment, container, false); + + ExpandableListView listView = (ExpandableListView) view.findViewById(android.R.id.list); + listAdapter = new WorldItemsAdapter(getActivity()); + listView.setAdapter(listAdapter); + expandAllGroups(); + setListView(listView); + + onCategorizationFinished(); + +<<<<<<< HEAD + getDownloadActivity().initFreeVersionBanner(view); + +======= +>>>>>>> 123c270bc595f997b400d0de3681e6ab22f427cf + return view; + } + + @Override + public void onResume() { + super.onResume(); + + if (!listAdapter.isEmpty()) { + expandAllGroups(); + } + } + + private void expandAllGroups() { + for (int i = 0; i < listAdapter.getGroupCount(); i++) { + getExpandableListView().expandGroup(i); + } + } + + public OsmandApplication getMyApplication() { + return (OsmandApplication) getActivity().getApplication(); + } + + private void fillWorldItemsAdapter(ItemsListBuilder builder) { + if (listAdapter != null) { + listAdapter.clear(); + if (builder.getRegionMapItems().size() > 0) { + int unusedIndex = 0; + worldRegionsIndex = unusedIndex++; + listAdapter.add("World regions", builder.getRegionsFromAllItems()); + worldMapsIndex = unusedIndex++; + listAdapter.add("World maps", builder.getRegionMapItems()); + + int unusedSubIndex = 0; + List voicePromptsItems = new LinkedList<>(); + if (!builder.isVoicePromptsItemsEmpty(VoicePromptsType.RECORDED)) { + voicePromptsItems.add(builder.getVoicePromtName(VoicePromptsType.RECORDED)); + voicePromptsItemsRecordedSubIndex = unusedSubIndex++; + } + if (!builder.isVoicePromptsItemsEmpty(VoicePromptsType.TTS)) { + voicePromptsItems.add(builder.getVoicePromtName(VoicePromptsType.TTS)); + voicePromptsItemsTTSSubIndex = unusedSubIndex; + } + if (!voicePromptsItems.isEmpty()) { + voicePromptsIndex = unusedIndex; + listAdapter.add("Voice prompts", voicePromptsItems); + } + } + //listAdapter.add("Voice promts", null); + } + } + + @Override + public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { + if (groupPosition == worldRegionsIndex) { + WorldRegion region = (WorldRegion)listAdapter.getChild(groupPosition, childPosition); +<<<<<<< HEAD + getDownloadActivity().showDialog(getActivity(), RegionDialogFragment.createInstance(region.getRegionId())); +======= + final RegionDialogFragment regionDialogFragment = RegionDialogFragment.createInstance(region.getRegionId()); + regionDialogFragment.setOnDismissListener((DownloadActivity) getActivity()); + ((DownloadActivity)getActivity()).showDialog(getActivity(), regionDialogFragment); +>>>>>>> 123c270bc595f997b400d0de3681e6ab22f427cf + return true; + } else if (groupPosition == voicePromptsIndex) { + if (childPosition == voicePromptsItemsRecordedSubIndex) { + getDownloadActivity().showDialog(getActivity(), + VoiceDialogFragment.createInstance(VoicePromptsType.RECORDED)); + } else { + getDownloadActivity().showDialog(getActivity(), + VoiceDialogFragment.createInstance(VoicePromptsType.TTS)); + } + } + return false; + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + MenuItem itemReload = menu.add(0, RELOAD_ID, 0, R.string.shared_string_refresh); + itemReload.setIcon(R.drawable.ic_action_refresh_dark); + MenuItemCompat.setShowAsAction(itemReload, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); + + MenuItem itemSearch = menu.add(0, SEARCH_ID, 1, R.string.shared_string_search); + itemSearch.setIcon(R.drawable.ic_action_search_dark); + MenuItemCompat.setShowAsAction(itemSearch, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case RELOAD_ID: + // re-create the thread + DownloadActivity.downloadListIndexThread.runReloadIndexFiles(); + return true; + case SEARCH_ID: + getDownloadActivity().showDialog(getActivity(), SearchDialogFragment.createInstance("")); + return true; + default: + return super.onOptionsItemSelected(item); + } + } + + private DownloadActivity getDownloadActivity() { + return (DownloadActivity) getActivity(); + } + + public void onCategorizationFinished() { + ItemsListBuilder builder = getDownloadActivity().getItemsBuilder(); + if (builder != null && builder.build()) { + fillWorldItemsAdapter(builder); + listAdapter.notifyDataSetChanged(); + expandAllGroups(); + } + } + + private class WorldItemsAdapter extends OsmandBaseExpandableListAdapter { + + private Map> data = new LinkedHashMap<>(); + private List sections = new LinkedList<>(); + private boolean srtmDisabled; + private boolean nauticalPluginDisabled; + private boolean freeVersion; + + private class SimpleViewHolder { + TextView textView; + } + + public WorldItemsAdapter(Context ctx) { + srtmDisabled = OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) == null; + nauticalPluginDisabled = OsmandPlugin.getEnabledPlugin(NauticalMapsPlugin.class) == null; + freeVersion = Version.isFreeVersion(getMyApplication()); + TypedArray ta = ctx.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorPrimary}); + ta.recycle(); + } + + public void clear() { + data.clear(); + sections.clear(); + notifyDataSetChanged(); + } + + public void add(String section, List list) { + if (!sections.contains(section)) { + sections.add(section); + } + if (!data.containsKey(section)) { + data.put(section, new ArrayList<>()); + } + data.get(section).addAll(list); + } + + @Override + public Object getChild(int groupPosition, int childPosition) { + String section = sections.get(groupPosition); + return data.get(section).get(childPosition); + } + + @Override + public long getChildId(int groupPosition, int childPosition) { + return groupPosition * 10000 + childPosition; + } + + @Override + public int getChildType(int groupPosition, int childPosition) { + if (groupPosition == worldRegionsIndex || groupPosition == voicePromptsIndex) { + return 0; + } else { + return 1; + } + } + + @Override + public int getChildTypeCount() { + return 2; + } + + @Override + public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { + + final Object child = getChild(groupPosition, childPosition); + + if (groupPosition == worldRegionsIndex) { + WorldRegion item = (WorldRegion)child; + SimpleViewHolder viewHolder; + if (convertView == null) { + convertView = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.simple_list_menu_item, parent, false); + viewHolder = new SimpleViewHolder(); + viewHolder.textView = (TextView) convertView.findViewById(R.id.title); + convertView.setTag(viewHolder); + } else { + viewHolder = (SimpleViewHolder) convertView.getTag(); + } + Drawable iconLeft = getMyApplication().getIconsCache() + .getContentIcon(R.drawable.ic_world_globe_dark); + viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(iconLeft, null, null, null); + viewHolder.textView.setText(item.getName()); + + } else if (groupPosition == worldMapsIndex) { + ItemsListBuilder.ResourceItem item = (ItemsListBuilder.ResourceItem) child; + ItemViewHolder viewHolder; + if (convertView == null) { + convertView = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.two_line_with_images_list_item, parent, false); + viewHolder = new ItemViewHolder(convertView); + convertView.setTag(viewHolder); + } else { + viewHolder = (ItemViewHolder) convertView.getTag(); + } + viewHolder.setSrtmDisabled(srtmDisabled); + viewHolder.setNauticalPluginDisabled(nauticalPluginDisabled); + viewHolder.setFreeVersion(freeVersion); + viewHolder.bindIndexItem(item.getIndexItem(), getDownloadActivity(), false, false); + } else if (groupPosition == voicePromptsIndex) { + String item = (String)child; + SimpleViewHolder viewHolder; + if (convertView == null) { + convertView = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.simple_list_menu_item, parent, false); + viewHolder = new SimpleViewHolder(); + viewHolder.textView = (TextView) convertView.findViewById(R.id.title); + convertView.setTag(viewHolder); + } else { + viewHolder = (SimpleViewHolder) convertView.getTag(); + } + Drawable iconLeft = getMyApplication().getIconsCache() + .getContentIcon(R.drawable.ic_action_volume_up); + viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(iconLeft, null, null, null); + viewHolder.textView.setText(item); + } + + return convertView; + } + + @Override + public View getGroupView(int groupPosition, boolean isExpanded, final View convertView, + final ViewGroup parent) { + View v = convertView; + String section = getGroup(groupPosition); + if (v == null) { + LayoutInflater inflater = (LayoutInflater) getDownloadActivity() + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + v = inflater.inflate(R.layout.download_item_list_section, parent, false); + } + TextView nameView = ((TextView) v.findViewById(R.id.section_name)); + nameView.setText(section); + + v.setOnClickListener(null); + + TypedValue typedValue = new TypedValue(); + Resources.Theme theme = getActivity().getTheme(); + theme.resolveAttribute(R.attr.ctx_menu_info_view_bg, typedValue, true); + v.setBackgroundColor(typedValue.data); + + return v; + } + + @Override + public int getChildrenCount(int groupPosition) { + String section = sections.get(groupPosition); + return data.get(section).size(); + } + + @Override + public String getGroup(int groupPosition) { + return sections.get(groupPosition); + } + + @Override + public int getGroupCount() { + return sections.size(); + } + + @Override + public long getGroupId(int groupPosition) { + return groupPosition; + } + + @Override + public boolean hasStableIds() { + return false; + } + + @Override + public boolean isChildSelectable(int groupPosition, int childPosition) { + return true; + } + } +}