private CustomEngine getData() {
    CustomEngine data = new CustomEngine();
    data.setName(mEditTextName.getText().toString());
    data.setUpload_url(mEditTextUrl.getText().toString());
    data.setPost_file_key(mEditTextFileKey.getText().toString());
    data.setResult_open_action(mSpinner.getSelectedItemPosition());

    if (!URLUtil.isNetworkUrl(data.getUpload_url())) {
      data.setUpload_url("http://" + data.getUpload_url());
    }

    data.post_text_key.clear();
    data.post_text_value.clear();
    data.post_text_type.clear();

    for (int i = 0; i < mRecyclerView.getChildCount(); i++) {
      View view = mRecyclerView.getChildAt(i);
      if (view == null) continue;
      EditText key = (EditText) view.findViewById(R.id.editText_key);
      EditText value = (EditText) view.findViewById(R.id.editText_value);
      if (key != null && value != null) {
        data.post_text_key.add(key.getEditableText().toString());
        data.post_text_value.add(value.getEditableText().toString());
        data.post_text_type.add(0);
      }
    }
    return data;
  }
  private void add() {
    /*SQLiteDatabase db = mDbHelper.getWritableDatabase();
    CustomEngineParcelable parcelable = getParcelable();
    parcelable.data.id = CustomEngine.getAvailableId();
    mData.add(parcelable.data);

    ContentValues values = new ContentValues();
    values.put(CustomEngineTable.COLUMN_ID, parcelable.data.id);
    values.put(CustomEngineTable.COLUMN_DATA, ParcelableUtils.marshall(parcelable));

    db.insert(CustomEngineTable.TABLE_NAME, null, values);*/

    CustomEngineParcelable parcelable = new CustomEngineParcelable();
    parcelable.data = getData();
    parcelable.data.setId(CustomEngine.getAvailableId());
    parcelable.data.setEnabled(1);

    CustomEngine.addEngineToDb(this, parcelable, parcelable.data.getId());
    CustomEngine.addEngineToList(parcelable.data);

    EditSitesActivity.getAdapter(this).notifyItemInserted(mData.size() - 1);
    EditSitesActivity.getAdapter(this).notifyItemChanged(mData.size() - 2);
  }
  private void modify() {
    SQLiteDatabase db = mDbHelper.getReadableDatabase();

    CustomEngineParcelable parcelable = new CustomEngineParcelable();
    parcelable.data = getData();

    ContentValues values = new ContentValues();
    values.put(CustomEngineTable.COLUMN_DATA, ParcelableUtils.marshall(parcelable));

    String selection = CustomEngineTable.COLUMN_ID + " LIKE ?";
    String[] selectionArgs = {String.valueOf(mItem.getId())};

    db.update(CustomEngineTable.TABLE_NAME, values, selection, selectionArgs);

    mItem.setName(parcelable.data.getName());
    mItem.setUpload_url(parcelable.data.getUpload_url());
    mItem.setPost_file_key(parcelable.data.getPost_file_key());
    mItem.setResult_open_action(parcelable.data.getResult_open_action());
    mItem.post_text_key = parcelable.data.post_text_key;
    mItem.post_text_value = parcelable.data.post_text_value;
    mItem.post_text_type = parcelable.data.post_text_type;

    EditSitesActivity.getAdapter(this).notifyItemChanged(mLocation);
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_engine);

    mActivity = this;

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    /*if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material);
        upArrow.setColorFilter(ContextCompat.getColor(this, android.R.color.white), PorterDuff.Mode.SRC_ATOP);
        mToolbar.setNavigationIcon(upArrow);
    }*/

    mCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);

    mEditTextName = (EditText) findViewById(R.id.edit_name);
    mEditTextUrl = (EditText) findViewById(R.id.edit_url);
    mEditTextFileKey = (EditText) findViewById(R.id.edit_file_key);

    mFormTitle = (TextView) findViewById(R.id.post_form_title);

    mTextInputName = (TextInputLayout) findViewById(R.id.textInupt_name);
    mTextInputUrl = (TextInputLayout) findViewById(R.id.textInput_url);

    mSpinner = (DropDown) findViewById(R.id.dropDown);
    mSpinner.setAdapter(
        ArrayAdapter.createFromResource(
            this, R.array.custom_open_with, android.R.layout.simple_spinner_dropdown_item));

    mDbHelper = DatabaseHelper.instance(this);
    mData = CustomEngine.getList(this);

    mFAB = (FloatingActionButton) findViewById(R.id.fab);
    mFAB.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            if (!mEnabled) {
              onBackPressed();
            }

            if (!check()) {
              Snackbar.make(mCoordinatorLayout, R.string.check_your_data, Snackbar.LENGTH_LONG)
                  .show();
              return;
            }

            if (mLocation == -1) {
              add();
            } else {
              modify();
            }

            onBackPressed();
          }
        });

    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    mLayoutManager = new MyLinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(EditSitesActivity.getAdapter(this));
    mRecyclerView.setNestedScrollingEnabled(false);
    mRecyclerView.setHasFixedSize(false);

    ItemTouchHelper.SimpleCallback simpleItemTouchCallback =
        new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
          @Override
          public boolean onMove(
              RecyclerView recyclerView,
              RecyclerView.ViewHolder viewHolder,
              RecyclerView.ViewHolder target) {
            return false;
          }

          @Override
          public int getMovementFlags(
              RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            if (viewHolder.getLayoutPosition() == mAdapter.getItemCount() - 1) {
              return 0;
            }
            return super.getMovementFlags(recyclerView, viewHolder);
          }

          @Override
          public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
            mAdapter.notifyItemRemoved(viewHolder.getLayoutPosition());
            mAdapter.setItemCount(mAdapter.getItemCount() - 1);
            mLayoutManager.setFakeItemCount(1);
            mCoordinatorLayout.postDelayed(
                new Runnable() {
                  @Override
                  public void run() {
                    mLayoutManager.setFakeItemCount(0);
                    mRecyclerView.requestLayout();
                  }
                },
                500);
          }
        };

    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);

    itemTouchHelper.attachToRecyclerView(mRecyclerView);

    Intent intent = getIntent();
    if (intent.hasExtra(EXTRA_EDIT_LOCATION)) {
      mLocation = intent.getIntExtra(EXTRA_EDIT_LOCATION, -1);
      mItem = mData.get(mLocation);
      if (mItem != null) {
        mEditTextUrl.setText(mItem.getUpload_url());
        mEditTextName.setText(mItem.getName());
        mEditTextFileKey.setText(mItem.getPost_file_key());
        if (mItem.getResult_open_action() <= CustomEngine.RESULT_OPEN_ACTION.OPEN_HTML_FILE) {
          mSpinner.setSelection(mItem.getResult_open_action());
        }

        mEnabled = (mItem.getId() > 5);
        mAdapter = new PostFormAdapter(mItem, mEnabled);
        mRecyclerView.setAdapter(mAdapter);

        if (!mEnabled) {
          mTextInputName.setEnabled(false);
          mTextInputUrl.setEnabled(false);
          mEditTextUrl.setEnabled(false);
          mEditTextName.setEnabled(false);
          mEditTextFileKey.setEnabled(false);
          mSpinner.setEnabled(false);
          mSpinner.setAdapter(
              ArrayAdapter.createFromResource(
                  this,
                  R.array.custom_open_with_in_app,
                  android.R.layout.simple_spinner_dropdown_item));
          mSpinner.setSelection(mItem.getResult_open_action());
        }

      } else {
        mLocation = -1;
      }
    } else {
      mAdapter = new PostFormAdapter();
      mRecyclerView.setAdapter(mAdapter);
    }

    mEditTextFileKey.setOnFocusChangeListener(
        new View.OnFocusChangeListener() {
          @Override
          public void onFocusChange(View v, boolean hasFocus) {
            setFormTitleColor(hasFocus);
          }
        });

    mAdapter.setOnFocusChangeListener(
        new PostFormAdapter.OnFocusChangeListener() {
          @Override
          public void onFocusChange(View view, boolean hasFocus) {
            setFormTitleColor(hasFocus);
          }
        });

    mEditTextName.addTextChangedListener(new TextChangeRemoveErrorTextWatcher(mTextInputName));
    mEditTextUrl.addTextChangedListener(new TextChangeRemoveErrorTextWatcher(mTextInputUrl));
  }