Example #1
0
 /** Returns the API client connection */
 public YOMPClient getClient() throws IOException, YOMPException {
   if (_YOMPCli == null) {
     _YOMPCli = _service.connectToServer();
   }
   if (_YOMPCli == null) {
     throw new IOException("Unable to connect to server");
   }
   return _YOMPCli;
 }
Example #2
0
  /** Download and fire new notifications from the server */
  protected void synchronizeNotifications() throws YOMPException, IOException {
    final Resources res = YOMPApplication.getContext().getResources();

    // Try to connect to YOMP
    if (_YOMPCli == null) {
      _YOMPCli = _service.connectToServer();
    }
    if (_YOMPCli == null) {
      throw new IOException("Unable to connect to server");
    }

    Metric metric;
    long localId;
    boolean newNotification = false;
    long notificationCount;
    ArrayList<String> acknowledge = new ArrayList<String>();
    final CoreDatabase YOMPdb = YOMPApplication.getDatabase();
    final SharedPreferences prefs =
        PreferenceManager.getDefaultSharedPreferences(_service.getApplicationContext());

    // Check if the notifications are enabled
    boolean enable = prefs.getBoolean(PREF_NOTIFICATIONS_ENABLE, true);
    boolean groupNotifications = res.getBoolean(R.bool.group_notifications);

    // Download pending notifications from the server
    List<Notification> pendingNotifications = _YOMPCli.getNotifications();
    if (pendingNotifications == null || pendingNotifications.isEmpty()) {
      return;
    }

    if (groupNotifications) {
      if (enable) {
        for (Notification notification : pendingNotifications) {
          // Add Notification to local database
          localId =
              YOMPdb.addNotification(
                  notification.getNotificationId(),
                  notification.getMetricId(),
                  notification.getTimestamp(),
                  notification.getDescription());
          if (localId != -1) {
            newNotification = true;
          }
        }
        NotificationUtils.createGroupedOsNotification(pendingNotifications);
      }
    } else {
      for (Notification notification : pendingNotifications) {
        // Update notification description;
        metric = YOMPdb.getMetric(notification.getMetricId());
        if (metric != null) {
          // Add Notification to local database
          localId =
              YOMPdb.addNotification(
                  notification.getNotificationId(),
                  notification.getMetricId(),
                  notification.getTimestamp(),
                  notification.getDescription());
          if (localId != -1) {
            // This is a new notification
            newNotification = true;

            // Fire OS notification
            if (enable) {
              notificationCount = YOMPdb.getUnreadNotificationCount();
              NotificationUtils.createOSNotification(
                  notification.getDescription(),
                  notification.getTimestamp(),
                  (int) localId,
                  notificationCount);
            }
            Log.i(
                TAG,
                "{TAG:ANDROID.NOTIFICATION.ADD} "
                    + notification.getTimestamp()
                    + " "
                    + notification.getMetricId()
                    + " - "
                    + notification.getDescription());
          }
        } else {
          Log.w(TAG, "Notification received for unknown metric: " + notification.getMetricId());
        }
        acknowledge.add(notification.getNotificationId());
      }

      // Acknowledge notifications
      _YOMPCli.acknowledgeNotifications(acknowledge.toArray(new String[acknowledge.size()]));
    }

    // Fire notification event
    if (newNotification) {
      fireNotificationChangedEvent(_service);
    }
  }