Example usage for android.preference CheckBoxPreference setEnabled

List of usage examples for android.preference CheckBoxPreference setEnabled

Introduction

In this page you can find the example usage for android.preference CheckBoxPreference setEnabled.

Prototype

public void setEnabled(boolean enabled) 

Source Link

Document

Sets whether this Preference is enabled.

Usage

From source file:bander.notepad.PrefsFragment.java

private void setSortAscendingEnabled(boolean enabled) {
    CheckBoxPreference sortAscendingPreference = (CheckBoxPreference) findPreference(KEY_SORTASCENDING);
    sortAscendingPreference.setEnabled(enabled);
    if (!enabled)
        sortAscendingPreference.setChecked(true);
}

From source file:com.tinfoil.sms.settings.QuickPrefsActivity.java

@TargetApi(Build.VERSION_CODES.KITKAT)
private void setKitKatPref() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        CheckBoxPreference p = (CheckBoxPreference) findPreference(NATIVE_SAVE_SETTING_KEY);
        p.setChecked(false);/*w  w  w. j  av a  2 s .  c o m*/
        p.setEnabled(false);
    }
}

From source file:com.chess.genesis.activity.Settings.java

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this;

    final Pref pref = new Pref(this);

    // set layout mode
    final boolean isTablet = pref.getBool(R.array.pf_tabletMode);

    if (isTablet)
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    else/*ww  w  . j  a  v a2s  .co m*/
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    setContentView(R.layout.activity_settings);
    addPreferencesFromResource(R.xml.settings);

    net = new NetworkClient(this, handle);
    progress = new ProgressMsg(this);

    final View button = findViewById(R.id.topbar_genesis);
    button.setOnLongClickListener(this);

    final boolean isLoggedin = pref.getBool(R.array.pf_isLoggedIn);

    final Preference bench = findPreference("benchmark");
    bench.setOnPreferenceClickListener(this);

    final IntListPreference poll = (IntListPreference) findPreference(pref.key(R.array.pf_notifierPolling));
    poll.setOnPreferenceChangeListener(this);

    CheckBoxPreference check = (CheckBoxPreference) findPreference(pref.key(R.array.pf_emailNoteEnabled));
    check.setOnPreferenceChangeListener(this);
    check.setEnabled(isLoggedin);

    check = (CheckBoxPreference) findPreference(pref.key(R.array.pf_noteEnabled));
    check.setOnPreferenceChangeListener(this);
    check.setEnabled(isLoggedin);

    CallBackPreference callbackPref = (CallBackPreference) findPreference("deleteLocalTable");
    callbackPref.setCallBack(this);
    callbackPref = (CallBackPreference) findPreference("resyncOnlineTable");
    callbackPref.setCallBack(this);
    callbackPref.setEnabled(isLoggedin);
    callbackPref = (CallBackPreference) findPreference("resyncArchiveTable");
    callbackPref.setCallBack(this);
    callbackPref.setEnabled(isLoggedin);
    callbackPref = (CallBackPreference) findPreference("resyncMsgTable");
    callbackPref.setCallBack(this);
    callbackPref.setEnabled(isLoggedin);
    callbackPref = (CallBackPreference) findPreference("bcReset");
    callbackPref.setCallBack(this);

    // Set email note value from server
    if (pref.getBool(R.array.pf_isLoggedIn)) {
        progress.setText("Retrieving Settings");
        net.get_option("emailnote");
        new Thread(net).start();
    }
}

