Fix wikivoyage crash

This commit is contained in:
Alexey Kulish 2018-05-22 14:24:52 +03:00
parent 64e415a894
commit ceea1d1646
4 changed files with 255 additions and 141 deletions

View file

@ -8,7 +8,10 @@ import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewTreeObserver;
@ -33,35 +36,37 @@ public class BaseOsmAndFragment extends Fragment implements TransitionAnimator {
super.onResume();
if (Build.VERSION.SDK_INT >= 21) {
Activity activity = getActivity();
int colorId = getStatusBarColorId();
if (colorId != -1) {
if (activity instanceof MapActivity) {
((MapActivity) activity).updateStatusBarColor();
} else {
statusBarColor = activity.getWindow().getStatusBarColor();
activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, colorId));
if (activity != null) {
int colorId = getStatusBarColorId();
if (colorId != -1) {
if (activity instanceof MapActivity) {
((MapActivity) activity).updateStatusBarColor();
} else {
statusBarColor = activity.getWindow().getStatusBarColor();
activity.getWindow().setStatusBarColor(ContextCompat.getColor(activity, colorId));
}
}
}
if (!isFullScreenAllowed() && activity instanceof MapActivity) {
View view = getView();
if (view != null) {
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
if (!isFullScreenAllowed() && activity instanceof MapActivity) {
View view = getView();
if (view != null) {
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onGlobalLayout() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onGlobalLayout() {
View view = getView();
if (view != null) {
ViewTreeObserver obs = view.getViewTreeObserver();
obs.removeOnGlobalLayoutListener(this);
view.requestLayout();
View view = getView();
if (view != null) {
ViewTreeObserver obs = view.getViewTreeObserver();
obs.removeOnGlobalLayoutListener(this);
view.requestLayout();
}
}
}
});
});
}
((MapActivity) activity).exitFromFullScreen();
}
((MapActivity) activity).exitFromFullScreen();
}
}
}
@ -71,11 +76,13 @@ public class BaseOsmAndFragment extends Fragment implements TransitionAnimator {
super.onPause();
if (Build.VERSION.SDK_INT >= 21) {
Activity activity = getActivity();
if (!(activity instanceof MapActivity) && statusBarColor != -1) {
activity.getWindow().setStatusBarColor(statusBarColor);
}
if (!isFullScreenAllowed() && activity instanceof MapActivity) {
((MapActivity) activity).enterToFullScreen();
if (activity != null) {
if (!(activity instanceof MapActivity) && statusBarColor != -1) {
activity.getWindow().setStatusBarColor(statusBarColor);
}
if (!isFullScreenAllowed() && activity instanceof MapActivity) {
((MapActivity) activity).enterToFullScreen();
}
}
}
}
@ -121,14 +128,33 @@ public class BaseOsmAndFragment extends Fragment implements TransitionAnimator {
return true;
}
@Nullable
protected OsmandApplication getMyApplication() {
return (OsmandApplication) getActivity().getApplication();
FragmentActivity activity = getActivity();
if (activity != null) {
return (OsmandApplication) activity.getApplication();
} else {
return null;
}
}
@NonNull
protected OsmandApplication requireMyApplication() {
FragmentActivity activity = requireActivity();
return (OsmandApplication) activity.getApplication();
}
@Nullable
protected OsmandActionBarActivity getMyActivity() {
return (OsmandActionBarActivity) getActivity();
}
@NonNull
protected OsmandActionBarActivity requireMyActivity() {
return (OsmandActionBarActivity) requireActivity();
}
@Nullable
protected OsmandInAppPurchaseActivity getInAppPurchaseActivity() {
Activity activity = getActivity();
if (activity instanceof OsmandInAppPurchaseActivity) {
@ -138,23 +164,28 @@ public class BaseOsmAndFragment extends Fragment implements TransitionAnimator {
}
}
@Nullable
protected IconsCache getIconsCache() {
if (iconsCache == null) {
iconsCache = getMyApplication().getIconsCache();
OsmandApplication app = getMyApplication();
if (iconsCache == null && app != null) {
iconsCache = app.getIconsCache();
}
return iconsCache;
}
protected Drawable getPaintedContentIcon(@DrawableRes int id, @ColorInt int color) {
return getIconsCache().getPaintedIcon(id, color);
IconsCache cache = getIconsCache();
return cache != null ? cache.getPaintedIcon(id, color) : null;
}
protected Drawable getIcon(@DrawableRes int id, @ColorRes int colorId) {
return getIconsCache().getIcon(id, colorId);
IconsCache cache = getIconsCache();
return cache != null ? cache.getIcon(id, colorId) : null;
}
protected Drawable getContentIcon(@DrawableRes int id) {
return getIconsCache().getThemedIcon(id);
IconsCache cache = getIconsCache();
return cache != null ? cache.getThemedIcon(id) : null;
}
protected void setThemedDrawable(View parent, @IdRes int viewId, @DrawableRes int iconId) {
@ -165,7 +196,19 @@ public class BaseOsmAndFragment extends Fragment implements TransitionAnimator {
((ImageView) view).setImageDrawable(getContentIcon(iconId));
}
@Nullable
protected OsmandSettings getSettings() {
return getMyApplication().getSettings();
OsmandApplication app = getMyApplication();
if (app != null) {
return app.getSettings();
} else {
return null;
}
}
@NonNull
protected OsmandSettings requireSettings() {
OsmandApplication app = requireMyApplication();
return app.getSettings();
}
}

View file

@ -46,15 +46,22 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv
private static final String WORLD_WIKIVOYAGE_FILE_NAME = "World_wikivoyage.sqlite";
@Nullable
private ExploreRvAdapter adapter = new ExploreRvAdapter();
private boolean nightMode;
@Nullable
private TravelDownloadUpdateCard downloadUpdateCard;
@Nullable
private TravelNeededMapsCard neededMapsCard;
@Nullable
private DownloadValidationManager downloadManager;
@Nullable
private IndexItem currentDownloadingIndexItem;
@Nullable
private IndexItem mainIndexItem;
private List<IndexItem> neededIndexItems = new ArrayList<>();
private boolean waitForIndexes;
@ -62,8 +69,9 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
downloadManager = new DownloadValidationManager(getMyApplication());
nightMode = !getMyApplication().getSettings().isLightContent();
OsmandApplication app = requireMyApplication();
downloadManager = new DownloadValidationManager(app);
nightMode = !app.getSettings().isLightContent();
final View mainView = inflater.inflate(R.layout.fragment_explore_tab, container, false);
final RecyclerView rv = (RecyclerView) mainView.findViewById(R.id.recycler_view);
@ -74,6 +82,15 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv
return mainView;
}
@Override
public void onResume() {
super.onResume();
WikivoyageExploreActivity exploreActivity = getExploreActivity();
if (exploreActivity != null) {
exploreActivity.onTabFragmentResume(this);
}
}
@Override
public void newDownloadIndexes() {
if (waitForIndexes) {
@ -84,26 +101,32 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv
@Override
public void downloadInProgress() {
IndexItem current = getMyApplication().getDownloadThread().getCurrentDownloadingItem();
if (current != null && current != currentDownloadingIndexItem) {
currentDownloadingIndexItem = current;
removeRedundantCards();
OsmandApplication app = getMyApplication();
if (app != null && adapter != null) {
IndexItem current = app.getDownloadThread().getCurrentDownloadingItem();
if (current != null && current != currentDownloadingIndexItem) {
currentDownloadingIndexItem = current;
removeRedundantCards();
}
adapter.updateDownloadUpdateCard(true);
adapter.updateNeededMapsCard(true);
}
adapter.updateDownloadUpdateCard(true);
adapter.updateNeededMapsCard(true);
}
@Override
public void downloadHasFinished() {
TravelDbHelper travelDbHelper = getMyApplication().getTravelDbHelper();
if (travelDbHelper.getSelectedTravelBook() == null) {
getMyApplication().getTravelDbHelper().initTravelBooks();
WikivoyageExploreActivity exploreActivity = getExploreActivity();
if (exploreActivity != null) {
exploreActivity.populateData();
OsmandApplication app = getMyApplication();
if (app != null) {
TravelDbHelper travelDbHelper = app.getTravelDbHelper();
if (travelDbHelper.getSelectedTravelBook() == null) {
app.getTravelDbHelper().initTravelBooks();
WikivoyageExploreActivity exploreActivity = getExploreActivity();
if (exploreActivity != null) {
exploreActivity.populateData();
}
} else {
removeRedundantCards();
}
} else {
removeRedundantCards();
}
}
@ -126,29 +149,32 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv
public void populateData() {
final List<BaseTravelCard> items = new ArrayList<>();
final OsmandApplication app = getMyApplication();
FragmentManager fm = getFragmentManager();
if (fm != null) {
if (!Version.isPaidVersion(app)) {
items.add(new OpenBetaTravelCard(app, nightMode, fm));
}
if (app.getTravelDbHelper().getSelectedTravelBook() != null) {
items.add(new HeaderTravelCard(app, nightMode, getString(R.string.popular_destinations)));
if (app != null) {
if (adapter != null) {
FragmentManager fm = getFragmentManager();
if (fm != null) {
if (!Version.isPaidVersion(app)) {
items.add(new OpenBetaTravelCard(app, nightMode, fm));
}
if (app.getTravelDbHelper().getSelectedTravelBook() != null) {
items.add(new HeaderTravelCard(app, nightMode, getString(R.string.popular_destinations)));
List<TravelArticle> popularArticles = app.getTravelDbHelper().getPopularArticles();
for (TravelArticle article : popularArticles) {
items.add(new ArticleTravelCard(app, nightMode, article, fm));
List<TravelArticle> popularArticles = app.getTravelDbHelper().getPopularArticles();
for (TravelArticle article : popularArticles) {
items.add(new ArticleTravelCard(app, nightMode, article, fm));
}
}
}
items.add(new StartEditingTravelCard(app, getMyActivity(), nightMode));
adapter.setItems(items);
}
final DownloadIndexesThread downloadThread = app.getDownloadThread();
if (!downloadThread.getIndexes().isDownloadedFromInternet) {
waitForIndexes = true;
downloadThread.runReloadIndexFilesSilent();
} else {
checkDownloadIndexes();
}
}
items.add(new StartEditingTravelCard(app, getMyActivity(), nightMode));
adapter.setItems(items);
final DownloadIndexesThread downloadThread = app.getDownloadThread();
if (!downloadThread.getIndexes().isDownloadedFromInternet) {
waitForIndexes = true;
downloadThread.runReloadIndexFilesSilent();
} else {
checkDownloadIndexes();
}
}
@ -182,69 +208,74 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv
private void addDownloadUpdateCard() {
final OsmandApplication app = getMyApplication();
final DownloadIndexesThread downloadThread = app.getDownloadThread();
if (app != null && adapter != null) {
final DownloadIndexesThread downloadThread = app.getDownloadThread();
boolean outdated = mainIndexItem != null && mainIndexItem.isOutdated();
File selectedTravelBook = app.getTravelDbHelper().getSelectedTravelBook();
boolean outdated = mainIndexItem != null && mainIndexItem.isOutdated();
File selectedTravelBook = app.getTravelDbHelper().getSelectedTravelBook();
if (selectedTravelBook == null || outdated) {
boolean showOtherMaps = false;
if (selectedTravelBook == null) {
List<IndexItem> items = downloadThread.getIndexes().getWikivoyageItems();
showOtherMaps = items != null && items.size() > 1;
}
downloadUpdateCard = new TravelDownloadUpdateCard(app, nightMode, !outdated);
downloadUpdateCard.setShowOtherMapsBtn(showOtherMaps);
downloadUpdateCard.setListener(new TravelDownloadUpdateCard.ClickListener() {
@Override
public void onPrimaryButtonClick() {
if (mainIndexItem != null) {
downloadManager.startDownload(getMyActivity(), mainIndexItem);
adapter.updateDownloadUpdateCard(false);
}
if (selectedTravelBook == null || outdated) {
boolean showOtherMaps = false;
if (selectedTravelBook == null) {
List<IndexItem> items = downloadThread.getIndexes().getWikivoyageItems();
showOtherMaps = items != null && items.size() > 1;
}
@Override
public void onSecondaryButtonClick() {
if (downloadUpdateCard.isLoading()) {
downloadThread.cancelDownload(mainIndexItem);
adapter.updateDownloadUpdateCard(false);
} else if (!downloadUpdateCard.isDownload()) {
removeDownloadUpdateCard();
} else if (downloadUpdateCard.isShowOtherMapsBtn()) {
Activity activity = getActivity();
if (activity != null) {
Intent newIntent = new Intent(activity,
getMyApplication().getAppCustomization().getDownloadActivity());
newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
activity.startActivity(newIntent);
downloadUpdateCard = new TravelDownloadUpdateCard(app, nightMode, !outdated);
downloadUpdateCard.setShowOtherMapsBtn(showOtherMaps);
downloadUpdateCard.setListener(new TravelDownloadUpdateCard.ClickListener() {
@Override
public void onPrimaryButtonClick() {
if (mainIndexItem != null && downloadManager != null && adapter != null) {
downloadManager.startDownload(getMyActivity(), mainIndexItem);
adapter.updateDownloadUpdateCard(false);
}
}
}
});
downloadUpdateCard.setIndexItem(mainIndexItem);
adapter.addDownloadUpdateCard(downloadUpdateCard);
@Override
public void onSecondaryButtonClick() {
if (downloadUpdateCard.isLoading() && adapter != null) {
downloadThread.cancelDownload(mainIndexItem);
adapter.updateDownloadUpdateCard(false);
} else if (!downloadUpdateCard.isDownload()) {
removeDownloadUpdateCard();
} else if (downloadUpdateCard.isShowOtherMapsBtn()) {
Activity activity = getActivity();
if (activity != null) {
Intent newIntent = new Intent(activity,
getMyApplication().getAppCustomization().getDownloadActivity());
newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
activity.startActivity(newIntent);
}
}
}
});
downloadUpdateCard.setIndexItem(mainIndexItem);
adapter.addDownloadUpdateCard(downloadUpdateCard);
}
}
}
private void addNeededMapsCard() {
if (!neededIndexItems.isEmpty()) {
final OsmandApplication app = getMyApplication();
final OsmandApplication app = getMyApplication();
if (app != null && !neededIndexItems.isEmpty() && adapter != null) {
neededMapsCard = new TravelNeededMapsCard(app, nightMode, neededIndexItems);
neededMapsCard.setListener(new TravelNeededMapsCard.CardListener() {
@Override
public void onPrimaryButtonClick() {
downloadManager.startDownload(getMyActivity(), getAllItemsForDownload());
adapter.updateNeededMapsCard(false);
if (adapter != null && downloadManager != null) {
downloadManager.startDownload(getMyActivity(), getAllItemsForDownload());
adapter.updateNeededMapsCard(false);
}
}
@Override
public void onSecondaryButtonClick() {
if (neededMapsCard.isDownloading()) {
app.getDownloadThread().cancelDownload(neededIndexItems);
adapter.updateNeededMapsCard(false);
if (adapter != null) {
app.getDownloadThread().cancelDownload(neededIndexItems);
adapter.updateNeededMapsCard(false);
}
} else {
removeNeededMapsCard();
}
@ -261,10 +292,12 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv
DownloadIndexesThread downloadThread = app.getDownloadThread();
if (downloadThread.isDownloading(item)) {
downloadThread.cancelDownload(item);
} else if (!item.isDownloaded()) {
} else if (!item.isDownloaded() && downloadManager != null) {
downloadManager.startDownload(getMyActivity(), item);
}
adapter.updateNeededMapsCard(false);
if (adapter != null) {
adapter.updateNeededMapsCard(false);
}
}
}
});
@ -285,17 +318,22 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv
@NonNull
private String getWikivoyageFileName() {
File selectedTravelBook = getMyApplication().getTravelDbHelper().getSelectedTravelBook();
OsmandApplication app = getMyApplication();
File selectedTravelBook = app != null ? app.getTravelDbHelper().getSelectedTravelBook() : null;
return selectedTravelBook == null ? WORLD_WIKIVOYAGE_FILE_NAME : selectedTravelBook.getName();
}
private void removeDownloadUpdateCard() {
adapter.removeDownloadUpdateCard();
if (adapter != null) {
adapter.removeDownloadUpdateCard();
}
downloadUpdateCard = null;
}
private void removeNeededMapsCard() {
adapter.removeNeededMapsCard();
if (adapter != null) {
adapter.removeNeededMapsCard();
}
neededMapsCard = null;
}

View file

@ -1,5 +1,6 @@
package net.osmand.plus.wikivoyage.explore;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@ -29,14 +30,15 @@ public class SavedArticlesTabFragment extends BaseOsmAndFragment implements Trav
protected static final Log LOG = PlatformUtil.getLog(SavedArticlesTabFragment.class);
@Nullable
private TravelLocalDataHelper dataHelper;
@Nullable
private SavedArticlesRvAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final OsmandApplication app = getMyApplication();
final OsmandApplication app = requireMyApplication();
dataHelper = app.getTravelDbHelper().getLocalDataHelper();
final View mainView = inflater.inflate(R.layout.fragment_saved_articles_tab, container, false);
@ -62,22 +64,42 @@ public class SavedArticlesTabFragment extends BaseOsmAndFragment implements Trav
@Override
public void onResume() {
super.onResume();
dataHelper.setListener(this);
if (dataHelper != null) {
dataHelper.setListener(this);
}
WikivoyageExploreActivity exploreActivity = getExploreActivity();
if (exploreActivity != null) {
exploreActivity.onTabFragmentResume(this);
}
}
@Override
public void onPause() {
super.onPause();
dataHelper.setListener(null);
if (dataHelper != null) {
dataHelper.setListener(null);
}
}
@Override
public void savedArticlesUpdated() {
List<Object> newItems = getItems();
SavedArticlesDiffCallback diffCallback = new SavedArticlesDiffCallback(adapter.getItems(), newItems);
DiffUtil.DiffResult diffRes = DiffUtil.calculateDiff(diffCallback);
adapter.setItems(newItems);
diffRes.dispatchUpdatesTo(adapter);
if (adapter != null) {
List<Object> newItems = getItems();
SavedArticlesDiffCallback diffCallback = new SavedArticlesDiffCallback(adapter.getItems(), newItems);
DiffUtil.DiffResult diffRes = DiffUtil.calculateDiff(diffCallback);
adapter.setItems(newItems);
diffRes.dispatchUpdatesTo(adapter);
}
}
@Nullable
private WikivoyageExploreActivity getExploreActivity() {
Activity activity = getActivity();
if (activity != null && activity instanceof WikivoyageExploreActivity) {
return (WikivoyageExploreActivity) activity;
} else {
return null;
}
}
public void invalidateAdapter() {
@ -88,11 +110,13 @@ public class SavedArticlesTabFragment extends BaseOsmAndFragment implements Trav
private List<Object> getItems() {
List<Object> items = new ArrayList<>();
List<TravelArticle> savedArticles = dataHelper.getSavedArticles();
if (!savedArticles.isEmpty()) {
Collections.reverse(savedArticles);
items.add(getString(R.string.saved_articles));
items.addAll(savedArticles);
if (dataHelper != null) {
List<TravelArticle> savedArticles = dataHelper.getSavedArticles();
if (!savedArticles.isEmpty()) {
Collections.reverse(savedArticles);
items.add(getString(R.string.saved_articles));
items.addAll(savedArticles);
}
}
return items;
}

View file

@ -42,8 +42,6 @@ import java.util.List;
public class WikivoyageExploreActivity extends TabActivity implements DownloadEvents, OnDialogFragmentResultListener {
public static final String INTENT_KEY_PARENT_WIKIVOYAGE_EXPLORE_ACTIVITY = "intent_key_parent_wikivoyage_explore_activity";
private static final String TAB_SELECTED = "tab_selected";
private static final String CITY_ID_KEY = "city_id_key";
private static final String SELECTED_LANG_KEY = "selected_lang_key";
@ -56,6 +54,7 @@ public class WikivoyageExploreActivity extends TabActivity implements DownloadEv
protected List<WeakReference<Fragment>> fragments = new ArrayList<>();
private LockableViewPager viewPager;
private boolean updateNeeded;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -293,14 +292,18 @@ public class WikivoyageExploreActivity extends TabActivity implements DownloadEv
private void onDataLoaded() {
switchProgressBarVisibility(false);
updateSearchBarVisibility();
updateFragments();
}
private void updateFragments() {
ExploreTabFragment exploreTabFragment = getExploreTabFragment();
if (exploreTabFragment != null) {
exploreTabFragment.populateData();
}
SavedArticlesTabFragment savedArticlesTabFragment = getSavedArticlesTabFragment();
if (savedArticlesTabFragment != null) {
if (exploreTabFragment != null && savedArticlesTabFragment != null) {
exploreTabFragment.populateData();
savedArticlesTabFragment.savedArticlesUpdated();
updateNeeded = false;
} else {
updateNeeded = true;
}
}
@ -324,6 +327,12 @@ public class WikivoyageExploreActivity extends TabActivity implements DownloadEv
}
}
public void onTabFragmentResume(Fragment fragment) {
if (updateNeeded) {
updateFragments();
}
}
private static class LoadWikivoyageData extends AsyncTask<Void, Void, Void> {
private WeakReference<WikivoyageExploreActivity> activityRef;