Android Open Source - diagnostic-tool Settings Manager






From Project

Back to project page diagnostic-tool.

License

The source code is released under:

Copyright (c) 2014 Ford Motor Company All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are me...

If you think the Android project diagnostic-tool listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.openxc.openxcdiagnostic.diagnostic;
//from   w w  w.  j a  va 2  s  . c  o m
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;

import com.openxc.openxcdiagnostic.R;
import com.openxc.openxcdiagnostic.util.Utilities;

/**
 * 
 * Manager for showing the settings alert, storing preferences, and executing
 * other actions.
 * 
 */
public class SettingsManager {

    private SharedPreferences mPreferences;
    private DiagnosticActivity mContext;
    // this value is requested a lot, so more efficient to store and save when
    // needed than to read from preferences every time
    private boolean mDisplayCommands;

    public SettingsManager(DiagnosticActivity context) {
        mContext = context;
        mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);

        if (!mPreferences.contains(getSniffingCheckboxKey())) {
            setShouldSniff(false);
        }
        if (!mPreferences.contains(getScrollingCheckboxKey())) {
            setShouldScroll(true);
        }
        if (!mPreferences.contains(getMultipleResponsesEnabledKey())) {
            setMultipleResponsesEnabled(true);
        }

        mDisplayCommands = mPreferences.getBoolean(getDisplayCommandsKey(),
                false);
    }

    /**
     * Display the settings alert dialog.
     */
    public void showAlert() {

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        LinearLayout settingsLayout = (LinearLayout) mContext
                .getLayoutInflater().inflate(R.layout.diagsettingsalert, null);

        builder.setView(settingsLayout);
        builder.setTitle(mContext.getResources().getString(
                R.string.settings_alert_label));
        builder.setPositiveButton("Done", null);
        builder.create().show();
        initButtons(settingsLayout);
        Utilities.scaleDownAllLabelsToFit(mContext, settingsLayout);
    }

    private void initButtons(View layout) {

        final Resources res = mContext.getResources();

        final CheckedTextView sniffingCheckBox = (CheckedTextView) layout
                .findViewById(R.id.sniffingCheckBox);
        sniffingCheckBox.setChecked(shouldSniff());
        sniffingCheckBox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sniffingCheckBox.setChecked(!sniffingCheckBox.isChecked());
                setShouldSniff(sniffingCheckBox.isChecked());
                if (sniffingCheckBox.isChecked()) {
                    mContext.startSniffing();
                } else {
                    mContext.stopSniffing();
                }
            }
        });

        final CheckedTextView multipleResponseCheckBox = (CheckedTextView) layout
                .findViewById(R.id.multipleResponseCheckBox);
        multipleResponseCheckBox.setChecked(multipleResponsesEnabled());
        multipleResponseCheckBox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                multipleResponseCheckBox.setChecked(!multipleResponseCheckBox
                        .isChecked());
                setMultipleResponsesEnabled(multipleResponseCheckBox
                        .isChecked());
            }
        });

        final CheckedTextView scrollingCheckBox = (CheckedTextView) layout
                .findViewById(R.id.responseScrollCheckBox);
        scrollingCheckBox.setChecked(shouldScroll());
        scrollingCheckBox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollingCheckBox.setChecked(!scrollingCheckBox.isChecked());
                setShouldScroll(scrollingCheckBox.isChecked());
            }
        });

        ((Button) layout.findViewById(R.id.cancelRecurringRequestsButton))
                .setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(
                                mContext);
                        builder.setMessage(res
                                .getString(R.string.cancel_recurring_requests_verification));
                        builder.setTitle(mContext.getResources().getString(
                                R.string.cancel_recurring_requests_label));
                        builder.setNegativeButton("Yes, Cancel",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        mContext.cancelRecurringRequests();
                                    }
                                });

                        builder.setPositiveButton("Don't Cancel", null);
                        builder.create().show();
                    }
                });

        ((Button) layout.findViewById(R.id.deleteDiagnosticResponsesButton))
                .setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(
                                mContext);
                        builder.setMessage(mContext
                                .getResources()
                                .getString(
                                        R.string.delete_diagnostic_responses_verification));
                        builder.setTitle("Delete Diagnostic Responses");
                        builder.setNegativeButton(
                                res.getString(R.string.delete_label),
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        mContext.clearDiagnosticTable();
                                    }
                                });

                        builder.setPositiveButton(
                                res.getString(R.string.cancel_label), null);
                        builder.create().show();
                    }
                });

        ((Button) layout.findViewById(R.id.deleteCommandResponsesButton))
                .setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(
                                mContext);
                        builder.setMessage(mContext.getResources().getString(
                                R.string.delete_command_responses_verification));
                        builder.setTitle("Delete Command Responses");
                        builder.setNegativeButton(
                                res.getString(R.string.delete_label),
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        mContext.clearCommandTable();
                                    }
                                });

                        builder.setPositiveButton(
                                res.getString(R.string.cancel_label), null);
                        builder.create().show();
                    }
                });

        final Button responseCommandToggle = (Button) layout
                .findViewById(R.id.responseCommandToggleButton);
        configureToggleButton(responseCommandToggle);
        responseCommandToggle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                setShouldDisplayCommands(!shouldDisplayCommands());
                configureToggleButton(responseCommandToggle);
            }
        });
    }

    /**
     * Set the background selector and text based on the value of
     * mDisplayCommands
     * 
     * @param toggleButton
     */
    private void configureToggleButton(Button toggleButton) {
        String buttonText;
        int backgroundSelector;

        if (shouldDisplayCommands()) {
            buttonText = "Send Requests";
            backgroundSelector = R.drawable.send_request_button_selector;
        } else {
            buttonText = "Send Commands";
            backgroundSelector = R.drawable.send_command_button_selector;
        }
        toggleButton.setText(buttonText);
        toggleButton.setBackground(mContext.getResources().getDrawable(
                backgroundSelector));
    }

    public boolean shouldDisplayCommands() {
        return mDisplayCommands;
    }

    private void setShouldDisplayCommands(boolean shouldDisplay) {
        save(getDisplayCommandsKey(), shouldDisplay);
        mDisplayCommands = shouldDisplay;
        mContext.setRequestCommandState(shouldDisplayCommands());
    }

    public boolean shouldSniff() {
        return mPreferences.getBoolean(getSniffingCheckboxKey(), false);
    }

    public boolean shouldScroll() {
        return mPreferences.getBoolean(getScrollingCheckboxKey(), true);
    }

    public boolean multipleResponsesEnabled() {
        return mPreferences.getBoolean(getMultipleResponsesEnabledKey(), true);
    }

    private void setShouldSniff(boolean shouldSniff) {
        save(getSniffingCheckboxKey(), shouldSniff);
    }

    private void setShouldScroll(boolean shouldScroll) {
        save(getScrollingCheckboxKey(), shouldScroll);
    }

    private void setMultipleResponsesEnabled(boolean enabled) {
        save(getMultipleResponsesEnabledKey(), enabled);
    }

    private void save(String key, boolean value) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    private static String getDisplayCommandsKey() {
        return "display_commands_key";
    }

    private static String getSniffingCheckboxKey() {
        return "sniffing_checkbox_key";
    }

    private static String getScrollingCheckboxKey() {
        return "scrolling_checkbox_key";
    }

    private static String getMultipleResponsesEnabledKey() {
        return "multiple_responses_enabled";
    }

}




