package cn.net.caoyao.cpzshandset.ui.activity.base;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Window;
import android.view.WindowManager;
import cn.net.caoyao.cpzshandset.MyHandsetApp;
import cn.net.caoyao.cpzshandset.common.config.ConfigCreateService;
import cn.net.caoyao.cpzshandset.common.util.CacheGet;

public abstract class BaseActivity extends Activity {

	protected Activity bContext;
	public ConfigCreateService createService;
	public CacheGet cacheG;
	private MyHandsetApp locationApplication;
	private ActionMode mActionMode;
	private Context context = BaseActivity.this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTranslucentStatus();
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 强制竖屏

		bContext = this;
		createService = new ConfigCreateService();
		cacheG = new CacheGet();
		locationApplication = (MyHandsetApp) getApplication();
		locationApplication.addActivity(this);
		setContentView(loadView());
		initTitleView();
		initView();
		initPresenter();
		setListener();
		initData();
	}

	@Override
	public void onActionModeStarted(ActionMode mode) {
		super.onActionModeStarted(mode);
		mActionMode = mode;
	}

	@Override
	protected void onPause() {
		super.onPause();
		endActionMode();
	}

	private void endActionMode() {
		if (mActionMode != null) {
			mActionMode.finish(); /**
									 * immediately calls
									 * {@link #onActionModeFinished(ActionMode)}
									 */
		}
	}

	@Override
	public void onActionModeFinished(ActionMode mode) {
		super.onActionModeFinished(mode);
		mActionMode = null;
	}

	/**
	 * 退出应用程序
	 */
	public void exitApplication() {
		locationApplication.exitApplication();
	}

	/**
	 * 设置状态栏透明
	 */
	private void setTranslucentStatus() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			// 透明状态栏
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

		}

	}

	public abstract int loadView();

	public abstract void initTitleView();

	public abstract void initView();

	public abstract void initPresenter();

	public abstract void setListener();

	public abstract void initData();
}
