/*******************************************************************************
* Copyright (c) 2011 Michel DAVID mimah35-at-gmail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package fr.gotorennes.view;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import fr.gotorennes.R;
import fr.gotorennes.util.BackgroundTask;
public class ExpandableView extends LinearLayout {
private boolean executed = false;
private boolean showing = false;
private ImageView expandIcon;
private BackgroundTask<?> backgroundTask;
private Intent intent;
public ExpandableView(final Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(VERTICAL);
final View view = inflate(context, R.layout.expandableview_title, null);
ImageView imageView = (ImageView) view.findViewById(R.id.icon);
imageView.setImageResource(attrs.getAttributeResourceValue(null, "icon", 0));
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setText(attrs.getAttributeResourceValue(null, "title", 0));
expandIcon = (ImageView) view.findViewById(R.id.expand);
final boolean link = attrs.getAttributeBooleanValue(null, "link", false);
if(link) {
expandIcon.setImageResource(R.drawable.arrow_right_float);
}
addView(view);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View paramView) {
if(link) {
context.startActivity(intent);
}
else {
final View innerView = getChildAt(1);
if (!executed && backgroundTask != null) {
backgroundTask.addHandler(new Handler() {
@Override
public void handleMessage(Message message) {
executed = true;
showing = true;
expandIcon.setImageResource(android.R.drawable.arrow_up_float);
show(innerView);
}
});
backgroundTask.start((Activity) getContext());
} else {
if (showing) {
expandIcon.setImageResource(android.R.drawable.arrow_down_float);
hide(innerView);
} else {
expandIcon.setImageResource(android.R.drawable.arrow_up_float);
show(innerView);
}
showing = !showing;
}
}
}
});
}
protected void show(final View view) {
AlphaAnimation showing = new AlphaAnimation(0, 1);
showing.setDuration(500);
showing.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
view.setVisibility(VISIBLE);
}
});
view.setAnimation(showing);
}
protected void hide(final View view) {
AlphaAnimation hiding = new AlphaAnimation(1, 0);
hiding.setDuration(500);
hiding.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
view.setVisibility(GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
});
view.setAnimation(hiding);
}
public void reset() {
executed = false;
showing = false;
getChildAt(1).setVisibility(GONE);
expandIcon.setImageResource(android.R.drawable.arrow_down_float);
}
public void setBackgroundTask(BackgroundTask<?> backgroundTask) {
this.backgroundTask = backgroundTask;
}
public void setIntent(Intent intent) {
this.intent = intent;
}
}