Java Code Examples for android.preference.TwoStatePreference#setChecked()

The following examples show how to use android.preference.TwoStatePreference#setChecked() . 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: AccountsSettingsFragment.java    From Android-Keyboard with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onPreferenceClick(final Preference preference) {
    final TwoStatePreference syncPreference = (TwoStatePreference) preference;
    if (syncPreference.isChecked()) {
        // Uncheck for now.
        syncPreference.setChecked(false);

        // Show opt-in.
        final AlertDialog optInDialog = new AlertDialog.Builder(getActivity())
                .setTitle(R.string.cloud_sync_title)
                .setMessage(R.string.cloud_sync_opt_in_text)
                .setPositiveButton(R.string.account_select_ok,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(final DialogInterface dialog,
                                                final int which) {
                                if (which == DialogInterface.BUTTON_POSITIVE) {
                                    final Context context = getActivity();
                                    final String[] accountsForLogin =
                                            LoginAccountUtils.getAccountsForLogin(context);
                                    createAccountPicker(accountsForLogin,
                                            getSignedInAccountName(),
                                            new AccountChangedListener(syncPreference))
                                            .show();
                                }
                            }
                })
                .setNegativeButton(R.string.cloud_sync_cancel, null)
                .create();
        optInDialog.setOnShowListener(this);
        optInDialog.show();
    }
    return true;
}
 
Example 2
Source File: AccountsSettingsFragment.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onPreferenceClick(final Preference preference) {
    final TwoStatePreference syncPreference = (TwoStatePreference) preference;
    if (syncPreference.isChecked()) {
        // Uncheck for now.
        syncPreference.setChecked(false);

        // Show opt-in.
        final AlertDialog optInDialog = new AlertDialog.Builder(getActivity())
                .setTitle(R.string.cloud_sync_title)
                .setMessage(R.string.cloud_sync_opt_in_text)
                .setPositiveButton(R.string.account_select_ok,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(final DialogInterface dialog,
                                                final int which) {
                                if (which == DialogInterface.BUTTON_POSITIVE) {
                                    final Context context = getActivity();
                                    final String[] accountsForLogin =
                                            LoginAccountUtils.getAccountsForLogin(context);
                                    createAccountPicker(accountsForLogin,
                                            getSignedInAccountName(),
                                            new AccountChangedListener(syncPreference))
                                            .show();
                                }
                            }
                })
                .setNegativeButton(R.string.cloud_sync_cancel, null)
                .create();
        optInDialog.setOnShowListener(this);
        optInDialog.show();
    }
    return true;
}
 
Example 3
Source File: SettingsActivity.java    From island with Apache License 2.0 5 votes vote down vote up
@Override public void onCreate(final Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	final TwoStatePreference pref_show_admin_message = (TwoStatePreference) findPreference(getString(R.string.key_show_admin_message));
	final DevicePolicies policies;
	if (SDK_INT >= N && (policies = new DevicePolicies(getActivity())).isActiveDeviceOwner()) {
		pref_show_admin_message.setChecked(policies.invoke(DevicePolicyManager::getShortSupportMessage) != null);
		pref_show_admin_message.setOnPreferenceChangeListener((pref, value) -> {
			final boolean enabled = value == Boolean.TRUE;
			policies.execute(DevicePolicyManager::setShortSupportMessage, enabled ? getText(R.string.device_admin_support_message_short) : null);
			policies.execute(DevicePolicyManager::setLongSupportMessage, enabled ? getText(R.string.device_admin_support_message_long) : null);
			return true;
		});
	} else if (pref_show_admin_message != null) removeLeafPreference(getPreferenceScreen(), pref_show_admin_message);
}
 
Example 4
Source File: PreferenceFragment.java    From AcDisplay with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void setValue(@NonNull Preference preference,
                     @NonNull ConfigBase.Option option,
                     @NonNull Object value) {
    TwoStatePreference cbp = (TwoStatePreference) preference;
    cbp.setChecked((Boolean) value);
}
 
