Android Open Source - PrincePolo Preferences






From Project

Back to project page PrincePolo.

License

The source code is released under:

GNU General Public License

If you think the Android project PrincePolo 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 se.agile.model;
//from   ww w.  j  av a 2s .  com
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import se.agile.githubdata.Branch;
import se.agile.githubdata.File;
import se.agile.githubdata.Repository;
import se.agile.githubdata.User;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class Preferences {
  private static String PREF_STRINGS_NAME = "princepolo";
    private static String logTag = "PrincePolo"; 
    private static SharedPreferences prefs;
    
    public static void initializePreferences(Context context){
      if(prefs == null){
        prefs = context.getSharedPreferences(PREF_STRINGS_NAME, Context.MODE_PRIVATE);
      }else{
        Log.e(logTag, "Preferences is already initialized");
      }
    }
    
    private static boolean isInitialized(){
      return prefs != null;
    }
    
    private static void setGeneral(PREF_KEY key, String value){
      if(isInitialized()){
        Editor edit = prefs.edit();
            edit.putString(key.getKey(), value);
            edit.apply();
            fireStateChanged(key);
//            Log.d(logTag,"saved " + key + ": " + value + " to preferences");
      }else{
        Log.e(logTag, "Preferences is not initialized");
      }
    }
    private static String getGeneral(PREF_KEY key) {
      if(isInitialized()){
        String value = prefs.getString(key.getKey(), "");
//            if(value.equals("")){
//              Log.e(logTag, "You have no " + key + " saved in prefereneces");
//            }else{
//              Log.d(logTag, "Got " + key + " from preferences: " + value);
//            }
            return value;
      }else{
        Log.e(logTag, "Preferences is not initialized");
        return "";
      }
    }
    
    public static void ClearPreferences(){
      setAccessToken("");
      setRepositories(new ArrayList<Repository>());
      setScope("");
      setTokenType("");
      setUser(new User(""));
      setSelectedRepository(new Repository(""));
      setUserAcountCreated("");
      setIsFirstTime(true);
      setTimeInterval("10"); // 10 seconds between checks
      removeAllBranches();
      setWorkingFiles(new ArrayList<File>());
    }
    
    public static enum PREF_KEY {
      COMMITS_MD5_VALUE("commits_md5_value"),
      TIME_INTERVAL("time_interval"),
      ACCESS_TOKEN("access_token"),
      TOKEN_TYPE("token_type"),
      SCOPE("scope"),
      USER_NAME("username"),
      USER_REPOSITORIES("repos"),
      SELECTED_REPOSITORY("selected_repository"),
      WORKING_FILES("working_files"),
      UNSELECTED_BRANCHES("unselected_branches"),
      USER_ACCOUNT_CREATED("account_created"),
      FIRST_TIME_USING_APP("is_first_time"),
      CLIENT_ID("387b05f90574b6fede43"),
      CLIENT_SECRET("557392acf8c742ac6e6a3a4ff36b172f378c1633"),
      EMPTY("");
    
    private final String key;

    private PREF_KEY(String key) {
      this.key = key;
    }

    private String getKey() {
      return key;
    }
    
    public static PREF_KEY getKey(String key){
      for(PREF_KEY pref : values()){
        if(pref.getKey().equals(key)){
          return pref;
        }
      }
      return PREF_KEY.EMPTY;
    }
  }
    
//    public static String ACCESS_TOKEN = "access_token";
    
    public static void setAccessToken(String access_token) {
      setGeneral(PREF_KEY.ACCESS_TOKEN, access_token);
    }
    public static void setTimeInterval(String time_interval) {
      setGeneral(PREF_KEY.TIME_INTERVAL, time_interval);
    }
    public static String getTimeInterval() {
      return getGeneral(PREF_KEY.TIME_INTERVAL);
    }
    public static String getAccessToken() {
      return getGeneral(PREF_KEY.ACCESS_TOKEN);
    }
    
    public static void setMD5Value(String commits_md5_value) {
      setGeneral(PREF_KEY.COMMITS_MD5_VALUE, commits_md5_value);
    }
    public static String getMD5Value() {
      return getGeneral(PREF_KEY.COMMITS_MD5_VALUE);
    }
    
    
//    public static String TOKEN_TYPE = "token_type";
    
    public static void setTokenType(String token_type) {
      setGeneral(PREF_KEY.TOKEN_TYPE, token_type);
    }
    public static String getTokenType() {
      return getGeneral(PREF_KEY.TOKEN_TYPE);
    }
    
    
//    public static String SCOPE = "scope";
    
    public static void setScope(String scope) {
      setGeneral(PREF_KEY.SCOPE, scope);
    }
    public static String getScope() {
      return getGeneral(PREF_KEY.SCOPE);
    }
    
    public static void setIsFirstTime(Boolean is_first_time) {
      if(is_first_time){
        setGeneral(PREF_KEY.FIRST_TIME_USING_APP, "True");
      }else{
        setGeneral(PREF_KEY.FIRST_TIME_USING_APP, "False");
      }
      
    }
    public static boolean isFirstTimeUsingApp() {
      String isFirstTime = getGeneral(PREF_KEY.FIRST_TIME_USING_APP);
        return (isFirstTime.equals("True") || isFirstTime.equals(""));
    }    

    
    public static void setUser(User user) {
      setGeneral(PREF_KEY.USER_NAME, user.getName());
    }
    public static User getUser() {
      return new User(getGeneral(PREF_KEY.USER_NAME));
    }
    
    
    public static void setRepositories(ArrayList<Repository> repos) {
      if(repos == null){
        setGeneral(PREF_KEY.USER_REPOSITORIES, "");
      }else{
        StringBuilder builder = new StringBuilder();
          for(Repository repo : repos){
            builder.append(repo.getName() + ",");
          }
          setGeneral(PREF_KEY.USER_REPOSITORIES, builder.toString());
      }
    }
    public static ArrayList<Repository> getRepositories() {
      ArrayList<Repository> list = new ArrayList<Repository>();
      String repos = getGeneral(PREF_KEY.USER_REPOSITORIES);
      for(String repo: repos.split(",")){
        if(!repo.equals("")){
          list.add(new Repository(repo));
        }
      }
      return list;
    }
    
    // The List<String> passed in 
    public static void setUnselectedBranchesArray(List<String> unselectedBranches) {
      StringBuilder builder = new StringBuilder();
      for(int i = 0; i < unselectedBranches.size();i++){
        builder.append(unselectedBranches.get(i) + (unselectedBranches.size() - 1 == i ? "" : ","));
      }
      setGeneral(PREF_KEY.UNSELECTED_BRANCHES, builder.toString());
    }

  public static void addUnselectedBranch(String unselectedBranch) {
    String branches = getGeneral(PREF_KEY.UNSELECTED_BRANCHES);
    StringBuilder builder = new StringBuilder();
    if (branches.equals("")) {
      builder.append(branches + unselectedBranch);
    } else {
      builder.append(branches + "," + unselectedBranch);
    }
    setGeneral(PREF_KEY.UNSELECTED_BRANCHES, builder.toString());
  }

  public static void removeUnselectedBranches(String unselectedBranch) {
    String branches = getGeneral(PREF_KEY.UNSELECTED_BRANCHES);

    if (!branches.contains(",")) {
      setGeneral(PREF_KEY.UNSELECTED_BRANCHES, "");
    } else {
      List<String> strings = new ArrayList<String>(Arrays.asList(branches
          .split(",")));
      Log.d(logTag, "Muh: " + unselectedBranch);
      while (strings.remove(unselectedBranch))
        ;
      setUnselectedBranchesArray(strings);
    }
  }
    public static void removeAllBranches() {
          setGeneral(PREF_KEY.UNSELECTED_BRANCHES, "");
    }
    
    public static ArrayList<Branch> getUnselectedBranches() {
      ArrayList<Branch> list = new ArrayList<Branch>();
      String branches = getGeneral(PREF_KEY.UNSELECTED_BRANCHES);
      for(String branch: branches.split(",")){
        if(!branch.equals("")){
          list.add(new Branch(branch));
        }
      }
      return list;
    }
    
    public static void setWorkingFiles(ArrayList<File> workingFiles) {
      StringBuilder builder = new StringBuilder();
      for(int i = 0; i < workingFiles.size(); i++){
        File file = workingFiles.get(i);
        builder.append(file.getName() + "+" + file.getPath() + (i == workingFiles.size() - 1 ? "" : ","));
      }
      setGeneral(PREF_KEY.WORKING_FILES, builder.toString());
    }
    
    public static ArrayList<File> getWorkingFiles() {
      ArrayList<File> list = new ArrayList<File>();
      String files = getGeneral(PREF_KEY.WORKING_FILES);
      for(String file: files.split(",")){
        String[] temp = file.split("\\+");
        if(temp.length > 1){
          File f = new File(temp[0], false);
          f.setPath(temp[1]);
          list.add(f);
        }
      }
      return list;
    }
    
    public static void setUserAcountCreated(String account_created) {
      setGeneral(PREF_KEY.USER_ACCOUNT_CREATED, account_created);
    }
    public static String getUserAcountCreated() {
      return getGeneral(PREF_KEY.USER_ACCOUNT_CREATED);
    }
    
    public static void setSelectedRepository(Repository selected_repository) {
      setGeneral(PREF_KEY.SELECTED_REPOSITORY, selected_repository.getName());
    }
    
    public static Repository getSelectedRepository() {
      return new Repository(getGeneral(PREF_KEY.SELECTED_REPOSITORY));
    }
    //-------------------------------
    
    public static String getClientId() {
      return PREF_KEY.CLIENT_ID.getKey();
    }
    
    public static String getClientSecret() {
      return PREF_KEY.CLIENT_SECRET.getKey();
    }

    private static ArrayList<PreferenceListener> prefListener = new ArrayList<PreferenceListener>();
    
    private static void fireStateChanged(PREF_KEY key){
      for(PreferenceListener listener : prefListener){
        listener.preferenceChanged(key);
      }
    }

    public static void addListener(PreferenceListener listener){
      prefListener.add(listener);
    }
    public static boolean removeListener(PreferenceListener listener){
      return prefListener.remove(listener);
    }
    
    public static boolean isConnectedToGitHub()
    {
      return getAccessToken().matches("[\\dA-z]+");
    }
    
    public static boolean hasSelectedRepository()
    {
      return !getSelectedRepository().getName().equals("");
    }
}




