add mapsforge activity to example

This commit is contained in:
Hannes Janetzek 2014-01-28 17:45:08 +01:00
parent a7519724f8
commit d52d761a5c
14 changed files with 705 additions and 1 deletions

View File

@ -27,6 +27,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="org.oscim.android.test.MapsforgeMapActivity$MapFilePicker" />
<activity
android:name="org.oscim.android.test.SimpleMapActivity"
@ -47,11 +48,17 @@
android:label="@string/title_activity_map" >
</activity>
<activity
<activity
android:name="org.oscim.android.test.BitmapTileMapActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:label="@string/title_activity_map" >
</activity>
<activity
android:name="org.oscim.android.test.MapsforgeMapActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:label="@string/title_activity_map" >
</activity>
</application>
</manifest>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/filePickerView"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:fadingEdge="vertical"
android:fadingEdgeLength="30dip" android:columnWidth="100dip" android:numColumns="auto_fit" />

View File

@ -7,5 +7,9 @@
<string name="theme_default">Default</string>
<string name="theme_tubes">Tubes</string>
<string name="theme_osmarender">Osmarender</string>
<string name="ok">OK</string>
<string name="cancel">Cancel</string>
<string name="error">Error</string>
<string name="file_invalid">The selected file is invalid.</string>
</resources>

View File