Example 5
Source File: AccountsSettingsFragment.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onPreferenceClick(final Preference preference) {
    final TwoStatePreference syncPreference = (TwoStatePreference) preference;
    if (syncPreference.isChecked()) {
        // Uncheck for now.
        syncPreference.setChecked(false);

        // Show opt-in.
        final AlertDialog optInDialog = new AlertDialog.Builder(getActivity())
                .setTitle(R.string.cloud_sync_title)
                .setMessage(R.string.cloud_sync_opt_in_text)
                .setPositiveButton(R.string.account_select_ok,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(final DialogInterface dialog,
                                                final int which) {
                                if (which == DialogInterface.BUTTON_POSITIVE) {
                                    final Context context = getActivity();
                                    final String[] accountsForLogin =
                                            LoginAccountUtils.getAccountsForLogin(context);
                                    createAccountPicker(accountsForLogin,
                                            getSignedInAccountName(),
                                            new AccountChangedListener(syncPreference))
                                            .show();
                                }
                            }
                })
                .setNegativeButton(R.string.cloud_sync_cancel, null)
                .create();
        optInDialog.setOnShowListener(this);
        optInDialog.show();
    }
    return true;
}
 
Example 6
Source File: ManageSpaceActivity.java    From MiPushFramework with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.fragmented_preferences);

    Context context = getActivity();

    //Too bad in ui thread

    //TODO: Three messages seem to be too much, and need separate strings for toast.
    getPreferenceScreen().findPreference("clear_history").setOnPreferenceClickListener(preference -> {
        Toast.makeText(context, getString(R.string.settings_clear_history) + getString(R.string.start), Toast.LENGTH_SHORT).show();
        EventDb.deleteHistory(context, null);
        Toast.makeText(context, getString(R.string.settings_clear_history) + getString(R.string.end), Toast.LENGTH_SHORT).show();
        return true;
    });

    getPreferenceScreen().findPreference("clear_log").setOnPreferenceClickListener(preference -> {
        Toast.makeText(context, getString(R.string.settings_clear_log) + getString(R.string.start), Toast.LENGTH_SHORT).show();
        LogUtils.clearLog(context);
        Toast.makeText(context, getString(R.string.settings_clear_log) + getString(R.string.end), Toast.LENGTH_SHORT).show();
        return true;
    });


    getPreferenceScreen().findPreference("mock_notification").setOnPreferenceClickListener(preference -> {
        String packageName = Constants.MANAGER_APP_NAME;
        Date date = new Date();
        String title = context.getString(R.string.debug_test_title);
        String description = context.getString(R.string.debug_test_content) + date.toString();
        NotificationController.test(context, packageName, title, description);
        return true;
    });


    PackageManager pm = context.getPackageManager();
    ComponentName componentName = new ComponentName(Constants.SERVICE_APP_NAME, EmptyActivity.OnePlus.class.getName());
    boolean disabled = pm.getComponentEnabledSetting(componentName) == COMPONENT_ENABLED_STATE_DISABLED;

    TwoStatePreference preferencePushIcon = (TwoStatePreference) getPreferenceScreen().findPreference("activity_push_icon");
    preferencePushIcon.setChecked(!disabled);
    preferencePushIcon.setOnPreferenceClickListener(preference -> {
        TwoStatePreference switchPreference = (TwoStatePreference) preference;
        boolean enableLauncher = switchPreference.isChecked();
        pm.setComponentEnabledSetting(componentName,
                enableLauncher ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
        return true;
    });

    Preference iceboxSupported = getPreferenceScreen().findPreference("IceboxSupported");
    if (!Utils.isAppInstalled(IceBox.PACKAGE_NAME)) {
        iceboxSupported.setEnabled(false);
        iceboxSupported.setTitle(R.string.settings_icebox_not_installed);
    } else {
        iceboxSupported.setOnPreferenceChangeListener((preference, newValue) -> {
            Boolean value = (Boolean) newValue;
            if (value) {
                if (ContextCompat.checkSelfPermission(getActivity(), IceBox.SDK_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(getActivity(), new String[]{IceBox.SDK_PERMISSION}, 0x233);
                } else {
                    Toast.makeText(context, getString(R.string.icebox_permission_granted), Toast.LENGTH_SHORT).show();
                }
            }
            return true;
        });

    }


}