[288126d] | 1 | package ve.gob.cenditel.tibisaymovil; |
---|
[2ff9cec] | 2 | import java.io.File; |
---|
| 3 | |
---|
| 4 | import android.content.Intent; |
---|
| 5 | import android.content.SharedPreferences; |
---|
| 6 | import android.database.Cursor; |
---|
| 7 | import android.graphics.Bitmap; |
---|
| 8 | import android.graphics.BitmapFactory; |
---|
| 9 | import android.net.Uri; |
---|
[288126d] | 10 | import android.os.Bundle; |
---|
[2ff9cec] | 11 | import android.preference.ListPreference; |
---|
| 12 | import android.preference.Preference; |
---|
| 13 | import android.preference.Preference.OnPreferenceChangeListener; |
---|
| 14 | import android.preference.Preference.OnPreferenceClickListener; |
---|
[288126d] | 15 | import android.preference.PreferenceActivity; |
---|
[2ff9cec] | 16 | import android.provider.MediaStore; |
---|
| 17 | import android.view.Window; |
---|
| 18 | import android.widget.Toast; |
---|
[288126d] | 19 | |
---|
[2ff9cec] | 20 | public class PreferencesActivity extends PreferenceActivity implements OnPreferenceClickListener,OnPreferenceChangeListener{ |
---|
| 21 | |
---|
| 22 | static final int PICK_IMAGE = 33; |
---|
| 23 | public static final String imageKey = "image"; |
---|
| 24 | public static final String positionKey = "position"; |
---|
| 25 | private static final int MAXWIDTH = 400; |
---|
| 26 | private static final int MAXHEIGHT = 400; |
---|
| 27 | private static final long SIZELIMIT = 1024 * 1024; |
---|
| 28 | |
---|
| 29 | private SharedPreferences options; |
---|
| 30 | private Preference imagePreference; |
---|
| 31 | |
---|
[288126d] | 32 | @Override |
---|
| 33 | public void onCreate(Bundle savedInstanceState) { |
---|
[2ff9cec] | 34 | //Estilando la barra de titulo |
---|
| 35 | final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); |
---|
| 36 | |
---|
[288126d] | 37 | super.onCreate(savedInstanceState); |
---|
[2ff9cec] | 38 | |
---|
[288126d] | 39 | addPreferencesFromResource(R.xml.preferences); |
---|
[2ff9cec] | 40 | |
---|
| 41 | this.options = this.getPreferenceManager().getSharedPreferences(); |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | this.imagePreference = (Preference) findPreference(PreferencesActivity.imageKey); |
---|
| 47 | this.imagePreference.setOnPreferenceClickListener(this); |
---|
| 48 | this.imagePreference.setOnPreferenceChangeListener(this); |
---|
| 49 | |
---|
| 50 | this.loadSummary(); |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | //Estilando Barra de titulo |
---|
| 54 | if(customTitleSupported) |
---|
| 55 | getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); |
---|
| 56 | |
---|
[288126d] | 57 | } |
---|
[2ff9cec] | 58 | |
---|
| 59 | private void loadSummary() { |
---|
| 60 | String path = options.getString(PreferencesActivity.imageKey, ""); |
---|
| 61 | if (!path.equals("")){ |
---|
| 62 | this.imagePreference.setSummary(path); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | @Override |
---|
| 70 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
---|
| 71 | String summary=""; |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | if (summary!=null && !summary.equals("")){ |
---|
| 75 | preference.setSummary((CharSequence) summary); |
---|
| 76 | |
---|
| 77 | } |
---|
| 78 | return true; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | @Override |
---|
| 84 | public boolean onPreferenceClick(Preference preference) { |
---|
| 85 | |
---|
| 86 | if (preference.getKey().equals(PreferencesActivity.imageKey)){ |
---|
| 87 | Intent i = new Intent( |
---|
| 88 | Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); |
---|
| 89 | startActivityForResult(i, PreferencesActivity.PICK_IMAGE); |
---|
| 90 | return true; |
---|
| 91 | } |
---|
| 92 | return false; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
---|
| 96 | super.onActivityResult(requestCode, resultCode, data); |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | if (requestCode == PreferencesActivity.PICK_IMAGE && resultCode == RESULT_OK){ |
---|
| 100 | |
---|
| 101 | Uri selectedImage = data.getData(); |
---|
| 102 | String[] filePathColumn = { MediaStore.Images.Media.DATA }; |
---|
| 103 | |
---|
| 104 | Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null); |
---|
| 105 | cursor.moveToFirst(); |
---|
| 106 | |
---|
| 107 | int columnIndex = cursor.getColumnIndex(filePathColumn[0]); |
---|
| 108 | String imageSelectedPath = cursor.getString(columnIndex); |
---|
| 109 | cursor.close(); |
---|
| 110 | |
---|
| 111 | //Filter images if wrong format. |
---|
| 112 | boolean validImage = this.validateImage(imageSelectedPath); |
---|
| 113 | |
---|
| 114 | if (validImage==true){ |
---|
| 115 | //Save and edit summary text in user interface. |
---|
| 116 | SharedPreferences.Editor editor = this.options.edit(); |
---|
| 117 | editor.putString(PreferencesActivity.imageKey, imageSelectedPath); |
---|
| 118 | editor.commit(); |
---|
| 119 | this.imagePreference.setSummary(imageSelectedPath); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | //Check image dimensions and format. |
---|
| 132 | private boolean validateImage(String imageSelectedPath) { |
---|
| 133 | File f = new File(imageSelectedPath); |
---|
| 134 | long size = f.length(); |
---|
| 135 | |
---|
| 136 | if (size <= (PreferencesActivity.SIZELIMIT)) { |
---|
| 137 | |
---|
| 138 | Bitmap chosenImage = BitmapFactory.decodeFile(imageSelectedPath); |
---|
| 139 | |
---|
| 140 | if (chosenImage !=null) |
---|
| 141 | return this.checkDimensions(chosenImage); |
---|
| 142 | else{ |
---|
| 143 | Toast.makeText(this, this.getString(R.string.errorformat),Toast.LENGTH_SHORT).show(); |
---|
| 144 | return false; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | } else { |
---|
| 148 | Toast.makeText(this, this.getString(R.string.errorsize)+" "+(PreferencesActivity.SIZELIMIT)/(1024*1024)+"MB",Toast.LENGTH_SHORT).show(); |
---|
| 149 | return false; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | private boolean checkDimensions(Bitmap chosenImage) { |
---|
| 158 | |
---|
| 159 | int width = chosenImage.getWidth(); |
---|
| 160 | int height = chosenImage.getHeight(); |
---|
| 161 | |
---|
| 162 | if (width <= PreferencesActivity.MAXWIDTH && height <= PreferencesActivity.MAXHEIGHT) { |
---|
| 163 | return true; |
---|
| 164 | |
---|
| 165 | } else { |
---|
| 166 | |
---|
| 167 | Toast.makeText( this,this.getString(R.string.errordimension) +" "+ PreferencesActivity.MAXWIDTH |
---|
| 168 | + "x" + PreferencesActivity.MAXHEIGHT,Toast.LENGTH_SHORT).show(); |
---|
| 169 | return false; |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | |
---|
| 176 | } |
---|