com.guoxiaoxing.kitty.ui.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.guoxiaoxing.kitty.ui.MainActivity.java

Source

package com.guoxiaoxing.kitty.ui;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.guoxiaoxing.kitty.AppConfig;
import com.guoxiaoxing.kitty.AppContext;
import com.guoxiaoxing.kitty.AppManager;
import com.guoxiaoxing.kitty.R;
import com.guoxiaoxing.kitty.cache.DataCleanManager;
import com.guoxiaoxing.kitty.model.Constants;
import com.guoxiaoxing.kitty.model.Notice;
import com.guoxiaoxing.kitty.model.SimpleBackPage;
import com.guoxiaoxing.kitty.service.NoticeUtils;
import com.guoxiaoxing.kitty.ui.activity.SimpleBackActivity;
import com.guoxiaoxing.kitty.ui.base.BaseViewInterface;
import com.guoxiaoxing.kitty.ui.base.OnTabReselectListener;
import com.guoxiaoxing.kitty.ui.dialog.QuickOptionDialog;
import com.guoxiaoxing.kitty.ui.fragment.MainBuyFragment;
import com.guoxiaoxing.kitty.ui.fragment.MainMineFragment;
import com.guoxiaoxing.kitty.ui.fragment.MainShoppingFragment;
import com.guoxiaoxing.kitty.util.DoubleClickExitHelper;
import com.guoxiaoxing.kitty.util.UIHelper;
import com.guoxiaoxing.kitty.util.UpdateManager;
import com.guoxiaoxing.kitty.widget.BadgeView;
import com.guoxiaoxing.kitty.widget.MyFragmentTabHost;
import com.networkbench.agent.impl.NBSAppAgent;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ??
 *
 * @author guoxiaoxing
 */

