@Override
 public void onAccountsUpdated(Account[] accounts) {
   // TODO: watch for package upgrades to invalidate cache; see 7206643
   mAuthenticatorHelper.updateAuthDescriptions(this);
   mAuthenticatorHelper.onAccountsUpdated(this, accounts);
   invalidateHeaders();
 }
  private int insertAccountsHeaders(List<Header> target, int headerIndex) {
    String[] accountTypes = mAuthenticatorHelper.getEnabledAccountTypes();
    List<Header> accountHeaders = new ArrayList<Header>(accountTypes.length);
    for (String accountType : accountTypes) {
      CharSequence label = mAuthenticatorHelper.getLabelForType(this, accountType);
      if (label == null) {
        continue;
      }

      Account[] accounts = AccountManager.get(this).getAccountsByType(accountType);
      boolean skipToAccount =
          accounts.length == 1 && !mAuthenticatorHelper.hasAccountPreferences(accountType);
      Header accHeader = new Header();
      accHeader.title = label;
      if (accHeader.extras == null) {
        accHeader.extras = new Bundle();
      }
      if (skipToAccount) {
        accHeader.breadCrumbTitleRes = R.string.account_sync_settings_title;
        accHeader.breadCrumbShortTitleRes = R.string.account_sync_settings_title;
        accHeader.fragment = AccountSyncSettings.class.getName();
        accHeader.fragmentArguments = new Bundle();
        // Need this for the icon
        accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
        accHeader.extras.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
        accHeader.fragmentArguments.putParcelable(AccountSyncSettings.ACCOUNT_KEY, accounts[0]);
      } else {
        accHeader.breadCrumbTitle = label;
        accHeader.breadCrumbShortTitle = label;
        accHeader.fragment = ManageAccountsSettings.class.getName();
        accHeader.fragmentArguments = new Bundle();
        accHeader.extras.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
        accHeader.fragmentArguments.putString(ManageAccountsSettings.KEY_ACCOUNT_TYPE, accountType);
        if (!isMultiPane()) {
          accHeader.fragmentArguments.putString(
              ManageAccountsSettings.KEY_ACCOUNT_LABEL, label.toString());
        }
      }
      accountHeaders.add(accHeader);
    }

    // Sort by label
    Collections.sort(
        accountHeaders,
        new Comparator<Header>() {
          @Override
          public int compare(Header h1, Header h2) {
            return h1.title.toString().compareTo(h2.title.toString());
          }
        });

    for (Header header : accountHeaders) {
      target.add(headerIndex++, header);
    }
    if (!mListeningToAccountUpdates) {
      AccountManager.get(this).addOnAccountsUpdatedListener(this, null, true);
      mListeningToAccountUpdates = true;
    }
    return headerIndex;
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    if (getIntent().getBooleanExtra(EXTRA_CLEAR_UI_OPTIONS, false)) {
      getWindow().setUiOptions(0);
    }

    mAuthenticatorHelper = new AuthenticatorHelper();
    mAuthenticatorHelper.updateAuthDescriptions(this);
    mAuthenticatorHelper.onAccountsUpdated(this, null);

    mDevelopmentPreferences =
        getSharedPreferences(DevelopmentSettings.PREF_FILE, Context.MODE_PRIVATE);

    getMetaData();
    mInLocalHeaderSwitch = true;
    super.onCreate(savedInstanceState);
    mInLocalHeaderSwitch = false;

    if (!onIsHidingHeaders() && onIsMultiPane()) {
      highlightHeader(mTopLevelHeaderId);
      // Force the title so that it doesn't get overridden by a direct launch of
      // a specific settings screen.
      setTitle(R.string.settings_label);
    }

    // Retrieve any saved state
    if (savedInstanceState != null) {
      mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
      mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
    }

    // If the current header was saved, switch to it
    if (savedInstanceState != null && mCurrentHeader != null) {
      // switchToHeaderLocal(mCurrentHeader);
      showBreadCrumbs(mCurrentHeader.title, null);
    }

    if (mParentHeader != null) {
      setParentTitle(
          mParentHeader.title,
          null,
          new OnClickListener() {
            public void onClick(View v) {
              switchToParent(mParentHeader.fragment);
            }
          });
    }

    // Override up navigation for multi-pane, since we handle it in the fragment breadcrumbs
    if (onIsMultiPane()) {
      getActionBar().setDisplayHomeAsUpEnabled(false);
      getActionBar().setHomeButtonEnabled(false);
    }
  }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      HeaderViewHolder holder;
      Header header = getItem(position);
      int headerType = getHeaderType(header);
      View view = null;

      if (convertView == null || headerType == HEADER_TYPE_SWITCH) {
        holder = new HeaderViewHolder();
        switch (headerType) {
          case HEADER_TYPE_CATEGORY:
            view = new TextView(getContext(), null, android.R.attr.listSeparatorTextViewStyle);
            holder.title = (TextView) view;
            break;

          case HEADER_TYPE_SWITCH:
            view = mInflater.inflate(R.layout.preference_header_switch_item, parent, false);
            holder.icon = (ImageView) view.findViewById(R.id.icon);
            holder.title = (TextView) view.findViewById(com.android.internal.R.id.title);
            holder.summary = (TextView) view.findViewById(com.android.internal.R.id.summary);
            holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
            break;

          case HEADER_TYPE_NORMAL:
            view = mInflater.inflate(R.layout.preference_header_item, parent, false);
            holder.icon = (ImageView) view.findViewById(R.id.icon);
            holder.title = (TextView) view.findViewById(com.android.internal.R.id.title);
            holder.summary = (TextView) view.findViewById(com.android.internal.R.id.summary);
            break;
        }
        view.setTag(holder);
      } else {
        view = convertView;
        holder = (HeaderViewHolder) view.getTag();
      }

      // All view fields must be updated every time, because the view may be recycled
      switch (headerType) {
        case HEADER_TYPE_CATEGORY:
          holder.title.setText(header.getTitle(getContext().getResources()));
          break;

        case HEADER_TYPE_SWITCH:
          // Would need a different treatment if the main menu had more switches
          if (header.id == R.id.wifi_settings) {
            mWifiEnabler.setSwitch(holder.switch_);
          } else if (header.id == R.id.bluetooth_settings) {
            mBluetoothEnabler.setSwitch(holder.switch_);
          } else if (header.id == R.id.profiles_settings) {
            mProfileEnabler.setSwitch(holder.switch_);
          } else if (header.id == R.id.trds_settings) {
            mTRDSSwitch = (Switch) view.findViewById(R.id.switchWidget);
            mTRDSEnabler.setSwitch(holder.switch_);
          }
          // No break, fall through on purpose to update common fields

          // $FALL-THROUGH$
        case HEADER_TYPE_NORMAL:
          if (header.extras != null
              && header.extras.containsKey(ManageAccountsSettings.KEY_ACCOUNT_TYPE)) {
            String accType = header.extras.getString(ManageAccountsSettings.KEY_ACCOUNT_TYPE);
            ViewGroup.LayoutParams lp = holder.icon.getLayoutParams();
            lp.width = getContext().getResources().getDimensionPixelSize(R.dimen.header_icon_width);
            lp.height = lp.width;
            holder.icon.setLayoutParams(lp);
            Drawable icon = mAuthHelper.getDrawableForType(getContext(), accType);
            holder.icon.setImageDrawable(icon);
          } else {
            holder.icon.setImageResource(header.iconRes);
          }
          holder.title.setText(header.getTitle(getContext().getResources()));
          CharSequence summary = header.getSummary(getContext().getResources());
          if (!TextUtils.isEmpty(summary)) {
            holder.summary.setVisibility(View.VISIBLE);
            holder.summary.setText(summary);
          } else {
            holder.summary.setVisibility(View.GONE);
          }
          break;
      }

      return view;
    }