From source file:de.tum.in.tumcampus.fragments.SettingsFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);

    mContext = getActivity();/*  w  w  w  . j a v a  2  s  . co  m*/

    // Disables silence service if the app is used without TUMOnline access
    CheckBoxPreference silent = (CheckBoxPreference) findPreference("silent_mode");
    if (!new AccessTokenManager(mContext).hasValidAccessToken()) {
        silent.setEnabled(false);
    }

    // Click listener for preference list entries. Used to simulate a button
    // (since it is not possible to add a button to the preferences screen)
    findPreference("button_wizard").setOnPreferenceClickListener(this);
    findPreference("button_clear_cache").setOnPreferenceClickListener(this);
    findPreference("facebook").setOnPreferenceClickListener(this);
    findPreference("github").setOnPreferenceClickListener(this);
    findPreference("first_run").setOnPreferenceClickListener(this);
    findPreference("licenses").setOnPreferenceClickListener(this);
    findPreference("feedback").setOnPreferenceClickListener(this);
    findPreference("privacy").setOnPreferenceClickListener(this);

    // Set summary for these preferences
    setSummary("card_cafeteria_default_G");
    setSummary("card_cafeteria_default_K");
    setSummary("card_cafeteria_default_W");
    setSummary("card_role");
    setSummary("card_stations_default_G");
    setSummary("card_stations_default_C");
    setSummary("card_stations_default_K");
    setSummary("card_default_campus");
    setSummary("silent_mode_set_to");
    setSummary("background_mode_set_to");

    // Register the change listener to react immediately on changes
    PreferenceManager.getDefaultSharedPreferences(mContext).registerOnSharedPreferenceChangeListener(this);

    // Populate news sources
    populateNewsSources();

    // Open a card's preference screen if selected from it's context menu
    Bundle bundle = getArguments();
    if (bundle != null && bundle.containsKey(Const.PREFERENCE_SCREEN)) {
        final String key = bundle.getString(Const.PREFERENCE_SCREEN);

        PreferenceScreen screen = (PreferenceScreen) findPreference("cards_pref_container");
        final PreferenceScreen cardPreferenceScreen = (PreferenceScreen) findPreference(key);
        cardPreferenceScreen.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                initializeActionBar(cardPreferenceScreen);
                return true;
            }
        });

        final ListAdapter listAdapter = screen.getRootAdapter();
        final int itemsCount = listAdapter.getCount();
        for (int i = 0; i < itemsCount; ++i) {
            if (listAdapter.getItem(i).equals(cardPreferenceScreen)) {
                screen.onItemClick(null, null, i, 0);
                break;
            }
        }
    }
}

From source file:fi.iki.murgo.irssinotifier.SettingsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "Opened settings");
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preference_screen);

    final Context ctx = this;

    final CheckBoxPreference enabled = (CheckBoxPreference) findPreference("NotificationsEnabled");
    enabled.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            final boolean checked = (Boolean) newValue;
            String s = "Disabling notifications...";
            if (checked) {
                s = "Enabling notifications...";
            }// w  w  w  .jav  a 2 s . c o  m

            SettingsSendingTask task = new SettingsSendingTask(SettingsActivity.this, "Sending settings", s);
            task.setCallback(new Callback<ServerResponse>() {
                @Override
                public void doStuff(ServerResponse result) {
                    if (result.getException() != null) {
                        if (result.getException() instanceof IOException) {
                            MessageBox.Show(ctx, "Network error",
                                    "Ensure your internet connection works and try again.", null);
                        } else if (result.getException() instanceof AuthenticationException) {
                            MessageBox.Show(ctx, "Authentication error", "Unable to authenticate to server.",
                                    null);
                        } else if (result.getException() instanceof ServerException) {
                            MessageBox.Show(ctx, "Server error",
                                    "Mystical server error, check if updates are available", null);
                        } else {
                            MessageBox.Show(ctx, null,
                                    "Unable to send settings to the server! Please try again later!", null);
                        }
                        enabled.setChecked(!checked);
                    }
                }
            });

            task.execute();

            return true;
        }
    });

    Preference aboutPref = findPreference("about");
    aboutPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            Intent i = new Intent(ctx, AboutActivity.class);
            startActivity(i);

            finish();
            return true;
        }
    });

    Preference channelsPref = findPreference("channels");
    channelsPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            Intent i = new Intent(ctx, ChannelSettingsActivity.class);
            startActivity(i);
            return true;
        }
    });

    ListPreference mode = (ListPreference) findPreference("notificationModeString");
    mode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            NotificationMode m = NotificationMode.PerChannel;
            String v = (String) newValue;
            if (v.equals(ctx.getResources().getStringArray(R.array.notification_modes)[0]))
                m = NotificationMode.Single;
            if (v.equals(ctx.getResources().getStringArray(R.array.notification_modes)[1]))
                m = NotificationMode.PerChannel;
            if (v.equals(ctx.getResources().getStringArray(R.array.notification_modes)[2]))
                m = NotificationMode.PerMessage;

            Preferences p = new Preferences(ctx);
            p.setNotificationMode(m);
            return true;
        }
    });

    Preference initialSettingsPref = findPreference("redoInitialSettings");
    initialSettingsPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            Preferences prefs = new Preferences(ctx);
            prefs.setAuthToken(null);
            prefs.setAccountName(null);
            prefs.setGcmRegistrationId(null);
            prefs.setLicenseCount(0);

            IrssiNotifierActivity.refreshIsNeeded();
            finish();
            return true;
        }
    });

    Preference disableThemePref = findPreference("ThemeDisabled");
    disableThemePref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            IrssiNotifierActivity.refreshIsNeeded();
            return true;
        }
    });

    handleColorPicker();

    handleIcb();

    if (!LicenseHelper.isPlusVersion(this)) {
        CheckBoxPreference usePullMechanismPref = (CheckBoxPreference) findPreference("UsePullMechanism");
        usePullMechanismPref.setSummary(usePullMechanismPref.getSummary() + ". Only in Plus version.");
        usePullMechanismPref.setEnabled(false);
        usePullMechanismPref.setChecked(false);
    }
}