@SuppressLint("InflateParams")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends AppCompatActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks,
        OnTabChangeListener, BaseViewInterface, View.OnClickListener, OnTouchListener,
        MainShoppingFragment.OnFragmentInteractionListener, MainBuyFragment.OnFragmentInteractionListener {

    public static Notice mNotice;

    @Bind(R.id.tabhost)
    MyFragmentTabHost mTabHost;
    @Bind(R.id.quick_option_iv)
    View mAddBt;

    private BadgeView mBvNotice;
    private DoubleClickExitHelper mDoubleClickExit;
    /*?{@link #restoreActionBar()}.*/
    private CharSequence mTitle;

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Constants.INTENT_ACTION_NOTICE)) {
                mNotice = (Notice) intent.getSerializableExtra("notice_bean");

                int activeCount = 0;
                if (mNotice != null) {
                    int atmeCount = mNotice.getAtmeCount();// @
                    int msgCount = mNotice.getMsgCount();// 
                    int reviewCount = mNotice.getReviewCount();// 
                    int newFansCount = mNotice.getNewFansCount();// ?
                    int newLikeCount = mNotice.getNewLikeCount();// 
                    activeCount = atmeCount + reviewCount + msgCount + newFansCount + newLikeCount;
                }

                Fragment fragment = getCurrentFragment();
                if (fragment instanceof MainMineFragment) {
                    ((MainMineFragment) fragment).setNotice();
                } else {
                    if (activeCount > 0) {
                        mBvNotice.setText(activeCount + "");
                        mBvNotice.show();
                    } else {
                        mBvNotice.hide();
                        mNotice = null;
                    }
                }
            } else if (intent.getAction().equals(Constants.INTENT_ACTION_LOGOUT)) {
                mBvNotice.hide();
                mNotice = null;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (AppContext.getNightModeSwitch()) {
            setTheme(R.style.AppBaseTheme_Night);
        } else {
            setTheme(R.style.AppBaseTheme_Light);
        }
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        AppManager.getAppManager().addActivity(this);

        handleIntent(getIntent());
        // ??
        NBSAppAgent.setLicenseKey("0ed0cc66c5cb45c0a91c6fa932ca99ac").withCrashReportEnabled(true)
                .withLocationServiceEnabled(true).start(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleIntent(intent);
    }

    /**
     * ??intent
     *
     * @param intent
     */
    private void handleIntent(Intent intent) {
        if (intent == null)
            return;
        String action = intent.getAction();
        if (action != null && action.equals(Intent.ACTION_VIEW)) {
            UIHelper.showUrlRedirect(this, intent.getDataString());
        } else if (intent.getBooleanExtra("NOTICE", false)) {
            notifitcationBarClick(intent);
        }
    }

    /**
     * ?
     */
    private void notifitcationBarClick(Intent fromWhich) {
        if (fromWhich != null) {
            boolean fromNoticeBar = fromWhich.getBooleanExtra("NOTICE", false);
            if (fromNoticeBar) {
                Intent toMyInfor = new Intent(this, SimpleBackActivity.class);
                toMyInfor.putExtra(SimpleBackActivity.BUNDLE_KEY_PAGE, SimpleBackPage.MY_MES.getValue());
                startActivity(toMyInfor);
            }
        }
    }

    @Override
    public void initView() {

        AppContext.getInstance().setLogin();

        mDoubleClickExit = new DoubleClickExitHelper(this);

        mTitle = getTitle();

        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        if (android.os.Build.VERSION.SDK_INT > 10) {
            mTabHost.getTabWidget().setShowDividers(0);
        }

        initTabs();

        // ?
        mAddBt.setOnClickListener(this);

        mTabHost.setCurrentTab(0);
        mTabHost.setOnTabChangedListener(this);

        IntentFilter filter = new IntentFilter(Constants.INTENT_ACTION_NOTICE);
        filter.addAction(Constants.INTENT_ACTION_LOGOUT);
        registerReceiver(mReceiver, filter);
        NoticeUtils.bindToService(this);

        if (AppContext.isFristStart()) {
            //            mNavigationDrawerFragment.openDrawerMenu();
            DataCleanManager.cleanInternalCache(AppContext.getInstance());
            AppContext.setFristStart(false);
        }

        //        checkUpdate();
    }

    private void checkUpdate() {
        if (!AppContext.get(AppConfig.KEY_CHECK_UPDATE, true)) {
            return;
        }
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                new UpdateManager(MainActivity.this, false).checkUpdate();
            }
        }, 2000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        NoticeUtils.unbindFromService(this);
        unregisterReceiver(mReceiver);
        mReceiver = null;
        NoticeUtils.tryToShutDown(this);
        AppManager.getAppManager().remove(this);
    }

    @Override
    public void initData() {

    }

    ImageView icon;
    TextView title;

    private void initTabs() {
        MainTab[] tabs = MainTab.values();
        final int size = tabs.length;
        for (int i = 0; i < size; i++) {
            MainTab mainTab = tabs[i];
            TabSpec tab = mTabHost.newTabSpec(getString(mainTab.getResName()));
            View indicator = LayoutInflater.from(getApplicationContext()).inflate(R.layout.tab_indicator, null);
            icon = (ImageView) indicator.findViewById(R.id.tab_icon);
            title = (TextView) indicator.findViewById(R.id.tab_title);

            if (icon != null) {
                icon.setImageResource(mainTab.getResIcon());
            }

            if (i == 2) {
                indicator.setVisibility(View.INVISIBLE);
                //                mTabHost.setNoTabChangedTag(getString(mainTab.getResName()));
            }

            title.setText(getString(mainTab.getResName()));
            tab.setIndicator(indicator);
            tab.setContent(new TabContentFactory() {

                @Override
                public View createTabContent(String tag) {
                    return new View(MainActivity.this);
                }
            });
            mTabHost.addTab(tab, mainTab.getClz(), null);

            if (mainTab.equals(MainTab.MINE)) {
                View cn = indicator.findViewById(R.id.tab_mes);
                mBvNotice = new BadgeView(MainActivity.this, cn);
                mBvNotice.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
                mBvNotice.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
                mBvNotice.setBackgroundResource(R.drawable.notification_bg);
                mBvNotice.setGravity(Gravity.CENTER);
            }
            mTabHost.getTabWidget().getChildAt(i).setOnTouchListener(this);
        }
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
    }

    @Override
    public void onTabChanged(String tabId) {
        final int size = mTabHost.getTabWidget().getTabCount();
        for (int i = 0; i < size; i++) {
            View view = mTabHost.getTabWidget().getChildAt(i);
            if (i == mTabHost.getCurrentTab()) {

                view.setSelected(true);
                Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.btn_zoom_in);
                view.startAnimation(animation);

            } else {
                view.setSelected(false);
            }
        }
        if (tabId.equals(getString(MainTab.MINE.getResName()))) {
            mBvNotice.setText("");
            mBvNotice.hide();
        }
        supportInvalidateOptionsMenu();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
        // ?
        case R.id.quick_option_iv:
            showQuickOption();
            break;
        default:
            break;
        }
    }

    // ??
    private void showQuickOption() {
        final QuickOptionDialog dialog = new QuickOptionDialog(MainActivity.this);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        super.onTouchEvent(event);
        boolean consumed = false;
        // use getTabHost().getCurrentTabView to decide if the current tab is
        // touched again
        if (event.getAction() == MotionEvent.ACTION_DOWN && v.equals(mTabHost.getCurrentTabView())) {
            // use getTabHost().getCurrentView() to get a handle to the view
            // which is displayed in the tab - and to get this views context
            Fragment currentFragment = getCurrentFragment();
            if (currentFragment != null && currentFragment instanceof OnTabReselectListener) {
                OnTabReselectListener listener = (OnTabReselectListener) currentFragment;
                listener.onTabReselect();
                consumed = true;
            }
        }
        return consumed;
    }

    private Fragment getCurrentFragment() {
        return getSupportFragmentManager().findFragmentByTag(mTabHost.getCurrentTabTag());
    }

    /**
     * ?--??
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // ?
            if (AppContext.get(AppConfig.KEY_DOUBLE_CLICK_EXIT, true)) {
                return mDoubleClickExit.onKeyDown(keyCode, event);
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

    }

    @Override
    public void onHomeFragmentInteraction(Uri uri) {

    }
}