Fix DownloadActivity NPE
This commit is contained in:
parent
8089fe2fbc
commit
7987baadd9
1 changed files with 42 additions and 139 deletions
|
@ -1,5 +1,25 @@
|
||||||
package net.osmand.plus.download;
|
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.ActivityNotFoundException;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
@ -16,35 +36,8 @@ import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.TextView;
|
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 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<>();
|
private List<LocalIndexInfo> localIndexInfos = new ArrayList<>();
|
||||||
|
|
||||||
List<TabActivity.TabItem> mTabs = new ArrayList<TabActivity.TabItem>();
|
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 DOWNLOAD_TAB = "download";
|
||||||
public static final String UPDATES_TAB = "updates";
|
public static final String UPDATES_TAB = "updates";
|
||||||
public static final MessageFormat formatGb = new MessageFormat("{0, number,#.##} GB", Locale.US);
|
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 BannerAndDownloadFreeVersion visibleBanner;
|
||||||
private ViewPager viewPager;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -76,19 +67,17 @@ public class DownloadActivity extends BaseDownloadActivity {
|
||||||
updateDescriptionTextWithSize(this, downloadProgressLayout);
|
updateDescriptionTextWithSize(this, downloadProgressLayout);
|
||||||
int currentTab = 0;
|
int currentTab = 0;
|
||||||
String tab = getIntent() == null || getIntent().getExtras() == null ? null : getIntent().getExtras().getString(TAB_TO_OPEN);
|
String tab = getIntent() == null || getIntent().getExtras() == null ? null : getIntent().getExtras().getString(TAB_TO_OPEN);
|
||||||
switch (tab) {
|
if (tab != null) {
|
||||||
case DOWNLOAD_TAB:
|
if (tab.equals(DOWNLOAD_TAB)) {
|
||||||
currentTab = DOWNLOAD_TAB_NUMBER;
|
currentTab = 0;
|
||||||
break;
|
} else if (tab.equals(LOCAL_TAB)) {
|
||||||
case LOCAL_TAB:
|
currentTab = 1;
|
||||||
currentTab = LOCAL_TAB_NUMBER;
|
} else if (tab.equals(UPDATES_TAB)) {
|
||||||
break;
|
currentTab = 2;
|
||||||
case UPDATES_TAB:
|
}
|
||||||
currentTab = UPDATES_TAB_NUMBER;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
visibleBanner = new BannerAndDownloadFreeVersion(findViewById(R.id.mainLayout), this);
|
||||||
viewPager = (ViewPager) findViewById(R.id.pager);
|
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
|
||||||
PagerSlidingTabStrip mSlidingTabLayout = (PagerSlidingTabStrip) findViewById(R.id.sliding_tabs);
|
PagerSlidingTabStrip mSlidingTabLayout = (PagerSlidingTabStrip) findViewById(R.id.sliding_tabs);
|
||||||
|
|
||||||
|
|
||||||
|
@ -103,27 +92,6 @@ public class DownloadActivity extends BaseDownloadActivity {
|
||||||
mSlidingTabLayout.setViewPager(viewPager);
|
mSlidingTabLayout.setViewPager(viewPager);
|
||||||
|
|
||||||
viewPager.setCurrentItem(currentTab);
|
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();
|
final Intent intent = getIntent();
|
||||||
// FIXME INITIAL FILTER & INITIAL KEY
|
// FIXME INITIAL FILTER & INITIAL KEY
|
||||||
|
@ -133,6 +101,7 @@ public class DownloadActivity extends BaseDownloadActivity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
@ -192,6 +161,7 @@ public class DownloadActivity extends BaseDownloadActivity {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@UiThread
|
@UiThread
|
||||||
public void newDownloadIndexes() {
|
public void newDownloadIndexes() {
|
||||||
|
@ -205,9 +175,8 @@ public class DownloadActivity extends BaseDownloadActivity {
|
||||||
downloadHasFinished();
|
downloadHasFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getCurrentTab() {
|
|
||||||
return viewPager.getCurrentItem();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isLightActionBar() {
|
public boolean isLightActionBar() {
|
||||||
return ((OsmandApplication) getApplication()).getSettings().isLightActionBar();
|
return ((OsmandApplication) getApplication()).getSettings().isLightActionBar();
|
||||||
|
@ -221,22 +190,17 @@ public class DownloadActivity extends BaseDownloadActivity {
|
||||||
private final View freeVersionDescriptionTextView;
|
private final View freeVersionDescriptionTextView;
|
||||||
private final View buttonsLinearLayout;
|
private final View buttonsLinearLayout;
|
||||||
private final View freeVersionTitle;
|
private final View freeVersionTitle;
|
||||||
private final OsmandSettings settings;
|
|
||||||
|
|
||||||
private ToggleCollapseFreeVersionBanner(View freeVersionDescriptionTextView,
|
private ToggleCollapseFreeVersionBanner(View freeVersionDescriptionTextView,
|
||||||
View buttonsLinearLayout, View freeVersionTitle,
|
View buttonsLinearLayout, View freeVersionTitle) {
|
||||||
OsmandSettings settings) {
|
|
||||||
this.freeVersionDescriptionTextView = freeVersionDescriptionTextView;
|
this.freeVersionDescriptionTextView = freeVersionDescriptionTextView;
|
||||||
this.buttonsLinearLayout = buttonsLinearLayout;
|
this.buttonsLinearLayout = buttonsLinearLayout;
|
||||||
this.freeVersionTitle = freeVersionTitle;
|
this.freeVersionTitle = freeVersionTitle;
|
||||||
this.settings = settings;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
|
if (freeVersionDescriptionTextView.getVisibility() == View.VISIBLE) {
|
||||||
if (freeVersionDescriptionTextView.getVisibility() == View.VISIBLE
|
|
||||||
&& isDownlodingPermitted(settings)) {
|
|
||||||
freeVersionDescriptionTextView.setVisibility(View.GONE);
|
freeVersionDescriptionTextView.setVisibility(View.GONE);
|
||||||
buttonsLinearLayout.setVisibility(View.GONE);
|
buttonsLinearLayout.setVisibility(View.GONE);
|
||||||
} else {
|
} 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 {
|
public static class BannerAndDownloadFreeVersion {
|
||||||
private final View freeVersionBanner;
|
private final View freeVersionBanner;
|
||||||
private final View downloadProgressLayout;
|
private final View downloadProgressLayout;
|
||||||
|
@ -292,10 +250,6 @@ public class DownloadActivity extends BaseDownloadActivity {
|
||||||
initFreeVersionBanner();
|
initFreeVersionBanner();
|
||||||
updateFreeVersionBanner();
|
updateFreeVersionBanner();
|
||||||
updateBannerInProgress();
|
updateBannerInProgress();
|
||||||
|
|
||||||
if (ctx.getCurrentTab() != UPDATES_TAB_NUMBER) {
|
|
||||||
downloadProgressLayout.setVisibility(View.VISIBLE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateBannerInProgress() {
|
public void updateBannerInProgress() {
|
||||||
|
@ -305,10 +259,6 @@ public class DownloadActivity extends BaseDownloadActivity {
|
||||||
if (isFinished) {
|
if (isFinished) {
|
||||||
downloadProgressLayout.setOnClickListener(null);
|
downloadProgressLayout.setOnClickListener(null);
|
||||||
updateDescriptionTextWithSize(ctx, downloadProgressLayout);
|
updateDescriptionTextWithSize(ctx, downloadProgressLayout);
|
||||||
if (ctx.getCurrentTab() == UPDATES_TAB_NUMBER) {
|
|
||||||
downloadProgressLayout.setVisibility(View.GONE);
|
|
||||||
}
|
|
||||||
updateFreeVersionBanner();
|
|
||||||
} else {
|
} else {
|
||||||
boolean indeterminate = basicProgressAsyncTask.isIndeterminate();
|
boolean indeterminate = basicProgressAsyncTask.isIndeterminate();
|
||||||
String message = basicProgressAsyncTask.getDescription();
|
String message = basicProgressAsyncTask.getDescription();
|
||||||
|
@ -316,7 +266,6 @@ public class DownloadActivity extends BaseDownloadActivity {
|
||||||
setMinimizedFreeVersionBanner(true);
|
setMinimizedFreeVersionBanner(true);
|
||||||
|
|
||||||
updateAvailableDownloads();
|
updateAvailableDownloads();
|
||||||
downloadProgressLayout.setVisibility(View.VISIBLE);
|
|
||||||
downloadProgressLayout.setOnClickListener(new View.OnClickListener() {
|
downloadProgressLayout.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
|
@ -326,7 +275,6 @@ public class DownloadActivity extends BaseDownloadActivity {
|
||||||
progressBar.setIndeterminate(indeterminate);
|
progressBar.setIndeterminate(indeterminate);
|
||||||
if (indeterminate) {
|
if (indeterminate) {
|
||||||
leftTextView.setText(message);
|
leftTextView.setText(message);
|
||||||
rightTextView.setText(null);
|
|
||||||
} else {
|
} else {
|
||||||
progressBar.setProgress(percent);
|
progressBar.setProgress(percent);
|
||||||
// final String format = ctx.getString(R.string.downloading_number_of_files);
|
// final String format = ctx.getString(R.string.downloading_number_of_files);
|
||||||
|
@ -334,14 +282,7 @@ public class DownloadActivity extends BaseDownloadActivity {
|
||||||
rightTextView.setText(percent + "%");
|
rightTextView.setText(percent + "%");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void hideDownloadProgressLayout() {
|
|
||||||
downloadProgressLayout.setVisibility(View.GONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void showDownloadProgressLayout() {
|
|
||||||
downloadProgressLayout.setVisibility(View.VISIBLE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initFreeVersionBanner() {
|
private void initFreeVersionBanner() {
|
||||||
|
@ -366,7 +307,7 @@ public class DownloadActivity extends BaseDownloadActivity {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
laterButton.setOnClickListener(new ToggleCollapseFreeVersionBanner(freeVersionDescriptionTextView,
|
laterButton.setOnClickListener(new ToggleCollapseFreeVersionBanner(freeVersionDescriptionTextView,
|
||||||
buttonsLinearLayout, freeVersionBannerTitle, application.getSettings()));
|
buttonsLinearLayout, freeVersionBannerTitle));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateFreeVersionBanner() {
|
private void updateFreeVersionBanner() {
|
||||||
|
@ -384,7 +325,7 @@ public class DownloadActivity extends BaseDownloadActivity {
|
||||||
}
|
}
|
||||||
downloadsLeftTextView.setText(ctx.getString(R.string.downloads_left_template, downloadsLeft));
|
downloadsLeftTextView.setText(ctx.getString(R.string.downloads_left_template, downloadsLeft));
|
||||||
freeVersionBanner.setOnClickListener(new ToggleCollapseFreeVersionBanner(freeVersionDescriptionTextView,
|
freeVersionBanner.setOnClickListener(new ToggleCollapseFreeVersionBanner(freeVersionDescriptionTextView,
|
||||||
buttonsLinearLayout, freeVersionBannerTitle, settings));
|
buttonsLinearLayout, freeVersionBannerTitle));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateAvailableDownloads() {
|
private void updateAvailableDownloads() {
|
||||||
|
@ -395,7 +336,7 @@ public class DownloadActivity extends BaseDownloadActivity {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setMinimizedFreeVersionBanner(boolean minimize) {
|
private void setMinimizedFreeVersionBanner(boolean minimize) {
|
||||||
if (minimize && isDownlodingPermitted(application.getSettings())) {
|
if (minimize) {
|
||||||
freeVersionDescriptionTextView.setVisibility(View.GONE);
|
freeVersionDescriptionTextView.setVisibility(View.GONE);
|
||||||
buttonsLinearLayout.setVisibility(View.GONE);
|
buttonsLinearLayout.setVisibility(View.GONE);
|
||||||
freeVersionBannerTitle.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")
|
@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 descriptionText = (TextView) view.findViewById(R.id.rightTextView);
|
||||||
TextView messageTextView = (TextView) view.findViewById(R.id.leftTextView);
|
TextView messageTextView = (TextView) view.findViewById(R.id.leftTextView);
|
||||||
ProgressBar sizeProgress = (ProgressBar) view.findViewById(R.id.progressBar);
|
ProgressBar sizeProgress = (ProgressBar) view.findViewById(R.id.progressBar);
|
||||||
|
@ -455,9 +358,9 @@ public class DownloadActivity extends BaseDownloadActivity {
|
||||||
File dir = activity.getMyApplication().getAppPath("").getParentFile();
|
File dir = activity.getMyApplication().getAppPath("").getParentFile();
|
||||||
String size = formatGb.format(new Object[]{0});
|
String size = formatGb.format(new Object[]{0});
|
||||||
int percent = 0;
|
int percent = 0;
|
||||||
if (dir.canRead()) {
|
if(dir.canRead()){
|
||||||
StatFs fs = new StatFs(dir.getAbsolutePath());
|
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());
|
percent = 100 - (int) (fs.getAvailableBlocks() * 100 / fs.getBlockCount());
|
||||||
}
|
}
|
||||||
sizeProgress.setIndeterminate(false);
|
sizeProgress.setIndeterminate(false);
|
||||||
|
|
Loading…
Reference in a new issue