package com.jiuqi.njt.widget;
import java.util.List;
import java.util.Set;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;
import cn.sharesdk.framework.ShareSDK;
import com.jiuqi.mobile.nigo.comeclose.bean.base.RoleCode;
import com.jiuqi.njt.R;
import com.jiuqi.njt.data.MyApp;
import com.jiuqi.njt.data.OptsharepreInterface;
import com.jiuqi.njt.data.WorkTaskBeanLocal;
import com.jiuqi.njt.db.WorkTaskNewsBeanDBHelper;
import com.jiuqi.njt.ui.NjtMainActivity;
import com.jiuqi.njt.ui.UploadInBackgroundActivity;
import com.jiuqi.njt.util.ActivityUtil;
import com.jiuqi.njt.util.Constants;
import com.jiuqi.njt.util.UIUtil;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
@TargetApi(Build.VERSION_CODES.FROYO)
public abstract class TabHostActivity extends TabActivity implements
OnClickListener {
private TabHost mTabHost;
private TabWidget mTabWidget;
private LayoutInflater mLayoutflater;
private int Notification_ID_BASE = 119;
/**
* 请登录
*/
protected RoundImageView login_layout;
/**
* 分享
*/
private View btnShare;
/**
* 我的收藏
*/
protected View btnMyFavor;
protected View layMyFavor;
/**
* 数据传输
*/
protected View btnRecord;
protected Button tvRecord;
protected View btnRecordLayout;
/**
* 扫一扫
*/
private View btnCodeScanning;
/**
* 软件设置
*/
protected View btnSet;
/**
* 请登录
*/
private View btnModuleCustom;
protected Button btnWzcx;
protected Button btnGjhf;
protected Button btnNjdd;
protected MyApp myApp;
private int width ;
protected OptsharepreInterface sharePre;
protected IWXAPI api;
private WorkTaskNewsBeanDBHelper dbhelper;
private OnTabChangeListener MyOnTabChangeListener = new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
whenTabChanged(tabId);
}
};
protected TextView loginUserName;
protected View noticeLayout;
protected View btnNotice;
private NotificationManager nm;//通知管理器
private PendingIntent pd;//通知显示的内容
private Notification notification;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set theme because we do not want the shadow
setContentView(R.layout.njt_tab_host);
mLayoutflater = getLayoutInflater();
mTabHost = getTabHost();
mTabHost.setOnTabChangedListener(MyOnTabChangeListener);
mTabWidget = getTabWidget();
mTabWidget.setStripEnabled(false); // need android2.2
prepare();
initTop();
initTabSpec();
initParam();
initWidgets();
initListeners();
initUI();
}
private void initParam() {
// TODO Auto-generated method stub
myApp = (MyApp) this.getApplication();
sharePre = new OptsharepreInterface(this);
ShareSDK.initSDK(this);
api = WXAPIFactory.createWXAPI(this, Constants.TENCENT_APP_KEY, false);
api.registerApp(Constants.TENCENT_APP_KEY);
width = ActivityUtil.getWindowWidth(this);
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this,UploadInBackgroundActivity.class);
pd = PendingIntent.getActivity(TabHostActivity.this, 0, intent, 0);
}
private void initWidgets() {
com.jiuqi.njt.widget.SlidingMenu slidingMenu = (SlidingMenu) findViewById(R.id.id_menu);
myApp.setsMenu(slidingMenu);
login_layout =(RoundImageView)findViewById(R.id.login_layout);
loginUserName = (TextView)findViewById(R.id.loginUserName);
layMyFavor = findViewById(R.id.layMyFavor);
btnSet = findViewById(R.id.btnYyset);
btnShare = findViewById(R.id.btnShare);
btnMyFavor = findViewById(R.id.btnMyFavor);
btnCodeScanning = findViewById(R.id.btnCodeScanning);
btnModuleCustom = findViewById(R.id.btnModuleCustom);
noticeLayout = findViewById(R.id.noticeLayout);
btnNotice = findViewById(R.id.btnNotice);
tvRecord = (Button) findViewById(R.id.recordNum);
btnRecordLayout = findViewById(R.id.btnRecordLayout);
btnRecord = findViewById(R.id.btnRecord);
btnWzcx = (Button) findViewById(R.id.btnWzcx);
btnGjhf = (Button) findViewById(R.id.btnGjhf);
btnNjdd = (Button) findViewById(R.id.btnNjdd);
}
private void initListeners() {
login_layout.setOnClickListener(this);
btnShare.setOnClickListener(this);
btnMyFavor.setOnClickListener(this);
btnCodeScanning.setOnClickListener(this);
btnSet.setOnClickListener(this);
btnModuleCustom.setOnClickListener(this);
loginUserName.setOnClickListener(this);
btnNotice.setOnClickListener(this);
btnRecord.setOnClickListener(this);
btnWzcx.setOnClickListener(this);
btnGjhf.setOnClickListener(this);
btnNjdd.setOnClickListener(this);
}
private void initUI() {
if (myApp.getIsLogin()) {
UIUtil.showView(btnMyFavor);
UIUtil.showView(layMyFavor);
UIUtil.showView(btnRecordLayout);
showNoticeNum();
showNotification();
// UIUtil.showView(noticeLayout);
//写在这块,上传条数提示
String rolesString = sharePre.getPres("rolesString");
int role = UIUtil.tryToInteger(sharePre.getPres("role"));
if(role == RoleCode.Cooper.getCode() ||rolesString.contains(RoleCode.Cooper.getWeiyima())){
UIUtil.showView(btnWzcx);
UIUtil.showView(btnGjhf);
UIUtil.showView(btnNjdd);
}else{
UIUtil.hideView(btnWzcx);
UIUtil.hideView(btnGjhf);
UIUtil.hideView(btnNjdd);
}
}
}
public void showNoticeNum(){
if(dbhelper==null){
dbhelper = new WorkTaskNewsBeanDBHelper(TabHostActivity.this);
}
List<WorkTaskBeanLocal> list = dbhelper.queryAll();
Log.wtf("list", list.size()+"");
if(list!=null && list.size()>0 ){
tvRecord.setVisibility(View.VISIBLE);
setNumTextView(list.size());
}else{
tvRecord.setVisibility(View.GONE);
}
}
public void showNotification()
{
if(dbhelper==null){
dbhelper = new WorkTaskNewsBeanDBHelper(TabHostActivity.this);
}
List<WorkTaskBeanLocal> list = dbhelper.queryAll();
Log.wtf("list", list.size()+"");
notification = new Notification();//新建通知栏
if(list!=null && list.size()>0 ){
notification.icon = R.drawable.ic_launcher_new;
notification.tickerText = "你有"+list.size()+"条待发布信息";
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE ;
// notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.setLatestEventInfo(TabHostActivity.this, "农机通","你有"+list.size()+"条待发布信息",pd );
nm.notify(119,notification);
}else {
nm.cancel(Notification_ID_BASE);
}
}
private void initTop() {
View child = getTop();
if (null != child) {
LinearLayout layout = (LinearLayout) findViewById(R.id.tab_top);
layout.setVisibility(View.VISIBLE);
layout.addView(child);
}
}
private void initTabSpec() {
Set<String> keys = getTabItemKeys();
for (String key : keys) {
// set text view
View tabItem = mLayoutflater.inflate(R.layout.njt_tab_item, null);
if (tabItem instanceof TextView) {
TextView tvTabItem = (TextView) tabItem;
// tabItem.findViewById(R.id.tab_item_tv)
TabItem tab = getTabItemByTitle(key);
// set tab spec
setTabItemNormalState(tvTabItem, tab);
TabSpec tabSpec = mTabHost.newTabSpec(tab.getTitle());
tabSpec.setIndicator(tabItem);
tabSpec.setContent(getTabItemIntent(tab));
mTabHost.addTab(tabSpec);
}
}
}
/** 在初始化界面之前调用 */
protected void prepare() {
// do nothing or you override it
}
/** 自定义头部布局 */
protected View getTop() {
// do nothing or you override it
return null;
}
protected int getTabCount() {
return mTabHost.getTabWidget().getTabCount();
}
/** 设置TabItem的图标和标题等 */
abstract protected void setTabItemNormalState(TextView textView, TabItem tab);
abstract protected TabItem getTabItemByTitle(String key);
abstract protected Intent getTabItemIntent(TabItem tab);
abstract protected Set<String> getTabItemKeys();
abstract protected void whenTabChanged(String tabTitle);
protected void setCurrentTab(int index) {
mTabHost.setCurrentTab(index);
}
protected void focusCurrentTab(int index) {
mTabWidget.focusCurrentTab(index);
}
private void setNumTextView(int size){
LinearLayout.LayoutParams layout = (LayoutParams) tvRecord.getLayoutParams();
layout.width = (int) (ActivityUtil.width * 0.045);
layout.height = (int) (ActivityUtil.width * 0.045);
layout.gravity=Gravity.CENTER;
tvRecord.setLayoutParams(layout);
if(size<10){
tvRecord.setTextSize(0, (int) (width * 0.03));
}else if(size<99){
tvRecord.setTextSize(0, (int) (width * 0.025));
}
tvRecord.setText(String.valueOf(size));
}
@Override
protected void onResume() {
super.onResume();
showNoticeNum();
showNotification();
Log.wtf("TAG", "onResume");
}
}