<commit_before>/**
 * Copyright 2013 <NAME>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.droidparts.widget;

import static org.droidparts.util.Strings.isEmpty;

import org.droidparts.adapter.ui.TextWatcherAdapter;
import org.droidparts.adapter.ui.TextWatcherAdapter.TextWatcherListener;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;

public class ClearableEditText extends EditText implements OnTouchListener,
		TextWatcherListener {

	public interface Listener {
		void didClearEditText();
	}

	public void setClearDrawable(int drawableResId) {
		xD = getResources().getDrawable(drawableResId);
		xD.setBounds(0, 0, xD.getIntrinsicWidth(), xD.getIntrinsicHeight());
	}

	public void setListener(Listener listener) {
		this.listener = listener;
	}

	private Drawable xD;
	private Listener listener;

	public ClearableEditText(Context context) {
		super(context);
		init();
	}

	public ClearableEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ClearableEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		setClearDrawable(android.R.drawable.presence_offline);
		super.setOnTouchListener(this);
		addTextChangedListener(new TextWatcherAdapter(this, this));
		onTextChanged(this, getText().toString());
	}

	@Override
	public void setOnTouchListener(OnTouchListener l) {
		this.l = l;
	}

	private OnTouchListener l;

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (getCompoundDrawables()[2] != null) {
			if (event.getAction() == MotionEvent.ACTION_UP) {
				boolean tappedX = event.getX() > (getWidth()
						- getPaddingRight() - xD.getIntrinsicWidth());
				if (tappedX) {
					setText("");
					if (listener != null) {
						listener.didClearEditText();
					}
					return true;
				}
			}
		}
		if (l != null) {
			return l.onTouch(v, event);
		}
		return false;
	}

	@Override
	public void onTextChanged(EditText view, String text) {
		Drawable x = isEmpty(text) ? null : xD;
		setCompoundDrawables(getCompoundDrawables()[0],
				getCompoundDrawables()[1], x, getCompoundDrawables()[3]);
	}

}<commit_msg>Show clear button if text is present on field focus<commit_after>/**
 * Copyright 2013 <NAME>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.droidparts.widget;

import static org.droidparts.util.Strings.isEmpty;

import org.droidparts.adapter.ui.TextWatcherAdapter;
import org.droidparts.adapter.ui.TextWatcherAdapter.TextWatcherListener;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;

public class ClearableEditText extends EditText implements OnTouchListener,
		OnFocusChangeListener, TextWatcherListener {

	public interface Listener {
		void didClearEditText();
	}

	public void setClearDrawable(int drawableResId) {
		xD = getResources().getDrawable(drawableResId);
		xD.setBounds(0, 0, xD.getIntrinsicWidth(), xD.getIntrinsicHeight());
	}

	public void setListener(Listener listener) {
		this.listener = listener;
	}

	private Drawable xD;
	private Listener listener;

	public ClearableEditText(Context context) {
		super(context);
		init();
	}

	public ClearableEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ClearableEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		setClearDrawable(android.R.drawable.presence_offline);
		super.setOnTouchListener(this);
		super.setOnFocusChangeListener(this);
		addTextChangedListener(new TextWatcherAdapter(this, this));
		onTextChanged(this, getText().toString());
	}

	@Override
	public void setOnTouchListener(OnTouchListener l) {
		this.l = l;
	}

	@Override
	public void setOnFocusChangeListener(OnFocusChangeListener f) {
		this.f = f;
	}

	private OnTouchListener l;
	private OnFocusChangeListener f;
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (getCompoundDrawables()[2] != null) {
			if (event.getAction() == MotionEvent.ACTION_UP) {
				boolean tappedX = event.getX() > (getWidth()
						- getPaddingRight() - xD.getIntrinsicWidth());
				if (tappedX) {
					setText("");
					if (listener != null) {
						listener.didClearEditText();
					}
					return true;
				}
			}
		}
		if (l != null) {
			return l.onTouch(v, event);
		}
		return false;
	}

	@Override
	public void onTextChanged(EditText view, String text) {
		toggleClearDrawable(text);
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		if(hasFocus) {
			toggleClearDrawable(getText().toString());
		}
		if (f != null) {
			f.onFocusChange(v, hasFocus);
		}
	}

	private void toggleClearDrawable(String text) {
		Drawable x = isEmpty(text) ? null : xD;
		setCompoundDrawables(getCompoundDrawables()[0],
				getCompoundDrawables()[1], x, getCompoundDrawables()[3]);
	}
}
{'fraction_non_alphanumeric': 0.10606060606060606, 'fraction_numerical': 0.004419191919191919, 'mean_word_length': 4.510434782608695, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4437114', 'n_tokens_mistral': 2109, 'n_tokens_neox': 1919, 'n_words': 514}