· 8 years ago · Jan 23, 2018, 01:00 PM
1public class Correo extends AppCompatActivity implements LocationListener{
2
3//Declaring EditText
4private EditText editTextSubject;
5private EditText editTextMessage;
6private EditText editTextNombre;
7private EditText editTextApellidos;
8private EditText editTextTelefono;
9/*public static final String Nombre = "NomKey";
10public static final String Apellidos = "ApKey";
11public static final String Asunto = "AsKey";
12public static final String Mensaje = "MenKey";
13public static final String Telefono = "TelKey";
14public static final String mypreference = "mypref";*/
15
16Button btnTackPic;
17ImageView ivThumbnailPhoto;
18Bitmap bitMap;
19static int TAKE_PICTURE = 1;
20private static final int PICK_IMAGE = 100;
21Uri imageUri;
22Button btngal;
23String rutaimg;
24LocationManager locationManager;
25Location location;
26
27//Send button
28private Button buttonSend;
29Button btnUbicacion;
30String ubicacion;
31double latitud = 0;
32double longitud = 0;
33EditText editubi;
34
35Bitmap bitimg;
36File imagefile;
37BodyPart adjunto = new MimeBodyPart();
38Multipart multipart = new MimeMultipart();
39
40
41@Override
42protected void onCreate(Bundle savedInstanceState) {
43 super.onCreate(savedInstanceState);
44 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
45 setContentView(R.layout.correo);
46
47
48 //Initializing the views
49 editTextSubject = (EditText) findViewById(R.id.editTextSubject);
50 editTextMessage = (EditText) findViewById(R.id.editTextMessage);
51 editTextNombre = (EditText) findViewById(R.id.editTextNombre);
52 editTextApellidos = (EditText) findViewById(R.id.editTextApellidos);
53 editTextTelefono = (EditText) findViewById(R.id.editTextTelefono);
54 editubi = (EditText) findViewById(R.id.editubi);
55
56 buttonSend = (Button) findViewById(R.id.buttonSend);
57 btnUbicacion = (Button) findViewById(R.id.btnUbicacion);
58
59 //Adding click listener
60 buttonSend.setOnClickListener(new View.OnClickListener() {
61 @Override
62 public void onClick(View v) {
63 sendEmail();
64 }
65 });
66 if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
67
68 ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
69
70 }
71 btnUbicacion.setOnClickListener(new View.OnClickListener() {
72 @Override
73 public void onClick(View view) {
74 getLocation();
75 }
76
77 });;
78
79
80
81
82 btnTackPic = (Button) findViewById(R.id.btnTakePic);
83 ivThumbnailPhoto = (ImageView) findViewById(R.id.ivThumbnailPhoto);
84
85 // add onclick listener to the button
86 btnTackPic.setOnClickListener(new View.OnClickListener() {
87 @Override
88 public void onClick(View v) {
89 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
90
91 // start camera activity
92 startActivityForResult(intent, TAKE_PICTURE);
93 }
94 });
95
96 btngal = (Button) findViewById(R.id.btngal) ;
97
98 btngal.setOnClickListener(new View.OnClickListener() {
99 @Override
100 public void onClick(View v) {
101 openGallery();
102 }
103 });
104
105}
106@Override
107protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
108
109 if (requestCode == TAKE_PICTURE && resultCode== RESULT_OK && intent!=null){
110 // get bundle
111 bitMap = (Bitmap) intent.getExtras().get("data");
112 ivThumbnailPhoto.setImageBitmap(bitMap);
113
114 }
115
116 if(resultCode == RESULT_OK && requestCode == PICK_IMAGE){
117 imageUri = intent.getData();
118 imagefile = new File(getRealPathFromURI(imageUri));
119
120 ivThumbnailPhoto.setImageURI(imageUri);
121
122 try {
123 bitimg = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
124
125 } catch (IOException e) {
126 e.printStackTrace();
127 }
128 }
129}
130
131
132private void openGallery(){
133 Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
134 startActivityForResult(gallery, PICK_IMAGE);
135}
136
137
138private void sendEmail() {
139 //Getting content for email
140 String email = "**********@gmail.com";
141 String subject = editTextSubject.getText().toString().trim();
142 String message = editTextMessage.getText().toString().trim();
143 String nombre = editTextNombre.getText().toString().trim();
144 String apellidos = editTextApellidos.getText().toString().trim();
145 String telefono = editTextTelefono.getText().toString().trim();
146 String ubiman = editubi.getText().toString().trim();
147
148 String msg = "Nombre: " + nombre + " " + apellidos + "n" + "n" + "Teléfono: " + telefono + "n" + "n" + "Asunto: " + subject + "n" + "n" + "Incidencia: " + message + "n" + "n" + "Ubicación manual : " + ubiman + "n" + "n" + "Ubicación GPS: " + ubicacion + "n" + "n" ;
149
150
151 /*if (subject.equals("") || message.equals("") || nombre.equals("") || apellidos.equals("") || telefono.equals("")) {
152 Toast.makeText(this, "Por favor, rellene todos los campos marcados con asterisco", Toast.LENGTH_SHORT).show();
153 }
154 else{*/
155 //Creating SendMail object
156 SendMail sm = new SendMail(this, email, subject, msg, imagefile);
157
158 //Executing sendmail to send email
159
160 sm.execute();
161 editTextMessage.setText("");
162 editTextSubject.setText("");
163 editTextTelefono.setText("");
164 editTextApellidos.setText("");
165 editTextNombre.setText("");
166 //}
167
168}
169void getLocation() {
170 try {
171 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
172 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 5, this);
173 }
174 catch(SecurityException e) {
175 e.printStackTrace();
176 }
177}
178@Override
179public void onLocationChanged(Location location) {
180 latitud = location.getLatitude();
181 longitud = location.getLongitude();
182 ubicacion = "https://maps.google.com?q=" + latitud + "," + longitud;
183 if (latitud != 0 || longitud != 0) {
184 try {
185 Geocoder geocoder = new Geocoder(this, Locale.getDefault());
186 List<Address> list = geocoder.getFromLocation(
187 latitud, longitud, 1);
188 if (!list.isEmpty()) {
189 Address DirCalle = list.get(0);
190 editubi.setText(DirCalle.getAddressLine(0));
191 }
192
193 } catch (IOException e) {
194 e.printStackTrace();
195 }
196 }
197}
198@Override
199public void onProviderDisabled(String provider) {
200 Toast.makeText(Correo.this, "Active el GPS y el Internet", Toast.LENGTH_SHORT).show();
201}
202@Override
203public void onStatusChanged(String provider, int status, Bundle extras) {
204
205}
206@Override
207public void onProviderEnabled(String provider) {
208
209}
210
211private String getRealPathFromURI(Uri contentURI) {
212 Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
213 if (cursor == null) { // Source is Dropbox or other similar local file path
214 return contentURI.getPath();
215 } else {
216 cursor.moveToFirst();
217 int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
218 return cursor.getString(idx);
219 }
220}
221
222public boolean onOptionsItemSelected(MenuItem item) {
223 switch (item.getItemId()) {
224 case android.R.id.home: //hago un case por si en un futuro agrego mas opciones
225 Log.i("ActionBar", "Atrás!");
226 finish();
227 return true;
228 default:
229 return super.onOptionsItemSelected(item);
230 }
231}
232
233}
234
235public class SendMail extends AsyncTask<Void,Void,Void> {
236
237//Declaring Variables
238private Context context;
239private Session session;
240
241//Information to send email
242private String email;
243private String subject;
244private String message;
245//private MimeMultipart Adjunto = new MimeMultipart();
246private File imagefile;
247
248//private BodyPart adjunto = new MimeBodyPart();
249//private BodyPart texto = new MimeBodyPart();
250
251
252
253//Progressdialog to show while sending email
254private ProgressDialog progressDialog;
255private Intent intent;
256
257//Class Constructor
258public SendMail(Context context, String email, String subject, String message, File imagefile){
259 //Initializing variables
260 this.context = context;
261 this.email = email;
262 this.subject = subject;
263 this.message = message;
264 this.imagefile = imagefile;
265}
266
267@Override
268protected void onPreExecute() {
269 super.onPreExecute();
270 //Showing progress dialog while sending email
271 progressDialog = ProgressDialog.show(context,"Enviando incidencia","Espere...",false,false);
272}
273
274@Override
275protected void onPostExecute(Void aVoid) {
276 super.onPostExecute(aVoid);
277 //Dismissing the progress dialog
278 progressDialog.dismiss();
279 //Showing a success message
280 Toast.makeText(context,"Incidencia enviada",Toast.LENGTH_LONG).show();
281}
282
283@Override
284protected Void doInBackground(Void... params) {
285 //Creating properties
286 Properties props = new Properties();
287
288 //Configuring properties for gmail
289 //If you are not using gmail you may need to change the values
290 props.setProperty("mail.smtp.host", "smtp.gmail.com");
291 props.setProperty("mail.smtp.starttls.enable", "true");
292 props.setProperty("mail.smtp.port", "587");
293 props.setProperty("mail.smtp.user", "***********@gmail.com");
294 props.setProperty("mail.smtp.auth", "true");
295
296
297 //Creating a new session
298 session = Session.getDefaultInstance(props,
299 new javax.mail.Authenticator() {
300 //Authenticating the password
301 protected PasswordAuthentication getPasswordAuthentication() {
302 return new PasswordAuthentication(Config.EMAIL, Config.PASSWORD);
303 }
304 });
305
306
307 try {
308 //Creating MimeMessage object
309 MimeMessage mm = new MimeMessage(session);
310
311 MimeMultipart multipart = new MimeMultipart();
312
313 //Setting sender address
314 mm.setFrom(new InternetAddress(Config.EMAIL));
315 //Adding receiver
316 mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
317 //Adding subject
318 mm.setSubject(subject);
319
320 mm.setText(message);
321
322 MimeBodyPart at = new MimeBodyPart();
323 at.setDisposition(MimeBodyPart.ATTACHMENT);
324 at.attachFile(imagefile);
325 at.setFileName("incidencia.jpg");
326
327 //multipart.addBodyPart(messageBodyPart);
328 multipart.addBodyPart(at);
329
330 //Adding message
331 mm.setContent(multipart);
332
333 Transport t = session.getTransport("smtp");
334 t.connect(Config.EMAIL,Config.PASSWORD);
335 t.sendMessage(mm,mm.getAllRecipients());
336 t.close();
337 //Sending email
338 //Transport.send(mm);
339
340 } catch (MessagingException e) {
341 e.printStackTrace();
342 } catch (IOException e) {
343 e.printStackTrace();
344 }
345 return null;
346}
347
348
349}