From b74b1f6d2131ee1fca00167a30de826a3100df5c Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Wed, 21 Oct 2015 15:54:10 +0200 Subject: [PATCH] Revert "Fix DownloadActivity NPE" This reverts commit 7987baadd9172a51b32aaf8e0af40cec24cb8eae. --- .../plus/download/DownloadActivity.java | 181 ++++++++++++++---- 1 file changed, 139 insertions(+), 42 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 8b692c9bf7..db6c299eb2 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -1,25 +1,5 @@ package net.osmand.plus.download; -import java.io.File; -import java.lang.ref.WeakReference; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; - -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.LocalIndexInfo; -import net.osmand.plus.activities.TabActivity; -import net.osmand.plus.base.BasicProgressAsyncTask; -import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; -import net.osmand.plus.download.ui.ActiveDownloadsDialogFragment; -import net.osmand.plus.download.ui.DownloadResourceGroupFragment; -import net.osmand.plus.download.ui.LocalIndexesFragment; -import net.osmand.plus.download.ui.UpdatesIndexFragment; -import net.osmand.plus.views.controls.PagerSlidingTabStrip; import android.content.ActivityNotFoundException; import android.content.Intent; import android.net.Uri; @@ -36,8 +16,35 @@ import android.view.MenuItem; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; +import android.widget.Toast; + +import net.osmand.IProgress; +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.LocalIndexInfo; +import net.osmand.plus.activities.TabActivity; +import net.osmand.plus.base.BasicProgressAsyncTask; +import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; +import net.osmand.plus.download.ui.ActiveDownloadsDialogFragment; +import net.osmand.plus.download.ui.DownloadResourceGroupFragment; +import net.osmand.plus.download.ui.LocalIndexesFragment; +import net.osmand.plus.download.ui.UpdatesIndexFragment; +import net.osmand.plus.views.controls.PagerSlidingTabStrip; + +import java.io.File; +import java.lang.ref.WeakReference; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; public class DownloadActivity extends BaseDownloadActivity { + public static final int UPDATES_TAB_NUMBER = 2; + public static final int LOCAL_TAB_NUMBER = 1; + public static final int DOWNLOAD_TAB_NUMBER = 0; private List localIndexInfos = new ArrayList<>(); List mTabs = new ArrayList(); @@ -49,8 +56,10 @@ public class DownloadActivity extends BaseDownloadActivity { public static final String DOWNLOAD_TAB = "download"; public static final String UPDATES_TAB = "updates"; public static final MessageFormat formatGb = new MessageFormat("{0, number,#.##} GB", Locale.US); + public static final MessageFormat formatMb = new MessageFormat("{0, number,##.#} MB", Locale.US); private BannerAndDownloadFreeVersion visibleBanner; + private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { @@ -67,17 +76,19 @@ public class DownloadActivity extends BaseDownloadActivity { updateDescriptionTextWithSize(this, downloadProgressLayout); 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 = 0; - } else if (tab.equals(LOCAL_TAB)) { - currentTab = 1; - } else if (tab.equals(UPDATES_TAB)) { - currentTab = 2; - } + switch (tab) { + case DOWNLOAD_TAB: + currentTab = DOWNLOAD_TAB_NUMBER; + break; + case LOCAL_TAB: + currentTab = LOCAL_TAB_NUMBER; + break; + case UPDATES_TAB: + currentTab = UPDATES_TAB_NUMBER; + break; } - visibleBanner = new BannerAndDownloadFreeVersion(findViewById(R.id.mainLayout), this); - ViewPager viewPager = (ViewPager) findViewById(R.id.pager); + + viewPager = (ViewPager) findViewById(R.id.pager); PagerSlidingTabStrip mSlidingTabLayout = (PagerSlidingTabStrip) findViewById(R.id.sliding_tabs); @@ -92,6 +103,27 @@ public class DownloadActivity extends BaseDownloadActivity { mSlidingTabLayout.setViewPager(viewPager); viewPager.setCurrentItem(currentTab); + viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { + @Override + public void onPageScrolled(int i, float v, int i1) { + + } + + @Override + public void onPageSelected(int i) { + if (i == UPDATES_TAB_NUMBER) { + visibleBanner.hideDownloadProgressLayout(); + } else { + visibleBanner.showDownloadProgressLayout(); + } + } + + @Override + public void onPageScrollStateChanged(int i) { + + } + }); + visibleBanner = new BannerAndDownloadFreeVersion(findViewById(R.id.mainLayout), this); final Intent intent = getIntent(); // FIXME INITIAL FILTER & INITIAL KEY @@ -101,7 +133,6 @@ public class DownloadActivity extends BaseDownloadActivity { } } - @Override protected void onResume() { super.onResume(); @@ -161,7 +192,6 @@ public class DownloadActivity extends BaseDownloadActivity { } - @Override @UiThread public void newDownloadIndexes() { @@ -175,8 +205,9 @@ public class DownloadActivity extends BaseDownloadActivity { downloadHasFinished(); } - - + private int getCurrentTab() { + return viewPager.getCurrentItem(); + } public boolean isLightActionBar() { return ((OsmandApplication) getApplication()).getSettings().isLightActionBar(); @@ -190,17 +221,22 @@ public class DownloadActivity extends BaseDownloadActivity { private final View freeVersionDescriptionTextView; private final View buttonsLinearLayout; private final View freeVersionTitle; + private final OsmandSettings settings; private ToggleCollapseFreeVersionBanner(View freeVersionDescriptionTextView, - View buttonsLinearLayout, View freeVersionTitle) { + View buttonsLinearLayout, View freeVersionTitle, + OsmandSettings settings) { this.freeVersionDescriptionTextView = freeVersionDescriptionTextView; this.buttonsLinearLayout = buttonsLinearLayout; this.freeVersionTitle = freeVersionTitle; + this.settings = settings; } @Override public void onClick(View v) { - if (freeVersionDescriptionTextView.getVisibility() == View.VISIBLE) { + + if (freeVersionDescriptionTextView.getVisibility() == View.VISIBLE + && isDownlodingPermitted(settings)) { freeVersionDescriptionTextView.setVisibility(View.GONE); buttonsLinearLayout.setVisibility(View.GONE); } else { @@ -211,6 +247,12 @@ public class DownloadActivity extends BaseDownloadActivity { } } + public static boolean isDownlodingPermitted(OsmandSettings settings) { + final Integer mapsDownloaded = settings.NUMBER_OF_FREE_DOWNLOADS.get(); + int downloadsLeft = BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS - mapsDownloaded; + return Math.max(downloadsLeft, 0) > 0; + } + public static class BannerAndDownloadFreeVersion { private final View freeVersionBanner; private final View downloadProgressLayout; @@ -250,6 +292,10 @@ public class DownloadActivity extends BaseDownloadActivity { initFreeVersionBanner(); updateFreeVersionBanner(); updateBannerInProgress(); + + if (ctx.getCurrentTab() != UPDATES_TAB_NUMBER) { + downloadProgressLayout.setVisibility(View.VISIBLE); + } } public void updateBannerInProgress() { @@ -259,6 +305,10 @@ public class DownloadActivity extends BaseDownloadActivity { if (isFinished) { downloadProgressLayout.setOnClickListener(null); updateDescriptionTextWithSize(ctx, downloadProgressLayout); + if (ctx.getCurrentTab() == UPDATES_TAB_NUMBER) { + downloadProgressLayout.setVisibility(View.GONE); + } + updateFreeVersionBanner(); } else { boolean indeterminate = basicProgressAsyncTask.isIndeterminate(); String message = basicProgressAsyncTask.getDescription(); @@ -266,6 +316,7 @@ public class DownloadActivity extends BaseDownloadActivity { setMinimizedFreeVersionBanner(true); updateAvailableDownloads(); + downloadProgressLayout.setVisibility(View.VISIBLE); downloadProgressLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -275,6 +326,7 @@ public class DownloadActivity extends BaseDownloadActivity { progressBar.setIndeterminate(indeterminate); if (indeterminate) { leftTextView.setText(message); + rightTextView.setText(null); } else { progressBar.setProgress(percent); // final String format = ctx.getString(R.string.downloading_number_of_files); @@ -282,7 +334,14 @@ public class DownloadActivity extends BaseDownloadActivity { rightTextView.setText(percent + "%"); } } + } + public void hideDownloadProgressLayout() { + downloadProgressLayout.setVisibility(View.GONE); + } + + public void showDownloadProgressLayout() { + downloadProgressLayout.setVisibility(View.VISIBLE); } private void initFreeVersionBanner() { @@ -307,7 +366,7 @@ public class DownloadActivity extends BaseDownloadActivity { } }); laterButton.setOnClickListener(new ToggleCollapseFreeVersionBanner(freeVersionDescriptionTextView, - buttonsLinearLayout, freeVersionBannerTitle)); + buttonsLinearLayout, freeVersionBannerTitle, application.getSettings())); } private void updateFreeVersionBanner() { @@ -325,7 +384,7 @@ public class DownloadActivity extends BaseDownloadActivity { } downloadsLeftTextView.setText(ctx.getString(R.string.downloads_left_template, downloadsLeft)); freeVersionBanner.setOnClickListener(new ToggleCollapseFreeVersionBanner(freeVersionDescriptionTextView, - buttonsLinearLayout, freeVersionBannerTitle)); + buttonsLinearLayout, freeVersionBannerTitle, settings)); } private void updateAvailableDownloads() { @@ -336,7 +395,7 @@ public class DownloadActivity extends BaseDownloadActivity { } private void setMinimizedFreeVersionBanner(boolean minimize) { - if (minimize) { + if (minimize && isDownlodingPermitted(application.getSettings())) { freeVersionDescriptionTextView.setVisibility(View.GONE); buttonsLinearLayout.setVisibility(View.GONE); freeVersionBannerTitle.setVisibility(View.GONE); @@ -348,9 +407,47 @@ public class DownloadActivity extends BaseDownloadActivity { } } + public void reloadLocalIndexes() { + AsyncTask> task = new AsyncTask>() { + @Override + protected void onPreExecute() { + super.onPreExecute(); + setSupportProgressBarIndeterminateVisibility(true); + } + + @Override + protected List doInBackground(Void... params) { + return getMyApplication().getResourceManager().reloadIndexes(IProgress.EMPTY_PROGRESS, + new ArrayList() + ); + } + + @Override + protected void onPostExecute(List warnings) { + setSupportProgressBarIndeterminateVisibility(false); + if (!warnings.isEmpty()) { + final StringBuilder b = new StringBuilder(); + boolean f = true; + for (String w : warnings) { + if (f) { + f = false; + } else { + b.append('\n'); + } + b.append(w); + } + AccessibleToast.makeText(DownloadActivity.this, b.toString(), Toast.LENGTH_LONG).show(); + } + newDownloadIndexes(); + } + }; + task.execute(); + + } + @SuppressWarnings("deprecation") - public static void updateDescriptionTextWithSize(DownloadActivity activity, View view){ + public static void updateDescriptionTextWithSize(DownloadActivity activity, View view) { TextView descriptionText = (TextView) view.findViewById(R.id.rightTextView); TextView messageTextView = (TextView) view.findViewById(R.id.leftTextView); ProgressBar sizeProgress = (ProgressBar) view.findViewById(R.id.progressBar); @@ -358,9 +455,9 @@ public class DownloadActivity extends BaseDownloadActivity { File dir = activity.getMyApplication().getAppPath("").getParentFile(); String size = formatGb.format(new Object[]{0}); int percent = 0; - if(dir.canRead()){ + if (dir.canRead()) { StatFs fs = new StatFs(dir.getAbsolutePath()); - size = formatGb.format(new Object[]{(float) (fs.getAvailableBlocks()) * fs.getBlockSize() / (1 << 30) }); + size = formatGb.format(new Object[]{(float) (fs.getAvailableBlocks()) * fs.getBlockSize() / (1 << 30)}); percent = 100 - (int) (fs.getAvailableBlocks() * 100 / fs.getBlockCount()); } sizeProgress.setIndeterminate(false);