Refactor ui callbacks for DownloadIndexesThread

This commit is contained in:
Alex Sytnyk 2018-04-27 15:53:32 +03:00
parent 607faa7857
commit 618b705d6c
4 changed files with 51 additions and 68 deletions

View file

@ -93,10 +93,8 @@ import net.osmand.plus.helpers.ImportHelper;
import net.osmand.plus.helpers.ImportHelper.ImportGpxBottomSheetDialogFragment; import net.osmand.plus.helpers.ImportHelper.ImportGpxBottomSheetDialogFragment;
import net.osmand.plus.helpers.WakeLockHelper; import net.osmand.plus.helpers.WakeLockHelper;
import net.osmand.plus.inapp.InAppPurchaseHelper; import net.osmand.plus.inapp.InAppPurchaseHelper;
import net.osmand.plus.inapp.InAppPurchaseHelper.InAppPurchaseTaskType;
import net.osmand.plus.mapcontextmenu.AdditionalActionsBottomSheetDialogFragment; import net.osmand.plus.mapcontextmenu.AdditionalActionsBottomSheetDialogFragment;
import net.osmand.plus.mapcontextmenu.MapContextMenu; import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.mapcontextmenu.MapContextMenuFragment;
import net.osmand.plus.mapcontextmenu.builders.cards.dialogs.ContextMenuCardDialogFragment; import net.osmand.plus.mapcontextmenu.builders.cards.dialogs.ContextMenuCardDialogFragment;
import net.osmand.plus.mapcontextmenu.other.DestinationReachedMenu; import net.osmand.plus.mapcontextmenu.other.DestinationReachedMenu;
import net.osmand.plus.mapcontextmenu.other.MapRouteInfoMenu; import net.osmand.plus.mapcontextmenu.other.MapRouteInfoMenu;
@ -1613,13 +1611,10 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
// DownloadEvents // DownloadEvents
@Override @Override
public void newDownloadIndexes() { public void newDownloadIndexes() {
FirstUsageWizardFragment wizardFragment = getFirstUsageWizardFragment(); for (Fragment fragment : getSupportFragmentManager().getFragments()) {
if (wizardFragment != null) { if (fragment instanceof DownloadEvents) {
wizardFragment.newDownloadIndexes(); ((DownloadEvents) fragment).newDownloadIndexes();
} }
WeakReference<MapContextMenuFragment> fragmentRef = getContextMenu().findMenuFragment();
if (fragmentRef != null) {
fragmentRef.get().newDownloadIndexes();
} }
if (dashboardOnMap.isVisible()) { if (dashboardOnMap.isVisible()) {
dashboardOnMap.onNewDownloadIndexes(); dashboardOnMap.onNewDownloadIndexes();
@ -1629,13 +1624,10 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
@Override @Override
public void downloadInProgress() { public void downloadInProgress() {
FirstUsageWizardFragment wizardFragment = getFirstUsageWizardFragment(); for (Fragment fragment : getSupportFragmentManager().getFragments()) {
if (wizardFragment != null) { if (fragment instanceof DownloadEvents) {
wizardFragment.downloadInProgress(); ((DownloadEvents) fragment).downloadInProgress();
} }
WeakReference<MapContextMenuFragment> fragmentRef = getContextMenu().findMenuFragment();
if (fragmentRef != null) {
fragmentRef.get().downloadInProgress();
} }
if (dashboardOnMap.isVisible()) { if (dashboardOnMap.isVisible()) {
dashboardOnMap.onDownloadInProgress(); dashboardOnMap.onDownloadInProgress();
@ -1644,13 +1636,10 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
@Override @Override
public void downloadHasFinished() { public void downloadHasFinished() {
FirstUsageWizardFragment wizardFragment = getFirstUsageWizardFragment(); for (Fragment fragment : getSupportFragmentManager().getFragments()) {
if (wizardFragment != null) { if (fragment instanceof DownloadEvents) {
wizardFragment.downloadHasFinished(); ((DownloadEvents) fragment).downloadHasFinished();
} }
WeakReference<MapContextMenuFragment> fragmentRef = getContextMenu().findMenuFragment();
if (fragmentRef != null) {
fragmentRef.get().downloadHasFinished();
} }
if (dashboardOnMap.isVisible()) { if (dashboardOnMap.isVisible()) {
dashboardOnMap.onDownloadHasFinished(); dashboardOnMap.onDownloadHasFinished();

View file

@ -52,7 +52,7 @@ public class DownloadIndexesThread {
private static final int NOTIFICATION_ID = 45; private static final int NOTIFICATION_ID = 45;
private OsmandApplication app; private OsmandApplication app;
private Set<DownloadEvents> uiCallbacks = new HashSet<>(); private DownloadEvents uiActivity = null;
private DatabaseHelper dbHelper; private DatabaseHelper dbHelper;
private DownloadFileHelper downloadFileHelper; private DownloadFileHelper downloadFileHelper;
private List<BasicProgressAsyncTask<?, ?, ?, ?>> currentRunningTask = Collections.synchronizedList(new ArrayList<BasicProgressAsyncTask<?, ?, ?, ?>>()); private List<BasicProgressAsyncTask<?, ?, ?, ?>> currentRunningTask = Collections.synchronizedList(new ArrayList<BasicProgressAsyncTask<?, ?, ?, ?>>());
@ -87,20 +87,20 @@ public class DownloadIndexesThread {
/// UI notifications methods /// UI notifications methods
public void setUiActivity(DownloadEvents uiActivity) { public void setUiActivity(DownloadEvents uiActivity) {
uiCallbacks.add(uiActivity); this.uiActivity = uiActivity;
} }
public void resetUiActivity(DownloadEvents uiActivity) { public void resetUiActivity(DownloadEvents uiActivity) {
uiCallbacks.remove(uiActivity); if (this.uiActivity == uiActivity) {
this.uiActivity = null;
}
} }
@UiThread @UiThread
protected void downloadInProgress() { protected void downloadInProgress() {
for (DownloadEvents uiActivity : uiCallbacks) {
if (uiActivity != null) { if (uiActivity != null) {
uiActivity.downloadInProgress(); uiActivity.downloadInProgress();
} }
}
updateNotification(); updateNotification();
} }
@ -151,11 +151,9 @@ public class DownloadIndexesThread {
@UiThread @UiThread
protected void downloadHasFinished() { protected void downloadHasFinished() {
for (DownloadEvents uiActivity : uiCallbacks) {
if (uiActivity != null) { if (uiActivity != null) {
uiActivity.downloadHasFinished(); uiActivity.downloadHasFinished();
} }
}
updateNotification(); updateNotification();
} }
@ -188,12 +186,10 @@ public class DownloadIndexesThread {
@UiThread @UiThread
protected void newDownloadIndexes() { protected void newDownloadIndexes() {
for (DownloadEvents uiActivity : uiCallbacks) {
if (uiActivity != null) { if (uiActivity != null) {
uiActivity.newDownloadIndexes(); uiActivity.newDownloadIndexes();
} }
} }
}
// PUBLIC API // PUBLIC API
@ -258,11 +254,9 @@ public class DownloadIndexesThread {
return; return;
} }
} }
for (DownloadEvents uiActivity : uiCallbacks) {
if (uiActivity instanceof Activity) { if (uiActivity instanceof Activity) {
app.logEvent((Activity) uiActivity, "download_files"); app.logEvent((Activity) uiActivity, "download_files");
} }
}
for(IndexItem item : items) { for(IndexItem item : items) {
if (!item.equals(currentDownloadingItem) && !indexItemDownloading.contains(item)) { if (!item.equals(currentDownloadingItem) && !indexItemDownloading.contains(item)) {
indexItemDownloading.add(item); indexItemDownloading.add(item);
@ -460,14 +454,12 @@ public class DownloadIndexesThread {
currentRunningTask.add(this); currentRunningTask.add(this);
super.onPreExecute(); super.onPreExecute();
downloadFileHelper.setInterruptDownloading(false); downloadFileHelper.setInterruptDownloading(false);
for (DownloadEvents uiActivity : uiCallbacks) {
if (uiActivity instanceof Activity) { if (uiActivity instanceof Activity) {
View mainView = ((Activity) uiActivity).findViewById(R.id.MainLayout); View mainView = ((Activity) uiActivity).findViewById(R.id.MainLayout);
if (mainView != null) { if (mainView != null) {
mainView.setKeepScreenOn(true); mainView.setKeepScreenOn(true);
} }
} }
}
startTask(ctx.getString(R.string.shared_string_downloading) + ctx.getString(R.string.shared_string_ellipsis), -1); startTask(ctx.getString(R.string.shared_string_downloading) + ctx.getString(R.string.shared_string_ellipsis), -1);
} }
@ -476,14 +468,12 @@ public class DownloadIndexesThread {
if (result != null && result.length() > 0) { if (result != null && result.length() > 0) {
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show(); Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
} }
for (DownloadEvents uiActivity : uiCallbacks) {
if (uiActivity instanceof Activity) { if (uiActivity instanceof Activity) {
View mainView = ((Activity) uiActivity).findViewById(R.id.MainLayout); View mainView = ((Activity) uiActivity).findViewById(R.id.MainLayout);
if (mainView != null) { if (mainView != null) {
mainView.setKeepScreenOn(false); mainView.setKeepScreenOn(false);
} }
} }
}
currentRunningTask.remove(this); currentRunningTask.remove(this);
indexes.updateFilesToUpdate(); indexes.updateFilesToUpdate();
downloadHasFinished(); downloadHasFinished();

View file

@ -67,18 +67,6 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
return mainView; return mainView;
} }
@Override
public void onResume() {
super.onResume();
getMyApplication().getDownloadThread().setUiActivity(this);
}
@Override
public void onPause() {
super.onPause();
getMyApplication().getDownloadThread().resetUiActivity(this);
}
@Override @Override
public void newDownloadIndexes() { public void newDownloadIndexes() {
if (downloadIndexesRequested) { if (downloadIndexesRequested) {

View file

@ -24,13 +24,14 @@ import net.osmand.PicassoUtils;
import net.osmand.plus.LockableViewPager; import net.osmand.plus.LockableViewPager;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.base.BaseOsmAndFragment;
import net.osmand.plus.download.DownloadIndexesThread;
import net.osmand.plus.wikivoyage.WikivoyageBaseDialogFragment; import net.osmand.plus.wikivoyage.WikivoyageBaseDialogFragment;
import net.osmand.plus.wikivoyage.search.WikivoyageSearchDialogFragment; import net.osmand.plus.wikivoyage.search.WikivoyageSearchDialogFragment;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
public class WikivoyageExploreDialogFragment extends WikivoyageBaseDialogFragment { public class WikivoyageExploreDialogFragment extends WikivoyageBaseDialogFragment implements DownloadIndexesThread.DownloadEvents {
public static final String TAG = "WikivoyageExploreDialogFragment"; public static final String TAG = "WikivoyageExploreDialogFragment";
@ -146,6 +147,21 @@ public class WikivoyageExploreDialogFragment extends WikivoyageBaseDialogFragmen
} }
} }
@Override
public void newDownloadIndexes() {
exploreTabFragment.newDownloadIndexes();
}
@Override
public void downloadInProgress() {
exploreTabFragment.downloadInProgress();
}
@Override
public void downloadHasFinished() {
exploreTabFragment.downloadHasFinished();
}
private ColorStateList createBottomNavColorStateList() { private ColorStateList createBottomNavColorStateList() {
return AndroidUtils.createCheckedColorStateList(getContext(), nightMode, return AndroidUtils.createCheckedColorStateList(getContext(), nightMode,
R.color.icon_color, R.color.wikivoyage_active_light, R.color.icon_color, R.color.wikivoyage_active_light,