From source file:net.willwebberley.gowertides.ui.PrferencesActivity.java

public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
    CheckBoxPreference timeBox = (CheckBoxPreference) getPreferenceScreen().findPreference("show_graph_time");
    CheckBoxPreference sunriseSunsetBox = (CheckBoxPreference) getPreferenceScreen()
            .findPreference("show_graph_sunrise_sunset");
    ListPreference metric = (ListPreference) getPreferenceScreen().findPreference("unitFormat");
    CheckBoxPreference timerBox = (CheckBoxPreference) getPreferenceScreen()
            .findPreference("show_sunset_timer");

    if (!arg0.getBoolean("show_graph", true)) {
        timeBox.setEnabled(false);
        sunriseSunsetBox.setEnabled(false);
    } else {/*from   w  ww  .j  av a 2s  .c  o m*/
        timeBox.setEnabled(true);
        sunriseSunsetBox.setEnabled(true);
    }

    if (arg0.getString("unitFormat", "true").equals("true")) {
        metric.setSummary("Display weather units in metric (km/h, Celcius).");
    } else {
        metric.setSummary("Display weather units in imperial (mph, Faranheit).");
    }

    if (!arg0.getBoolean("show_sunrise_sunset", true)) {
        timerBox.setEnabled(false);
    } else {
        timerBox.setEnabled(true);
    }

}

From source file:com.android.settings.locationprivacy.LocationPrivacyAdvancedSettings.java