@ -0,0 +1,250 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.android.filepicker;
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Comparator;
import org.oscim.android.test.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
/**
* A FilePicker displays the contents of directories. The user can navigate
* within the file system and select a single
* file whose path is then returned to the calling activity. The ordering of
* directory contents can be specified via
* {@link #setFileComparator(Comparator)}. By default subfolders and files are
* grouped and each group is ordered
* alphabetically.
* <p>
* A {@link FileFilter} can be activated via
* {@link #setFileDisplayFilter(FileFilter)} to restrict the displayed files and
* folders. By default all files and folders are visible.
* <p>
* Another <code>FileFilter</code> can be applied via
* {@link #setFileSelectFilter(ValidFileFilter)} to check if a selected file is
* valid before its path is returned. By default all files are considered as
* valid and can be selected.
*/
public class FilePicker extends Activity implements AdapterView.OnItemClickListener {
/**
* The name of the extra data in the result {@link Intent}.
*/
public static final String SELECTED_FILE = "selectedFile";
private static final String PREFERENCES_FILE = "FilePicker";
private static final String CURRENT_DIRECTORY = "currentDirectory";
private static final String DEFAULT_DIRECTORY = "/";
private static final int DIALOG_FILE_INVALID = 0;
// private static final int DIALOG_FILE_SELECT = 1;
protected Comparator<File> mFileComparator = getDefaultFileComparator();
protected FileFilter mFileDisplayFilter;
protected ValidFileFilter mFileSelectFilter;
/**
* Sets the file comparator which is used to order the contents of all
* directories before displaying them. If set to
* null, subfolders and files will not be ordered.
*
* @param fileComparator
* the file comparator (may be null).
*/
public void setFileComparator(Comparator<File> fileComparator) {
mFileComparator = fileComparator;
}
/**
* Sets the file display filter. This filter is used to determine which
* files and subfolders of directories will be
* displayed. If set to null, all files and subfolders are shown.
*
* @param fileDisplayFilter
* the file display filter (may be null).
*/
public void setFileDisplayFilter(FileFilter fileDisplayFilter) {
mFileDisplayFilter = fileDisplayFilter;
}
/**
* Sets the file select filter. This filter is used when the user selects a
* file to determine if it is valid. If set
* to null, all files are considered as valid.
*
* @param fileSelectFilter
* the file selection filter (may be null).
*/
public void setFileSelectFilter(ValidFileFilter fileSelectFilter) {
mFileSelectFilter = fileSelectFilter;
}
/**
* Creates the default file comparator.
*
* @return the default file comparator.
*/
private static Comparator<File> getDefaultFileComparator() {
// order all files by type and alphabetically by name
return new Comparator<File>() {
@Override
public int compare(File file1, File file2) {
if (file1.isDirectory() && !file2.isDirectory()) {
return -1;
} else if (!file1.isDirectory() && file2.isDirectory()) {
return 1;
} else {
return file1.getName().compareToIgnoreCase(file2.getName());
}
}
};
}
private File mDirectory;
private FilePickerIconAdapter mFilePickerIconAdapter;
private File[] mFiles;
private File[] mFilesWithParentFolder;
@SuppressWarnings("deprecation")
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
File selectedFile = mFiles[(int) id];
if (selectedFile.isDirectory()) {
mDirectory = selectedFile;
browseToCurrentDirectory();
} else if (mFileSelectFilter == null || mFileSelectFilter.accept(selectedFile)) {
setResult(RESULT_OK,
new Intent().putExtra(SELECTED_FILE, selectedFile.getAbsolutePath()));
finish();
} else {
showDialog(DIALOG_FILE_INVALID);
}
}
/**
* Browses to the current directory.
*/
private void browseToCurrentDirectory() {
setTitle(mDirectory.getAbsolutePath());
// read the subfolders and files from the current directory
if (mFileDisplayFilter == null) {
mFiles = mDirectory.listFiles();
} else {
mFiles = mDirectory.listFiles(mFileDisplayFilter);
}
if (mFiles == null) {
mFiles = new File[0];
} else {
// order the subfolders and files
Arrays.sort(mFiles, mFileComparator);
}
// if a parent directory exists, add it at the first position
if (mDirectory.getParentFile() != null) {
mFilesWithParentFolder = new File[mFiles.length + 1];
mFilesWithParentFolder[0] = mDirectory.getParentFile();
System.arraycopy(mFiles, 0, mFilesWithParentFolder, 1,
mFiles.length);
mFiles = mFilesWithParentFolder;
mFilePickerIconAdapter.setFiles(mFiles, true);
} else {
mFilePickerIconAdapter.setFiles(mFiles, false);
}
mFilePickerIconAdapter.notifyDataSetChanged();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_picker);
mFilePickerIconAdapter = new FilePickerIconAdapter(this);
GridView gridView = (GridView) findViewById(R.id.filePickerView);
gridView.setOnItemClickListener(this);
gridView.setAdapter(mFilePickerIconAdapter);
// if (savedInstanceState == null) {
// // first start of this instance
// showDialog(DIALOG_FILE_SELECT);
// }
}
@Override
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
switch (id) {
case DIALOG_FILE_INVALID:
builder.setIcon(android.R.drawable.ic_menu_info_details);
builder.setTitle(R.string.error);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(getString(R.string.file_invalid));
stringBuilder.append("\n\n");
stringBuilder.append(mFileSelectFilter.getFileOpenResult()
.getErrorMessage());
builder.setMessage(stringBuilder.toString());
builder.setPositiveButton(R.string.ok, null);
return builder.create();
// case DIALOG_FILE_SELECT:
// builder.setMessage(R.string.file_select);
// builder.setPositiveButton(R.string.ok, null);
// return builder.create();
default:
// do dialog will be created
return null;
}
}
@Override
protected void onPause() {
super.onPause();
// save the current directory
Editor editor = getSharedPreferences(PREFERENCES_FILE, MODE_PRIVATE).edit();
editor.clear();
if (mDirectory != null) {
editor.putString(CURRENT_DIRECTORY, mDirectory.getAbsolutePath());
}
editor.commit();
}
@Override
protected void onResume() {
super.onResume();
// restore the current directory
SharedPreferences preferences = getSharedPreferences(PREFERENCES_FILE,
MODE_PRIVATE);
mDirectory = new File(preferences.getString(CURRENT_DIRECTORY,
DEFAULT_DIRECTORY));
if (!mDirectory.exists() || !mDirectory.canRead()) {
mDirectory = new File(DEFAULT_DIRECTORY);
}
browseToCurrentDirectory();
}
}

View File

