/**
   * Callback method from {@link TaskListFragment.Callbacks} indicating that the item with the given
   * ID was selected.
   */
  @Override
  public void onItemSelected(Uri uri, boolean forceReload, int pagePosition) {
    // only accept selections from the current visible task fragment or the activity itself
    if (pagePosition == -1 || pagePosition == mCurrentPagePosition) {
      if (mTwoPane) {
        mShouldShowDetails = true;
        if (forceReload) {
          mSelectedTaskUri = null;
          mShouldSwitchToDetail = false;
          mTaskDetailFrag.loadUri(uri);
        } else {
          mTaskDetailFrag.loadUri(uri);
        }
      } else if (forceReload) {
        mSelectedTaskUri = uri;

        // In single-pane mode, simply start the detail activity
        // for the selected item ID.
        Intent detailIntent = new Intent(Intent.ACTION_VIEW);
        detailIntent.setData(uri);
        startActivity(detailIntent);
        mSwitchedToDetail = true;
        mShouldSwitchToDetail = false;
      }
    }
  }
  @Override
  public void onDelete(Uri taskUri) {
    // nothing to do here, the loader will take care of reloading the list and the list view will
    // take care of selecting the next element.

    // empty the detail fragment
    if (mTwoPane) {
      mTaskDetailFrag.loadUri(null);
    }
  }
  @SuppressLint("NewApi")
  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate called again");
    super.onCreate(savedInstanceState);

    // check for single pane activity change
    mTwoPane = getResources().getBoolean(R.bool.has_two_panes);

    resolveIntentAction(getIntent());

    if (mSelectedTaskUri != null) {
      if (mShouldShowDetails && mShouldSwitchToDetail) {
        Intent viewTaskIntent = new Intent(Intent.ACTION_VIEW);
        viewTaskIntent.setData(mSelectedTaskUri);
        startActivity(viewTaskIntent);
        mSwitchedToDetail = true;
        mShouldSwitchToDetail = false;
        mTransientState = true;
      }
    } else {
      mShouldShowDetails = false;
    }

    setContentView(R.layout.activity_task_list);
    mAppBarLayout = (AppBarLayout) findViewById(R.id.appbar);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mAuthority = TaskContract.taskAuthority(this);
    mSearchHistoryHelper = new SearchHistoryHelper(this);

    if (findViewById(R.id.task_detail_container) != null) {
      // In two-pane mode, list items should be given the
      // 'activated' state when touched.

      // get list fragment
      // mTaskListFrag = (TaskListFragment)
      // getSupportFragmentManager().findFragmentById(R.id.task_list);
      // mTaskListFrag.setListViewScrollbarPositionLeft(true);

      // mTaskListFrag.setActivateOnItemClick(true);

      /*
       * Create a detail fragment, but don't load any URL yet, we do that later when the fragment gets attached
       */
      mTaskDetailFrag = ViewTaskFragment.newInstance(mSelectedTaskUri);
      getSupportFragmentManager()
          .beginTransaction()
          .replace(R.id.task_detail_container, mTaskDetailFrag, DETAIL_FRAGMENT_TAG)
          .commit();
    } else {
      FragmentManager fragmentManager = getSupportFragmentManager();
      Fragment detailFragment = fragmentManager.findFragmentByTag(DETAIL_FRAGMENT_TAG);
      if (detailFragment != null) {
        fragmentManager.beginTransaction().remove(detailFragment).commit();
      }
    }

    mGroupingFactories =
        new AbstractGroupingFactory[] {
          new ByList(mAuthority),
          new ByDueDate(mAuthority),
          new ByStartDate(mAuthority),
          new ByPriority(mAuthority),
          new ByProgress(mAuthority),
          new BySearch(mAuthority, mSearchHistoryHelper)
        };

    // set up pager adapter
    try {
      mPagerAdapter =
          new TaskGroupPagerAdapter(
              getSupportFragmentManager(), mGroupingFactories, this, R.xml.listview_tabs);
    } catch (XmlPullParserException e) {
      // TODO Automatisch generierter Erfassungsblock
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Automatisch generierter Erfassungsblock
      e.printStackTrace();
    } catch (XmlObjectPullParserException e) {
      // TODO Automatisch generierter Erfassungsblock
      e.printStackTrace();
    }

    // Setup ViewPager
    mPagerAdapter.setTwoPaneLayout(mTwoPane);
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mPagerAdapter);

    int currentPageIndex = mPagerAdapter.getPagePosition(mCurrentPageId);

    if (currentPageIndex >= 0) {
      mCurrentPagePosition = currentPageIndex;
      mViewPager.setCurrentItem(currentPageIndex);
      if (VERSION.SDK_INT >= 14 && mCurrentPageId == R.id.task_group_search) {
        if (mSearchItem != null) {
          // that's actually quite impossible to happen
          MenuItemCompat.expandActionView(mSearchItem);
        } else {
          mAutoExpandSearchView = true;
        }
      }
    }
    updateTitle(currentPageIndex);

    // Bind the tabs to the ViewPager
    mTabs = (TabLayout) findViewById(R.id.tabs);
    mTabs.setupWithViewPager(mViewPager);

    // set up the tab icons
    for (int i = 0, count = mPagerAdapter.getCount(); i < count; ++i) {
      mTabs.getTabAt(i).setIcon(mPagerAdapter.getTabIcon(i));
    }

    mViewPager.addOnPageChangeListener(
        new OnPageChangeListener() {

          @Override
          public void onPageSelected(int position) {
            mSelectedTaskUri = null;
            mCurrentPagePosition = position;

            int newPageId = mPagerAdapter.getPageId(mCurrentPagePosition);

            if (newPageId == R.id.task_group_search) {
              int oldPageId = mCurrentPageId;
              mCurrentPageId = newPageId;

              // store the page position we're coming from
              mPreviousPagePosition = mPagerAdapter.getPagePosition(oldPageId);
            } else if (mCurrentPageId == R.id.task_group_search) {
              // we've been on the search page before, so commit the search and close the search
              // view
              mSearchHistoryHelper.commitSearch();
              mHandler.post(mSearchUpdater);
              mCurrentPageId = newPageId;
              hideSearchActionView();
            }
            mCurrentPageId = newPageId;
            updateTitle(mCurrentPageId);
          }

          @Override
          public void onPageScrolled(
              int position, float positionOffset, int positionOffsetPixels) {}

          @Override
          public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_IDLE && mCurrentPageId == R.id.task_group_search) {
              // the search page is selected now, expand the search view
              mHandler.postDelayed(
                  new Runnable() {
                    @Override
                    public void run() {
                      MenuItemCompat.expandActionView(mSearchItem);
                    }
                  },
                  50);
            }
          }
        });

    mFloatingActionButton = (FloatingActionButton) findViewById(R.id.floating_action_button);
    if (mFloatingActionButton != null) {
      mFloatingActionButton.setOnClickListener(
          new OnClickListener() {

            @Override
            public void onClick(View v) {
              onAddNewTask();
            }
          });
    }
  }