Android Open Source - Google-Plus-Android-Sample Shared Preferences Credential Store






From Project

Back to project page Google-Plus-Android-Sample.

License

The source code is released under:

Apache License

If you think the Android project Google-Plus-Android-Sample 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

/* 
 * Copyright (C) 2011 iMellon//from w  ww  . j av a2s .c o  m
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.imellon.android.googleplus.store;

import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * @author Christos Papazafeiropoulos, Dimitris Makris
 * 
 * This class keeps the authentication tokens and the ID + SECRET
 * of the Google application.
 * 
 */

public class SharedPreferencesCredentialStore{
  
  /* 
   * Get access to OAuth 2 credentials in the <a
   * href="https://code.google.com/apis/console">Google apis console</a>
   * 
   */
  
  public static final String CLIENT_ID = "";
  public static final String CLIENT_SECRET = "";
  
  
  public static final String SCOPE = "https://www.googleapis.com/auth/plus.me";
  public static final String REDIRECT_URI = "http://localhost";

  private static final String ACCESS_TOKEN = "access_token";
  private static final String EXPIRATION_TIME = "token_expiration_perion";
  private static final String REFRESH_TOKEN = "refresh_token";
  private static final String SCOPE_STRING = "scope";

  private SharedPreferences prefs;

  private static SharedPreferencesCredentialStore store;

  private SharedPreferencesCredentialStore(SharedPreferences prefs) {
    this.prefs = prefs;
  }

  public static SharedPreferencesCredentialStore getInstance(SharedPreferences prefs) {
    if (store == null)
      store = new SharedPreferencesCredentialStore(prefs);

    return store;
  }

  public AccessTokenResponse read() {
    AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
    accessTokenResponse.accessToken = prefs.getString(ACCESS_TOKEN, "");
    accessTokenResponse.expiresIn = prefs.getLong(EXPIRATION_TIME, 0);
    accessTokenResponse.refreshToken = prefs.getString(REFRESH_TOKEN, "");
    accessTokenResponse.scope = prefs.getString(SCOPE_STRING, "");
    return accessTokenResponse;
  }

  public void write(AccessTokenResponse accessTokenResponse) {
    Editor editor = prefs.edit();
    editor.putString(ACCESS_TOKEN, accessTokenResponse.accessToken);
    editor.putLong(EXPIRATION_TIME, accessTokenResponse.expiresIn);
    editor.putString(REFRESH_TOKEN, accessTokenResponse.refreshToken);
    editor.putString(SCOPE_STRING, accessTokenResponse.scope);
    editor.commit();
  }

  public void clearCredentials() {
    Editor editor = prefs.edit();
    editor.remove(ACCESS_TOKEN);
    editor.remove(EXPIRATION_TIME);
    editor.remove(REFRESH_TOKEN);
    editor.remove(SCOPE_STRING);
    editor.commit();
  }
}




Java Source Code List

com.imellon.android.googleplus.GooglePlusActivity.java
com.imellon.android.googleplus.MyProfileActivity.java
com.imellon.android.googleplus.OAuthActivity.java
com.imellon.android.googleplus.ProfileActivitiesActivity.java
com.imellon.android.googleplus.ProfilesActivity.java
com.imellon.android.googleplus.store.SharedPreferencesCredentialStore.java
com.imellon.android.googleplus.utils.ProfileActivitiesAdapter.java
com.imellon.android.googleplus.utils.ProfilesAdapter.java