@ -0,0 +1,116 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.android.filepicker;
import java.io.File;
import org.oscim.android.test.R;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
* An adapter for the FilePicker GridView.
*/
class FilePickerIconAdapter extends BaseAdapter {
private final Context mContext;
private File mCurrentFile;
private File[] mFiles;
private boolean mHasParentFolder;
private TextView mTextView;
/**
* Creates a new FilePickerIconAdapter with the given context.
*
* @param context
* the context of this adapter, through which new Views are
* created.
*/
FilePickerIconAdapter(Context context) {
super();
mContext = context;
}
@Override
public int getCount() {
if (mFiles == null) {
return 0;
}
return mFiles.length;
}
@Override
public Object getItem(int index) {
return mFiles[index];
}
@Override
public long getItemId(int index) {
return index;
}
@Override
public View getView(int index, View convertView, ViewGroup parent) {
if (convertView instanceof TextView) {
// recycle the old view
mTextView = (TextView) convertView;
} else {
// create a new view object
mTextView = new TextView(mContext);
mTextView.setLines(2);
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
mTextView.setPadding(5, 10, 5, 10);
}
if (index == 0 && mHasParentFolder) {
// the parent directory of the current folder
mTextView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.file_picker_back, 0, 0);
mTextView.setText("..");
} else {
mCurrentFile = mFiles[index];
if (mCurrentFile.isDirectory()) {
mTextView.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.file_picker_folder,
0,
0);
} else {
mTextView.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.file_picker_file,
0,
0);
}
mTextView.setText(mCurrentFile.getName());
}
return mTextView;
}
/**
* Sets the data of this adapter.
*
* @param files
* the new files for this adapter.
* @param newHasParentFolder
* true if the file array has a parent folder at index 0, false
* otherwise.
*/
void setFiles(File[] files, boolean newHasParentFolder) {
mFiles = files.clone();
mHasParentFolder = newHasParentFolder;
}
}

View File

