Advertisement
Guest User

Untitled

a guest
Jan 26th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.56 KB | None | 0 0
  1.  
  2.  
  3. import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
  4. import static mesutdokumaci.reader.Delete_Mail.delete;
  5.  
  6. /**
  7. * Created by mesutdokumaci on 8.01.16.
  8. */
  9. public class DATABASE_KAYIT_SERVICE extends Service {
  10. Timer timing;
  11. Handler helper;
  12. int id = 1;
  13. String server, kullanıcı, sifre;
  14. public static String deneme = null;
  15. public static String govde;
  16. public static String tarih;
  17. public static String konu;
  18. public static String okunmadı;
  19. public static String connect = null;
  20. public static String ses2;
  21. public static Integer a;
  22. private static final int NOT_ID = 1;
  23.  
  24. @Override
  25. public IBinder onBind(Intent intent) {
  26. return null;
  27. }
  28.  
  29. @Override
  30. public void onCreate() {
  31. // TODO Auto-generated method stub
  32. super.onCreate();
  33. DATABASE_SERVER db = new DATABASE_SERVER(getApplicationContext());
  34. final HashMap<String, String> map = db.kullanici_detay(id);
  35.  
  36. String time = map.get("zaman").toString();
  37. a = Integer.parseInt(time);
  38. System.out.println("ZAMAAANNN" + time);
  39. timing = new Timer();
  40. helper = new Handler(Looper.getMainLooper());
  41. timing.scheduleAtFixedRate(new TimerTask() {
  42.  
  43. @Override
  44. public void run() {
  45. callMethod();
  46. }
  47. private void callMethod() {
  48. helper.post(new Runnable() {
  49. public void run() {
  50.  
  51. DATABASE_SERVER asd = new DATABASE_SERVER(getApplicationContext());
  52. HashMap<String, String> map = asd.kullanici_detay(id);
  53.  
  54. server = (map.get("mail_server").toString());
  55. kullanıcı = (map.get("kullanici").toString());
  56. sifre = (map.get("sifre").toString());
  57.  
  58. System.out.println(server);
  59. System.out.println(kullanıcı);
  60. System.out.println(sifre);
  61. String host = server;
  62. String mailStoreType = "pop3";
  63. String username = kullanıcı;
  64. String password = sifre;
  65. String s = "açık";
  66.  
  67.  
  68. if (s.equals(map.get("kontrol").toString())) {
  69. Fetching_Mail(host, mailStoreType, username, password);
  70. delete(host, mailStoreType, username, password);
  71. } else {
  72. System.out.println("Otomatik Güncelleme Servisi Kapalıdır");
  73. }
  74. }
  75.  
  76. protected void Fetching_Mail(String pop3Host, String storeType, String user, String password) {
  77.  
  78. try {
  79. Properties properties = new Properties();
  80. properties.put("mail.store.protocol", "pop3");
  81. properties.put("mail.pop3.host", pop3Host);
  82. properties.put("mail.pop3.port", "110");
  83. properties.put("mail.pop3.starttls.enable", "false");
  84. Session emailSession = Session.getDefaultInstance(properties);
  85. Store store = emailSession.getStore("pop3");
  86. store.connect(pop3Host, user, password);
  87. connect = String.valueOf("a");
  88. Folder emailFolder = store.getFolder("INBOX");
  89. emailFolder.open(Folder.READ_ONLY);
  90. BufferedReader reader = new BufferedReader(new InputStreamReader(
  91. System.in));
  92. Message[] messages = emailFolder.getMessages();
  93. System.out.println("messages.length---" + messages.length);
  94. deneme = String.valueOf(messages.length);
  95.  
  96. for (int i = 0; i < messages.length; i++) {
  97. Message message = messages[i];
  98. System.out.println("---------------------------------");
  99. writePart(message);
  100. String line = reader.readLine();
  101. if ("YES".equals(line)) {
  102. message.writeTo(System.out);
  103. } else if ("QUIT".equals(line)) {
  104. break;
  105. }
  106.  
  107. DATABASE_MAIL db = new DATABASE_MAIL(getApplicationContext());
  108. db.mail_ekle(tarih, konu, govde, null);
  109. db.close();
  110.  
  111. }
  112. emailFolder.close(true);
  113. store.close();
  114. } catch (NoSuchProviderException e) {
  115. e.printStackTrace();
  116. } catch (MessagingException e) {
  117. e.printStackTrace();
  118. } catch (IOException e) {
  119. e.printStackTrace();
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. }
  123. }
  124.  
  125. public void writePart(Part p) throws Exception {
  126. if (p instanceof Message)
  127. writeEnvelope((Message) p);
  128. System.out.println("----------------------------");
  129. System.out.println("CONTENT-TYPE: " + p.getContentType());
  130.  
  131. if (p.isMimeType("text/plain")) {
  132. System.out.println("This is plain text");
  133. System.out.println("---------------------------");
  134. System.out.println((String) p.getContent());
  135.  
  136. govde = String.valueOf(p.getContent());
  137.  
  138. System.out.println("Mail Veri Tabanına Eklendi");
  139.  
  140. DATABASE_SERVER db = new DATABASE_SERVER(getApplicationContext());
  141. HashMap<String, String> map = db.kullanici_detay(id);
  142. String ses = map.get("ses");
  143. System.out.println("sesssss : " + ses);
  144. ses2 = "açık";
  145. System.out.println("Stringg sesssss : " + ses2);
  146.  
  147. if (ses.equals(ses2)) {
  148.  
  149. System.out.println("sesssss111 : " + ses);
  150. System.out.println("Stringg sesssss1111 : " + ses2);
  151. String ns = Context.NOTIFICATION_SERVICE;
  152.  
  153.  
  154. /*
  155. *BU ise 10 tane bildirim gelse bile üst üste güncelliyor 1 tane bildirim varmış gibi gösteriyor en son
  156. *geleni görüyoruz. tıkladığımızda en son gelenin ayrıntıları gösteriyor
  157.  
  158. */
  159. NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
  160. int icon = R.mipmap.ic_launcher;
  161. CharSequence tickerText = "Bildirim ";
  162. long when = System.currentTimeMillis();
  163.  
  164. int requestID = (int) System.currentTimeMillis();
  165. Notification notification = new Notification(icon, tickerText, when);
  166. Context context = getApplicationContext();
  167. Intent notificationIntent = new Intent(getApplicationContext(), Notifications.class);
  168. notificationIntent.putExtra("data1", tarih);
  169. notificationIntent.putExtra("data2", konu);
  170. notificationIntent.putExtra("data3", govde);
  171.  
  172. notificationIntent.setAction("Bildirim" + requestID);
  173. // PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), requestID,new Intent(getApplication(),
  174. // Notifications.class), PendingIntent.FLAG_ONE_SHOT);
  175. PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), requestID, notificationIntent, 0);
  176. notificationIntent.setData((Uri.parse("mystring" + requestID++)));
  177. notification.setLatestEventInfo(context, konu, govde, contentIntent);
  178. notification.flags += Notification.FLAG_ONGOING_EVENT;
  179. notification.flags += Notification.FLAG_AUTO_CANCEL;
  180. mNotificationManager.notify(NOT_ID, notification);
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187. //Bu benim yapmış olduğum bildirimler tek tek geliyor tıkladığında ana sayfa açılıyor
  188.  
  189. /* NotificationManager bildirimyoneticisi = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  190. Notification notification = new Notification(R.mipmap.ic_launcher, "BİLDİRİM", System.currentTimeMillis());
  191. Intent notificationIntent = new Intent(getApplicationContext(), mail_detay.class);
  192. // notificationIntent.putExtra("tarih",tarih);
  193. // notificationIntent.putExtra("konu",konu);
  194. notificationIntent.putExtra("id", mail_id);
  195. PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplication(),
  196. MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
  197. //) Intent intent = new Intent(getApplicationContext(), mail_detay.class);
  198. //intent.putExtra("id", mail_id);
  199. notification.setLatestEventInfo(getApplication(), konu, govde, pendingIntent);
  200. notification.flags = Notification.FLAG_AUTO_CANCEL;
  201. notification.defaults |= Notification.DEFAULT_VIBRATE;
  202. notification.defaults |= Notification.DEFAULT_SOUND;
  203. bildirimyoneticisi.notify((int) System.currentTimeMillis(), notification);
  204. */
  205.  
  206. Fetching_Mail.tarih = " ";
  207. } else {
  208. System.out.println("sesssss2222 : " + ses);
  209. System.out.println("Stringg sesssss2222 : " + ses2);
  210. NotificationManager bildirimyoneticisi = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  211. Notification notification = new Notification(R.mipmap.ic_launcher, "BİLDİRİM", System.currentTimeMillis());
  212. PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplication(),
  213. MainActivity.class), PendingIntent.FLAG_ONE_SHOT);
  214. notification.setLatestEventInfo(getApplication(), konu, govde, pendingIntent);
  215.  
  216. notification.flags = Notification.FLAG_AUTO_CANCEL;
  217. // notification.defaults |= Notification.DEFAULT_VIBRATE;
  218. // notification.defaults |= Notification.DEFAULT_SOUND;
  219. bildirimyoneticisi.notify((int) System.currentTimeMillis(), notification);
  220. System.out.println("Mail Silinmiştir");
  221. Fetching_Mail.tarih = " ";
  222.  
  223. }
  224. }
  225.  
  226. else if(p.isMimeType("multipart/*"))
  227.  
  228. {
  229. System.out.println("This is a Multipart");
  230. System.out.println("---------------------------");
  231. Multipart mp = (Multipart) p.getContent();
  232. int count = mp.getCount();
  233. for (int i = 0; i < count; i++)
  234. writePart(mp.getBodyPart(i));
  235. }
  236.  
  237. else if(p.isMimeType("message/rfc822"))
  238.  
  239. {
  240. System.out.println("This is a Nested Message");
  241. System.out.println("---------------------------");
  242. writePart((Part) p.getContent());
  243. }
  244.  
  245. else if(p.isMimeType("image/jpeg"))
  246.  
  247. {
  248. System.out.println("--------> image/jpeg");
  249. Object o = p.getContent();
  250. InputStream x = (InputStream) o;
  251. // Construct the required byte array
  252. System.out.println("x.length = " + x.available());
  253. int i = 0;
  254. byte[] bArray = new byte[x.available()];
  255.  
  256. while ((i = (int) ((InputStream) x).available()) > 0) {
  257. int result = (int) (((InputStream) x).read(bArray));
  258. if (result == -1)
  259. break;
  260. }
  261. FileOutputStream f2 = new FileOutputStream("/tmp/image.jpg");
  262. f2.write(bArray);
  263. }
  264.  
  265. else if(p.getContentType().
  266.  
  267. contains("image/")
  268.  
  269. )
  270.  
  271. {
  272. System.out.println("content type" + p.getContentType());
  273. File f = new File("image" + new Date(0).getTime() + ".jpg");
  274. DataOutputStream output = new DataOutputStream(
  275. new BufferedOutputStream(new FileOutputStream(f)));
  276. com.sun.mail.util.BASE64DecoderStream test =
  277. (com.sun.mail.util.BASE64DecoderStream) p
  278. .getContent();
  279. byte[] buffer = new byte[1024];
  280. int bytesRead;
  281. while ((bytesRead = test.read(buffer)) != -1) {
  282. output.write(buffer, 0, bytesRead);
  283. }
  284. }
  285.  
  286. else
  287.  
  288. {
  289. Object o = p.getContent();
  290. if (o instanceof String) {
  291. System.out.println("This is a string");
  292. System.out.println("---------------------------");
  293. System.out.println((String) o);
  294.  
  295. } else if (o instanceof InputStream) {
  296. System.out.println("This is just an input stream");
  297. System.out.println("---------------------------");
  298. InputStream is = (InputStream) o;
  299. is = (InputStream) o;
  300. int c;
  301. while ((c = is.read()) != -1)
  302. System.out.write(c);
  303. } else {
  304. System.out.println("This is an unknown type");
  305. System.out.println("---------------------------");
  306. System.out.println(o.toString());
  307. }
  308. }
  309.  
  310. }
  311.  
  312. protected void writeEnvelope(Message m) throws Exception {
  313. System.out.println("This is the message envelope");
  314. System.out.println("---------------------------");
  315. Address[] a;
  316.  
  317. if ((a = m.getFrom()) != null) {
  318. for (int j = 0; j < a.length; j++)
  319. System.out.println("FROM: " + a[j].toString());
  320. }
  321. if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
  322. for (int j = 0; j < a.length; j++)
  323. System.out.println("TO: " + a[j].toString());
  324. }
  325. if (m.getSubject() != null)
  326. System.out.println("SUBJECT: " + m.getSubject());
  327. konu = m.getSubject();
  328.  
  329.  
  330. if (m.getSentDate() != null)
  331. System.out.println("DATE: " + m.getSentDate());
  332. SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",
  333. Locale.ENGLISH);
  334. Date parsedDate = sdf.parse(String.valueOf(m.getSentDate()));
  335. SimpleDateFormat print = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
  336. System.out.println(print.format(parsedDate));
  337. tarih = String.valueOf(print.format(parsedDate));
  338. System.out.println("YENİİ TARİHH" + tarih);
  339. }
  340.  
  341. });
  342. }
  343.  
  344. }, 0, a * 1000);
  345. }
  346.  
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement