Android Open Source - diagnostic-tool Table Saver






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.output;
//from w  ww  .j  av a  2  s  .  c om
import java.lang.reflect.Type;
import java.util.ArrayList;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.openxc.openxcdiagnostic.diagnostic.DiagnosticActivity;
import com.openxc.openxcdiagnostic.diagnostic.pair.CommandPair;
import com.openxc.openxcdiagnostic.diagnostic.pair.DiagnosticPair;

/**
 * 
 * Manager for storing Requests and Responses for the table.
 * 
 */
public class TableSaver {

    private ArrayList<DiagnosticPair> diagnosticPairs;
    private ArrayList<CommandPair> commandPairs;
    private static SharedPreferences sPreferences;

    public TableSaver(DiagnosticActivity context) {
        sPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        diagnosticPairs = loadDiagnosticPairs();
        commandPairs = loadCommandPairs();
    }

    public void saveDiagnosticRows(ArrayList<OutputRow> rows) {
        diagnosticPairs = new ArrayList<>();
        for (int i = 0; i < rows.size(); i++) {
            diagnosticPairs.add((DiagnosticPair) rows.get(i).getPair());
        }
        saveDiagnosticPairs();
    }

    public void saveCommandRows(ArrayList<OutputRow> rows) {
        commandPairs = new ArrayList<>();
        for (int i = 0; i < rows.size(); i++) {
            commandPairs.add((CommandPair) rows.get(i).getPair());
        }
        saveCommandPairs();
    }

    public ArrayList<DiagnosticPair> getDiagnosticPairs() {
        return diagnosticPairs;
    }

    public ArrayList<CommandPair> getCommandPairs() {
        return commandPairs;
    }

    private void saveDiagnosticPairs() {
        save(getSavedDiagnosticRowsKey(), (new Gson()).toJson(diagnosticPairs));
    }

    private void saveCommandPairs() {
        save(getSavedCommandRowsKey(), (new Gson()).toJson(commandPairs));
    }

    private void save(String key, String jsonString) {
        Editor prefsEditor = sPreferences.edit();
        prefsEditor.putString(key, jsonString);
        prefsEditor.commit();
    }

    private ArrayList<CommandPair> loadCommandPairs() {

        @SuppressWarnings("serial")
        Type type = new TypeToken<ArrayList<CommandPair>>() {
        }.getType();
        String json = sPreferences.getString(getSavedCommandRowsKey(), "");
        ArrayList<CommandPair> rowList = (new Gson()).fromJson(json, type);
        if (rowList != null) {
            return new ArrayList<>(rowList);
        }

        return new ArrayList<>();
    }

    private ArrayList<DiagnosticPair> loadDiagnosticPairs() {

        @SuppressWarnings("serial")
        Type type = new TypeToken<ArrayList<DiagnosticPair>>() {
        }.getType();
        String json = sPreferences.getString(getSavedDiagnosticRowsKey(), "");
        ArrayList<DiagnosticPair> rowList = (new Gson()).fromJson(json, type);
        if (rowList != null) {
            return new ArrayList<>(rowList);
        }

        return new ArrayList<>();
    }

    private static String getSavedCommandRowsKey() {
        return "saved_command_rows_key";
    }

    private static String getSavedDiagnosticRowsKey() {
        return "saved_diagnostic_rows_key";
    }

}




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