/* Tibisay Movil Copyright (C) 2013 Antonio Araujo (aaraujo@cenditel.gob.ve), Jose Ruiz (jruiz@cenditel.gob.ve), Fundacion Centro Nacional de Desarrollo e Investigacion en Tecnologias Libres - CENDITEL. La Fundación CENDITEL concede permiso para usar, copiar, distribuir y/o modificar este programa, reconociendo el derecho que la humanidad posee al libre acceso al conocimiento, bajo los términos de la licencia de software GPL versión 2.0 de la Free Software Foundation. Este programa se distribuye con la esperanza de que sea util, pero SIN NINGUNA GARANTIA; tampoco las implicitas garantias de MERCANTILIDAD o ADECUACION A UN PROPOSITO PARTICULAR. Para mayor información sobre los términos de la licencia ver el archivo llamado "gpl-2.0.txt" en ingles. */ package ve.gob.cenditel.tibisaymovil; import java.io.File; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.preference.ListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceActivity; import android.provider.MediaStore; import android.view.Window; import android.widget.Toast; public class PreferencesActivity extends PreferenceActivity implements OnPreferenceClickListener,OnPreferenceChangeListener{ static final int PICK_IMAGE = 33; public static final String imageKey = "image"; public static final String positionKey = "position"; private static final int MAXWIDTH = 400; private static final int MAXHEIGHT = 400; private static final long SIZELIMIT = 1024 * 1024; private SharedPreferences options; private Preference imagePreference; @Override public void onCreate(Bundle savedInstanceState) { //Estilando la barra de titulo final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); this.options = this.getPreferenceManager().getSharedPreferences(); this.imagePreference = (Preference) findPreference(PreferencesActivity.imageKey); this.imagePreference.setOnPreferenceClickListener(this); this.imagePreference.setOnPreferenceChangeListener(this); this.loadSummary(); //Estilando Barra de titulo if(customTitleSupported) getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); } private void loadSummary() { String path = options.getString(PreferencesActivity.imageKey, ""); if (!path.equals("")){ this.imagePreference.setSummary(path); } } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { String summary=""; if (summary!=null && !summary.equals("")){ preference.setSummary((CharSequence) summary); } return true; } @Override public boolean onPreferenceClick(Preference preference) { if (preference.getKey().equals(PreferencesActivity.imageKey)){ Intent i = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, PreferencesActivity.PICK_IMAGE); return true; } return false; } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PreferencesActivity.PICK_IMAGE && resultCode == RESULT_OK){ Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String imageSelectedPath = cursor.getString(columnIndex); cursor.close(); //Filter images if wrong format. boolean validImage = this.validateImage(imageSelectedPath); if (validImage==true){ //Save and edit summary text in user interface. SharedPreferences.Editor editor = this.options.edit(); editor.putString(PreferencesActivity.imageKey, imageSelectedPath); editor.commit(); this.imagePreference.setSummary(imageSelectedPath); } } } //Check image dimensions and format. private boolean validateImage(String imageSelectedPath) { File f = new File(imageSelectedPath); long size = f.length(); if (size <= (PreferencesActivity.SIZELIMIT)) { Bitmap chosenImage = BitmapFactory.decodeFile(imageSelectedPath); if (chosenImage !=null) return this.checkDimensions(chosenImage); else{ Toast.makeText(this, this.getString(R.string.errorformat),Toast.LENGTH_SHORT).show(); return false; } } else { Toast.makeText(this, this.getString(R.string.errorsize)+" "+(PreferencesActivity.SIZELIMIT)/(1024*1024)+"MB",Toast.LENGTH_SHORT).show(); return false; } } private boolean checkDimensions(Bitmap chosenImage) { int width = chosenImage.getWidth(); int height = chosenImage.getHeight(); if (width <= PreferencesActivity.MAXWIDTH && height <= PreferencesActivity.MAXHEIGHT) { return true; } else { Toast.makeText( this,this.getString(R.string.errordimension) +" "+ PreferencesActivity.MAXWIDTH + "x" + PreferencesActivity.MAXHEIGHT,Toast.LENGTH_SHORT).show(); return false; } } }