Android Open Source - game_guess_lib Guess Main 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;
/*from ww  w  .j  av  a 2s .  co m*/
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.wkmf.guess.lib.R;
import com.wkmf.guess.lib.common.Constants;
import com.wkmf.guess.lib.common.ads.GuessGameAds;
import com.wkmf.guess.lib.common.ads.GuessGameAdsListener;
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.impl.GuessGameInterface;
import com.wkmf.guess.lib.screen.adapter.LevelsAdapter;
import com.wkmf.guess.lib.service.GuessGameService;
import com.wkmf.guess.lib.structure.GuessConfig;
import com.wkmf.guess.lib.structure.GuessDrawable;
import com.wkmf.guess.lib.structure.GuessGame;
import com.wkmf.lib.curl.CurlListener;

/**
 * Created by ernestofndz on 9/02/14.
 */
public abstract class GuessMainScreen extends GuessGameBaseApp implements GuessGameInterface, CurlListener, GuessGameAdsListener {

    private GuessGameAds guessGameAds;

    // callback de curl al solicitar los niveles
    @Override
    public void callback(String result) {
        guessGame = new GuessGame(config.getId(), GuessApi.toLevels(result));
        // insertamos en bdd
        new GuessGameBDDHandler(this, GuessConfig.BDD).insert(guessGame);
        // cargamos la pantalla
        loadScreen();
    }

    @Override
    public void onAdClosed() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // inicializamos el contexto
        setContext(this);
        // configuracion
        config = getConfig();
        // 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);
        // intentamos recuperar los datos
        getData();
        // iniciamos el servicio si ya no lo est previamente
        if (!GuessGameService.isRunning()) startService(new Intent(this, GuessGameService.class));
        // inicializamos los ads
        this.guessGameAds = new GuessGameAds();
        this.guessGameAds.initAd(getContext(), this);
    }

    // recuperar los datos ya sea desde la bdd o haciendo la peticin al servidor
    @Override
    protected void getData() {
        GuessGame guessGameBDD = new GuessGameBDDHandler(this, GuessConfig.BDD).getGuessGame();
        // si el resultado es null, significa que tenemos que recuperar los datos
        // del servidor
        if (guessGameBDD == null) {
            // hacemos la peticin via cURL para recuperar los niveles
            new GuessRestApi(this, this, true).getLevels(config.getId());
        } else {
            // asignamos el valor que teniamos almacenado en bdd
            guessGame = guessGameBDD;
            // capturamos un posible NPE
            try {
                // cargamos la pantalla
                loadScreen();
            } catch (NullPointerException npe) {
                // descargamos de nuevo la info pero antes borramos lo que teniamos en BDD
                new GuessGameBDDHandler(getContext(), GuessConfig.BDD).delete(guessGame);
                new GuessRestApi(this, this, true).getLevels(config.getId());
            }
        }
    }

    // cargamos los datos de la pantalla
    @Override
    protected void loadScreen() {
        // asignamos el layout
        setContentView(R.layout.screen_main);
        // imagen de fondo
        findViewById(R.id.guess_game_main_root).setBackgroundDrawable(GuessDrawable.background);
        ((ImageView) findViewById(R.id.guess_game_header_icon)).setImageDrawable(GuessDrawable.guessGameIcon);
        // rate us
        ((TextView) findViewById(R.id.guess_game_rate_us_label)).setTextColor(Color.parseColor(config.getNameTextColor()));
        findViewById(R.id.guess_game_rate_us).setBackgroundColor(Color.parseColor(config.getNameBackgroundColor()));
        findViewById(R.id.guess_game_rate_us).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // abrimos enlace al market
                Uri uri = Uri.parse("market://details?id=" + getPackageName());
                Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
                try {
                    startActivity(goToMarket);
                } catch (ActivityNotFoundException anfe) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())));
                }
            }
        });
        // cargamos la lista de niveles
        loadLevels();
    }

    // cargamos la lista de niveles
    private void loadLevels() {
        // grid view
        final GridView levelsLayout = (GridView) findViewById(R.id.guess_game_levels);
        levelsLayout.setAdapter(new LevelsAdapter(this, config, guessGame.getLevels()));
        // damos funcionalidad al pulsar sobre los elementos
        levelsLayout.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // enviamos los datos a la siguiente tarea
                Intent toLevel = new Intent(getContext(), GuessLevelScreen.class);
                toLevel.putExtra(Constants.EXTRA_GUESS_LEVEL, i);
                startActivityForResult(toLevel, 0);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });
    }

    @Override
    protected void resultOK() {
        // recargamos la pantalla
        loadScreen();
    }

    @Override
    public void onBackPressed() {
        if (this.guessGameAds.isAdLoaded()) {
            this.guessGameAds.showAd();
        } else {
            super.onBackPressed();
        }
    }
}




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