Java Code Examples for com.gianlu.commonutils.preferences.Prefs#getString()

The following examples show how to use com.gianlu.commonutils.preferences.Prefs#getString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PyxDiscoveryApi.java    From PretendYoureXyzzyAndroid with GNU General Public License v3.0 6 votes vote down vote up
public final void getWelcomeMessage(@Nullable Activity activity, @NonNull Pyx.OnResult<String> listener) {
    String cached = Prefs.getString(PK.WELCOME_MSG_CACHE, null);
    if (cached != null && !CommonUtils.isDebug()) {
        long age = Prefs.getLong(PK.WELCOME_MSG_CACHE_AGE, 0);
        if (System.currentTimeMillis() - age < TimeUnit.HOURS.toMillis(12)) {
            listener.onDone(cached);
            return;
        }
    }

    executor.execute(new LifecycleAwareRunnable(handler, activity == null ? listener : activity) {
        @Override
        public void run() {
            try {
                JSONObject obj = new JSONObject(requestSync(WELCOME_MSG_URL));
                final String msg = obj.getString("msg");
                Prefs.putString(PK.WELCOME_MSG_CACHE, msg);
                Prefs.putLong(PK.WELCOME_MSG_CACHE_AGE, System.currentTimeMillis());
                post(() -> listener.onDone(msg));
            } catch (JSONException | IOException ex) {
                post(() -> listener.onException(ex));
            }
        }
    });
}
 
Example 2
Source File: PrefsJsonStoring.java    From CommonUtils with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public JSONObject getJsonObject(@NonNull String key) throws JSONException {
    String str = Prefs.getString(key, null);
    if (str == null) return null;
    str = new String(Base64.decode(str, Base64.NO_WRAP));
    return new JSONObject(str);
}
 
Example 3
Source File: PrefsJsonStoring.java    From CommonUtils with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public JSONArray getJsonArray(@NonNull String key) throws JSONException {
    String str = Prefs.getString(key, null);
    if (str == null) return null;
    str = new String(Base64.decode(str, Base64.NO_WRAP));
    return new JSONArray(str);
}
 
Example 4
Source File: AnalyticsApplication.java    From CommonUtils with Apache License 2.0 5 votes vote down vote up
@Override
@CallSuper
public void onCreate() {
    super.onCreate();

    String uuid = Prefs.getString(CommonPK.ANALYTICS_USER_ID, null);
    if (uuid == null) {
        uuid = UUID.randomUUID().toString();
        Prefs.putString(CommonPK.ANALYTICS_USER_ID, uuid);
    }

    if (FossUtils.hasFirebaseCrashlytics()) {
        if (Prefs.getBoolean(CommonPK.CRASH_REPORT_ENABLED)) {
            FirebaseCrashlytics.getInstance().setUserId(uuid);
            CRASHLYTICS_ENABLED = true;
        } else {
            CRASHLYTICS_ENABLED = false;
        }
    } else {
        Prefs.putBoolean(CommonPK.CRASH_REPORT_ENABLED, false);
    }

    if (FossUtils.hasFirebaseAnalytics()) {
        ANALYTICS = FirebaseAnalytics.getInstance(this);
        if (Prefs.getBoolean(CommonPK.TRACKING_ENABLED)) {
            ANALYTICS.setUserId(uuid);
            ANALYTICS.setAnalyticsCollectionEnabled(true);
        } else {
            ANALYTICS.setAnalyticsCollectionEnabled(false);
            ANALYTICS = null;
        }
    } else {
        Prefs.putBoolean(CommonPK.TRACKING_ENABLED, false);
    }
}
 
Example 5
Source File: ProfilesManager.java    From Aria2App with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
public MultiProfile getLastProfile() {
    String id = Prefs.getString(PK.LAST_USED_PROFILE, null);
    if (id == null || !profileExists(id)) return null;

    try {
        return retrieveProfile(id);
    } catch (IOException | JSONException ex) {
        Log.e(TAG, "Failed getting profile: " + id, ex);
        return null;
    }
}
 
Example 6
Source File: Pyx.java    From PretendYoureXyzzyAndroid with GNU General Public License v3.0 4 votes vote down vote up
protected void prepareRequest(@NonNull Op operation, @NonNull Request.Builder request) {
    if (operation == Op.FIRST_LOAD) {
        String lastSessionId = Prefs.getString(PK.LAST_JSESSIONID, null);
        if (lastSessionId != null) request.addHeader("Cookie", "JSESSIONID=" + lastSessionId);
    }
}
 
Example 7
Source File: LoadingActivity.java    From PretendYoureXyzzyAndroid with GNU General Public License v3.0 4 votes vote down vote up
private void showRegisterUI(final FirstLoadedPyx pyx) {
    loading.setVisibility(View.GONE);
    register.setVisibility(View.VISIBLE);
    registerNickname.setErrorEnabled(false);
    registerIdCode.setErrorEnabled(false);

    String lastNickname = Prefs.getString(PK.LAST_NICKNAME, null);
    if (lastNickname != null) CommonUtils.setText(registerNickname, lastNickname);

    String lastIdCode = Prefs.getString(PK.LAST_ID_CODE, null);
    if (lastIdCode != null) CommonUtils.setText(registerIdCode, lastIdCode);

    if (!pyx.isServerSecure() && !pyx.config().insecureIdAllowed())
        registerIdCode.setEnabled(false);
    else
        registerIdCode.setEnabled(true);

    registerSubmit.setOnClickListener(v -> {
        loading.setVisibility(View.VISIBLE);
        register.setVisibility(View.GONE);

        String idCode = getIdCode();
        String nick = CommonUtils.getText(registerNickname);
        pyx.register(nick, idCode, this, new Pyx.OnResult<RegisteredPyx>() {
            @Override
            public void onDone(@NonNull RegisteredPyx result) {
                Prefs.putString(PK.LAST_NICKNAME, result.user().nickname);
                Prefs.putString(PK.LAST_ID_CODE, idCode);
                goToMain();
            }

            @Override
            public void onException(@NonNull Exception ex) {
                Log.e(TAG, "Failed registering on server.", ex);

                loading.setVisibility(View.GONE);
                register.setVisibility(View.VISIBLE);

                if (ex instanceof PyxException) {
                    switch (((PyxException) ex).errorCode) {
                        case "rn":
                            registerNickname.setError(getString(R.string.reservedNickname));
                            return;
                        case "in":
                            registerNickname.setError(getString(R.string.invalidNickname));
                            return;
                        case "niu":
                            registerNickname.setError(getString(R.string.alreadyUsedNickname));
                            return;
                        case "tmu":
                            registerNickname.setError(getString(R.string.tooManyUsers));
                            return;
                        case "iid":
                            registerIdCode.setError(getString(R.string.invalidIdCode));
                            return;
                    }
                }

                Log.e(TAG, "Failed registering user on server.", ex);
                Toaster.with(LoadingActivity.this).message(R.string.failedLoading).show();
            }
        });
    });
}