@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_snooze);
    setTitle(R.string.later_title);

    mGcmMessage = getIntent().getParcelableExtra(GCM_MESSAGE_KEY);

    ListView list = (ListView) findViewById(R.id.snooze_list);
    list.setAdapter(new SnoozeAdapter(this));
    list.setOnItemClickListener(this);

    PlaceTableHandler handler = new PlaceTableHandler(this);
    mPlaces = handler.getPlaces();
    handler.close();
  }
  @Override
  public void onProcessResult(int requestCode, ResultSet result) {
    List<Place> primaryPlaces = ((ParserModels.PlacesResultSet) result).results;
    Log.d(TAG, "Primary places: " + primaryPlaces.toString());

    // Two lists are used to sort the list
    List<UserPlace> places = new ArrayList<>();
    List<UserPlace> userPlaces = new ArrayList<>();
    PlaceTableHandler handler = new PlaceTableHandler(this);
    List<UserPlace> currentPlaces = handler.getPlaces();
    handler.close();

    // The user places are added to the list in the appropriate order
    for (UserPlace userPlace : currentPlaces) {
      userPlace.getPlace().setSet(true);

      int primaryIndex = -1;
      for (int i = 0; i < primaryPlaces.size(); i++) {
        if (userPlace.is(primaryPlaces.get(i))) {
          primaryIndex = i;
          break;
        }
      }

      // If the place is primary it is added at the head, otherwise it is added at the tail
      if (primaryIndex != -1) {
        userPlace.getPlace().setPrimary(true);
        places.add(userPlace);
        // The primary place is removed from the list to keep track of which ones have
        //  been added already
        primaryPlaces.remove(primaryIndex);
      } else {
        userPlace.getPlace().setPrimary(false);
        userPlaces.add(userPlace);
      }
    }
    // The reminder of primary places need to be added to the list as well
    for (Place place : primaryPlaces) {
      place.setSet(false);
      places.add(new UserPlace(place, -1, 0, 0));
    }
    // The list of user places are added to the final list
    places.addAll(userPlaces);

    // Finally, set the adapter
    mAdapter = new PlacesAdapter(this, places);
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PLACES_REQUEST_CODE) {
      // Reload the list of places
      PlaceTableHandler handler = new PlaceTableHandler(this);
      mPlaces = handler.getPlaces();
      handler.close();

      // Create the dialog
      displayPlacesDialog();
    } else if (requestCode == SETTINGS_REQUEST_CODE) {
      if (CompassUtil.hasPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
        snooze(mPickedPlace);
      } else {
        displayPlacesDialog();
      }
    }
  }
  @Override
  public void onClick(View view) {
    String name = mName.getText().toString().trim();

    // If the user didn't input a name, don't let him continue
    if (name.equals("")) {
      Toast.makeText(this, R.string.places_name_dialog_empty, Toast.LENGTH_SHORT).show();
    } else {
      // Check if the name is already set
      boolean duplicate = false;
      for (UserPlace userPlace : mAdapter.getPlaces()) {
        if (userPlace.getName().equals(name)) {
          duplicate = true;
          break;
        }
      }
      // If the name exists, don't let the user continue
      if (duplicate) {
        Toast.makeText(this, R.string.places_name_dialog_chosen, Toast.LENGTH_SHORT).show();
      } else {
        // If everything checks out, dismiss the dialog
        mNameDialog.dismiss();
        // If we are editing the name, then save it
        if (mEdition) {
          mCurrentPlace.getPlace().setName(mName.getText().toString().trim());
          mAdapter.notifyDataSetChanged();
          HttpRequest.put(
              null, API.URL.postPutPlace(mCurrentPlace), API.BODY.postPutPlace(mCurrentPlace));

          // Update the place in the database
          PlaceTableHandler handler = new PlaceTableHandler(this);
          handler.updatePlace(mCurrentPlace);
          handler.close();
        }
        // Otherwise this is a new place request, fire the place picker
        else {
          Intent add = new Intent(this, PlacePickerActivity.class);
          add.putExtra(PlacePickerActivity.PLACE_KEY, new UserPlace(name));
          startActivityForResult(add, PLACE_PICKER_REQUEST_CODE);
        }
      }
    }
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
      if (requestCode == PLACE_PICKER_REQUEST_CODE) {
        UserPlace place = data.getParcelableExtra(PlacePickerActivity.PLACE_RESULT_KEY);
        Log.d(TAG, place.toString());
        // If a place wasn't selected this is a new place, so save it.
        if (mCurrentPlace == null) {
          place.getPlace().setName(mName.getText().toString().trim());

          // Write the place to the database
          PlaceTableHandler handler = new PlaceTableHandler(this);
          handler.savePlace(place);
          handler.close();

          mAdapter.addPlace(place);
        }
        // Otherwise, this place was edited, so update both, the place and the adapter
        else {
          mCurrentPlace.setLatitude(place.getLatitude());
          mCurrentPlace.setLongitude(place.getLongitude());

          PlaceTableHandler handler = new PlaceTableHandler(this);
          // If the place is primary and not set it won't be in the database, so save
          if (mCurrentPlace.isPrimary() && !mCurrentPlace.isSet()) {
            handler.savePlace(mCurrentPlace);
          }
          // Otherwise it will, so update
          else {
            handler.updatePlace(mCurrentPlace);
          }
          handler.close();

          mCurrentPlace.getPlace().setSet(true);
          mAdapter.notifyDataSetChanged();
        }

        // Once a place is saved, start the location notification service to
        //  update its internal lists of places
        // LocationNotificationService.updateDataSet(this);
      }
    }
  }