Example usage for android.content SharedPreferences getString

List of usage examples for android.content SharedPreferences getString

Introduction

In this page you can find the example usage for android.content SharedPreferences getString.

Prototype

@Nullable
String getString(String key, @Nullable String defValue);

Source Link

Document

Retrieve a String value from the preferences.

Usage

From source file:com.irccloud.android.GCMIntentService.java

public static String getRegistrationId(Context context) {
    final SharedPreferences prefs = context.getSharedPreferences("prefs", 0);
    String registrationId = prefs.getString("gcm_reg_id", "");
    if (registrationId.length() == 0) {
        Log.i("IRCCloud", "Registration not found.");
        return "";
    }/*from  w  ww.  jav a 2s  .  co  m*/
    // Check if app was updated; if so, it must clear the registration ID
    // since the existing regID is not guaranteed to work with the new
    // app version.
    int registeredVersion = prefs.getInt("gcm_app_version", Integer.MIN_VALUE);
    int currentVersion = getAppVersion();
    if (registeredVersion != currentVersion) {
        Log.i("IRCCloud", "App version changed.");
        return "";
    }
    String build = prefs.getString("gcm_app_build", "");
    if (!Build.FINGERPRINT.equals(build)) {
        Log.i("IRCCloud", "OS version changed.");
        return "";
    }
    return registrationId;
}

From source file:ee.ioc.phon.android.speak.Utils.java

public static String getPrefString(SharedPreferences prefs, Resources res, int key) {
    return prefs.getString(res.getString(key), null);
}

From source file:org.fdroid.enigtext.mms.MmsCommunication.java

protected static MmsConnectionParameters getLocallyConfiguredMmsConnectionParameters(Context context)
        throws ApnUnavailableException {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

    if (preferences.getBoolean(ApplicationPreferencesActivity.ENABLE_MANUAL_MMS_PREF, false)) {
        String mmsc = preferences.getString(ApplicationPreferencesActivity.MMSC_HOST_PREF, null);

        if (mmsc == null)
            throw new ApnUnavailableException("Malformed locally configured MMSC.");

        if (!mmsc.startsWith("http"))
            mmsc = "http://" + mmsc;

        String proxy = preferences.getString(ApplicationPreferencesActivity.MMSC_PROXY_HOST_PREF, null);
        String port = preferences.getString(ApplicationPreferencesActivity.MMSC_PROXY_PORT_PREF, null);

        return new MmsConnectionParameters(mmsc, proxy, port);
    }/*from  w  w w  .  j a v  a 2s . c o  m*/

    throw new ApnUnavailableException("No locally configured parameters available");
}

From source file:io.v.android.apps.syncslides.SignInActivity.java

/**
 * Returns the Google profile information of the signed-in user, or {@code null} if the
 * profile information couldn't be retrieved.
 *///from www  .  j a v  a2  s .  c  o  m
public static JSONObject getUserProfile(Context ctx) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    String userProfileJsonStr = prefs.getString(PREF_USER_PROFILE_JSON, "");
    if (!userProfileJsonStr.isEmpty()) {
        try {
            return new JSONObject(userProfileJsonStr);
        } catch (JSONException e) {
            Log.e(TAG, "Couldn't parse user profile data: " + userProfileJsonStr);
        }
    }
    return null;
}

From source file:be.ac.ucl.lfsab1509.llncampus.ADE.java

/**
 * Start information update from ADE.//  w  ww .j  a va  2s  .  c  om
 * 
 * @param sa
 *            Activity that launches the update thread.
 * @param updateRunnable
 *            Runnable that launches the display update.
 * @param handler
 *            Handler to allow the display update at the end of the execution.
 */
