From 3609492cc7a460ab0141dd50545f91e083ee0397 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Fri, 23 Oct 2015 00:27:41 +0200 Subject: [PATCH] Update indexes --- OsmAnd/res/values/strings.xml | 2 + .../src/net/osmand/plus/AppInitializer.java | 65 ++++++++-- .../net/osmand/plus/ContextMenuAdapter.java | 8 ++ .../net/osmand/plus/OsmandApplication.java | 9 ++ .../src/net/osmand/plus/OsmandSettings.java | 9 +- .../osmand/plus/activities/MapActivity.java | 3 +- .../plus/activities/MapActivityActions.java | 12 +- .../plus/base/BasicProgressAsyncTask.java | 8 +- .../SettingsDevelopmentActivity.java | 19 ++- .../plus/download/DownloadActivity.java | 52 ++++++-- .../plus/download/DownloadIndexesThread.java | 44 +++---- .../download/DownloadOsmandIndexesHelper.java | 15 +-- ...ty.java => DownloadValidationManager.java} | 113 +++++++----------- 13 files changed, 226 insertions(+), 133 deletions(-) rename OsmAnd/src/net/osmand/plus/download/{BaseDownloadActivity.java => DownloadValidationManager.java} (59%) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 84e76696c5..6a47902c78 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -9,6 +9,8 @@ 3. All your modified/created strings are in the top of the file (to make easier find what\'s translated). PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy --> + "Resets flag indicating first startup, keep other settings in the original state" + Simulate initial startup Clipboard geo: QR-Code diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index 2629aa01d2..b02b0e00af 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -57,8 +57,8 @@ public class AppInitializer implements IProgress { public static final boolean TIPS_AND_TRICKS = false; private static final String FIRST_TIME_APP_RUN = "FIRST_TIME_APP_RUN"; //$NON-NLS-1$ - protected static final String NUMBER_OF_STARTS = "NUMBER_OF_STARTS"; //$NON-NLS-1$ - protected static final String FIRST_INSTALLED = "FIRST_INSTALLED"; //$NON-NLS-1$ + public static final String NUMBER_OF_STARTS = "NUMBER_OF_STARTS"; //$NON-NLS-1$ + public static final String FIRST_INSTALLED = "FIRST_INSTALLED"; //$NON-NLS-1$ private static final String VECTOR_INDEXES_CHECK = "VECTOR_INDEXES_CHECK"; //$NON-NLS-1$ private static final String VERSION_INSTALLED = "VERSION_INSTALLED"; //$NON-NLS-1$ private static final String EXCEPTION_FILE_SIZE = "EXCEPTION_FS"; //$NON-NLS-1$ @@ -79,6 +79,7 @@ public class AppInitializer implements IProgress { private List warnings = new ArrayList(); private String taskName; private List listeners = new ArrayList(); + private SharedPreferences startPrefs; public enum InitEvents { FAVORITES_INITIALIZED, NATIVE_INITIALIZED, @@ -113,26 +114,46 @@ public class AppInitializer implements IProgress { if(initSettings) { return; } - SharedPreferences pref = activity.getPreferences(Context.MODE_WORLD_WRITEABLE); - if(!pref.contains(NUMBER_OF_STARTS)) { - pref.edit().putInt(NUMBER_OF_STARTS, 1).commit(); + startPrefs = activity.getPreferences(Context.MODE_WORLD_WRITEABLE); + if(!startPrefs.contains(NUMBER_OF_STARTS)) { + startPrefs.edit().putInt(NUMBER_OF_STARTS, 1).commit(); } else { - pref.edit().putInt(NUMBER_OF_STARTS, pref.getInt(NUMBER_OF_STARTS, 0) + 1).commit(); + startPrefs.edit().putInt(NUMBER_OF_STARTS, startPrefs.getInt(NUMBER_OF_STARTS, 0) + 1).commit(); } - if (!pref.contains(FIRST_INSTALLED)) { - pref.edit().putLong(FIRST_INSTALLED, System.currentTimeMillis()).commit(); + if (!startPrefs.contains(FIRST_INSTALLED)) { + startPrefs.edit().putLong(FIRST_INSTALLED, System.currentTimeMillis()).commit(); } - if (!pref.contains(FIRST_TIME_APP_RUN)) { + if (!startPrefs.contains(FIRST_TIME_APP_RUN)) { firstTime = true; - pref.edit().putBoolean(FIRST_TIME_APP_RUN, true).commit(); - pref.edit().putString(VERSION_INSTALLED, Version.getFullVersion(app)).commit(); - } else if (!Version.getFullVersion(app).equals(pref.getString(VERSION_INSTALLED, ""))) { - pref.edit().putString(VERSION_INSTALLED, Version.getFullVersion(app)).commit(); + startPrefs.edit().putBoolean(FIRST_TIME_APP_RUN, true).commit(); + startPrefs.edit().putString(VERSION_INSTALLED, Version.getFullVersion(app)).commit(); + } else if (!Version.getFullVersion(app).equals(startPrefs.getString(VERSION_INSTALLED, ""))) { + startPrefs.edit().putString(VERSION_INSTALLED, Version.getFullVersion(app)).commit(); appVersionChanged = true; } initSettings = true; } + public int getNumberOfStarts() { + if(startPrefs == null) { + return 0; + } + return startPrefs.getInt(NUMBER_OF_STARTS, 1); + } + + public long getFirstInstalled() { + if(startPrefs == null) { + return 0; + } + return startPrefs.getLong(FIRST_INSTALLED, 0); + } + + public void resetFirstTimeRun() { + if(startPrefs != null) { + startPrefs.edit().remove(FIRST_TIME_APP_RUN).commit(); + } + } + public boolean isFirstTime(Activity activity) { initUiVars(activity); return firstTime; @@ -154,8 +175,20 @@ public class AppInitializer implements IProgress { activityChangesShowed = true; return true; } + checkMapUpdates(); + return false; } + + private void checkMapUpdates() { + long diff = System.currentTimeMillis() - app.getSettings().LAST_CHECKED_UPDATES.get(); + if(diff >= 2 * 24 * 60 * 60l && new Random().nextInt(5) == 0 && + app.getSettings().isInternetConnectionAvailable()) { + app.getDownloadThread().runReloadIndexFiles(); + } else if(Version.isDeveloperVersion(app)) { +// app.getDownloadThread().runReloadIndexFiles(); + } + } public boolean checkPreviousRunsForExceptions(Activity activity, boolean writeFileSize) { initUiVars(activity); @@ -419,6 +452,9 @@ public class AppInitializer implements IProgress { } + + + private void restoreBackupForFavoritesFiles() { final File appDir = app.getAppPath(null); File save = new File(appDir, FavouritesDbHelper.FILE_TO_SAVE); @@ -619,4 +655,7 @@ public class AppInitializer implements IProgress { public void removeListener(AppInitializeListener listener) { this.listeners.remove(listener); } + + + } diff --git a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java index 3141cf028a..dd23b8c655 100644 --- a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java @@ -173,6 +173,7 @@ public class ContextMenuAdapter { i.name = name; return i; } + public Item item(int resId) { Item i = new Item(); @@ -206,6 +207,7 @@ public class ContextMenuAdapter { this.lightIcon = icon; return this; } + public Item position(int pos) { this.pos = pos; @@ -257,6 +259,12 @@ public class ContextMenuAdapter { cat = b; return this; } + + public Item name(String name) { + this.name = name; + return this; + } + } public String[] getItemNames() { diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index 332aa851a5..ab4ae0917b 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -23,6 +23,7 @@ import net.osmand.plus.activities.SettingsActivity; import net.osmand.plus.api.SQLiteAPI; import net.osmand.plus.api.SQLiteAPIImpl; import net.osmand.plus.dashboard.DashRateUsFragment; +import net.osmand.plus.download.DownloadIndexesThread; import net.osmand.plus.helpers.AvoidSpecificRoads; import net.osmand.plus.helpers.WaypointHelper; import net.osmand.plus.monitoring.LiveMonitoringHelper; @@ -93,6 +94,7 @@ public class OsmandApplication extends Application { LiveMonitoringHelper liveMonitoringHelper; TargetPointsHelper targetPointsHelper; WaypointHelper waypointHelper; + DownloadIndexesThread downloadIndexesThread; AvoidSpecificRoads avoidSpecificRoads; BRouterServiceConnection bRouterServiceConnection; OsmandRegions regions; @@ -250,6 +252,13 @@ public class OsmandApplication extends Application { public DayNightHelper getDaynightHelper() { return daynightHelper; } + + public synchronized DownloadIndexesThread getDownloadThread() { + if(downloadIndexesThread == null) { + downloadIndexesThread = new DownloadIndexesThread(this); + } + return downloadIndexesThread; + } @Override public void onLowMemory() { diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index c8c0698306..958e903f60 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -1874,8 +1874,6 @@ public class OsmandSettings { public final CommonPreference SHOW_RULER = new BooleanPreference("show_ruler", true).makeProfile().cache(); - public final OsmandPreference FIRST_INSTALLED_DATE = new LongPreference("first_installed_date", -1).makeGlobal(); - // public final OsmandPreference NUMBER_OF_FREE_DOWNLOADS_V2 = new IntPreference("free_downloads_v2", 0).makeGlobal(); public final OsmandPreference NUMBER_OF_FREE_DOWNLOADS = new IntPreference("free_downloads_v3", 0).makeGlobal(); @@ -1883,8 +1881,13 @@ public class OsmandSettings { // For DashRateUsFragment public final OsmandPreference LAST_DISPLAY_TIME = new LongPreference("last_display_time", 0).makeGlobal().cache(); + + public final OsmandPreference LAST_CHECKED_UPDATES = + new LongPreference("last_checked_updates", 0).makeGlobal(); + public final OsmandPreference NUMBER_OF_APPLICATION_STARTS = - new IntPreference("number_of_application_starts", 0).makeGlobal().cache(); + new IntPreference("number_of_app_starts", 0).makeGlobal().cache(); + public final OsmandPreference RATE_US_STATE = new EnumIntPreference<>("rate_us_state", DashRateUsFragment.RateUsState.INITIAL_STATE, DashRateUsFragment.RateUsState.values()) diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 7f84f0e50b..a1d7357545 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -31,7 +31,6 @@ import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; - import net.osmand.Location; import net.osmand.StateChangedListener; import net.osmand.access.AccessibilityPlugin; @@ -89,6 +88,7 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -240,7 +240,6 @@ public class MapActivity extends AccessibleActivity { } - private void checkAppInitialization() { if (app.isApplicationInitializing()) { findViewById(R.id.init_progress).setVisibility(View.VISIBLE); diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index 8d863844b0..0b7a3a3193 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -20,7 +20,6 @@ import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; - import net.londatiga.android.ActionItem; import net.londatiga.android.QuickAction; import net.osmand.IndexConstants; @@ -49,6 +48,7 @@ import net.osmand.plus.activities.actions.OsmAndDialogs; import net.osmand.plus.activities.search.SearchActivity; import net.osmand.plus.dashboard.DashboardOnMap.DashboardType; import net.osmand.plus.dialogs.FavoriteDialogs; +import net.osmand.plus.download.IndexItem; import net.osmand.plus.routing.RouteProvider.GPXRouteParamsBuilder; import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.views.BaseMapLayer; @@ -698,8 +698,14 @@ public class MapActivityActions implements DialogProvider { return false; } }).reg(); - - optionsMenuHelper.item(R.string.index_settings).iconColor(R.drawable.ic_type_archive) + String d = getString(R.string.index_settings); + if(app.getDownloadThread().getIndexes().isDownloadedFromInternet) { + List updt = app.getDownloadThread().getIndexes().getItemsToUpdate(); + if(updt != null && updt.size() > 0) { + d += " ("+updt.size()+")"; + } + } + optionsMenuHelper.item(R.string.index_settings).name(d).iconColor(R.drawable.ic_type_archive) .listen(new OnContextMenuClick() { @Override public boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int pos, boolean isChecked) { diff --git a/OsmAnd/src/net/osmand/plus/base/BasicProgressAsyncTask.java b/OsmAnd/src/net/osmand/plus/base/BasicProgressAsyncTask.java index c0d0008361..6eac51c0e6 100644 --- a/OsmAnd/src/net/osmand/plus/base/BasicProgressAsyncTask.java +++ b/OsmAnd/src/net/osmand/plus/base/BasicProgressAsyncTask.java @@ -2,8 +2,8 @@ package net.osmand.plus.base; import net.osmand.IProgress; import net.osmand.plus.OsmAndConstants; +import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; -import android.content.Context; import android.os.AsyncTask; import android.os.Handler; import android.os.Message; @@ -14,13 +14,13 @@ public abstract class BasicProgressAsyncTask exte protected int deltaProgress; protected int work; protected String message = ""; //$NON-NLS-1$ - protected Context ctx; + protected OsmandApplication ctx; protected boolean interrupted = false; protected Tag tag; private Handler uiHandler; - public BasicProgressAsyncTask(Context ctx) { - this.ctx = ctx; + public BasicProgressAsyncTask(OsmandApplication app) { + this.ctx = app; this.work = -1; } diff --git a/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java b/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java index c96c8f2f6f..27ffe3d2a1 100644 --- a/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java +++ b/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java @@ -10,6 +10,7 @@ import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmAndLocationSimulation; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; +import net.osmand.plus.Version; import net.osmand.plus.activities.SettingsBaseActivity; import net.osmand.plus.activities.actions.AppModeDialog; import net.osmand.util.SunriseSunset; @@ -58,8 +59,24 @@ public class SettingsDevelopmentActivity extends SettingsBaseActivity { cat.addPreference(openGlRender); - cat.addPreference(createCheckBoxPreference(settings.BETA_TESTING_LIVE_UPDATES, + final Preference firstRunPreference = new Preference(this); + firstRunPreference.setTitle(R.string.simulate_initial_startup); + firstRunPreference.setSummary(R.string.simulate_initial_startup_descr); + firstRunPreference.setSelectable(true); + firstRunPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + getMyApplication().getAppInitializer().resetFirstTimeRun(); + getMyApplication().showToastMessage(R.string.shared_string_ok); + return true; + } + }); + cat.addPreference(firstRunPreference); + + if(Version.isDeveloperVersion(getMyApplication())) { + cat.addPreference(createCheckBoxPreference(settings.BETA_TESTING_LIVE_UPDATES, "Live updates", "Beta testing for live updates")); + } Preference pref = new Preference(this); final Preference simulate = pref; final OsmAndLocationSimulation sim = getMyApplication().getLocationProvider().getLocationSimulation(); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 7738588eb3..50bd049b0f 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -4,8 +4,10 @@ import java.io.File; import java.lang.ref.WeakReference; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Set; import net.osmand.IProgress; import net.osmand.access.AccessibleToast; @@ -13,6 +15,7 @@ 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.ActionBarProgressActivity; import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.activities.TabActivity; import net.osmand.plus.base.BasicProgressAsyncTask; @@ -22,6 +25,7 @@ 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.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.net.Uri; @@ -40,7 +44,7 @@ import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; -public class DownloadActivity extends BaseDownloadActivity { +public class DownloadActivity extends ActionBarProgressActivity implements DownloadEvents { public static final int UPDATES_TAB_NUMBER = 2; public static final int LOCAL_TAB_NUMBER = 1; public static final int DOWNLOAD_TAB_NUMBER = 0; @@ -61,11 +65,16 @@ public class DownloadActivity extends BaseDownloadActivity { private ViewPager viewPager; private String filter; private String filterCat; + protected Set> fragSet = new HashSet<>(); + private DownloadIndexesThread downloadThread; + private DownloadValidationManager downloadValidationManager; @Override protected void onCreate(Bundle savedInstanceState) { getMyApplication().applyTheme(this); super.onCreate(savedInstanceState); + downloadValidationManager = new DownloadValidationManager(getMyApplication()); + downloadThread = getMyApplication().getDownloadThread(); DownloadResources indexes = getDownloadThread().getIndexes(); if (!indexes.isDownloadedFromInternet) { getDownloadThread().runReloadIndexFiles(); @@ -126,11 +135,29 @@ public class DownloadActivity extends BaseDownloadActivity { filterCat = intent.getExtras().getString(FILTER_CAT); } } + + public DownloadIndexesThread getDownloadThread() { + return downloadThread; + } + + public void startDownload(IndexItem... indexItem) { + downloadValidationManager.startDownload(this, indexItem); + } + + public void makeSureUserCancelDownload(IndexItem item) { + downloadValidationManager.makeSureUserCancelDownload(this, item); + } + + @Override + public void onAttachFragment(Fragment fragment) { + fragSet.add(new WeakReference(fragment)); + } @Override protected void onResume() { super.onResume(); getMyApplication().getAppCustomization().resumeActivity(DownloadActivity.class, this); + downloadThread.setUiActivity(this); downloadInProgress(); } @@ -154,11 +181,16 @@ public class DownloadActivity extends BaseDownloadActivity { public List getLocalIndexInfos() { return localIndexInfos; } + + public OsmandApplication getMyApplication() { + return (OsmandApplication) getApplication(); + } @Override public void onPause() { super.onPause(); getMyApplication().getAppCustomization().pauseActivity(DownloadActivity.class); + downloadThread.setUiActivity(null); } @Override @@ -244,7 +276,7 @@ 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; + int downloadsLeft = DownloadValidationManager.MAXIMUM_AVAILABLE_FREE_DOWNLOADS - mapsDownloaded; return Math.max(downloadsLeft, 0) > 0; } @@ -344,15 +376,15 @@ public class DownloadActivity extends BaseDownloadActivity { return; } freeVersionBanner.setVisibility(View.VISIBLE); - downloadsLeftProgressBar.setMax(BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS); + downloadsLeftProgressBar.setMax(DownloadValidationManager.MAXIMUM_AVAILABLE_FREE_DOWNLOADS); freeVersionDescriptionTextView.setText(ctx.getString(R.string.free_version_message, - BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS)); + DownloadValidationManager.MAXIMUM_AVAILABLE_FREE_DOWNLOADS)); freeVersionBanner.findViewById(R.id.getFullVersionButton).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - BaseDownloadActivity context = (BaseDownloadActivity) v.getContext(); - Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Version.marketPrefix(context - .getMyApplication()) + "net.osmand.plus")); + Activity context = (Activity) v.getContext(); + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Version.marketPrefix((OsmandApplication) context + .getApplication()) + "net.osmand.plus")); try { context.startActivity(intent); } catch (ActivityNotFoundException e) { @@ -372,7 +404,7 @@ public class DownloadActivity extends BaseDownloadActivity { OsmandSettings settings = application.getSettings(); final Integer mapsDownloaded = settings.NUMBER_OF_FREE_DOWNLOADS.get(); downloadsLeftProgressBar.setProgress(mapsDownloaded); - int downloadsLeft = BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS - mapsDownloaded; + int downloadsLeft = DownloadValidationManager.MAXIMUM_AVAILABLE_FREE_DOWNLOADS - mapsDownloaded; downloadsLeft = Math.max(downloadsLeft, 0); if (downloadsLeft <= 0) { laterButton.setVisibility(View.GONE); @@ -475,4 +507,8 @@ public class DownloadActivity extends BaseDownloadActivity { messageTextView.setText(R.string.device_memory); } + + + + } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index dde6f583a4..f60b6a8252 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -25,6 +25,7 @@ import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; import android.annotation.SuppressLint; +import android.app.Activity; import android.app.AlertDialog; import android.app.Notification; import android.app.NotificationManager; @@ -48,10 +49,9 @@ import android.widget.Toast; public class DownloadIndexesThread { private final static Log LOG = PlatformUtil.getLog(DownloadIndexesThread.class); private static final int NOTIFICATION_ID = 45; - private final Context ctx; private OsmandApplication app; - private BaseDownloadActivity uiActivity = null; + private DownloadEvents uiActivity = null; private DatabaseHelper dbHelper; private DownloadFileHelper downloadFileHelper; private List> currentRunningTask = Collections.synchronizedList(new ArrayList>()); @@ -72,9 +72,8 @@ public class DownloadIndexesThread { } - public DownloadIndexesThread(Context ctx) { - this.ctx = ctx; - app = (OsmandApplication) ctx.getApplicationContext(); + public DownloadIndexesThread(OsmandApplication app) { + this.app = app; indexes = new DownloadResources(app); updateLoadedFiles(); downloadFileHelper = new DownloadFileHelper(app); @@ -86,7 +85,7 @@ public class DownloadIndexesThread { } /// UI notifications methods - public void setUiActivity(BaseDownloadActivity uiActivity) { + public void setUiActivity(DownloadEvents uiActivity) { this.uiActivity = uiActivity; } @@ -200,7 +199,7 @@ public class DownloadIndexesThread { if (checkRunning()) { return; } - execute(new ReloadIndexesTask(ctx)); + execute(new ReloadIndexesTask()); } public void runDownloadFiles(IndexItem... items) { @@ -213,7 +212,7 @@ public class DownloadIndexesThread { indexItemDownloading.add(i); } if (currentDownloadingItem == null) { - execute(new DownloadIndexesAsyncTask(ctx)); + execute(new DownloadIndexesAsyncTask()); } } @@ -282,8 +281,8 @@ public class DownloadIndexesThread { private class ReloadIndexesTask extends BasicProgressAsyncTask { - public ReloadIndexesTask(Context ctx) { - super(ctx); + public ReloadIndexesTask() { + super(app); } @Override @@ -304,6 +303,7 @@ public class DownloadIndexesThread { } result.isDownloadedFromInternet = indexFileList.isDownloadedFromInternet(); result.mapVersionIsIncreased = indexFileList.isIncreasedMapVersion(); + app.getSettings().LAST_CHECKED_UPDATES.set(System.currentTimeMillis()); result.prepareData(indexFileList.getIndexFiles()); } catch (Exception e) { } @@ -357,8 +357,8 @@ public class DownloadIndexesThread { private OsmandPreference downloads; - public DownloadIndexesAsyncTask(Context ctx) { - super(ctx); + public DownloadIndexesAsyncTask() { + super(app); downloads = app.getSettings().NUMBER_OF_FREE_DOWNLOADS; } @@ -389,8 +389,8 @@ public class DownloadIndexesThread { // ctx.getString(R.string.shared_string_io_error) +": Interrupted"; if (!message.toLowerCase().contains("interrupted")) { if (uiActivity == null || - !message.equals(uiActivity.getString(R.string.shared_string_download_successful))) { - AccessibleToast.makeText(ctx, message, Toast.LENGTH_LONG).show(); + !message.equals(app.getString(R.string.shared_string_download_successful))) { + app.showToastMessage(message); } } } @@ -403,14 +403,14 @@ public class DownloadIndexesThread { protected void onPreExecute() { currentRunningTask.add(this); super.onPreExecute(); - if (uiActivity != null) { - downloadFileHelper.setInterruptDownloading(false); - View mainView = uiActivity.findViewById(R.id.MainLayout); + downloadFileHelper.setInterruptDownloading(false); + if (uiActivity instanceof Activity) { + View mainView = ((Activity) uiActivity).findViewById(R.id.MainLayout); if (mainView != null) { 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); } @Override @@ -418,8 +418,8 @@ public class DownloadIndexesThread { if (result != null && result.length() > 0) { AccessibleToast.makeText(ctx, result, Toast.LENGTH_LONG).show(); } - if (uiActivity != null) { - View mainView = uiActivity.findViewById(R.id.MainLayout); + if (uiActivity instanceof Activity) { + View mainView = ((Activity) uiActivity).findViewById(R.id.MainLayout); if (mainView != null) { mainView.setKeepScreenOn(false); } @@ -508,10 +508,10 @@ public class DownloadIndexesThread { private boolean validateNotExceedsFreeLimit(IndexItem item) { boolean exceed = Version.isFreeVersion(app) && - DownloadActivityType.isCountedInDownloads(item) && downloads.get() >= DownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS; + DownloadActivityType.isCountedInDownloads(item) && downloads.get() >= DownloadValidationManager.MAXIMUM_AVAILABLE_FREE_DOWNLOADS; if(exceed) { String breakDownloadMessage = app.getString(R.string.free_version_message, - DownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS + ""); + DownloadValidationManager.MAXIMUM_AVAILABLE_FREE_DOWNLOADS + ""); publishProgress(breakDownloadMessage); } return !exceed; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java index 04aadf6abb..77c862de86 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java @@ -110,17 +110,17 @@ public class DownloadOsmandIndexesHelper { } - public static IndexFileList getIndexesList(Context ctx) { - PackageManager pm = ctx.getPackageManager(); - AssetManager amanager = ctx.getAssets(); - IndexFileList result = downloadIndexesListFromInternet((OsmandApplication) ctx.getApplicationContext()); + public static IndexFileList getIndexesList(OsmandApplication app) { + PackageManager pm = app.getPackageManager(); + AssetManager amanager = app.getAssets(); + IndexFileList result = downloadIndexesListFromInternet(app); if (result == null) { result = new IndexFileList(); } else { result.setDownloadedFromInternet(true); } // add all tts files from assets - listVoiceAssets(result, amanager, pm, ((OsmandApplication) ctx.getApplicationContext()).getSettings()); + listVoiceAssets(result, amanager, pm, app.getSettings()); return result; } @@ -183,11 +183,12 @@ public class DownloadOsmandIndexesHelper { try { String strUrl = ctx.getAppCustomization().getIndexesUrl(); OsmandSettings settings = ctx.getSettings(); - Long nd = settings.FIRST_INSTALLED_DATE.get(); + + long nd = ctx.getAppInitializer().getFirstInstalled(); if(nd > 0) { strUrl += "&nd=" + ((System.currentTimeMillis() - nd) / (1000l * 24l * 60l * 60l)); } - strUrl += "&ns=" + settings.NUMBER_OF_APPLICATION_STARTS.get(); + strUrl += "&ns=" + ctx.getAppInitializer().getNumberOfStarts(); try { strUrl += "&aid=" + Secure.getString(ctx.getContentResolver(), Secure.ANDROID_ID); } catch (Exception e) { diff --git a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java similarity index 59% rename from OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java rename to OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java index 245afee49f..d08f45b066 100644 --- a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java @@ -12,9 +12,11 @@ import net.osmand.plus.R; import net.osmand.plus.Version; import net.osmand.plus.activities.ActionBarProgressActivity; import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; +import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.ActivityNotFoundException; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; @@ -23,66 +25,38 @@ import android.support.annotation.NonNull; import android.support.annotation.UiThread; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; import android.widget.Toast; -public class BaseDownloadActivity extends ActionBarProgressActivity implements DownloadEvents { - protected OsmandSettings settings; - private static DownloadIndexesThread downloadListIndexThread; - protected Set> fragSet = new HashSet<>(); +public class DownloadValidationManager { public static final int MAXIMUM_AVAILABLE_FREE_DOWNLOADS = 5; + protected OsmandSettings settings; + private OsmandApplication app; + private DownloadIndexesThread downloadThread; - @Override - protected void onCreate(Bundle savedInstanceState) { - settings = ((OsmandApplication) getApplication()).getSettings(); - if (downloadListIndexThread == null) { - downloadListIndexThread = new DownloadIndexesThread(this); - } - super.onCreate(savedInstanceState); - } - - @Override - protected void onResume() { - super.onResume(); - downloadListIndexThread.setUiActivity(this); - } - - @Override - protected void onPause() { - super.onPause(); - downloadListIndexThread.setUiActivity(null); + public DownloadValidationManager(OsmandApplication app) { + this.app = app; + settings = app.getSettings(); + downloadThread = app.getDownloadThread(); } public DownloadIndexesThread getDownloadThread() { - return downloadListIndexThread; + return downloadThread; } - public void startDownload(IndexItem... items) { - downloadFilesWithAllChecks(items); + public void startDownload(FragmentActivity activity, IndexItem... items) { + downloadFilesWithAllChecks(activity, items); } - - @UiThread - public void downloadInProgress() { - } - - @UiThread - public void downloadHasFinished() { - } - - @UiThread - public void newDownloadIndexes() { - } - - public OsmandApplication getMyApplication() { - return (OsmandApplication) getApplication(); + return app; } - public void downloadFilesCheck_3_ValidateSpace(final IndexItem... items) { + public void downloadFilesCheck_3_ValidateSpace(final FragmentActivity activity, final IndexItem... items) { long szLong = 0; int i = 0; - for (IndexItem es : downloadListIndexThread.getCurrentDownloadingItems()) { + for (IndexItem es : downloadThread.getCurrentDownloadingItems()) { szLong += es.contentSize; i++; } @@ -92,77 +66,76 @@ public class BaseDownloadActivity extends ActionBarProgressActivity implements D } double sz = ((double) szLong) / (1 << 20); // get availabile space - double asz = downloadListIndexThread.getAvailableSpace(); + double asz = downloadThread.getAvailableSpace(); if (asz != -1 && asz > 0 && sz / asz > 0.4) { - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage(MessageFormat.format(getString(R.string.download_files_question_space), i, sz, asz)); + AlertDialog.Builder builder = new AlertDialog.Builder(activity); + builder.setMessage(MessageFormat.format(activity.getString(R.string.download_files_question_space), i, sz, asz)); builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - downloadFileCheck_Final_Run(items); + downloadFileCheck_Final_Run(activity, items); } }); builder.setNegativeButton(R.string.shared_string_no, null); builder.show(); } else { - downloadFileCheck_Final_Run(items); + downloadFileCheck_Final_Run(activity, items); } } - private void downloadFileCheck_Final_Run(IndexItem[] items) { - downloadListIndexThread.runDownloadFiles(items); - downloadInProgress(); + private void downloadFileCheck_Final_Run(FragmentActivity activity, IndexItem[] items) { + downloadThread.runDownloadFiles(items); + if(activity instanceof DownloadEvents) { + ((DownloadEvents) activity).downloadInProgress(); + } } - protected void downloadFilesWithAllChecks(IndexItem[] items) { - downloadFilesCheck_1_FreeVersion(items); + protected void downloadFilesWithAllChecks(FragmentActivity activity, IndexItem[] items) { + downloadFilesCheck_1_FreeVersion(activity, items); } - protected void downloadFilesCheck_1_FreeVersion(IndexItem[] items) { + protected void downloadFilesCheck_1_FreeVersion(FragmentActivity activity, IndexItem[] items) { if (Version.isFreeVersion(getMyApplication())) { int total = settings.NUMBER_OF_FREE_DOWNLOADS.get(); if (total > MAXIMUM_AVAILABLE_FREE_DOWNLOADS) { new InstallPaidVersionDialogFragment() - .show(getSupportFragmentManager(), InstallPaidVersionDialogFragment.TAG); + .show(activity.getSupportFragmentManager(), InstallPaidVersionDialogFragment.TAG); } else { - downloadFilesCheck_2_Internet(items); + downloadFilesCheck_2_Internet(activity, items); } } else { - downloadFilesCheck_2_Internet(items); + downloadFilesCheck_2_Internet(activity, items); } } - protected void downloadFilesCheck_2_Internet(final IndexItem[] items) { + protected void downloadFilesCheck_2_Internet(final FragmentActivity activity, final IndexItem[] items) { if (!getMyApplication().getSettings().isWifiConnected()) { if (getMyApplication().getSettings().isInternetConnectionAvailable()) { - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage(getString(R.string.download_using_mobile_internet)); + AlertDialog.Builder builder = new AlertDialog.Builder(activity); + builder.setMessage(activity.getString(R.string.download_using_mobile_internet)); builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - downloadFilesCheck_3_ValidateSpace(items); + downloadFilesCheck_3_ValidateSpace(activity, items); } }); builder.setNegativeButton(R.string.shared_string_no, null); builder.show(); } else { - AccessibleToast.makeText(this, R.string.no_index_file_to_download, Toast.LENGTH_LONG).show(); + AccessibleToast.makeText(activity, R.string.no_index_file_to_download, Toast.LENGTH_LONG).show(); } } else { - downloadFilesCheck_3_ValidateSpace(items); + downloadFilesCheck_3_ValidateSpace(activity, items); } } - @Override - public void onAttachFragment(Fragment fragment) { - fragSet.add(new WeakReference(fragment)); - } + - public void makeSureUserCancelDownload(final IndexItem item) { - AlertDialog.Builder bld = new AlertDialog.Builder(this); - bld.setTitle(getString(R.string.shared_string_cancel)); + public void makeSureUserCancelDownload(Context ctx, final IndexItem item) { + AlertDialog.Builder bld = new AlertDialog.Builder(ctx); + bld.setTitle(ctx.getString(R.string.shared_string_cancel)); bld.setMessage(R.string.confirm_interrupt_download); bld.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { @Override