1 | package ve.gob.cenditel.tibisaymovil; |
---|
2 | |
---|
3 | import java.io.ByteArrayInputStream; |
---|
4 | import java.io.ByteArrayOutputStream; |
---|
5 | import java.io.File; |
---|
6 | import java.io.FileOutputStream; |
---|
7 | import java.io.IOException; |
---|
8 | import java.io.InputStream; |
---|
9 | import java.io.ObjectOutputStream; |
---|
10 | import java.security.PrivateKey; |
---|
11 | import java.security.cert.X509Certificate; |
---|
12 | import java.util.ArrayList; |
---|
13 | import java.util.List; |
---|
14 | |
---|
15 | import com.sun.jersey.api.client.ClientHandlerException; |
---|
16 | import com.sun.jersey.api.client.UniformInterfaceException; |
---|
17 | |
---|
18 | import android.app.Activity; |
---|
19 | import android.content.Context; |
---|
20 | import android.content.Intent; |
---|
21 | import android.content.res.Resources; |
---|
22 | import android.net.Uri; |
---|
23 | import android.os.Bundle; |
---|
24 | import android.os.Environment; |
---|
25 | import android.text.method.ScrollingMovementMethod; |
---|
26 | import android.util.Log; |
---|
27 | import android.view.View; |
---|
28 | import android.view.Window; |
---|
29 | import android.view.View.OnClickListener; |
---|
30 | import android.widget.Button; |
---|
31 | import android.widget.LinearLayout; |
---|
32 | import android.widget.TextView; |
---|
33 | import android.widget.Toast; |
---|
34 | |
---|
35 | |
---|
36 | import com.lowagie.text.exceptions.InvalidPdfException; |
---|
37 | // prueba de droidtext |
---|
38 | import com.lowagie.text.pdf.*; |
---|
39 | import com.lowagie.text.*; |
---|
40 | |
---|
41 | public class SignActivity extends Activity implements OnClickListener { |
---|
42 | |
---|
43 | private boolean embedInPdf; |
---|
44 | private String alias; |
---|
45 | private List<Uri> uris; |
---|
46 | private SignView viewHolder; |
---|
47 | private List<Uri> signed; |
---|
48 | private List<Uri> unsigned; |
---|
49 | private ArrayList<Uri> send = new ArrayList<Uri>(); |
---|
50 | private SignTask task; |
---|
51 | |
---|
52 | /** Called when the activity is first created. */ |
---|
53 | @Override |
---|
54 | public void onCreate(Bundle savedInstanceState) { |
---|
55 | //Estilando la barra de titulo |
---|
56 | final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); |
---|
57 | |
---|
58 | super.onCreate(savedInstanceState); |
---|
59 | this.embedInPdf = getIntent().getBooleanExtra(IntentExtraField.EMBED_SIGNATURE_IN_PDF, false); |
---|
60 | this.alias = getIntent().getStringExtra(IntentExtraField.ALIAS); |
---|
61 | this.uris = getIntent().getParcelableArrayListExtra(IntentExtraField.FILES); |
---|
62 | this.viewHolder = new SignView(); |
---|
63 | |
---|
64 | this.task = (SignTask) getLastNonConfigurationInstance(); |
---|
65 | if (this.task != null) { |
---|
66 | this.task.activity = this; |
---|
67 | } else if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { |
---|
68 | this.task = (SignTask) new SignTask(this, FsUtils.createOutputDirectory(this), this).execute( |
---|
69 | uris.toArray(new Uri[uris.size()])); |
---|
70 | } else { |
---|
71 | this.viewHolder.noStorageError(); |
---|
72 | } |
---|
73 | |
---|
74 | if (savedInstanceState != null) { |
---|
75 | this.send = savedInstanceState.getParcelableArrayList("send"); |
---|
76 | this.viewHolder.done(); |
---|
77 | } |
---|
78 | //Estilando Barra de titulo |
---|
79 | if(customTitleSupported) |
---|
80 | getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); |
---|
81 | } |
---|
82 | |
---|
83 | @Override |
---|
84 | public Object onRetainNonConfigurationInstance() { |
---|
85 | |
---|
86 | return this.task; |
---|
87 | } |
---|
88 | |
---|
89 | @Override |
---|
90 | protected void onDestroy() { |
---|
91 | this.task.cancel(true); |
---|
92 | |
---|
93 | if (this.task != null) { |
---|
94 | this.task.activity = null; |
---|
95 | } |
---|
96 | super.onDestroy(); |
---|
97 | } |
---|
98 | |
---|
99 | @Override |
---|
100 | protected void onSaveInstanceState(Bundle outState) { |
---|
101 | |
---|
102 | super.onSaveInstanceState(outState); |
---|
103 | outState.putParcelableArrayList("send", this.send); |
---|
104 | } |
---|
105 | |
---|
106 | @Override |
---|
107 | public void onBackPressed() { |
---|
108 | Intent back = new Intent(this, TibisayMovilActivity.class); |
---|
109 | startActivity(back); |
---|
110 | finish(); |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | @Override |
---|
115 | public void onClick(View v) { |
---|
116 | |
---|
117 | switch (v.getId()) { |
---|
118 | |
---|
119 | case R.id.button_finish_zone: |
---|
120 | Intent back = new Intent(this, TibisayMovilActivity.class); |
---|
121 | startActivity(back); |
---|
122 | finish(); |
---|
123 | break; |
---|
124 | |
---|
125 | case R.id.button_share_zone: |
---|
126 | Intent intent = IntentUtils.sendMultipleFiles(this.send); |
---|
127 | //In case user has no email application. |
---|
128 | try{ |
---|
129 | startActivity(intent); |
---|
130 | finish(); |
---|
131 | }catch (android.content.ActivityNotFoundException e){ |
---|
132 | Toast.makeText(this, this.getString(R.string.errornoemail), Toast.LENGTH_SHORT).show(); |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | private static class SignTask extends AsyncErrorTask<Uri, Void, List<Uri>> { |
---|
138 | |
---|
139 | private SignActivity activity; |
---|
140 | private String directory; |
---|
141 | private Context context; |
---|
142 | |
---|
143 | public SignTask(SignActivity activity, String directory, Context context) { |
---|
144 | |
---|
145 | this.activity = activity; |
---|
146 | this.directory = directory; |
---|
147 | this.context = context; |
---|
148 | } |
---|
149 | |
---|
150 | @Override |
---|
151 | protected List<Uri> doInBackground(Uri... uris) { |
---|
152 | |
---|
153 | X509Certificate[] chain; |
---|
154 | X509Certificate certificate; |
---|
155 | PrivateKey key; |
---|
156 | KeyChainStrategy keyChain = KeyChainStrategy.getInstance(); |
---|
157 | |
---|
158 | try { |
---|
159 | chain = keyChain.getCertificateChain(this.activity.alias); |
---|
160 | certificate = chain[0]; |
---|
161 | key = keyChain.getPrivateKey(this.activity.alias); |
---|
162 | } catch (Exception e) { |
---|
163 | this.cancel(true); |
---|
164 | chain=null; |
---|
165 | key=null; |
---|
166 | certificate=null; |
---|
167 | throw new RuntimeException(e); |
---|
168 | } |
---|
169 | |
---|
170 | ArrayList<Uri> signed = new ArrayList<Uri>(uris.length); |
---|
171 | |
---|
172 | //NEW: History. |
---|
173 | ArrayList<String> sourcesPath = new ArrayList<String>(); |
---|
174 | ArrayList<String> destinationsPath = new ArrayList<String>(); |
---|
175 | |
---|
176 | Log.d("SignActivity:SignTask", "antes del for"); |
---|
177 | |
---|
178 | for (Uri uri : uris) { |
---|
179 | Uri signedUri = null; |
---|
180 | |
---|
181 | try { |
---|
182 | String inputFilename = uri.getPath(); |
---|
183 | if (this.activity.embedInPdf && FsUtils.isPDF(inputFilename)) { |
---|
184 | |
---|
185 | |
---|
186 | // ******************************************************************** |
---|
187 | Log.d("SignActivity", "antes de CryptoUtils.signPDF"); |
---|
188 | |
---|
189 | PdfReader reader = new PdfReader(inputFilename); |
---|
190 | |
---|
191 | Log.d("SignActivity", "new PdfReader"); |
---|
192 | |
---|
193 | int n; |
---|
194 | String targetFilename = new File(this.directory, uri.getLastPathSegment()).getPath(); |
---|
195 | if ((n = targetFilename.lastIndexOf(".")) > targetFilename.lastIndexOf("/")) { |
---|
196 | targetFilename = targetFilename.substring(0, n) + "-firma.pdf"; |
---|
197 | } else { |
---|
198 | targetFilename = targetFilename + "-firma.pdf"; |
---|
199 | } |
---|
200 | Log.d("SignActivity", "targetFilename: "+targetFilename); |
---|
201 | |
---|
202 | FileOutputStream fout = new FileOutputStream(targetFilename); |
---|
203 | |
---|
204 | PdfStamper stp = PdfStamper.createSignature(reader, fout, '?'); |
---|
205 | |
---|
206 | Log.d("SignActivity", "PdfStamper.createSignature"); |
---|
207 | |
---|
208 | PdfSignatureAppearance sap = stp.getSignatureAppearance(); |
---|
209 | |
---|
210 | Log.d("SignActivity", "stp.getSignatureAppearance()"); |
---|
211 | |
---|
212 | sap.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED); |
---|
213 | |
---|
214 | Log.d("SignActivity", "sap.setCrypto"); |
---|
215 | |
---|
216 | sap.setReason("Firma PKCS12 Android"); |
---|
217 | |
---|
218 | Log.d("SignActivity", "sap.setReason"); |
---|
219 | |
---|
220 | sap.setLocation("Imaginanet"); |
---|
221 | |
---|
222 | Log.d("SignActivity", "sap.setLocation"); |
---|
223 | |
---|
224 | stp.close(); |
---|
225 | |
---|
226 | Log.d("SignActivity", "stp.close"); |
---|
227 | |
---|
228 | //signedUri = FsUtils.saveToFile(fout, targetFilename); |
---|
229 | File file = new File(targetFilename); |
---|
230 | signedUri = Uri.fromFile(file); |
---|
231 | |
---|
232 | // ******************************************************************** |
---|
233 | /* |
---|
234 | InputStream signedPDF = CryptoUtils.signPDF(new File(inputFilename), chain, key, this.context); |
---|
235 | int n; |
---|
236 | String targetFilename = new File(this.directory, uri.getLastPathSegment()).getPath(); |
---|
237 | if ((n = targetFilename.lastIndexOf(".")) > targetFilename.lastIndexOf("/")) { |
---|
238 | targetFilename = targetFilename.substring(0, n) + "-firma.pdf"; |
---|
239 | } else { |
---|
240 | targetFilename = targetFilename + "-firma.pdf"; |
---|
241 | } |
---|
242 | signedUri = FsUtils.saveToFile(signedPDF, targetFilename); |
---|
243 | */ |
---|
244 | |
---|
245 | //TODO: signedUri for uri |
---|
246 | signed.add(uri); |
---|
247 | |
---|
248 | this.activity.send.add(signedUri); |
---|
249 | } else { |
---|
250 | FsUtils.copyTo(new File(uri.getPath()), this.directory); |
---|
251 | |
---|
252 | |
---|
253 | Log.d("SignActivity", "antes de CryptoUtils.signPKCS7"); |
---|
254 | |
---|
255 | byte[] signature = CryptoUtils.signPKCS7(new File(inputFilename), certificate, key); |
---|
256 | int n; |
---|
257 | String targetFilename = new File(this.directory, uri.getLastPathSegment()).getPath(); |
---|
258 | if ((n = targetFilename.lastIndexOf(".")) > targetFilename.lastIndexOf("/")) { |
---|
259 | targetFilename = targetFilename.substring(0, n) + ".p7b"; |
---|
260 | } else { |
---|
261 | targetFilename = targetFilename + ".p7b"; |
---|
262 | } |
---|
263 | Log.d("SignActivity", "antes de signedUri"); |
---|
264 | signedUri = FsUtils.saveToFile(signature, targetFilename); |
---|
265 | signed.add(uri); |
---|
266 | this.activity.send.add(uri); |
---|
267 | this.activity.send.add(signedUri); |
---|
268 | Log.d("SignActivity", "despues de signedUri"); |
---|
269 | } |
---|
270 | } catch (FileAlreadyExistsException e) { |
---|
271 | final Activity act = ((Activity) this.context); |
---|
272 | Toast.makeText(act, act.getString(R.string.errorServerNotReachable), Toast.LENGTH_SHORT).show(); |
---|
273 | |
---|
274 | //} catch (DocumentWritingException e) { |
---|
275 | |
---|
276 | } catch (ClientHandlerException e){ |
---|
277 | final Activity act = ((Activity) this.context); |
---|
278 | act.runOnUiThread(new Runnable() { |
---|
279 | public void run() { |
---|
280 | Toast.makeText(act, act.getString(R.string.errorServerNotReachable), Toast.LENGTH_SHORT).show(); |
---|
281 | } |
---|
282 | }); |
---|
283 | } catch (UniformInterfaceException e) { |
---|
284 | final Activity act = ((Activity) this.context); |
---|
285 | act.runOnUiThread(new Runnable() { |
---|
286 | public void run() { |
---|
287 | Toast.makeText(act, act.getString(R.string.errorConnection), Toast.LENGTH_SHORT).show(); |
---|
288 | } |
---|
289 | }); |
---|
290 | } catch (InvalidPdfException e) { |
---|
291 | //empty |
---|
292 | } catch (Exception e) { |
---|
293 | final Activity act = ((Activity) this.context); |
---|
294 | Toast.makeText(act, "exception", Toast.LENGTH_SHORT).show(); |
---|
295 | FsUtils.checkOutOfSpace(e, true); |
---|
296 | this.cancel(true); |
---|
297 | //throw new RuntimeException(e); |
---|
298 | } |
---|
299 | |
---|
300 | |
---|
301 | //HISTORY: |
---|
302 | //Fixed spaces path error with getPath instead of getEncodedPath |
---|
303 | finally{ |
---|
304 | sourcesPath.add(uri.getPath()); |
---|
305 | if (signedUri!=null) |
---|
306 | destinationsPath.add(signedUri.getPath()); |
---|
307 | else |
---|
308 | destinationsPath.add(""); |
---|
309 | |
---|
310 | |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | |
---|
315 | // DIR Save history.mbs |
---|
316 | String[] pathSplit = this.directory.split("/"); |
---|
317 | String dirName = pathSplit[pathSplit.length-1]; |
---|
318 | File targetHistory = new File(this.directory, dirName); |
---|
319 | |
---|
320 | //File targetHistory = new File(this.directory, "history.mbs"); |
---|
321 | |
---|
322 | String alias = this.activity.alias; |
---|
323 | // this.saveHistoryData(alias, CertificateUtils.getSubject(chain[0]), |
---|
324 | // CertificateUtils.getSerial(chain[0]), |
---|
325 | // CertificateUtils.getIssuer(chain[0]),sourcesPath, destinationsPath, targetHistory); |
---|
326 | //---------- |
---|
327 | |
---|
328 | |
---|
329 | return signed; |
---|
330 | } |
---|
331 | |
---|
332 | |
---|
333 | |
---|
334 | |
---|
335 | |
---|
336 | |
---|
337 | @Override |
---|
338 | protected void onPostExecute(List<Uri> signed) { |
---|
339 | |
---|
340 | if (this.activity != null) { |
---|
341 | if (getError() != null) { |
---|
342 | if (getError().getCause() instanceof OutOfSpaceError) { |
---|
343 | this.activity.viewHolder.outOfSpaceError(); |
---|
344 | } else { |
---|
345 | this.activity.viewHolder.unknownError(); |
---|
346 | } |
---|
347 | return; |
---|
348 | } |
---|
349 | this.activity.signed = signed; |
---|
350 | if (signed.size() < this.activity.uris.size()) { |
---|
351 | this.activity.unsigned = new ArrayList<Uri>(this.activity.uris); |
---|
352 | this.activity.unsigned.removeAll(signed); |
---|
353 | |
---|
354 | } |
---|
355 | this.activity.viewHolder.update(); |
---|
356 | |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | |
---|
361 | // private void saveHistoryData(String alias, String subject, String serial, String issuer, List<String> files, |
---|
362 | // ArrayList<String> unsigned, File path) { |
---|
363 | // |
---|
364 | // |
---|
365 | // HistoryData data = new HistoryData (alias, subject, serial, issuer, files, unsigned); |
---|
366 | // |
---|
367 | // ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
---|
368 | // try{ |
---|
369 | // ObjectOutputStream oos = new ObjectOutputStream(baos); |
---|
370 | // oos.writeObject(data); |
---|
371 | // oos.flush(); |
---|
372 | // oos.close(); |
---|
373 | // }catch(IOException e){} |
---|
374 | // InputStream is = new ByteArrayInputStream(baos.toByteArray()); |
---|
375 | // |
---|
376 | // try { |
---|
377 | // FsUtils.saveToFile(is, path.getPath()); |
---|
378 | // } catch (Exception e){} |
---|
379 | // |
---|
380 | // |
---|
381 | // } |
---|
382 | |
---|
383 | |
---|
384 | } |
---|
385 | |
---|
386 | private class SignView { |
---|
387 | |
---|
388 | public LinearLayout finish; |
---|
389 | public LinearLayout share; |
---|
390 | public TextView summary; |
---|
391 | public TextView summary2; |
---|
392 | public View signing; |
---|
393 | |
---|
394 | public SignView () { |
---|
395 | setContentView(R.layout.sign); |
---|
396 | |
---|
397 | this.finish = (LinearLayout) findViewById(R.id.button_finish_zone); |
---|
398 | this.finish.setOnClickListener(SignActivity.this); |
---|
399 | |
---|
400 | this.share = (LinearLayout) findViewById(R.id.button_share_zone); |
---|
401 | this.share.setOnClickListener(SignActivity.this); |
---|
402 | |
---|
403 | this.summary = (TextView) findViewById(R.id.summary); |
---|
404 | this.summary.setMovementMethod(new ScrollingMovementMethod()); |
---|
405 | this.summary2 = (TextView) findViewById(R.id.summary2); |
---|
406 | this.summary2.setMovementMethod(new ScrollingMovementMethod()); |
---|
407 | |
---|
408 | this.signing = findViewById(R.id.signing); |
---|
409 | } |
---|
410 | |
---|
411 | public void noStorageError() { |
---|
412 | |
---|
413 | this.summary.setText(R.string.no_storage); |
---|
414 | this.signing.setVisibility(View.GONE); |
---|
415 | } |
---|
416 | |
---|
417 | public void outOfSpaceError() { |
---|
418 | |
---|
419 | this.summary.setText(R.string.error_no_space); |
---|
420 | this.signing.setVisibility(View.GONE); |
---|
421 | } |
---|
422 | |
---|
423 | public void unknownError() { |
---|
424 | |
---|
425 | this.summary.setText(R.string.error_unknown); |
---|
426 | this.signing.setVisibility(View.GONE); |
---|
427 | } |
---|
428 | |
---|
429 | public void update() { |
---|
430 | |
---|
431 | StringBuilder builder = new StringBuilder(); |
---|
432 | StringBuilder builder2 = new StringBuilder(); |
---|
433 | Resources res = SignActivity.this.getResources(); |
---|
434 | |
---|
435 | //Fix Error #75235 |
---|
436 | if (SignActivity.this.signed != null && !SignActivity.this.signed.isEmpty()) { |
---|
437 | // builder.append(res.getString(R.string.signedfiles)); |
---|
438 | for (Uri file : SignActivity.this.signed) { |
---|
439 | builder.append("\n - "); |
---|
440 | builder.append(file.getLastPathSegment()); |
---|
441 | } |
---|
442 | // builder.append("\n\n"); |
---|
443 | } |
---|
444 | if (SignActivity.this.unsigned != null && !SignActivity.this.unsigned.isEmpty()) { |
---|
445 | |
---|
446 | builder2.append(res.getString(R.string.unsigned2)); |
---|
447 | for (Uri file : SignActivity.this.unsigned) { |
---|
448 | builder2.append("\n - "); |
---|
449 | builder2.append(file.getLastPathSegment()); |
---|
450 | } |
---|
451 | |
---|
452 | |
---|
453 | } |
---|
454 | |
---|
455 | this.summary.setText(builder); |
---|
456 | this.summary2.setText(builder2); |
---|
457 | done(); |
---|
458 | } |
---|
459 | |
---|
460 | public void done() { |
---|
461 | |
---|
462 | this.signing.setVisibility(View.GONE); |
---|
463 | this.share.setEnabled(true); |
---|
464 | } |
---|
465 | } |
---|
466 | } |
---|