|
| 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 | +} |
0 commit comments