com.meinv.ui.activity.HomeActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.meinv.ui.activity.HomeActivity.java

Source

/*
 * 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.meinv.ui.activity;

import android.annotation.TargetApi;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.SearchView;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.baichuan.android.trade.AlibcTrade;
import com.alibaba.baichuan.android.trade.callback.AlibcTradeCallback;
import com.alibaba.baichuan.android.trade.constants.AlibcConstants;
import com.alibaba.baichuan.android.trade.model.AlibcShowParams;
import com.alibaba.baichuan.android.trade.model.OpenType;
import com.alibaba.baichuan.android.trade.model.ResultType;
import com.alibaba.baichuan.android.trade.model.TradeResult;
import com.alibaba.baichuan.android.trade.page.AlibcBasePage;
import com.alibaba.baichuan.android.trade.page.AlibcShopPage;
import com.library.adapter.ListViewDataAdapter;
import com.library.adapter.ViewHolderBase;
import com.library.adapter.ViewHolderCreator;
import com.library.base.BaseLazyFragment;
import com.library.base.BaseWebActivity;
import com.library.eventbus.EventCenter;
import com.library.netstatus.NetUtils;
import com.library.widgets.XViewPager;
import com.meinv.bean.NavigationEntity;
import com.meinv.presenter.Presenter;
import com.meinv.presenter.impl.HomePresenterImpl;
import com.meinv.ui.activity.base.BaseActivity;
import com.meinv.ui.adpter.VPFragmentAdapter;
import com.meinv.ui.fragment.VideosContainerFragment;
import com.meinv.vibrator.ui.MainActivity;
import com.meinv.view.HomeView;
import com.nineoldandroids.view.ViewHelper;
import com.w.meinv.R;

import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Author:  Tau.Chen
 * Email:   1076559197@qq.com | tauchen1990@gmail.com
 * Date:    2015/3/12.
 * Description:
 */
public class HomeActivity extends BaseActivity implements HomeView {

    private static long DOUBLE_CLICK_TIME = 0L;

    @InjectView(R.id.home_container)
    XViewPager mViewPager;

    @InjectView(R.id.home_navigation_list)
    ListView mNavListView;

    @InjectView(R.id.home_drawer)
    DrawerLayout mDrawerLayout;

    @InjectView(R.id.rl_left)
    RelativeLayout mRlLeft;

    private int mCheckedListItemColorResIds[] = { R.color.navigation_checked_news_text_color,
            R.color.navigation_checked_news_text_color, R.color.navigation_checked_news_text_color, };
    private int mCurrentMenuCheckedPos = 0;
    private ActionBarDrawerToggle mActionBarDrawerToggle = null;
    private ListViewDataAdapter<NavigationEntity> mNavListAdapter = null;

    private Presenter mHomePresenter = null;
    //    private FeedbackAgent mFeedbackAgent = null;
    private SearchView mSearchView;
    private MenuItem mSearchItem;
    private VPFragmentAdapter mVpAdapter;

    private String shopId = "60552065";//id