private void refresh() {
    for (int i = 0; i < screen.getPreferenceCount(); i++) {
        Preference pref = screen.getPreference(i);
        if (pref != null) {
            System.out.println(pref);
            String key = pref.getKey();
            if (key != null) {
                System.out.println(key);
                if (key.equals("lp_settings_advanced_useonlinealgorithm")) {
                    CheckBoxPreference checkPref = (CheckBoxPreference) pref;
                    checkPref.setChecked(lpManager.isUseOnlineAlgorithm());
                } else if (key.equals("lp_settings_advanced_shareprivacyaettings")) {
                    CheckBoxPreference checkPref = (CheckBoxPreference) pref;
                    checkPref.setChecked(lpManager.isSharePrivacySettings());
                    checkPref.setEnabled(GooglePlayServicesUtil
                            .isGooglePlayServicesAvailable(getActivity()) == ConnectionResult.SUCCESS
                            && lpManager.isWebhostShareSettings()
                            && AccountManager.get(getActivity()).getAccountsByType("com.google").length >= 1);
                } else if (key.equals("lp_settings_advanced_showcommunityadvice")) {
                    CheckBoxPreference checkPref = (CheckBoxPreference) pref;
                    checkPref.setChecked(lpManager.isShowCommunityAdvice());
                    checkPref.setEnabled(lpManager.isShowCommunityAdvice());
                } else if (key.equals("lp_settings_advanced_stars")) {
                    CheckBoxPreference checkPref = (CheckBoxPreference) pref;
                    checkPref.setChecked(lpManager.isUseStarsInDialog());
                } else if (key.equals("lp_settings_advanced_webservice")) {
                    EditTextPreference editTextPref = (EditTextPreference) pref;
                    editTextPref.setText("" + lpManager.getWebserviceHostAdress());
                } else if (key.equals("lp_settings_advanced_setpreset_min")) {
                    EditTextPreference editTextPref = (EditTextPreference) pref;
                    editTextPref.setText("" + lpManager.getMinDistance());
                }//w  w w  . j  a v a2  s .  c o m
                pref.setOnPreferenceChangeListener(this);
                pref.setOnPreferenceClickListener(this);
            }

        }
    }

}

From source file:de.Maxr1998.xposed.maxlock.ui.settings.SettingsFragment.java

private void setupPro() {
    String version;/*  w  ww .  j a v a 2  s.co m*/
    try {
        version = " v" + getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(),
                0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        version = "Error getting app version";
        e.printStackTrace();
    }

    CheckBoxPreference ep = (CheckBoxPreference) findPreference(Common.ENABLE_PRO);
    String appName;
    if (BillingHelper.donated(getActivity().getApplicationContext(), getBp())) {
        appName = getString(R.string.app_name_pro);
        PREFS.edit().putBoolean(Common.ENABLE_PRO, true).apply();
        ep.setEnabled(false);
        ep.setChecked(true);
    } else {
        if (PREFS.getBoolean(Common.ENABLE_PRO, false))
            appName = getString(R.string.app_name_pseudo_pro);
        else
            appName = getString(R.string.app_name);
    }
    if (Util.isDevMode()) {
        appName = getString(R.string.app_name) + " Indev";
    }
    getActivity().setTitle(appName);

}

From source file:org.linphone.AccountPreferencesFragment.java

