fr.eoit.parameter.Parameters.java Source code

Java tutorial

Introduction

Here is the source code for fr.eoit.parameter.Parameters.java

Source

/**
 * Copyright (C) 2012 Picon software
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
    
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package fr.eoit.parameter;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import fr.eoit.EOITConst;
import fr.eoit.db.bean.Parameter;
import fr.eoit.db.bean.Station;
import fr.eoit.parameter.skill.Skills;
import fr.eoit.parameter.station.Stations;
import fr.piconsoft.db.util.DbUtil;

/**
 * @author picon.software
 *
 */
public final class Parameters implements LoaderCallbacks<Cursor> {

    private final static int PARAM_LOADER_ID = EOITConst.getNextLoaderIdSequence();
    private final static int STATIONS_LOADER_ID = EOITConst.getNextLoaderIdSequence();

    public static long characterId;
    public static long keyId = -1;
    public static String vCode;
    public static boolean isMiningActive = true;
    public static int territory = EOITConst.Territories.CALDARI_SPACE;
    public static float securityStatus = 0.7f;

    private static Context context;

    private Parameters() {
    }

    public static void loadParameters(FragmentActivity pActivity) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(pActivity);
        characterId = preferences.getLong("character_id", -1);
        keyId = preferences.getLong("user_id", -1);
        vCode = preferences.getString("api_key", null);
        isMiningActive = preferences.getBoolean("mining_active", true);
        territory = preferences.getInt("territory", EOITConst.Territories.CALDARI_SPACE);
        securityStatus = preferences.getFloat("security_status", 0.7f);

        context = pActivity.getApplicationContext();

        if (keyId == -1 && vCode == null) {
            Skills.setLevelVChar(true);
        }

        pActivity.getSupportLoaderManager().initLoader(PARAM_LOADER_ID, null, new Parameters());
        pActivity.getSupportLoaderManager().initLoader(STATIONS_LOADER_ID, null, new Parameters());

        validateStationParams(pActivity);
    }

    public static void saveParameters(Context context) {
        Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.putLong("character_id", characterId);
        editor.putLong("user_id", keyId);
        editor.putString("api_key", vCode);
        editor.putBoolean("mining_active", isMiningActive);
        editor.putInt("territory", territory);
        editor.putFloat("security_status", securityStatus);
        editor.commit();
    }

    public static void resetParameters(FragmentActivity pActivity) {
        context = pActivity.getApplicationContext();
        pActivity.getSupportLoaderManager().restartLoader(PARAM_LOADER_ID, null, new Parameters());
        pActivity.getSupportLoaderManager().restartLoader(STATIONS_LOADER_ID, null, new Parameters());
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        if (id == PARAM_LOADER_ID) {
            return new CursorLoader(context, Parameter.CONTENT_URI,
                    new String[] { Parameter._ID, Parameter.COLUMN_NAME_PARAM_VALUE }, null, null, null);
        }
        if (id == STATIONS_LOADER_ID) {
            return new CursorLoader(context, Station.CONTENT_URI,
                    new String[] { Station._ID, Station.COLUMN_NAME_REGION_ID, Station.COLUMN_NAME_SOLAR_SYSTEM_ID,
                            Station.COLUMN_NAME_ROLE, Station.COLUMN_NAME_NAME, Station.COLUMN_NAME_STANDING },
                    Station.COLUMN_NAME_ROLE + " IS NOT NULL", null, null);
        }
        return null;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        if (DbUtil.hasAtLeastOneRow(cursor) && loader.getId() == PARAM_LOADER_ID) {

            while (!cursor.isAfterLast()) {
                int paramId = cursor.getInt(cursor.getColumnIndexOrThrow(Parameter._ID));
                String paramValue = cursor
                        .getString(cursor.getColumnIndexOrThrow(Parameter.COLUMN_NAME_PARAM_VALUE));

                switch (paramId) {
                // no more used.
                case EOITConst.PARAM_USER_ID_ID:
                case EOITConst.PARAM_API_KEY_ID:
                case EOITConst.PARAM_CHARACTER_ID_ID:
                case EOITConst.PARAM_TRADE_STATION_ID:
                case EOITConst.PARAM_PROD_STATION_ID:
                    break;

                default:
                    Skills.initSkill(paramId, Short.parseShort(paramValue));
                    break;
                }
                cursor.moveToNext();
            }
        }

        if (DbUtil.hasAtLeastOneRow(cursor) && loader.getId() == STATIONS_LOADER_ID) {

            parseStationCursor(cursor);
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
    }

    public static void validateStationParams(Context context) {
        fr.eoit.parameter.station.Stations.Station tradeStation = Stations.getTradeStation();

        if (tradeStation.regionId == 0 && tradeStation.solarSystemId == 0 && tradeStation.stationName == null) {
            Parameters.resetStationParams(context);
        }

        if (tradeStation.regionId == 0) {
            tradeStation.regionId = EOITConst.SolarSystem.JITA_SOLAR_SYSTEM_ID;
        }
    }

    public static void resetStationParams(Context context) {
        Cursor cursor = context.getContentResolver().query(Station.CONTENT_URI,
                new String[] { Station._ID, Station.COLUMN_NAME_REGION_ID, Station.COLUMN_NAME_SOLAR_SYSTEM_ID,
                        Station.COLUMN_NAME_ROLE, Station.COLUMN_NAME_NAME, Station.COLUMN_NAME_STANDING },
                Station.COLUMN_NAME_ROLE + " IS NOT NULL", null, null);

        try {
            if (DbUtil.hasAtLeastOneRow(cursor)) {
                parseStationCursor(cursor);
            }
        } finally {
            cursor.close();
        }
    }

    private static void parseStationCursor(Cursor cursor) {
        while (!cursor.isAfterLast()) {
            int stationId = cursor.getInt(cursor.getColumnIndexOrThrow(Station._ID));
            int regionId = cursor.getInt(cursor.getColumnIndexOrThrow(Station.COLUMN_NAME_REGION_ID));
            int solarSystemId = cursor.getInt(cursor.getColumnIndexOrThrow(Station.COLUMN_NAME_SOLAR_SYSTEM_ID));
            int role = cursor.getInt(cursor.getColumnIndexOrThrow(Station.COLUMN_NAME_ROLE));
            String stationName = cursor.getString(cursor.getColumnIndexOrThrow(Station.COLUMN_NAME_NAME));
            float standing = cursor.getFloat(cursor.getColumnIndexOrThrow(Station.COLUMN_NAME_STANDING));

            switch (role) {
            case EOITConst.Stations.PRODUCTION_ROLE:
                Stations.initProdStation(regionId, solarSystemId, stationId, stationName, standing);
                break;
            case EOITConst.Stations.TRADE_ROLE:
                Stations.initTradeStation(regionId, solarSystemId, stationId, stationName, standing);
                break;
            case EOITConst.Stations.BOTH_ROLES:
                Stations.initProdStation(regionId, solarSystemId, stationId, stationName, standing);
                Stations.initTradeStation(regionId, solarSystemId, stationId, stationName, standing);
                break;

            default:
                break;
            }
            cursor.moveToNext();
        }
    }
}