gxu.software_engineering.market.android.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for gxu.software_engineering.market.android.MainActivity.java

Source

/*
 * Copyright 2013 Department of Computer Science and Technology, Guangxi University
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package gxu.software_engineering.market.android;

import gxu.software_engineering.market.android.activity.UserServiceActivity;
import gxu.software_engineering.market.android.service.SyncService;
import gxu.software_engineering.market.android.ui.CategoriesFragment;
import gxu.software_engineering.market.android.ui.LoginBoxFragment;
import gxu.software_engineering.market.android.ui.PagerAdapter;
import gxu.software_engineering.market.android.ui.RegisterBoxFragment;
import gxu.software_engineering.market.android.util.C;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.format.DateUtils;
import android.widget.Toast;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

/**
 * ?
 * 
 * @author longkai()
 * @email  im.longkai@gmail.com
 * @since  2013-06-22
 */
public class MainActivity extends SlidingFragmentActivity {

    private MarketApp app;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBehindContentView(cn.longkai.android.R.layout.fragment_container);

        FragmentManager fm = getSupportFragmentManager();
        if (fm.findFragmentByTag("menu") == null) {
            Fragment fragment = new CategoriesFragment();
            FragmentTransaction tx = fm.beginTransaction();
            tx.replace(cn.longkai.android.R.id.fragment_container, fragment, "menu");
            tx.commit();
        }

        SlidingMenu sm = getSlidingMenu();
        sm.setShadowWidth(15);
        sm.setBehindOffset(120);
        sm.setFadeDegree(0.35f);
        sm.setShadowDrawable(R.drawable.shadow);
        sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        setContentView(R.layout.activity_main);

        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new PagerAdapter(fm));

        PagerTabStrip pagerTabStrip = (PagerTabStrip) pager.findViewById(R.id.pager_tab_strip);
        pagerTabStrip.setTabIndicatorColor(getResources().getColor(R.color.info));

        pager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                switch (arg0) {
                case 0:
                    getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                    break;

                default:
                    getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
                    break;
                }
            }

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

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        pager.setCurrentItem(1);
        getSupportActionBar().setTitle(R.string.app_name);
        getSupportActionBar().setSubtitle(R.string.hello_world);

        app = MarketApp.marketApp();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            toggle();
            break;
        case R.id.login:
            if (app.hasLogedIn()) {
                Intent intent = new Intent(this, UserServiceActivity.class);
                startActivity(intent);
            } else {
                LoginBoxFragment fragment = new LoginBoxFragment();
                fragment.show(getSupportFragmentManager(), "login");
            }
            break;
        case R.id.register:
            RegisterBoxFragment register = new RegisterBoxFragment();
            register.show(getSupportFragmentManager(), "register");
            break;
        case R.id.sync:
            long last = app.getPrefs().getLong(C.LAST_SYNC, 0);
            Toast.makeText(this, getString(R.string.syncing, DateUtils.getRelativeTimeSpanString(last)),
                    Toast.LENGTH_SHORT).show();
            Intent sync = new Intent(this, SyncService.class);
            startService(sync);
            break;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

}