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();
  }
  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());
  }
  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;
 }