Android Open Source - dashclock-gerrit Gerrit Preferences






From Project

Back to project page dashclock-gerrit.

License

The source code is released under:

Apache License

If you think the Android project dashclock-gerrit 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.plusonelabs.dashclock.gerrit.prefs;
//w ww .j a  v  a  2  s .co m
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Base64;

public class GerritPreferences {

  public static final String SERVER_URL = "server_url";
  public static final String SERVER_USERNAME = "server_username";
  public static final String SERVER_PASSWORD = "server_password";
  public static final String DISPLAY_CHANGES_SCOPE = "display_scope";
  public static final String DISPLAY_CHANGES_SCOPE_ALL = "display_scope_all";
  public static final String DISPLAY_CHANGES_SCOPE_ASSIGNED = "display_scope_assigned";
  public static final String DISPLAY_FILTER_PROJECT = "display_filter_project";
  public static final String DISPLAY_FILTER_BRANCH = "display_filter_branch";
  public static final String DISPLAY_REVIEWER = "display_reviewer";

  private static final String SECURE_KEY = "secure_key";
  private static final String DEFAULT_SECURE_PREFERENCES_KEY = "v7Ja53ezpxgNxBPgeCQwVuoqFA9AK4ea";

  private static SecurePreferences securePreferences;

  private GerritPreferences() {
    // prohibit instantiation
  }

  public static SecurePreferences getSecurePreferences(Context context) {
    if (securePreferences == null) {
      String key = getSecurePreferencesKey(context);
      securePreferences = new SecurePreferences(context, "secure-preferences.xml", key, true);
    }
    return securePreferences;
  }

  private static String getSecurePreferencesKey(Context context) {
    String securePreferencesKey = getSharedPreferences(context).getString(SECURE_KEY, null);
    if (securePreferencesKey == null) {
      SecretKey key = generateKey();
      if (key != null) {
        String generatedKey = Base64.encodeToString(key.getEncoded(), Base64.DEFAULT);
        securePreferencesKey = generatedKey;
        SharedPreferences prefs = getSharedPreferences(context);
        prefs.edit().putString(SECURE_KEY, securePreferencesKey).commit();
      }
    }
    if (securePreferencesKey == null) {
      return DEFAULT_SECURE_PREFERENCES_KEY;
    }
    return securePreferencesKey + DEFAULT_SECURE_PREFERENCES_KEY;
  }

  public static SharedPreferences getSharedPreferences(Context context) {
    return PreferenceManager.getDefaultSharedPreferences(context);
  }

  public static SecretKey generateKey() {
    try {
      // the SecureRandom is not seeded because since 4.2 it is not deterministic anymore
      SecureRandom secureRandom = new SecureRandom();
      KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
      keyGenerator.init(256, secureRandom);
      SecretKey key = keyGenerator.generateKey();
      return key;
    } catch (NoSuchAlgorithmException e) {
      return null;
    }
  }

}




Java Source Code List

com.plusonelabs.dashclock.gerrit.Change.java
com.plusonelabs.dashclock.gerrit.GerritDashClockExtension.java
com.plusonelabs.dashclock.gerrit.GerritEndpoint.java
com.plusonelabs.dashclock.gerrit.Gerrit.java
com.plusonelabs.dashclock.gerrit.QueryBuilder.java
com.plusonelabs.dashclock.gerrit.SettingsActivity.java
com.plusonelabs.dashclock.gerrit.TestActivity.java
com.plusonelabs.dashclock.gerrit.auth.AnonymousAuthenticationProvider.java
com.plusonelabs.dashclock.gerrit.auth.AuthenticationProvider.java
com.plusonelabs.dashclock.gerrit.auth.BasicAuthWithCookieAuthenticationProvider.java
com.plusonelabs.dashclock.gerrit.prefs.AboutFragment.java
com.plusonelabs.dashclock.gerrit.prefs.DisplayFragment.java
com.plusonelabs.dashclock.gerrit.prefs.GerritPreferences.java
com.plusonelabs.dashclock.gerrit.prefs.SecurePreferences.java
com.plusonelabs.dashclock.gerrit.prefs.ServerFragment.java
com.plusonelabs.dashclock.gerrit.util.ParamCheck.java
com.plusonelabs.dashclock.gerrit.util.StringUtil.java
com.plusonelabs.dashclock.gerrit.util.UrlUtil.java