use of net.osmand.plus.resources.SQLiteTileSource in project Osmand by osmandapp.
the class TerrainLayer method createTileSource.
private SQLiteTileSource createTileSource(@NonNull OsmandApplication app) {
return new SQLiteTileSource(app, null, new ArrayList<>()) {
@Override
protected SQLiteConnection getDatabase() {
throw new UnsupportedOperationException();
}
public boolean isLocked() {
return false;
}
List<String> getTileSource(int x, int y, int zoom) {
ArrayList<String> ls = new ArrayList<>();
int z = (zoom - ZOOM_BOUNDARY);
if (z > 0) {
indexedResources.queryInBox(new QuadRect(x >> z, y >> z, (x >> z), (y >> z)), ls);
} else {
indexedResources.queryInBox(new QuadRect(x << -z, y << -z, (x + 1) << -z, (y + 1) << -z), ls);
}
return ls;
}
@Override
public boolean exists(int x, int y, int zoom) {
List<String> ts = getTileSource(x, y, zoom);
for (String t : ts) {
SQLiteTileSource sqLiteTileSource = resources.get(t);
if (sqLiteTileSource != null && sqLiteTileSource.exists(x, y, zoom)) {
return true;
}
}
return false;
}
@Override
public Bitmap getImage(int x, int y, int zoom, long[] timeHolder) {
List<String> ts = getTileSource(x, y, zoom);
for (String t : ts) {
SQLiteTileSource sqLiteTileSource = resources.get(t);
if (sqLiteTileSource != null) {
Bitmap bmp = sqLiteTileSource.getImage(x, y, zoom, timeHolder);
if (bmp != null) {
return sqLiteTileSource.getImage(x, y, zoom, timeHolder);
}
}
}
return null;
}
@Override
public int getBitDensity() {
return 32;
}
@Override
public int getMinimumZoomSupported() {
return 5;
}
@Override
public int getMaximumZoomSupported() {
return 11;
}
@Override
public int getTileSize() {
return 256;
}
@Override
public boolean couldBeDownloadedFromInternet() {
return false;
}
@Override
public String getName() {
return Algorithms.capitalizeFirstLetter(mode.name().toLowerCase());
}
@Override
public String getTileFormat() {
return "jpg";
}
};
}
use of net.osmand.plus.resources.SQLiteTileSource in project Osmand by osmandapp.
the class LocalIndexesFragment method openPopUpMenu.
private void openPopUpMenu(View v, final LocalIndexInfo info) {
UiUtilities iconsCache = getMyApplication().getUIUtilities();
final PopupMenu optionsMenu = new PopupMenu(getActivity(), v);
DirectionsDialogs.setupPopUpMenuIcon(optionsMenu);
final boolean restore = info.isBackupedData();
MenuItem item;
if ((info.getType() == LocalIndexType.MAP_DATA) || (info.getType() == LocalIndexType.DEACTIVATED)) {
item = optionsMenu.getMenu().add(restore ? R.string.local_index_mi_restore : R.string.local_index_mi_backup).setIcon(iconsCache.getThemedIcon(R.drawable.ic_type_archive));
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
performBasicOperation(restore ? R.string.local_index_mi_restore : R.string.local_index_mi_backup, info);
return true;
}
});
}
if (info.getType() != LocalIndexType.TILES_DATA) {
item = optionsMenu.getMenu().add(R.string.shared_string_rename).setIcon(iconsCache.getThemedIcon(R.drawable.ic_action_edit_dark));
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
performBasicOperation(R.string.shared_string_rename, info);
return true;
}
});
}
if (info.getType() == LocalIndexType.TILES_DATA && ((info.getAttachedObject() instanceof TileSourceManager.TileSourceTemplate) || ((info.getAttachedObject() instanceof SQLiteTileSource) && ((SQLiteTileSource) info.getAttachedObject()).couldBeDownloadedFromInternet()))) {
item = optionsMenu.getMenu().add(R.string.shared_string_edit).setIcon(iconsCache.getThemedIcon(R.drawable.ic_action_edit_dark));
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
performBasicOperation(R.string.shared_string_edit, info);
return true;
}
});
}
if (info.getType() == LocalIndexType.TILES_DATA && (info.getAttachedObject() instanceof ITileSource) && ((ITileSource) info.getAttachedObject()).couldBeDownloadedFromInternet()) {
item = optionsMenu.getMenu().add(R.string.clear_tile_data).setIcon(iconsCache.getThemedIcon(R.drawable.ic_action_remove_dark));
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
performBasicOperation(R.string.clear_tile_data, info);
return true;
}
});
}
final IndexItem update = filesToUpdate.get(info.getFileName());
if (update != null) {
item = optionsMenu.getMenu().add(R.string.update_tile).setIcon(iconsCache.getThemedIcon(R.drawable.ic_action_import));
item.setOnMenuItemClickListener(i -> {
DownloadActivity downloadActivity = getDownloadActivity();
if (downloadActivity != null) {
downloadActivity.startDownload(update);
}
return true;
});
}
item = optionsMenu.getMenu().add(R.string.shared_string_delete).setIcon(iconsCache.getThemedIcon(R.drawable.ic_action_delete_dark));
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
performBasicOperation(R.string.shared_string_delete, info);
return true;
}
});
optionsMenu.show();
}
use of net.osmand.plus.resources.SQLiteTileSource in project Osmand by osmandapp.
the class EditMapSourceDialogFragment method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (savedInstanceState != null) {
editedLayerName = savedInstanceState.getString(EDIT_LAYER_NAME_KEY);
minZoom = savedInstanceState.getInt(MIN_ZOOM_KEY);
maxZoom = savedInstanceState.getInt(MAX_ZOOM_KEY);
expireTimeMinutes = savedInstanceState.getInt(EXPIRE_TIME_KEY);
elliptic = savedInstanceState.getBoolean(ELLIPTIC_KEY);
sqliteDB = savedInstanceState.getBoolean(SQLITE_DB_KEY);
fromTemplate = savedInstanceState.getBoolean(FROM_TEMPLATE_KEY);
}
View root = UiUtilities.getInflater(requireContext(), nightMode).inflate(R.layout.fragment_edit_map_source, container, false);
Toolbar toolbar = root.findViewById(R.id.toolbar);
toolbar.setBackgroundColor(ColorUtilities.getAppBarColor(app, nightMode));
toolbar.setTitleTextColor(ColorUtilities.getActiveButtonsAndLinksTextColor(app, nightMode));
toolbar.setTitle(editedLayerName == null ? R.string.add_online_source : R.string.edit_online_source);
ImageButton iconHelp = root.findViewById(R.id.toolbar_action);
int activeButtonsColorId = ColorUtilities.getActiveButtonsAndLinksTextColorId(nightMode);
Drawable closeDrawable = app.getUIUtilities().getIcon(AndroidUtils.getNavigationIconResId(app), activeButtonsColorId);
Drawable helpDrawable = app.getUIUtilities().getIcon(R.drawable.ic_action_help_online, activeButtonsColorId);
iconHelp.setImageDrawable(helpDrawable);
iconHelp.setOnClickListener(view -> onHelpClick());
toolbar.setNavigationIcon(closeDrawable);
toolbar.setNavigationContentDescription(R.string.shared_string_close);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (wasChanged || fromTemplate) {
showExitDialog();
} else {
dismiss();
}
}
});
int boxStrokeColor = ContextCompat.getColor(app, nightMode ? R.color.icon_color_osmand_dark : R.color.icon_color_osmand_light);
int btnBgColorRes = ColorUtilities.getListBgColorId(nightMode);
nameInputLayout = root.findViewById(R.id.name_input_layout);
nameInputLayout.setBoxStrokeColor(boxStrokeColor);
nameEditText = root.findViewById(R.id.name_edit_text);
nameEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
nameEditText.setRawInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
nameEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
nameEditText.setSelection(nameEditText.getText().length());
}
}
});
urlInputLayout = root.findViewById(R.id.url_input_layout);
urlInputLayout.setBoxStrokeColor(boxStrokeColor);
urlEditText = root.findViewById(R.id.url_edit_text);
urlEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
urlEditText.setRawInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
urlEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
urlEditText.setSelection(urlEditText.getText().length());
}
}
});
contentContainer = root.findViewById(R.id.content_container);
saveBtn = root.findViewById(R.id.save_button);
saveBtn.setBackgroundResource(nightMode ? R.drawable.dlg_btn_primary_dark : R.drawable.dlg_btn_primary_light);
FrameLayout saveBtnBg = root.findViewById(R.id.save_button_bg);
saveBtnBg.setBackgroundColor(ContextCompat.getColor(app, btnBgColorRes));
saveBtnTitle = root.findViewById(R.id.save_button_title);
saveBtnTitle.setTypeface(FontCache.getRobotoMedium(requireContext()));
saveBtnTitle.setTextColor(ContextCompat.getColorStateList(app, nightMode ? R.color.dlg_btn_primary_text_dark : R.color.dlg_btn_primary_text_light));
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveTemplate();
dismiss();
}
});
final ScrollView scrollView = root.findViewById(R.id.scroll_view);
scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
int pastY = 0;
@Override
public void onScrollChanged() {
int y = scrollView.getScrollY();
if (pastY != y) {
pastY = y;
View view = getDialog().getCurrentFocus();
AndroidUtils.hideSoftKeyboard(requireActivity(), view);
}
}
});
if (template == null) {
template = new TileSourceTemplate("", "", PNG_EXT, MAX_ZOOM, MIN_ZOOM, TILE_SIZE, BIT_DENSITY, AVG_SIZE);
}
if (editedLayerName != null && !fromTemplate) {
if (!editedLayerName.endsWith(IndexConstants.SQLITE_EXT)) {
File f = app.getAppPath(IndexConstants.TILES_INDEX_DIR + editedLayerName);
template = TileSourceManager.createTileSourceTemplate(f);
sqliteDB = false;
} else {
List<TileSourceTemplate> knownTemplates = TileSourceManager.getKnownSourceTemplates();
File tPath = app.getAppPath(IndexConstants.TILES_INDEX_DIR);
File dir = new File(tPath, editedLayerName);
SQLiteTileSource sqLiteTileSource = new SQLiteTileSource(app, dir, knownTemplates);
sqLiteTileSource.couldBeDownloadedFromInternet();
template = new TileSourceTemplate(sqLiteTileSource.getName(), sqLiteTileSource.getUrlTemplate(), PNG_EXT, sqLiteTileSource.getMaximumZoomSupported(), sqLiteTileSource.getMinimumZoomSupported(), sqLiteTileSource.getTileSize(), sqLiteTileSource.getBitDensity(), AVG_SIZE);
template.setExpirationTimeMinutes(sqLiteTileSource.getExpirationTimeMinutes());
template.setEllipticYTile(sqLiteTileSource.isEllipticYTile());
sqliteDB = true;
}
}
if (savedInstanceState == null) {
if (fromTemplate) {
editedLayerName = template.getName();
}
urlToLoad = template.getUrlTemplate();
expireTimeMinutes = template.getExpirationTimeMinutes();
minZoom = template.getMinimumZoomSupported();
maxZoom = template.getMaximumZoomSupported();
elliptic = template.isEllipticYTile();
}
updateUi();
return root;
}
use of net.osmand.plus.resources.SQLiteTileSource in project Osmand by osmandapp.
the class EditMapSourceDialogFragment method saveTemplate.
private void saveTemplate() {
try {
String newName = nameEditText.getText().toString();
String urlToLoad = urlEditText.getText().toString();
template.setName(newName);
template.setUrlToLoad(urlToLoad.isEmpty() ? null : urlToLoad.replace("{$x}", "{1}").replace("{$y}", "{2}").replace("{$z}", "{0}"));
template.setMinZoom(minZoom);
template.setMaxZoom(maxZoom);
template.setEllipticYTile(elliptic);
template.setExpirationTimeMinutes(expireTimeMinutes);
File f = app.getAppPath(IndexConstants.TILES_INDEX_DIR + editedLayerName);
String ext = null;
boolean storageChanged = false;
if (f.exists()) {
int extIndex = f.getName().lastIndexOf('.');
ext = extIndex == -1 ? "" : f.getName().substring(extIndex);
String originalName = extIndex == -1 ? f.getName() : f.getName().substring(0, extIndex);
if (!Algorithms.objectEquals(newName, originalName)) {
if (IndexConstants.SQLITE_EXT.equals(ext)) {
f = FileUtils.renameSQLiteFile(app, f, newName + ext, null);
} else {
f.renameTo(app.getAppPath(IndexConstants.TILES_INDEX_DIR + newName));
f = app.getAppPath(IndexConstants.TILES_INDEX_DIR + newName);
}
}
}
if (sqliteDB) {
if (IndexConstants.SQLITE_EXT.equals(ext)) {
List<TileSourceTemplate> knownTemplates = TileSourceManager.getKnownSourceTemplates();
SQLiteTileSource sqLiteTileSource = new SQLiteTileSource(app, f, knownTemplates);
sqLiteTileSource.couldBeDownloadedFromInternet();
sqLiteTileSource.updateFromTileSourceTemplate(template);
} else {
SQLiteTileSource sqLiteTileSource = new SQLiteTileSource(app, newName, minZoom, maxZoom, urlToLoad, "", elliptic, false, "", "", expireTimeMinutes > 0, expireTimeMinutes * 60 * 1000L, false, "");
sqLiteTileSource.createDataBase();
storageChanged = f.exists();
}
} else {
getSettings().installTileSource(template);
storageChanged = f != null && f.exists() && IndexConstants.SQLITE_EXT.equals(ext);
}
if (storageChanged) {
new DeleteTilesTask(app).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, f);
}
Fragment fragment = getTargetFragment();
if (fragment instanceof OnMapSourceUpdateListener) {
((OnMapSourceUpdateListener) fragment).onMapSourceUpdated();
}
} catch (RuntimeException e) {
LOG.error("Error on saving template " + e);
}
}
use of net.osmand.plus.resources.SQLiteTileSource in project Osmand by osmandapp.
the class MapLayers method updateMapSource.
public void updateMapSource(@NonNull OsmandMapTileView mapView, CommonPreference<String> settingsToWarnAboutMap) {
OsmandSettings settings = app.getSettings();
// update transparency
int mapTransparency = settings.MAP_UNDERLAY.get() == null ? 255 : settings.MAP_TRANSPARENCY.get();
mapTileLayer.setAlpha(mapTransparency);
mapVectorLayer.setAlpha(mapTransparency);
ITileSource newSource = settings.getMapTileSource(settings.MAP_TILE_SOURCES == settingsToWarnAboutMap);
ITileSource oldMap = mapTileLayer.getMap();
if (newSource != oldMap) {
if (oldMap instanceof SQLiteTileSource) {
((SQLiteTileSource) oldMap).closeDB();
}
mapTileLayer.setMap(newSource);
}
boolean vectorData = !settings.MAP_ONLINE_DATA.get();
mapTileLayer.setVisible(!vectorData);
mapVectorLayer.setVisible(vectorData);
if (vectorData) {
mapView.setMainLayer(mapVectorLayer);
} else {
mapView.setMainLayer(mapTileLayer);
}
}
Aggregations