private void initAccountPreferencesFields(PreferenceScreen parent) {
    boolean isDefaultAccount = mPrefs.getDefaultAccountIndex() == n;

    accountCreator = LinphoneCoreFactory.instance().createAccountCreator(LinphoneManager.getLc(),
            LinphonePreferences.instance().getXmlrpcUrl());
    accountCreator.setListener(this);

    PreferenceCategory account = (PreferenceCategory) getPreferenceScreen()
            .findPreference(getString(R.string.pref_sipaccount_key));
    EditTextPreference username = (EditTextPreference) account.getPreference(0);
    username.setOnPreferenceChangeListener(usernameChangedListener);
    if (!isNewAccount) {
        username.setText(mPrefs.getAccountUsername(n));
        username.setSummary(username.getText());
    }/* w ww.j a v a  2  s  . c  om*/

    EditTextPreference userid = (EditTextPreference) account.getPreference(1);
    userid.setOnPreferenceChangeListener(useridChangedListener);
    if (!isNewAccount) {
        userid.setText(mPrefs.getAccountUserId(n));
        userid.setSummary(userid.getText());
    }

    EditTextPreference password = (EditTextPreference) account.getPreference(2);
    password.setOnPreferenceChangeListener(passwordChangedListener);
    if (!isNewAccount) {
        password.setText(mPrefs.getAccountPassword(n));
    }

    EditTextPreference domain = (EditTextPreference) account.getPreference(3);
    domain.setOnPreferenceChangeListener(domainChangedListener);
    if (!isNewAccount) {
        domain.setText(mPrefs.getAccountDomain(n));
        domain.setSummary(domain.getText());
    }

    EditTextPreference displayName = (EditTextPreference) account.getPreference(4);
    displayName.setOnPreferenceChangeListener(displayNameChangedListener);
    if (!isNewAccount) {
        displayName.setText(mPrefs.getAccountDisplayName(n));
        displayName.setSummary(displayName.getText());
    }

    PreferenceCategory advanced = (PreferenceCategory) getPreferenceScreen()
            .findPreference(getString(R.string.pref_advanced_key));
    mTransportPreference = (ListPreference) advanced.getPreference(0);
    initializeTransportPreference(mTransportPreference);
    mTransportPreference.setOnPreferenceChangeListener(transportChangedListener);
    if (!isNewAccount) {
        mTransportPreference.setSummary(mPrefs.getAccountTransportString(n));
    }

    mProxyPreference = (EditTextPreference) advanced.getPreference(1);
    mProxyPreference.setOnPreferenceChangeListener(proxyChangedListener);
    if (!isNewAccount) {
        mProxyPreference.setText(mPrefs.getAccountProxy(n));
        mProxyPreference
                .setSummary("".equals(mProxyPreference.getText()) || (mProxyPreference.getText() == null)
                        ? getString(R.string.pref_help_proxy)
                        : mProxyPreference.getText());
    }

    CheckBoxPreference outboundProxy = (CheckBoxPreference) advanced.getPreference(2);
    outboundProxy.setOnPreferenceChangeListener(outboundProxyChangedListener);
    if (!isNewAccount) {
        outboundProxy.setChecked(mPrefs.isAccountOutboundProxySet(n));
    }

    EditTextPreference expires = (EditTextPreference) advanced.getPreference(3);
    expires.setOnPreferenceChangeListener(expiresChangedListener);
    if (!isNewAccount) {
        expires.setText(mPrefs.getExpires(n));
        expires.setSummary(mPrefs.getExpires(n));
    }

    EditTextPreference prefix = (EditTextPreference) advanced.getPreference(4);
    prefix.setOnPreferenceChangeListener(prefixChangedListener);
    if (!isNewAccount) {
        String prefixValue = mPrefs.getPrefix(n);
        prefix.setText(prefixValue);
        prefix.setSummary(prefixValue);
    }

    CheckBoxPreference avpf = (CheckBoxPreference) advanced.getPreference(5);
    avpf.setOnPreferenceChangeListener(avpfChangedListener);
    if (!isNewAccount) {
        avpf.setChecked(mPrefs.avpfEnabled(n));
    }

    EditTextPreference avpfRRInterval = (EditTextPreference) advanced.getPreference(6);
    avpfRRInterval.setOnPreferenceChangeListener(avpfRRIntervalChangedListener);
    if (!isNewAccount) {
        avpfRRInterval.setText(mPrefs.getAvpfRRInterval(n));
        avpfRRInterval.setSummary(mPrefs.getAvpfRRInterval(n));
    }

    CheckBoxPreference escape = (CheckBoxPreference) advanced.getPreference(7);
    escape.setOnPreferenceChangeListener(escapeChangedListener);
    if (!isNewAccount) {
        escape.setChecked(mPrefs.getReplacePlusByZeroZero(n));
    }

    Preference linkAccount = advanced.getPreference(8);
    linkAccount.setOnPreferenceClickListener(linkAccountListener);

    PreferenceCategory manage = (PreferenceCategory) getPreferenceScreen()
            .findPreference(getString(R.string.pref_manage_key));
    final CheckBoxPreference disable = (CheckBoxPreference) manage.getPreference(0);
    disable.setEnabled(true);
    disable.setOnPreferenceChangeListener(disableChangedListener);
    if (!isNewAccount) {
        disable.setChecked(!mPrefs.isAccountEnabled(n));
    }

    CheckBoxPreference mainAccount = (CheckBoxPreference) manage.getPreference(1);
    mainAccount.setChecked(isDefaultAccount);
    mainAccount.setEnabled(!mainAccount.isChecked());
    mainAccount.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            mPrefs.setDefaultAccount(n);
            disable.setEnabled(false);
            disable.setChecked(false);
            preference.setEnabled(false);
            return true;
        }
    });
    if (!isNewAccount) {
        mainAccount.setEnabled(!mainAccount.isChecked());
    }

    final Preference changePassword = manage.getPreference(2);
    if (mPrefs.getAccountDomain(n).compareTo(getString(R.string.default_domain)) == 0) {
        changePassword.setEnabled(!isNewAccount);
        changePassword.setOnPreferenceClickListener(new OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {
                final AlertDialog.Builder alert = new AlertDialog.Builder(LinphoneActivity.instance());
                LayoutInflater inflater = LinphoneActivity.instance().getLayoutInflater();
                View layout = inflater.inflate(R.layout.new_password, null);
                final EditText pass1 = (EditText) layout.findViewById(R.id.password1);
                final EditText pass2 = (EditText) layout.findViewById(R.id.password2);
                alert.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        LinphoneAccountCreator.PasswordCheck status = accountCreator
                                .setPassword(pass1.getText().toString());
                        if (status.equals(LinphoneAccountCreator.PasswordCheck.Ok)) {
                            if (pass1.getText().toString().compareTo(pass2.getText().toString()) == 0) {
                                accountCreator.setUsername(mPrefs.getAccountUsername(n));
                                accountCreator.setHa1(mPrefs.getAccountHa1(n));
                                LinphoneAccountCreator.RequestStatus req_status = accountCreator
                                        .updatePassword(pass1.getText().toString());
                                if (!req_status.equals(LinphoneAccountCreator.RequestStatus.Ok)) {
                                    LinphoneUtils.displayErrorAlert(
                                            LinphoneUtils.errorForRequestStatus(req_status),
                                            LinphoneActivity.instance());
                                } else {
                                    progress = ProgressDialog.show(LinphoneActivity.instance(), null, null);
                                    Drawable d = new ColorDrawable(ContextCompat
                                            .getColor(LinphoneActivity.instance(), R.color.colorE));
                                    d.setAlpha(200);
                                    progress.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                                            WindowManager.LayoutParams.MATCH_PARENT);
                                    progress.getWindow().setBackgroundDrawable(d);
                                    progress.setContentView(R.layout.progress_dialog);
                                    progress.show();
                                }
                            } else {
                                LinphoneUtils.displayErrorAlert(getString(R.string.wizard_passwords_unmatched),
                                        LinphoneActivity.instance());
                            }
                            return;
                        }
                        LinphoneUtils.displayErrorAlert(LinphoneUtils.errorForPasswordStatus(status),
                                LinphoneActivity.instance());
                    }
                });

                alert.setView(layout);
                alert.show();
                return true;
            }
        });
    } else {
        changePassword.setEnabled(false);
    }

    final Preference delete = manage.getPreference(3);
    delete.setEnabled(!isNewAccount);
    delete.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            mPrefs.deleteAccount(n);
            LinphoneActivity.instance().refreshAccounts();
            LinphoneActivity.instance().displaySettings();
            return true;
        }
    });
}

