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