Пример #1
0
  /** @brief Method to handle creation of notification */
  private Notification createNotification(Recipe triggered) {
    try {
      // Get shared preferences
      SharedPreferences prefs =
          getApplicationContext().getSharedPreferences("RAOStore", Context.MODE_PRIVATE);
      SharedPreferences.Editor editor = prefs.edit();
      String jsonString = prefs.getString(NOTIFICATION_MAP_NAME, (new JSONObject()).toString());
      JSONObject jsonObject = new JSONObject(jsonString);

      String action = triggered.getDoList().get(0);
      Log.i(TAG, "Found matching recipe. Performing action: " + action);

      // Create null notification
      Notification not = null;
      System.out.println("Action is : " + action);
      // Check what this action was
      if (action.equals(Constants.DROP_PIN)) {
        // If drop pin, get location and create notification
        Log.e(TAG, "Creating notification");
        Location notifLoc = getLocation();

        not = new Notification(triggered.getName(), action, notifLoc);
      } else if (action.equals("Silence Phone")) {
        AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        audio.setRingerMode(0);

        not = new Notification(triggered.getName(), action);
      }

      if (not != null) {
        // Add notification to shared preferences
        jsonObject.put(not.getName(), not.toString());
        editor.putString(NOTIFICATION_MAP_NAME, jsonObject.toString());
        editor.commit();
      }
      return not;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }