Update indexes

This commit is contained in:
Victor Shcherb 2015-10-23 00:27:41 +02:00
parent 85f54c096b
commit 3609492cc7
13 changed files with 226 additions and 133 deletions

View file

@ -9,6 +9,8 @@
3. All your modified/created strings are in the top of the file (to make easier find what\'s translated). 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 PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy
--> -->
<string name="simulate_initial_startup_descr">"Resets flag indicating first startup, keep other settings in the original state"</string>
<string name="simulate_initial_startup">Simulate initial startup</string>
<string name="share_clipboard">Clipboard</string> <string name="share_clipboard">Clipboard</string>
<string name="share_geo">geo:</string> <string name="share_geo">geo:</string>
<string name="share_qr_code">QR-Code</string> <string name="share_qr_code">QR-Code</string>

View file

@ -57,8 +57,8 @@ public class AppInitializer implements IProgress {
public static final boolean TIPS_AND_TRICKS = false; public static final boolean TIPS_AND_TRICKS = false;
private static final String FIRST_TIME_APP_RUN = "FIRST_TIME_APP_RUN"; //$NON-NLS-1$ 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$ public 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 FIRST_INSTALLED = "FIRST_INSTALLED"; //$NON-NLS-1$
private static final String VECTOR_INDEXES_CHECK = "VECTOR_INDEXES_CHECK"; //$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 VERSION_INSTALLED = "VERSION_INSTALLED"; //$NON-NLS-1$
private static final String EXCEPTION_FILE_SIZE = "EXCEPTION_FS"; //$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<String> warnings = new ArrayList<String>(); private List<String> warnings = new ArrayList<String>();
private String taskName; private String taskName;
private List<AppInitializeListener> listeners = new ArrayList<AppInitializer.AppInitializeListener>(); private List<AppInitializeListener> listeners = new ArrayList<AppInitializer.AppInitializeListener>();
private SharedPreferences startPrefs;
public enum InitEvents { public enum InitEvents {
FAVORITES_INITIALIZED, NATIVE_INITIALIZED, FAVORITES_INITIALIZED, NATIVE_INITIALIZED,
@ -113,26 +114,46 @@ public class AppInitializer implements IProgress {
if(initSettings) { if(initSettings) {
return; return;
} }
SharedPreferences pref = activity.getPreferences(Context.MODE_WORLD_WRITEABLE); startPrefs = activity.getPreferences(Context.MODE_WORLD_WRITEABLE);
if(!pref.contains(NUMBER_OF_STARTS)) { if(!startPrefs.contains(NUMBER_OF_STARTS)) {
pref.edit().putInt(NUMBER_OF_STARTS, 1).commit(); startPrefs.edit().putInt(NUMBER_OF_STARTS, 1).commit();
} else { } 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)) { if (!startPrefs.contains(FIRST_INSTALLED)) {
pref.edit().putLong(FIRST_INSTALLED, System.currentTimeMillis()).commit(); startPrefs.edit().putLong(FIRST_INSTALLED, System.currentTimeMillis()).commit();
} }
if (!pref.contains(FIRST_TIME_APP_RUN)) { if (!startPrefs.contains(FIRST_TIME_APP_RUN)) {
firstTime = true; firstTime = true;
pref.edit().putBoolean(FIRST_TIME_APP_RUN, true).commit(); startPrefs.edit().putBoolean(FIRST_TIME_APP_RUN, true).commit();
pref.edit().putString(VERSION_INSTALLED, Version.getFullVersion(app)).commit(); startPrefs.edit().putString(VERSION_INSTALLED, Version.getFullVersion(app)).commit();
} else if (!Version.getFullVersion(app).equals(pref.getString(VERSION_INSTALLED, ""))) { } else if (!Version.getFullVersion(app).equals(startPrefs.getString(VERSION_INSTALLED, ""))) {
pref.edit().putString(VERSION_INSTALLED, Version.getFullVersion(app)).commit(); startPrefs.edit().putString(VERSION_INSTALLED, Version.getFullVersion(app)).commit();
appVersionChanged = true; appVersionChanged = true;
} }
initSettings = 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) { public boolean isFirstTime(Activity activity) {
initUiVars(activity); initUiVars(activity);
return firstTime; return firstTime;
@ -154,8 +175,20 @@ public class AppInitializer implements IProgress {
activityChangesShowed = true; activityChangesShowed = true;
return true; return true;
} }
checkMapUpdates();
return false; 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) { public boolean checkPreviousRunsForExceptions(Activity activity, boolean writeFileSize) {
initUiVars(activity); initUiVars(activity);
@ -419,6 +452,9 @@ public class AppInitializer implements IProgress {
} }
private void restoreBackupForFavoritesFiles() { private void restoreBackupForFavoritesFiles() {
final File appDir = app.getAppPath(null); final File appDir = app.getAppPath(null);
File save = new File(appDir, FavouritesDbHelper.FILE_TO_SAVE); File save = new File(appDir, FavouritesDbHelper.FILE_TO_SAVE);
@ -619,4 +655,7 @@ public class AppInitializer implements IProgress {
public void removeListener(AppInitializeListener listener) { public void removeListener(AppInitializeListener listener) {
this.listeners.remove(listener); this.listeners.remove(listener);
} }
} }

View file

@ -173,6 +173,7 @@ public class ContextMenuAdapter {
i.name = name; i.name = name;
return i; return i;
} }
public Item item(int resId) { public Item item(int resId) {
Item i = new Item(); Item i = new Item();
@ -206,6 +207,7 @@ public class ContextMenuAdapter {
this.lightIcon = icon; this.lightIcon = icon;
return this; return this;
} }
public Item position(int pos) { public Item position(int pos) {
this.pos = pos; this.pos = pos;
@ -257,6 +259,12 @@ public class ContextMenuAdapter {
cat = b; cat = b;
return this; return this;
} }
public Item name(String name) {
this.name = name;
return this;
}
} }
public String[] getItemNames() { public String[] getItemNames() {

View file

@ -23,6 +23,7 @@ import net.osmand.plus.activities.SettingsActivity;
import net.osmand.plus.api.SQLiteAPI; import net.osmand.plus.api.SQLiteAPI;
import net.osmand.plus.api.SQLiteAPIImpl; import net.osmand.plus.api.SQLiteAPIImpl;
import net.osmand.plus.dashboard.DashRateUsFragment; import net.osmand.plus.dashboard.DashRateUsFragment;
import net.osmand.plus.download.DownloadIndexesThread;
import net.osmand.plus.helpers.AvoidSpecificRoads; import net.osmand.plus.helpers.AvoidSpecificRoads;
import net.osmand.plus.helpers.WaypointHelper; import net.osmand.plus.helpers.WaypointHelper;
import net.osmand.plus.monitoring.LiveMonitoringHelper; import net.osmand.plus.monitoring.LiveMonitoringHelper;
@ -93,6 +94,7 @@ public class OsmandApplication extends Application {
LiveMonitoringHelper liveMonitoringHelper; LiveMonitoringHelper liveMonitoringHelper;
TargetPointsHelper targetPointsHelper; TargetPointsHelper targetPointsHelper;
WaypointHelper waypointHelper; WaypointHelper waypointHelper;
DownloadIndexesThread downloadIndexesThread;
AvoidSpecificRoads avoidSpecificRoads; AvoidSpecificRoads avoidSpecificRoads;
BRouterServiceConnection bRouterServiceConnection; BRouterServiceConnection bRouterServiceConnection;
OsmandRegions regions; OsmandRegions regions;
@ -250,6 +252,13 @@ public class OsmandApplication extends Application {
public DayNightHelper getDaynightHelper() { public DayNightHelper getDaynightHelper() {
return daynightHelper; return daynightHelper;
} }
public synchronized DownloadIndexesThread getDownloadThread() {
if(downloadIndexesThread == null) {
downloadIndexesThread = new DownloadIndexesThread(this);
}
return downloadIndexesThread;
}
@Override @Override
public void onLowMemory() { public void onLowMemory() {

View file

@ -1874,8 +1874,6 @@ public class OsmandSettings {
public final CommonPreference<Boolean> SHOW_RULER = public final CommonPreference<Boolean> SHOW_RULER =
new BooleanPreference("show_ruler", true).makeProfile().cache(); new BooleanPreference("show_ruler", true).makeProfile().cache();
public final OsmandPreference<Long> FIRST_INSTALLED_DATE = new LongPreference("first_installed_date", -1).makeGlobal();
// public final OsmandPreference<Integer> NUMBER_OF_FREE_DOWNLOADS_V2 = new IntPreference("free_downloads_v2", 0).makeGlobal(); // public final OsmandPreference<Integer> NUMBER_OF_FREE_DOWNLOADS_V2 = new IntPreference("free_downloads_v2", 0).makeGlobal();
public final OsmandPreference<Integer> NUMBER_OF_FREE_DOWNLOADS = new IntPreference("free_downloads_v3", 0).makeGlobal(); public final OsmandPreference<Integer> NUMBER_OF_FREE_DOWNLOADS = new IntPreference("free_downloads_v3", 0).makeGlobal();
@ -1883,8 +1881,13 @@ public class OsmandSettings {
// For DashRateUsFragment // For DashRateUsFragment
public final OsmandPreference<Long> LAST_DISPLAY_TIME = public final OsmandPreference<Long> LAST_DISPLAY_TIME =
new LongPreference("last_display_time", 0).makeGlobal().cache(); new LongPreference("last_display_time", 0).makeGlobal().cache();
public final OsmandPreference<Long> LAST_CHECKED_UPDATES =
new LongPreference("last_checked_updates", 0).makeGlobal();
public final OsmandPreference<Integer> NUMBER_OF_APPLICATION_STARTS = public final OsmandPreference<Integer> 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<DashRateUsFragment.RateUsState> RATE_US_STATE = public final OsmandPreference<DashRateUsFragment.RateUsState> RATE_US_STATE =
new EnumIntPreference<>("rate_us_state", new EnumIntPreference<>("rate_us_state",
DashRateUsFragment.RateUsState.INITIAL_STATE, DashRateUsFragment.RateUsState.values()) DashRateUsFragment.RateUsState.INITIAL_STATE, DashRateUsFragment.RateUsState.values())

View file

@ -31,7 +31,6 @@ import android.widget.ListView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import net.osmand.Location; import net.osmand.Location;
import net.osmand.StateChangedListener; import net.osmand.StateChangedListener;
import net.osmand.access.AccessibilityPlugin; import net.osmand.access.AccessibilityPlugin;
@ -89,6 +88,7 @@ import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -240,7 +240,6 @@ public class MapActivity extends AccessibleActivity {
} }
private void checkAppInitialization() { private void checkAppInitialization() {
if (app.isApplicationInitializing()) { if (app.isApplicationInitializing()) {
findViewById(R.id.init_progress).setVisibility(View.VISIBLE); findViewById(R.id.init_progress).setVisibility(View.VISIBLE);

View file

@ -20,7 +20,6 @@ import android.widget.EditText;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import net.londatiga.android.ActionItem; import net.londatiga.android.ActionItem;
import net.londatiga.android.QuickAction; import net.londatiga.android.QuickAction;
import net.osmand.IndexConstants; 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.activities.search.SearchActivity;
import net.osmand.plus.dashboard.DashboardOnMap.DashboardType; import net.osmand.plus.dashboard.DashboardOnMap.DashboardType;
import net.osmand.plus.dialogs.FavoriteDialogs; import net.osmand.plus.dialogs.FavoriteDialogs;
import net.osmand.plus.download.IndexItem;
import net.osmand.plus.routing.RouteProvider.GPXRouteParamsBuilder; import net.osmand.plus.routing.RouteProvider.GPXRouteParamsBuilder;
import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.views.BaseMapLayer; import net.osmand.plus.views.BaseMapLayer;
@ -698,8 +698,14 @@ public class MapActivityActions implements DialogProvider {
return false; return false;
} }
}).reg(); }).reg();
String d = getString(R.string.index_settings);
optionsMenuHelper.item(R.string.index_settings).iconColor(R.drawable.ic_type_archive) if(app.getDownloadThread().getIndexes().isDownloadedFromInternet) {
List<IndexItem> 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() { .listen(new OnContextMenuClick() {
@Override @Override
public boolean onContextMenuClick(ArrayAdapter<?> adapter, int itemId, int pos, boolean isChecked) { public boolean onContextMenuClick(ArrayAdapter<?> adapter, int itemId, int pos, boolean isChecked) {

View file

@ -2,8 +2,8 @@ package net.osmand.plus.base;
import net.osmand.IProgress; import net.osmand.IProgress;
import net.osmand.plus.OsmAndConstants; import net.osmand.plus.OsmAndConstants;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import android.content.Context;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
@ -14,13 +14,13 @@ public abstract class BasicProgressAsyncTask<Tag, Params, Progress, Result> exte
protected int deltaProgress; protected int deltaProgress;
protected int work; protected int work;
protected String message = ""; //$NON-NLS-1$ protected String message = ""; //$NON-NLS-1$
protected Context ctx; protected OsmandApplication ctx;
protected boolean interrupted = false; protected boolean interrupted = false;
protected Tag tag; protected Tag tag;
private Handler uiHandler; private Handler uiHandler;
public BasicProgressAsyncTask(Context ctx) { public BasicProgressAsyncTask(OsmandApplication app) {
this.ctx = ctx; this.ctx = app;
this.work = -1; this.work = -1;
} }

View file

@ -10,6 +10,7 @@ import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmAndLocationSimulation; import net.osmand.plus.OsmAndLocationSimulation;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.Version;
import net.osmand.plus.activities.SettingsBaseActivity; import net.osmand.plus.activities.SettingsBaseActivity;
import net.osmand.plus.activities.actions.AppModeDialog; import net.osmand.plus.activities.actions.AppModeDialog;
import net.osmand.util.SunriseSunset; import net.osmand.util.SunriseSunset;
@ -58,8 +59,24 @@ public class SettingsDevelopmentActivity extends SettingsBaseActivity {
cat.addPreference(openGlRender); 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")); "Live updates", "Beta testing for live updates"));
}
Preference pref = new Preference(this); Preference pref = new Preference(this);
final Preference simulate = pref; final Preference simulate = pref;
final OsmAndLocationSimulation sim = getMyApplication().getLocationProvider().getLocationSimulation(); final OsmAndLocationSimulation sim = getMyApplication().getLocationProvider().getLocationSimulation();

View file

@ -4,8 +4,10 @@ import java.io.File;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Set;
import net.osmand.IProgress; import net.osmand.IProgress;
import net.osmand.access.AccessibleToast; import net.osmand.access.AccessibleToast;
@ -13,6 +15,7 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.Version; import net.osmand.plus.Version;
import net.osmand.plus.activities.ActionBarProgressActivity;
import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.activities.LocalIndexInfo;
import net.osmand.plus.activities.TabActivity; import net.osmand.plus.activities.TabActivity;
import net.osmand.plus.base.BasicProgressAsyncTask; 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.LocalIndexesFragment;
import net.osmand.plus.download.ui.UpdatesIndexFragment; import net.osmand.plus.download.ui.UpdatesIndexFragment;
import net.osmand.plus.views.controls.PagerSlidingTabStrip; import net.osmand.plus.views.controls.PagerSlidingTabStrip;
import android.app.Activity;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
@ -40,7 +44,7 @@ import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; 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 UPDATES_TAB_NUMBER = 2;
public static final int LOCAL_TAB_NUMBER = 1; public static final int LOCAL_TAB_NUMBER = 1;
public static final int DOWNLOAD_TAB_NUMBER = 0; public static final int DOWNLOAD_TAB_NUMBER = 0;
@ -61,11 +65,16 @@ public class DownloadActivity extends BaseDownloadActivity {
private ViewPager viewPager; private ViewPager viewPager;
private String filter; private String filter;
private String filterCat; private String filterCat;
protected Set<WeakReference<Fragment>> fragSet = new HashSet<>();
private DownloadIndexesThread downloadThread;
private DownloadValidationManager downloadValidationManager;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
getMyApplication().applyTheme(this); getMyApplication().applyTheme(this);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
downloadValidationManager = new DownloadValidationManager(getMyApplication());
downloadThread = getMyApplication().getDownloadThread();
DownloadResources indexes = getDownloadThread().getIndexes(); DownloadResources indexes = getDownloadThread().getIndexes();
if (!indexes.isDownloadedFromInternet) { if (!indexes.isDownloadedFromInternet) {
getDownloadThread().runReloadIndexFiles(); getDownloadThread().runReloadIndexFiles();
@ -126,11 +135,29 @@ public class DownloadActivity extends BaseDownloadActivity {
filterCat = intent.getExtras().getString(FILTER_CAT); 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>(fragment));
}
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
getMyApplication().getAppCustomization().resumeActivity(DownloadActivity.class, this); getMyApplication().getAppCustomization().resumeActivity(DownloadActivity.class, this);
downloadThread.setUiActivity(this);
downloadInProgress(); downloadInProgress();
} }
@ -154,11 +181,16 @@ public class DownloadActivity extends BaseDownloadActivity {
public List<LocalIndexInfo> getLocalIndexInfos() { public List<LocalIndexInfo> getLocalIndexInfos() {
return localIndexInfos; return localIndexInfos;
} }
public OsmandApplication getMyApplication() {
return (OsmandApplication) getApplication();
}
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
getMyApplication().getAppCustomization().pauseActivity(DownloadActivity.class); getMyApplication().getAppCustomization().pauseActivity(DownloadActivity.class);
downloadThread.setUiActivity(null);
} }
@Override @Override
@ -244,7 +276,7 @@ public class DownloadActivity extends BaseDownloadActivity {
public static boolean isDownlodingPermitted(OsmandSettings settings) { public static boolean isDownlodingPermitted(OsmandSettings settings) {
final Integer mapsDownloaded = settings.NUMBER_OF_FREE_DOWNLOADS.get(); 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; return Math.max(downloadsLeft, 0) > 0;
} }
@ -344,15 +376,15 @@ public class DownloadActivity extends BaseDownloadActivity {
return; return;
} }
freeVersionBanner.setVisibility(View.VISIBLE); 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, 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() { freeVersionBanner.findViewById(R.id.getFullVersionButton).setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
BaseDownloadActivity context = (BaseDownloadActivity) v.getContext(); Activity context = (Activity) v.getContext();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Version.marketPrefix(context Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Version.marketPrefix((OsmandApplication) context
.getMyApplication()) + "net.osmand.plus")); .getApplication()) + "net.osmand.plus"));
try { try {
context.startActivity(intent); context.startActivity(intent);
} catch (ActivityNotFoundException e) { } catch (ActivityNotFoundException e) {
@ -372,7 +404,7 @@ public class DownloadActivity extends BaseDownloadActivity {
OsmandSettings settings = application.getSettings(); OsmandSettings settings = application.getSettings();
final Integer mapsDownloaded = settings.NUMBER_OF_FREE_DOWNLOADS.get(); final Integer mapsDownloaded = settings.NUMBER_OF_FREE_DOWNLOADS.get();
downloadsLeftProgressBar.setProgress(mapsDownloaded); downloadsLeftProgressBar.setProgress(mapsDownloaded);
int downloadsLeft = BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS - mapsDownloaded; int downloadsLeft = DownloadValidationManager.MAXIMUM_AVAILABLE_FREE_DOWNLOADS - mapsDownloaded;
downloadsLeft = Math.max(downloadsLeft, 0); downloadsLeft = Math.max(downloadsLeft, 0);
if (downloadsLeft <= 0) { if (downloadsLeft <= 0) {
laterButton.setVisibility(View.GONE); laterButton.setVisibility(View.GONE);
@ -475,4 +507,8 @@ public class DownloadActivity extends BaseDownloadActivity {
messageTextView.setText(R.string.device_memory); messageTextView.setText(R.string.device_memory);
} }
} }

View file

@ -25,6 +25,7 @@ import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
@ -48,10 +49,9 @@ import android.widget.Toast;
public class DownloadIndexesThread { public class DownloadIndexesThread {
private final static Log LOG = PlatformUtil.getLog(DownloadIndexesThread.class); private final static Log LOG = PlatformUtil.getLog(DownloadIndexesThread.class);
private static final int NOTIFICATION_ID = 45; private static final int NOTIFICATION_ID = 45;
private final Context ctx;
private OsmandApplication app; private OsmandApplication app;
private BaseDownloadActivity uiActivity = null; 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<?, ?, ?, ?>>());
@ -72,9 +72,8 @@ public class DownloadIndexesThread {
} }
public DownloadIndexesThread(Context ctx) { public DownloadIndexesThread(OsmandApplication app) {
this.ctx = ctx; this.app = app;
app = (OsmandApplication) ctx.getApplicationContext();
indexes = new DownloadResources(app); indexes = new DownloadResources(app);
updateLoadedFiles(); updateLoadedFiles();
downloadFileHelper = new DownloadFileHelper(app); downloadFileHelper = new DownloadFileHelper(app);
@ -86,7 +85,7 @@ public class DownloadIndexesThread {
} }
/// UI notifications methods /// UI notifications methods
public void setUiActivity(BaseDownloadActivity uiActivity) { public void setUiActivity(DownloadEvents uiActivity) {
this.uiActivity = uiActivity; this.uiActivity = uiActivity;
} }
@ -200,7 +199,7 @@ public class DownloadIndexesThread {
if (checkRunning()) { if (checkRunning()) {
return; return;
} }
execute(new ReloadIndexesTask(ctx)); execute(new ReloadIndexesTask());
} }
public void runDownloadFiles(IndexItem... items) { public void runDownloadFiles(IndexItem... items) {
@ -213,7 +212,7 @@ public class DownloadIndexesThread {
indexItemDownloading.add(i); indexItemDownloading.add(i);
} }
if (currentDownloadingItem == null) { if (currentDownloadingItem == null) {
execute(new DownloadIndexesAsyncTask(ctx)); execute(new DownloadIndexesAsyncTask());
} }
} }
@ -282,8 +281,8 @@ public class DownloadIndexesThread {
private class ReloadIndexesTask extends BasicProgressAsyncTask<Void, Void, Void, DownloadResources> { private class ReloadIndexesTask extends BasicProgressAsyncTask<Void, Void, Void, DownloadResources> {
public ReloadIndexesTask(Context ctx) { public ReloadIndexesTask() {
super(ctx); super(app);
} }
@Override @Override
@ -304,6 +303,7 @@ public class DownloadIndexesThread {
} }
result.isDownloadedFromInternet = indexFileList.isDownloadedFromInternet(); result.isDownloadedFromInternet = indexFileList.isDownloadedFromInternet();
result.mapVersionIsIncreased = indexFileList.isIncreasedMapVersion(); result.mapVersionIsIncreased = indexFileList.isIncreasedMapVersion();
app.getSettings().LAST_CHECKED_UPDATES.set(System.currentTimeMillis());
result.prepareData(indexFileList.getIndexFiles()); result.prepareData(indexFileList.getIndexFiles());
} catch (Exception e) { } catch (Exception e) {
} }
@ -357,8 +357,8 @@ public class DownloadIndexesThread {
private OsmandPreference<Integer> downloads; private OsmandPreference<Integer> downloads;
public DownloadIndexesAsyncTask(Context ctx) { public DownloadIndexesAsyncTask() {
super(ctx); super(app);
downloads = app.getSettings().NUMBER_OF_FREE_DOWNLOADS; downloads = app.getSettings().NUMBER_OF_FREE_DOWNLOADS;
} }
@ -389,8 +389,8 @@ public class DownloadIndexesThread {
// ctx.getString(R.string.shared_string_io_error) +": Interrupted"; // ctx.getString(R.string.shared_string_io_error) +": Interrupted";
if (!message.toLowerCase().contains("interrupted")) { if (!message.toLowerCase().contains("interrupted")) {
if (uiActivity == null || if (uiActivity == null ||
!message.equals(uiActivity.getString(R.string.shared_string_download_successful))) { !message.equals(app.getString(R.string.shared_string_download_successful))) {
AccessibleToast.makeText(ctx, message, Toast.LENGTH_LONG).show(); app.showToastMessage(message);
} }
} }
} }
@ -403,14 +403,14 @@ public class DownloadIndexesThread {
protected void onPreExecute() { protected void onPreExecute() {
currentRunningTask.add(this); currentRunningTask.add(this);
super.onPreExecute(); super.onPreExecute();
if (uiActivity != null) { downloadFileHelper.setInterruptDownloading(false);
downloadFileHelper.setInterruptDownloading(false); if (uiActivity instanceof Activity) {
View mainView = 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);
} }
@Override @Override
@ -418,8 +418,8 @@ public class DownloadIndexesThread {
if (result != null && result.length() > 0) { if (result != null && result.length() > 0) {
AccessibleToast.makeText(ctx, result, Toast.LENGTH_LONG).show(); AccessibleToast.makeText(ctx, result, Toast.LENGTH_LONG).show();
} }
if (uiActivity != null) { if (uiActivity instanceof Activity) {
View mainView = 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);
} }
@ -508,10 +508,10 @@ public class DownloadIndexesThread {
private boolean validateNotExceedsFreeLimit(IndexItem item) { private boolean validateNotExceedsFreeLimit(IndexItem item) {
boolean exceed = Version.isFreeVersion(app) && 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) { if(exceed) {
String breakDownloadMessage = app.getString(R.string.free_version_message, String breakDownloadMessage = app.getString(R.string.free_version_message,
DownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS + ""); DownloadValidationManager.MAXIMUM_AVAILABLE_FREE_DOWNLOADS + "");
publishProgress(breakDownloadMessage); publishProgress(breakDownloadMessage);
} }
return !exceed; return !exceed;

View file

@ -110,17 +110,17 @@ public class DownloadOsmandIndexesHelper {
} }
public static IndexFileList getIndexesList(Context ctx) { public static IndexFileList getIndexesList(OsmandApplication app) {
PackageManager pm = ctx.getPackageManager(); PackageManager pm = app.getPackageManager();
AssetManager amanager = ctx.getAssets(); AssetManager amanager = app.getAssets();
IndexFileList result = downloadIndexesListFromInternet((OsmandApplication) ctx.getApplicationContext()); IndexFileList result = downloadIndexesListFromInternet(app);
if (result == null) { if (result == null) {
result = new IndexFileList(); result = new IndexFileList();
} else { } else {
result.setDownloadedFromInternet(true); result.setDownloadedFromInternet(true);
} }
// add all tts files from assets // add all tts files from assets
listVoiceAssets(result, amanager, pm, ((OsmandApplication) ctx.getApplicationContext()).getSettings()); listVoiceAssets(result, amanager, pm, app.getSettings());
return result; return result;
} }
@ -183,11 +183,12 @@ public class DownloadOsmandIndexesHelper {
try { try {
String strUrl = ctx.getAppCustomization().getIndexesUrl(); String strUrl = ctx.getAppCustomization().getIndexesUrl();
OsmandSettings settings = ctx.getSettings(); OsmandSettings settings = ctx.getSettings();
Long nd = settings.FIRST_INSTALLED_DATE.get();
long nd = ctx.getAppInitializer().getFirstInstalled();
if(nd > 0) { if(nd > 0) {
strUrl += "&nd=" + ((System.currentTimeMillis() - nd) / (1000l * 24l * 60l * 60l)); strUrl += "&nd=" + ((System.currentTimeMillis() - nd) / (1000l * 24l * 60l * 60l));
} }
strUrl += "&ns=" + settings.NUMBER_OF_APPLICATION_STARTS.get(); strUrl += "&ns=" + ctx.getAppInitializer().getNumberOfStarts();
try { try {
strUrl += "&aid=" + Secure.getString(ctx.getContentResolver(), Secure.ANDROID_ID); strUrl += "&aid=" + Secure.getString(ctx.getContentResolver(), Secure.ANDROID_ID);
} catch (Exception e) { } catch (Exception e) {

View file

@ -12,9 +12,11 @@ import net.osmand.plus.R;
import net.osmand.plus.Version; import net.osmand.plus.Version;
import net.osmand.plus.activities.ActionBarProgressActivity; import net.osmand.plus.activities.ActionBarProgressActivity;
import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents;
import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
@ -23,66 +25,38 @@ import android.support.annotation.NonNull;
import android.support.annotation.UiThread; import android.support.annotation.UiThread;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast; import android.widget.Toast;
public class BaseDownloadActivity extends ActionBarProgressActivity implements DownloadEvents { public class DownloadValidationManager {
protected OsmandSettings settings;
private static DownloadIndexesThread downloadListIndexThread;
protected Set<WeakReference<Fragment>> fragSet = new HashSet<>();
public static final int MAXIMUM_AVAILABLE_FREE_DOWNLOADS = 5; public static final int MAXIMUM_AVAILABLE_FREE_DOWNLOADS = 5;
protected OsmandSettings settings;
private OsmandApplication app;
private DownloadIndexesThread downloadThread;
@Override public DownloadValidationManager(OsmandApplication app) {
protected void onCreate(Bundle savedInstanceState) { this.app = app;
settings = ((OsmandApplication) getApplication()).getSettings(); settings = app.getSettings();
if (downloadListIndexThread == null) { downloadThread = app.getDownloadThread();
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 DownloadIndexesThread getDownloadThread() { public DownloadIndexesThread getDownloadThread() {
return downloadListIndexThread; return downloadThread;
} }
public void startDownload(IndexItem... items) { public void startDownload(FragmentActivity activity, IndexItem... items) {
downloadFilesWithAllChecks(items); downloadFilesWithAllChecks(activity, items);
} }
@UiThread
public void downloadInProgress() {
}
@UiThread
public void downloadHasFinished() {
}
@UiThread
public void newDownloadIndexes() {
}
public OsmandApplication getMyApplication() { 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; long szLong = 0;
int i = 0; int i = 0;
for (IndexItem es : downloadListIndexThread.getCurrentDownloadingItems()) { for (IndexItem es : downloadThread.getCurrentDownloadingItems()) {
szLong += es.contentSize; szLong += es.contentSize;
i++; i++;
} }
@ -92,77 +66,76 @@ public class BaseDownloadActivity extends ActionBarProgressActivity implements D
} }
double sz = ((double) szLong) / (1 << 20); double sz = ((double) szLong) / (1 << 20);
// get availabile space // get availabile space
double asz = downloadListIndexThread.getAvailableSpace(); double asz = downloadThread.getAvailableSpace();
if (asz != -1 && asz > 0 && sz / asz > 0.4) { if (asz != -1 && asz > 0 && sz / asz > 0.4) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(MessageFormat.format(getString(R.string.download_files_question_space), i, sz, asz)); 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() { builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { 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.setNegativeButton(R.string.shared_string_no, null);
builder.show(); builder.show();
} else { } else {
downloadFileCheck_Final_Run(items); downloadFileCheck_Final_Run(activity, items);
} }
} }
private void downloadFileCheck_Final_Run(IndexItem[] items) { private void downloadFileCheck_Final_Run(FragmentActivity activity, IndexItem[] items) {
downloadListIndexThread.runDownloadFiles(items); downloadThread.runDownloadFiles(items);
downloadInProgress(); if(activity instanceof DownloadEvents) {
((DownloadEvents) activity).downloadInProgress();
}
} }
protected void downloadFilesWithAllChecks(IndexItem[] items) { protected void downloadFilesWithAllChecks(FragmentActivity activity, IndexItem[] items) {
downloadFilesCheck_1_FreeVersion(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())) { if (Version.isFreeVersion(getMyApplication())) {
int total = settings.NUMBER_OF_FREE_DOWNLOADS.get(); int total = settings.NUMBER_OF_FREE_DOWNLOADS.get();
if (total > MAXIMUM_AVAILABLE_FREE_DOWNLOADS) { if (total > MAXIMUM_AVAILABLE_FREE_DOWNLOADS) {
new InstallPaidVersionDialogFragment() new InstallPaidVersionDialogFragment()
.show(getSupportFragmentManager(), InstallPaidVersionDialogFragment.TAG); .show(activity.getSupportFragmentManager(), InstallPaidVersionDialogFragment.TAG);
} else { } else {
downloadFilesCheck_2_Internet(items); downloadFilesCheck_2_Internet(activity, items);
} }
} else { } 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().isWifiConnected()) {
if (getMyApplication().getSettings().isInternetConnectionAvailable()) { if (getMyApplication().getSettings().isInternetConnectionAvailable()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(getString(R.string.download_using_mobile_internet)); builder.setMessage(activity.getString(R.string.download_using_mobile_internet));
builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { 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.setNegativeButton(R.string.shared_string_no, null);
builder.show(); builder.show();
} else { } 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 { } else {
downloadFilesCheck_3_ValidateSpace(items); downloadFilesCheck_3_ValidateSpace(activity, items);
} }
} }
@Override
public void onAttachFragment(Fragment fragment) {
fragSet.add(new WeakReference<Fragment>(fragment));
}
public void makeSureUserCancelDownload(final IndexItem item) { public void makeSureUserCancelDownload(Context ctx, final IndexItem item) {
AlertDialog.Builder bld = new AlertDialog.Builder(this); AlertDialog.Builder bld = new AlertDialog.Builder(ctx);
bld.setTitle(getString(R.string.shared_string_cancel)); bld.setTitle(ctx.getString(R.string.shared_string_cancel));
bld.setMessage(R.string.confirm_interrupt_download); bld.setMessage(R.string.confirm_interrupt_download);
bld.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { bld.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() {
@Override @Override