Java Source Code List

se.agile.activities.AboutFragment.java
se.agile.activities.BranchFragment.java
se.agile.activities.BranchesFragment.java
se.agile.activities.CardFragment.java
se.agile.activities.CollaboratorsFragment.java
se.agile.activities.CommitFragment.java
se.agile.activities.ConflictFragment.java
se.agile.activities.ConnectToGitHubFragment.java
se.agile.activities.LoginActivity.java
se.agile.activities.MainActivity.java
se.agile.activities.NotificationFragmentSwitcher.java
se.agile.activities.NotificationsFragment.java
se.agile.activities.PersonalNotesFragment.java
se.agile.activities.PokerFragment.java
se.agile.activities.RepositoryOverviewFragment.java
se.agile.activities.SelectRepositoryFragment.java
se.agile.activities.SelectWorkingFilesActivity.java
se.agile.activities.SelectWorkingFilesFragment.java
se.agile.activities.SelectWorkingFilesOverviewFragment.java
se.agile.activities.SettingsFragment.java
se.agile.activities.SignOutFragment.java
se.agile.asynctasks.RequestAccessToken.java
se.agile.asynctasks.RequestAllCommitsForBranch.java
se.agile.asynctasks.RequestAllFilesForBranch.java
se.agile.asynctasks.RequestBranch.java
se.agile.asynctasks.RequestBranches.java
se.agile.asynctasks.RequestFiles.java
se.agile.asynctasks.RequestFullCommit.java
se.agile.asynctasks.RequestListenerAdapter.java
se.agile.asynctasks.RequestListener.java
se.agile.asynctasks.RequestRepositories.java
se.agile.asynctasks.RequestShortCommit.java
se.agile.asynctasks.RequestTask.java
se.agile.asynctasks.RequestUser.java
se.agile.controller.MainActivityController.java
se.agile.controller.settingsFragmentController.java
se.agile.githubdata.Branch.java
se.agile.githubdata.Commit.java
se.agile.githubdata.Directory.java
se.agile.githubdata.File.java
se.agile.githubdata.Folder.java
se.agile.githubdata.GitHubDataInterface.java
se.agile.githubdata.Repository.java
se.agile.githubdata.User.java
se.agile.model.BranchNotification.java
se.agile.model.BranchSelectionModel.java
se.agile.model.CommitNotification.java
se.agile.model.ConflictNotification.java
se.agile.model.CreateNotificationActivity.java
se.agile.model.DirectoryListArrayAdapter.java
se.agile.model.InteractiveArrayAdapter.java
se.agile.model.JSONParser.java
se.agile.model.NotificationDialog.java
se.agile.model.NotificationHandler.java
se.agile.model.NotificationListArrayAdapter.java
se.agile.model.NotificationListener.java
se.agile.model.Notification.java
se.agile.model.PreferenceListener.java
se.agile.model.Preferences.java
se.agile.model.TemporaryStorage.java
se.agile.model.Tuple.java
se.agile.model.WorkingFileListArrayAdapter.java
se.agile.navigator.NavDrawerItem.java
se.agile.navigator.NavDrawerListAdapter.java