Thingse/app/src/main/java/com/daneshzaki/thingse/Preferences.java
// this method loads the preferences from the XML file
this method loads the preferences from the XML file
this method loads the preferences from the XML file
@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.getWindow().setNavigationBarColor(Color.parseColor("#D0D0D0"));
		addPreferencesFromResource(R.xml.preferences);
		ActionBar actionBar = getActionBar();
		actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33B5E5")));
		actionBar.setTitle("Settings");
		actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE);
		actionBar.setHomeButtonEnabled(true);
		Preference aboutPref = (Preference) findPreference("aboutPref");
		aboutPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
												   public boolean onPreferenceClick(Preference preference) {
													   Log.i("Preferences", "about clicked");
													   startActivity(new Intent(Preferences.this, AboutThingse.class));
													   return true;
												   }
											   }
		);
		Preference osPref = (Preference) findPreference("osPref");
		osPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
											   {
												   public boolean onPreferenceClick(Preference preference)
												   {
													   Log.i("Preferences", "open src clicked");
													   startActivity(new Intent(Preferences.this, OpenSrcLicenses.class));
													   return true;
												   }
											   }
		);
		adapter = new SQLAdapter(this);
		adapter.open();
		Preference exportPref = (Preference) findPreference("export");
		exportPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
		{
			public boolean onPreferenceClick(Preference preference)
			{
				Log.i("Preferences", "export clicked");
				Cursor cursor = adapter.getAllThings();
				StringBuffer records = new StringBuffer();
				if (cursor != null && cursor.moveToFirst())
				{
					do
					{
						Log.i("Preferences", "cursor contents 0 " + cursor.getString(0));
						Log.i("Preferences", "cursor contents 1 " + cursor.getString(1));
						Log.i("Preferences", "cursor contents 2 " + cursor.getString(2));
						Log.i("Preferences", "cursor contents 3 " + cursor.getString(3));
						Log.i("Preferences", "cursor contents 4 " + cursor.getString(4));
						Log.i("Preferences", "cursor contents 5 " + cursor.getString(5));
						Log.i("Preferences", "cursor contents 6 " + cursor.getString(6));
						StringBuffer record = new StringBuffer();
						record.append(cursor.getString(0));
						record.append(",");
						record.append(cursor.getString(1));
						record.append(",");
						record.append(cursor.getString(2));
						record.append(",");
						record.append(cursor.getString(3));
						record.append(",");
						record.append(cursor.getString(4));
						record.append(",");
						record.append(cursor.getString(5));
						record.append(",");
						record.append(cursor.getString(6));
						record.append("\n");
						records.append(record.toString());
					} while (cursor.moveToNext());
				}
				writeContentToFile(records.toString(),"thingse.csv");
		    	Toast.makeText(getBaseContext(), "Contents successfully exported to "+exportFileLocation, Toast.LENGTH_SHORT).show();
				return true;
			}
		}
		);
		Preference importPref = (Preference) findPreference("import");
		importPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
		{
			public boolean onPreferenceClick(Preference preference)
			{
				Log.i("Preferences", "import clicked");
				String[] importedContents = readContentFromFile("thingse.csv");
				int i = importedContents.length -1;
				Log.i("Preferences", "imported contents = \n");
				while(i>=0)
				{
					Log.i("Preferences", "record = "+importedContents[i]);
					StringTokenizer tokenizer = new StringTokenizer(importedContents[i], ",");
					String[] fields = new String[7];
					int j=0;
					while(tokenizer.hasMoreTokens())
					{
						fields[j] = tokenizer.nextToken();
						Log.i("Preferences", "field "+j+" = "+fields[j]);
						j++;
					}
					if(fields[2] != null && fields[2].trim().equalsIgnoreCase("g"))
					{
						/*fields[6]=fields[5];
						fields[5]=fields[4];
						fields[4]=fields[3];						
						fields[3]=fields[2];
						fields[2] = "";*/
						fields[6]=fields[5];
						fields[5]=fields[4];
						fields[4]=fields[3];
						fields[3]="g";
						fields[2] = "";
					}
					adapter.insertThing(fields[1], fields[2], fields[3], fields[4], fields[5], fields[6]);
					Log.i("Preferences", "writing rec to db - "+fields[1] +" "+ fields[2]+" "+ fields[3]+" "+ fields[4]+" "+ fields[5]+" "+ fields[6]);
					i--;
				}
		    	Toast.makeText(getBaseContext(), "Contents imported successfully", Toast.LENGTH_SHORT).show();
				return true;
			}
		}
		);
	}
