Android Open Source - GuildViewerApp2 Utility






From Project

Back to project page GuildViewerApp2.

License

The source code is released under:

Apache License

If you think the Android project GuildViewerApp2 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.skywomantechnology.app.guildviewer;
//ww w  .  j  a v  a 2  s .co m

/*
 * Guild Viewer is an Android app that allows users to view news feeds and news feed details
 * on a mobile device and while not logged into the game servers.
 *
 * Copyright 2014 Sky Woman Technology LLC
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

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

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * Mostly static methods that are helpers for the other classes
  */
public class Utility {


    // Static map of Possible Races in WOW
    private static final Map<Integer, Integer> mCharacterRace;
    static {
        Map<Integer, Integer> aMap = new HashMap<Integer, Integer>();
        aMap.put(1, R.string.display_human);
        aMap.put(2, R.string.display_orc);
        aMap.put(3, R.string.display_dwarf);
        aMap.put(4, R.string.display_night_elf);
        aMap.put(5, R.string.display_undead);
        aMap.put(6, R.string.display_tauren);
        aMap.put(7, R.string.display_gnome);
        aMap.put(8, R.string.display_troll);
        aMap.put(9, R.string.display_goblin);
        aMap.put(10, R.string.display_blood_elf);
        aMap.put(11, R.string.display_draenei);
        aMap.put(22, R.string.display_worgen);
        aMap.put(24, R.string.display_pandaren);
        aMap.put(25, R.string.display_pandaren);
        aMap.put(26, R.string.display_pandaren);
        mCharacterRace = Collections.unmodifiableMap(aMap);
    }

    /**
     *  use identifier to generate the appropriate display text
     *  this is not language modifiable at this time
     * @param context for string resource
     * @param race identifier
     * @return String with Race name
     */
    public static String getRace(Context context, int race) {
        Integer raceValue =  mCharacterRace.get(race);
        if (raceValue == null) {
            raceValue = R.string.display_unknown;
        }
        return context.getString(raceValue);
    }

    // matching mapping for Races by Side
    private static final Map<Integer, Integer> mCharacterSide;
    static {
        Map<Integer, Integer> aMap = new HashMap<Integer, Integer>();
        aMap.put(1, R.string.display_alliance);
        aMap.put(2, R.string.display_horde);
        aMap.put(3, R.string.display_alliance);
        aMap.put(4, R.string.display_alliance);
        aMap.put(5, R.string.display_horde);
        aMap.put(6, R.string.display_horde);
        aMap.put(7, R.string.display_alliance);
        aMap.put(8, R.string.display_horde);
        aMap.put(9, R.string.display_horde);
        aMap.put(10, R.string.display_horde);
        aMap.put(11, R.string.display_alliance);
        aMap.put(22, R.string.display_alliance);
        aMap.put(24, R.string.display_horde);
        aMap.put(25, R.string.display_alliance);
        aMap.put(26, R.string.display_horde);
        mCharacterSide = Collections.unmodifiableMap(aMap);
    }

    /**
     *  Uses Side identifier to generate appropriate string for
     *  the character by class type... maps to the Character Race values
     * @param context for string resource
     * @param side identifier
     * @return String with displayable text
     */
    public static String getSide(Context context, int side) {
        Integer sideValue =  mCharacterSide.get(side);
        if (sideValue == null) {
            sideValue = R.string.display_unknown;
        }
        return context.getString(sideValue);
    }

    /**
     * Uses Guild Side identifier to generate appropriate string for
     * displaying the guild's side.. This is different than class
     * @param context for string resource
     * @param side identifier
     * @return String with displayable text
     */
    public static String getGuildSide(Context context, int side) {
        int sideValue = (side == 0 ? R.string.display_alliance : R.string.display_horde);
        return context.getString(sideValue);
    }

    // mapping of WOW class identifiers to displayable strings
    private static final Map<Integer, Integer> mCharacterClass;
    static {
        Map<Integer, Integer> aMap = new HashMap<Integer, Integer>();
        aMap.put(1, R.string.display_warrior);
        aMap.put(2, R.string.display_paladin);
        aMap.put(3, R.string.display_hunter);
        aMap.put(4, R.string.display_rogue);
        aMap.put(5, R.string.display_priest);
        aMap.put(6, R.string.display_death_knight);
        aMap.put(7, R.string.display_shaman);
        aMap.put(8, R.string.display_mage);
        aMap.put(9, R.string.display_warlock);
        aMap.put(10, R.string.display_monk);
        aMap.put(11, R.string.display_druid);
        mCharacterClass = Collections.unmodifiableMap(aMap);
    }

