Android Open Source - capture-the-flag Game Menu Fragment






From Project

Back to project page capture-the-flag.

License

The source code is released under:

Copyright ? 2014 Microsoft Mobile Oy. All rights reserved. Microsoft is a registered trademark of Microsoft Corporation. Nokia and HERE are trademarks and/or registered trademarks of Nokia Corporati...

If you think the Android project capture-the-flag listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2014 Microsoft Mobile. All rights reserved.
 * See the license text file provided with this project for more information.
 *///from ww w .ja va  2 s  .c  o  m

package com.nokia.example.capturetheflag;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nokia.example.capturetheflag.location.LocationManagerInterface.ReverseGeocodingResultListener;
import com.nokia.example.capturetheflag.network.GameListRequest;
import com.nokia.example.capturetheflag.network.model.Game;
import com.nokia.example.capturetheflag.network.model.ModelConstants;

/**
 * Shows the game menu where user can select to create a new game or join an
 * existing game.
 */
public class GameMenuFragment
        extends Fragment
        implements View.OnClickListener, ReverseGeocodingResultListener
{
    public static final String FRAGMENT_TAG = "GameMenuFragment";
    private static final String TAG = "CtF/GameMenuFragment";

    private Game[] mGames;
    private Button mNewGameButton;
    private TextView mUserLocation;
    private ProgressBar mProgressBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Always try to use online mode when showing game menu
        Controller.getInstance().switchOnlineMode(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.game_menu_fragment, container, false);

        v.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

        mUserLocation = (TextView) v.findViewById(R.id.user_address);
        mNewGameButton = (Button) v.findViewById(R.id.create_new_game);
        mNewGameButton.setOnClickListener(this);
        mProgressBar = (ProgressBar) v.findViewById(R.id.menu_loading_indicator);
        return v;
    }

    @Override
    public void onStart() {
        super.onStart();
        Controller.getInstance().getNetworkClient().emit(new GameListRequest());
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onClick(View view) {
        FragmentManager manager = getFragmentManager();
        if (!Controller.getInstance().isLocationFound()) {
            /* Location is required for starting a game, but the user has not
             * been located yet.
             */
            AlertDialog.Builder noLocationDialogBuilder = new AlertDialog.Builder(getActivity());
            noLocationDialogBuilder.setTitle(getString(R.string.no_location));
            noLocationDialogBuilder.setMessage(getString(R.string.location_required_to_start));

            noLocationDialogBuilder.setPositiveButton(getString(android.R.string.ok), new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            noLocationDialogBuilder.create().show();
            return;
        }
        if (view == mNewGameButton) {
            FragmentTransaction transaction = manager.beginTransaction();
            CreateGameFragment createGameFragment = new CreateGameFragment();
            Controller controller =
                    (Controller) getFragmentManager()
                            .findFragmentByTag(Controller.FRAGMENT_TAG);
            Bundle args = new Bundle();
            args.putBoolean(ModelConstants.IS_PREMIUM_KEY, controller.isPremium());
            createGameFragment.setArguments(args);
            transaction.remove(this);
            transaction.add(R.id.top_fragments,
                    createGameFragment, CreateGameFragment.FRAGMENT_TAG);
            transaction.commit();
        } else {
            Game game = (Game) view.getTag();
            FragmentTransaction transaction = manager.beginTransaction();
            JoinGameFragment join = new JoinGameFragment();
            join.setGame(game);
            transaction.remove(this);
            transaction.add(R.id.top_fragments, join, JoinGameFragment.FRAGMENT_TAG);
            transaction.commit();
        }
    }

    /**
     * Populates the view with "join game" buttons.
     *
     * @param games
     */
    public void setGames(Game[] games) {
        Log.d(TAG, "setGames()");
        mGames = games;
        LinearLayout layout = (LinearLayout) getView().findViewById(R.id.open_game_area);
        layout.removeAllViews();
        setProgressBarVisibility(false);

        for (Game game : mGames) {
            Button button = (Button) getActivity().getLayoutInflater().inflate(R.layout.button, layout, false);
            button.setTag(game);
            button.setText("Join \"" + game.getName() + "\"");
            button.setOnClickListener(this);
            layout.addView(button);
        }
    }

    /**
     * Helper method to toggle the progress bar visibility.
     * 
     * @param visible If true will show the progress bar, if false will hide it.
     */
    public void setProgressBarVisibility(boolean visible) {
        if (isVisible()) {
            int visibility = (visible ? View.VISIBLE : View.GONE);
            
            if (mProgressBar.getVisibility() != visibility) {
                mProgressBar.setVisibility(visibility);
            }
        }
    }

    /**
     * Shows the address, found with reverse geocoding, to the user.
     */
    @Override
    public void onReverseGeocodingResult(String result) {
        if (getActivity() != null) {
            if (result == null) {
                mUserLocation.setText("Unable to find location");
            } else {
                if (result.isEmpty()) {
                    mUserLocation.setText(getString(R.string.no_street_address));
                } else {
                    mUserLocation.setText(getString(R.string.you_are_at) + " " + result);
                }
            }
        }
    }
}




Java Source Code List

com.nokia.example.capturetheflag.AboutActivity.java
com.nokia.example.capturetheflag.Controller.java
com.nokia.example.capturetheflag.CreateGameFragment.java
com.nokia.example.capturetheflag.GameEndedDialogFragment.java
com.nokia.example.capturetheflag.GameMenuFragment.java
com.nokia.example.capturetheflag.HelpActivity.java
com.nokia.example.capturetheflag.JoinGameFragment.java
com.nokia.example.capturetheflag.MainActivity.java
com.nokia.example.capturetheflag.PauseDialog.java
com.nokia.example.capturetheflag.PurchasePremiumFragment.java
com.nokia.example.capturetheflag.ServerSettingsDialog.java
com.nokia.example.capturetheflag.Settings.java
com.nokia.example.capturetheflag.location.LocationManagerBase.java
com.nokia.example.capturetheflag.location.LocationManagerFactory.java
com.nokia.example.capturetheflag.location.LocationManagerInterface.java
com.nokia.example.capturetheflag.location.LocationManagerListener.java
com.nokia.example.capturetheflag.location.LocationUtils.java
com.nokia.example.capturetheflag.location.google.LocationManagerGoogle.java
com.nokia.example.capturetheflag.location.here.LocationManagerHere.java
com.nokia.example.capturetheflag.map.GameMapFactory.java
com.nokia.example.capturetheflag.map.GameMapInterface.java
com.nokia.example.capturetheflag.map.GameMapUtils.java
com.nokia.example.capturetheflag.map.MarkerFactoryBase.java
com.nokia.example.capturetheflag.map.google.GameMapGoogle.java
com.nokia.example.capturetheflag.map.google.MarkerFactoryGoogle.java
com.nokia.example.capturetheflag.map.here.GameMapHere.java
com.nokia.example.capturetheflag.map.here.MarkerFactoryHere.java
com.nokia.example.capturetheflag.network.FlagCapturedResponse.java
com.nokia.example.capturetheflag.network.GameListRequest.java
com.nokia.example.capturetheflag.network.GameListResponse.java
com.nokia.example.capturetheflag.network.JSONRequest.java
com.nokia.example.capturetheflag.network.JSONResponse.java
com.nokia.example.capturetheflag.network.JoinRequest.java
com.nokia.example.capturetheflag.network.JoinedResponse.java
com.nokia.example.capturetheflag.network.NetworkClient.java
com.nokia.example.capturetheflag.network.OfflineClient.java
com.nokia.example.capturetheflag.network.SocketIONetworkClient.java
com.nokia.example.capturetheflag.network.UpdatePlayerRequest.java
com.nokia.example.capturetheflag.network.UpdatePlayerResponse.java
com.nokia.example.capturetheflag.network.model.Flag.java
com.nokia.example.capturetheflag.network.model.Game.java
com.nokia.example.capturetheflag.network.model.ModelConstants.java
com.nokia.example.capturetheflag.network.model.Player.java
com.nokia.example.capturetheflag.notifications.NotificationsManagerBase.java
com.nokia.example.capturetheflag.notifications.NotificationsManagerFactory.java
com.nokia.example.capturetheflag.notifications.NotificationsManagerInterface.java
com.nokia.example.capturetheflag.notifications.NotificationsUtils.java
com.nokia.example.capturetheflag.notifications.google.GcmBroadcastReceiver.java
com.nokia.example.capturetheflag.notifications.google.GcmIntentService.java
com.nokia.example.capturetheflag.notifications.google.NotificationsManagerGoogle.java
com.nokia.example.capturetheflag.notifications.nokia.NokiaNotificationsBroadcastReceiver.java
com.nokia.example.capturetheflag.notifications.nokia.NokiaNotificationsIntentService.java
com.nokia.example.capturetheflag.notifications.nokia.NotificationsManagerNokia.java