Android Open Source - Freebloks-Android Freebloks Preferences






From Project

Back to project page Freebloks-Android.

License

The source code is released under:

GNU General Public License

If you think the Android project Freebloks-Android 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 de.saschahlusiak.freebloks.preferences;
//from ww w  .j  a  v a2  s . c om
import java.util.List;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.example.games.basegameutils.GameHelper;
import com.google.example.games.basegameutils.GameHelper.GameHelperListener;
import com.google.android.gms.games.Games;

import de.saschahlusiak.freebloks.Global;
import de.saschahlusiak.freebloks.R;
import android.app.backup.BackupManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceGroup;
import android.view.MenuItem;
import android.view.ViewConfiguration;

public class FreebloksPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener, GameHelperListener {
  private static final int REQUEST_LEADERBOARD = 1;
  private static final int REQUEST_ACHIEVEMENTS = 2;
  GameHelper mHelper;
  boolean hasHeaders = false;

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

    if (Build.VERSION.SDK_INT >= 11) {
      if (getActionBar() != null)
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    if (savedInstanceState != null) {
      hasHeaders = savedInstanceState.getBoolean("hasHeaders");
    }

    if (getIntent().getStringExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT) != null)
      hasHeaders = true;

    if (hasHeaders)
      return;

    mHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
    mHelper.setMaxAutoSignInAttempts(0);
        mHelper.setup(this);

    addPreferencesFromResource(R.xml.preferences);
    
    ViewConfiguration viewConfig = ViewConfiguration.get(this);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT || viewConfig.hasPermanentMenuKey()) {
      Preference p = findPreference("immersive_mode");
      ((PreferenceGroup)findPreference("interface_category")).removePreference(p);
    }

    findPreference("rate_review").setOnPreferenceClickListener(new OnPreferenceClickListener() {
      @Override
      public boolean onPreferenceClick(Preference preference) {
        String url;
        if (Global.IS_AMAZON)
          url = Global.getMarketURLString(getPackageName());
        else
          url = "market://details?id=" + getApplication().getPackageName();
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
      }
    });
    findPreference("rate_review").setTitle(getString(R.string.prefs_rate_review, Global.IS_AMAZON ? "Amazon App Store" : "Google Play"));

    int avail = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (avail != ConnectionResult.SUCCESS) {
      getPreferenceScreen().removePreference(findPreference("googleplus_category"));
    } else {
      findPreference("googleplus_signin").setOnPreferenceClickListener(new OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
          if (mHelper.isSignedIn()) {
            mHelper.signOut();
            onSignInFailed();
          } else
            mHelper.beginUserInitiatedSignIn();
          return true;
        }
      });
      findPreference("googleplus_leaderboard").setOnPreferenceClickListener(new OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
          if (!mHelper.isSignedIn())
            return false;
  //        startActivityForResult(mHelper.getGamesClient().getAllLeaderboardsIntent(), REQUEST_LEADERBOARD);
          startActivityForResult(Games.Leaderboards.getLeaderboardIntent(mHelper.getApiClient(), getString(R.string.leaderboard_points_total)), REQUEST_LEADERBOARD);
          return true;
        }
      });
      findPreference("googleplus_achievements").setOnPreferenceClickListener(new OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
          startActivityForResult(Games.Achievements.getAchievementsIntent(mHelper.getApiClient()), REQUEST_ACHIEVEMENTS);
          return true;
        }
      });
    }
  }

  @Override
  public void onBuildHeaders(List<Header> target) {
    if (!onIsMultiPane())
      return;
    hasHeaders = true;
        loadHeadersFromResource(R.xml.preference_headers, target);
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    outState.putBoolean("hasHeaders", hasHeaders);
    super.onSaveInstanceState(outState);
  }

  @Override
  protected void onDestroy() {
    if (Build.VERSION.SDK_INT >= 8) {
      BackupManager backupManager = new BackupManager(this);
      backupManager.dataChanged();
    }
    super.onDestroy();
  }

  @Override
  protected void onStart() {
    super.onStart();
    if (mHelper != null)
      mHelper.onStart(this);
  }

  @Override
  protected void onStop() {
    super.onStop();
    if (mHelper != null)
      mHelper.onStop();
  }

    @Override
    protected void onActivityResult(int request, int response, Intent data) {
        super.onActivityResult(request, response, data);
        if (mHelper != null)
          mHelper.onActivityResult(request, response, data);
    }

  @Override
  protected void onResume() {
    if (!hasHeaders) {
      SharedPreferences prefs = getPreferenceScreen().getSharedPreferences();
      onSharedPreferenceChanged(prefs, "player_name");
      onSharedPreferenceChanged(prefs, "theme");
      onSharedPreferenceChanged(prefs, "animations");
      prefs.registerOnSharedPreferenceChangeListener(this);
    }
    super.onResume();
  }

  @Override
  protected void onPause() {
    if (!hasHeaders)
      getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    super.onPause();
  }

  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if (key.equals("player_name")) {
      EditTextPreference pref = (EditTextPreference)findPreference(key);
      String s = pref.getText();
      if (s == null || s.equals(""))
        s = getString(R.string.prefs_player_name_default);
      pref.setSummary(s);
    }
    if (key.equals("theme") || key.equals("animations")) {
      ListPreference pref = (ListPreference)findPreference(key);
      pref.setSummary(pref.getEntry());
    }
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
      finish();
      return true;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  public void onSignInFailed() {
    if (hasHeaders)
      return;
    if (findPreference("googleplus_category") == null)
      return;

    findPreference("googleplus_signin").setTitle(R.string.googleplus_signin);
    findPreference("googleplus_signin").setSummary(R.string.googleplus_signin_long);
    findPreference("googleplus_leaderboard").setEnabled(false);
    findPreference("googleplus_achievements").setEnabled(false);
  }

  @Override
  public void onSignInSucceeded() {
    if (hasHeaders)
      return;
    findPreference("googleplus_signin").setTitle(R.string.googleplus_signout);
    findPreference("googleplus_signin").setSummary(getString(R.string.googleplus_signout_long, Games.Players.getCurrentPlayer(mHelper.getApiClient()).getDisplayName()));
    findPreference("googleplus_leaderboard").setEnabled(true);
    findPreference("googleplus_achievements").setEnabled(true);
  }

  @Override
  protected boolean isValidFragment(String fragmentName) {
    if (InterfaceFragment.class.getCanonicalName().equals(fragmentName))
      return true;
    if (DisplayFragment.class.getCanonicalName().equals(fragmentName))
      return true;
    if (MiscFragment.class.getCanonicalName().equals(fragmentName))
      return true;
    if (StatisticsFragment.class.getCanonicalName().equals(fragmentName))
      return true;
    if (AboutFragment.class.getCanonicalName().equals(fragmentName))
      return true;
    return false;
  }
}




