Android Open Source - diagnostic-tool Favorites 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 av a 2 s . co  m*/
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

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

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.openxc.messages.Command;
import com.openxc.messages.DiagnosticRequest;
import com.openxc.messages.VehicleMessage;
import com.openxc.openxcdiagnostic.util.MessageAnalyzer;

/**
 * 
 * Manager for storing favorite requests and commands.
 * 
 */
public class FavoritesManager {

    private static String TAG = "DiagnosticFavoritesManager";

    private static ArrayList<DiagnosticRequest> sFavoriteRequests;
    private static ArrayList<Command> sFavoriteCommands;
    private static SharedPreferences sPreferences;

    // Don't like having to pass in a context to a static class on init, but
    // advantageous this way so you can access favorites from any class.
    public static void init(DiagnosticActivity context) {
        sPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        sFavoriteRequests = loadFavoriteRequests();
        sFavoriteCommands = loadFavoriteCommands();
    }

    /**
     * Add the given <code>req</code> to favorites. The method will decide
     * whether it should be added as a <code>DiagnosticRequest</code> or a
     * <code>Command</code>.
     * 
     * @param req
     *            The request/command to add.
     */
    public static void add(VehicleMessage req) {
        if (MessageAnalyzer.isDiagnosticRequest(req)) {
            addFavoriteRequest((DiagnosticRequest) req);
        } else if (MessageAnalyzer.isCommand(req)) {
            addFavoriteCommand((Command) req);
        } else {
            Log.e(TAG, "Unable to add message to favorites of type "
                    + req.getClass().toString());
        }
    }

    private static void addFavoriteRequest(DiagnosticRequest req) {
        ArrayList<DiagnosticRequest> newFavorites = sFavoriteRequests;
        newFavorites.add(findAlphabeticPosition(req), req);
        setFavoriteRequests(newFavorites);
    }

    private static void addFavoriteCommand(Command command) {
        ArrayList<Command> newFavorites = sFavoriteCommands;
        newFavorites.add(findAlphabeticPosition(command), command);
        setFavoriteCommands(newFavorites);
    }

    /**
     * Determines if the given strings are in the correct alphabetic order
     * determined by the order of the arguments
     * 
     * @param s1
     * @param s2
     * @return <code>True</code> if <code>s1</code> precedes <code>s2</code>,
     *         false otherwise. If <code>s1</code> is null or empty/whitespace,
     *         the method will return <code>false</code>. If <code>s2</code> is
     *         null or empty/whitespace, the method will return
     *         <code>true</code>.
     */
    private static boolean isOrdered(String s1, String s2) {
        if (s1 == null || s1.trim() == "") {
            return false;
        }

        if (s2 == null || s2.trim() == "") {
            return true;
        }

        return s1.compareToIgnoreCase(s2) < 0;
    }

    /**
     * Finds the index at which the <code>req</code> should be placed in
     * <code>sFavoriteRequests</code>
     * 
     * @param req
     *            The request to insert
     * @return The index
     */
    private static int findAlphabeticPosition(DiagnosticRequest req) {

        int position = 0;
        for (; position < sFavoriteRequests.size(); position++) {
            if (isOrdered(req.getName(), sFavoriteRequests.get(position)
                    .getName())) {
                break;
            }
        }
        return position;
    }

    /**
     * Finds the index at which the <code>command</code> should be placed in
     * <code>sFavoriteCommands</code>
     * 
     * @param req
     *            The request to insert
     * @return The index
     */
    private static int findAlphabeticPosition(Command command) {

        int position = 0;
        for (; position < sFavoriteCommands.size(); position++) {
            if (isOrdered(command.getCommand().toString(), sFavoriteCommands
                    .get(position).getCommand().toString())) {
                break;
            }
        }
        return position;
    }

    /**
     * Remove the given <code>req</code> from favorites. The method will decide
     * whether it should be removed from <code>sFavoriteRequests</code> or
     * <code>sFavoriteCommands</code>.
     * 
     * @param req
     *            The request/command to add.
     */
    public static void remove(VehicleMessage req) {
        if (MessageAnalyzer.isDiagnosticRequest(req)) {
            removeFavoriteRequest((DiagnosticRequest) req);
        } else if (MessageAnalyzer.isCommand(req)) {
            removeFavoriteCommand((Command) req);
        } else {
            Log.w(TAG, "Unable to remove message from favorites of type "
                    + req.getClass().toString());
        }
    }

    private static void removeFavoriteRequest(DiagnosticRequest req) {
        ArrayList<DiagnosticRequest> newFavorites = sFavoriteRequests;
        newFavorites.remove(req);
        setFavoriteRequests(newFavorites);
    }

    private static void removeFavoriteCommand(Command command) {
        ArrayList<Command> newFavorites = sFavoriteCommands;
        newFavorites.remove(command);
        setFavoriteCommands(newFavorites);
    }

    public static ArrayList<DiagnosticRequest> getFavoriteRequests() {
        return sFavoriteRequests;
    }

    public static ArrayList<Command> getFavoriteCommands() {
        return sFavoriteCommands;
    }

    private static void setFavoriteRequests(
            ArrayList<DiagnosticRequest> newFavorites) {
        String json = (new Gson()).toJson(newFavorites);
        save(getFavoriteRequestsKey(), json);
        sFavoriteRequests = newFavorites;
    }

    private static void setFavoriteCommands(ArrayList<Command> newFavorites) {
        String json = (new Gson()).toJson(newFavorites);
        save(getFavoriteCommandsKey(), json);
        sFavoriteCommands = newFavorites;
    }

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

    /**
     * Determines if the given VehicleMessage is in favorites
     * 
     * @param message
     * @return True if the message is in favorites, false otherwise.
     */
    public static boolean isInFavorites(VehicleMessage message) {
        if (MessageAnalyzer.isDiagnosticRequest(message)) {
            return containsFavoriteRequest((DiagnosticRequest) message);
        } else if (MessageAnalyzer.isCommand(message)) {
            return containsFavoriteCommand((Command) message);
        }
        return false;
    }

    private static boolean containsFavoriteRequest(DiagnosticRequest req) {
        return sFavoriteRequests.contains(req);
    }

    private static boolean containsFavoriteCommand(Command command) {
        return sFavoriteCommands.contains(command);
    }

    private static ArrayList<DiagnosticRequest> loadFavoriteRequests() {

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

        return new ArrayList<>();
    }

    private static ArrayList<Command> loadFavoriteCommands() {

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

        return new ArrayList<>();
    }

    private static String getFavoriteRequestsKey() {
        return "favorite_requests_key";
    }

    private static String getFavoriteCommandsKey() {
        return "favorite_commands_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