Java tutorial
/* * Copyright (C) 2015 Maros Cavojsky, (mpage.sk) * * 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 sk.mpage.androidsample.drawerwithauthenticator; import android.accounts.Account; import android.accounts.AccountManager; import android.accounts.AccountManagerCallback; import android.accounts.AccountManagerFuture; import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import sk.mpage.androidsample.drawerwithauthenticator.authentication.AccountGeneral; import sk.mpage.androidsample.drawerwithauthenticator.drawer.CustomAdapter; import sk.mpage.androidsample.drawerwithauthenticator.drawer.MenuItemObject; public class MainActivity extends AppCompatActivity { public static final String[] titles = { "Profile", "News", "Friends", "About" }; int[] titleIcons = { R.drawable.ic_account, R.drawable.ic_bullhorn, R.drawable.ic_account_multiple, R.drawable.ic_email }; public static final int PROFILE_FRAGMENT = 0; public static final int NEWS_FRAGMENT = 1; public static final int FRIENDS_FRAGMENT = 2; public static final int ABOUT_FRAGMENT = 3; private DrawerLayout mDrawerLayout; private ListView mDrawerList; private CharSequence mTitle; private CharSequence mDrawerTitle; private ActionBarDrawerToggle mDrawerToggle; private Toolbar topToolBar; private AccountManager mAccountManager; private String authToken = null; private Account mConnectedAccount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mAccountManager = AccountManager.get(this); getTokenForAccountCreateIfNeeded(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS); mTitle = mDrawerTitle = getTitle(); topToolBar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(topToolBar); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); LayoutInflater inflater = getLayoutInflater(); View listHeaderView = inflater.inflate(R.layout.header_list, null, false); mDrawerList.addHeaderView(listHeaderView); List<MenuItemObject> listViewItems = new ArrayList<MenuItemObject>(); for (int i = 1; i < titles.length; i++) { listViewItems.add(new MenuItemObject(titles[i], titleIcons[i])); } mDrawerList.setAdapter(new CustomAdapter(this, listViewItems)); mDrawerToggle = new ActionBarDrawerToggle(MainActivity.this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) { /** Called when a drawer has settled in a completely closed state. */ public void onDrawerClosed(View view) { super.onDrawerClosed(view); getSupportActionBar().setTitle(mTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } /** Called when a drawer has settled in a completely open state. */ public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); getSupportActionBar().setTitle(mDrawerTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; // Set the drawer toggle as the DrawerListener mDrawerLayout.setDrawerListener(mDrawerToggle); mDrawerToggle.setDrawerIndicatorEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // make Toast when click selectItemFragment(position); } }); if (savedInstanceState == null) { selectItemFragment(NEWS_FRAGMENT); } } private void selectItemFragment(int position) { Fragment fragment = null; Bundle args = new Bundle(); args.putInt("fragmentId", position); switch (position) { default: case NEWS_FRAGMENT: fragment = new GeneralFragment(); break; case FRIENDS_FRAGMENT: fragment = new GeneralFragment(); break; case PROFILE_FRAGMENT: fragment = new GeneralFragment(); break; case ABOUT_FRAGMENT: fragment = new GeneralFragment(); break; } fragment.setArguments(args); FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.main_fragment_container, fragment).commit(); mDrawerList.setItemChecked(position, true); setTitle(titles[position]); mDrawerLayout.closeDrawer(mDrawerList); } @Override public void setTitle(CharSequence title) { mTitle = title; getSupportActionBar().setTitle(mTitle); } @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); mDrawerToggle.onConfigurationChanged(newConfig); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. //getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { // If the nav drawer is open, hide action items related to the content view boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } private void setAppUIForUser() { TextView name = (TextView) findViewById(R.id.profile_name); TextView email = (TextView) findViewById(R.id.profile_email); name.setText("John Doe"); email.setText(mConnectedAccount.name); } private void getTokenForAccountCreateIfNeeded(String accountType, String authTokenType) { final AccountManagerFuture<Bundle> future = mAccountManager.getAuthTokenByFeatures(accountType, authTokenType, null, this, null, null, new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> future) { Bundle bnd = null; try { bnd = future.getResult(); authToken = bnd.getString(AccountManager.KEY_AUTHTOKEN); if (authToken != null) { String accountName = bnd.getString(AccountManager.KEY_ACCOUNT_NAME); mConnectedAccount = new Account(accountName, AccountGeneral.ACCOUNT_TYPE); //callback after connected setAppUIForUser(); } else { getTokenForAccountCreateIfNeeded(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS); } } catch (Exception e) { e.printStackTrace(); } } }, null); } }