    /**
     * Uses class identifier to generate displayable string for the class
     *
     * @param context for string resource
     * @param characterClass identifier
     * @return String with displayable class
     */
    public static String getClass(Context context, int characterClass) {
        Integer classValue =  mCharacterClass.get(characterClass);
        if (classValue == null) {
            classValue = R.string.display_unknown;
        }
        return context.getString(classValue);
    }

    /**
     *
     * @param context for string resource
     * @param gender male or female identifier
     * @return String to display
     */
    public static String getGender( Context context, int gender) {
        int genderValue =  (gender == 0 ? R.string.display_male : R.string.display_female);
        return context.getString(genderValue);
    }


    /**
     * returns the region stored in the preferences
     *
     * @param context for the preference
     * @return String with guild region preference
     */
    public static String getRegion(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getString(context.getString(R.string.pref_region_key),
                context.getString(R.string.pref_region_default));
    }

    /**
     * returns the guild realm as stored in the preferences
     *
     * @param context for the preference
     * @return String with guild realm preference
     */
    public static String getRealm(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getString(context.getString(R.string.pref_realm_key),
                context.getString(R.string.pref_realm_default));
    }

    /**
     * returns the guild name as stored in the preferences
     *
     * @param context for the preference
     * @return String with the guild name preference
     */
    public static String getGuildName(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getString(context.getString(R.string.pref_guild_key),
                context.getString(R.string.pref_guild_default));
    }

    /**
     * returns the favorite character as stored in the preferences
     *
     * @param context for the preference
     * @return String with favorite character preference
     */
    public static String getCharacter(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getString(context.getString(R.string.pref_character_key),
                context.getString(R.string.pref_character_default));
    }

    /**
     * How many days should the news be kept on the device
     *
     * @param context for the preference
     * @return int indicating the number of days to keep
     */
    public static int getPreferenceForDaysToKeepNews(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return Integer.parseInt(
                prefs.getString(context.getString(R.string.pref_days_to_keep_key),
                        Integer.toString(R.string.pref_days_to_keep_default)));
    }

    /**
     * This is the timestamp for the last notification that was sent
     *
     * @param context for the preference
     * @return long indicating the last notification date
     */
    public static long getPreferenceForLastNotificationDate(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        // returns zero if not found
        return prefs.getLong(context.getString(R.string.pref_last_notification), 0L);
    }

