Skip to content

Commit 474019f

Browse files
committed
osmdroid#435 part 2, settings preferences for the various configuration options
1 parent 3217574 commit 474019f

File tree

7 files changed

+416
-6
lines changed

7 files changed

+416
-6
lines changed

OpenStreetMapViewer/src/main/AndroidManifest.xml

+9
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@
184184
<category android:name="android.intent.category.DEFAULT" />
185185
</intent-filter>
186186
</activity>
187+
<activity android:name=".PreferenceActivity"
188+
android:label="OpenStreetMap Preferences">
189+
<intent-filter>
190+
<action android:name="android.intent.action.VIEW" />
191+
192+
<category android:name="android.intent.category.DEFAULT" />
193+
</intent-filter>
194+
</activity>
195+
187196
</application>
188197

189198
</manifest>

OpenStreetMapViewer/src/main/java/org/osmdroid/MainActivity.java

+21
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import android.Manifest;
55
import android.app.Activity;
66
import android.content.Intent;
7+
import android.content.SharedPreferences;
78
import android.content.pm.PackageManager;
89
import android.graphics.Color;
910
import android.graphics.Typeface;
1011
import android.net.Uri;
1112
import android.os.Build;
1213
import android.os.Bundle;
1314
import android.os.Environment;
15+
import android.preference.PreferenceManager;
1416
import android.support.v4.content.ContextCompat;
1517
import android.view.View;
1618
import android.widget.AdapterView;
@@ -24,7 +26,10 @@
2426
import org.osmdroid.samples.SampleWithMinimapZoomcontrols;
2527
import org.osmdroid.samples.SampleWithTilesOverlay;
2628
import org.osmdroid.samples.SampleWithTilesOverlayAndCustomTileSource;
29+
import org.osmdroid.tileprovider.constants.OpenStreetMapTileProviderConstants;
30+
import org.osmdroid.views.MapView;
2731

32+
import java.io.File;
2833
import java.util.ArrayList;
2934
import java.util.HashMap;
3035
import java.util.List;
@@ -45,6 +50,17 @@ public void onCreate(final Bundle savedInstanceState) {
4550
checkPermissions();
4651
}
4752

