コード例 #1
0
        private async Task UpdateWeather(DataMap dataMap)
        {
            if (dataMap != null && !dataMap.IsEmpty)
            {
                var update_time = dataMap.GetLong("update_time", 0);
                if (update_time != 0)
                {
                    if (Settings.HomeData is WeatherData.LocationData location)
                    {
                        var upDateTime = new DateTime(update_time, DateTimeKind.Utc);

                        /*
                         *  DateTime < 0 - This instance is earlier than value.
                         *  DateTime == 0 - This instance is the same as value.
                         *  DateTime > 0 - This instance is later than value.
                         */
                        if (Settings.UpdateTime.CompareTo(upDateTime) >= 0)
                        {
                            // Send callback to receiver
                            LocalBroadcastManager.GetInstance(this).SendBroadcast(
                                new Intent(WearableHelper.WeatherPath));
                            return;
                        }
                    }
                }

                var weatherJSON = dataMap.GetString("weatherData", String.Empty);
                if (!String.IsNullOrWhiteSpace(weatherJSON))
                {
                    using (var weatherTextReader = new Newtonsoft.Json.JsonTextReader(
                               new System.IO.StringReader(weatherJSON)))
                    {
                        var weatherData = WeatherData.Weather.FromJson(weatherTextReader);
                        var alerts      = dataMap.GetStringArrayList("weatherAlerts");

                        if (weatherData != null && weatherData.IsValid())
                        {
                            if (alerts.Count > 0)
                            {
                                weatherData.weather_alerts = new List <WeatherData.WeatherAlert>();
                                foreach (String alertJSON in alerts)
                                {
                                    using (var alertTextReader = new Newtonsoft.Json.JsonTextReader(
                                               new System.IO.StringReader(alertJSON)))
                                    {
                                        var alert = WeatherData.WeatherAlert.FromJson(alertTextReader);

                                        if (alert != null)
                                        {
                                            weatherData.weather_alerts.Add(alert);
                                        }
                                    }
                                }
                            }

                            await Settings.SaveWeatherAlerts(Settings.HomeData, weatherData.weather_alerts);

                            await Settings.SaveWeatherData(weatherData);

                            Settings.UpdateTime = weatherData.update_time.UtcDateTime;

                            // Send callback to receiver
                            LocalBroadcastManager.GetInstance(this).SendBroadcast(
                                new Intent(WearableHelper.WeatherPath));

                            // Update complications
                            WeatherComplicationIntentService.EnqueueWork(this,
                                                                         new Intent(this, typeof(WeatherComplicationIntentService))
                                                                         .SetAction(WeatherComplicationIntentService.ACTION_UPDATECOMPLICATIONS)
                                                                         .PutExtra(WeatherComplicationIntentService.EXTRA_FORCEUPDATE, true));
                        }
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Puts a local notification to show calendar card
        /// </summary>
        /// <param name="dataItem">Data item.</param>
        private void UpdateNotificationForDataItem(IDataItem dataItem)
        {
            if (Log.IsLoggable(Constants.TAG, LogPriority.Verbose))
            {
                Log.Verbose(Constants.TAG, "Updating notification for IDataItem");
            }

            DataMapItem mapDataItem = DataMapItem.FromDataItem(dataItem);
            DataMap     data        = mapDataItem.DataMap;

            String description = data.GetString(Constants.DESCRIPTION);

            if (TextUtils.IsEmpty(description))
            {
                description = "";
            }
            else
            {
                // Add a space between the description and the time of the event
                description += " ";
            }

            String contentText;

            if (data.GetBoolean(Constants.ALL_DAY))
            {
                contentText = GetString(Resource.String.desc_all_day, description);
            }
            else
            {
                String startTime = DateFormat.GetTimeFormat(this).Format(new Date(data.GetLong(Constants.BEGIN)));
                String endTime   = DateFormat.GetTimeFormat(this).Format(new Date(data.GetLong(Constants.END)));
                contentText = GetString(Resource.String.desc_time_period, description, startTime, endTime);
            }

            Intent deleteOperation = new Intent(this, typeof(DeleteService));
            // Use a unique identifier for the delete action.

            String deleteAction = "action_delete" + dataItem.Uri.ToString() + sNotificationId;

            deleteOperation.SetAction(deleteAction);
            deleteOperation.SetData(dataItem.Uri);
            PendingIntent deleteIntent       = PendingIntent.GetService(this, 0, deleteOperation, PendingIntentFlags.OneShot);
            PendingIntent silentDeleteIntent = PendingIntent.GetService(this, 1, deleteOperation.PutExtra(Constants.EXTRA_SILENT, true), PendingIntentFlags.OneShot);

            Notification.Builder notificationBuilder = new Notification.Builder(this)
                                                       .SetContentTitle(data.GetString(Constants.TITLE))
                                                       .SetContentText(contentText)
                                                       .SetSmallIcon(Resource.Drawable.ic_launcher)
                                                       .AddAction(Resource.Drawable.ic_menu_delete, GetText(Resource.String.delete), deleteIntent)
                                                       .SetDeleteIntent(silentDeleteIntent)
                                                       .SetLocalOnly(true)
                                                       .SetPriority((int)NotificationPriority.Min);

            // Set the event owner's profile picture as the notification background
            Asset asset = data.GetAsset(Constants.PROFILE_PIC);

            if (asset != null)
            {
                if (mGoogleApiClient.IsConnected)
                {
                    IDataApiGetFdForAssetResult assetFdResult = WearableClass.DataApi.GetFdForAsset(mGoogleApiClient, asset).Await().JavaCast <IDataApiGetFdForAssetResult>();
                    if (assetFdResult.Status.IsSuccess)
                    {
                        Bitmap profilePic = BitmapFactory.DecodeStream(assetFdResult.InputStream);
                        notificationBuilder.Extend(new Notification.WearableExtender().SetBackground(profilePic));
                    }
                    else if (Log.IsLoggable(Constants.TAG, LogPriority.Debug))
                    {
                        Log.Debug(Constants.TAG, "asset fetch failed with StatusCode: " + assetFdResult.Status.StatusCode);
                    }
                }
                else
                {
                    Log.Error(Constants.TAG, "Failed to set notification background - Client disconnected from Google Play Services");
                }

                Notification card = notificationBuilder.Build();

                (GetSystemService(Context.NotificationService).JavaCast <NotificationManager>()).Notify(sNotificationId, card);

                sNotificationIdByDataItemUri.Add(dataItem.Uri, sNotificationId++);
            }
        }