@ SuppressWarnings ( "deprecation" ) @ Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; this . getWindow ( ) . setNavigationBarColor ( Color . parseColor ( "#D0D0D0" ) ) ; addPreferencesFromResource ( R . xml . preferences ) ; ActionBar actionBar = getActionBar ( ) ; actionBar . setBackgroundDrawable ( new ColorDrawable ( Color . parseColor ( "#33B5E5" ) ) ) ; actionBar . setTitle ( "Settings" ) ; actionBar . setDisplayOptions ( ActionBar . DISPLAY_HOME_AS_UP | ActionBar . DISPLAY_SHOW_TITLE ) ; actionBar . setHomeButtonEnabled ( true ) ; Preference aboutPref = ( Preference ) findPreference ( "aboutPref" ) ; aboutPref . setOnPreferenceClickListener ( new OnPreferenceClickListener ( ) { public boolean onPreferenceClick ( Preference preference ) { Log . i ( "Preferences" , "about clicked" ) ; startActivity ( new Intent ( Preferences . this , AboutThingse . class ) ) ; return true ; } } ) ; Preference osPref = ( Preference ) findPreference ( "osPref" ) ; osPref . setOnPreferenceClickListener ( new OnPreferenceClickListener ( ) { public boolean onPreferenceClick ( Preference preference ) { Log . i ( "Preferences" , "open src clicked" ) ; startActivity ( new Intent ( Preferences . this , OpenSrcLicenses . class ) ) ; return true ; } } ) ; adapter = new SQLAdapter ( this ) ; adapter . open ( ) ; Preference exportPref = ( Preference ) findPreference ( "export" ) ; exportPref . setOnPreferenceClickListener ( new OnPreferenceClickListener ( ) { public boolean onPreferenceClick ( Preference preference ) { Log . i ( "Preferences" , "export clicked" ) ; Cursor cursor = adapter . getAllThings ( ) ; StringBuffer records = new StringBuffer ( ) ; if ( cursor != null && cursor . moveToFirst ( ) ) { do { Log . i ( "Preferences" , "cursor contents 0 " + cursor . getString ( 0 ) ) ; Log . i ( "Preferences" , "cursor contents 1 " + cursor . getString ( 1 ) ) ; Log . i ( "Preferences" , "cursor contents 2 " + cursor . getString ( 2 ) ) ; Log . i ( "Preferences" , "cursor contents 3 " + cursor . getString ( 3 ) ) ; Log . i ( "Preferences" , "cursor contents 4 " + cursor . getString ( 4 ) ) ; Log . i ( "Preferences" , "cursor contents 5 " + cursor . getString ( 5 ) ) ; Log . i ( "Preferences" , "cursor contents 6 " + cursor . getString ( 6 ) ) ; StringBuffer record = new StringBuffer ( ) ; record . append ( cursor . getString ( 0 ) ) ; record . append ( "," ) ; record . append ( cursor . getString ( 1 ) ) ; record . append ( "," ) ; record . append ( cursor . getString ( 2 ) ) ; record . append ( "," ) ; record . append ( cursor . getString ( 3 ) ) ; record . append ( "," ) ; record . append ( cursor . getString ( 4 ) ) ; record . append ( "," ) ; record . append ( cursor . getString ( 5 ) ) ; record . append ( "," ) ; record . append ( cursor . getString ( 6 ) ) ; record . append ( "\n" ) ; records . append ( record . toString ( ) ) ; } while ( cursor . moveToNext ( ) ) ; } writeContentToFile ( records . toString ( ) , "thingse.csv" ) ; Toast . makeText ( getBaseContext ( ) , "Contents successfully exported to " + exportFileLocation , Toast . LENGTH_SHORT ) . show ( ) ; return true ; } } ) ; Preference importPref = ( Preference ) findPreference ( "import" ) ; importPref . setOnPreferenceClickListener ( new OnPreferenceClickListener ( ) { public boolean onPreferenceClick ( Preference preference ) { Log . i ( "Preferences" , "import clicked" ) ; String [ ] importedContents = readContentFromFile ( "thingse.csv" ) ; int i = importedContents . length - 1 ; Log . i ( "Preferences" , "imported contents = \n" ) ; while ( i >= 0 ) { Log . i ( "Preferences" , "record = " + importedContents [ i ] ) ; StringTokenizer tokenizer = new StringTokenizer ( importedContents [ i ] , "," ) ; String [ ] fields = new String [ 7 ] ; int j = 0 ; while ( tokenizer . hasMoreTokens ( ) ) { fields [ j ] = tokenizer . nextToken ( ) ; Log . i ( "Preferences" , "field " + j + " = " + fields [ j ] ) ; j ++ ; } if ( fields [ 2 ] != null && fields [ 2 ] . trim ( ) . equalsIgnoreCase ( "g" ) ) { /*fields[6]=fields[5];
						fields[5]=fields[4];
						fields[4]=fields[3];						
						fields[3]=fields[2];
						fields[2] = "";*/ fields [ 6 ] = fields [ 5 ] ; fields [ 5 ] = fields [ 4 ] ; fields [ 4 ] = fields [ 3 ] ; fields [ 3 ] = "g" ; fields [ 2 ] = "" ; } adapter . insertThing ( fields [ 1 ] , fields [ 2 ] , fields [ 3 ] , fields [ 4 ] , fields [ 5 ] , fields [ 6 ] ) ; Log . i ( "Preferences" , "writing rec to db - " + fields [ 1 ] + " " + fields [ 2 ] + " " + fields [ 3 ] + " " + fields [ 4 ] + " " + fields [ 5 ] + " " + fields [ 6 ] ) ; i -- ; } Toast . makeText ( getBaseContext ( ) , "Contents imported successfully" , Toast . LENGTH_SHORT ) . show ( ) ; return true ; } } ) ; }
this method loads the preferences from the XML file
this method loads the preferences from the XML file
// handle preference actions // about dialog // display about screen // open source licenses dialog // display about screen //instantiate db adapter and open db // export contents to file //read db table contents and write to a stringBuffer //records to be written to file // navigate through cursor to retrieve all records starting from the first // get each record // create a thing rec //display toast with status   // import contents from file //record counter //e.g. 6,xiomi,6000,p,2015-07-23,,/storage/emulated/0/Pictures/Thingse/IMG_20150707_102042.jpg //e.g 7,mouse,,g,2015-05-15,/storage/emulated/0/DCIM/Camera/IMG_20150515_182849.jpg,null //get fields from rec //field counter //TODO:check for gift //July 3 2015 fix for pic location //adapter.insertThing(name, price, gift, purchaseDate, description, picLocation); //write importedContents to the db table //display toast with status		