Android Open Source - ScoponeDaPolso Game Fragment






From Project

Back to project page ScoponeDaPolso.

License

The source code is released under:

GNU General Public License

If you think the Android project ScoponeDaPolso 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 org.gdg.bari.scopone.fragment;
/*  ww w  .ja  v a  2s.  c  o m*/

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.gdg.bari.entities.Card;
import org.gdg.bari.scopone.R;
import org.gdg.bari.scopone.activity.MobileMainActivity;
import org.gdg.bari.scopone.util.LogUtil;

import java.lang.reflect.Type;
import java.util.List;

public class GameFragment extends Fragment implements View.OnClickListener{

    private static final String TAG = GameFragment.class.getSimpleName();

    private MobileMainActivity mActivity;

    private OnGameFragmentInteractionListener mListener;

    private Button doneBtn;

    private CardReceiver cardReceiver;

    private IntentFilter shootedCardIntentFilter;

    public static final String ACTION_SHOOTED_CARD = "shootedCard";

    private Gson gson;

    public Button getDoneBtn(){
        return this.doneBtn;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        shootedCardIntentFilter = new IntentFilter(ACTION_SHOOTED_CARD);
        cardReceiver = new CardReceiver();
        gson = new Gson();
    }

    @Override
    public void onStart() {
        super.onStart();
        getActivity().registerReceiver(cardReceiver, shootedCardIntentFilter);
    }

    @Override
    public void onStop() {
        super.onStop();
        getActivity().unregisterReceiver(cardReceiver);
    }

    @Override
    public void onAttach(Activity activity) {

        super.onAttach(activity);

        mActivity = (MobileMainActivity) activity;

        try {
            mListener = (OnGameFragmentInteractionListener) mActivity;

        } catch (ClassCastException ex) {

            LogUtil.w(TAG, TAG + " ClassCastException");
            LogUtil.e(TAG, Log.getStackTraceString(ex));

            throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_game, container, false);
        doneBtn = (Button) v.findViewById(R.id.doneBtn);
        doneBtn.setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.doneBtn:
                mListener.onNextTurnClicked(this.doneBtn);
                break;
        }
    }

    public interface OnGameFragmentInteractionListener {

        public void onNextTurnClicked(Button btn);
    }

    //BROADCAST RECEIVER
    public class CardReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String jsonCard = intent.getExtras().getString("shootedCardJson");
            Log.d(TAG, "GameFragment:OnReceive()");
            Log.d(TAG, jsonCard);
            //Type cardListType = new TypeToken<Card>() {}.getType();
            Card playedCard = gson.fromJson(jsonCard, Card.class);
            Toast.makeText(getActivity().getApplicationContext(), "giocato " + playedCard.toString(), Toast.LENGTH_LONG).show();

        }
    }
}




Java Source Code List

com.google.example.games.basegameutils.BaseGameActivity.java
com.google.example.games.basegameutils.BaseGameUtils.java
com.google.example.games.basegameutils.GameHelperUtils.java
com.google.example.games.basegameutils.GameHelper.java
com.mariux.teleport.lib.ApplicationTest.java
com.mariux.teleport.lib.TeleportClient.java
com.mariux.teleport.lib.TeleportService.java
org.gdg.bari.entities.ApplicationTest.java
org.gdg.bari.entities.Card.java
org.gdg.bari.entities.Constants.java
org.gdg.bari.entities.Deck.java
org.gdg.bari.entities.Score.java
org.gdg.bari.entities.TableTurn.java
org.gdg.bari.entities.Table.java
org.gdg.bari.entities.TeleportClientManager.java
org.gdg.bari.scopone.ApplicationTest.java
org.gdg.bari.scopone.activity.MobileGameActivity.java
org.gdg.bari.scopone.activity.MobileMainActivity.java
org.gdg.bari.scopone.activity.WearConfirmationActivity.java
org.gdg.bari.scopone.activity.WearMainActivity.java
org.gdg.bari.scopone.fragment.CardListFragment.java
org.gdg.bari.scopone.fragment.GameFragment.java
org.gdg.bari.scopone.fragment.LoginFragment.java
org.gdg.bari.scopone.fragment.MenuFragment.java
org.gdg.bari.scopone.service.MobileTeleportService.java
org.gdg.bari.scopone.services.WearTeleportService.java
org.gdg.bari.scopone.util.GoogleApiClientManager.java
org.gdg.bari.scopone.util.LogUtil.java