    public void showShop(View view) {

        AlibcBasePage alibcBasePage = new AlibcShopPage(shopId.trim());

        AlibcShowParams alibcShowParams = new AlibcShowParams(OpenType.H5, false);

        HashMap<Object, Object> exParams = new HashMap<>();
        exParams.put(AlibcConstants.ISV_CODE, "appisvcode");
        exParams.put("alibaba", "");//???

        mDrawerLayout.closeDrawer(Gravity.LEFT);

        AlibcTrade.show(this, alibcBasePage, alibcShowParams, null, exParams, new AlibcTradeCallback() {
            @Override
            public void onTradeSuccess(TradeResult tradeResult) {
                //addCartPage?page?

                if (tradeResult.resultType.equals(ResultType.TYPECART)) {
                    //?
                    Toast.makeText(HomeActivity.this, "?", Toast.LENGTH_SHORT).show();
                } else if (tradeResult.resultType.equals(ResultType.TYPEPAY)) {
                    //?
                    Toast.makeText(HomeActivity.this,
                            "?,???" + tradeResult.payResult.paySuccessOrders,
                            Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                Toast.makeText(HomeActivity.this,
                        "SDK,?=" + errCode + " / ?=" + errMsg, Toast.LENGTH_SHORT)
                        .show();
            }
        });
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_home;
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {

    }

    @Override
    protected View getLoadingTargetView() {
        return ButterKnife.findById(this, R.id.home_container);
    }

    @Override
    protected void initViewsAndEvents() {
        //        mFeedbackAgent = new FeedbackAgent(this);
        //        mFeedbackAgent.sync();
        //        mFeedbackAgent.closeFeedbackPush();
        //        mFeedbackAgent.closeAudioFeedback();
        //        mFeedbackAgent.setWelcomeInfo(getResources().getString(R.string.feedback_welcome_info));

        mHomePresenter = new HomePresenterImpl(this, this);
        mHomePresenter.initialized();
    }

    @Override
    public void initializeViews(List<BaseLazyFragment> fragments, List<NavigationEntity> navigationList) {
        mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open,
                R.string.drawer_close) {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                View mContent = mDrawerLayout.getChildAt(0);
                View mMenu = drawerView;
                float scale = 1 - slideOffset;
                float rightScale = 0.8f + scale * 0.2f;
                if (drawerView.getTag() == null) {
                    return;
                }
                if (drawerView.getTag().equals("LEFT")) {

                    float leftScale = 1 - 0.3f * scale;

                    //                    ViewHelper.setScaleX(mMenu, leftScale);
                    //                    ViewHelper.setScaleY(mMenu, leftScale);
                    ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale));
                    ViewHelper.setTranslationX(mContent, mMenu.getMeasuredWidth() * (1 - scale));
                    ViewHelper.setPivotX(mContent, 0);
                    ViewHelper.setPivotY(mContent, mContent.getMeasuredHeight() / 2);
                    mContent.invalidate();
                    ViewHelper.setScaleX(mContent, rightScale);
                    ViewHelper.setScaleY(mContent, rightScale);
                } else {
                    ViewHelper.setTranslationX(mContent, -mMenu.getMeasuredWidth() * slideOffset);
                    ViewHelper.setPivotX(mContent, mContent.getMeasuredWidth());
                    ViewHelper.setPivotY(mContent, mContent.getMeasuredHeight() / 2);
                    mContent.invalidate();
                    ViewHelper.setScaleX(mContent, rightScale);
                    ViewHelper.setScaleY(mContent, rightScale);
                }

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                setTitle(getString(R.string.app_name));
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                if (null != mNavListAdapter) {
                    setTitle(mNavListAdapter.getItem(mCurrentMenuCheckedPos).getName());
                }
            }
        };

        mActionBarDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);

        if (null != fragments && !fragments.isEmpty()) {
            mViewPager.setEnableScroll(false);
            mViewPager.setOffscreenPageLimit(fragments.size());
            mVpAdapter = new VPFragmentAdapter(getSupportFragmentManager(), fragments);
            mViewPager.setAdapter(mVpAdapter);
        }

        mNavListAdapter = new ListViewDataAdapter<NavigationEntity>(new ViewHolderCreator<NavigationEntity>() {

            @Override
            public ViewHolderBase<NavigationEntity> createViewHolder(int position) {

                return new ViewHolderBase<NavigationEntity>() {

                    ImageView itemIcon;
                    TextView itemName;

                    @Override
                    public View createView(LayoutInflater layoutInflater) {
                        View convertView = layoutInflater.inflate(R.layout.list_item_navigation, null);
                        itemIcon = ButterKnife.findById(convertView, R.id.list_item_navigation_icon);
                        itemName = ButterKnife.findById(convertView, R.id.list_item_navigation_name);

                        return convertView;
                    }

                    @Override
                    public void showData(int i, NavigationEntity navigationEntity) {
                        itemIcon.setImageResource(navigationEntity.getIconResId());
                        itemName.setText(navigationEntity.getName());
                        if (mCurrentMenuCheckedPos == i) {
                            // checked
                            itemName.setTextColor(getResources().getColor(mCheckedListItemColorResIds[i]));
                        } else {
                            // unchecked
                            itemName.setTextColor(getResources().getColor(android.R.color.black));
                        }
                    }
                };
            }
        });

        mNavListView.setAdapter(mNavListAdapter);
        mNavListAdapter.getDataList().addAll(navigationList);
        mNavListAdapter.notifyDataSetChanged();
        setTitle(mNavListAdapter.getItem(mCurrentMenuCheckedPos).getName());

        mNavListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mCurrentMenuCheckedPos = position;
                mSearchItem.setVisible(mCurrentMenuCheckedPos == 0);
                mSearchItem.collapseActionView();
                mNavListAdapter.notifyDataSetChanged();
                mDrawerLayout.closeDrawer(Gravity.LEFT);
                mViewPager.setCurrentItem(mCurrentMenuCheckedPos, false);
            }
        });
        setupBannerAdForView(mRlLeft);
        //        setupBannerAd();
    }

    /**
     * ??
     */
    private void setupSpotAd() {
        // ???
        //        SpotManager.getInstance(mContext).loadSpotAds();
        //        // ???????
        //        SpotManager.getInstance(mContext)
        //                .setSpotOrientation(SpotManager.ORIENTATION_LANDSCAPE);
        //        // ??0:ANIM_NONE1:ANIM_SIMPLE?2:ANIM_ADVANCE
        //        SpotManager.getInstance(mContext).setAnimationType(SpotManager.ANIM_ADVANCE);
        //
        ////        Button btnShowSpot = (Button) findViewById(R.id.btn_show_spot);
        ////        if (btnShowSpot.getVisibility() == View.GONE) {
        ////            btnShowSpot.setVisibility(View.VISIBLE);
        ////        }
        ////
        ////        btnShowSpot.setOnClickListener(new View.OnClickListener() {
        ////            @Override
        ////            public void onClick(View v) {
        //        // ????
        //        SpotManager.getInstance(mContext)
        //                .showSpotAds(mContext, new SpotDialogListener() {
        //                    public static final String TAG = "AD";
        //
        //                    @Override
        //                    public void onShowSuccess() {
        //                        Log.i(TAG, "???");
        //                    }
        //
        //                    @Override
        //                    public void onShowFailed() {
        //                        Log.i(TAG, "??");
        //                    }
        //
        //                    @Override
        //                    public void onSpotClosed() {
        //                        Log.i(TAG, "??");
        //                    }
        //
        //                    @Override
        //                    public void onSpotClick(boolean isWebPath) {
        //                        Log.i(TAG, "??isWebPath = " + isWebPath);
        //                    }
        //
        //                });
        ////            }
        ////        });
    }

    @Override
    public void onBackPressed() {
        // ????
        //        if (!SpotManager.getInstance(mContext).disMiss()) {
        //            super.onBackPressed();
        //        }
    }

    @Override
    protected void onStop() {
        // ?home
        //        SpotManager.getInstance(mContext).onStop();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        // ????
        //        SpotManager.getInstance(mContext).onDestroy();
        super.onDestroy();
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        if (mActionBarDrawerToggle != null) {
            mActionBarDrawerToggle.syncState();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (mActionBarDrawerToggle != null) {
            mActionBarDrawerToggle.onConfigurationChanged(newConfig);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_home, menu);
        mSearchItem = menu.findItem(R.id.action_search);//??item
        mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchItem);
        mSearchView.setQueryHint("?");
        mSearchItem.setVisible(false);
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
            @Override
            public boolean onQueryTextSubmit(String query) {
                Toast.makeText(HomeActivity.this, query, Toast.LENGTH_SHORT).show();
                Fragment item = mVpAdapter.getItem(mViewPager.getCurrentItem());
                if (item instanceof VideosContainerFragment) {
                    ((VideosContainerFragment) item).searsh(query);
                }
                mSearchItem.collapseActionView();
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mActionBarDrawerToggle != null && mActionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        Bundle extras = new Bundle();
        extras.putString(BaseWebActivity.BUNDLE_KEY_TITLE, item.getTitle().toString());
        extras.putBoolean(BaseWebActivity.BUNDLE_KEY_SHOW_BOTTOM_BAR, true);
        switch (item.getItemId()) {
        case R.id.action_capture:
            //                readyGo(CaptureActivity.class);
            //                if (ApiConstants.runing())
            readyGo(MainActivity.class);
            break;
        case R.id.action_about_us:
            //                readyGo(AboutUsActivity.class);
            extras.putString(BaseWebActivity.BUNDLE_KEY_URL, "http://jiyutang.kuaizhan.com/50/36/p40674244876b91");
            readyGo(BaseWebActivity.class, extras);
            break;
        case R.id.action_feedback:
            //                FeedbackAPI.openFeedbackActivity();
            //                Bundle extras = new Bundle();
            //                extras.putString(FeedbackFragment.BUNDLE_KEY_CONVERSATION_ID, mFeedbackAgent.getDefaultConversation().getId());
            //                readyGo(FeedBackActivity.class, extras);
            //                FeedbackAgent agent = new FeedbackAgent(this);
            //                agent.startFeedbackActivity();
            extras.putString(BaseWebActivity.BUNDLE_KEY_URL,
                    "http://jiyutang.kuaizhan.com/clubv2/forums/WJ8DG03NskvEQmEH");
            readyGo(BaseWebActivity.class, extras);
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
            if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
                mDrawerLayout.closeDrawer(Gravity.LEFT);
            } else {
                mDrawerLayout.openDrawer(Gravity.LEFT);
            }
            return true;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
                mDrawerLayout.closeDrawer(Gravity.LEFT);
            } else {
                if ((System.currentTimeMillis() - DOUBLE_CLICK_TIME) > 2000) {
                    showToast(getString(R.string.double_click_exit));
                    DOUBLE_CLICK_TIME = System.currentTimeMillis();
                } else {
                    getBaseApplication().exitApp();
                }
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

}