1 | /* |
---|
2 | Tibisay Movil |
---|
3 | |
---|
4 | Copyright (C) 2013 Antonio Araujo (aaraujo@cenditel.gob.ve), Jose Ruiz |
---|
5 | (jruiz@cenditel.gob.ve), Fundacion Centro Nacional de Desarrollo e |
---|
6 | Investigacion en Tecnologias Libres - CENDITEL. |
---|
7 | |
---|
8 | La Fundación CENDITEL concede permiso para usar, copiar, distribuir y/o |
---|
9 | modificar este programa, reconociendo el derecho que la humanidad posee al |
---|
10 | libre acceso al conocimiento, bajo los términos de la licencia de software |
---|
11 | GPL versión 2.0 de la Free Software Foundation. |
---|
12 | |
---|
13 | Este programa se distribuye con la esperanza de que sea util, pero SIN |
---|
14 | NINGUNA GARANTIA; tampoco las implicitas garantias de MERCANTILIDAD o |
---|
15 | ADECUACION A UN PROPOSITO PARTICULAR. |
---|
16 | |
---|
17 | Para mayor información sobre los términos de la licencia ver el archivo |
---|
18 | llamado "gpl-2.0.txt" en ingles. |
---|
19 | */ |
---|
20 | |
---|
21 | package ve.gob.cenditel.tibisaymovil; |
---|
22 | import java.io.File; |
---|
23 | |
---|
24 | import android.content.Intent; |
---|
25 | import android.content.SharedPreferences; |
---|
26 | import android.database.Cursor; |
---|
27 | import android.graphics.Bitmap; |
---|
28 | import android.graphics.BitmapFactory; |
---|
29 | import android.net.Uri; |
---|
30 | import android.os.Bundle; |
---|
31 | import android.preference.ListPreference; |
---|
32 | import android.preference.Preference; |
---|
33 | import android.preference.Preference.OnPreferenceChangeListener; |
---|
34 | import android.preference.Preference.OnPreferenceClickListener; |
---|
35 | import android.preference.PreferenceActivity; |
---|
36 | import android.provider.MediaStore; |
---|
37 | import android.view.Window; |
---|
38 | import android.widget.Toast; |
---|
39 | |
---|
40 | public class PreferencesActivity extends PreferenceActivity implements OnPreferenceClickListener,OnPreferenceChangeListener{ |
---|
41 | |
---|
42 | static final int PICK_IMAGE = 33; |
---|
43 | public static final String imageKey = "image"; |
---|
44 | public static final String positionKey = "position"; |
---|
45 | private static final int MAXWIDTH = 400; |
---|
46 | private static final int MAXHEIGHT = 400; |
---|
47 | private static final long SIZELIMIT = 1024 * 1024; |
---|
48 | |
---|
49 | private SharedPreferences options; |
---|
50 | private Preference imagePreference; |
---|
51 | |
---|
52 | @Override |
---|
53 | public void onCreate(Bundle savedInstanceState) { |
---|
54 | //Estilando la barra de titulo |
---|
55 | final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); |
---|
56 | |
---|
57 | super.onCreate(savedInstanceState); |
---|
58 | |
---|
59 | addPreferencesFromResource(R.xml.preferences); |
---|
60 | |
---|
61 | this.options = this.getPreferenceManager().getSharedPreferences(); |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | this.imagePreference = (Preference) findPreference(PreferencesActivity.imageKey); |
---|
67 | this.imagePreference.setOnPreferenceClickListener(this); |
---|
68 | this.imagePreference.setOnPreferenceChangeListener(this); |
---|
69 | |
---|
70 | this.loadSummary(); |
---|
71 | |
---|
72 | |
---|
73 | //Estilando Barra de titulo |
---|
74 | if(customTitleSupported) |
---|
75 | getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); |
---|
76 | |
---|
77 | } |
---|
78 | |
---|
79 | private void loadSummary() { |
---|
80 | String path = options.getString(PreferencesActivity.imageKey, ""); |
---|
81 | if (!path.equals("")){ |
---|
82 | this.imagePreference.setSummary(path); |
---|
83 | } |
---|
84 | |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | @Override |
---|
90 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
---|
91 | String summary=""; |
---|
92 | |
---|
93 | |
---|
94 | if (summary!=null && !summary.equals("")){ |
---|
95 | preference.setSummary((CharSequence) summary); |
---|
96 | |
---|
97 | } |
---|
98 | return true; |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | @Override |
---|
104 | public boolean onPreferenceClick(Preference preference) { |
---|
105 | |
---|
106 | if (preference.getKey().equals(PreferencesActivity.imageKey)){ |
---|
107 | Intent i = new Intent( |
---|
108 | Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); |
---|
109 | startActivityForResult(i, PreferencesActivity.PICK_IMAGE); |
---|
110 | return true; |
---|
111 | } |
---|
112 | return false; |
---|
113 | } |
---|
114 | |
---|
115 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
---|
116 | super.onActivityResult(requestCode, resultCode, data); |
---|
117 | |
---|
118 | |
---|
119 | if (requestCode == PreferencesActivity.PICK_IMAGE && resultCode == RESULT_OK){ |
---|
120 | |
---|
121 | Uri selectedImage = data.getData(); |
---|
122 | String[] filePathColumn = { MediaStore.Images.Media.DATA }; |
---|
123 | |
---|
124 | Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null); |
---|
125 | cursor.moveToFirst(); |
---|
126 | |
---|
127 | int columnIndex = cursor.getColumnIndex(filePathColumn[0]); |
---|
128 | String imageSelectedPath = cursor.getString(columnIndex); |
---|
129 | cursor.close(); |
---|
130 | |
---|
131 | //Filter images if wrong format. |
---|
132 | boolean validImage = this.validateImage(imageSelectedPath); |
---|
133 | |
---|
134 | if (validImage==true){ |
---|
135 | //Save and edit summary text in user interface. |
---|
136 | SharedPreferences.Editor editor = this.options.edit(); |
---|
137 | editor.putString(PreferencesActivity.imageKey, imageSelectedPath); |
---|
138 | editor.commit(); |
---|
139 | this.imagePreference.setSummary(imageSelectedPath); |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | } |
---|
150 | |
---|
151 | //Check image dimensions and format. |
---|
152 | private boolean validateImage(String imageSelectedPath) { |
---|
153 | File f = new File(imageSelectedPath); |
---|
154 | long size = f.length(); |
---|
155 | |
---|
156 | if (size <= (PreferencesActivity.SIZELIMIT)) { |
---|
157 | |
---|
158 | Bitmap chosenImage = BitmapFactory.decodeFile(imageSelectedPath); |
---|
159 | |
---|
160 | if (chosenImage !=null) |
---|
161 | return this.checkDimensions(chosenImage); |
---|
162 | else{ |
---|
163 | Toast.makeText(this, this.getString(R.string.errorformat),Toast.LENGTH_SHORT).show(); |
---|
164 | return false; |
---|
165 | } |
---|
166 | |
---|
167 | } else { |
---|
168 | Toast.makeText(this, this.getString(R.string.errorsize)+" "+(PreferencesActivity.SIZELIMIT)/(1024*1024)+"MB",Toast.LENGTH_SHORT).show(); |
---|
169 | return false; |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | private boolean checkDimensions(Bitmap chosenImage) { |
---|
178 | |
---|
179 | int width = chosenImage.getWidth(); |
---|
180 | int height = chosenImage.getHeight(); |
---|
181 | |
---|
182 | if (width <= PreferencesActivity.MAXWIDTH && height <= PreferencesActivity.MAXHEIGHT) { |
---|
183 | return true; |
---|
184 | |
---|
185 | } else { |
---|
186 | |
---|
187 | Toast.makeText( this,this.getString(R.string.errordimension) +" "+ PreferencesActivity.MAXWIDTH |
---|
188 | + "x" + PreferencesActivity.MAXHEIGHT,Toast.LENGTH_SHORT).show(); |
---|
189 | return false; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | |
---|
196 | } |
---|