package ru.vfilippov.electronreminder; import android.app.IntentService; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.util.Log; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; /** * This {@code IntentService} does the app's actual work. * {@code RecheckAlarmReceiver} (a {@code WakefulBroadcastReceiver}) holds a * partial wake lock for this service while the service does its work. When the * service is finished, it calls {@code completeWakefulIntent()} to release the * wake lock. */ public class ElectronReminderService extends IntentService { public ElectronReminderService() { super("ElectronReminderService"); } public static final String TAG = "Electron Reminder"; // An ID used to post the notification. public static final int NOTIFICATION_ID = 1; private NotificationManager mNotificationManager; NotificationCompat.Builder builder; @Override protected void onHandleIntent(Intent intent) { String urlString = "https://www.google.com"; String result = ""; // Try to connect to the Google homepage and download content. try { result = loadFromNetwork(urlString); } catch (IOException e) { Log.i(TAG, "Connection error"); } // If the app finds the string "doodle" in the Google home page content, it // indicates the presence of a doodle. Post a "Doodle Alert" notification. if (result.indexOf("doodle") != -1) { sendNotification(/*getString(R.string.doodle_found)*/"doodle found"); Log.i(TAG, "Found doodle!!"); } else { sendNotification("no doodle"); Log.i(TAG, "No doodle found. :-("); } // Release the wake lock provided by the BroadcastReceiver. RecheckAlarmReceiver.completeWakefulIntent(intent); } // Post a notification indicating suburban changes private void sendNotification(String msg) { mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("doodle alert") .setStyle(new NotificationCompat.BigTextStyle() .bigText(msg)) .setContentText(msg); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); } // Given a URL string, initiate a fetch operation. private String loadFromNetwork(String urlString) throws IOException { InputStream stream = null; String str = ""; try { stream = downloadUrl(urlString); str = readIt(stream); } finally { if (stream != null) stream.close(); } return str; } /** * Given a string representation of a URL, sets up a connection and gets * an input stream. * @param urlString A string representation of a URL. * @return An InputStream retrieved from a successful HttpURLConnection. * @throws IOException */ private InputStream downloadUrl(String urlString) throws IOException { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000 /* milliseconds */); conn.setConnectTimeout(15000 /* milliseconds */); conn.setRequestMethod("GET"); conn.setDoInput(true); // Start the query conn.connect(); InputStream stream = conn.getInputStream(); return stream; } /** * Reads an InputStream and converts it to a String. * @param stream InputStream containing HTML from www.google.com. * @return String version of InputStream. * @throws IOException */ private String readIt(InputStream stream) throws IOException { StringBuilder builder = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); for (String line = reader.readLine(); line != null; line = reader.readLine()) builder.append(line); reader.close(); return builder.toString(); } }