Java Source Code List

com.google.example.games.basegameutils.BaseGameActivity.java
com.google.example.games.basegameutils.GameHelperUtils.java
com.google.example.games.basegameutils.GameHelper.java
de.saschahlusiak.freebloks.AboutActivity.java
de.saschahlusiak.freebloks.Global.java
de.saschahlusiak.freebloks.backup.FreebloksBackupAgent.java
de.saschahlusiak.freebloks.controller.GameMode.java
de.saschahlusiak.freebloks.controller.JNIServer.java
de.saschahlusiak.freebloks.controller.PlayerData.java
de.saschahlusiak.freebloks.controller.SpielClientInterface.java
de.saschahlusiak.freebloks.controller.SpielClient.java
de.saschahlusiak.freebloks.controller.Spielleiter.java
de.saschahlusiak.freebloks.database.FreebloksDBOpenHandler.java
de.saschahlusiak.freebloks.database.FreebloksDB.java
de.saschahlusiak.freebloks.database.HighscoreDB.java
de.saschahlusiak.freebloks.donate.DonateActivity.java
de.saschahlusiak.freebloks.game.ActivityInterface.java
de.saschahlusiak.freebloks.game.AddScoreTask.java
de.saschahlusiak.freebloks.game.CustomGameDialog.java
de.saschahlusiak.freebloks.game.FreebloksActivity.java
de.saschahlusiak.freebloks.game.GameFinishActivity.java
de.saschahlusiak.freebloks.game.GameMenu.java
de.saschahlusiak.freebloks.game.RateAppDialog.java
de.saschahlusiak.freebloks.game.RetainedConfig.java
de.saschahlusiak.freebloks.game.SpielClientThread.java
de.saschahlusiak.freebloks.lobby.ChatEntry.java
de.saschahlusiak.freebloks.lobby.ChatListAdapter.java
de.saschahlusiak.freebloks.lobby.ColorAdapter.java
de.saschahlusiak.freebloks.lobby.LobbyDialog.java
de.saschahlusiak.freebloks.model.Player.java
de.saschahlusiak.freebloks.model.Spiel.java
de.saschahlusiak.freebloks.model.Stone.java
de.saschahlusiak.freebloks.model.Turn.java
de.saschahlusiak.freebloks.model.Turnpool.java
de.saschahlusiak.freebloks.network.NET_CHAT.java
de.saschahlusiak.freebloks.network.NET_CURRENT_PLAYER.java
de.saschahlusiak.freebloks.network.NET_GAME_FINISH.java
de.saschahlusiak.freebloks.network.NET_GRANT_PLAYER.java
de.saschahlusiak.freebloks.network.NET_HEADER.java
de.saschahlusiak.freebloks.network.NET_REQUEST_GAME_MODE.java
de.saschahlusiak.freebloks.network.NET_REQUEST_HINT.java
de.saschahlusiak.freebloks.network.NET_REQUEST_PLAYER.java
de.saschahlusiak.freebloks.network.NET_REQUEST_UNDO.java
de.saschahlusiak.freebloks.network.NET_REVOKE_PLAYER.java
de.saschahlusiak.freebloks.network.NET_SERVER_STATUS.java
de.saschahlusiak.freebloks.network.NET_SET_STONE.java
de.saschahlusiak.freebloks.network.NET_START_GAME.java
de.saschahlusiak.freebloks.network.NET_UNDO_STONE.java
de.saschahlusiak.freebloks.network.Network.java
de.saschahlusiak.freebloks.preferences.AboutFragment.java
de.saschahlusiak.freebloks.preferences.DisplayFragment.java
de.saschahlusiak.freebloks.preferences.FreebloksPreferences.java
de.saschahlusiak.freebloks.preferences.InterfaceFragment.java
de.saschahlusiak.freebloks.preferences.MiscFragment.java
de.saschahlusiak.freebloks.preferences.StatisticsFragment.java
de.saschahlusiak.freebloks.preferences.ThemePreference.java
de.saschahlusiak.freebloks.stats.StatisticsActivity.java
de.saschahlusiak.freebloks.stats.StatisticsAdapter.java
de.saschahlusiak.freebloks.view.BackgroundRenderer.java
de.saschahlusiak.freebloks.view.BoardRenderer.java
de.saschahlusiak.freebloks.view.Freebloks3DView.java
de.saschahlusiak.freebloks.view.FreebloksRenderer.java
de.saschahlusiak.freebloks.view.SimpleModel.java
de.saschahlusiak.freebloks.view.effects.AbsEffect.java
de.saschahlusiak.freebloks.view.effects.AbsStoneEffect.java
de.saschahlusiak.freebloks.view.effects.BoardStoneGlowEffect.java
de.saschahlusiak.freebloks.view.effects.EffectSet.java
de.saschahlusiak.freebloks.view.effects.Effect.java
de.saschahlusiak.freebloks.view.effects.PhysicalStoneEffect.java
de.saschahlusiak.freebloks.view.effects.StoneFadeEffect.java
de.saschahlusiak.freebloks.view.effects.StoneRollEffect.java
de.saschahlusiak.freebloks.view.effects.StoneUndoEffect.java
de.saschahlusiak.freebloks.view.model.Board.java
de.saschahlusiak.freebloks.view.model.CurrentStone.java
de.saschahlusiak.freebloks.view.model.Intro.java
de.saschahlusiak.freebloks.view.model.Sounds.java
de.saschahlusiak.freebloks.view.model.Theme.java
de.saschahlusiak.freebloks.view.model.ViewElement.java
de.saschahlusiak.freebloks.view.model.ViewModel.java
de.saschahlusiak.freebloks.view.model.Wheel.java