Back to project page Joetz-Android-V2.
The source code is released under:
GNU General Public License
If you think the Android project Joetz-Android-V2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.jens.myapplication.main; // w ww . j a v a 2 s. c om import android.app.AlertDialog; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import com.example.jens.myapplication.R; import com.example.jens.myapplication.account.FragmentAccount; import com.example.jens.myapplication.account.LoginFragment; import com.example.jens.myapplication.account.ProfileFragment; import com.example.jens.myapplication.account.RegisterFragment; import com.example.jens.myapplication.activities.ActivityDetailActivity; import com.example.jens.myapplication.activities.FragmentListActivities; import com.example.jens.myapplication.apimanager.manager.LoginManager; import com.example.jens.myapplication.calendar.FragmentCalendar; import com.example.jens.myapplication.camps.CampDetailActivity; import com.example.jens.myapplication.camps.FragmentListCamps; import com.example.jens.myapplication.camps.InfoFragment; import com.example.jens.myapplication.domain.UserRole; import com.example.jens.myapplication.home.HomeFragment; import com.example.jens.myapplication.sam.JoetzApplication; import com.example.jens.myapplication.sam.MyFragment; import java.util.ArrayList; import java.util.List; public class MainActivity extends FragmentActivity implements FragmentListCamps.Callbacks,FragmentCalendar.Callbacks,HomeFragment.Callbacks, LoginFragment.OnFragmentInteractionListener, RegisterFragment.OnFragmentInteractionListener, ProfileFragment.OnFragmentInteractionListener, FragmentAccount.OnFragmentInteractionListener, FragmentListActivities.OnFragmentInteractionListener{ private static final String TAG_FRAGMENT = "fragmentTag"; public static final String EXTRA_ACCOUNT_START_PAGE = "accountStartPage"; /** * Use constant starting with "PAGE_" as value */ public static final String EXTRA_START_PAGE = "startPageExtra"; public static final int VALUE_PAGE_CAMPS = 0; public static final int VALUE_PAGE_FAVORITES = 1; public static final int VALUE_PAGE_CALENDAR = 2; public static final int VALUE_PAGE_ACCOUNT = 3; public static final int VALUE_PAGE_ACTIVITIES = 4; private static final int PAGE_HOME = 1; private static final int PAGE_CAMPS = 2; private static final int PAGE_FAVORITES = 3; private static final int PAGE_CALENDAR = 4; private static final int PAGE_ACTIVITIES = 5; private static final int PAGE_ACCOUNT = 6; //Not in navlist private static final int PAGE_REGISTER = 100; /* private int mPageCamps = -1; private int mPageFavorites = -1; private int mPageCalendar = -1; private int mPageAccount = -1; private int mPageActivities = -1;*/ private DrawerLayout mDrawerLayout; private ListView mDrawerList; private ActionBarDrawerToggle mDrawerToggle; private int selectedDrawerItemId = -1; //private MenuItem mMenuItemFilter; //private boolean mFilterVisible = false; private CharSequence mDrawerTitle; private CharSequence mTitle; private NavDrawerAdapter mNavAdapter; private int mAccountStartPage = 0; private NavDrawerItem[] mNavItems; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //refreshNavList(); LoginManager loginManager = JoetzApplication.getContext().getLoginManager(); mTitle = mDrawerTitle = getTitle(); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,GravityCompat.START); mNavAdapter = new NavDrawerAdapter(this, R.layout.navdrawer_item, new NavDrawerItem[0]); refreshNavList(); //mDrawerList.setAdapter(mNavAdapter); mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); // getActionBar().setDisplayHomeAsUpEnabled(true); // getActionBar().setHomeButtonEnabled(true); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) { public void onDrawerClosed(View view) { // getActionBar().setTitle(mTitle); setTitle(mTitle); invalidateOptionsMenu(); // creates call to // onPrepareOptionsMenu() } public void onDrawerOpened(View drawerView) { getActionBar().setTitle(mDrawerTitle); invalidateOptionsMenu(); // creates call to // onPrepareOptionsMenu() } }; mDrawerLayout.setDrawerListener(mDrawerToggle); //Choose which page to navigate to on startup if (savedInstanceState == null) { if(getIntent() != null && getIntent().getExtras() != null && getIntent().getExtras().containsKey(EXTRA_START_PAGE)){ if(getIntent().getExtras().containsKey(EXTRA_ACCOUNT_START_PAGE)){ mAccountStartPage = getIntent().getExtras().getInt(EXTRA_ACCOUNT_START_PAGE); } int startPage = getIntent().getExtras().getInt(EXTRA_START_PAGE); int s; switch(startPage){ case VALUE_PAGE_FAVORITES : s = PAGE_FAVORITES; break; case VALUE_PAGE_CALENDAR : s = PAGE_CALENDAR; break; case VALUE_PAGE_ACCOUNT : s = PAGE_ACCOUNT; break; case VALUE_PAGE_ACTIVITIES : s = PAGE_ACTIVITIES; break; case VALUE_PAGE_CAMPS : s = PAGE_CAMPS; break; default: s = PAGE_HOME; } if( s == -1 ){ s = PAGE_HOME; } selectItem(s); } else{ selectItem(PAGE_HOME); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the mNavItems; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); //mMenuItemFilter = mNavItems.findItem(R.id.action_filter); //mMenuItemFilter.setVisible(mFilterVisible); return true; } private void changeFragment(int id){ Fragment f = null; if(id == PAGE_REGISTER){ f = RegisterFragment.newInstance(); } if(f == null){ return; } f.setArguments(new Bundle()); FragmentManager frgManager = getSupportFragmentManager() ; frgManager.beginTransaction().replace(R.id.content_frame, f, TAG_FRAGMENT).commit(); } public void selectItem(int navItemId) { // ALT CODE // if(mMenuItemFilter != null){ // mMenuItemFilter.setVisible(false); // } if(navItemId == PAGE_FAVORITES && !JoetzApplication.getContext().getLoginManager().isLoggedIn()){ //Toast.makeText(this, R.string.need_logged_in_for_favos, Toast.LENGTH_SHORT).show(); new AlertDialog.Builder(this) .setMessage(R.string.need_logged_in_for_favos) .setNeutralButton(R.string.ok, null) .create().show(); navItemId = PAGE_CAMPS; } selectedDrawerItemId = navItemId; int positionIndex = mNavAdapter.findIndexOfItemId(navItemId); mNavAdapter.setSelectedItem(positionIndex); NavDrawerItem selectedItem = mNavItems[positionIndex]; Fragment fragment = null; //Bundle args = new Bundle(); int id = selectedItem.getId(); if(id== PAGE_HOME) fragment = new HomeFragment(); else if(id == PAGE_CAMPS){ fragment = new FragmentListCamps(); }else if(id == PAGE_CALENDAR){ fragment = new FragmentCalendar(); }else if(id == PAGE_ACCOUNT){ // CHECK IF LOGGED IN if(JoetzApplication.getContext().getLoginManager().isLoggedIn()){ fragment = FragmentAccount.newInstance(mAccountStartPage); mAccountStartPage = 0; }else{ fragment = new LoginFragment(); } }else if(id == PAGE_FAVORITES){ fragment = FragmentListCamps.newInstance(true); }else if(id == PAGE_ACTIVITIES){ fragment = FragmentListActivities.newInstance(); } /*switch (selectedItem.getId()) { case mPageCamps: fragment = new FragmentListCamps(); break; case 102: fragment = new FragmentCalendar(); break; case 103: // CHECK IF LOGGED IN if(JoetzApplication.getContext().getLoginManager().isLoggedIn()){ fragment = FragmentAccount.newInstance(mAccountStartPage); mAccountStartPage = 0; }else{ fragment = new LoginFragment(); } //fragment = new FragmentAccount(); break; case 104: fragment = FragmentListCamps.newInstance(true); break; }*/ if(fragment == null){ return; } //fragment.setArguments(args); FragmentManager frgManager = getSupportFragmentManager() ; frgManager.beginTransaction().replace(R.id.content_frame, fragment, TAG_FRAGMENT).commit(); mDrawerList.setItemChecked(positionIndex, true); setTitle(mNavItems[positionIndex].getLabel()); mDrawerLayout.closeDrawer(mDrawerList); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); } @Override public void setTitle(CharSequence title) { mTitle = title; getActionBar().setTitle(mTitle); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override protected void onResume() { super.onResume(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Pass any configuration change to the drawer toggles mDrawerToggle.onConfigurationChanged(newConfig); } @Override public boolean onOptionsItemSelected(MenuItem item) { // The action bar home/up action should open or close the drawer. // ActionBarDrawerToggle will take care of this. if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } return false; } @Override public void onLoginFinished() { refreshNavList(); selectItem(PAGE_ACCOUNT); } @Override public void onGoRegister() { changeFragment(PAGE_REGISTER); } @Override public void onLoginCancel() { throw new UnsupportedOperationException(); //not used } @Override public void onBackPressed() { // boolean consumed = false; if(mDrawerLayout.isDrawerOpen(GravityCompat.START)){ mDrawerLayout.closeDrawers(); // consumed = true; return; } // if(!consumed){ // consumed = ((MyFragment) getSupportFragmentManager() // .findFragmentByTag(TAG_FRAGMENT)).onBackPressed(); if(((MyFragment) getSupportFragmentManager() .findFragmentByTag(TAG_FRAGMENT)).onBackPressed()){ return; } // } // if(!consumed // && selectedDrawerItemId >= 0 // && mNavItems[selectedDrawerItemId].getId() != PAGE_HOME){ // selectItem(PAGE_HOME); // consumed=true; // } // int curId = mNavItems[selectedDrawerItemId].getId(); if(selectedDrawerItemId >= 0 && selectedDrawerItemId != PAGE_HOME){ selectItem(PAGE_HOME); return; } // if(!consumed) { super.onBackPressed(); // } } /*@Override public void onLogoutFinished() { refreshNavList(); selectItem(PAGE_ACCOUNT); }*/ @Override public void onCloseRegisterFragment() { selectItem(PAGE_ACCOUNT); } @Override public void onRegistrationCompleted() { selectItem(PAGE_ACCOUNT); } @Override public void onUserLogout() { refreshNavList(); selectItem(PAGE_ACCOUNT); } private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { int clickedId = mNavItems[position].getId(); selectItem(clickedId); } } private void refreshNavList(){ /*mNavItems = new NavDrawerItem[] { NavMenuSection.create(200, getString(R.string.app_name)), NavMenuItem.create(PAGE_CAMPS, getString(R.string.camps), R.drawable.navdrawer_kampen, R.drawable.navdrawer_kampen_selected, true, this), NavMenuItem.create(PAGE_FAVORITES, getString(R.string.favorite_camps), R.drawable.navdrawer_favo, R.drawable.navdrawer_favo_selected, true, this), NavMenuItem.create(PAGE_CALENDAR, getString(R.string.calendar), R.drawable.navdrawer_kalender, R.drawable.navdrawer_kalender_selected, true, this), NavMenuItem.create(PAGE_ACTIVITIES, getString(R.string.activities), R.drawable.navdrawer_activities, R.drawable.navdrawer_activities_selected, true, this), NavMenuItem.create(PAGE_ACCOUNT, getString(R.string.account), R.drawable.navdrawer_account, R.drawable.navdrawer_account_selected, true, this)};*/ //mNavAdapter.setItems(mNavItems); List<NavDrawerItem> items = new ArrayList<NavDrawerItem>(); items.add(NavMenuSection.create(200, getString(R.string.app_name))); items.add(NavMenuItem.create(PAGE_HOME, getString(R.string.home), R.drawable.navdrawer_home, R.drawable.navdrawer_home_selected, true, this)); items.add(NavMenuItem.create(PAGE_CAMPS, getString(R.string.camps), R.drawable.navdrawer_kampen, R.drawable.navdrawer_kampen_selected, true, this)); items.add(NavMenuItem.create(PAGE_FAVORITES, getString(R.string.favorite_camps), R.drawable.navdrawer_favo, R.drawable.navdrawer_favo_selected, true, this)); items.add(NavMenuItem.create(PAGE_CALENDAR, getString(R.string.calendar), R.drawable.navdrawer_kalender, R.drawable.navdrawer_kalender_selected, true, this)); LoginManager loginManager = JoetzApplication.getContext().getLoginManager(); if(loginManager.isLoggedIn() && loginManager.getUser() .getUserRole().value() >= UserRole.MONITOR.value()){ items.add(NavMenuItem.create(PAGE_ACTIVITIES, getString(R.string.activities), R.drawable.navdrawer_activities, R.drawable.navdrawer_activities_selected, true, this)); } items.add(NavMenuItem.create(PAGE_ACCOUNT, getString(R.string.account), R.drawable.navdrawer_account, R.drawable.navdrawer_account_selected, true, this)); mNavItems = items.toArray(new NavDrawerItem[items.size()]); mNavAdapter = new NavDrawerAdapter(this, R.layout.navdrawer_item, mNavItems); mDrawerList.setAdapter(mNavAdapter); } // DIENT VOOR DE MASTER DETAIL FLOW VAN CAMP public void onCampSelected(Long id){ Intent detailIntent = new Intent(this, CampDetailActivity.class); detailIntent.putExtra(InfoFragment.ARG_CAMP_ID, id); startActivity(detailIntent); } // DIENT VOOR DE MASTER DETAIL FLOW VAN CALENDAR public void onKalenderItemSelected(Long id, String soort){ if (soort.equals("CAMP")){ Intent detailIntent = new Intent(this, CampDetailActivity.class); detailIntent.putExtra(InfoFragment.ARG_CAMP_ID, id); startActivity(detailIntent); } else if(soort.equals("ACTIVITY")){ Intent detailIntent = new Intent(this, ActivityDetailActivity.class); detailIntent.putExtra(ActivityDetailActivity.ARG_ACTIVITY_ID, id); startActivity(detailIntent); } } // DIENT VOOR DE MASTER DETAIL FLOW VAN AANBEVOLEN public void onAanbevolenItemSelected(Long id){ Intent detailIntent = new Intent(this, CampDetailActivity.class); detailIntent.putExtra(InfoFragment.ARG_CAMP_ID, id); startActivity(detailIntent); } }