Permalink
Browse files

Done S10.04-Notifications

  • Loading branch information...
GinaHsu committed Feb 14, 2018
1 parent ff2c8e5 commit d9987ca602609987b873376af80451d94ba1a89f
@@ -154,6 +154,36 @@ public static boolean isLocationLatLonAvailable(Context context) {
return spContainBothLatitudeAndLongitude;
}
// TODO (13.1) create a boolean function to check notificaitons enabled.
/**
* Returns true if the user prefers to see notifications from Sunshine, false otherwise. This
* preference can be changed by the user within the SettingsFragment.
*
* @param context Used to access SharedPreferences
* @return true if the user prefers to see notifications, false otherwise
*/
public static boolean areNotificationsEnabled(Context context) {
/* Key for accessing the preference for showing notifications */
String displayNotificationsKey = context.getString(R.string.pref_enable_notifications_key);
/*
* In Sunshine, the user has the ability to say whether she would like notifications
* enabled or not. If no preference has been chosen, we want to be able to determine
* whether or not to show them. To do this, we reference a bool stored in bools.xml.
*/
boolean shouldDisplayNotificationsByDefault = context
.getResources()
.getBoolean(R.bool.show_notifications_by_default);
/* As usual, we use the default SharedPreferences to access the user's preferences */
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
/* If a value is stored with the key, we extract it here. If not, use a default. */
boolean shouldDisplayNotifications = sp
.getBoolean(displayNotificationsKey, shouldDisplayNotificationsByDefault);
return shouldDisplayNotifications;
}
/**
* Returns the last time that a notification was shown (in UNIX time)
@@ -15,12 +15,16 @@
*/
package com.example.android.sunshine.sync;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.text.format.DateUtils;
import com.example.android.sunshine.data.SunshinePreferences;
import com.example.android.sunshine.data.WeatherContract;
import com.example.android.sunshine.utilities.NetworkUtils;
import com.example.android.sunshine.utilities.NotificationUtils;
import com.example.android.sunshine.utilities.OpenWeatherJsonUtils;
import java.net.URL;
@@ -73,12 +77,36 @@ synchronized public static void syncWeather(Context context) {
WeatherContract.WeatherEntry.CONTENT_URI,
weatherValues);
// TODO (13) Check if notifications are enabled
// TODO (13.2) Check if notifications are enabled
/*
* Finally, after we insert data into the ContentProvider, determine whether or not
* we should notify the user that the weather has been refreshed.
*/
boolean notificationsEnabled = SunshinePreferences.areNotificationsEnabled(context);
/*
* If the last notification was shown was more than 1 day ago, we want to send
* another notification to the user that the weather has been updated. Remember,
* it's important that you shouldn't spam your users with notifications.
*/
long timeSinceLastNotification = SunshinePreferences
.getEllapsedTimeSinceLastNotification(context);
boolean oneDayPassedSinceLastNotification = false;
// TODO (14) Check if a day has passed since the last notification
if(timeSinceLastNotification >= DateUtils.DAY_IN_MILLIS){
oneDayPassedSinceLastNotification = true;
}
// TODO (15) If more than a day have passed and notifications are enabled, notify the user
/*
* We only want to show the notification if the user wants them shown and we
* haven't shown a notification in the past day.
*/
if(notificationsEnabled && oneDayPassedSinceLastNotification){
NotificationUtils.notifyUserOfNewWeather(context);
}
/* If the code reaches this point, we have successfully performed our sync */
}
@@ -1,14 +1,22 @@
package com.example.android.sunshine.utilities;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.content.ContextCompat;
import com.example.android.sunshine.DetailActivity;
import com.example.android.sunshine.R;
import com.example.android.sunshine.data.SunshinePreferences;
import com.example.android.sunshine.data.WeatherContract;
public class NotificationUtils {
@@ -33,6 +41,7 @@
public static final int INDEX_MIN_TEMP = 2;
// TODO (1) Create a constant int value to identify the notification
private static final int WEATHER_NOTIFICATION_ID = 3004;
/**
* Constructs and displays a notification for the newly updated weather for today.
@@ -84,18 +93,52 @@ public static void notifyUserOfNewWeather(Context context) {
.getSmallArtResourceIdForWeatherCondition(weatherId);
// TODO (2) Use NotificationCompat.Builder to begin building the notification
/*
* NotificationCompat Builder is a very convenient way to build backward-compatible
* notifications. In order to use it, we provide a context and specify a color for the
* notification, a couple of different icons, the title for the notification, and
* finally the text of the notification, which in our case in a summary of today's
* forecast.
*/
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setSmallIcon(smallArtResourceId)
.setLargeIcon(largeIcon)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
.setAutoCancel(true);
// TODO (3) Create an Intent with the proper URI to start the DetailActivity
/*
* This Intent will be triggered when the user clicks the notification. In our case,
* we want to open Sunshine to the DetailActivity to display the newly updated weather.
*/
Intent detailIntentForToday = new Intent(context, DetailActivity.class);
detailIntentForToday.setData(todaysWeatherUri);
// TODO (4) Use TaskStackBuilder to create the proper PendingIntent
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
taskStackBuilder.addNextIntentWithParentStack(detailIntentForToday);
PendingIntent resultPendingIntent = taskStackBuilder
.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
// TODO (5) Set the content Intent of the NotificationBuilder
notificationBuilder.setContentIntent(resultPendingIntent);
// TODO (6) Get a reference to the NotificationManager
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
// TODO (7) Notify the user with the ID WEATHER_NOTIFICATION_ID
/* WEATHER_NOTIFICATION_ID allows you to update or cancel the notification later on */
notificationManager.notify(WEATHER_NOTIFICATION_ID, notificationBuilder.build());
// TODO (8) Save the time at which the notification occurred using SunshinePreferences
/*
* Since we just showed a notification, save the current time. That way, we can check
* next time the weather is refreshed if we should show another notification.
*/
SunshinePreferences.saveLastNotificationTime(context, System.currentTimeMillis());
}
/* Always close your cursor when you're done with it to avoid wasting resources. */
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- TODO (9) Add a new resource file for boolean values in the res directory and call it bools.xml -->
<!-- TODO (10) Add a boolean value for showing notifications by default to res/bools.xml -->
<resources>
<bool name="show_notifications_by_default">true</bool>
</resources>
@@ -32,10 +32,14 @@
android:key="@string/pref_units_key"
android:title="@string/pref_units_label" />
<!-- TODO (9) Add a new resource file for boolean values in the res directory and call it bools.xml -->
<!-- TODO (10) Add a boolean value for showing notifications by default to res/bools.xml -->
<!-- TODO (11) Add a checkbox preference for enabling and disabling notifications -->
<!-- TODO (12) Set the default of whether or not to show preferences using bools.xml -->
<CheckBoxPreference
android:defaultValue="@bool/show_notifications_by_default"
android:key="@string/pref_enable_notifications_key"
android:summaryOff="@string/pref_enable_notifications_false"
android:summaryOn="@string/pref_enable_notifications_true"
android:title="@string/pref_enable_notifications_label" />
</PreferenceScreen>
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<resources></resources>

0 comments on commit d9987ca

Please sign in to comment.