/*
* Copyright (c) 2015 [1076559197@qq.com | tchen0707@gmail.com]
*
* 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 com.youku.player.base;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import com.github.obsessive.library.base.BaseAppManager;
import com.github.obsessive.library.eventbus.EventCenter;
import com.github.obsessive.library.loading.VaryViewHelperController;
import com.github.obsessive.library.netstatus.NetChangeObserver;
import com.github.obsessive.library.netstatus.NetStateReceiver;
import com.github.obsessive.library.netstatus.NetUtils;
import com.github.obsessive.library.swipeback.SwipeBackActivityBase;
import com.github.obsessive.library.swipeback.SwipeBackActivityHelper;
import com.github.obsessive.library.swipeback.SwipeBackLayout;
import com.github.obsessive.library.swipeback.Utils;
import com.github.obsessive.library.utils.CommonUtils;
import com.github.obsessive.library.utils.SmartBarUtils;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
/**
* Author: Tau.Chen
* Email: 1076559197@qq.com | tauchen1990@gmail.com
* Date: 15/7/21
* Description:
*/
public abstract class BasePlayerActivity extends YoukuBasePlayerActivity implements SwipeBackActivityBase {
/**
* Log tag
*/
protected static String TAG_LOG = null;
/**
* Screen information
*/
protected int mScreenWidth = 0;
protected int mScreenHeight = 0;
protected float mScreenDensity = 0.0f;
/**
* context
*/
protected Context mContext = null;
/**
* network status
*/
protected NetChangeObserver mNetChangeObserver = null;
/**
* loading view controller
*/
private VaryViewHelperController mVaryViewHelperController = null;
private SwipeBackActivityHelper mHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// base setup
mHelper = new SwipeBackActivityHelper(this);
mHelper.onActivityCreate();
Bundle extras = getIntent().getExtras();
if (null != extras) {
getBundleExtras(extras);
}
if (isBindEventBusHere()) {
EventBus.getDefault().register(this);
}
SmartBarUtils.hide(getWindow().getDecorView());
setTranslucentStatus(isApplyStatusBarTranslucency());
mContext = this;
TAG_LOG = this.getClass().getSimpleName();
BaseAppManager.getInstance().addActivity(this);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
mScreenDensity = displayMetrics.density;
mScreenHeight = displayMetrics.heightPixels;
mScreenWidth = displayMetrics.widthPixels;
if (getContentViewLayoutID() != 0) {
setContentView(getContentViewLayoutID());
} else {
throw new IllegalArgumentException("You must return a right contentView layout resource Id");
}
mNetChangeObserver = new NetChangeObserver() {
@Override
public void onNetConnected(NetUtils.NetType type) {
super.onNetConnected(type);
onNetworkConnected(type);
}
@Override
public void onNetDisConnect() {
super.onNetDisConnect();
onNetworkDisConnected();
}
};
NetStateReceiver.registerObserver(mNetChangeObserver);
initViewsAndEvents();
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mHelper.onPostCreate();
}
@Override
public View findViewById(int id) {
View v = super.findViewById(id);
if (v == null && mHelper != null)
return mHelper.findViewById(id);
return v;
}
@Override
public SwipeBackLayout getSwipeBackLayout() {
return mHelper.getSwipeBackLayout();
}
@Override
public void setSwipeBackEnable(boolean enable) {
getSwipeBackLayout().setEnableGesture(enable);
}
@Override
public void scrollToFinishActivity() {
Utils.convertActivityToTranslucent(this);
getSwipeBackLayout().scrollToFinishActivity();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
ButterKnife.inject(this);
if (null != getLoadingTargetView()) {
mVaryViewHelperController = new VaryViewHelperController(getLoadingTargetView());
}
}
@Override
public void finish() {
super.finish();
BaseAppManager.getInstance().removeActivity(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
ButterKnife.reset(this);
NetStateReceiver.removeRegisterObserver(mNetChangeObserver);
if (isBindEventBusHere()) {
EventBus.getDefault().unregister(this);
}
}
/**
* get bundle data
*
* @param extras
*/
protected abstract void getBundleExtras(Bundle extras);
/**
* bind layout resource file
*
* @return id of layout resource
*/
protected abstract int getContentViewLayoutID();
/**
* when event comming
*
* @param eventCenter
*/
protected abstract void onEventComming(EventCenter eventCenter);
/**
* get loading target view
*/
protected abstract View getLoadingTargetView();
/**
* init all views and add events
*/
protected abstract void initViewsAndEvents();
/**
* network connected
*/
protected abstract void onNetworkConnected(NetUtils.NetType type);
/**
* network disconnected
*/
protected abstract void onNetworkDisConnected();
/**
* is applyStatusBarTranslucency
*
* @return
*/
protected abstract boolean isApplyStatusBarTranslucency();
/**
* is bind eventBus
*
* @return
*/
protected abstract boolean isBindEventBusHere();
/**
* startActivity
*
* @param clazz
*/
protected void readyGo(Class<?> clazz) {
Intent intent = new Intent(this, clazz);
startActivity(intent);
}
/**
* startActivity with bundle
*
* @param clazz
* @param bundle
*/
protected void readyGo(Class<?> clazz, Bundle bundle) {
Intent intent = new Intent(this, clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
startActivity(intent);
}
/**
* startActivity then finish
*
* @param clazz
*/
protected void readyGoThenKill(Class<?> clazz) {
Intent intent = new Intent(this, clazz);
startActivity(intent);
finish();
}
/**
* startActivity with bundle then finish
*
* @param clazz
* @param bundle
*/
protected void readyGoThenKill(Class<?> clazz, Bundle bundle) {
Intent intent = new Intent(this, clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
startActivity(intent);
finish();
}
/**
* startActivityForResult
*
* @param clazz
* @param requestCode
*/
protected void readyGoForResult(Class<?> clazz, int requestCode) {
Intent intent = new Intent(this, clazz);
startActivityForResult(intent, requestCode);
}
/**
* startActivityForResult with bundle
*
* @param clazz
* @param requestCode
* @param bundle
*/
protected void readyGoForResult(Class<?> clazz, int requestCode, Bundle bundle) {
Intent intent = new Intent(this, clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
startActivityForResult(intent, requestCode);
}
/**
* show toast
*
* @param msg
*/
protected void showToast(String msg) {
if (null != msg && !CommonUtils.isEmpty(msg)) {
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}
/**
* toggle show loading
*
* @param toggle
*/
protected void toggleShowLoading(boolean toggle, String msg) {
if (null == mVaryViewHelperController) {
throw new IllegalArgumentException("You must return a right target view for loading");
}
if (toggle) {
mVaryViewHelperController.showLoading(msg);
} else {
mVaryViewHelperController.restore();
}
}
/**
* toggle show empty
*
* @param toggle
*/
protected void toggleShowEmpty(boolean toggle, String msg, View.OnClickListener onClickListener) {
if (null == mVaryViewHelperController) {
throw new IllegalArgumentException("You must return a right target view for loading");
}
if (toggle) {
mVaryViewHelperController.showEmpty(msg, onClickListener);
} else {
mVaryViewHelperController.restore();
}
}
/**
* toggle show error
*
* @param toggle
*/
protected void toggleShowError(boolean toggle, String msg, View.OnClickListener onClickListener) {
if (null == mVaryViewHelperController) {
throw new IllegalArgumentException("You must return a right target view for loading");
}
if (toggle) {
mVaryViewHelperController.showError(msg, onClickListener);
} else {
mVaryViewHelperController.restore();
}
}
/**
* toggle show network error
*
* @param toggle
*/
protected void toggleNetworkError(boolean toggle, View.OnClickListener onClickListener) {
if (null == mVaryViewHelperController) {
throw new IllegalArgumentException("You must return a right target view for loading");
}
if (toggle) {
mVaryViewHelperController.showNetworkError(onClickListener);
} else {
mVaryViewHelperController.restore();
}
}
public void onEventMainThread(EventCenter eventCenter) {
if (null == mVaryViewHelperController) {
throw new IllegalArgumentException("You must return a right target view for loading");
}
if (null != eventCenter) {
onEventComming(eventCenter);
}
}
/**
* use SytemBarTintManager
*
* @param tintDrawable
*/
protected void setSystemBarTintDrawable(Drawable tintDrawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
SystemBarTintManager mTintManager = new SystemBarTintManager(this);
if (tintDrawable != null) {
mTintManager.setStatusBarTintEnabled(true);
mTintManager.setTintDrawable(tintDrawable);
} else {
mTintManager.setStatusBarTintEnabled(false);
mTintManager.setTintDrawable(null);
}
}
}
/**
* set status bar translucency
*
* @param on
*/
protected void setTranslucentStatus(boolean on) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
}
}