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).
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_geo">geo:</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;
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<String> warnings = new ArrayList<String>();
private String taskName;
private List<AppInitializeListener> listeners = new ArrayList<AppInitializer.AppInitializeListener>();
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,9 +175,21 @@ 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);
long size = activity.getPreferences(Context.MODE_WORLD_READABLE).getLong(EXCEPTION_FILE_SIZE, 0);
@ -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);
}
}

View file

@ -174,6 +174,7 @@ public class ContextMenuAdapter {
return i;
}
public Item item(int resId) {
Item i = new Item();
i.id = resId;
@ -207,6 +208,7 @@ public class ContextMenuAdapter {
return this;
}
public Item position(int pos) {
this.pos = pos;
return this;
@ -257,6 +259,12 @@ public class ContextMenuAdapter {
cat = b;
return this;
}
public Item name(String name) {
this.name = name;
return this;
}
}
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.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;
@ -251,6 +253,13 @@ public class OsmandApplication extends Application {
return daynightHelper;
}
public synchronized DownloadIndexesThread getDownloadThread() {
if(downloadIndexesThread == null) {
downloadIndexesThread = new DownloadIndexesThread(this);
}
return downloadIndexesThread;
}
@Override
public void onLowMemory() {
super.onLowMemory();

View file

@ -1874,8 +1874,6 @@ public class OsmandSettings {
public final CommonPreference<Boolean> SHOW_RULER =
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 = new IntPreference("free_downloads_v3", 0).makeGlobal();
@ -1883,8 +1881,13 @@ public class OsmandSettings {
// For DashRateUsFragment
public final OsmandPreference<Long> LAST_DISPLAY_TIME =
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 =
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 =
new EnumIntPreference<>("rate_us_state",
DashRateUsFragment.RateUsState.INITIAL_STATE, DashRateUsFragment.RateUsState.values())

View file

@ -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);

View file

@ -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<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() {
@Override
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.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<Tag, Params, Progress, Result> 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;
}

View file

@ -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);
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();

View file

@ -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<WeakReference<Fragment>> 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();
@ -127,10 +136,28 @@ public class DownloadActivity extends BaseDownloadActivity {
}
}
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
protected void onResume() {
super.onResume();
getMyApplication().getAppCustomization().resumeActivity(DownloadActivity.class, this);
downloadThread.setUiActivity(this);
downloadInProgress();
}
@ -155,10 +182,15 @@ public class DownloadActivity extends BaseDownloadActivity {
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);
}
}

View file

@ -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<BasicProgressAsyncTask<?, ?, ?, ?>> currentRunningTask = Collections.synchronizedList(new ArrayList<BasicProgressAsyncTask<?, ?, ?, ?>>());
@ -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<Void, Void, Void, DownloadResources> {
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<Integer> 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);
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;

View file

@ -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) {

View file

@ -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<WeakReference<Fragment>> 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>(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