/**
  * Inits adapter. Most of times is just an AssessmentAdapter. In a version with several adapters
  * in dashboard (like in 'mock' branch) a new one like the one in session is created.
  */
 private void initAdapter() {
   IDashboardAdapter adapterInSession = Session.getAdapterUnsent();
   if (adapterInSession == null) {
     adapterInSession = new AssessmentUnsentAdapter(this.surveys, getActivity());
   } else {
     adapterInSession = adapterInSession.newInstance(this.surveys, getActivity());
   }
   this.adapter = adapterInSession;
   Session.setAdapterUnsent(this.adapter);
 }
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult");
    // This captures the return code sent by Login Activity, to know whether or not the user got the
    // authorization
    if (requestCode == Constants.AUTHORIZE_PUSH) {
      if (resultCode == Activity.RESULT_OK) {

        // Tell the activity NOT to reload on next resume since the push itself will do it
        ((DashboardActivity) getActivity()).setReloadOnResume(false);

        // In case authorization was ok, we launch push action
        Bundle extras = data.getExtras();
        int position = extras.getInt("Survey", 0);
        String user = extras.getString("User");
        String password = extras.getString("Password");
        final Survey survey = (Survey) adapter.getItem(position - 1);
        AsyncPush asyncPush = new AsyncPush(survey, user, password);
        asyncPush.execute((Void) null);
      } else {
        // Otherwise we notify and continue
        new AlertDialog.Builder(getActivity())
            .setTitle("Authorization failed")
            .setMessage("User or password introduced are wrong. Push aborted.")
            .setPositiveButton(android.R.string.ok, null)
            .setNegativeButton(android.R.string.no, null)
            .create()
            .show();
      }
    }
  }
  /** Initializes the listview component, adding a listener for swiping right */
  private void initListView() {
    LayoutInflater inflater = LayoutInflater.from(getActivity());
    View header = inflater.inflate(this.adapter.getHeaderLayout(), null, false);
    View footer = inflater.inflate(this.adapter.getFooterLayout(), null, false);
    CustomTextView title = (CustomTextView) getActivity().findViewById(R.id.titleInProgress);
    title.setText(adapter.getTitle());
    ListView listView = getListView();
    listView.addHeaderView(header);
    listView.addFooterView(footer);
    setListAdapter((BaseAdapter) adapter);

    // Create a ListView-specific touch listener. ListViews are given special treatment because
    // by default they handle touches for their list items... i.e. they're in charge of drawing
    // the pressed state (the list selector), handling list item clicks, etc.
    SwipeDismissListViewTouchListener touchListener =
        new SwipeDismissListViewTouchListener(
            listView,
            new SwipeDismissListViewTouchListener.DismissCallbacks() {
              @Override
              public boolean canDismiss(int position) {
                return position > 0 && position <= surveys.size();
              }

              @Override
              public void onDismiss(ListView listView, int[] reverseSortedPositions) {
                for (final int position : reverseSortedPositions) {
                  new AlertDialog.Builder(getActivity())
                      .setTitle(getActivity().getString(R.string.dialog_title_delete_survey))
                      .setMessage(getActivity().getString(R.string.dialog_info_delete_survey))
                      .setPositiveButton(
                          android.R.string.yes,
                          new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0, int arg1) {
                              ((Survey) adapter.getItem(position - 1)).delete();
                              // Reload data using service
                              Intent surveysIntent = new Intent(getActivity(), SurveyService.class);
                              surveysIntent.putExtra(
                                  SurveyService.SERVICE_METHOD,
                                  SurveyService.RELOAD_DASHBOARD_ACTION);
                              getActivity().startService(surveysIntent);
                            }
                          })
                      .setNegativeButton(android.R.string.no, null)
                      .create()
                      .show();
                }
              }
            });
    listView.setOnTouchListener(touchListener);
    // Setting this scroll listener is required to ensure that during ListView scrolling,
    // we don't look for swipes.
    listView.setOnScrollListener(touchListener.makeScrollListener());

    listView.setLongClickable(true);

    listView.setOnItemLongClickListener(
        new AdapterView.OnItemLongClickListener() {
          @Override
          public boolean onItemLongClick(
              AdapterView<?> parent, View view, final int position, long id) {

            new AlertDialog.Builder(getActivity())
                .setTitle(getActivity().getString(R.string.dialog_title_send_preview))
                .setMessage(getActivity().getString(R.string.dialog_content_send_preview))
                .setPositiveButton(
                    getActivity().getString(R.string.send),
                    new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface arg0, int arg1) {
                        new AlertDialog.Builder(getActivity())
                            .setTitle(getActivity().getString(R.string.dialog_title_push_data))
                            .setMessage(getActivity().getString(R.string.dialog_content_push_data))
                            .setPositiveButton(
                                android.R.string.yes,
                                new DialogInterface.OnClickListener() {
                                  public void onClick(DialogInterface arg0, int arg1) {
                                    // We launch the login system, to authorize the push
                                    Intent authorizePush =
                                        new Intent(getActivity(), LoginActivity.class);
                                    authorizePush.putExtra("Action", Constants.AUTHORIZE_PUSH);
                                    authorizePush.putExtra("Survey", position);
                                    startActivityForResult(authorizePush, Constants.AUTHORIZE_PUSH);
                                  }
                                })
                            .setNegativeButton(android.R.string.no, null)
                            .create()
                            .show();
                      }
                    })
                .setNegativeButton(android.R.string.cancel, null)
                .setNeutralButton(
                    getActivity().getString(R.string.dialog_button_preview_feedback),
                    new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface arg0, int arg1) {
                        // Put selected survey in session
                        Session.setSurvey(surveys.get(position - 1));
                        // Go to FeedbackActivity
                        ((DashboardActivity) getActivity()).go(FeedbackActivity.class);
                        getActivity().finish();
                      }
                    })
                .create()
                .show();

            return true;
          }
        });

    Session.listViewUnsent = listView;
  }