public static void runUpdateADE(final ScheduleActivity sa, final Handler handler,
        final Runnable updateRunnable) {
    final Resources r = sa.getResources();

    final NotificationManager nm = (NotificationManager) sa.getSystemService(Context.NOTIFICATION_SERVICE);

    final NotificationCompat.Builder nb = new NotificationCompat.Builder(sa)
            .setSmallIcon(android.R.drawable.stat_sys_download)
            .setContentTitle(r.getString(R.string.download_from_ADE))
            .setContentText(r.getString(R.string.download_progress)).setAutoCancel(true);

    final NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.setBigContentTitle(r.getString(R.string.download_from_ADE));

    nm.notify(NOTIFY_ID, nb.build());
    new Thread(new Runnable() {
        @Override
        public void run() {

            /*
             * Fetching of code of courses to load.
             */
            ArrayList<Course> courses = Course.getList();

            if (courses == null || courses.isEmpty()) {
                // Take the context of the ScheduleActivity
                SharedPreferences preferences = new SecurePreferences(sa);
                String username = preferences.getString("username", null);
                String password = preferences.getString("password", null);
                if (username != null && password != null) {
                    UCLouvain.downloadCoursesFromUCLouvain(sa, username, password, new Runnable() {
                        public void run() {
                            runUpdateADE(sa, handler, updateRunnable);
                        }
                    }, handler);
                }

            }

            /*
             * Number of weeks. To download all the schedule, the numbers go from 0 to 51
             * (0 = beginning of first semester, 51 = ending of the September exams session).
             */
            String weeks = getWeeks();

            /*
             * Fetching data from ADE and updating the database
             */
            int nbError = 0;
            int i = 0;
            ArrayList<Event> events;
            for (Course course : courses) {
                i++;
                nb.setProgress(courses.size(), i, false);
                nb.setContentText(r.getString(R.string.download_for) + " " + course.getCourseCode() + "...");
                nm.notify(NOTIFY_ID, nb.build());
                events = ADE.getInfo(course.getCourseCode(), weeks);
                if (events == null) {
                    nbError++;
                    inboxStyle.addLine(course.getCourseCode() + " : " + r.getString(R.string.download_error));
                } else {
                    // Removing old data
                    LLNCampus.getDatabase().delete("Horaire", "COURSE = ?",
                            new String[] { course.getCourseCode() });
                    // Adding new data
                    for (Event e : events) {
                        ContentValues cv = e.toContentValues();
                        cv.put("COURSE", course.getCourseCode());
                        if (LLNCampus.getDatabase().insert("Horaire", cv) < 0) {
                            nbError++;
                        }
                    }
                    inboxStyle.addLine(course.getCourseCode() + " : " + events.size() + " "
                            + r.getString(R.string.events));
                    nb.setStyle(inboxStyle);
                }
            }

            nb.setProgress(courses.size(), courses.size(), false);

            if (nbError == 0) {
                nb.setContentText(r.getString(R.string.download_done));
                inboxStyle.setBigContentTitle(r.getString(R.string.download_done));
                nb.setSmallIcon(android.R.drawable.stat_sys_download_done);
                nb.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
            } else {
                nb.setContentText(r.getString(R.string.download_done) + ". " + r.getString(R.string.nb_error)
                        + nbError + " :/");
                inboxStyle.setBigContentTitle(r.getString(R.string.download_done) + ". "
                        + r.getString(R.string.nb_error) + nbError + " :/");
                nb.setSmallIcon(android.R.drawable.stat_notify_error);
                nb.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
            }
            nb.setStyle(inboxStyle);
            nm.notify(NOTIFY_ID, nb.build());

            handler.post(updateRunnable);

        }
    }).start();
}

From source file:ee.ioc.phon.android.speak.Utils.java

public static String getPrefString(SharedPreferences prefs, Resources res, int key, int defaultValue) {
    return prefs.getString(res.getString(key), res.getString(defaultValue));
}

From source file:de.schaeuffelhut.android.openvpn.Preferences.java

public final static String getSuArguments(SharedPreferences sharedPreferences) {
    return sharedPreferences.getString(Preferences.KEY_OPENVPN_SU_ARGUMENTS, "-s -x");
}

From source file:com.atinternet.tracker.LifeCycle.java

