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 | |
---|
23 | import java.io.File; |
---|
24 | import java.io.FileOutputStream; |
---|
25 | import java.util.Calendar; |
---|
26 | import android.app.Activity; |
---|
27 | import android.content.Context; |
---|
28 | import android.content.ContextWrapper; |
---|
29 | import android.content.Intent; |
---|
30 | import android.graphics.Bitmap; |
---|
31 | import android.graphics.Canvas; |
---|
32 | import android.graphics.Color; |
---|
33 | import android.graphics.Paint; |
---|
34 | import android.graphics.Path; |
---|
35 | import android.graphics.RectF; |
---|
36 | import android.os.Bundle; |
---|
37 | import android.os.Environment; |
---|
38 | import android.provider.MediaStore.Images; |
---|
39 | import android.util.AttributeSet; |
---|
40 | import android.util.Log; |
---|
41 | import android.view.MotionEvent; |
---|
42 | import android.view.View; |
---|
43 | import android.view.Window; |
---|
44 | import android.view.View.OnClickListener; |
---|
45 | import android.view.ViewGroup.LayoutParams; |
---|
46 | import android.widget.LinearLayout; |
---|
47 | import android.widget.Toast; |
---|
48 | |
---|
49 | public class CaptureImgHandwrittenSignatureActivity extends Activity{ |
---|
50 | |
---|
51 | LinearLayout mContent; |
---|
52 | signature mSignature; |
---|
53 | LinearLayout mClear, mGetSign, mCancel; |
---|
54 | |
---|
55 | public static String tempDir; |
---|
56 | public int count = 1; |
---|
57 | public String current = null; |
---|
58 | |
---|
59 | private Bitmap mBitmap; |
---|
60 | View mView; |
---|
61 | File mypath; |
---|
62 | private Canvas canvas; |
---|
63 | |
---|
64 | private String uniqueId; |
---|
65 | private String fileToSign; |
---|
66 | |
---|
67 | @Override |
---|
68 | public void onCreate(Bundle savedInstanceState) |
---|
69 | { |
---|
70 | super.onCreate(savedInstanceState); |
---|
71 | //Capturando archivo que será firmado |
---|
72 | fileToSign = getIntent().getExtras().getString("FILE_TO_SIGN"); |
---|
73 | |
---|
74 | //Estilando la barra de titulo |
---|
75 | final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); |
---|
76 | setContentView(R.layout.activity_capture_img_handwritten_signature); |
---|
77 | //Estilando Barra de titulo |
---|
78 | if(customTitleSupported) |
---|
79 | getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); |
---|
80 | |
---|
81 | tempDir = Environment.getExternalStorageDirectory() + "/" + |
---|
82 | getResources().getString(R.string.app_name) + "/" + |
---|
83 | getResources().getString(R.string.external_dir) + "/"; |
---|
84 | |
---|
85 | /* |
---|
86 | * Crea si es necesario, un directorio en el cual la aplicación |
---|
87 | * puede colocar sus propios archivos de datos personalizados |
---|
88 | */ |
---|
89 | ContextWrapper cw = new ContextWrapper(getApplicationContext()); |
---|
90 | File directory = cw.getDir(getResources().getString(R.string.external_dir), Context.MODE_PRIVATE); |
---|
91 | File directory2 = cw.getDir(getResources().getString(R.string.external_dir_files), Context.MODE_PRIVATE); |
---|
92 | |
---|
93 | prepareDirectory(); |
---|
94 | |
---|
95 | // Obtiene identificador unico para el nombre del archivo |
---|
96 | uniqueId = getTodaysDate() + "_" + getCurrentTime(); |
---|
97 | current = uniqueId + ".png"; |
---|
98 | |
---|
99 | //Ruta completa para crear el archivo |
---|
100 | mypath= new File(tempDir,current); |
---|
101 | |
---|
102 | // Obtiene referencia a los recursos del layout |
---|
103 | mContent = (LinearLayout) findViewById(R.id.linearLayout); |
---|
104 | mClear = (LinearLayout)findViewById(R.id.button_clear_zone); |
---|
105 | mGetSign = (LinearLayout)findViewById(R.id.button_accept_zone); |
---|
106 | mCancel = (LinearLayout)findViewById(R.id.button_cancel_zone); |
---|
107 | |
---|
108 | mSignature = new signature(this, null); |
---|
109 | mContent.addView(mSignature, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); |
---|
110 | mGetSign.setEnabled(false); |
---|
111 | mView = mContent; |
---|
112 | |
---|
113 | |
---|
114 | mClear.setOnClickListener(new OnClickListener() |
---|
115 | { |
---|
116 | public void onClick(View v) |
---|
117 | { |
---|
118 | mSignature.clear(); |
---|
119 | mGetSign.setEnabled(false); |
---|
120 | } |
---|
121 | }); |
---|
122 | |
---|
123 | mGetSign.setOnClickListener(new OnClickListener() |
---|
124 | { |
---|
125 | public void onClick(View v) |
---|
126 | { |
---|
127 | boolean error = captureSignature(); |
---|
128 | if(!error){ |
---|
129 | mView.setDrawingCacheEnabled(true); |
---|
130 | mSignature.save(mView); |
---|
131 | |
---|
132 | |
---|
133 | Intent intent = new Intent(CaptureImgHandwrittenSignatureActivity.this, SelectSignUbicationHandwrittenSignatureActivity.class); |
---|
134 | intent.putExtra("STATUS", "done"); |
---|
135 | intent.putExtra("IMAGE_CAPTURED", mypath.getAbsolutePath()); |
---|
136 | intent.putExtra("FILE_TO_SIGN", fileToSign); |
---|
137 | startActivity(intent); |
---|
138 | Log.i("DEBUG", "saliendo"); |
---|
139 | // setResult(RESULT_OK,intent); |
---|
140 | // finish(); |
---|
141 | |
---|
142 | } |
---|
143 | } |
---|
144 | }); |
---|
145 | |
---|
146 | mCancel.setOnClickListener(new OnClickListener() |
---|
147 | { |
---|
148 | public void onClick(View v) |
---|
149 | { |
---|
150 | finish(); |
---|
151 | } |
---|
152 | }); |
---|
153 | |
---|
154 | } |
---|
155 | |
---|
156 | @Override |
---|
157 | protected void onDestroy() { |
---|
158 | super.onDestroy(); |
---|
159 | } |
---|
160 | |
---|
161 | private boolean captureSignature() { |
---|
162 | |
---|
163 | boolean error = false; |
---|
164 | //String errorMessage = ""; |
---|
165 | |
---|
166 | /* |
---|
167 | // por ahora no nos interesa mostrar la caja de texto |
---|
168 | if(yourName.getText().toString().equalsIgnoreCase("")){ |
---|
169 | errorMessage = errorMessage + "Por favor introduzca su nombre\n"; |
---|
170 | error = true; |
---|
171 | } |
---|
172 | |
---|
173 | if(error){ |
---|
174 | Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); |
---|
175 | toast.setGravity(Gravity.TOP, 105, 50); |
---|
176 | toast.show(); |
---|
177 | } |
---|
178 | */ |
---|
179 | return error; |
---|
180 | } |
---|
181 | |
---|
182 | private String getTodaysDate() { |
---|
183 | |
---|
184 | final Calendar c = Calendar.getInstance(); |
---|
185 | int todaysDate = (c.get(Calendar.YEAR) * 10000) + |
---|
186 | ((c.get(Calendar.MONTH) + 1) * 100) + |
---|
187 | (c.get(Calendar.DAY_OF_MONTH)); |
---|
188 | return(String.valueOf(todaysDate)); |
---|
189 | |
---|
190 | } |
---|
191 | |
---|
192 | private String getCurrentTime() { |
---|
193 | |
---|
194 | final Calendar c = Calendar.getInstance(); |
---|
195 | int currentTime = (c.get(Calendar.HOUR_OF_DAY) * 10000) + |
---|
196 | (c.get(Calendar.MINUTE) * 100) + |
---|
197 | (c.get(Calendar.SECOND)); |
---|
198 | return(String.valueOf(currentTime)); |
---|
199 | |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * Prepara directorio |
---|
204 | * @return boolean |
---|
205 | */ |
---|
206 | private boolean prepareDirectory() |
---|
207 | { |
---|
208 | try |
---|
209 | { |
---|
210 | if (makedirs()) |
---|
211 | { |
---|
212 | return true; |
---|
213 | } else { |
---|
214 | return false; |
---|
215 | } |
---|
216 | } catch (Exception e) |
---|
217 | { |
---|
218 | e.printStackTrace(); |
---|
219 | Toast.makeText(this, "Could not initiate File System.. Is Sdcard mounted properly?", Toast.LENGTH_LONG).show(); |
---|
220 | return false; |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | * Crea directorio utilizando la variable tmpDir |
---|
226 | * @return boolean |
---|
227 | */ |
---|
228 | private boolean makedirs() |
---|
229 | { |
---|
230 | File tempdir = new File(tempDir); |
---|
231 | if (!tempdir.exists()) |
---|
232 | tempdir.mkdirs(); |
---|
233 | |
---|
234 | if (tempdir.isDirectory()) |
---|
235 | { |
---|
236 | File[] files = tempdir.listFiles(); |
---|
237 | for (File file : files) |
---|
238 | { |
---|
239 | if (!file.delete()) |
---|
240 | { |
---|
241 | System.out.println("Failed to delete " + file); |
---|
242 | } |
---|
243 | } |
---|
244 | } |
---|
245 | return (tempdir.isDirectory()); |
---|
246 | } |
---|
247 | |
---|
248 | public class signature extends View |
---|
249 | { |
---|
250 | private static final float STROKE_WIDTH = 5f; |
---|
251 | private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2; |
---|
252 | private Paint paint = new Paint(); |
---|
253 | private Path path = new Path(); |
---|
254 | |
---|
255 | private float lastTouchX; |
---|
256 | private float lastTouchY; |
---|
257 | private final RectF dirtyRect = new RectF(); |
---|
258 | |
---|
259 | public signature(Context context, AttributeSet attrs) |
---|
260 | { |
---|
261 | super(context, attrs); |
---|
262 | paint.setAntiAlias(true); |
---|
263 | paint.setColor(Color.BLACK); |
---|
264 | paint.setStyle(Paint.Style.STROKE); |
---|
265 | paint.setStrokeJoin(Paint.Join.ROUND); |
---|
266 | paint.setStrokeWidth(STROKE_WIDTH); |
---|
267 | } |
---|
268 | |
---|
269 | public void save(View v) |
---|
270 | { |
---|
271 | |
---|
272 | if(mBitmap == null) |
---|
273 | { |
---|
274 | mBitmap = Bitmap.createBitmap (mContent.getWidth(), mContent.getHeight(), Bitmap.Config.ARGB_8888); |
---|
275 | } |
---|
276 | canvas = new Canvas(mBitmap); |
---|
277 | try |
---|
278 | { |
---|
279 | //Log.v("*****yourName:", yourName.getText().toString()); |
---|
280 | |
---|
281 | FileOutputStream mFileOutStream = new FileOutputStream(mypath); |
---|
282 | |
---|
283 | v.draw(canvas); |
---|
284 | mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream); |
---|
285 | mFileOutStream.flush(); |
---|
286 | mFileOutStream.close(); |
---|
287 | // String url = Images.Media.insertImage(getContentResolver(), mBitmap, "title", null); |
---|
288 | |
---|
289 | // Toast.makeText(CaptureImgHandwrittenSignatureActivity.this, |
---|
290 | // getString(R.string.firma_guardada) + " " + mypath, Toast.LENGTH_SHORT).show(); |
---|
291 | |
---|
292 | //In case you want to delete the file |
---|
293 | //boolean deleted = mypath.delete(); |
---|
294 | //Log.v("log_tag","deleted: " + mypath.toString() + deleted); |
---|
295 | //If you want to convert the image to string use base64 converter |
---|
296 | |
---|
297 | } |
---|
298 | catch(Exception e) |
---|
299 | { |
---|
300 | Log.v("log_tag", e.toString()); |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | public void clear() |
---|
305 | { |
---|
306 | path.reset(); |
---|
307 | invalidate(); |
---|
308 | } |
---|
309 | |
---|
310 | @Override |
---|
311 | protected void onDraw(Canvas canvas) |
---|
312 | { |
---|
313 | canvas.drawPath(path, paint); |
---|
314 | // canvas.drawCircle(lastTouchX, lastTouchX, HALF_STROKE_WIDTH, paint); |
---|
315 | } |
---|
316 | |
---|
317 | @Override |
---|
318 | public boolean onTouchEvent(MotionEvent event) |
---|
319 | { |
---|
320 | float eventX = event.getX(); |
---|
321 | float eventY = event.getY(); |
---|
322 | mGetSign.setEnabled(true); |
---|
323 | |
---|
324 | switch (event.getAction()) |
---|
325 | { |
---|
326 | case MotionEvent.ACTION_DOWN: |
---|
327 | path.moveTo(eventX, eventY); |
---|
328 | lastTouchX = eventX; |
---|
329 | lastTouchY = eventY; |
---|
330 | |
---|
331 | return true; |
---|
332 | |
---|
333 | case MotionEvent.ACTION_MOVE: |
---|
334 | |
---|
335 | case MotionEvent.ACTION_UP: |
---|
336 | |
---|
337 | resetDirtyRect(eventX, eventY); |
---|
338 | int historySize = event.getHistorySize(); |
---|
339 | for (int i = 0; i < historySize; i++) |
---|
340 | { |
---|
341 | float historicalX = event.getHistoricalX(i); |
---|
342 | float historicalY = event.getHistoricalY(i); |
---|
343 | expandDirtyRect(historicalX, historicalY); |
---|
344 | path.lineTo(historicalX, historicalY); |
---|
345 | } |
---|
346 | path.lineTo(eventX, eventY); |
---|
347 | break; |
---|
348 | |
---|
349 | default: |
---|
350 | Log.i("DRAW:","Ignored touch event: " + event.toString()); |
---|
351 | return false; |
---|
352 | } |
---|
353 | |
---|
354 | invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH), |
---|
355 | (int) (dirtyRect.top - HALF_STROKE_WIDTH), |
---|
356 | (int) (dirtyRect.right + HALF_STROKE_WIDTH), |
---|
357 | (int) (dirtyRect.bottom + HALF_STROKE_WIDTH)); |
---|
358 | |
---|
359 | lastTouchX = eventX; |
---|
360 | lastTouchY = eventY; |
---|
361 | |
---|
362 | return true; |
---|
363 | } |
---|
364 | |
---|
365 | private void debug(String string){ |
---|
366 | } |
---|
367 | |
---|
368 | private void expandDirtyRect(float historicalX, float historicalY) |
---|
369 | { |
---|
370 | if (historicalX < dirtyRect.left) |
---|
371 | { |
---|
372 | dirtyRect.left = historicalX; |
---|
373 | } |
---|
374 | else if (historicalX > dirtyRect.right) |
---|
375 | { |
---|
376 | dirtyRect.right = historicalX; |
---|
377 | } |
---|
378 | |
---|
379 | if (historicalY < dirtyRect.top) |
---|
380 | { |
---|
381 | dirtyRect.top = historicalY; |
---|
382 | } |
---|
383 | else if (historicalY > dirtyRect.bottom) |
---|
384 | { |
---|
385 | dirtyRect.bottom = historicalY; |
---|
386 | } |
---|
387 | } |
---|
388 | |
---|
389 | private void resetDirtyRect(float eventX, float eventY) |
---|
390 | { |
---|
391 | dirtyRect.left = Math.min(lastTouchX, eventX); |
---|
392 | dirtyRect.right = Math.max(lastTouchX, eventX); |
---|
393 | dirtyRect.top = Math.min(lastTouchY, eventY); |
---|
394 | dirtyRect.bottom = Math.max(lastTouchY, eventY); |
---|
395 | } |
---|
396 | |
---|
397 | } |
---|
398 | } |
---|