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 | import java.io.FileOutputStream; |
---|
24 | import java.io.IOException; |
---|
25 | import java.util.Calendar; |
---|
26 | import java.util.Vector; |
---|
27 | |
---|
28 | import com.lowagie.text.DocumentException; |
---|
29 | import com.lowagie.text.Image; |
---|
30 | import com.lowagie.text.Rectangle; |
---|
31 | import com.lowagie.text.pdf.PdfContentByte; |
---|
32 | import com.lowagie.text.pdf.PdfReader; |
---|
33 | import com.lowagie.text.pdf.PdfStamper; |
---|
34 | |
---|
35 | import android.app.Activity; |
---|
36 | import android.app.ProgressDialog; |
---|
37 | import android.content.Intent; |
---|
38 | import android.os.AsyncTask; |
---|
39 | import android.os.Bundle; |
---|
40 | import android.os.Environment; |
---|
41 | import android.util.Log; |
---|
42 | import android.view.View; |
---|
43 | import android.view.View.OnClickListener; |
---|
44 | import android.view.Window; |
---|
45 | import android.widget.ArrayAdapter; |
---|
46 | import android.widget.LinearLayout; |
---|
47 | import android.widget.Spinner; |
---|
48 | import android.widget.TextView; |
---|
49 | import android.widget.Toast; |
---|
50 | |
---|
51 | public class SelectSignUbicationHandwrittenSignatureActivity extends Activity{ |
---|
52 | |
---|
53 | private TextView selected_position = null; |
---|
54 | private String fileToSign; |
---|
55 | private String imageCaptured; |
---|
56 | private float x_coef, y_coef; |
---|
57 | private LinearLayout button_accept, button_cancel; |
---|
58 | private PdfReader pdfReader; |
---|
59 | private Spinner spinner_page_number; |
---|
60 | private String tempDir; |
---|
61 | |
---|
62 | @Override |
---|
63 | protected void onCreate(Bundle savedInstanceState) { |
---|
64 | |
---|
65 | |
---|
66 | //Capturando archivo que será firmado |
---|
67 | fileToSign = getIntent().getExtras().getString("FILE_TO_SIGN"); |
---|
68 | //Capturando imagen de firma |
---|
69 | imageCaptured = getIntent().getExtras().getString("IMAGE_CAPTURED"); |
---|
70 | |
---|
71 | //Estilando la barra de titulo |
---|
72 | final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); |
---|
73 | |
---|
74 | super.onCreate(savedInstanceState); |
---|
75 | setContentView(R.layout.activity_select_sign_ubication_handwritten_signature); |
---|
76 | //Estilando Barra de titulo |
---|
77 | if(customTitleSupported) |
---|
78 | getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); |
---|
79 | |
---|
80 | TextView text_view_11 = (TextView) this.findViewById(R.id.textView11); |
---|
81 | TextView text_view_12 = (TextView) this.findViewById(R.id.textView12); |
---|
82 | TextView text_view_13 = (TextView) this.findViewById(R.id.textView13); |
---|
83 | |
---|
84 | TextView text_view_21 = (TextView) this.findViewById(R.id.textView21); |
---|
85 | TextView text_view_22 = (TextView) this.findViewById(R.id.textView22); |
---|
86 | TextView text_view_23 = (TextView) this.findViewById(R.id.textView23); |
---|
87 | |
---|
88 | TextView text_view_31 = (TextView) this.findViewById(R.id.textView31); |
---|
89 | TextView text_view_32 = (TextView) this.findViewById(R.id.textView32); |
---|
90 | TextView text_view_33 = (TextView) this.findViewById(R.id.textView33); |
---|
91 | |
---|
92 | spinner_page_number = (Spinner) findViewById(R.id.pages_spinner); |
---|
93 | |
---|
94 | button_accept = (LinearLayout) this.findViewById(R.id.button_accept_zone); |
---|
95 | button_cancel = (LinearLayout) this.findViewById(R.id.button_cancel_zone); |
---|
96 | |
---|
97 | button_accept.setEnabled(false); |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() { |
---|
105 | |
---|
106 | private ProgressDialog pd; |
---|
107 | |
---|
108 | @Override |
---|
109 | protected void onPreExecute() { |
---|
110 | |
---|
111 | pd = new ProgressDialog(SelectSignUbicationHandwrittenSignatureActivity.this); |
---|
112 | pd.setTitle(getString(R.string.title_progress_file_read)); |
---|
113 | pd.setMessage(getString(R.string.por_favor_epsere)); |
---|
114 | pd.setCancelable(false); |
---|
115 | pd.setIndeterminate(true); |
---|
116 | pd.show(); |
---|
117 | |
---|
118 | } |
---|
119 | |
---|
120 | @Override |
---|
121 | protected Void doInBackground(Void... arg0) { |
---|
122 | |
---|
123 | //Cargar Spinner |
---|
124 | //Cargar PDF |
---|
125 | try{ |
---|
126 | pdfReader = new PdfReader(fileToSign); |
---|
127 | |
---|
128 | } catch (IOException e) { |
---|
129 | e.printStackTrace(); |
---|
130 | |
---|
131 | //Si el archivo no puede ser leído se envía al activity de selectionar archivo |
---|
132 | Intent oIntent = new Intent(SelectSignUbicationHandwrittenSignatureActivity.this, |
---|
133 | FileToSignHandwrittenSignatureActivity.class); |
---|
134 | oIntent.setAction(Intent.ACTION_VIEW); |
---|
135 | oIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); |
---|
136 | startActivity(oIntent); |
---|
137 | finish(); |
---|
138 | Toast.makeText(SelectSignUbicationHandwrittenSignatureActivity.this, |
---|
139 | SelectSignUbicationHandwrittenSignatureActivity.this.getString(R.string.error_lectura), |
---|
140 | Toast.LENGTH_SHORT).show(); |
---|
141 | } |
---|
142 | |
---|
143 | return null; |
---|
144 | } |
---|
145 | @Override |
---|
146 | protected void onPostExecute(Void result) { |
---|
147 | |
---|
148 | int number_pages = pdfReader.getNumberOfPages(); |
---|
149 | |
---|
150 | Vector<String> pages_vector = new Vector<String>(); |
---|
151 | for(int i = 0; i < number_pages ; i++) |
---|
152 | pages_vector.add(String.valueOf(i+1)); |
---|
153 | |
---|
154 | // Create an ArrayAdapter using the string array and a default spinner layout |
---|
155 | ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>( |
---|
156 | SelectSignUbicationHandwrittenSignatureActivity.this, |
---|
157 | android.R.layout.simple_spinner_item, pages_vector); |
---|
158 | // Specify the layout to use when the list of choices appears |
---|
159 | spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
---|
160 | // Apply the adapter to the spinner |
---|
161 | spinner_page_number.setAdapter(spinnerArrayAdapter); |
---|
162 | |
---|
163 | pd.dismiss(); |
---|
164 | |
---|
165 | } |
---|
166 | |
---|
167 | }; |
---|
168 | |
---|
169 | task.execute((Void[])null); |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | button_accept.setOnClickListener(new OnClickListener() |
---|
174 | { |
---|
175 | public void onClick(View v) |
---|
176 | { |
---|
177 | pdfSignatureDig(); |
---|
178 | } |
---|
179 | }); |
---|
180 | |
---|
181 | button_cancel.setOnClickListener(new OnClickListener() |
---|
182 | { |
---|
183 | public void onClick(View v) |
---|
184 | { |
---|
185 | finish(); |
---|
186 | } |
---|
187 | }); |
---|
188 | |
---|
189 | |
---|
190 | OnClickListener listen_area = new OnClickListener() { |
---|
191 | |
---|
192 | @Override |
---|
193 | public void onClick(View v) { |
---|
194 | |
---|
195 | if(!button_accept.isEnabled()) button_accept.setEnabled(true); |
---|
196 | |
---|
197 | if(selected_position != null) { |
---|
198 | selected_position.setBackgroundResource(R.drawable.shape_table1); |
---|
199 | } |
---|
200 | v.setBackgroundColor(getResources().getColor(R.color.gris1)); |
---|
201 | selected_position = (TextView) v; |
---|
202 | |
---|
203 | int cols = 3; |
---|
204 | int rows = 3; |
---|
205 | int row = 0; |
---|
206 | int col = 0; |
---|
207 | switch (v.getId()) { |
---|
208 | case R.id.textView11: |
---|
209 | row = 1; |
---|
210 | col = 1; |
---|
211 | break; |
---|
212 | case R.id.textView12: |
---|
213 | row = 1; |
---|
214 | col = 2; |
---|
215 | break; |
---|
216 | case R.id.textView13: |
---|
217 | row = 1; |
---|
218 | col = 3; |
---|
219 | break; |
---|
220 | case R.id.textView21: |
---|
221 | row = 2; |
---|
222 | col = 1; |
---|
223 | break; |
---|
224 | case R.id.textView22: |
---|
225 | row = 2; |
---|
226 | col = 2; |
---|
227 | break; |
---|
228 | case R.id.textView23: |
---|
229 | row = 2; |
---|
230 | col = 3; |
---|
231 | break; |
---|
232 | case R.id.textView31: |
---|
233 | row = 3; |
---|
234 | col = 1; |
---|
235 | break; |
---|
236 | case R.id.textView32: |
---|
237 | row = 3; |
---|
238 | col = 2; |
---|
239 | break; |
---|
240 | case R.id.textView33: |
---|
241 | row = 3; |
---|
242 | col = 3; |
---|
243 | break; |
---|
244 | |
---|
245 | default: |
---|
246 | break; |
---|
247 | } |
---|
248 | |
---|
249 | // Coeficientes para calcular la posición de la firma |
---|
250 | x_coef = (float)col/(float)cols - 1f/(float)cols/2f; |
---|
251 | y_coef = ((float)rows-(float)row+1)/(float)rows - 1f/(float)rows/2f; |
---|
252 | |
---|
253 | } |
---|
254 | }; |
---|
255 | |
---|
256 | text_view_11.setOnClickListener(listen_area); |
---|
257 | text_view_12.setOnClickListener(listen_area); |
---|
258 | text_view_13.setOnClickListener(listen_area); |
---|
259 | |
---|
260 | text_view_21.setOnClickListener(listen_area); |
---|
261 | text_view_22.setOnClickListener(listen_area); |
---|
262 | text_view_23.setOnClickListener(listen_area); |
---|
263 | |
---|
264 | text_view_31.setOnClickListener(listen_area); |
---|
265 | text_view_32.setOnClickListener(listen_area); |
---|
266 | text_view_33.setOnClickListener(listen_area); |
---|
267 | } |
---|
268 | |
---|
269 | public boolean pdfSignatureDig(){ |
---|
270 | try{ |
---|
271 | // Número de página seleccionada |
---|
272 | int number_page = Integer.parseInt(spinner_page_number.getSelectedItem().toString()); |
---|
273 | // Lee el archivo pdf |
---|
274 | // PdfReader pdfReader = new PdfReader(fileToSign); |
---|
275 | |
---|
276 | // Crea un archivo de salida para estampar la firma |
---|
277 | String uniqueId = getTodaysDate() + "_" + getCurrentTime(); |
---|
278 | |
---|
279 | String output_filename = "firmaManuscrita"+uniqueId+".pdf"; |
---|
280 | |
---|
281 | tempDir = Environment.getExternalStorageDirectory() + "/" + |
---|
282 | getResources().getString(R.string.app_name) + "/" + |
---|
283 | getResources().getString(R.string.external_dir_files) + "/"; |
---|
284 | prepareDirectory(); |
---|
285 | PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(tempDir+output_filename)); |
---|
286 | |
---|
287 | // Obtiene referencia a la imágen de la firma |
---|
288 | Image image = Image.getInstance(imageCaptured); |
---|
289 | |
---|
290 | image.scalePercent(75); //Escala al 75% |
---|
291 | |
---|
292 | // Obtiene las dimensiones de la página |
---|
293 | Rectangle rec = pdfReader.getPageSize(number_page); |
---|
294 | float page_width = rec.getWidth(); |
---|
295 | float page_height = rec.getHeight(); |
---|
296 | |
---|
297 | /* |
---|
298 | * Calcula la posición de la firma basada en los coeficientes |
---|
299 | * y el tamaño de la página a firmar |
---|
300 | */ |
---|
301 | float x = (page_width * x_coef) - image.getWidth() / 2; |
---|
302 | float y = (page_height * y_coef) - image.getHeight() / 2; |
---|
303 | |
---|
304 | //put content under |
---|
305 | PdfContentByte content = pdfStamper.getUnderContent(number_page); |
---|
306 | //put content over |
---|
307 | content = pdfStamper.getOverContent(number_page); |
---|
308 | |
---|
309 | image.setAbsolutePosition(x, y); |
---|
310 | content.addImage(image); |
---|
311 | |
---|
312 | pdfStamper.close(); |
---|
313 | |
---|
314 | // Toast.makeText(getApplicationContext(), "Archivo firmado correctamente...", Toast.LENGTH_LONG).show(); |
---|
315 | // finish(); |
---|
316 | Intent intent = new Intent(SelectSignUbicationHandwrittenSignatureActivity.this, |
---|
317 | SignResultHandwrittenSignatureActivity.class); |
---|
318 | intent.putExtra("STATUS", "done"); |
---|
319 | intent.putExtra("IMAGE_CAPTURED", imageCaptured); |
---|
320 | intent.putExtra("FILE_TO_SIGN", fileToSign); |
---|
321 | intent.putExtra("FILE_SIGNED", output_filename); |
---|
322 | intent.putExtra("PATH_FILE_SIGNED", tempDir); |
---|
323 | startActivity(intent); |
---|
324 | |
---|
325 | }catch (IOException e) { |
---|
326 | e.printStackTrace(); |
---|
327 | Toast.makeText(getApplicationContext(), "Error al firmar el archivo", Toast.LENGTH_LONG).show(); |
---|
328 | }catch (DocumentException e) { |
---|
329 | e.printStackTrace(); |
---|
330 | Toast.makeText(getApplicationContext(), "Error al firmar el archivo", Toast.LENGTH_LONG).show(); |
---|
331 | } |
---|
332 | return true; |
---|
333 | } |
---|
334 | |
---|
335 | private String getTodaysDate() { |
---|
336 | |
---|
337 | final Calendar c = Calendar.getInstance(); |
---|
338 | int todaysDate = (c.get(Calendar.YEAR) * 10000) + |
---|
339 | ((c.get(Calendar.MONTH) + 1) * 100) + |
---|
340 | (c.get(Calendar.DAY_OF_MONTH)); |
---|
341 | return(String.valueOf(todaysDate)); |
---|
342 | |
---|
343 | } |
---|
344 | |
---|
345 | private String getCurrentTime() { |
---|
346 | |
---|
347 | final Calendar c = Calendar.getInstance(); |
---|
348 | int currentTime = (c.get(Calendar.HOUR_OF_DAY) * 10000) + |
---|
349 | (c.get(Calendar.MINUTE) * 100) + |
---|
350 | (c.get(Calendar.SECOND)); |
---|
351 | return(String.valueOf(currentTime)); |
---|
352 | |
---|
353 | } |
---|
354 | |
---|
355 | /** |
---|
356 | * Prepara directorio |
---|
357 | * @return boolean |
---|
358 | */ |
---|
359 | private boolean prepareDirectory() |
---|
360 | { |
---|
361 | try |
---|
362 | { |
---|
363 | if (makedirs()) |
---|
364 | { |
---|
365 | return true; |
---|
366 | } else { |
---|
367 | return false; |
---|
368 | } |
---|
369 | } catch (Exception e) |
---|
370 | { |
---|
371 | e.printStackTrace(); |
---|
372 | Toast.makeText(this, "Could not initiate File System.. Is Sdcard mounted properly?", Toast.LENGTH_LONG).show(); |
---|
373 | return false; |
---|
374 | } |
---|
375 | } |
---|
376 | |
---|
377 | /** |
---|
378 | * Crea directorio utilizando la variable tmpDir |
---|
379 | * @return boolean |
---|
380 | */ |
---|
381 | private boolean makedirs() |
---|
382 | { |
---|
383 | File tempdir = new File(tempDir); |
---|
384 | if (!tempdir.exists()) |
---|
385 | tempdir.mkdirs(); |
---|
386 | |
---|
387 | // if (tempdir.isDirectory()) |
---|
388 | // { |
---|
389 | // File[] files = tempdir.listFiles(); |
---|
390 | // for (File file : files) |
---|
391 | // { |
---|
392 | // if (!file.delete()) |
---|
393 | // { |
---|
394 | // System.out.println("Failed to delete " + file); |
---|
395 | // } |
---|
396 | // } |
---|
397 | // } |
---|
398 | return (tempdir.isDirectory()); |
---|
399 | } |
---|
400 | |
---|
401 | |
---|
402 | } |
---|