- extract App into separate project, make VectorTileMap a library project

- added minimum MapActivity example
This commit is contained in:
Hannes Janetzek
2012-10-28 17:28:24 +01:00
parent ab5962d56c
commit 5916f1be5e
120 changed files with 0 additions and 7694 deletions

View File

@@ -1,38 +0,0 @@
/*
* Copyright 2012 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.app;
import org.oscim.view.MapView;
import android.app.Application;
import android.content.res.Resources;
// see http://androidcookbook.com : Android's Application Object as a "Singleton"
public class App extends Application {
public static final String TAG = "TileMap";
static MapView map;
static POISearch poiSearch;
static Resources res;
@Override
public void onCreate() {
super.onCreate();
res = getResources();
// Log.d("...", ">>>>>>>> INIT <<<<<<<");
// sInstance = this;
// sInstance.initializeInstance();
}
}

View File

@@ -1,65 +0,0 @@
/*
* 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.app;
import java.text.DecimalFormat;
import android.content.res.Resources;
final class FileUtils {
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.00 ");
private static final double ONE_GIGABYTE = 1000000000;
private static final double ONE_KILOBYTE = 1000;
private static final double ONE_MEGABYTE = 1000000;
/**
* Formats the given file size as a human readable string, using SI prefixes.
*
* @param fileSize
* the file size to be formatted.
* @param resources
* a reference to the application resources.
* @return a human readable file size.
* @throws IllegalArgumentException
* if the given file size is negative.
*/
static String formatFileSize(long fileSize, Resources resources) {
if (fileSize < 0) {
throw new IllegalArgumentException("invalid file size: " + fileSize);
} else if (fileSize < 1000) {
if (fileSize == 1) {
// singular
return "1 " + resources.getString(R.string.file_size_byte);
}
// plural, including zero
return fileSize + " " + resources.getString(R.string.file_size_bytes);
} else {
if (fileSize < ONE_MEGABYTE) {
return DECIMAL_FORMAT.format(fileSize / ONE_KILOBYTE)
+ resources.getString(R.string.file_size_kb);
} else if (fileSize < ONE_GIGABYTE) {
return DECIMAL_FORMAT.format(fileSize / ONE_MEGABYTE)
+ resources.getString(R.string.file_size_mb);
}
return DECIMAL_FORMAT.format(fileSize / ONE_GIGABYTE)
+ resources.getString(R.string.file_size_gb);
}
}
private FileUtils() {
throw new IllegalStateException();
}
}

View File

@@ -1,47 +0,0 @@
/*
* 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.app;
import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.WindowManager;
import android.webkit.WebView;
/**
* Simple activity to display the info web page from the assets folder.
*/
public class InfoView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
webView.loadUrl("file:///android_asset/info.xml");
setContentView(webView);
}
@Override
protected void onResume() {
super.onResume();
// check if the full screen mode should be activated
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("fullscreen", false)) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
}
}

View File

@@ -1,113 +0,0 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
* Copyright 2012 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.app;
import org.oscim.core.GeoPoint;
import org.oscim.core.MapPosition;
import org.oscim.view.MapView;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
public class LocationDialog {
void prepareDialog(MapView mapView, final Dialog dialog) {
EditText editText = (EditText) dialog.findViewById(R.id.latitude);
GeoPoint mapCenter = mapView.getMapPosition().getMapCenter();
editText.setText(Double.toString(mapCenter.getLatitude()));
editText = (EditText) dialog.findViewById(R.id.longitude);
editText.setText(Double.toString(mapCenter.getLongitude()));
SeekBar zoomlevel = (SeekBar) dialog.findViewById(R.id.zoomLevel);
zoomlevel.setMax(20); // FIXME
// map.getMapGenerator().getZoomLevelMax());
zoomlevel.setProgress(mapView.getMapPosition().getZoomLevel());
final TextView textView = (TextView) dialog.findViewById(R.id.zoomlevelValue);
textView.setText(String.valueOf(zoomlevel.getProgress()));
zoomlevel.setOnSeekBarChangeListener(new SeekBarChangeListener(textView));
}
Dialog createDialog(final TileMap map) {
AlertDialog.Builder builder = new AlertDialog.Builder(map);
builder.setIcon(android.R.drawable.ic_menu_mylocation);
builder.setTitle(R.string.menu_position_enter_coordinates);
LayoutInflater factory = LayoutInflater.from(map);
final View view = factory.inflate(R.layout.dialog_enter_coordinates, null);
builder.setView(view);
builder.setPositiveButton(R.string.go_to_position,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// disable GPS follow mode if it is enabled
map.mLocation.disableSnapToLocation(true);
// set the map center and zoom level
EditText latitudeView = (EditText) view
.findViewById(R.id.latitude);
EditText longitudeView = (EditText) view
.findViewById(R.id.longitude);
double latitude = Double.parseDouble(latitudeView.getText()
.toString());
double longitude = Double.parseDouble(longitudeView.getText()
.toString());
SeekBar zoomLevelView = (SeekBar) view
.findViewById(R.id.zoomLevel);
byte zoom = (byte) (zoomLevelView.getProgress());
MapPosition mapPosition = new MapPosition(latitude,
longitude, zoom, 1, 0);
map.map.setMapCenter(mapPosition);
}
});
builder.setNegativeButton(R.string.cancel, null);
return builder.create();
}
class SeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {
private final TextView textView;
SeekBarChangeListener(TextView textView) {
this.textView = textView;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
this.textView.setText(String.valueOf(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// do nothing
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// do nothing
}
}
}

View File

@@ -1,258 +0,0 @@
/*
* 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.app;
import org.oscim.core.GeoPoint;
import org.oscim.core.MapPosition;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;
public class LocationHandler {
private static final int DIALOG_LOCATION_PROVIDER_DISABLED = 2;
private MyLocationListener mLocationListener;
private LocationManager mLocationManager;
private boolean mShowMyLocation;
private ToggleButton mSnapToLocationView;
private boolean mSnapToLocation;
/* package */final TileMap mTileMap;
LocationHandler(TileMap tileMap) {
mTileMap = tileMap;
mLocationManager = (LocationManager) tileMap
.getSystemService(Context.LOCATION_SERVICE);
mLocationListener = new MyLocationListener();
mSnapToLocationView = (ToggleButton) tileMap
.findViewById(R.id.snapToLocationView);
mSnapToLocationView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (isSnapToLocationEnabled()) {
disableSnapToLocation(true);
} else {
enableSnapToLocation(true);
}
}
});
}
boolean enableShowMyLocation(boolean centerAtFirstFix) {
Log.d("TileMap", "enableShowMyLocation " + mShowMyLocation);
gotoLastKnownPosition();
if (!mShowMyLocation) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestProvider = mLocationManager.getBestProvider(criteria, true);
if (bestProvider == null) {
mTileMap.showDialog(DIALOG_LOCATION_PROVIDER_DISABLED);
return false;
}
mShowMyLocation = true;
Log.d("TileMap", "enableShowMyLocation " + mShowMyLocation);
mLocationListener.setFirstCenter(centerAtFirstFix);
mLocationManager.requestLocationUpdates(bestProvider, 1000, 0,
mLocationListener);
mSnapToLocationView.setVisibility(View.VISIBLE);
return true;
}
return false;
}
void gotoLastKnownPosition() {
Location currentLocation;
Location bestLocation = null;
for (String provider : mLocationManager.getProviders(true)) {
currentLocation = mLocationManager.getLastKnownLocation(provider);
if (currentLocation == null)
continue;
if (bestLocation == null
|| currentLocation.getAccuracy() < bestLocation.getAccuracy()) {
bestLocation = currentLocation;
}
}
if (bestLocation != null) {
byte zoom = mTileMap.map.getMapPosition().getZoomLevel();
if (zoom < 12)
zoom = (byte) 12;
MapPosition mapPosition = new MapPosition(bestLocation.getLatitude(),
bestLocation.getLongitude(), zoom, 1, 0);
mTileMap.map.setMapCenter(mapPosition);
} else {
mTileMap.showToastOnUiThread(mTileMap
.getString(R.string.error_last_location_unknown));
}
}
/**
* Disables the "show my location" mode.
*
* @return ...
*/
boolean disableShowMyLocation() {
if (mShowMyLocation) {
mShowMyLocation = false;
disableSnapToLocation(false);
mLocationManager.removeUpdates(mLocationListener);
// if (circleOverlay != null) {
// mapView.getOverlays().remove(circleOverlay);
// mapView.getOverlays().remove(itemizedOverlay);
// circleOverlay = null;
// itemizedOverlay = null;
// }
mSnapToLocationView.setVisibility(View.GONE);
return true;
}
return false;
}
/**
* Returns the status of the "show my location" mode.
*
* @return true if the "show my location" mode is enabled, false otherwise.
*/
boolean isShowMyLocationEnabled() {
return mShowMyLocation;
}
/**
* Disables the "snap to location" mode.
*
* @param showToast
* defines whether a toast message is displayed or not.
*/
void disableSnapToLocation(boolean showToast) {
if (mSnapToLocation) {
mSnapToLocation = false;
mSnapToLocationView.setChecked(false);
mTileMap.map.setClickable(true);
if (showToast) {
mTileMap.showToastOnUiThread(mTileMap
.getString(R.string.snap_to_location_disabled));
}
}
}
/**
* Enables the "snap to location" mode.
*
* @param showToast
* defines whether a toast message is displayed or not.
*/
void enableSnapToLocation(boolean showToast) {
if (!mSnapToLocation) {
mSnapToLocation = true;
mTileMap.map.setClickable(false);
if (showToast) {
mTileMap.showToastOnUiThread(mTileMap
.getString(R.string.snap_to_location_enabled));
}
}
}
/**
* Returns the status of the "snap to location" mode.
*
* @return true if the "snap to location" mode is enabled, false otherwise.
*/
boolean isSnapToLocationEnabled() {
return mSnapToLocation;
}
class MyLocationListener implements LocationListener {
private boolean mSetCenter;
@Override
public void onLocationChanged(Location location) {
Log.d("LocationListener", "onLocationChanged, "
+ " lon:" + location.getLongitude()
+ " lat:" + location.getLatitude());
if (!isShowMyLocationEnabled()) {
return;
}
GeoPoint point = new GeoPoint(location.getLatitude(), location.getLongitude());
// this.advancedMapViewer.overlayCircle.setCircleData(point, location.getAccuracy());
// this.advancedMapViewer.overlayItem.setPoint(point);
// this.advancedMapViewer.circleOverlay.requestRedraw();
// this.advancedMapViewer.itemizedOverlay.requestRedraw();
if (mSetCenter || isSnapToLocationEnabled()) {
mSetCenter = false;
mTileMap.map.setCenter(point);
}
}
@Override
public void onProviderDisabled(String provider) {
// do nothing
}
@Override
public void onProviderEnabled(String provider) {
// do nothing
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// do nothing
}
boolean isFirstCenter() {
return mSetCenter;
}
void setFirstCenter(boolean center) {
mSetCenter = center;
}
}
}

