com.kaku.weac.activities.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.kaku.weac.activities.MainActivity.java

Source

/*
 * Copyright (c) 2016  <kaku201313@163.com | 3772304@qq.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.kaku.weac.activities;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.kaku.weac.R;
import com.kaku.weac.fragment.AlarmClockFragment;
import com.kaku.weac.fragment.MoreFragment;
import com.kaku.weac.fragment.TimeFragment;
import com.kaku.weac.fragment.WeaFragment;
import com.kaku.weac.service.DaemonService;
import com.kaku.weac.util.LogUtil;
import com.kaku.weac.util.MyUtil;
import com.kaku.weac.util.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.umeng.fb.FeedbackAgent;
import com.umeng.update.UmengUpdateAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * Activity
 *
 * @author 
 * @version 1.0 2015/04/12
 */
public class MainActivity extends BaseActivity implements OnClickListener {

    /**
     * Log tag MainActivity
     */
    private static final String LOG_TAG = "MainActivity";

    /**
     * Tab
     */
    private TextView tv_alarm_clock;

    /**
     * Tab
     */
    private TextView tv_wea;

    /**
     * Tab
     */
    private TextView tv_time;

    /**
     * Tab
     */
    private TextView tv_more;

    /**
     * Fragment?
     */
    private FragmentManager mFm;

    /**
     * Tab
     */
    private int mUnSelectColor;

    /**
     * Tab
     */
    private int mSelectColor;

    /**
     * ??
     */
    private ViewPager mViewPager;

    /**
     * Tab??
     */
    private List<Fragment> mFragmentList;

    /**
     * ?TabIndex
     */
    private int mCurrentIndex = -1;

    /**
     * Fragment
     */
    private WeaFragment mWeaFragment;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ??
        configureUmeng();
        // ??
        setSwipeBackEnable(false);
        startService(new Intent(this, DaemonService.class));
        setContentView(R.layout.activity_main);
        // ?
        setThemeWallpaper();