53+
54+
File discoveredBestPath = OpenStreetMapTileProviderConstants.TILE_PATH_BASE;
55+
56+
57+
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
58+
OpenStreetMapTileProviderConstants.TILE_PATH_BASE = new File(prefs.getString("textViewCacheDirectory", discoveredBestPath.getAbsolutePath()));
59+
OpenStreetMapTileProviderConstants.DEBUGMODE=prefs.getBoolean("checkBoxDebugMode",false);
60+
OpenStreetMapTileProviderConstants.DEBUG_TILE_PROVIDERS=prefs.getBoolean("checkBoxDebugTileProvider",false);
61+
MapView.hardwareAccelerated=prefs.getBoolean("checkBoxHardwareAcceleration",false);
62+
63+
4864
// Generate a ListView with Sample Maps
4965
final ArrayList<String> list = new ArrayList<>();
5066
list.add("OSMDroid Sample map (Start Here)");
@@ -56,6 +72,7 @@ public void onCreate(final Bundle savedInstanceState) {
5672
list.add("More Samples");
5773
list.add("Bug Drivers");
5874
list.add("Report a bug");
75+
list.add("Settings");
5976
//this.setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list));
6077
ListView lv = (ListView) findViewById(R.id.activitylist);
6178
ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
@@ -108,6 +125,10 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
108125
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/osmdroid/osmdroid/issues"));
109126
startActivity(browserIntent);
110127
break;
128+
case 9:
129+
Intent i = new Intent(this,PreferenceActivity.class);
130+
startActivity(i);
131+
break;
111132
}
112133
}
113134

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package org.osmdroid;
2+
3+
import android.app.Activity;
4+
import android.app.AlertDialog;
5+
import android.content.DialogInterface;
6+
import android.content.SharedPreferences;
7+
import android.os.Bundle;
8+
import android.preference.PreferenceManager;
9+
import android.text.Editable;
10+
import android.text.InputType;
11+
import android.text.TextWatcher;
12+
import android.view.View;
13+
import android.widget.Button;
14+
import android.widget.CheckBox;
15+
import android.widget.EditText;
16+
import android.widget.TextView;
17+
18+
import org.osmdroid.tileprovider.constants.OpenStreetMapTileProviderConstants;
19+
import org.osmdroid.tileprovider.util.StorageUtils;
20+
import org.osmdroid.views.MapView;
21+
22+
import java.io.File;
23+
24+
/**
25+
* Created by alex on 10/21/16.
26+
*/
27+
28+
public class PreferenceActivity extends Activity implements View.OnClickListener {
29+
CheckBox checkBoxDebugTileProvider,
30+
checkBoxDebugMode,
31+
checkBoxHardwareAcceleration;
32+
Button buttonSetCache,
33+
buttonManualCacheEntry;
34+
TextView textViewCacheDirectory;
35+
36+
@Override
37+
public void onCreate(final Bundle savedInstanceState) {
38+
super.onCreate(savedInstanceState);
39+
setContentView(R.layout.activity_prefs);
40+
checkBoxDebugTileProvider = (CheckBox) findViewById(R.id.checkBoxDebugTileProvider);
41+
checkBoxDebugMode = (CheckBox) findViewById(R.id.checkBoxDebugMode);
42+
checkBoxHardwareAcceleration = (CheckBox) findViewById(R.id.checkBoxHardwareAcceleration);
43+
buttonSetCache = (Button) findViewById(R.id.buttonSetCache);
44+
buttonManualCacheEntry = (Button) findViewById(R.id.buttonManualCacheEntry);
45+
textViewCacheDirectory = (TextView) findViewById(R.id.textViewCacheDirectory);
46+
47+
checkBoxDebugTileProvider.setOnClickListener(this);
48+
checkBoxDebugMode.setOnClickListener(this);
49+
checkBoxHardwareAcceleration.setOnClickListener(this);
50+
buttonSetCache.setOnClickListener(this);
51+
buttonManualCacheEntry.setOnClickListener(this);
52+
}
53+
54+
@Override
55+
public void onResume() {
56+
super.onResume();
57+
//TODO load from preferneces
58+
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
59+
checkBoxDebugMode.setChecked(prefs.getBoolean("checkBoxDebugMode", OpenStreetMapTileProviderConstants.DEBUGMODE));
60+
checkBoxDebugTileProvider.setChecked(prefs.getBoolean("checkBoxDebugTileProvider", OpenStreetMapTileProviderConstants.DEBUG_TILE_PROVIDERS));
61+
checkBoxHardwareAcceleration.setChecked(prefs.getBoolean("checkBoxHardwareAcceleration", MapView.hardwareAccelerated));
62+
textViewCacheDirectory.setText(prefs.getString("textViewCacheDirectory", OpenStreetMapTileProviderConstants.getBasePath().getAbsolutePath()));
63+
64+
}
65+
66+
@Override
67+
public void onPause() {
68+
super.onPause();
69+
OpenStreetMapTileProviderConstants.DEBUGMODE = checkBoxDebugMode.isChecked();
70+
OpenStreetMapTileProviderConstants.DEBUG_TILE_PROVIDERS = checkBoxDebugTileProvider.isChecked();
71+
MapView.hardwareAccelerated = checkBoxHardwareAcceleration.isChecked();
72+
OpenStreetMapTileProviderConstants.TILE_PATH_BASE=new File(textViewCacheDirectory.getText().toString());
73+
74+
SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
75+
edit.putBoolean("checkBoxDebugMode", checkBoxDebugMode.isChecked());
76+
edit.putBoolean("checkBoxDebugTileProvider", checkBoxDebugTileProvider.isChecked());
77+
edit.putBoolean("checkBoxHardwareAcceleration", checkBoxHardwareAcceleration.isChecked());
78+
edit.putString("textViewCacheDirectory", textViewCacheDirectory.getText().toString());
79+
edit.commit();
80+
}
81+
82+
@Override
83+
public void onClick(View v) {
84+
85+
switch (v.getId()) {
86+
case R.id.buttonManualCacheEntry: {
87+
showManualEntry();
88+
}
89+
case R.id.buttonSetCache: {
90+
91+
}
92+
93+
}
94+
}
95+
96+
String m_Text = "";
97+
98+
private void showManualEntry() {
99+
100+
AlertDialog.Builder builder = new AlertDialog.Builder(this);
101+
builder.setTitle("Title");
102+
103+
// Set up the input
104+
final EditText input = new EditText(this);
105+
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
106+
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
107+
input.setLines(1);
108+
input.addTextChangedListener(new TextWatcher() {
109+
@Override
110+
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
111+
112+
}
113+
114+
@Override
115+
public void onTextChanged(CharSequence s, int start, int before, int count) {
116+
117+
}
118+
119+
@Override
120+
public void afterTextChanged(Editable s) {
121+
File file = new File(input.getText().toString());
122+
if (!file.exists()) {
123+
input.setError("Does not exist");
124+
} else if (file.exists() && !file.isDirectory()) {
125+
input.setError("Not a directory");
126+
} else if (!StorageUtils.isWritable(file)){
127+
input.setError("Not writable");
128+
} else {
129+
input.setError(null);
130+
}
131+
}
132+
});
133+
builder.setView(input);
134+
135+
// Set up the buttons
136+
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
137+
@Override
138+
public void onClick(DialogInterface dialog, int which) {
139+
if (input.getError()==null) {
140+
m_Text = input.getText().toString();
141+
textViewCacheDirectory.setText(m_Text);
142+
}
143+
}
144+
});
145+
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
146+
@Override
147+
public void onClick(DialogInterface dialog, int which) {
148+
dialog.cancel();
149+
}
150+
});
151+
152+
builder.show();
153+
154+
}
155+
}