View File

@@ -1,96 +0,0 @@
/*
* 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.app;
public class MapInfoDialog {
//onCreate
// else if (id == DIALOG_INFO_MAP_FILE) {
// builder.setIcon(android.R.drawable.ic_menu_info_details);
// builder.setTitle(R.string.menu_info_map_file);
// LayoutInflater factory = LayoutInflater.from(this);
// builder.setView(factory.inflate(R.layout.dialog_info_map_file, null));
// builder.setPositiveButton(R.string.ok, null);
// return builder.create();
//onPrepare
// } else if (id == DIALOG_INFO_MAP_FILE) {
// MapInfo mapInfo = map.getMapDatabase().getMapInfo();
//
// TextView textView = (TextView)
// dialog.findViewById(R.id.infoMapFileViewName);
// textView.setText(map.getMapFile());
//
// textView = (TextView)
// dialog.findViewById(R.id.infoMapFileViewSize);
// textView.setText(FileUtils.formatFileSize(mapInfo.fileSize,
// getResources()));
//
// textView = (TextView)
// dialog.findViewById(R.id.infoMapFileViewVersion);
// textView.setText(String.valueOf(mapInfo.fileVersion));
//
// // textView = (TextView)
// dialog.findViewById(R.id.infoMapFileViewDebug);
// // if (mapFileInfo.debugFile) {
// // textView.setText(R.string.info_map_file_debug_yes);
// // } else {
// // textView.setText(R.string.info_map_file_debug_no);
// // }
//
// textView = (TextView)
// dialog.findViewById(R.id.infoMapFileViewDate);
// Date date = new Date(mapInfo.mapDate);
// textView.setText(DateFormat.getDateTimeInstance().format(date));
//
// textView = (TextView)
// dialog.findViewById(R.id.infoMapFileViewArea);
// BoundingBox boundingBox = mapInfo.boundingBox;
// textView.setText(boundingBox.getMinLatitude() + ", "
// + boundingBox.getMinLongitude() + " - \n"
// + boundingBox.getMaxLatitude() + ", " +
// boundingBox.getMaxLongitude());
//
// textView = (TextView)
// dialog.findViewById(R.id.infoMapFileViewStartPosition);
// GeoPoint startPosition = mapInfo.startPosition;
// if (startPosition == null) {
// textView.setText(null);
// } else {
// textView.setText(startPosition.getLatitude() + ", "
// + startPosition.getLongitude());
// }
//
// textView = (TextView)
// dialog.findViewById(R.id.infoMapFileViewStartZoomLevel);
// Byte startZoomLevel = mapInfo.startZoomLevel;
// if (startZoomLevel == null) {
// textView.setText(null);
// } else {
// textView.setText(startZoomLevel.toString());
// }
//
// textView = (TextView) dialog
// .findViewById(R.id.infoMapFileViewLanguagePreference);
// textView.setText(mapInfo.languagePreference);
//
// textView = (TextView)
// dialog.findViewById(R.id.infoMapFileViewComment);
// textView.setText(mapInfo.comment);
//
// textView = (TextView)
// dialog.findViewById(R.id.infoMapFileViewCreatedBy);
// textView.setText(mapInfo.createdBy);
}

View File

@@ -1,261 +0,0 @@
/*
* Copyright 2012 osmdroidbonuspack: M.Kergall
* Copyright 2012 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.app;
import java.util.ArrayList;
import org.osmdroid.location.POI;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
/**
* Activity showing POIs as a list.
* @author M.Kergall
*/
public class POIActivity extends Activity {
AutoCompleteTextView poiTagText;
POIAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.items_list);
// TextView title = (TextView) findViewById(R.id.title);
// title.setText("Points of Interest");
ListView list = (ListView) findViewById(R.id.items);
Intent myIntent = getIntent();
final ArrayList<POI> pois = myIntent.getParcelableArrayListExtra("POI");
final int currentNodeId = myIntent.getIntExtra("ID", -1);
POIAdapter adapter = new POIAdapter(this, pois);
mAdapter = adapter;
// Log.d(App.TAG, "got POIs:" + (pois == null ? pois : pois.size()));
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long index) {
Intent intent = new Intent();
intent.putExtra("ID", position);
setResult(RESULT_OK, intent);
finish();
}
});
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
Log.d(App.TAG, "context menu created 2");
}
});
list.setAdapter(adapter);
list.setSelection(currentNodeId);
// POI search interface:
String[] poiTags = getResources().getStringArray(R.array.poi_tags);
poiTagText = (AutoCompleteTextView) findViewById(R.id.poiTag);
ArrayAdapter<String> textadapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, poiTags);
poiTagText.setAdapter(textadapter);
Button setPOITagButton = (Button) findViewById(R.id.buttonSetPOITag);
setPOITagButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Hide the soft keyboard:
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(poiTagText.getWindowToken(), 0);
//Start search:
App.poiSearch.getPOIAsync(poiTagText.getText().toString());
}
});
registerForContextMenu(list);
// only show keyboard when nothing in the list yet
if (pois == null || pois.size() == 0) {
poiTagText.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(poiTagText, 0);
}
}, 200);
}
}
@Override
protected void onNewIntent(Intent intent) {
Log.d(App.TAG, "NEW INTENT!!!!");
// from SearchableDictionary Example:
// Because this activity has set launchMode="singleTop", the system calls this method
// to deliver the intent if this activity is currently the foreground activity when
// invoked again (when the user executes a search from this activity, we don't create
// a new instance of this activity, so the system delivers the search intent here)
// handleIntent(intent);
final ArrayList<POI> pois = intent.getParcelableArrayListExtra("POI");
// final int currentNodeId = intent.getIntExtra("ID", -1);
// POIAdapter adapter = new POIAdapter(this, pois);
mAdapter.setPOI(pois);
mAdapter.notifyDataSetChanged();
}
// http://www.mikeplate.com/2010/01/21/show-a-context-menu-for-long-clicks-in-an-android-listview/
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (v.getId() == R.id.items) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
Log.d(App.TAG, "list context menu created " + info.position);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.poi_menu, menu);
}
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
Log.d(App.TAG, "context menu item selected " + item.getItemId());
if (item.getItemId() == R.id.menu_link) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
POI poi = (POI) mAdapter.getItem(info.position);
if (poi != null && poi.url != null) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(poi.url));
startActivity(i);
}
return true;
}
return super.onContextItemSelected(item);
}
}
class POIAdapter extends BaseAdapter implements OnClickListener {
private Context mContext;
private ArrayList<POI> mPois;
public POIAdapter(Context context, ArrayList<POI> pois) {
mContext = context;
mPois = pois;
}
public void setPOI(ArrayList<POI> pois) {
mPois = pois;
}
@Override
public int getCount() {
if (mPois == null)
return 0;
return mPois.size();
}
@Override
public Object getItem(int position) {
if (mPois == null)
return null;
return mPois.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
POI entry = (POI) getItem(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_layout, null);
} else {
}
TextView tvTitle = (TextView) convertView.findViewById(R.id.title);
tvTitle.setText((entry.url == null ? "" : "[link] ") + entry.type);
TextView tvDetails = (TextView) convertView.findViewById(R.id.details);
tvDetails.setText(entry.description);
ImageView iv = (ImageView) convertView.findViewById(R.id.thumbnail);
//ivManeuver.setImageBitmap(entry.mThumbnail);
// iv.getT
// entry.fetchThumbnailOnThread(iv);
entry.fetchThumbnail(iv);
return convertView;
}
@Override
public void onClick(View arg0) {
Log.d(App.TAG, "click" + arg0.getId());
//nothing to do.
}
}

