Android Open Source - Werewolf-Android-Client Day Tab






From Project

Back to project page Werewolf-Android-Client.

License

The source code is released under:

Copyright (c) 2013, Timothy Cohen All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: ...

If you think the Android project Werewolf-Android-Client 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

package edu.wm.werewolf_client;
//  w w  w  .j a v  a2 s .  c om
import java.util.ArrayList;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class DayTab extends Fragment{
   
  private String TAG = "DayTab";
  private Boolean isAlive;
  private Boolean isNight;
  private Boolean isWerewolf = false;
  public String password;
  public String username;
  public ArrayList<Player> livingPlayers = new ArrayList<Player>();
  public Context context;
  public Vote voteInstance; 
  public boolean voted;

  private final Handler myHandler = new Handler();
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.activity_day,
              container, false);
     
    username = UsernameAndPassword.getUsername();
    password = UsernameAndPassword.getPassword();
    
    this.context = view.getContext();
    
    final GetAllAlive getAllAlive = new GetAllAlive();
    final IsNight isNightInstance = new IsNight();
    voteInstance = new Vote();
    
    new Thread(new Runnable() {
      @Override
      public void run() {
        livingPlayers = getAllAlive.getLivingPlayers(username, password);
        AccessUI();
      }
    }).start();
    
    
    new Thread(new Runnable() {
      @Override
      public void run() {
        isNight = isNightInstance.isNight(username, password);
        if (isNight){
          voted = false;//reset
          Log.i(TAG, "It is Night");
        }
        else{
          Log.i(TAG, "It is Day");
        }
        AccessUI();
      }
    }).start();
    
    
    return view;
  }
  

  private void UpdateUI(){

    try{

      if ((isNight == null) || (isNight)){
        TextView isDeadText = (TextView) getView().findViewById(R.id.voteText);
        isDeadText.setText("It is night. You cannot vote now");

        Button voteButton = (Button) getView().findViewById(R.id.voteButton);
        voteButton.setVisibility(View.GONE);
      }
      else{
        TextView isDeadText = (TextView) getView().findViewById(R.id.voteText);
        isDeadText.setText("It is daytime, you can vote now");

        if (voted){
          isDeadText.setText("You already voted today");
          Button voteButton = (Button) getView().findViewById(R.id.voteButton);
          voteButton.setVisibility(View.GONE);
        }

        if ((!livingPlayers.isEmpty()) && (!voted)){
          final RadioGroup rg = (RadioGroup) getView().findViewById(R.id.radiogroup);//not this RadioGroup rg = new RadioGroup(this);
          rg.removeAllViews();//prevent duplicates
          rg.setOrientation(RadioGroup.VERTICAL);
          for(int i=0; i<livingPlayers.size(); i++)
          {
            RadioButton rb = new RadioButton(context);
            rg.addView(rb); 
            rb.setText(livingPlayers.get(i).getId());
          }

          Button voteButton = (Button) getView().findViewById(R.id.voteButton);
          voteButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
              Log.i(TAG, "Vote Button Pressed!");
              Log.i(TAG, "Result from button was: "+rg.getCheckedRadioButtonId());
              // find the radiobutton by returned id
              final Button selectedPlayerButton = (RadioButton) getView().findViewById(rg.getCheckedRadioButtonId());
              final String selectedPlayer = selectedPlayerButton.getText().toString();
              Log.i(TAG, "selected player was: "+selectedPlayer);
              new Thread(new Runnable() {
                @Override
                public void run() {
                  voteInstance.vote(selectedPlayer);
                  Log.i(TAG, "Voted for "+selectedPlayer);
                }
              }).start();
              Toast.makeText(context, "Thank you for voting! You can only vote once per day", Toast.LENGTH_LONG).show();
              Button voteButton = (Button) getView().findViewById(R.id.voteButton);
              voteButton.setVisibility(View.GONE);
              voted = true;

            }
          });

        }
      }


    }catch (NullPointerException e){
      Log.e(TAG, "Null Pointer!");
    }

  }


  final Runnable updateRunnable = new Runnable() {
       public void run() {
           //call the activity method that updates the UI
           UpdateUI();
       }
   };
   
   private void AccessUI()
   {
        //update the UI using the handler and the runnable
        myHandler.post(updateRunnable);

   }
   
  public void setPassword(String password) {
    this.password = password;
  }


  public void setUsername(String username) {
    this.username = username;
  }
}




Java Source Code List

edu.wm.werewolf_client.DayTab.java
edu.wm.werewolf_client.FindLocation.java
edu.wm.werewolf_client.GetAllAlive.java
edu.wm.werewolf_client.GetKills.java
edu.wm.werewolf_client.IsNight.java
edu.wm.werewolf_client.KillAttempt.java
edu.wm.werewolf_client.Locate.java
edu.wm.werewolf_client.MainActivity.java
edu.wm.werewolf_client.MainInterface.java
edu.wm.werewolf_client.NightTab.java
edu.wm.werewolf_client.ObatainLocation.java
edu.wm.werewolf_client.Play.java
edu.wm.werewolf_client.Player.java
edu.wm.werewolf_client.Register.java
edu.wm.werewolf_client.Stats.java
edu.wm.werewolf_client.UserTab.java
edu.wm.werewolf_client.UsernameAndPassword.java
edu.wm.werewolf_client.Validate.java
edu.wm.werewolf_client.Vote.java
edu.wm.werewolf_client.isWerewolf.java