        mFm = getSupportFragmentManager();
        // Tab
        mSelectColor = getResources().getColor(R.color.white);
        // Tab
        mUnSelectColor = getResources().getColor(R.color.white_trans50);
        // ?
        initViews();
        // ???Tab
        setTabSelection(0);
    }

    /**
     * ??
     */
    private void configureUmeng() {
        // ???
        MobclickAgent.setDebugMode(true);
        // ??????
        // ??????????
        UmengUpdateAgent.setDefault();
        UmengUpdateAgent.setUpdateOnlyWifi(false);
        UmengUpdateAgent.update(this);

        // ???????
        new FeedbackAgent(this).sync();
    }

    /**
     * ?
     */
    private void setThemeWallpaper() {
        ViewGroup vg = (ViewGroup) findViewById(R.id.llyt_activity_main);
        MyUtil.setBackground(vg, this);
    }

    // XXX
    // @Override
    // public void onBackPressed() {
    // // super.onBackPressed();
    // // Intent localIntent = new Intent("android.intent.action.MAIN");
    // // localIntent.addCategory("android.intent.category.HOME");
    // // localIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    // // MainActivity.this.startActivity(localIntent);
    // // System.exit(0);
    // Process.killProcess(Process.myPid());
    // }

    /**
     * ?
     */
    private void initViews() {
        // ?Tab
        // Tab?
        ViewGroup tab_alarm_clock = (ViewGroup) findViewById(R.id.tab_alarm_clock);
        // Tab?
        ViewGroup tab_wea = (ViewGroup) findViewById(R.id.tab_wea);
        // Tab?
        ViewGroup tab_time = (ViewGroup) findViewById(R.id.tab_time);
        // Tab?
        ViewGroup tab_more = (ViewGroup) findViewById(R.id.tab_more);

        // ?Tab
        tv_alarm_clock = (TextView) findViewById(R.id.tv_alarm_clock);
        tv_wea = (TextView) findViewById(R.id.tv_wea);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_more = (TextView) findViewById(R.id.tv_more);

        // Tab
        tab_alarm_clock.setOnClickListener(this);
        tab_wea.setOnClickListener(this);
        tab_time.setOnClickListener(this);
        tab_more.setOnClickListener(this);

        // Tab??
        mFragmentList = new ArrayList<>();
        // Fragment
        AlarmClockFragment mAlarmClockFragment = new AlarmClockFragment();
        // Fragment
        mWeaFragment = new WeaFragment();
        // Fragment
        TimeFragment mTimeFragment = new TimeFragment();
        // Fragment
        MoreFragment mMoreFragment = new MoreFragment();
        mFragmentList.add(mAlarmClockFragment);
        mFragmentList.add(mWeaFragment);
        mFragmentList.add(mTimeFragment);
        mFragmentList.add(mMoreFragment);

        // ViewPager
        mViewPager = (ViewPager) findViewById(R.id.fragment_container);
        mViewPager.setAdapter(new MyFragmentPagerAdapter(mFm));
        // page
        mViewPager.setOffscreenPageLimit(3);
        // TODO???
        mViewPager.addOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int index) {
                setTabSelection(index);

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

    }

    /**
     * ViewPager?
     */
    class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

    }

    @Override
    public void onClick(View v) {
        // Tab
        switch (v.getId()) {
        // Tab
        case R.id.tab_alarm_clock:
            // ?
            setTabSelection(0);
            break;
        // Tab
        case R.id.tab_wea:
            // ?
            setTabSelection(1);
            break;
        // Tab
        case R.id.tab_time:
            // ?
            setTabSelection(2);
            break;
        // Tab
        case R.id.tab_more:
            // ?
            setTabSelection(3);
            break;
        default:
            break;
        }
    }

    /**
     * Tab
     *
     * @param index ?tab0123
     */
    private void setTabSelection(int index) {
        // ???Tab??
        if (mCurrentIndex == index) {
            return;
        }

        if (index != 1) {
            // ???
            if (mWeaFragment.mHandler != null && mWeaFragment.mIsPostDelayed) {
                // ?
                mWeaFragment.mHandler.removeCallbacks(mWeaFragment.mRun);
                mWeaFragment.mIsPostDelayed = false;
                LogUtil.i(LOG_TAG, "");
            }
            if (mWeaFragment.mPullRefreshScrollView != null) {
                // 
                if (mWeaFragment.mPullRefreshScrollView.isRefreshing()) {
                    // ?
                    mWeaFragment.mPullRefreshScrollView.onRefreshComplete();
                    LogUtil.i(LOG_TAG, "?");
                }
            }
            // ?
            if (mWeaFragment.mRefreshBtn != null) {
                mWeaFragment.mRefreshBtn.clearAnimation();
            }
        }

        // ?TabIndexIndex
        mCurrentIndex = index;
        // ?ViewPager
        mViewPager.setCurrentItem(index, false);
        // ?
        clearSelection();
        // Index
        switch (index) {
        // 
        case 0:
            // ?
            setTextView(R.drawable.ic_alarm_clock_select, tv_alarm_clock, mSelectColor);
            break;
        // 
        case 1:
            // ?
            setTextView(R.drawable.ic_weather_select, tv_wea, mSelectColor);
            break;
        // 
        case 2:
            // ?
            setTextView(R.drawable.ic_time_select, tv_time, mSelectColor);
            break;
        // 
        case 3:
            // ?
            setTextView(R.drawable.ic_more_select, tv_more, mSelectColor);
            break;
        }

    }

    /**
     * ?
     */
    private void clearSelection() {
        // Tab?
        setTextView(R.drawable.ic_alarm_clock_unselect, tv_alarm_clock, mUnSelectColor);
        // Tab?
        setTextView(R.drawable.ic_weather_unselect, tv_wea, mUnSelectColor);
        // Tab?
        setTextView(R.drawable.ic_time_unselect, tv_time, mUnSelectColor);
        // Tab?
        setTextView(R.drawable.ic_more_unselect, tv_more, mUnSelectColor);
    }

    /**
     * Tab
     *
     * @param iconId   Tab
     * @param textView Tab
     * @param color    Tab
     */
    private void setTextView(int iconId, TextView textView, int color) {
        @SuppressWarnings("deprecation")
        Drawable drawable = getResources().getDrawable(iconId);
        if (drawable != null) {
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            // 
            textView.setCompoundDrawables(null, drawable, null, null);
        }
        // 
        textView.setTextColor(color);
    }

    @Override
    protected void onDestroy() {
        LogUtil.d(LOG_TAG, "onDestroy()");
        //        Process.killProcess(Process.myPid());
        super.onDestroy();
    }

    private long mExitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                ToastUtil.showShortToast(this, getString(R.string.press_again_exit));
                mExitTime = System.currentTimeMillis();
            } else {
                MobclickAgent.onKillProcess(this);
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}