View File

@@ -1,197 +0,0 @@
/*
* Copyright 2012 osmdroid: M.Kergall
* Copyright 2012 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.app;
import java.util.ArrayList;
import org.oscim.core.BoundingBox;
import org.oscim.overlay.OverlayItem;
import org.osmdroid.location.FlickrPOIProvider;
import org.osmdroid.location.GeoNamesPOIProvider;
import org.osmdroid.location.NominatimPOIProvider;
import org.osmdroid.location.POI;
import org.osmdroid.location.PicasaPOIProvider;
import org.osmdroid.overlays.ExtendedOverlayItem;
import org.osmdroid.overlays.ItemizedOverlayWithBubble;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
public class POISearch {
ArrayList<POI> mPOIs;
ItemizedOverlayWithBubble<ExtendedOverlayItem> poiMarkers;
AutoCompleteTextView poiTagText;
final TileMap tileMap;
POISearch(TileMap tileMap) {
this.tileMap = tileMap;
//POI markers:
final ArrayList<ExtendedOverlayItem> poiItems = new ArrayList<ExtendedOverlayItem>();
poiMarkers = new ItemizedOverlayWithBubble<ExtendedOverlayItem>(tileMap.map, tileMap,
poiItems, null); //new POIInfoWindow(map));
tileMap.map.getOverlays().add(poiMarkers);
// if (savedInstanceState != null) {
// mPOIs = savedInstanceState.getParcelableArrayList("poi");
// updateUIWithPOI(mPOIs);
// }
}
// void init() {
//
// }
class POITask extends AsyncTask<Object, Void, ArrayList<POI>> {
String mTag;
@Override
protected ArrayList<POI> doInBackground(Object... params) {
mTag = (String) params[0];
if (mTag == null || mTag.equals("")) {
return null;
} else if (mTag.equals("wikipedia")) {
GeoNamesPOIProvider poiProvider = new GeoNamesPOIProvider("mkergall");
//ArrayList<POI> pois = poiProvider.getPOICloseTo(point, 30, 20.0);
//Get POI inside the bounding box of the current map view:
BoundingBox bb = App.map.getBoundingBox();
ArrayList<POI> pois = poiProvider.getPOIInside(bb, 30);
return pois;
} else if (mTag.equals("flickr")) {
FlickrPOIProvider poiProvider = new FlickrPOIProvider(
"c39be46304a6c6efda8bc066c185cd7e");
BoundingBox bb = App.map.getBoundingBox();
ArrayList<POI> pois = poiProvider.getPOIInside(bb, 20);
return pois;
} else if (mTag.startsWith("picasa")) {
PicasaPOIProvider poiProvider = new PicasaPOIProvider(null);
BoundingBox bb = App.map.getBoundingBox();
String q = mTag.substring("picasa".length());
ArrayList<POI> pois = poiProvider.getPOIInside(bb, 20, q);
return pois;
}
else {
NominatimPOIProvider poiProvider = new NominatimPOIProvider();
// poiProvider.setService(NominatimPOIProvider.MAPQUEST_POI_SERVICE);
poiProvider.setService(NominatimPOIProvider.NOMINATIM_POI_SERVICE);
ArrayList<POI> pois;
// if (destinationPoint == null) {
BoundingBox bb = App.map.getBoundingBox();
pois = poiProvider.getPOIInside(bb, mTag, 100);
// } else {
// pois = poiProvider.getPOIAlong(mRoad.getRouteLow(), mTag, 100, 2.0);
// }
return pois;
}
}
@Override
protected void onPostExecute(ArrayList<POI> pois) {
mPOIs = pois;
if (mTag.equals("")) {
//no search, no message
} else if (mPOIs == null) {
Toast.makeText(tileMap.getApplicationContext(),
"Technical issue when getting " + mTag + " POI.", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(tileMap.getApplicationContext(),
"" + mPOIs.size() + " " + mTag + " entries found",
Toast.LENGTH_LONG).show();
// if (mTag.equals("flickr") || mTag.startsWith("picasa") || mTag.equals("wikipedia"))
// startAsyncThumbnailsLoading(mPOIs);
}
updateUIWithPOI(mPOIs);
}
}
void updateUIWithPOI(ArrayList<POI> pois) {
if (pois != null) {
for (POI poi : pois) {
int end = 0, first = 0;
String desc = null;
String name = poi.description;
if (poi.serviceId == POI.POI_SERVICE_NOMINATIM) {
if (name != null) {
// FIXME or nominatim...
// String name = "";
for (int i = 0; i < 3; i++) {
int pos = poi.description.indexOf(',', end);
if (pos > 0) {
if (i == 0) {
name = poi.description.substring(0, pos);
first = pos + 2;
}
end = pos + 1;
}
}
if (end > 0)
desc = poi.description.substring(first, end - 1);
else
desc = poi.description;
}
}
ExtendedOverlayItem poiMarker = new ExtendedOverlayItem(
poi.type + (name == null ? "" : ": " + name), desc, poi.location);
Drawable marker = null;
if (poi.serviceId == POI.POI_SERVICE_NOMINATIM) {
marker = App.res.getDrawable(R.drawable.marker_poi_default);
} else if (poi.serviceId == POI.POI_SERVICE_GEONAMES_WIKIPEDIA) {
if (poi.rank < 90)
marker = App.res.getDrawable(R.drawable.marker_poi_wikipedia_16);
else
marker = App.res.getDrawable(R.drawable.marker_poi_wikipedia_32);
} else if (poi.serviceId == POI.POI_SERVICE_FLICKR) {
marker = App.res.getDrawable(R.drawable.marker_poi_flickr);
} else if (poi.serviceId == POI.POI_SERVICE_PICASA) {
marker = App.res.getDrawable(R.drawable.marker_poi_picasa_24);
poiMarker.setSubDescription(poi.category);
}
poiMarker.setMarker(marker);
poiMarker.setMarkerHotspot(OverlayItem.HotspotPlace.CENTER);
//thumbnail loading moved in POIInfoWindow.onOpen for better performances.
poiMarker.setRelatedObject(poi);
poiMarkers.addItem(poiMarker);
}
Log.d(App.TAG, "SEND INTENT");
Intent intent = new Intent(tileMap.getApplicationContext(), POIActivity.class);
intent.putParcelableArrayListExtra("POI", mPOIs);
intent.putExtra("ID", poiMarkers.getBubbledItemId());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
tileMap.startActivityForResult(intent, TileMap.POIS_REQUEST);
}
App.map.redrawMap();
}
void getPOIAsync(String tag) {
poiMarkers.removeAllItems();
new POITask().execute(tag);
}
}

View File

@@ -1,403 +0,0 @@
/*
* Copyright 2012 osmdroid: M.Kergall
* Copyright 2012 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.app;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.oscim.core.GeoPoint;
import org.oscim.overlay.Overlay;
import org.oscim.overlay.OverlayItem;
import org.oscim.overlay.PathOverlay;
import org.oscim.view.MapView;
import org.osmdroid.location.GeocoderNominatim;
import org.osmdroid.overlays.DefaultInfoWindow;
import org.osmdroid.overlays.ExtendedOverlayItem;
import org.osmdroid.overlays.ItemizedOverlayWithBubble;
import org.osmdroid.overlays.MapEventsOverlay;
import org.osmdroid.overlays.MapEventsReceiver;
import org.osmdroid.routing.OSRMRoadManager;
import org.osmdroid.routing.Road;
import org.osmdroid.routing.RoadManager;
import org.osmdroid.routing.RoadNode;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.os.AsyncTask;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;
public class RouteSearch implements MapEventsReceiver {
protected Road mRoad;
protected PathOverlay mRoadOverlay;
protected ItemizedOverlayWithBubble<ExtendedOverlayItem> mRoadNodeMarkers;
protected GeoPoint startPoint, destinationPoint;
protected ArrayList<GeoPoint> viaPoints;
protected static int START_INDEX = -2, DEST_INDEX = -1;
protected ExtendedOverlayItem markerStart, markerDestination;
protected ItemizedOverlayWithBubble<ExtendedOverlayItem> itineraryMarkers;
private final TileMap tileMap;
RouteSearch(TileMap tileMap) {
// keep context and mapview reference
this.tileMap = tileMap;
//To use MapEventsReceiver methods, we add a MapEventsOverlay:
MapEventsOverlay overlay = new MapEventsOverlay(tileMap, this);
tileMap.map.getOverlays().add(overlay);
// if (savedInstanceState == null) {
// Location l = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// if (l != null) {
// startPoint = new GeoPoint(l.getLatitude(), l.getLongitude());
// } else {
// //we put a hard-coded start
startPoint = new GeoPoint(53.067221, 8.78767);
// }
destinationPoint = null;
viaPoints = new ArrayList<GeoPoint>();
// mapController.setZoom(9);
// mapController.setCenter(startPoint);
// }
// Itinerary markers:
final ArrayList<ExtendedOverlayItem> waypointsItems = new ArrayList<ExtendedOverlayItem>();
itineraryMarkers = new ItemizedOverlayWithBubble<ExtendedOverlayItem>(tileMap.map, tileMap,
waypointsItems, new ViaPointInfoWindow(R.layout.itinerary_bubble, tileMap.map));
tileMap.map.getOverlays().add(itineraryMarkers);
updateUIWithItineraryMarkers();
//Route and Directions
final ArrayList<ExtendedOverlayItem> roadItems = new ArrayList<ExtendedOverlayItem>();
mRoadNodeMarkers = new ItemizedOverlayWithBubble<ExtendedOverlayItem>(tileMap, roadItems,
tileMap.map);
tileMap.map.getOverlays().add(mRoadNodeMarkers);
// if (savedInstanceState != null) {
// mRoad = savedInstanceState.getParcelable("road");
// updateUIWithRoad(mRoad);
// }
}
//------------- Geocoding and Reverse Geocoding
/**
* Reverse Geocoding
* @param p
* ...
* @return ...
*/
public String getAddress(GeoPoint p) {
GeocoderNominatim geocoder = new GeocoderNominatim(tileMap);
String theAddress;
try {
double dLatitude = p.getLatitude();
double dLongitude = p.getLongitude();
List<Address> addresses = geocoder.getFromLocation(dLatitude, dLongitude, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
int n = address.getMaxAddressLineIndex();
for (int i = 0; i <= n; i++) {
if (i != 0)
sb.append(", ");
sb.append(address.getAddressLine(i));
}
theAddress = new String(sb.toString());
} else {
theAddress = null;
}
} catch (IOException e) {
theAddress = null;
}
if (theAddress != null) {
return theAddress;
}
return "";
}
//Async task to reverse-geocode the marker position in a separate thread:
class GeocodingTask extends AsyncTask<Object, Void, String> {
ExtendedOverlayItem marker;
@Override
protected String doInBackground(Object... params) {
marker = (ExtendedOverlayItem) params[0];
return getAddress(marker.getPoint());
}
@Override
protected void onPostExecute(String result) {
marker.setDescription(result);
//itineraryMarkers.showBubbleOnItem(???, map); //open bubble on the item
}
}
//------------ Itinerary markers
/* add (or replace) an item in markerOverlays. p position. */
public ExtendedOverlayItem putMarkerItem(ExtendedOverlayItem item, GeoPoint p, int index,
int titleResId, int markerResId, int iconResId) {
if (item != null) {
itineraryMarkers.removeItem(item);
}
Drawable marker = App.res.getDrawable(markerResId);
String title = App.res.getString(titleResId);
ExtendedOverlayItem overlayItem = new ExtendedOverlayItem(title, "", p);
overlayItem.setMarkerHotspot(OverlayItem.HotspotPlace.BOTTOM_CENTER);
overlayItem.setMarker(marker);
if (iconResId != -1)
overlayItem.setImage(App.res.getDrawable(iconResId));
overlayItem.setRelatedObject(Integer.valueOf(index));
itineraryMarkers.addItem(overlayItem);
tileMap.map.redrawMap();
//map.invalidate();
//Start geocoding task to update the description of the marker with its address:
new GeocodingTask().execute(overlayItem);
return overlayItem;
}
public void addViaPoint(GeoPoint p) {
viaPoints.add(p);
putMarkerItem(null, p, viaPoints.size() - 1,
R.string.viapoint, R.drawable.marker_via, -1);
}
public void removePoint(int index) {
if (index == START_INDEX)
startPoint = null;
else if (index == DEST_INDEX)
destinationPoint = null;
else
viaPoints.remove(index);
getRoadAsync();
updateUIWithItineraryMarkers();
}
public void updateUIWithItineraryMarkers() {
itineraryMarkers.removeAllItems();
//Start marker:
if (startPoint != null) {
markerStart = putMarkerItem(null, startPoint, START_INDEX,
R.string.departure, R.drawable.marker_departure, -1);
}
//Via-points markers if any:
for (int index = 0; index < viaPoints.size(); index++) {
putMarkerItem(null, viaPoints.get(index), index,
R.string.viapoint, R.drawable.marker_via, -1);
}
//Destination marker if any:
if (destinationPoint != null) {
markerDestination = putMarkerItem(null, destinationPoint, DEST_INDEX,
R.string.destination,
R.drawable.marker_destination, -1);
}
}
//------------ Route and Directions
private void putRoadNodes(Road road) {
mRoadNodeMarkers.removeAllItems();
Drawable marker = App.res.getDrawable(R.drawable.marker_node);
int n = road.nodes.size();
// TypedArray iconIds = App.res.obtainTypedArray(R.array.direction_icons);
for (int i = 0; i < n; i++) {
RoadNode node = road.nodes.get(i);
String instructions = (node.instructions == null ? "" : node.instructions);
ExtendedOverlayItem nodeMarker = new ExtendedOverlayItem(
"Step " + (i + 1), instructions, node.location);
nodeMarker.setSubDescription(road.getLengthDurationText(node.length, node.duration));
nodeMarker.setMarkerHotspot(OverlayItem.HotspotPlace.CENTER);
nodeMarker.setMarker(marker);
// int iconId = iconIds.getResourceId(node.mManeuverType, R.drawable.ic_empty);
// if (iconId != R.drawable.ic_empty) {
// Drawable icon = App.res.getDrawable(iconId);
// nodeMarker.setImage(icon);
// }
mRoadNodeMarkers.addItem(nodeMarker);
}
}
void updateUIWithRoad(Road road) {
mRoadNodeMarkers.removeAllItems();
List<Overlay> mapOverlays = tileMap.map.getOverlays();
if (mRoadOverlay != null) {
mapOverlays.remove(mRoadOverlay);
}
if (road == null)
return;
if (road.status == Road.STATUS_DEFAULT)
Toast.makeText(tileMap, "We have a problem to get the route",
Toast.LENGTH_SHORT).show();
mRoadOverlay = RoadManager.buildRoadOverlay(tileMap.map, road, tileMap);
Overlay removedOverlay = mapOverlays.set(1, mRoadOverlay);
//we set the road overlay at the "bottom", just above the MapEventsOverlay,
//to avoid covering the other overlays.
mapOverlays.add(removedOverlay);
putRoadNodes(road);
tileMap.map.redrawMap();
// map.invalidate();
//Set route info in the text view:
// ((TextView) findViewById(R.id.routeInfo)).setText(road.getLengthDurationText(-1));
}
/**
* Async task to get the road in a separate thread.
*/
class UpdateRoadTask extends AsyncTask<WayPoints, Void, Road> {
@Override
protected Road doInBackground(WayPoints... wp) {
ArrayList<GeoPoint> waypoints = wp[0].waypoints;
//RoadManager roadManager = new GoogleRoadManager();
RoadManager roadManager = new OSRMRoadManager();
roadManager.addRequestOption("");
/* RoadManager roadManager = new MapQuestRoadManager(); Locale
* locale = Locale.getDefault();
* roadManager.addRequestOption("locale="+locale.getLanguage
* ()+"_"+locale.getCountry()); */
return roadManager.getRoad(waypoints);
}
@Override
protected void onPostExecute(Road result) {
mRoad = result;
updateUIWithRoad(result);
/// ??? getPOIAsync(poiTagText.getText().toString());
}
}
// Just to make JAVA shut up!
class WayPoints {
public ArrayList<GeoPoint> waypoints;
}
public void getRoadAsync() {
mRoad = null;
if (startPoint == null || destinationPoint == null) {
updateUIWithRoad(mRoad);
return;
}
ArrayList<GeoPoint> waypoints = new ArrayList<GeoPoint>(2);
waypoints.add(startPoint);
//add intermediate via points:
for (GeoPoint p : viaPoints) {
waypoints.add(p);
}
waypoints.add(destinationPoint);
WayPoints wp = new WayPoints();
wp.waypoints = waypoints;
new UpdateRoadTask().execute(wp);
}
//------------ MapEventsReceiver implementation
GeoPoint tempClickedGeoPoint; //any other way to pass the position to the menu ???
@Override
public boolean longPressHelper(GeoPoint p) {
Log.d(TileMap.TAG, ">>> got long press event " + p);
tempClickedGeoPoint = p;
//new GeoPoint((GeoPoint) p);
//menu is hooked on the "Search" button
// Button searchButton = (Button) findViewById(R.id.buttonSearch);
// tileMap.openContextMenu(searchButton);
tileMap.openContextMenu(tileMap.map);
return true;
}
@Override
public boolean singleTapUpHelper(GeoPoint p) {
Log.d(App.TAG, "single tap up");
mRoadNodeMarkers.hideBubble();
itineraryMarkers.hideBubble();
return false;
}
// ----------- context menu
boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_departure:
startPoint = tempClickedGeoPoint;
//new GeoPoint((GeoPoint) tempClickedGeoPoint);
markerStart = putMarkerItem(markerStart, startPoint, START_INDEX,
R.string.departure, R.drawable.marker_departure, -1);
getRoadAsync();
return true;
case R.id.menu_destination:
destinationPoint = tempClickedGeoPoint;
//new GeoPoint((GeoPoint) tempClickedGeoPoint);
markerDestination = putMarkerItem(markerDestination, destinationPoint, DEST_INDEX,
R.string.destination,
R.drawable.marker_destination, -1);
getRoadAsync();
return true;
case R.id.menu_viapoint:
GeoPoint viaPoint = tempClickedGeoPoint; //new GeoPoint((GeoPoint) tempClickedGeoPoint);
addViaPoint(viaPoint);
getRoadAsync();
return true;
default:
}
return false;
}
class ViaPointInfoWindow extends DefaultInfoWindow {
int mSelectedPoint;
public ViaPointInfoWindow(int layoutResId, MapView mapView) {
super(layoutResId, mapView);
// Button btnDelete = (Button)(mView.findViewById(R.id.bubble_delete));
// btnDelete.setOnClickListener(new View.OnClickListener() {
// public void onClick(View view) {
// //Call the removePoint method on MapActivity.
// //TODO: find a cleaner way to do that!
// MapActivity mapActivity = (MapActivity)view.getContext();
// mapActivity.removePoint(mSelectedPoint);
// close();
// }
// });
}
@Override
public void onOpen(ExtendedOverlayItem item) {
mSelectedPoint = ((Integer) item.getRelatedObject()).intValue();
super.onOpen(item);
}
}
}

