Fix DownloadActivity NPE

This commit is contained in:
Alexey Kulish 2015-10-21 16:47:47 +03:00
parent 8089fe2fbc
commit 7987baadd9

View file

@ -1,5 +1,25 @@
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;
@ -16,35 +36,8 @@ 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<LocalIndexInfo> localIndexInfos = new ArrayList<>();
List<TabActivity.TabItem> mTabs = new ArrayList<TabActivity.TabItem>();
@ -56,10 +49,8 @@ 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) {
@ -76,19 +67,17 @@ 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);
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;
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;
}
}
viewPager = (ViewPager) findViewById(R.id.pager);
visibleBanner = new BannerAndDownloadFreeVersion(findViewById(R.id.mainLayout), this);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
PagerSlidingTabStrip mSlidingTabLayout = (PagerSlidingTabStrip) findViewById(R.id.sliding_tabs);
@ -103,27 +92,6 @@ 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
@ -133,6 +101,7 @@ public class DownloadActivity extends BaseDownloadActivity {
}
}
@Override
protected void onResume() {
super.onResume();
@ -192,6 +161,7 @@ public class DownloadActivity extends BaseDownloadActivity {
}
@Override
@UiThread
public void newDownloadIndexes() {
@ -205,9 +175,8 @@ public class DownloadActivity extends BaseDownloadActivity {
downloadHasFinished();
}
private int getCurrentTab() {
return viewPager.getCurrentItem();
}
public boolean isLightActionBar() {
return ((OsmandApplication) getApplication()).getSettings().isLightActionBar();
@ -221,22 +190,17 @@ 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,
OsmandSettings settings) {
View buttonsLinearLayout, View freeVersionTitle) {
this.freeVersionDescriptionTextView = freeVersionDescriptionTextView;
this.buttonsLinearLayout = buttonsLinearLayout;
this.freeVersionTitle = freeVersionTitle;
this.settings = settings;
}
@Override
public void onClick(View v) {
if (freeVersionDescriptionTextView.getVisibility() == View.VISIBLE
&& isDownlodingPermitted(settings)) {
if (freeVersionDescriptionTextView.getVisibility() == View.VISIBLE) {
freeVersionDescriptionTextView.setVisibility(View.GONE);
buttonsLinearLayout.setVisibility(View.GONE);
} else {
@ -247,12 +211,6 @@ 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;
@ -292,10 +250,6 @@ public class DownloadActivity extends BaseDownloadActivity {
initFreeVersionBanner();
updateFreeVersionBanner();
updateBannerInProgress();
if (ctx.getCurrentTab() != UPDATES_TAB_NUMBER) {
downloadProgressLayout.setVisibility(View.VISIBLE);
}
}
public void updateBannerInProgress() {
@ -305,10 +259,6 @@ 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();
@ -316,7 +266,6 @@ public class DownloadActivity extends BaseDownloadActivity {
setMinimizedFreeVersionBanner(true);
updateAvailableDownloads();
downloadProgressLayout.setVisibility(View.VISIBLE);
downloadProgressLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -326,7 +275,6 @@ 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);
@ -334,14 +282,7 @@ 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() {
@ -366,7 +307,7 @@ public class DownloadActivity extends BaseDownloadActivity {
}
});
laterButton.setOnClickListener(new ToggleCollapseFreeVersionBanner(freeVersionDescriptionTextView,
buttonsLinearLayout, freeVersionBannerTitle, application.getSettings()));
buttonsLinearLayout, freeVersionBannerTitle));
}
private void updateFreeVersionBanner() {
@ -384,7 +325,7 @@ public class DownloadActivity extends BaseDownloadActivity {
}
downloadsLeftTextView.setText(ctx.getString(R.string.downloads_left_template, downloadsLeft));
freeVersionBanner.setOnClickListener(new ToggleCollapseFreeVersionBanner(freeVersionDescriptionTextView,
buttonsLinearLayout, freeVersionBannerTitle, settings));
buttonsLinearLayout, freeVersionBannerTitle));
}
private void updateAvailableDownloads() {
@ -395,7 +336,7 @@ public class DownloadActivity extends BaseDownloadActivity {
}
private void setMinimizedFreeVersionBanner(boolean minimize) {
if (minimize && isDownlodingPermitted(application.getSettings())) {
if (minimize) {
freeVersionDescriptionTextView.setVisibility(View.GONE);
buttonsLinearLayout.setVisibility(View.GONE);
freeVersionBannerTitle.setVisibility(View.GONE);
@ -407,47 +348,9 @@ public class DownloadActivity extends BaseDownloadActivity {
}
}
public void reloadLocalIndexes() {
AsyncTask<Void, String, List<String>> task = new AsyncTask<Void, String, List<String>>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
setSupportProgressBarIndeterminateVisibility(true);
}
@Override
protected List<String> doInBackground(Void... params) {
return getMyApplication().getResourceManager().reloadIndexes(IProgress.EMPTY_PROGRESS,
new ArrayList<String>()
);
}
@Override
protected void onPostExecute(List<String> 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);
@ -455,9 +358,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);