@Override
 public boolean onContextItemSelected(MenuItem item) {
   final ContextMenuInfo menuInfo = item.getMenuInfo();
   if (!(menuInfo instanceof AdapterContextMenuInfo)) return false;
   final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
   mSelectedAccount = mAdapter.getAccount(info.position);
   if (mSelectedAccount == null) return false;
   switch (item.getItemId()) {
     case MENU_SET_COLOR:
       {
         final Intent intent = new Intent(getActivity(), ColorPickerDialogActivity.class);
         intent.putExtra(EXTRA_COLOR, mSelectedAccount.color);
         intent.putExtra(EXTRA_ALPHA_SLIDER, false);
         startActivityForResult(intent, REQUEST_SET_COLOR);
         break;
       }
     case MENU_DELETE:
       {
         final AccountDeletionDialogFragment f = new AccountDeletionDialogFragment();
         final Bundle args = new Bundle();
         args.putLong(EXTRA_ACCOUNT_ID, mSelectedAccount.account_id);
         f.setArguments(args);
         f.show(getChildFragmentManager(), FRAGMENT_TAG_ACCOUNT_DELETION);
         break;
       }
   }
   return false;
 }
 private void saveAccountPositions() {
   final ContentResolver cr = getContentResolver();
   final ArrayList<Integer> positions = mAdapter.getCursorPositions();
   final Cursor c = mAdapter.getCursor();
   if (positions != null && c != null && !c.isClosed()) {
     final int idIdx = c.getColumnIndex(Accounts._ID);
     for (int i = 0, j = positions.size(); i < j; i++) {
       c.moveToPosition(positions.get(i));
       final long id = c.getLong(idIdx);
       final ContentValues values = new ContentValues();
       values.put(Accounts.SORT_POSITION, i);
       final Expression where = Expression.equals(Accounts._ID, id);
       cr.update(Accounts.CONTENT_URI, values, where.getSQL(), null);
     }
   }
 }
 @Override
 public void drop(int from, int to) {
   mAdapter.drop(from, to);
   if (mListView.getChoiceMode() != AbsListView.CHOICE_MODE_NONE) {
     mListView.moveCheckState(from, to);
   }
   saveAccountPositions();
 }
 @Override
 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
   if (!(menuInfo instanceof AdapterContextMenuInfo)) return;
   final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
   final ParcelableAccount account = mAdapter.getAccount(info.position);
   menu.setHeaderTitle(account.name);
   final MenuInflater inflater = new MenuInflater(v.getContext());
   inflater.inflate(R.menu.action_manager_account, menu);
 }
 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
   super.onActivityCreated(savedInstanceState);
   setHasOptionsMenu(true);
   final FragmentActivity activity = getActivity();
   mPreferences = getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
   mPreferences.registerOnSharedPreferenceChangeListener(this);
   mAdapter = new AccountsAdapter(activity);
   Utils.configBaseAdapter(activity, mAdapter);
   mAdapter.setSortEnabled(true);
   mListView.setAdapter(mAdapter);
   mListView.setDragEnabled(true);
   mListView.setDropListener(this);
   mListView.setOnCreateContextMenuListener(this);
   mListView.setEmptyView(mEmptyView);
   mEmptyText.setText(R.string.no_account);
   mEmptyIcon.setImageResource(R.drawable.ic_info_error_generic);
   getLoaderManager().initLoader(0, null, this);
   setListShown(false);
 }
 private void updateDefaultAccount() {
   mAdapter.notifyDataSetChanged();
 }
 @Override
 public void onLoaderReset(Loader<Cursor> loader) {
   mAdapter.changeCursor(null);
 }
 @Override
 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
   setListShown(true);
   mAdapter.changeCursor(cursor);
 }