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;
//www.java2s.comimport com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.games.Games;
import com.google.example.games.basegameutils.GameHelper;
import com.google.example.games.basegameutils.GameHelper.GameHelperListener;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.Preference.OnPreferenceClickListener;
import de.saschahlusiak.freebloks.R;
publicclass StatisticsFragment extends PreferenceFragment implements GameHelperListener {
privatestaticfinalint REQUEST_LEADERBOARD = 1;
privatestaticfinalint REQUEST_ACHIEVEMENTS = 2;
GameHelper mHelper;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences_stats);
}
@Override
publicvoid onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mHelper = new GameHelper(getActivity(), GameHelper.CLIENT_GAMES);
mHelper.setMaxAutoSignInAttempts(0);
mHelper.setup(this);
/* XXX: this is a hack, because the onActivityResult of the activity will be called instead of the fragment *//* make sure that the activity knows about the helper, which is created in the context of the fragment. bah. */
((FreebloksPreferences)getActivity()).mHelper = mHelper;
int avail = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
if (avail != ConnectionResult.SUCCESS) {
getPreferenceScreen().removePreference(findPreference("googleplus_category"));
} else {
findPreference("googleplus_signin").setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
publicboolean onPreferenceClick(Preference preference) {
if (mHelper.isSignedIn()) {
mHelper.signOut();
onSignInFailed();
} else
mHelper.beginUserInitiatedSignIn();
return true;
}
});
findPreference("googleplus_leaderboard").setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
publicboolean 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
publicboolean onPreferenceClick(Preference preference) {
startActivityForResult(Games.Achievements.getAchievementsIntent(mHelper.getApiClient()), REQUEST_ACHIEVEMENTS);
return true;
}
});
}
}
@Override
publicvoid onStart() {
super.onStart();
mHelper.onStart(getActivity());
}
@Override
publicvoid onStop() {
super.onStop();
mHelper.onStop();
}
@Override
publicvoid onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// mHelper.onActivityResult(requestCode, resultCode, data);
/* the onActivityResult of the activity will be called :-/ */
}
@Override
publicvoid onSignInFailed() {
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
publicvoid onSignInSucceeded() {
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);
}
}