Android Open Source - game_guess_lib Guess Level Screen






From Project

Back to project page game_guess_lib.

License

The source code is released under:

MIT License

If you think the Android project game_guess_lib 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 com.wkmf.guess.lib.screen;
//w w  w.java  2 s  .  c  om
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.wkmf.guess.lib.R;
import com.wkmf.guess.lib.common.Constants;
import com.wkmf.guess.lib.common.api.GuessApi;
import com.wkmf.guess.lib.common.api.GuessRestApi;
import com.wkmf.guess.lib.data.GuessGameBDDHandler;
import com.wkmf.guess.lib.impl.GuessGameBaseApp;
import com.wkmf.guess.lib.screen.adapter.QuestionsAdapter;
import com.wkmf.guess.lib.structure.GuessConfig;
import com.wkmf.guess.lib.structure.GuessDrawable;
import com.wkmf.guess.lib.structure.GuessLevel;
import com.wkmf.lib.curl.CurlListener;

/**
 * Created by ernestofndz on 19/02/14.
 */
public class GuessLevelScreen extends GuessGameBaseApp implements CurlListener {

    private GuessLevel level;
    private int levelPos;

    // callback donde recibimos el json de preguntas
    @Override
    public void callback(String result) {
        this.level.setQuestions(GuessApi.toQuestions(result));
        // insertamos en bdd
        new GuessGameBDDHandler(this, GuessConfig.BDD).update(guessGame);
        loadScreen();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // recuperamos los datos que nos llegan de la actividad anterior
        Bundle b = getIntent().getExtras();
        if (b != null) {
            this.levelPos = b.getInt(Constants.EXTRA_GUESS_LEVEL);
            this.level = guessGame.getLevels().get(this.levelPos);
            // inicializamos el contexto
            setContext(this);
            // seteamos el theme de la actividad, aunque realmente es a pantalla completa
            setTheme(config.getTheme());
            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            // recuperamos los datos
            getData();
        } else {
            // TODO controlar un posible error de que no llegan los datos, son OBLIGATORIOS
        }
    }

    // recuperamos los datos, si no lo tenemos en el array, tenemos que hacer la peticin al servidor
    @Override
    protected void getData() {
        if (this.level.getQuestions() != null
                && this.level.getQuestions().size() > 0) {
            // tenemos las preguntas del nivel
            loadScreen();
        } else {
            // hay que consultar las preguntas al servidor
            new GuessRestApi(this, this, true).getQuestions(config.getId(), this.level.getId());
        }
    }

