com.shenma.yueba.yangjia.activity.MainActivityForYangJia.java Source code

Java tutorial

Introduction

Here is the source code for com.shenma.yueba.yangjia.activity.MainActivityForYangJia.java

Source

package com.shenma.yueba.yangjia.activity;

/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 *
 * 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.
 *******************************************************************************/

import im.broadcast.ImBroadcastReceiver;
import im.broadcast.ImBroadcastReceiver.ImBroadcastReceiverLinstener;
import im.broadcast.ImBroadcastReceiver.RECEIVER_type;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;

import com.shenma.yueba.R;
import com.shenma.yueba.application.MyApplication;
import com.shenma.yueba.baijia.modle.RequestUploadProductDataBean;
import com.shenma.yueba.camera2.ActivityCapture;
import com.shenma.yueba.util.FileUtils;
import com.shenma.yueba.util.ToolsUtil;
import com.shenma.yueba.view.SelecteKXPOrPublishType;
import com.shenma.yueba.yangjia.fragment.CircleFragment;
import com.shenma.yueba.yangjia.fragment.IndexFragmentForYangJia;
import com.shenma.yueba.yangjia.fragment.MeFragmentForYangJia;
import com.shenma.yueba.yangjia.fragment.MessageFragmentForYangJia;
import com.shenma.yueba.yangjia.fragment.TaskRewardFragment;
import com.umeng.analytics.MobclickAgent;

/**
 * ?
 * @author a
 *
 */
public final class MainActivityForYangJia extends FragmentActivity implements ImBroadcastReceiverLinstener {
    private long exitTime = 0;// ??
    private FragmentTabHost mTabHost;
    //   // ?
    private int mImageViewArray[] = { R.drawable.tab_index_selector, R.drawable.tab_reward_selector,
            R.drawable.tab_publish_selector, R.drawable.tab_msg_selector, R.drawable.tab_me_selector };
    // Tab?
    private String mTextviewArray[] = { "", "", "?", "?", "" };
    // ?Fragment?
    private Class fragmentArray[] = { IndexFragmentForYangJia.class, TaskRewardFragment.class, CircleFragment.class,
            MessageFragmentForYangJia.class, MeFragmentForYangJia.class };
    View round_view;//?
    ImBroadcastReceiver imBroadcastReceiver;
    boolean isbroadcase = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);//
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        imBroadcastReceiver = new ImBroadcastReceiver(this);
        initView();

        //      CustomProgressDialog progressDialog = CustomProgressDialog.createDialog(this);
        //      progressDialog.setMessage("...");
        //      progressDialog.show();

    }

    /**
     * ?
     */
    private void initView() {
        TextView yangjia_msg_textview = (TextView) findViewById(R.id.yangjia_msg_textview);
        round_view = findViewById(R.id.round_view);
        registerBroadcase();
        //      yangjia_msg_textview.setOnClickListener(new OnClickListener() {
        //         
        //         @Override
        //         public void onClick(View v) {
        //            setRedView(false);
        //         }
        //      });
        TextView tv_center = (TextView) findViewById(R.id.tv_center);
        tv_center.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {//?
                    //               showDialog();
                    showBottomDialog();
                }
                return true;
            }
        });

        tv_center.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivityForYangJia.this, "click", 1000).show();
            }
        });
        // TabHostTabHost
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        // fragment
        int count = fragmentArray.length;
        for (int i = 0; i < count; i++) {
            // ?Tab?
            TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
            // TabTab?
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
        }
        mTabHost.getTabWidget().setDividerDrawable(null); //tab
        mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

            @Override
            public void onTabChanged(String tabId) {
                if (tabId.equals("?")) {
                    setRedView(false);
                }
            }
        });
    }

    /**
     * Tab
     */
    private View getTabItemView(int index) {
        View view = View.inflate(this, R.layout.tab_item_view, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        imageView.setImageResource(mImageViewArray[index]);
        //   TextView textView = (TextView) view.findViewById(R.id.textview);
        //   textView.setText(mTextviewArray[index]);
        //   FontManager.changeFonts(getApplicationContext(), textView);
        return view;
    }

    @Override
    protected void onDestroy() {
        MyApplication.getInstance().removeActivity(this);
        super.onDestroy();
        unregisterBroadcase();
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                exitTime = System.currentTimeMillis();
                Toast.makeText(getApplicationContext(), "??", Toast.LENGTH_SHORT).show();
            } else {
                MyApplication.getInstance().exit();
            }
            return true; // true???
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * (???)
     */
    protected void showBottomDialog() {
        ToolsUtil.hideSoftInputKeyBoard(MainActivityForYangJia.this);
        ShowMenu showMenu = new ShowMenu(MainActivityForYangJia.this, findViewById(R.id.parent),
                R.layout.main_popwindow);
        showMenu.createView();
    }

    /**
     * ??
     * 
     * @author
     */
    class ShowMenu extends SelecteKXPOrPublishType {
        public ShowMenu(Activity activity, View parent, int popLayout) {
            super(activity, parent, popLayout);
        }

        @Override
        public void onExitClick(View v) {
            canceView();
        }

        @Override
        public void onPublish(View v) {
            MyApplication.getInstance().getPublishUtil().setBean(new RequestUploadProductDataBean());
            MyApplication.getInstance().getPublishUtil().setIndex("0");
            MyApplication.getInstance().getPublishUtil().getTagCacheList().clear();
            FileUtils.delAllFile(FileUtils.getRootPath() + "/tagPic/");
            Intent intentCamera = new Intent(MainActivityForYangJia.this, ActivityCapture.class);
            startActivity(intentCamera);
            canceView();
        }

        @Override
        public void onKxp(View v) {
            // ?
            Intent intentXP = new Intent(MainActivityForYangJia.this, KaiXiaoPiaoActivity.class);
            startActivity(intentXP);
            canceView();
        }

    }

    @Override
    public void newMessage(Object obj) {
        // TODO Auto-generated method stub
        if (!mTabHost.getCurrentTabTag().equals("?")) {
            setRedView(true);
        }

    }

    @Override
    public void roomMessage(Object obj) {
        // TODO Auto-generated method stub
        if (!mTabHost.getCurrentTabTag().equals("?")) {
            setRedView(true);
        }
    }

    @Override
    public void clearMsgNotation(RECEIVER_type type) {
        // TODO Auto-generated method stub

    }

    /***
     *  ??
     * @param i int ? item  
     * @param b  boolean ? true  false?
     * **/
    void setRedView(boolean b) {
        if (round_view != null) {
            if (b) {
                round_view.setVisibility(View.VISIBLE);
            } else {
                round_view.setVisibility(View.GONE);
            }
        }
    }

    /******
     *  ??
     * ***/
    void registerBroadcase() {
        if (!isbroadcase) {
            isbroadcase = true;
            MainActivityForYangJia.this.registerReceiver(imBroadcastReceiver,
                    new IntentFilter(ImBroadcastReceiver.IntentFilterRoomMsg));
        }

    }

    /******
     *  ??
     * ***/
    void unregisterBroadcase() {
        if (isbroadcase) {
            MainActivityForYangJia.this.unregisterReceiver(imBroadcastReceiver);
            isbroadcase = false;
        }

    }
}