OpenStreetMapViewer/src/main/java/org/osmdroid/constants/OpenStreetMapConstants.java

-6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ public interface OpenStreetMapConstants {
1313
// Final Fields
1414
// ===========================================================
1515

16-
public static final String DEBUGTAG = "OPENSTREETMAP";
17-
18-
public static final boolean DEBUGMODE = false;
19-
20-
public static final int NOT_SET = Integer.MIN_VALUE;
21-
2216
public static final String PREFS_NAME = "org.andnav.osm.prefs";
2317
public static final String PREFS_TILE_SOURCE = "tilesource";
2418
public static final String PREFS_SCROLL_X = "scrollX";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="vertical" android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<CheckBox
7+
android:text="@string/tile_provided_debugging"
8+
android:layout_width="match_parent"
9+
android:layout_height="wrap_content"
10+
android:id="@+id/checkBoxDebugTileProvider" />
11+
12+
<CheckBox
13+
android:text="@string/debug_logging"
14+
android:layout_width="match_parent"
15+
android:layout_height="wrap_content"
16+
android:id="@+id/checkBoxDebugMode" />
17+
18+
<CheckBox
19+
android:text="@string/enable_mapview_hardware_acceleration"
20+
android:layout_width="match_parent"
21+
android:layout_height="wrap_content"
22+
android:id="@+id/checkBoxHardwareAcceleration" />
23+
24+
<TextView
25+
android:text="@string/cache_directory"
26+
android:layout_width="match_parent"
27+
android:layout_height="wrap_content"
28+
/>
29+
30+
<TextView
31+
android:text="/storage/sdcard"
32+
android:layout_width="match_parent"
33+
android:layout_height="wrap_content"
34+
android:id="@+id/textViewCacheDirectory" />
35+
36+
<Button
37+
android:text="Pick Cache Location"
38+
android:layout_width="match_parent"
39+
android:layout_height="wrap_content"
40+
android:id="@+id/buttonSetCache" />
41+
42+
<Button
43+
android:text="Enter Cache Location"
44+
android:layout_width="match_parent"
45+
android:layout_height="wrap_content"
46+
android:id="@+id/buttonManualCacheEntry" />
47+
</LinearLayout>

OpenStreetMapViewer/src/main/res/values/strings.xml

+4
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,9 @@
3535

3636
<string name="bt_center_map_description">Center on my location</string>
3737
<string name="bt_follow_me_description">Follow me</string>
38+
<string name="debug_logging">Debug Logging</string>
39+
<string name="tile_provided_debugging">Tile Provided Debugging</string>
40+
<string name="cache_directory">Cache Directory</string>
41+
<string name="enable_mapview_hardware_acceleration">Enable MapView Hardware Acceleration</string>
3842

3943
</resources>

0 commit comments

Comments
 (0)