    /**
     * set the timestamp for the last notification
     *
     * @param context for the preference
     * @param value long value timestamp (already x1000)
     */
    public static void setPreferenceForLastNotificationDate(Context context, long value) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putLong(context.getString(R.string.pref_last_notification), value);
        editor.apply();
    }

    public static long getPreferenceForLastGuildMemberUpdate(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        // returns zero if not found
        return prefs.getLong(context.getString(R.string.pref_last_member_update), 0L);
    }

    public static void setPreferenceForLastGuildMemberUpdate(Context context, long value) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putLong(context.getString(R.string.pref_last_member_update), value);
        editor.apply();
    }

    /**
     * formats timestamp into a readable date string
     *
     * @param time timestamp in unix long format
     * @param dateFormat format string to use for timestamp display
     * @return String with formatted timestamp
     */
    public static String getReadableDateString(long time, String dateFormat) {
        // Blizzard API returns long unix value already * 1000 so no need to multiply
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        return format.format(date);
    }

    /**
     * overload of the getReadableDateString to allow the context to be used for
     * getting the default formatting string
     *
     * @param context  application context for formatted string
     * @param time long unix formatted timestamp
     * @param stringFormatId  String identifier
     * @return String with formatted timestamp
     */
    public static String getReadableDateString(Context context, long time, int stringFormatId) {
        return getReadableDateString(time, context.getString(stringFormatId));
    }

    /**
     * These appear to be the only news types that have been found so we make them more
     * readable but default to the original news type if we don't find a match here.
     * @param context for the preference
     * @param newsType action of the news item
     * @return String with appropriate display language
     */
    public static String getNewsTypeVerb(Context context, String newsType) {
        String verb = newsType;
        if (newsType.contains(Constants.ITEM_LOOT)) verb = context.getString(R.string.type_itemLoot);
        if (newsType.contains(Constants.ITEM_CRAFT)) verb = context.getString(R.string.type_itemCraft);
        if (newsType.contains(Constants.ITEM_PURCHASE)) verb = context.getString(R.string.type_itemPurchase);
        if (newsType.contains(Constants.PLAYER_ACHIEVEMENT))
            verb = context.getString(R.string.type_playerAchievement);
        return verb;
    }
    /**
     * Helper method to provide the icon resource id according to the news item type
     *
     * @param newsType from guild news item
     * @return resource id for the corresponding icon. -1 if no relation is found.
     */
    public static int getImageResourceForNews(String newsType) {

        if (newsType.contains(Constants.ITEM_LOOT)) return R.drawable.ic_looted;
        if (newsType.contains(Constants.ITEM_CRAFT)) return R.drawable.ic_crafted;
        if (newsType.contains(Constants.ITEM_PURCHASE)) return R.drawable.ic_purchased;
        if (newsType.contains(Constants.PLAYER_ACHIEVEMENT)) return R.drawable.ic_achieved;
        return R.drawable.ic_unknown;
    }

    /**
     * Helper method to provide the icon resource id according to the news item type
     *
     * @param newsType from guild news item
     * @return resource id for the corresponding icon. -1 if no relation is found.
     */
    public static int getImageResourceForNewsDetails(String newsType) {

        if (newsType.contains(Constants.ITEM_LOOT)) return R.drawable.ic_looted;
        if (newsType.contains(Constants.ITEM_CRAFT)) return R.drawable.ic_crafted;
        if (newsType.contains(Constants.ITEM_PURCHASE)) return R.drawable.ic_purchased;
        if (newsType.contains(Constants.PLAYER_ACHIEVEMENT)) return R.drawable.ic_achieved;
        return R.drawable.ic_unknown;
    }

    /**
     *  helper to see if the news type is an item related one
     * @param news action of the news item
     * @return true if item related news else false
     */
    public static boolean containsItem(String news) {
        return news.toLowerCase().contains(Constants.ITEM);
    }

    /**
     *  helper to see if the news type is an achievement
     *
     * @param news action of the news item
     * @return true if achievement else false
     */
    public static boolean containsAchievement(String news) {
        return news.toLowerCase().contains(Constants.ACHIEVEMENT);
    }

    /**
     * figure out the unix timestamp for x number of days in the past
     * @param numDays how many days to use for calculation
     * @return timestamp for number of days from current time
     */
    public static long getTimestampForDaysFromNow(int numDays) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, numDays); // negative values are in the past
        return cal.getTimeInMillis();
    }

}




Java Source Code List

com.skywomantechnology.app.guildviewer.Constants.java
com.skywomantechnology.app.guildviewer.NewsAdapter.java
com.skywomantechnology.app.guildviewer.NewsDetailActivity.java
com.skywomantechnology.app.guildviewer.NewsDetailFragment.java
com.skywomantechnology.app.guildviewer.NewsListActivity.java
com.skywomantechnology.app.guildviewer.NewsListFragment.java
com.skywomantechnology.app.guildviewer.SetPreferenceActivity.java
com.skywomantechnology.app.guildviewer.SettingsFragment.java
com.skywomantechnology.app.guildviewer.Utility.java
com.skywomantechnology.app.guildviewer.data.GuildViewerAchievement.java
com.skywomantechnology.app.guildviewer.data.GuildViewerContract.java
com.skywomantechnology.app.guildviewer.data.GuildViewerDbHelper.java
com.skywomantechnology.app.guildviewer.data.GuildViewerGuild.java
com.skywomantechnology.app.guildviewer.data.GuildViewerItem.java
com.skywomantechnology.app.guildviewer.data.GuildViewerMember.java
com.skywomantechnology.app.guildviewer.data.GuildViewerNewsItem.java
com.skywomantechnology.app.guildviewer.data.NewsProvider.java
com.skywomantechnology.app.guildviewer.sync.GuildViewerAuthenticatorService.java
com.skywomantechnology.app.guildviewer.sync.GuildViewerAuthenticator.java
com.skywomantechnology.app.guildviewer.sync.GuildViewerSyncAdapter.java
com.skywomantechnology.app.guildviewer.sync.GuildViewerSyncService.java