Java tutorial
/* * Copyright 2013 The Android Open Source Project * * 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.casin.activity; import me.crossle.android.distribution.MainActivity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Fragment; import android.app.FragmentManager; import android.content.Intent; import android.content.res.Configuration; import android.graphics.Typeface; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.AdapterView; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.casin.activity.adapter.HomeListAdapter; import com.casin.activity.fragments.ChangePasswordFragment; import com.casin.activity.fragments.CreateUserFragment; import com.casin.activity.fragments.HomeFragment; import com.casin.activity.fragments.MyWinningsFragment; import com.casin.activity.fragments.ProfileFragment; import com.casin.activity.fragments.TransferPointsFragment; import com.casin.json.JSONParser; import com.casin.network.NetworkCallback; import com.casino.bean.GameBean; import com.casino.utils.BetUtility; import com.naseeb.R; public class HomeDrawerActivity extends BaseActivity implements NetworkCallback { private DrawerLayout mDrawerLayout; private ListView mDrawerList; private ActionBarDrawerToggle mDrawerToggle; private String name; Handler responseHandler = new Handler(); private String role; private String[] arrayAdminData = { "Profile", "Change Password", "Create Broker", "Transfer Points", "Signout" }; private String[] arrayBrokerData = { "Profile", "Change Password", "Create User", "Transfer Points", "Signout" }; static String[] arrayUserData = { "Select Game", "Profile", "Change Password", "Signout" }; private int[] brokerDrawableIcons = { R.drawable.icon_profile_big, R.drawable.icon_changepass, R.drawable.icon_createuser, R.drawable.icon_transfer, R.drawable.icon_signout }; private int[] userDrawableIcons = { R.drawable.icon_selectgame, R.drawable.icon_profile_big, R.drawable.icon_changepass, R.drawable.icon_signout }; private int loginCount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.homedrawer); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.header_title); name = getIntent().getStringExtra("name"); role = getIntent().getStringExtra("role"); loginCount = getIntent().getIntExtra("loginCount", -1); // mPlanetTitles = getResources().getStringArray(R.array.planets_array); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); // set a custom shadow that overlays the main content when the drawer opens mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); String[] arrayPerRole; int[] drawablePerRole; if (role.equalsIgnoreCase("broker")) { arrayPerRole = arrayBrokerData; drawablePerRole = brokerDrawableIcons; } else if (role.equalsIgnoreCase("admin")) { arrayPerRole = arrayAdminData; drawablePerRole = brokerDrawableIcons; } else { // if(userRole.equalsIgnoreCase("user")) arrayPerRole = arrayUserData; drawablePerRole = userDrawableIcons; } // set up the drawer's list view with items and click listener mDrawerList.setAdapter( new HomeListAdapter(this, R.layout.drawer_list_item, role, arrayPerRole, drawablePerRole)); mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); // enable ActionBar app icon to behave as action to toggle nav drawer // getActionBar().setHomeButtonEnabled(true); /* getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); getActionBar().setCustomView(R.layout.header_title); */ // getActionBar().setHomeAsUpIndicator(R.drawable.nlogo4); // ActionBarDrawerToggle ties together the the proper interactions // between the sliding drawer and the action bar app icon mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */ mDrawerLayout, /* DrawerLayout object */ R.drawable.game_btn_coin, /* nav drawer image to replace 'Up' caret */ R.string.drawer_open, /* "open drawer" description for accessibility */ R.string.drawer_close /* "close drawer" description for accessibility */ ) { public void onDrawerClosed(View view) { // getActionBar().setTitle(mTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } public void onDrawerOpened(View drawerView) { /* getActionBar().setTitle(mDrawerTitle); */ // invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; mDrawerLayout.setDrawerListener(mDrawerToggle); if (savedInstanceState == null) { if (role.equalsIgnoreCase("user")) { selectItem(0, "Select Game"); } else { selectItem(0, "Profile"); } } //check if first login if (loginCount < 1) { selectItem(1, "Change Password"); alert("Attention", "Please change your password first.", null, null); } //initHomeViews(); initHeaderLayout(name, String.valueOf(BetUtility.getInstance().getBalanceAmount())); } /* The click listner for ListView in the navigation drawer */ private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { selectItem(position, view.getTag().toString()); } } private void selectItem(int position, String tag) { String[] arrayAllData = { "Select Game", "Profile", "Change Password", "My Winnings", "Create User", "Transfer Points", "Settings", "Signout", "Create Broker" }; Bundle args = new Bundle(); // update the main content by replacing fragments Fragment fragment = new ProfileFragment(); if (tag.equalsIgnoreCase(arrayAllData[0])) { fragment = new HomeFragment(); } else if (tag.equalsIgnoreCase(arrayAllData[1])) { fragment = new ProfileFragment(); } else if (tag.equalsIgnoreCase(arrayAllData[2])) { fragment = new ChangePasswordFragment(); } else if (tag.equalsIgnoreCase(arrayAllData[3])) { fragment = new MyWinningsFragment(); } else if (tag.equalsIgnoreCase(arrayAllData[4])) { args.putBoolean("isAdmin", false); fragment = new CreateUserFragment(); } else if (tag.equalsIgnoreCase(arrayAllData[5])) { fragment = new TransferPointsFragment(); } else if (tag.equalsIgnoreCase(arrayAllData[6])) { //settings fragment = new ProfileFragment(); } else if (tag.equalsIgnoreCase(arrayAllData[7])) { //signout signOut(true, null); } else if (tag.equalsIgnoreCase(arrayAllData[8])) { fragment = new CreateUserFragment(); args.putBoolean("isAdmin", true); } fragment.setArguments(args); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); // update selected item and title, then close the drawer mDrawerList.setItemChecked(position, true); mDrawerLayout.closeDrawer(mDrawerList); } /** * 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); } public void initHeaderLayout(String name, String balancePoints) { Typeface snapFont = Typeface.createFromAsset(getAssets(), "SNAP.TTF"); TextView headrTitleTextView = (TextView) findViewById(R.id.headerTitle); headrTitleTextView.setTypeface(snapFont); headrTitleTextView.setText(name); TextView scoreTitleTextView = (TextView) findViewById(R.id.score); scoreTitleTextView.setText(balancePoints); ImageView leftMenuBarImageView = (ImageView) findViewById(R.id.leftMenuBar); leftMenuBarImageView.setVisibility(View.VISIBLE); leftMenuBarImageView.setOnClickListener(leftMenuBarClickListener); } @Override public void responseReceived(final String response) { final GameBean currentgameBean = new JSONParser().parseCurrentGameData(response); responseHandler.post(new Runnable() { @Override public void run() { if (currentgameBean != null) { Intent homeIntent = new Intent(HomeDrawerActivity.this, MainActivity.class); homeIntent.putExtra("name", name); homeIntent.putExtra("timeElapsed", (currentgameBean.getCurrentTime() - currentgameBean.getStartTime())); homeIntent.putExtra("gameId", currentgameBean.getGameId()); homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(homeIntent); HomeDrawerActivity.this.finish(); } else { //show alert dialog AlertDialog.Builder builder = new Builder(HomeDrawerActivity.this); builder.setTitle("Error"); builder.setMessage("Could not connect to server. Retry"); builder.setPositiveButton("Ok", null); builder.show(); } } }); } @Override public void onResume() { super.onResume(); mDrawerLayout.openDrawer(mDrawerList); initHeaderLayout(name, String.valueOf(BetUtility.getInstance().getBalanceAmount())); } OnClickListener leftMenuBarClickListener = new OnClickListener() { @Override public void onClick(View arg0) { boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); if (drawerOpen) { mDrawerLayout.closeDrawer(mDrawerList); } else { mDrawerLayout.openDrawer(mDrawerList); } } }; public String getName() { return name; } public String getBalancePoints() { return String.valueOf(BetUtility.getInstance().getBalanceAmount()); } public void setBalancePoints(String balance) { BetUtility.getInstance().setBalanceAmount(Integer.valueOf(balance)); } public void alert(String title, String msg, String negativeButtonText, android.content.DialogInterface.OnClickListener okClickListener) { AlertDialog.Builder builder = new Builder(HomeDrawerActivity.this); builder.setTitle(title); builder.setMessage(msg); builder.setPositiveButton("Ok", okClickListener); if (negativeButtonText != null) { builder.setNegativeButton(negativeButtonText, null); } builder.show(); } }