View File

@@ -1,564 +0,0 @@
/* Copyright 2010, 2011, 2012 mapsforge.org
* Copyright 2012 Hannes Janetzek
* Copyright 2012 osmdroidbonuspack: M.Kergall
*
* 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.app;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import org.oscim.app.filefilter.FilterByFileExtension;
import org.oscim.app.filefilter.ValidRenderTheme;
import org.oscim.app.filepicker.FilePicker;
import org.oscim.app.preferences.EditPreferences;
import org.oscim.database.MapDatabases;
import org.oscim.theme.InternalRenderTheme;
import org.oscim.utils.AndroidUtils;
import org.oscim.view.DebugSettings;
import org.oscim.view.MapActivity;
import org.oscim.view.MapView;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
public class TileMap extends MapActivity {
static final String TAG = TileMap.class.getSimpleName();
MapView map;
// private static final String BUNDLE_CENTER_AT_FIRST_FIX =
// "centerAtFirstFix";
private static final String BUNDLE_SHOW_MY_LOCATION = "showMyLocation";
private static final String BUNDLE_SNAP_TO_LOCATION = "snapToLocation";
private static final int DIALOG_ENTER_COORDINATES = 0;
// private static final int DIALOG_INFO_MAP_FILE = 1;
private static final int DIALOG_LOCATION_PROVIDER_DISABLED = 2;
// private static final FileFilter FILE_FILTER_EXTENSION_MAP =
// new FilterByFileExtension(".map");
private static final FileFilter FILE_FILTER_EXTENSION_XML =
new FilterByFileExtension(".xml");
// private static final int SELECT_MAP_FILE = 0;
// Intents
private static final int SELECT_RENDER_THEME_FILE = 1;
protected static final int POIS_REQUEST = 2;
LocationHandler mLocation;
private MapDatabases mMapDatabase;
private WakeLock mWakeLock;
private Menu mMenu = null;
POISearch mPoiSearch;
RouteSearch mRouteSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set up the layout views
setContentView(R.layout.activity_tilemap);
map = (MapView) findViewById(R.id.mapView);
App.map = map;
// configure the MapView and activate the zoomLevel buttons
map.setClickable(true);
// map.setBuiltInZoomControls(true);
map.setFocusable(true);
mLocation = new LocationHandler(this);
// get the pointers to different system services
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = powerManager
.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "AMV");
if (savedInstanceState != null) {
if (savedInstanceState.getBoolean(BUNDLE_SHOW_MY_LOCATION)) {
// enableShowMyLocation(savedInstanceState
// .getBoolean(BUNDLE_CENTER_AT_FIRST_FIX));
if (savedInstanceState.getBoolean(BUNDLE_SNAP_TO_LOCATION)) {
mLocation.enableSnapToLocation(false);
}
}
}
App.poiSearch = mPoiSearch = new POISearch(this);
registerForContextMenu(map);
mRouteSearch = new RouteSearch(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
mMenu = menu;
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_info_about:
startActivity(new Intent(this, InfoView.class));
return true;
case R.id.menu_position:
return true;
case R.id.menu_rotation_enable:
map.enableRotation(true);
toggleMenuRotation();
return true;
case R.id.menu_rotation_disable:
map.enableRotation(false);
toggleMenuRotation();
return true;
case R.id.menu_compass_enable:
map.enableCompass(true);
toggleMenuRotation();
return true;
case R.id.menu_compass_disable:
map.enableCompass(false);
toggleMenuRotation();
return true;
case R.id.menu_position_my_location_enable:
toggleMenuItem(mMenu,
R.id.menu_position_my_location_enable,
R.id.menu_position_my_location_disable,
!mLocation.enableShowMyLocation(true));
return true;
case R.id.menu_position_my_location_disable:
toggleMenuItem(mMenu,
R.id.menu_position_my_location_enable,
R.id.menu_position_my_location_disable,
mLocation.disableShowMyLocation());
return true;
case R.id.menu_position_enter_coordinates:
showDialog(DIALOG_ENTER_COORDINATES);
return true;
case R.id.menu_preferences:
startActivity(new Intent(this, EditPreferences.class));
return true;
case R.id.menu_render_theme:
return true;
case R.id.menu_render_theme_osmarender:
map.setRenderTheme(InternalRenderTheme.OSMARENDER);
return true;
case R.id.menu_render_theme_tronrender:
map.setRenderTheme(InternalRenderTheme.TRONRENDER);
return true;
case R.id.menu_render_theme_select_file:
startRenderThemePicker();
return true;
// case R.id.menu_position_map_center:
// // disable GPS follow mode if it is enabled
// location.disableSnapToLocation(true);
//
// map.setCenter(map.getMapDatabase()
// .getMapInfo().mapCenter);
// return true;
// case R.id.menu_mapfile:
// startMapFilePicker();
// return true;
case R.id.menu_pois:
mPoiSearch.getPOIAsync("bar");
// Intent myIntent = new Intent(this, POIActivity.class);
// myIntent.putParcelableArrayListExtra("POI", mPOIs);
// // myIntent.putExtra("ID", poiMarkers.getBubbledItemId());
// startActivityForResult(myIntent, POIS_REQUEST);
return true;
case R.id.menu_poi_list:
Intent myIntent = new Intent(this, POIActivity.class);
myIntent.putParcelableArrayListExtra("POI", mPoiSearch.mPOIs);
myIntent.putExtra("ID", mPoiSearch.poiMarkers.getBubbledItemId());
startActivityForResult(myIntent, POIS_REQUEST);
return true;
default:
return false;
}
}
private void toggleMenuRotation() {
toggleMenuItem(mMenu,
R.id.menu_rotation_enable,
R.id.menu_rotation_disable,
!map.enableRotation);
toggleMenuItem(mMenu,
R.id.menu_compass_enable,
R.id.menu_compass_disable,
!map.enableCompass);
}
private static void toggleMenuItem(Menu menu, int id, int id2, boolean enable) {
menu.findItem(id).setVisible(enable);
menu.findItem(id).setEnabled(enable);
menu.findItem(id2).setVisible(!enable);
menu.findItem(id2).setEnabled(!enable);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (!isPreHoneyComb()) {
menu.clear();
onCreateOptionsMenu(menu);
}
toggleMenuItem(menu,
R.id.menu_position_my_location_enable,
R.id.menu_position_my_location_disable,
!mLocation.isShowMyLocationEnabled());
if (mMapDatabase == MapDatabases.MAP_READER) {
//menu.findItem(R.id.menu_mapfile).setVisible(true);
menu.findItem(R.id.menu_position_map_center).setVisible(true);
}
// else {
// menu.findItem(R.id.menu_mapfile).setVisible(false);
// menu.findItem(R.id.menu_position_map_center).setVisible(false);
// }
toggleMenuRotation();
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onTrackballEvent(MotionEvent event) {
// forward the event to the MapView
return map.onTrackballEvent(event);
}
// private void startMapFilePicker() {
// FilePicker.setFileDisplayFilter(FILE_FILTER_EXTENSION_MAP);
// FilePicker.setFileSelectFilter(new ValidMapFile());
// startActivityForResult(new Intent(this, FilePicker.class),
// SELECT_MAP_FILE);
// }
private void startRenderThemePicker() {
FilePicker.setFileDisplayFilter(FILE_FILTER_EXTENSION_XML);
FilePicker.setFileSelectFilter(new ValidRenderTheme());
startActivityForResult(new Intent(this, FilePicker.class),
SELECT_RENDER_THEME_FILE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case POIS_REQUEST:
Log.d(TAG, "result: POIS_REQUEST");
if (resultCode == RESULT_OK) {
int id = intent.getIntExtra("ID", 0);
Log.d(TAG, "result: POIS_REQUEST: " + id);
// map.getController().setCenter(mPOIs.get(id).mLocation);
mPoiSearch.poiMarkers.showBubbleOnItem(id, map);
map.getMapViewPosition().animateTo(mPoiSearch.mPOIs.get(id).location);
}
break;
case SELECT_RENDER_THEME_FILE:
if (resultCode == RESULT_OK && intent != null
&& intent.getStringExtra(FilePicker.SELECTED_FILE) != null) {
try {
map.setRenderTheme(intent
.getStringExtra(FilePicker.SELECTED_FILE));
} catch (FileNotFoundException e) {
showToastOnUiThread(e.getLocalizedMessage());
}
}
break;
default:
break;
}
// if (requestCode == SELECT_MAP_FILE) {
// if (resultCode == RESULT_OK) {
//
// location.disableSnapToLocation(true);
//
// if (intent != null) {
// if (intent.getStringExtra(FilePicker.SELECTED_FILE) != null) {
// map.setMapFile(intent
// .getStringExtra(FilePicker.SELECTED_FILE));
// }
// }
// } else if (resultCode == RESULT_CANCELED) {
// startActivity(new Intent(this, EditPreferences.class));
// }
// } else
// if (requestCode == SELECT_RENDER_THEME_FILE && resultCode == RESULT_OK
// && intent != null
// && intent.getStringExtra(FilePicker.SELECTED_FILE) != null) {
// try {
// map.setRenderTheme(intent
// .getStringExtra(FilePicker.SELECTED_FILE));
// } catch (FileNotFoundException e) {
// showToastOnUiThread(e.getLocalizedMessage());
// }
// }
}
static boolean isPreHoneyComb() {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB;
}
@Override
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
if (id == DIALOG_ENTER_COORDINATES) {
if (mLocationDialog == null)
mLocationDialog = new LocationDialog();
return mLocationDialog.createDialog(this);
} else if (id == DIALOG_LOCATION_PROVIDER_DISABLED) {
builder.setIcon(android.R.drawable.ic_menu_info_details);
builder.setTitle(R.string.error);
builder.setMessage(R.string.no_location_provider_available);
builder.setPositiveButton(R.string.ok, null);
return builder.create();
} else {
// no dialog will be created
return null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocation.disableShowMyLocation();
}
@Override
protected void onPause() {
Log.d(TAG, "onPause");
super.onPause();
// release the wake lock if necessary
if (mWakeLock.isHeld()) {
mWakeLock.release();
}
}
LocationDialog mLocationDialog;
@Override
protected void onPrepareDialog(int id, final Dialog dialog) {
if (id == DIALOG_ENTER_COORDINATES) {
mLocationDialog.prepareDialog(map, dialog);
} else {
super.onPrepareDialog(id, dialog);
}
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(this);
// MapScaleBar mapScaleBar = mapView.getMapScaleBar();
// mapScaleBar.setShowMapScaleBar(preferences.getBoolean("showScaleBar",
// false));
// String scaleBarUnitDefault =
// getString(R.string.preferences_scale_bar_unit_default);
// String scaleBarUnit = preferences.getString("scaleBarUnit",
// scaleBarUnitDefault);
// mapScaleBar.setImperialUnits(scaleBarUnit.equals("imperial"));
if (preferences.contains("mapDatabase")) {
String name = preferences.getString("mapDatabase",
MapDatabases.PBMAP_READER.name());
MapDatabases mapDatabaseNew;
try {
mapDatabaseNew = MapDatabases.valueOf(name);
} catch (IllegalArgumentException e) {
mapDatabaseNew = MapDatabases.PBMAP_READER;
}
if (mapDatabaseNew != mMapDatabase) {
Log.d(TAG, "set map database " + mapDatabaseNew);
map.setMapDatabase(mapDatabaseNew);
mMapDatabase = mapDatabaseNew;
}
}
// try {
// String textScaleDefault =
// getString(R.string.preferences_text_scale_default);
// map.setTextScale(Float.parseFloat(preferences.getString("textScale",
// textScaleDefault)));
// } catch (NumberFormatException e) {
// map.setTextScale(1);
// }
if (preferences.getBoolean("fullscreen", false)) {
Log.i("mapviewer", "FULLSCREEN");
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
Log.i("mapviewer", "NO FULLSCREEN");
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
if (preferences.getBoolean("fixOrientation", true)) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// this all returns the orientation which is not currently active?!
// getWindow().getWindowManager().getDefaultDisplay().getRotation());
// getWindow().getWindowManager().getDefaultDisplay().getOrientation());
}
if (preferences.getBoolean("wakeLock", false) && !mWakeLock.isHeld()) {
mWakeLock.acquire();
}
boolean drawTileFrames =
preferences.getBoolean("drawTileFrames", false);
boolean drawTileCoordinates =
preferences.getBoolean("drawTileCoordinates", false);
boolean disablePolygons =
preferences.getBoolean("disablePolygons", false);
boolean drawUnmatchedWays =
preferences.getBoolean("drawUnmatchedWays", false);
DebugSettings cur = map.getDebugSettings();
if (cur.mDisablePolygons != disablePolygons
|| cur.mDrawTileCoordinates != drawTileCoordinates
|| cur.mDrawTileFrames != drawTileFrames
|| cur.mDrawUnmatchted != drawUnmatchedWays) {
Log.d(TAG, "set map debug settings");
DebugSettings debugSettings = new DebugSettings(drawTileCoordinates,
drawTileFrames, disablePolygons, drawUnmatchedWays);
map.setDebugSettings(debugSettings);
}
// if (mMapDatabase == MapDatabases.MAP_READER) {
// if (map.getMapFile() == null)
// startMapFilePicker();
// } else {
// map.setMapFile(map.getMapFile());
// }
map.redrawMap();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(BUNDLE_SHOW_MY_LOCATION, mLocation.isShowMyLocationEnabled());
// outState.putBoolean(BUNDLE_CENTER_AT_FIRST_FIX,
// mMyLocationListener.isCenterAtFirstFix());
// outState.putBoolean(BUNDLE_SNAP_TO_LOCATION, mSnapToLocation);
}
/**
* Uses the UI thread to display the given text message as toast
* notification.
* @param text
* the text message to display
*/
void showToastOnUiThread(final String text) {
if (AndroidUtils.currentThreadIsUiThread()) {
Toast toast = Toast.makeText(this, text, Toast.LENGTH_LONG);
toast.show();
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast toast = Toast.makeText(TileMap.this, text, Toast.LENGTH_LONG);
toast.show();
}
});
}
}
//----------- Context Menu when clicking on the map
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
Log.d(TAG, "create context menu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.map_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
Log.d(TAG, "context menu item selected " + item.getItemId());
if (mRouteSearch.onContextItemSelected(item))
return true;
return super.onContextItemSelected(item);
}
}

