Back to project page Calma.
The source code is released under:
Apache License
If you think the Android project Calma listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2013 Thomas Schmid/* ww w. j a va 2 s. c o m*/ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ package com.scto.android.calma.activities; import com.scto.android.calma.R; import com.scto.android.calma.CalmaApp; import com.scto.android.calma.activities.SettingsActivity; import com.scto.android.calma.adapter.NavDrawerListAdapter; import com.scto.android.calma.fragments.CommunityFragment; import com.scto.android.calma.fragments.FindPeopleFragment; import com.scto.android.calma.fragments.HomeFragment; import com.scto.android.calma.fragments.PagesFragment; import com.scto.android.calma.fragments.PhotosFragment; import com.scto.android.calma.fragments.WhatsHotFragment; import com.scto.android.calma.model.NavDrawerItem; import com.scto.android.calma.utils.CalmaUtils; import com.scto.android.calma.utils.PreferenceUtils; import java.util.ArrayList; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.content.Intent; import android.content.res.Configuration; import android.content.res.TypedArray; import android.graphics.Point; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.widget.DrawerLayout; import android.util.Log; import android.view.GestureDetector; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.Menu; import android.view.MenuItem; import android.view.OrientationEventListener; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import com.espian.showcaseview.OnShowcaseEventListener; import com.espian.showcaseview.ShowcaseView; import com.espian.showcaseview.targets.ViewTarget; public class CalmaActivity extends BaseActivity implements OnShowcaseEventListener { private ShowcaseView mShowcaseView; private DrawerLayout mDrawerLayout; private ListView mDrawerList; private ActionBarDrawerToggle mDrawerToggle; private GestureDetector mGestureDetector; private CalmaApp mApp; /** * Keeps track of the back button being used */ private boolean mIsBackPressed = false; // nav drawer title private CharSequence mDrawerTitle; // used to store app title private CharSequence mTitle; // slide menu items private String[] navMenuTitles; private TypedArray navMenuIcons; private ArrayList<NavDrawerItem> navDrawerItems; private NavDrawerListAdapter adapter; private final static int SHOWCASE_INDEX_WELCOME_1 = 0; private final static int SHOWCASE_INDEX_WELCOME_2 = 1; private final static String KEY_SHOWCASE_WELCOME = "SHOWCASE_WELCOME"; private int mShowcaseIndex; @Override protected void onCreate(Bundle savedInstanceState) { mApp = (CalmaApp)getApplication(); super.onCreate(savedInstanceState); setTheme( new PreferenceUtils(this).getTheme()); //int theme = new PreferenceUtils(this).getTheme(); // Fade it in overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); setContentView(R.layout.activity_main); mTitle = mDrawerTitle = getTitle(); // load slide menu items navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); // nav drawer icons from resources navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); /* switch(theme) { case CalmaApp.THEME_WHITE: mDrawerLayout.setBackgroundResource(R.color.blue_transparent); break; case CalmaApp.THEME_BLACK: mDrawerLayout.setBackgroundResource(R.color.black_transparent); break; } */ mDrawerList = (ListView) findViewById(R.id.list_bookmarks); navDrawerItems = new ArrayList<NavDrawerItem>(); // adding nav drawer items to array // Home navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1))); // Find People navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1))); // Photos navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1))); // Communities, Will add a counter here navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22")); // Pages navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1))); // What's hot, We will add a counter here navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+")); // Recycle the typed array navMenuIcons.recycle(); mDrawerList.setOnItemClickListener(new BookmarksClickListener()); // setting the nav drawer list adapter adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems); mDrawerList.setAdapter(adapter); // enabling action bar app icon and behaving it as toggle button getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, //nav menu toggle icon R.string.app_name, // nav drawer open - description for accessibility R.string.app_name // nav drawer close - description for accessibility ) { public void onDrawerClosed(View view) { getActionBar().setTitle(mTitle); // calling onPrepareOptionsMenu() to show action bar icons invalidateOptionsMenu(); } public void onDrawerOpened(View drawerView) { getActionBar().setTitle(mDrawerTitle); // calling onPrepareOptionsMenu() to hide action bar icons invalidateOptionsMenu(); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); startShowcaseWelcome(); if (savedInstanceState == null) { // on first time display view for first nav item displayView(0); } } @Override protected void onResume() { super.onResume(); if (shouldRestartApp) { shouldRestartApp = false; restartApp(); } } /** * {@inheritDoc} */ @Override public void onBackPressed() { if (mIsBackPressed) { super.onBackPressed(); return; } this.mIsBackPressed = true; Toast.makeText(this, R.string.toast_on_back_pressed, Toast.LENGTH_SHORT).show(); new Handler().postDelayed(new Runnable() { @Override public void run() { mIsBackPressed = false; } }, 2000); } // @Override // public void onBackPressed() { // if (getPreferenceUtils().useBackNavigation()) { // if (Util.isRoot(currentDir)) { // finish(); // } else { // gotoParent(); // } // } else { // super.onBackPressed(); // } // } @Override public void onShowcaseViewHide(ShowcaseView showcaseView) { //The view is hidden/dismissed } @Override public void onShowcaseViewDidHide(ShowcaseView showcaseView) { //The view is hidden/dismissed } @Override public void onShowcaseViewShow(ShowcaseView showcaseView) { //The view is shown } public void startShowcaseWelcome() { if (new PreferenceUtils(this).getShowcaseWelcome().equals("0")) { new PreferenceUtils(this).setShowcaseWelcome("1"); ShowcaseView.ConfigOptions co = new ShowcaseView.ConfigOptions(); co.hideOnClickOutside = true; mShowcaseView = ShowcaseView.insertShowcaseView(mDrawerList, this, getString(R.string.showcase_welcome_1_title), getString(R.string.showcase_welcome_1_body), co); // Animate gesture Point size = new Point(); getWindowManager().getDefaultDisplay().getSize(size); mShowcaseView.animateGesture(size.x/2, size.y*2.0f/3.0f, size.x/2, size.y/2.0f); mShowcaseView.setOnShowcaseEventListener(this); mShowcaseIndex = SHOWCASE_INDEX_WELCOME_1; } } /** * Slide menu item click listener * */ private class BookmarksClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // display view for selected nav drawer item displayView(position); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // toggle nav drawer on selecting action bar app icon/title if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle action bar actions click switch (item.getItemId()) { case R.id.action_settings: Intent prefsIntent = new Intent(CalmaActivity.this,SettingsActivity.class); startActivity(prefsIntent); return true; default: return super.onOptionsItemSelected(item); } } /* * * Called when invalidateOptionsMenu() is triggered */ @Override public boolean onPrepareOptionsMenu(Menu menu) { // if nav drawer is opened, hide the action items boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); menu.findItem(R.id.action_settings).setVisible(!drawerOpen); return super.onPrepareOptionsMenu(menu); } /** * Diplaying fragment view for selected nav drawer list item * */ private void displayView(int position) { // update the main content by replacing fragments Fragment fragment = null; switch (position) { case 0: fragment = new HomeFragment(); break; case 1: fragment = new FindPeopleFragment(); break; case 2: fragment = new PhotosFragment(); break; case 3: fragment = new CommunityFragment(); break; case 4: fragment = new PagesFragment(); break; case 5: fragment = new WhatsHotFragment(); break; default: break; } if (fragment != null) { FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.frame_container, fragment).commit(); // update selected item and title, then close the drawer mDrawerList.setItemChecked(position, true); mDrawerList.setSelection(position); setTitle(navMenuTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); } else { // error in creating fragment Log.e("MainActivity", "Error in creating fragment"); } } @Override public void setTitle(CharSequence title) { mTitle = title; getActionBar().setTitle(mTitle); } /** * When using the ActionBarDrawerToggle, you must call it during * onPostCreate() and onConfigurationChanged()... */ @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Pass any configuration change to the drawer toggls mDrawerToggle.onConfigurationChanged(newConfig); } /** * Handles the swipe and tap gestures on the lower layer of the screen * (ie. the preview surface) * * @note Remember that the default orientation of the screen is landscape, thus * the side bar is at the BOTTOM of the screen, and is swiped UP/DOWN. */ public class GestureListener extends GestureDetector.SimpleOnGestureListener { private static final int SWIPE_MIN_DISTANCE = 10; private final float DRAG_MIN_DISTANCE = CalmaUtils.dpToPx(CalmaActivity.this, 5.0f); private static final int SWIPE_MAX_OFF_PATH = 80; private static final int SWIPE_THRESHOLD_VELOCITY = 800; // Allow to drag the side bar up to half of the screen private static final int SIDEBAR_THRESHOLD_FACTOR = 2; private boolean mCancelSwipe = false; @Override public boolean onSingleTapConfirmed(MotionEvent e) { return super.onSingleTapConfirmed(e); } @Override public boolean onDoubleTap(MotionEvent e) { return super.onDoubleTap(e); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { if (e1 == null || e2 == null) { return false; } return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { try { if (Math.abs(e1.getY() - e2.getY()) < SWIPE_MAX_OFF_PATH) { // swipes to open/close the sidebar and/or hide/restore the widgets if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { if (!mDrawerLayout.isDrawerOpen(mDrawerList)) { mDrawerLayout.openDrawer(mDrawerList); } } else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { if (!mDrawerLayout.isDrawerOpen(mDrawerList)) { mDrawerLayout.openDrawer(mDrawerList); } } } } catch (Exception e) { // Do nothing here } return true; } } private void restartApp() { Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName()); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); } }