static void newSessionInit(SharedPreferences preferences) {
    try {//from   w  w  w .  j  av  a  2 s  .  c om

        updateFirstSession(preferences);
        // Calcul dsfs
        String firstLaunchDate = preferences.getString(FIRST_SESSION_DATE, "");
        if (!TextUtils.isEmpty(firstLaunchDate)) {
            long timeSinceFirstLaunch = simpleDateFormat.parse(firstLaunchDate).getTime();
            preferences.edit().putInt(DAYS_SINCE_FIRST_SESSION,
                    Tool.getDaysBetweenTimes(System.currentTimeMillis(), timeSinceFirstLaunch)).apply();
        }

        // Calcul dsu
        String firstLaunchDateAfterUpdate = preferences.getString(FIRST_SESSION_DATE_AFTER_UPDATE, "");
        if (!TextUtils.isEmpty(firstLaunchDateAfterUpdate)) {
            long timeSinceFirstLaunchAfterUpdate = simpleDateFormat.parse(firstLaunchDateAfterUpdate).getTime();
            preferences.edit().putInt(DAYS_SINCE_UPDATE,
                    Tool.getDaysBetweenTimes(System.currentTimeMillis(), timeSinceFirstLaunchAfterUpdate))
                    .apply();
        }

        // Calcul dsls
        String lastLaunchDate = preferences.getString(LAST_SESSION_DATE, "");
        if (!TextUtils.isEmpty(lastLaunchDate)) {
            long timeSinceLastUse = simpleDateFormat.parse(lastLaunchDate).getTime();
            preferences.edit().putInt(DAYS_SINCE_LAST_SESSION,
                    Tool.getDaysBetweenTimes(System.currentTimeMillis(), timeSinceLastUse)).apply();
        }
        preferences.edit().putString(LAST_SESSION_DATE, simpleDateFormat.format(new Date())).apply();

        // sc
        preferences.edit().putInt(SESSION_COUNT, preferences.getInt(SESSION_COUNT, 0) + 1).apply();

        // Calcul scsu
        preferences.edit()
                .putInt(SESSION_COUNT_SINCE_UPDATE, preferences.getInt(SESSION_COUNT_SINCE_UPDATE, 0) + 1)
                .apply();

        // Application version changed
        String savedApvr = preferences.getString(VERSION_CODE_KEY, "");
        // Update detected
        if (!versionCode.equals(savedApvr)) {
            preferences.edit().putString(FIRST_SESSION_DATE_AFTER_UPDATE, simpleDateFormat.format(new Date()))
                    .putString(VERSION_CODE_KEY, versionCode).putInt(SESSION_COUNT_SINCE_UPDATE, 1)
                    .putInt(DAYS_SINCE_UPDATE, 0).putBoolean(FIRST_SESSION_AFTER_UPDATE, true).apply();
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    sessionId = UUID.randomUUID().toString();
}

From source file:com.microsoft.activitytracker.Core.NetworkCalls.java

/***
 * Based on the soap action build the restadapter. Since both have differently structured xml responses we need
 * choose which parser to use./*from   w  w  w. ja v a2s . c o m*/
 * @param soapAction pass the soap action so we can determine which parser to use with the xml that is returned
 * @param appPrefs app prefs so we can pull the endpoint for the restadapter
 * @return returns the restadapter that we are going to use for this SOAP request
 */
private static RestAdapter buildSoapRestAdapter(String soapAction, SharedPreferences appPrefs) {
    if (soapAction.equals(Constants.SOAP_ACTION_RETRIEVE_MULTIPLE)) {
        return new RestAdapter.Builder()
                //                    .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(appPrefs.getString(Constants.ENDPOINT, ""))
                .setConverter(new SoapRetrieveMultipleParser())
                .setRequestInterceptor(ActivityTracker.getRequestInterceptor()).build();
    } else if (soapAction.equals(Constants.SOAP_ACTION_EXECUTE)) {
        return new RestAdapter.Builder()
                //                    .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(appPrefs.getString(Constants.ENDPOINT, "")).setConverter(new SoapExecuteParser())
                .setRequestInterceptor(ActivityTracker.getRequestInterceptor()).build();
    } else {
        return null;
    }
}

From source file:com.fusionx.lightirc.util.SharedPreferencesUtils.java

private static ServerConfiguration.Builder convertPrefsToBuilder(final Context context, final String filename) {
    final SharedPreferences serverSettings = context.getSharedPreferences(filename, MODE_PRIVATE);
    final ServerConfiguration.Builder builder = new ServerConfiguration.Builder();

    // Server connection
    builder.setTitle(serverSettings.getString(PreferenceConstants.PREF_TITLE, ""));
    builder.setUrl(serverSettings.getString(PreferenceConstants.PREF_URL, "").trim());
    builder.setPort(Integer.parseInt(serverSettings.getString(PreferenceConstants.PREF_PORT, "6667")));

    // SSL/*from w  w  w . j a va 2s.  co  m*/
    builder.setSsl(serverSettings.getBoolean(PreferenceConstants.PREF_SSL, false));
    builder.setSslAcceptAllCertificates(
            serverSettings.getBoolean(PreferenceConstants.PREF_SSL_ACCEPT_ALL_CONNECTIONS, false));

    // User settings
    final String firstChoice = serverSettings.getString(PreferenceConstants.PREF_NICK, "HoloIRCUser");
    final String secondChoice = serverSettings.getString(PreferenceConstants.PREF_SECOND_NICK, "");
    final String thirdChoice = serverSettings.getString(PreferenceConstants.PREF_THIRD_NICK, "");
    final NickStorage nickStorage = new NickStorage(firstChoice, secondChoice, thirdChoice);
    builder.setNickStorage(nickStorage);
    builder.setRealName(serverSettings.getString(PreferenceConstants.PREF_REALNAME, "HoloIRC"));
    builder.setNickChangeable(serverSettings.getBoolean(PreferenceConstants.PREF_AUTO_NICK, true));

    // Autojoin channels
    final ArrayList<String> auto = new ArrayList<String>(
            serverSettings.getStringSet(PreferenceConstants.PREF_AUTOJOIN, new HashSet<String>()));
    for (final String channel : auto) {
        builder.getAutoJoinChannels().add(channel);
    }

    // Server authorisation
    builder.setServerUserName(serverSettings.getString(PreferenceConstants.PREF_LOGIN_USERNAME, "holoirc"));
    builder.setServerPassword(serverSettings.getString(PreferenceConstants.PREF_LOGIN_PASSWORD, ""));

    // SASL authorisation
    builder.setSaslUsername(serverSettings.getString(PreferenceConstants.PREF_SASL_USERNAME, ""));
    builder.setSaslPassword(serverSettings.getString(PreferenceConstants.PREF_SASL_PASSWORD, ""));

    // NickServ authorisation
    builder.setNickservPassword(serverSettings.getString(PreferenceConstants.PREF_NICKSERV_PASSWORD, ""));
    return builder;
}