View File

@@ -1,47 +0,0 @@
/*
* 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.app.filefilter;
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 extension;
/**
* @param extension
* the allowed file name extension.
*/
public FilterByFileExtension(String extension) {
this.extension = 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(this.extension)) {
return true;
}
}
return false;
}
}

View File

@@ -1,29 +0,0 @@
/*
* 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.app.filefilter;
import java.io.FileFilter;
import org.oscim.database.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

@@ -1,46 +0,0 @@
/*
* 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.app.filefilter;
import java.io.File;
import java.util.HashMap;
import org.oscim.database.IMapDatabase;
import org.oscim.database.OpenResult;
import org.oscim.database.mapfile.MapDatabase;
/**
* Accepts all valid map files.
*/
public final class ValidMapFile implements ValidFileFilter {
private OpenResult openResult;
@Override
public boolean accept(File file) {
IMapDatabase mapDatabase = new MapDatabase();
HashMap<String, String> options = new HashMap<String, String>();
options.put("mapfile", file.getAbsolutePath());
this.openResult = mapDatabase.open(options);
mapDatabase.close();
return this.openResult.isSuccess();
}
@Override
public OpenResult getFileOpenResult() {
return this.openResult;
}
}

View File

@@ -1,71 +0,0 @@
/*
* 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.app.filefilter;
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.database.OpenResult;
import org.oscim.theme.RenderThemeHandler;
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 openResult;
@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));
this.openResult = OpenResult.SUCCESS;
} catch (ParserConfigurationException e) {
this.openResult = new OpenResult(e.getMessage());
} catch (SAXException e) {
this.openResult = new OpenResult(e.getMessage());
} catch (IOException e) {
this.openResult = new OpenResult(e.getMessage());
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
this.openResult = new OpenResult(e.getMessage());
}
}
return this.openResult.isSuccess();
}
@Override
public OpenResult getFileOpenResult() {
return this.openResult;
}
}

View File

@@ -1,255 +0,0 @@
/*
* 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.app.filepicker;
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Comparator;
import org.oscim.app.R;
import org.oscim.app.filefilter.ValidFileFilter;
import android.annotation.TargetApi;
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.Build;
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 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;
private static Comparator<File> fileComparator = getDefaultFileComparator();
private static FileFilter fileDisplayFilter;
private static ValidFileFilter fileSelectFilter;
private static final String PREFERENCES_FILE = "FilePicker";
/**
* 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 static void setFileComparator(Comparator<File> fileComparator) {
FilePicker.fileComparator = 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 static void setFileDisplayFilter(FileFilter fileDisplayFilter) {
FilePicker.fileDisplayFilter = 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 static void setFileSelectFilter(ValidFileFilter fileSelectFilter) {
FilePicker.fileSelectFilter = 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 currentDirectory;
private FilePickerIconAdapter filePickerIconAdapter;
private File[] files;
private File[] filesWithParentFolder;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
File selectedFile = this.files[(int) id];
if (selectedFile.isDirectory()) {
this.currentDirectory = selectedFile;
browseToCurrentDirectory();
} else if (fileSelectFilter == null || fileSelectFilter.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(this.currentDirectory.getAbsolutePath());
// read the subfolders and files from the current directory
if (fileDisplayFilter == null) {
this.files = this.currentDirectory.listFiles();
} else {
this.files = this.currentDirectory.listFiles(fileDisplayFilter);
}
if (this.files == null) {
this.files = new File[0];
} else {
// order the subfolders and files
Arrays.sort(this.files, fileComparator);
}
// if a parent directory exists, add it at the first position
if (this.currentDirectory.getParentFile() != null) {
this.filesWithParentFolder = new File[this.files.length + 1];
this.filesWithParentFolder[0] = this.currentDirectory.getParentFile();
System.arraycopy(this.files, 0, this.filesWithParentFolder, 1,
this.files.length);
this.files = this.filesWithParentFolder;
this.filePickerIconAdapter.setFiles(this.files, true);
} else {
this.filePickerIconAdapter.setFiles(this.files, false);
}
this.filePickerIconAdapter.notifyDataSetChanged();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_picker);
this.filePickerIconAdapter = new FilePickerIconAdapter(this);
GridView gridView = (GridView) findViewById(R.id.filePickerView);
gridView.setOnItemClickListener(this);
gridView.setAdapter(this.filePickerIconAdapter);
// 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(FilePicker.fileSelectFilter.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 (this.currentDirectory != null) {
editor.putString(CURRENT_DIRECTORY, this.currentDirectory.getAbsolutePath());
}
editor.commit();
}
@TargetApi(11)
@Override
protected void onResume() {
super.onResume();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
getActionBar().hide();
// check if the full screen mode should be activated
// if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("fullscreen",
// false)) {
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// } else {
// getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// }
// restore the current directory
SharedPreferences preferences = getSharedPreferences(PREFERENCES_FILE,
MODE_PRIVATE);
this.currentDirectory = new File(preferences.getString(CURRENT_DIRECTORY,
DEFAULT_DIRECTORY));
if (!this.currentDirectory.exists() || !this.currentDirectory.canRead()) {
this.currentDirectory = new File(DEFAULT_DIRECTORY);
}
browseToCurrentDirectory();
}
}

View File

@@ -1,111 +0,0 @@
/*
* 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.app.filepicker;
import java.io.File;
import org.oscim.app.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 context;
private File currentFile;
private File[] files;
private boolean hasParentFolder;
private TextView textView;
/**
* 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();
this.context = context;
}
@Override
public int getCount() {
if (this.files == null) {
return 0;
}
return this.files.length;
}
@Override
public Object getItem(int index) {
return this.files[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
this.textView = (TextView) convertView;
} else {
// create a new view object
this.textView = new TextView(this.context);
this.textView.setLines(2);
this.textView.setGravity(Gravity.CENTER_HORIZONTAL);
this.textView.setPadding(5, 10, 5, 10);
}
if (index == 0 && this.hasParentFolder) {
// the parent directory of the current folder
this.textView.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.file_picker_back, 0, 0);
this.textView.setText("..");
} else {
this.currentFile = this.files[index];
if (this.currentFile.isDirectory()) {
this.textView.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.file_picker_folder, 0, 0);
} else {
this.textView.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.file_picker_file, 0, 0);
}
this.textView.setText(this.currentFile.getName());
}
return this.textView;
}
/**
* 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) {
this.files = files.clone();
this.hasParentFolder = newHasParentFolder;
}
}

View File

@@ -1,50 +0,0 @@
/*
* 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.app.preferences;
import org.oscim.app.R;
import android.os.Bundle;
import android.preference.PreferenceActivity;
/**
* Activity to edit the application preferences.
*/
public class EditPreferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
// @TargetApi(11)
@Override
protected void onResume() {
super.onResume();
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
// getActionBar().hide();
// check if the full screen mode should be activated
// if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("fullscreen",
// false)) {
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// } else {
// getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// }
}
}