Java Source Code List

com.openxc.openxcdiagnostic.dash.DashboardActivity.java
com.openxc.openxcdiagnostic.dash.package-info.java
com.openxc.openxcdiagnostic.diagnostic.ButtonManager.java
com.openxc.openxcdiagnostic.diagnostic.DiagnosticActivity.java
com.openxc.openxcdiagnostic.diagnostic.DiagnosticManager.java
com.openxc.openxcdiagnostic.diagnostic.FavoritesAlertManager.java
com.openxc.openxcdiagnostic.diagnostic.FavoritesManager.java
com.openxc.openxcdiagnostic.diagnostic.InputManager.java
com.openxc.openxcdiagnostic.diagnostic.ResponseDetailsAlertManager.java
com.openxc.openxcdiagnostic.diagnostic.SettingsManager.java
com.openxc.openxcdiagnostic.diagnostic.command.ButtonCommand.java
com.openxc.openxcdiagnostic.diagnostic.command.ClearInputFieldsCommand.java
com.openxc.openxcdiagnostic.diagnostic.command.LaunchFavoritesDialogCommand.java
com.openxc.openxcdiagnostic.diagnostic.command.LaunchSettingsDialogCommand.java
com.openxc.openxcdiagnostic.diagnostic.command.RequestSendCommand.java
com.openxc.openxcdiagnostic.diagnostic.command.package-info.java
com.openxc.openxcdiagnostic.diagnostic.output.OutputRow.java
com.openxc.openxcdiagnostic.diagnostic.output.OutputTableManager.java
com.openxc.openxcdiagnostic.diagnostic.output.TableAdapter.java
com.openxc.openxcdiagnostic.diagnostic.output.TableSaver.java
com.openxc.openxcdiagnostic.diagnostic.output.package-info.java
com.openxc.openxcdiagnostic.diagnostic.pair.CommandPair.java
com.openxc.openxcdiagnostic.diagnostic.pair.DiagnosticPair.java
com.openxc.openxcdiagnostic.diagnostic.pair.Pair.java
com.openxc.openxcdiagnostic.diagnostic.pair.package-info.java
com.openxc.openxcdiagnostic.diagnostic.package-info.java
com.openxc.openxcdiagnostic.dump.DumpActivity.java
com.openxc.openxcdiagnostic.dump.SettingsManager.java
com.openxc.openxcdiagnostic.dump.package-info.java
com.openxc.openxcdiagnostic.menu.GraphDataRetrieveTask.java
com.openxc.openxcdiagnostic.menu.GrapherActivity.java
com.openxc.openxcdiagnostic.menu.GridImageAdapter.java
com.openxc.openxcdiagnostic.menu.GridManager.java
com.openxc.openxcdiagnostic.menu.MenuActivity.java
com.openxc.openxcdiagnostic.menu.package-info.java
com.openxc.openxcdiagnostic.util.ActivityLauncher.java
com.openxc.openxcdiagnostic.util.DialogLauncher.java
com.openxc.openxcdiagnostic.util.Formatter.java
com.openxc.openxcdiagnostic.util.MessageAnalyzer.java
com.openxc.openxcdiagnostic.util.RecurringResponseGenerator.java
com.openxc.openxcdiagnostic.util.ResponseEmulator.java
com.openxc.openxcdiagnostic.util.Toaster.java
com.openxc.openxcdiagnostic.util.Utilities.java
com.openxc.openxcdiagnostic.util.package-info.java