private void addTile(String tile) {
    // Add the new tile to the last available position before "Add / Delete" tile
    int newPosition = mDraggableGridView.getChildCount() - 1;
    if (newPosition < 0) newPosition = 0;

    mDraggableGridView.addView(buildQSTile(tile), newPosition);
    updateAddDeleteState();
    updateSettings();
  }
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    ContentResolver resolver = getActivity().getContentResolver();
    rebuildTiles();

    mDraggableGridView.setOnRearrangeListener(this);
    mDraggableGridView.setOnItemClickListener(this);
    mDraggableGridView.setUseLargeFirstRow(
        Settings.Secure.getInt(resolver, Settings.Secure.QS_USE_MAIN_TILES, 1) == 1);
  }
  private void updateSettings() {
    ContentResolver resolver = getActivity().getContentResolver();
    StringBuilder tiles = new StringBuilder();

    // Add every tile except the last one (Add / Delete) to the list
    for (int i = 0; i < mDraggableGridView.getChildCount(); i++) {
      String type = (String) mDraggableGridView.getChildAt(i).getTag();
      if (!TextUtils.isEmpty(type)) {
        if (tiles.length() > 0) {
          tiles.append(",");
        }
        tiles.append(type);
      }
    }

    Settings.Secure.putString(resolver, Settings.Secure.QS_TILES, tiles.toString());
  }
 @Override
 public boolean onStartDrag(int position) {
   // add/delete tile shouldn't be dragged
   if (mDraggableGridView.getChildAt(position) == mAddDeleteTile) {
     return false;
   }
   mDraggingActive = true;
   updateAddDeleteState();
   return true;
 }
  private void rebuildTiles() {
    mDraggableGridView.resetState();
    String order =
        Settings.Secure.getString(getActivity().getContentResolver(), Settings.Secure.QS_TILES);
    if (order == null) {
      order = resetTiles(getActivity());
    }

    if (!TextUtils.isEmpty(order)) {
      for (String tileType : order.split(",")) {
        View tile = buildQSTile(tileType);
        if (tile != null) {
          mDraggableGridView.addView(tile);
        }
      }
    }
    // Add a dummy tile for the "Add / Delete" tile
    mAddDeleteTile = buildQSTile(QSTileHolder.TILE_ADD_DELETE);
    mDraggableGridView.addView(mAddDeleteTile);
    updateAddDeleteState();
  }
  private void updateAddDeleteState() {
    int activeTiles = mDraggableGridView.getChildCount() - (mDraggingActive ? 2 : 1);
    boolean limitReached = activeTiles >= QSUtils.getAvailableTiles(getActivity()).size();
    int iconResId = mDraggingActive ? R.drawable.ic_menu_delete : R.drawable.ic_menu_add_dark;
    int titleResId =
        mDraggingActive
            ? R.string.qs_action_delete
            : limitReached ? R.string.qs_action_no_more_tiles : R.string.qs_action_add;

    TextView title = (TextView) mAddDeleteTile.findViewById(android.R.id.title);
    ImageView icon = (ImageView) mAddDeleteTile.findViewById(android.R.id.icon);

    title.setText(titleResId);
    title.setEnabled(!limitReached);

    icon.setImageResource(iconResId);
    icon.setEnabled(!limitReached);
  }
 @Override
 public boolean isDeleteTarget(int position) {
   return mDraggingActive && position == mDraggableGridView.getChildCount() - 1;
 }