    private void loadComponents() {
        // damos funcionalidad al cerrar popup
        findViewById(R.id.popup_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // cerramos el popup, o mejor dicho, lo ocultamos
                findViewById(R.id.popup_root).setVisibility(View.INVISIBLE);
                // y mostramos el grid
                findViewById(R.id.guess_game_questions).setVisibility(View.VISIBLE);
                // refrescamos la pantalla
                loadScreen();
            }
        });
        // imagen de fondo
        findViewById(R.id.guess_game_level_root).setBackgroundDrawable(GuessDrawable.background);
    }

    @Override
    protected void loadScreen() {
        // seteamos el layout
        setContentView(R.layout.screen_level);
        // cargamos los componentes
        loadComponents();
        // cargamos el header
        loadHeader();
        final GridView questions = (GridView) findViewById(R.id.guess_game_questions);
        questions.setAdapter(new QuestionsAdapter(this, this.level.getQuestions(), config));
        // damos la funcin al pulsar sobre un elemento
        questions.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // enviamos los datos a la siguiente tarea
                Intent toQuestion = new Intent(getContext(), GuessQuestionScreen.class);
                toQuestion.putExtra(Constants.EXTRA_GUESS_LEVEL, levelPos);
                toQuestion.putExtra(Constants.EXTRA_GUESS_QUESTION, i);
                startActivityForResult(toQuestion, 0);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });
    }

    // cargamos el header que indica el nivel, rating
    private void loadHeader() {
        ((TextView) findViewById(R.id.guess_game_level_header_label)).setTextColor(Color.parseColor(config.getNameTextColor()));
        findViewById(R.id.guess_game_level_header).setBackgroundColor(Color.parseColor(config.getNameBackgroundColor()));
        final TextView levelNumber = (TextView) findViewById(R.id.guess_game_level_header_number);
        levelNumber.setTextColor(Color.parseColor(config.getNameTextColor()));
        // seteamos el nivel
        levelNumber.setText(String.valueOf(levelPos + 1));
        // seteamos la dificultad
        final RatingBar ratingBar = (RatingBar) findViewById(R.id.guess_game_level_header_rating);
        ratingBar.setNumStars(level.getDifficulty());
        ratingBar.setRating(level.getDifficulty());
    }

    @Override
    protected void resultOK() {
        boolean completed = true;
        // comprobamos si el nivel est completo
        for (int i = 0; i < this.level.getQuestions().size(); i++) {
            if (!this.level.getQuestions().get(i).isCompleted()) {
                completed = false;
            }
        }
        if (completed) {
            // premio por completar el nivel
            int prize = this.level.getLevelCompletePrize();
            // marcamos el nivel como completado
            this.level.setCompleted(true);
            // mostramos el popup y ocultamos el gridview mientras
            findViewById(R.id.guess_game_questions).setVisibility(View.INVISIBLE);
            findViewById(R.id.popup_root).setVisibility(View.VISIBLE);
            // e indicamos cuantas vidas ha ganado
            ((TextView) findViewById(R.id.popup_earned)).setText(String.valueOf(prize));
            // sumamos las vidas que se ha ganado
            guessGame.addLives(prize);
            // guardamos en bdd
            new GuessGameBDDHandler(getContext(), GuessConfig.BDD).update(guessGame);
            // est completo, marcamos resultado para que se refresque la pantalla inicial
            setResult(RESULT_OK);
        } else {
            // refrescamos la pantalla
            loadScreen();
        }

    }
}




Java Source Code List

com.android.vending.billing.util.Base64DecoderException.java
com.android.vending.billing.util.Base64.java
com.android.vending.billing.util.IabException.java
com.android.vending.billing.util.IabHelper.java
com.android.vending.billing.util.IabResult.java
com.android.vending.billing.util.Inventory.java
com.android.vending.billing.util.Purchase.java
com.android.vending.billing.util.Security.java
com.android.vending.billing.util.SkuDetails.java
com.wkmf.guess.lib.common.Constants.java
com.wkmf.guess.lib.common.ads.GuessGameAdsListener.java
com.wkmf.guess.lib.common.ads.GuessGameAds.java
com.wkmf.guess.lib.common.api.GuessApi.java
com.wkmf.guess.lib.common.api.GuessRestApi.java
com.wkmf.guess.lib.common.async.DownloadTask.java
com.wkmf.guess.lib.common.dialog.DialogElement.java
com.wkmf.guess.lib.common.dialog.DialogModalListAdapter.java
com.wkmf.guess.lib.common.dialog.DialogModal.java
com.wkmf.guess.lib.data.GuessGameBDDHandler.java
com.wkmf.guess.lib.data.GuessSQL.java
com.wkmf.guess.lib.impl.GuessGameBaseApp.java
com.wkmf.guess.lib.impl.GuessGameImageDownload.java
com.wkmf.guess.lib.impl.GuessGameInterface.java
com.wkmf.guess.lib.purchase.items.GuessGameItems.java
com.wkmf.guess.lib.screen.GuessLevelScreen.java
com.wkmf.guess.lib.screen.GuessMainScreen.java
com.wkmf.guess.lib.screen.GuessQuestionScreen.java
com.wkmf.guess.lib.screen.adapter.LevelsAdapter.java
com.wkmf.guess.lib.screen.adapter.QuestionsAdapter.java
com.wkmf.guess.lib.service.GuessGameService.java
com.wkmf.guess.lib.service.GuessGameUpdater.java
com.wkmf.guess.lib.service.ServiceStarter.java
com.wkmf.guess.lib.structure.GuessConfig.java
com.wkmf.guess.lib.structure.GuessDrawable.java
com.wkmf.guess.lib.structure.GuessGame.java
com.wkmf.guess.lib.structure.GuessLevelType.java
com.wkmf.guess.lib.structure.GuessLevel.java
com.wkmf.guess.lib.structure.GuessQuestion.java