Android Open Source - capture-the-flag Join Game 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 w w w  .  j a va  2  s . c  o  m*/

package com.nokia.example.capturetheflag;

import com.nokia.example.capturetheflag.location.LocationManagerFactory;
import com.nokia.example.capturetheflag.location.LocationManagerInterface;
import com.nokia.example.capturetheflag.network.JoinRequest;
import com.nokia.example.capturetheflag.network.model.Game;
import com.nokia.example.capturetheflag.network.model.Player;
import com.nokia.example.capturetheflag.notifications.NotificationsManagerFactory;
import com.nokia.example.capturetheflag.notifications.NotificationsManagerInterface;
import com.nokia.example.capturetheflag.notifications.NotificationsManagerInterface.NotificationServiceType;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.TextView;

/**
 * Shows the join game UI.
 */
public class JoinGameFragment
        extends Fragment
        implements MainActivity.BackCallback {
    public static final String FRAGMENT_TAG = "JoinGameFragment";
    private Game mGame;
    private ProgressBar mProgressBar;
    private EditText mPlayerName;

    public JoinGameFragment() {
        super();
        mGame = new Game(Game.NEW_GAME);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).setBackCallback(this);
    }

    @Override
    public void onDetach() {
        ((MainActivity) getActivity()).removeBackCallback();
        super.onDetach();
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.join_game_fragment, container, false);
        TextView gameName = (TextView) root.findViewById(R.id.game_name);
        gameName.setText(mGame.getName());
        mPlayerName = (EditText) root.findViewById(R.id.editText1);
        mPlayerName.setText(Settings.getUsername(getActivity()));
        mProgressBar = (ProgressBar) root.findViewById(R.id.join_loading_indicator);
        final Button joinGameButton = (Button) root.findViewById(R.id.join_game_button);

        joinGameButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (validate()) {
                    joinGameButton.setVisibility(View.GONE);
                    mProgressBar.setVisibility(View.VISIBLE);
                    Settings.setUsername(mPlayerName.getText().toString().trim(), getActivity());
                    joinGame();
                    FragmentManager manager = getActivity().getFragmentManager();
                    FragmentTransaction transaction = manager.beginTransaction();
                    transaction.remove(JoinGameFragment.this);
                    transaction.commit();
                }
            }
        });

        return root;
    }

    @Override
    public void onBackPressed() {
        MainActivity mainActivity = (MainActivity) getActivity();

        if (mainActivity != null) {
            mainActivity.showGameMenu(this);
        }
    }

    public void setGame(Game g) {
        mGame = g;
    }

    private boolean validate() {
        if (mPlayerName.getText().toString().trim().length() > 0) {
            mPlayerName.setError(null); // Clear error if present
            return true;
        }

        mPlayerName.setError(getString(R.string.invalid_name));
        return false;
    }

    private void joinGame() {
        // Create player
        Player player = new Player(0, Settings.getUsername(getActivity()));
        NotificationsManagerInterface notificationManager = NotificationsManagerFactory.getInstance(getActivity());
        player.setRegistrationId(notificationManager.getRegistrationId());
        player.setPlatformType(notificationManager.getServiceType() == NotificationServiceType.NOKIA_NOTIFICATIONS ? Player.PLATFORM_NOKIA : Player.PLATFORM_GOOGLE);

        RadioGroup group = (RadioGroup) getView().findViewById(R.id.radiobuttons);

        switch (group.getCheckedRadioButtonId()) {
            case R.id.blue_team_choice:
                player.setTeam(Player.BLUE);
                break;
            case R.id.red_team_choice:
                player.setTeam(Player.RED);
                break;
            default:
                // TODO: Show validation error
                break;
        }

        LocationManagerInterface locationManager = LocationManagerFactory.getInstance(getActivity());
        Location location = locationManager.getCurrentLocation();
        player.setLocation(location);

        Controller.getInstance().getNetworkClient().emit(new JoinRequest(mGame, player));
    }
}




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