View File

@@ -1,154 +0,0 @@
/*
* 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.app.preferences;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.DialogPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
/**
* This abstract class provides all code for a seek bar preference. Deriving
* classes only need to set the current and
* maximum value of the seek bar. An optional text message above the seek bar is
* also supported as well as an optional
* current value message below the seek bar.
*/
abstract class SeekBarPreference extends DialogPreference implements OnSeekBarChangeListener {
private TextView mCurrentValueTextView;
private Editor mEditor;
private SeekBar mPreferenceSeekBar;
/**
* How much the value should increase when the seek bar is moved.
*/
int increment = 1;
/**
* The maximum value of the seek bar.
*/
int max;
/**
* Optional text message to display on top of the seek bar.
*/
String messageText;
/**
* The SharedPreferences instance that is used.
*/
final SharedPreferences preferencesDefault;
/**
* The current value of the seek bar.
*/
int seekBarCurrentValue;
/**
* Create a new seek bar preference.
* @param context
* the context of the seek bar preferences activity.
* @param attrs
* A set of attributes (currently ignored).
*/
SeekBarPreference(Context context, AttributeSet attrs) {
super(context, attrs);
preferencesDefault = PreferenceManager.getDefaultSharedPreferences(context);
}
@Override
public void onClick(DialogInterface dialog, int which) {
// check if the "OK" button was pressed and the seek bar value has changed
if (which == DialogInterface.BUTTON_POSITIVE
&& seekBarCurrentValue != mPreferenceSeekBar.getProgress()) {
// get the value of the seek bar and save it in the preferences
seekBarCurrentValue = mPreferenceSeekBar.getProgress();
mEditor = preferencesDefault.edit();
mEditor.putInt(getKey(), seekBarCurrentValue);
mEditor.commit();
}
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mCurrentValueTextView != null) {
mCurrentValueTextView.setText(getCurrentValueText(progress));
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// do nothing
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// do nothing
}
@Override
protected View onCreateDialogView() {
// create a layout for the optional text messageText and the seek bar
LinearLayout linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setPadding(20, 10, 20, 10);
// check if a text message should appear above the seek bar
if (messageText != null) {
// create a text view for the text messageText
TextView messageTextView = new TextView(getContext());
messageTextView.setText(messageText);
messageTextView.setPadding(0, 0, 0, 20);
// add the text message view to the layout
linearLayout.addView(messageTextView);
}
// create the seek bar and set the maximum and current value
mPreferenceSeekBar = new SeekBar(getContext());
mPreferenceSeekBar.setOnSeekBarChangeListener(this);
mPreferenceSeekBar.setMax(max);
mPreferenceSeekBar.setProgress(Math.min(seekBarCurrentValue, max));
mPreferenceSeekBar.setKeyProgressIncrement(increment);
mPreferenceSeekBar.setPadding(0, 0, 0, 10);
// add the seek bar to the layout
linearLayout.addView(mPreferenceSeekBar);
// create the text view for the current value below the seek bar
mCurrentValueTextView = new TextView(getContext());
mCurrentValueTextView.setText(getCurrentValueText(mPreferenceSeekBar.getProgress()));
mCurrentValueTextView.setGravity(Gravity.CENTER_HORIZONTAL);
// add the current value text view to the layout
linearLayout.addView(mCurrentValueTextView);
return linearLayout;
}
/**
* Get the current value text.
* @param progress
* the current progress level of the seek bar.
* @return the new current value text
*/
abstract String getCurrentValueText(int progress);
}