@ -0,0 +1,48 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.android.filepicker;
import java.io.File;
import java.io.FileFilter;
/**
* Accepts all readable directories and all readable files with a given
* extension.
*/
public class FilterByFileExtension implements FileFilter {
private final String mExtension;
/**
* @param extension
* the allowed file name extension.
*/
public FilterByFileExtension(String extension) {
mExtension = extension;
}
@Override
public boolean accept(File file) {
// accept only readable files
if (file.canRead()) {
if (file.isDirectory()) {
// accept all directories
return true;
} else if (file.isFile() && file.getName().endsWith(mExtension)) {
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,29 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.android.filepicker;
import java.io.FileFilter;
import org.oscim.tiling.source.TileSource.OpenResult;
/**
* An extension of the {@link FileFilter} interface.
*/
public interface ValidFileFilter extends FileFilter {
/**
* @return the result of the last {@link #accept} call (might be null).
*/
OpenResult getFileOpenResult();
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.android.filepicker;
import java.io.File;
import org.oscim.tiling.source.TileSource.OpenResult;
import org.oscim.tiling.source.mapfile.MapFileTileSource;
/**
* Accepts all valid map files.
*/
public final class ValidMapFile implements ValidFileFilter {
private OpenResult mOpenResult;
@Override
public boolean accept(File file) {
MapFileTileSource mapFileSource = new MapFileTileSource();
mapFileSource.setMapFile(file.getAbsolutePath());
this.mOpenResult = mapFileSource.open();
mapFileSource.close();
return mOpenResult.isSuccess();
}
@Override
public OpenResult getFileOpenResult() {
return mOpenResult;
}
}

View File

@ -0,0 +1,71 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.android.filepicker;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.oscim.theme.RenderThemeHandler;
import org.oscim.tiling.source.TileSource.OpenResult;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* Accepts all valid render theme XML files.
*/
public final class ValidRenderTheme implements ValidFileFilter {
private OpenResult mOpenResult;
@Override
public boolean accept(File file) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
RenderThemeHandler renderThemeHandler = new RenderThemeHandler();
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
xmlReader.setContentHandler(renderThemeHandler);
xmlReader.parse(new InputSource(inputStream));
mOpenResult = OpenResult.SUCCESS;
} catch (ParserConfigurationException e) {
mOpenResult = new OpenResult(e.getMessage());
} catch (SAXException e) {
mOpenResult = new OpenResult(e.getMessage());
} catch (IOException e) {
mOpenResult = new OpenResult(e.getMessage());
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
mOpenResult = new OpenResult(e.getMessage());
}
}
return mOpenResult.isSuccess();
}
@Override
public OpenResult getFileOpenResult() {
return mOpenResult;
}
}

View File

@ -0,0 +1,127 @@
/*
* Copyright 2014 Hannes Janetzek
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.android.test;
import org.oscim.android.MapActivity;
import org.oscim.android.MapView;
import org.oscim.android.filepicker.FilePicker;
import org.oscim.android.filepicker.FilterByFileExtension;
import org.oscim.android.filepicker.ValidMapFile;
import org.oscim.core.MapPosition;
import org.oscim.core.Tile;
import org.oscim.layers.tile.vector.VectorTileLayer;
import org.oscim.layers.tile.vector.labeling.LabelLayer;
import org.oscim.theme.InternalRenderTheme;
import org.oscim.tiling.source.mapfile.MapFileTileSource;
import org.oscim.tiling.source.mapfile.MapInfo;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MapsforgeMapActivity extends MapActivity {
private static final int SELECT_MAP_FILE = 0;
MapView mMapView;
MapFileTileSource mTileSource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mMapView = (MapView) findViewById(R.id.mapView);
registerMapView(mMapView);
startActivityForResult(new Intent(this, MapFilePicker.class),
SELECT_MAP_FILE);
}
public static class MapFilePicker extends FilePicker {
public MapFilePicker() {
setFileDisplayFilter(new FilterByFileExtension(".map"));
setFileSelectFilter(new ValidMapFile());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.theme_menu, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.theme_default:
mMap.setTheme(InternalRenderTheme.DEFAULT);
item.setChecked(true);
return true;
case R.id.theme_tubes:
mMap.setTheme(InternalRenderTheme.TRONRENDER);
item.setChecked(true);
return true;
case R.id.theme_osmarender:
mMap.setTheme(InternalRenderTheme.OSMARENDER);
item.setChecked(true);
return true;
}
return false;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == SELECT_MAP_FILE) {
if (resultCode != RESULT_OK || intent == null)
return;
if (intent.getStringExtra(FilePicker.SELECTED_FILE) == null)
return;
mTileSource = new MapFileTileSource();
String file = intent.getStringExtra(FilePicker.SELECTED_FILE);
if (mTileSource.setMapFile(file)) {
VectorTileLayer l = mMap.setBaseMap(mTileSource);
mMap.setTheme(InternalRenderTheme.DEFAULT);
mMap.getLayers().add(new LabelLayer(mMap, l.getTileRenderer()));
MapInfo info = mTileSource.getMapInfo();
if (info.boundingBox != null) {
MapPosition pos = new MapPosition();
pos.setByBoundingBox(info.boundingBox,
Tile.SIZE * 4,
Tile.SIZE * 4);
mMap.setMapPosition(pos);
Samples.log.debug("set position {}",pos);
} else if (info.mapCenter != null) {
double scale = 1 << 8;
if (info.startZoomLevel != null)
scale = 1 << info.startZoomLevel.intValue();
mMap.setMapPosition(info.mapCenter.getLatitude(),
info.mapCenter.getLongitude(),
scale);
}
}
}
}
}

View File

@ -17,6 +17,9 @@
*/
package org.oscim.android.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
@ -29,6 +32,8 @@ import android.widget.LinearLayout;
* A simple start screen for the sample activities.
*/
public class Samples extends Activity {
public static Logger log = LoggerFactory.getLogger(Samples.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -37,6 +42,7 @@ public class Samples extends Activity {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.samples);
linearLayout.addView(createButton(SimpleMapActivity.class));
linearLayout.addView(createButton(BitmapTileMapActivity.class));
linearLayout.addView(createButton(MapsforgeMapActivity.class));
linearLayout.addView(createButton(PathOverlayActivity.class));
linearLayout.addView(createButton(MarkerOverlayActivity.class));
}