package com.wei.core.activity;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.baidu.mobstat.StatService;
import com.house365.app.analyse.HouseAnalyse;
import com.house365.app.analyse.data.AnalyseMetaData;
import com.house365.core.R;
import com.house365.core.action.ActionTag;
import com.house365.core.anim.AnimBean;
import com.house365.core.application.BaseApplication;
import com.house365.core.constant.CorePreferences;
import com.house365.core.image.AsyncImageLoader;
import com.house365.core.image.AsyncImageLoader.BitMapInputStream;
import com.house365.core.image.CacheImageUtil;
import com.house365.core.image.CacheImageUtil.TextImagePosition;
import com.house365.core.image.ImageLoadedCallback;
import com.house365.core.touch.ImageViewTouch;
import com.house365.core.view.LoadingDialog;
import com.umeng.analytics.MobclickAgent;
public abstract class BaseCommonActivity extends FragmentActivity {
protected String getTAG() {
return this.getClass().getName();
}
protected Activity thisInstance;
protected LoadingDialog tloadingDialog;
private AsyncImageLoader mAil;
protected BaseApplication mApplication;
protected AlertDialog.Builder alertDialog;
private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
protected void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
thisInstance = this;
mApplication = (BaseApplication) getApplication();
registerReceiver(mLoggedOutReceiver, new IntentFilter(ActionTag.INTENT_ACTION_LOGGED_OUT));
preparedCreate(savedInstanceState);
initView();
initData();
}
protected abstract void preparedCreate(android.os.Bundle savedInstanceState);
protected abstract void initView();
protected abstract void initData();
protected void onDestroy() {
// overridePendingTransition(R.anim.slide_fix, R.anim.slide_out_bottom);
super.onDestroy();
dismissLoadingDialog();
tloadingDialog = null;
alertDialog = null;
if (mAil != null) {
mAil.clearCacheImage();
}
clean();
// mApplication.onAppCancel();
unregisterReceiver(mLoggedOutReceiver);
}
public void setImage(final ImageView imageView, String imageUrl, final int resId, int scaleType) {
CacheImageUtil.setCacheImage(imageView, imageUrl, resId, scaleType, getImageLoader());
}
// liuziqiang overload setImage
public void setImage(final ImageView imageView, String imageUrl, int scaleType, final ImageLoadedCallback imgLoadObj) {
CacheImageUtil.setCacheImage(imageView, imageUrl, scaleType, getImageLoader(), imgLoadObj);
}
public void setTextImage(final TextView textView, TextImagePosition position, String imageUrl) {
CacheImageUtil.setCacheTextImage(textView, position, imageUrl, getImageLoader());
}
public void setTouchImage(final ImageViewTouch imageView, String imageUrl, final int resId, int scaleType) {
CacheImageUtil.setTouchImage(imageView, imageUrl, getResources(), resId, scaleType, getImageLoader());
}
public AsyncImageLoader getImageLoader() {
if (mAil == null) {
mAil = new AsyncImageLoader(this);
}
return mAil;
}
public void setImage(final ImageView imageView, final String localFileUri) {
try {
int w = imageView.getWidth();
int h = imageView.getHeight();
if (w < 0) {
w = -1;
}
if (h < 0) {
h = -1;
}
Bitmap bitmap = mAil.getBitMapFromStream(new BitMapInputStream() {
@Override
public InputStream getInputStream() throws IOException {
return new FileInputStream(localFileUri);
}
}, w, h, mAil.IMAGE_SCALE_TYPE_NONE);
if (bitmap != null && !bitmap.isRecycled()) {
imageView.setImageBitmap(bitmap);
}
} catch (IOException e) {
CorePreferences.ERROR(e);
}
}
private LoadingDialog getLoadingDialog() {
if (tloadingDialog == null) {
tloadingDialog = new LoadingDialog(this, R.style.dialog, R.string.loading);
tloadingDialog.setCancelable(isCancelDialog());
if (isCancelDialog()) {
tloadingDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
onExitDialog();
}
});
}
}
return tloadingDialog;
}
public void showLoadingDialog() {
if (getLoadingDialog() != null) {
getLoadingDialog().show();
}
}
public void showLoadingDialog(int resid) {
if (getLoadingDialog() != null) {
getLoadingDialog().setMessage(this.getResources().getString(resid));
getLoadingDialog().show();
}
}
public void dismissLoadingDialog() {
if (getLoadingDialog() != null) {
getLoadingDialog().dismiss();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
try {
super.onSaveInstanceState(outState);
} catch (NullPointerException e) {
}
}
public AlertDialog.Builder getAlertDialog() {
if (alertDialog == null) {
alertDialog = new AlertDialog.Builder(this);
}
return alertDialog;
}
public void showToast(String str) {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
public void showToast(int resId) {
Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
}
public void onExitDialog() {
thisInstance.finish();
}
public boolean isCancelDialog() {
return true;
}
protected void clean() {
}
AnalyseMetaData analyseMetadata;
protected void onResume() {
super.onResume();
if (CorePreferences.getInstance(this).getCoreConfig().isAnalyse()) {
analyseMetadata = HouseAnalyse.onPageResume(this);
// UMeng
String pageName = this.getClass().getSimpleName();
CorePreferences.DEBUG("pageName:" + pageName);
MobclickAgent.onPageStart(pageName);
MobclickAgent.onResume(this);
}
if (CorePreferences.getInstance(this).getCoreConfig().isOpenBaiduStat()) {
StatService.onResume(this);
}
}
protected void onPause() {
super.onPause();
if (CorePreferences.getInstance(this).getCoreConfig().isAnalyse()) {
// 自有统计后台
HouseAnalyse.onPagePause(analyseMetadata);
// UMeng
String pageName = this.getClass().getSimpleName();
CorePreferences.DEBUG("pageName:" + pageName);
MobclickAgent.onPageEnd(pageName);
MobclickAgent.onPause(this);
}
if (CorePreferences.getInstance(this).getCoreConfig().isOpenBaiduStat()) {
StatService.onPause(this);
}
}
public void finish() {
super.finish();
AnimBean animBean = getFinishAnim();
if (animBean != null && animBean.getIn() > 0 && animBean.getOut() > 0) {
overridePendingTransition(animBean.getIn(), animBean.getOut());
}
}
@Override
public void startActivity(Intent intent) {
super.startActivity(intent);
AnimBean animBean = getStartAnim();
if (animBean != null && animBean.getIn() > 0 && animBean.getOut() > 0) {
overridePendingTransition(animBean.getIn(), animBean.getOut());
}
}
@Override
public void startActivityForResult(Intent intent, int requestCode) {
super.startActivityForResult(intent, requestCode);
AnimBean animBean = getStartAnim();
if (animBean != null && animBean.getIn() > 0 && animBean.getOut() > 0) {
overridePendingTransition(animBean.getIn(), animBean.getOut());
}
}
public AnimBean getStartAnim() {
// return new AnimBean(R.anim.slide_in_bottom, R.anim.slide_fix);
return new AnimBean(R.anim.slide_in_right, R.anim.slide_fix);
}
public AnimBean getFinishAnim() {
// return new AnimBean(R.anim.slide_fix, R.anim.slide_out_bottom);
return new AnimBean(R.anim.slide_fix, R.anim.slide_out_right);
}
}