Search in sources :

Example 1 with ErrorDialog

use of org.moire.ultrasonic.util.ErrorDialog in project ultrasonic by ultrasonic.

the class ServerSettingsFragment method onPreferenceChange.

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
    if (preference == serverNamePref) {
        sharedPreferences.edit().putString(Constants.PREFERENCES_KEY_SERVER_NAME + serverId, (String) newValue).apply();
        updateName();
        return true;
    } else if (preference == serverUrlPref) {
        final String url = (String) newValue;
        try {
            new URL(url);
            if (!url.equals(url.trim()) || url.contains("@")) {
                throw new Exception();
            }
        } catch (Exception x) {
            new ErrorDialog(getActivity(), R.string.settings_invalid_url, false);
            return false;
        }
        sharedPreferences.edit().putString(Constants.PREFERENCES_KEY_SERVER_URL + serverId, url).apply();
        updateUrl();
        return true;
    } else if (preference == serverUsernamePref) {
        String username = (String) newValue;
        if (username == null || !username.equals(username.trim())) {
            new ErrorDialog(getActivity(), R.string.settings_invalid_username, false);
            return false;
        }
        sharedPreferences.edit().putString(Constants.PREFERENCES_KEY_USERNAME + serverId, username).apply();
        updateUsername();
        return true;
    } else if (preference == serverPasswordPref) {
        sharedPreferences.edit().putString(Constants.PREFERENCES_KEY_PASSWORD + serverId, (String) newValue).apply();
        updatePassword();
        return true;
    } else if (preference == equalizerPref) {
        sharedPreferences.edit().putBoolean(Constants.PREFERENCES_KEY_SERVER_ENABLED + serverId, (Boolean) newValue).apply();
        return true;
    } else if (preference == jukeboxPref) {
        sharedPreferences.edit().putBoolean(Constants.PREFERENCES_KEY_JUKEBOX_BY_DEFAULT + serverId, (Boolean) newValue).apply();
        return true;
    } else if (preference == allowSelfSignedCertificatePref) {
        sharedPreferences.edit().putBoolean(Constants.PREFERENCES_KEY_ALLOW_SELF_SIGNED_CERTIFICATE + serverId, (Boolean) newValue).apply();
        return true;
    } else if (preference == enableLdapUserSupportPref) {
        sharedPreferences.edit().putBoolean(Constants.PREFERENCES_KEY_LDAP_SUPPORT + serverId, (Boolean) newValue).apply();
        return true;
    }
    return false;
}
Also used : ErrorDialog(org.moire.ultrasonic.util.ErrorDialog) URL(java.net.URL)

Example 2 with ErrorDialog

use of org.moire.ultrasonic.util.ErrorDialog in project ultrasonic by ultrasonic.

the class ServerSettingsFragment method testConnection.

private void testConnection() {
    ModalBackgroundTask<Boolean> task = new ModalBackgroundTask<Boolean>(getActivity(), false) {

        private int previousInstance;

        @Override
        protected Boolean doInBackground() throws Throwable {
            updateProgress(R.string.settings_testing_connection);
            final Context context = getActivity();
            previousInstance = Util.getActiveServer(context);
            Util.setActiveServer(context, serverId);
            try {
                MusicService musicService = MusicServiceFactory.getMusicService(context);
                musicService.ping(context, this);
                return musicService.isLicenseValid(context, null);
            } finally {
                Util.setActiveServer(context, previousInstance);
            }
        }

        @Override
        protected void done(Boolean licenseValid) {
            if (licenseValid) {
                Util.toast(getActivity(), R.string.settings_testing_ok);
            } else {
                Util.toast(getActivity(), R.string.settings_testing_unlicensed);
            }
        }

        @Override
        protected void cancel() {
            super.cancel();
            Util.setActiveServer(getActivity(), previousInstance);
        }

        @Override
        protected void error(Throwable error) {
            Log.w(LOG_TAG, error.toString(), error);
            new ErrorDialog(getActivity(), String.format("%s %s", getResources().getString(R.string.settings_connection_failure), getErrorMessage(error)), false);
        }
    };
    task.execute();
}
Also used : Context(android.content.Context) MusicService(org.moire.ultrasonic.service.MusicService) ErrorDialog(org.moire.ultrasonic.util.ErrorDialog) ModalBackgroundTask(org.moire.ultrasonic.util.ModalBackgroundTask)

Aggregations

ErrorDialog (org.moire.ultrasonic.util.ErrorDialog)2 Context (android.content.Context)1 URL (java.net.URL)1 MusicService (org.moire.ultrasonic.service.MusicService)1 ModalBackgroundTask (org.moire.ultrasonic.util.ModalBackgroundTask)1