1 /* 2 Copyright (c) 2012-2013, BogDan Vatra <bogdan@kde.org> 3 Contact: http://www.qt-project.org/legal 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions 7 are met: 8 9 1. Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 2. Redistributions in binary form must reproduce the above copyright 12 notice, this list of conditions and the following disclaimer in the 13 documentation and/or other materials provided with the distribution. 14 15 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 package org.opencpn; 27 28 29 import java.io.File; 30 import java.util.ArrayList; 31 import java.util.List; 32 import java.util.StringTokenizer; 33 34 35 import android.content.Intent; 36 import android.preference.EditTextPreference; 37 import android.preference.ListPreference; 38 import android.util.Log; 39 40 import android.view.View; 41 import android.widget.Toast; 42 43 import android.os.Bundle; 44 import android.preference.PreferenceManager; 45 import android.preference.PreferenceFragment; 46 import android.preference.PreferenceActivity; 47 import android.preference.Preference; 48 import android.content.SharedPreferences; 49 import android.content.SharedPreferences.Editor; 50 51 import ar.com.daidalos.afiledialog.*; 52 53 import org.opencpn.opencpn.R; 54 55 //@ANDROID-11 56 57 58 public class OCPNSettingsActivity extends PreferenceActivity 59 { 60 61 private boolean mbS52 = false; 62 63 @Override onBuildHeaders(List<Header> target)64 public void onBuildHeaders(List<Header> target) { 65 loadHeadersFromResource(R.xml.ocpn_preference_headers, target); 66 67 m_chartDirList = new ArrayList<String>(); 68 69 Bundle extras = getIntent().getExtras(); 70 if (extras != null) { 71 String settings = extras.getString("SETTINGS_STRING"); 72 73 Log.i("DEBUGGER_TAG", "OCPNSettingsActivity"); 74 // Log.i("DEBUGGER_TAG", settings); 75 76 m_settings = settings; 77 m_newSettings = ""; 78 79 // Create the various settings elements 80 81 StringTokenizer tkz = new StringTokenizer(settings, ";"); 82 83 while(tkz.hasMoreTokens()){ 84 String tk = tkz.nextToken(); 85 // Log.i("DEBUGGER_TAG", tk); 86 87 if( tk.startsWith("ChartDir") ){ 88 int mark = tk.indexOf(":"); 89 if(mark > 0){ 90 String dir = tk.substring(mark+1); 91 // Log.i("DEBUGGER_TAG", dir); 92 m_chartDirList.add(dir); 93 } 94 } 95 96 } 97 98 // Stuff the application shared preferences data from the settings string 99 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 100 Editor editor = preferences.edit(); 101 102 StringTokenizer tkzp = new StringTokenizer(settings, ";"); 103 104 while(tkzp.hasMoreTokens()){ 105 String tk = tkzp.nextToken(); 106 107 if( tk.startsWith("prefb_") ){ // simple Boolean 108 mbS52 |= isPrefS52(tk); 109 int mark = tk.indexOf(":"); 110 if(mark > 0){ 111 String key = tk.substring(0, mark); 112 String value = tk.substring(mark+1); 113 if(value.equals("1")) 114 editor.putBoolean(key, true); 115 else 116 editor.putBoolean(key, false); 117 } 118 } 119 120 else if( tk.startsWith("prefs_") ){ // simple string 121 mbS52 |= isPrefS52(tk); 122 int mark = tk.indexOf(":"); 123 if(mark > 0){ 124 String key = tk.substring(0, mark); 125 // Log.i("DEBUGGER_TAG", key); 126 // Log.i("DEBUGGER_TAG", (mbS52)?"True":"False"); 127 String value = tk.substring(mark+1); 128 editor.putString(key, value); 129 } 130 } 131 132 } 133 134 editor.commit(); 135 } 136 137 } 138 139 // We use this method to pass initial arguments to fragments 140 // Note: Same bundle goes to all fragments. 141 @Override onHeaderClick(Header header, int position)142 public void onHeaderClick(Header header, int position) { 143 if(header.fragmentArguments == null) 144 { 145 header.fragmentArguments = new Bundle(); 146 } 147 148 if(mbS52){ 149 header.fragmentArguments.putString("S52", "TRUE"); 150 } 151 else{ 152 header.fragmentArguments.putString("S52", "FALSE"); 153 } 154 155 156 super.onHeaderClick(header, position); 157 158 } 159 isPrefS52( String s )160 private boolean isPrefS52( String s ){ 161 162 if(s.startsWith("prefs_displaycategory")) return true; 163 if(s.startsWith("prefs_shallowdepth")) return true; 164 if(s.startsWith("prefs_safetydepth")) return true; 165 if(s.startsWith("prefs_deepdepth")) return true; 166 if(s.startsWith("prefs_vectorgraphicsstyle")) return true; 167 if(s.startsWith("prefs_vectorboundarystyle")) return true; 168 if(s.startsWith("prefs_vectorchartcolors")) return true; 169 if(s.startsWith("prefb_showsound")) return true; 170 if(s.startsWith("prefb_showATONLabels")) return true; 171 if(s.startsWith("prefb_showlightldesc")) return true; 172 if(s.startsWith("prefb_showimptext")) return true; 173 if(s.startsWith("prefb_showSCAMIN")) return true; 174 175 return false; 176 } 177 addDirectory(View vw)178 public void addDirectory(View vw) 179 { 180 // Toast.makeText(this, "OCPNsettingsactivity Add Dir ", Toast.LENGTH_SHORT).show(); 181 182 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 183 184 Intent intent = new Intent(OCPNSettingsActivity.this, FileChooserActivity.class); 185 intent.putExtra(FileChooserActivity.INPUT_START_FOLDER, preferences.getString("prefs_chartInitDir", "/mnt/sdcard")); 186 intent.putExtra(FileChooserActivity.INPUT_FOLDER_MODE, true); 187 intent.putExtra(FileChooserActivity.INPUT_SHOW_FULL_PATH_IN_TITLE, true); 188 intent.putExtra(FileChooserActivity.INPUT_SHOW_ONLY_SELECTABLE, true); 189 intent.putExtra(FileChooserActivity.INPUT_TITLE_STRING, "Add Chart Directory"); 190 191 startActivityForResult(intent, 0); 192 } 193 removeDirectory(View vw)194 public void removeDirectory(View vw) 195 { 196 // Toast.makeText(this, "OCPNsettingsactivity Remove Dir ", Toast.LENGTH_SHORT).show(); 197 198 OCPNSettingsFragmentCharts cfrag = OCPNSettingsFragmentCharts.getFragment(); 199 String removalCandidate = cfrag.getSelected(); 200 201 m_chartDirList.remove(removalCandidate); 202 203 cfrag.updateChartDirListView(); 204 } 205 206 @Override onActivityResult(int requestCode, int resultCode, Intent data)207 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 208 // Check which request we're responding to 209 if (requestCode == 0) { 210 // Make sure the request was successful 211 if (resultCode == RESULT_OK) { 212 boolean fileCreated = false; 213 String filePath = ""; 214 String parentDir = ""; 215 Bundle bundle = data.getExtras(); 216 if(bundle != null) 217 { 218 if(bundle.containsKey(FileChooserActivity.OUTPUT_NEW_FILE_NAME)) { 219 fileCreated = true; 220 File folder = (File) bundle.get(FileChooserActivity.OUTPUT_FILE_OBJECT); 221 String name = bundle.getString(FileChooserActivity.OUTPUT_NEW_FILE_NAME); 222 filePath = folder.getAbsolutePath() + "/" + name; 223 } else { 224 fileCreated = false; 225 File file = (File) bundle.get(FileChooserActivity.OUTPUT_FILE_OBJECT); 226 filePath = file.getAbsolutePath(); 227 parentDir = file.getParent(); 228 if(null == parentDir) 229 parentDir = "/mnt"; 230 } 231 } 232 233 String message = fileCreated? "File created" : "File opened"; 234 message += ": " + filePath + " parentDir:" + parentDir;; 235 // Log.i("DEBUGGER_TAG", message); 236 m_chartDirList.add(filePath); 237 238 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 239 Editor editor = preferences.edit(); 240 editor.putString("prefs_chartInitDir", parentDir); 241 editor.commit(); 242 243 244 OCPNSettingsFragmentCharts cfrag = OCPNSettingsFragmentCharts.getFragment(); 245 cfrag.updateChartDirListView(); 246 247 } 248 } 249 } 250 251 @Override onResume()252 public void onResume() { 253 super.onResume(); 254 } 255 256 257 258 @Override onPause()259 protected void onPause() 260 { 261 super.onPause(); 262 263 Log.i("DEBUGGER_TAG", "SettingsActivity onPause"); 264 265 createSettingsString(); 266 267 Bundle b = new Bundle(); 268 b.putString("SettingsString", m_newSettings); 269 270 Intent i = new Intent(); 271 i.putExtras(b); 272 setResult(RESULT_OK, i); 273 274 } 275 276 @Override finish()277 public void finish() { 278 Log.i("DEBUGGER_TAG", "SettingsActivity finish"); 279 280 createSettingsString(); 281 282 Bundle b = new Bundle(); 283 b.putString("SettingsString", m_newSettings); 284 Intent i = new Intent(); 285 i.putExtras(b); 286 setResult(RESULT_OK, i); 287 288 super.finish(); 289 } 290 createSettingsString()291 private void createSettingsString() 292 { 293 m_newSettings = ""; 294 295 296 // Record the chart dir list contents 297 for(int i=0 ; i < m_chartDirList.size() ; ++i){ 298 m_newSettings = m_newSettings.concat("ChartDir:"); 299 String dir = m_chartDirList.get(i); 300 // Log.i("DEBUGGER_TAG", dir); 301 m_newSettings = m_newSettings.concat(dir); 302 m_newSettings = m_newSettings.concat(";"); 303 // Log.i("DEBUGGER_TAG", m_newSettings); 304 305 } 306 307 // Get the android persisted values, one-by-one 308 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 309 310 // Simple Booleans 311 m_newSettings = m_newSettings.concat(appendBoolSetting("prefb_lookahead", preferences.getBoolean("prefb_lookahead", false))); 312 m_newSettings = m_newSettings.concat(appendBoolSetting("prefb_quilt", preferences.getBoolean("prefb_quilt", false))); 313 m_newSettings = m_newSettings.concat(appendBoolSetting("prefb_showgrid", preferences.getBoolean("prefb_showgrid", false))); 314 m_newSettings = m_newSettings.concat(appendBoolSetting("prefb_showoutlines", preferences.getBoolean("prefb_showoutlines", false))); 315 m_newSettings = m_newSettings.concat(appendBoolSetting("prefb_showdepthunits", preferences.getBoolean("prefb_showdepthunits", false))); 316 m_newSettings = m_newSettings.concat(appendBoolSetting("prefb_lockwp", preferences.getBoolean("prefb_lockwp", false))); 317 m_newSettings = m_newSettings.concat(appendBoolSetting("prefb_confirmdelete", preferences.getBoolean("prefb_confirmdelete", false))); 318 m_newSettings = m_newSettings.concat(appendBoolSetting("prefb_expertmode", preferences.getBoolean("prefb_expertmode", false))); 319 m_newSettings = m_newSettings.concat(appendBoolSetting("prefb_internalGPS", preferences.getBoolean("prefb_internalGPS", false))); 320 321 if(mbS52){ 322 m_newSettings = m_newSettings.concat(appendBoolSetting("prefb_showsound", preferences.getBoolean("prefb_showsound", false))); 323 m_newSettings = m_newSettings.concat(appendBoolSetting("prefb_showATONLabels", preferences.getBoolean("prefb_showATONLabels", false))); 324 m_newSettings = m_newSettings.concat(appendBoolSetting("prefb_showlightldesc", preferences.getBoolean("prefb_showlightldesc", false))); 325 m_newSettings = m_newSettings.concat(appendBoolSetting("prefb_showimptext", preferences.getBoolean("prefb_showimptext", false))); 326 m_newSettings = m_newSettings.concat(appendBoolSetting("prefb_showSCAMIN", preferences.getBoolean("prefb_showSCAMIN", false))); 327 } 328 329 330 // Other assorted string values 331 if(mbS52){ 332 m_newSettings = m_newSettings.concat(appendStringSetting("prefs_displaycategory", preferences.getString("prefs_displaycategory", "?"))); 333 m_newSettings = m_newSettings.concat(appendStringSetting("prefs_shallowdepth", preferences.getString("prefs_shallowdepth", "?"))); 334 m_newSettings = m_newSettings.concat(appendStringSetting("prefs_safetydepth", preferences.getString("prefs_safetydepth", "?"))); 335 m_newSettings = m_newSettings.concat(appendStringSetting("prefs_deepdepth", preferences.getString("prefs_deepdepth", "?"))); 336 m_newSettings = m_newSettings.concat(appendStringSetting("prefs_vectorgraphicsstyle", preferences.getString("prefs_vectorgraphicsstyle", "?"))); 337 m_newSettings = m_newSettings.concat(appendStringSetting("prefs_vectorboundarystyle", preferences.getString("prefs_vectorboundarystyle", "?"))); 338 m_newSettings = m_newSettings.concat(appendStringSetting("prefs_vectorchartcolors", preferences.getString("prefs_vectorchartcolors", "?"))); 339 } 340 341 m_newSettings = m_newSettings.concat(appendStringSetting("prefs_navmode", preferences.getString("prefs_navmode", "?"))); 342 m_newSettings = m_newSettings.concat(appendStringSetting("prefs_UIScaleFactor", preferences.getString("prefs_UIScaleFactor", "?"))); 343 m_newSettings = m_newSettings.concat(appendStringSetting("prefs_chartScaleFactor", preferences.getString("prefs_chartScaleFactor", "?"))); 344 m_newSettings = m_newSettings.concat(appendStringSetting("prefs_chartInitDir", preferences.getString("prefs_chartInitDir", "?"))); 345 346 // Log.i("DEBUGGER_TAG", m_newSettings); 347 348 } 349 appendBoolSetting(String key, Boolean value)350 private String appendBoolSetting(String key, Boolean value) 351 { 352 String ret = key; 353 if(value) 354 ret = ret.concat(":1;"); 355 else 356 ret = ret.concat(":0;"); 357 358 return ret; 359 } 360 appendStringSetting(String key, String value)361 private String appendStringSetting(String key, String value) 362 { 363 String ret = key; 364 ret = ret.concat(":"); 365 ret = ret.concat(value); 366 ret = ret.concat(";"); 367 368 return ret; 369 } 370 371 372 // info at 373 //http://stackoverflow.com/questions/19973034/isvalidfragment-android-api-19 374 // @Override 375 // protected boolean isValidFragment(String fragmentName) { 376 // return StockPreferenceFragment.class.getName().equals(fragmentName); 377 // } 378 379 // @TargetApi(Build.VERSION_CODES.HONEYCOMB) 380 381 @Override isValidFragment(String fragmentName)382 protected boolean isValidFragment (String fragmentName) { 383 384 return true; //"com.fullpackage.MyPreferenceFragment".equals(fragmentName); 385 386 } 387 388 389 390 /** 391 * This fragment contains a second-level set of preference 392 * for Charts->Vector Chart Settings. 393 */ 394 public static class ChartsVectorSettings extends PreferenceFragment { 395 396 public ListPreference mDisplayCategoryPreference; 397 public ListPreference mGraphicsStylePreference; 398 public ListPreference mBoundaryStylePreference; 399 public ListPreference mColorsPreference; 400 401 public EditTextPreference mShallowPreference; 402 public EditTextPreference mSafetyPreference; 403 public EditTextPreference mDeepPreference; 404 405 public SharedPreferences.OnSharedPreferenceChangeListener m_listenerVector; 406 407 @Override onCreate(Bundle savedInstanceState)408 public void onCreate(Bundle savedInstanceState) { 409 super.onCreate(savedInstanceState); 410 411 // Can retrieve arguments from preference XML. 412 // Log.i("args", "Arguments: " + getArguments()); 413 414 // Load the preferences from an XML resource 415 addPreferencesFromResource(R.xml.charts_vector_settings); 416 417 // Set up listeners for a few items, so that the "summaries" will update properly 418 419 // Display Category 420 mDisplayCategoryPreference = (ListPreference)getPreferenceScreen().findPreference("prefs_displaycategory"); 421 // Graphics Style 422 mGraphicsStylePreference = (ListPreference)getPreferenceScreen().findPreference("prefs_vectorgraphicsstyle"); 423 // Boundary Style 424 mBoundaryStylePreference = (ListPreference)getPreferenceScreen().findPreference("prefs_vectorboundarystyle"); 425 // Colors 426 mColorsPreference = (ListPreference)getPreferenceScreen().findPreference("prefs_vectorchartcolors"); 427 428 mShallowPreference = (EditTextPreference)getPreferenceScreen().findPreference("prefs_shallowdepth"); 429 mSafetyPreference = (EditTextPreference)getPreferenceScreen().findPreference("prefs_safetydepth"); 430 mDeepPreference = (EditTextPreference)getPreferenceScreen().findPreference("prefs_deepdepth"); 431 432 m_listenerVector = new SharedPreferences.OnSharedPreferenceChangeListener() { 433 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { 434 // listener implementation 435 // Set new summary, when a preference value changes 436 if (key.equals("prefs_displaycategory")) { 437 mDisplayCategoryPreference.setSummary(mDisplayCategoryPreference.getEntry().toString()); 438 } 439 if (key.equals("prefs_vectorgraphicsstyle")) { 440 mGraphicsStylePreference.setSummary(mGraphicsStylePreference.getEntry().toString()); 441 } 442 if (key.equals("prefs_vectorboundarystyle")) { 443 mBoundaryStylePreference.setSummary(mBoundaryStylePreference.getEntry().toString()); 444 } 445 if (key.equals("prefs_vectorchartcolors")) { 446 mColorsPreference.setSummary(mColorsPreference.getEntry().toString()); 447 } 448 if (key.equals("prefs_shallowdepth")) { 449 mShallowPreference.setSummary(mShallowPreference.getText()); 450 } 451 if (key.equals("prefs_safetydepth")) { 452 mSafetyPreference.setSummary(mSafetyPreference.getText()); 453 } 454 if (key.equals("prefs_deepdepth")) { 455 mDeepPreference.setSummary(mDeepPreference.getText()); 456 } 457 } 458 }; 459 460 getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(m_listenerVector); 461 462 463 // Setup initial values, checking for undefined as yet. 464 465 CharSequence cs = mDisplayCategoryPreference.getEntry(); 466 if(cs == null) 467 mDisplayCategoryPreference.setValueIndex(0); 468 469 cs = mGraphicsStylePreference.getEntry(); 470 if(cs == null) 471 mGraphicsStylePreference.setValueIndex(0); 472 473 cs = mBoundaryStylePreference.getEntry(); 474 if(cs == null) 475 mBoundaryStylePreference.setValueIndex(0); 476 477 cs = mColorsPreference.getEntry(); 478 if(cs == null) 479 mColorsPreference.setValueIndex(0); 480 481 mDisplayCategoryPreference.setSummary(mDisplayCategoryPreference.getEntry().toString()); 482 mGraphicsStylePreference.setSummary(mGraphicsStylePreference.getEntry().toString()); 483 mBoundaryStylePreference.setSummary(mBoundaryStylePreference.getEntry().toString()); 484 mColorsPreference.setSummary(mColorsPreference.getEntry().toString()); 485 486 if(mShallowPreference.getText() == null) 487 mShallowPreference.setText("2"); 488 if(mSafetyPreference.getText() == null) 489 mSafetyPreference.setText("4"); 490 if(mDeepPreference.getText() == null) 491 mDeepPreference.setText("6"); 492 493 mShallowPreference.setSummary(mShallowPreference.getText()); 494 mSafetyPreference.setSummary(mSafetyPreference.getText()); 495 mDeepPreference.setSummary(mDeepPreference.getText()); 496 497 } 498 } 499 500 getSettingsString()501 public static String getSettingsString(){ return m_settings;} getChartDirList()502 public static ArrayList<String> getChartDirList(){ return m_chartDirList;} 503 504 private static String m_settings; 505 private String m_newSettings; 506 private static ArrayList<String> m_chartDirList; 507 } 508 509 510 511 512 513