From source file:co.taqat.call.AccountPreferencesFragment.java

private void initAccountPreferencesFields(PreferenceScreen parent) {
    boolean isDefaultAccount = mPrefs.getDefaultAccountIndex() == n;

    accountCreator = LinphoneCoreFactory.instance().createAccountCreator(LinphoneManager.getLc(),
            LinphonePreferences.instance().getXmlrpcUrl());
    accountCreator.setListener(this);

    PreferenceCategory account = (PreferenceCategory) getPreferenceScreen()
            .findPreference(getString(R.string.pref_sipaccount_key));
    EditTextPreference username = (EditTextPreference) account.getPreference(0);
    username.setOnPreferenceChangeListener(usernameChangedListener);
    if (!isNewAccount) {
        username.setText(mPrefs.getAccountUsername(n));
        username.setSummary(username.getText());
    }//from  ww  w  . j a  v  a2  s. c om

    EditTextPreference userid = (EditTextPreference) account.getPreference(1);
    userid.setOnPreferenceChangeListener(useridChangedListener);
    if (!isNewAccount) {
        userid.setText(mPrefs.getAccountUserId(n));
        userid.setSummary(userid.getText());
    }

    EditTextPreference password = (EditTextPreference) account.getPreference(2);
    password.setOnPreferenceChangeListener(passwordChangedListener);
    if (!isNewAccount) {
        password.setText(mPrefs.getAccountPassword(n));
    }

    EditTextPreference domain = (EditTextPreference) account.getPreference(3);
    domain.setOnPreferenceChangeListener(domainChangedListener);
    if (!isNewAccount) {
        domain.setText(mPrefs.getAccountDomain(n));
        domain.setSummary(domain.getText());
    }

    EditTextPreference displayName = (EditTextPreference) account.getPreference(4);
    displayName.setOnPreferenceChangeListener(displayNameChangedListener);
    if (!isNewAccount) {
        displayName.setText(mPrefs.getAccountDisplayName(n));
        displayName.setSummary(displayName.getText());
    }

    PreferenceCategory advanced = (PreferenceCategory) getPreferenceScreen()
            .findPreference(getString(R.string.pref_advanced_key));
    mTransportPreference = (ListPreference) advanced.getPreference(0);
    initializeTransportPreference(mTransportPreference);
    mTransportPreference.setOnPreferenceChangeListener(transportChangedListener);
    if (!isNewAccount) {
        mTransportPreference.setSummary(mPrefs.getAccountTransportString(n));
    }

    mProxyPreference = (EditTextPreference) advanced.getPreference(1);
    mProxyPreference.setOnPreferenceChangeListener(proxyChangedListener);
    if (!isNewAccount) {
        mProxyPreference.setText(mPrefs.getAccountProxy(n));
        mProxyPreference
                .setSummary("".equals(mProxyPreference.getText()) || (mProxyPreference.getText() == null)
                        ? getString(R.string.pref_help_proxy)
                        : mProxyPreference.getText());
    }

    CheckBoxPreference outboundProxy = (CheckBoxPreference) advanced.getPreference(2);
    outboundProxy.setOnPreferenceChangeListener(outboundProxyChangedListener);
    if (!isNewAccount) {
        outboundProxy.setChecked(mPrefs.isAccountOutboundProxySet(n));
    }

    EditTextPreference expires = (EditTextPreference) advanced.getPreference(3);
    expires.setOnPreferenceChangeListener(expiresChangedListener);
    if (!isNewAccount) {
        expires.setText(mPrefs.getExpires(n));
        expires.setSummary(mPrefs.getExpires(n));
    }

    EditTextPreference prefix = (EditTextPreference) advanced.getPreference(4);
    prefix.setOnPreferenceChangeListener(prefixChangedListener);
    if (!isNewAccount) {
        String prefixValue = mPrefs.getPrefix(n);
        prefix.setText(prefixValue);
        prefix.setSummary(prefixValue);
    }

    CheckBoxPreference avpf = (CheckBoxPreference) advanced.getPreference(5);
    avpf.setOnPreferenceChangeListener(avpfChangedListener);
    if (!isNewAccount) {
        avpf.setChecked(mPrefs.avpfEnabled(n));
    }

    EditTextPreference avpfRRInterval = (EditTextPreference) advanced.getPreference(6);
    avpfRRInterval.setOnPreferenceChangeListener(avpfRRIntervalChangedListener);
    if (!isNewAccount) {
        avpfRRInterval.setText(mPrefs.getAvpfRRInterval(n));
        avpfRRInterval.setSummary(mPrefs.getAvpfRRInterval(n));
    }

    CheckBoxPreference escape = (CheckBoxPreference) advanced.getPreference(7);
    escape.setOnPreferenceChangeListener(escapeChangedListener);
    if (!isNewAccount) {
        escape.setChecked(mPrefs.getReplacePlusByZeroZero(n));
    }

    CheckBoxPreference friendlistSubscribe = (CheckBoxPreference) advanced.getPreference(8);
    friendlistSubscribe.setOnPreferenceChangeListener(friendlistSubscribeListener);
    if (!isNewAccount) {
        friendlistSubscribe.setChecked(mPrefs.isFriendlistsubscriptionEnabled());
    }

    Preference linkAccount = advanced.getPreference(9);
    linkAccount.setOnPreferenceClickListener(linkAccountListener);

    PreferenceCategory manage = (PreferenceCategory) getPreferenceScreen()
            .findPreference(getString(R.string.pref_manage_key));
    final CheckBoxPreference disable = (CheckBoxPreference) manage.getPreference(0);
    disable.setEnabled(true);
    disable.setOnPreferenceChangeListener(disableChangedListener);
    if (!isNewAccount) {
        disable.setChecked(!mPrefs.isAccountEnabled(n));
    }

    CheckBoxPreference mainAccount = (CheckBoxPreference) manage.getPreference(1);
    mainAccount.setChecked(isDefaultAccount);
    mainAccount.setEnabled(!mainAccount.isChecked());
    mainAccount.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            mPrefs.setDefaultAccount(n);
            disable.setEnabled(false);
            disable.setChecked(false);
            preference.setEnabled(false);
            return true;
        }
    });
    if (!isNewAccount) {
        mainAccount.setEnabled(!mainAccount.isChecked());
    }

    final Preference changePassword = manage.getPreference(2);
    if (mPrefs.getAccountDomain(n).compareTo(getString(R.string.default_domain)) == 0) {
        changePassword.setEnabled(!isNewAccount);
        changePassword.setOnPreferenceClickListener(new OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {
                final AlertDialog.Builder alert = new AlertDialog.Builder(LinphoneActivity.instance());
                LayoutInflater inflater = LinphoneActivity.instance().getLayoutInflater();
                View layout = inflater.inflate(R.layout.new_password, null);
                final EditText pass1 = (EditText) layout.findViewById(R.id.password1);
                final EditText pass2 = (EditText) layout.findViewById(R.id.password2);
                alert.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        LinphoneAccountCreator.Status status = accountCreator
                                .setPassword(pass1.getText().toString());
                        if (status.equals(LinphoneAccountCreator.Status.Ok)) {
                            if (pass1.getText().toString().compareTo(pass2.getText().toString()) == 0) {
                                accountCreator.setUsername(mPrefs.getAccountUsername(n));
                                accountCreator.setHa1(mPrefs.getAccountHa1(n));
                                status = accountCreator.updatePassword(pass1.getText().toString());
                                if (!status.equals(LinphoneAccountCreator.Status.Ok)) {
                                    LinphoneUtils.displayErrorAlert(LinphoneUtils.errorForStatus(status),
                                            LinphoneActivity.instance());
                                } else {
                                    progress = ProgressDialog.show(LinphoneActivity.instance(), null, null);
                                    Drawable d = new ColorDrawable(ContextCompat
                                            .getColor(LinphoneActivity.instance(), R.color.colorE));
                                    d.setAlpha(200);
                                    progress.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                                            WindowManager.LayoutParams.MATCH_PARENT);
                                    progress.getWindow().setBackgroundDrawable(d);
                                    progress.setContentView(R.layout.progress_dialog);
                                    progress.show();
                                }
                            } else {
                                LinphoneUtils.displayErrorAlert(getString(R.string.wizard_passwords_unmatched),
                                        LinphoneActivity.instance());
                            }
                            return;
                        }
                        LinphoneUtils.displayErrorAlert(LinphoneUtils.errorForStatus(status),
                                LinphoneActivity.instance());
                    }
                });

                alert.setView(layout);
                alert.show();
                return true;
            }
        });
    } else {
        changePassword.setEnabled(false);
    }

    final Preference delete = manage.getPreference(3);
    delete.setEnabled(!isNewAccount);
    delete.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            mPrefs.deleteAccount(n);
            LinphoneActivity.instance().refreshAccounts();
            LinphoneActivity.instance().displaySettings();
            return true;
        }
    });
}