package com.example.wordbook.common; import java.text.MessageFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Build; import android.os.Environment; import android.preference.PreferenceManager; import android.view.View; import com.example.wordbook.R; /** * 共通ツールクラス */ public final class Common { /** アイコンクリック識別用ID */ public static final long ITEM_CLICK = -100; /** アイコンロングクリック識別用ID */ public static final long ITEM_LONG_CLICK = -200; /** アイコンタッチ識別用ID */ public static final long ITEM_TOUCH = -300; /** 入出力ディレクトリ */ public static final String WORDBOOK_DIRECTORY = Environment .getExternalStorageDirectory().getPath() + "/wordbook/"; /** 入出力拡張子 */ public static final String WORDBOOK_EXT = ".csv"; /** 出力接尾辞 */ public static final String WORDBOOK_OUT_SUFFIX = "_out"; /** 文字列キー */ public static final String FLAG_TEXT = "flag_text"; /** 背景色キー */ public static final String FLAG_BACK = "flag_back"; /** 単語帳情報用インデックスキー */ public static final String WORDBOOK_INDEX = "index"; /** 単語帳情報用項目数キー */ public static final String WORDBOOK_COUNT = "count"; /** 単語帳情報用先頭キー */ public static final String WORDBOOK_BASE = "base"; /** 単語帳情報用タイトルキー */ public static final String WORDBOOK_TITLE = "title"; /** 単語帳情報用コメントキー */ public static final String WORDBOOK_VALUE = "value"; /** 単語帳情報用未解答項目数キー */ public static final String WORDBOOK_ZERO = "zero"; /** 単語帳情報用正解数キー */ public static final String WORDBOOK_OK = "ok"; /** 単語帳情報用不正解数キー */ public static final String WORDBOOK_NG = "ng"; /** 単語帳情報用ハイライトフラグキー */ public static final String WORDBOOK_HL = "hl"; /** インテント用インデックスキー */ public static final String INTENT_INDEX = "intent_index"; /** インテント用項目数キー */ public static final String INTENT_COUNT = "intent_count"; /** インテント用先頭キー */ public static final String INTENT_BASE = "intent_base"; /** インテント用タイトルキー */ public static final String INTENT_TITLE = "intent_title"; /** 設定ファイル最大サイズ */ public static final int PREF_MAX_SIZE = 100; /** 設定ファイル用Typeキー */ public static final String PREF_TYPE = "pref_type"; /** 設定ファイル用Modeキー */ public static final String PREF_MODE = "pref_mode"; /** 設定ファイル用Sortキー */ public static final String PREF_SORT = "pref_sort"; /** 設定ファイル用Suiteキー */ public static final String PREF_SUITE = "pref_suite"; /** 設定ファイル用Commキー */ public static final String PREF_COMM = "pref_comm"; /** 設定ファイル用Iconキー */ public static final String PREF_ICON = "pref_icon"; /** 設定ファイル用Flagアイテムリストキー */ public static final String PREF_FLAGITEM = "pref_flagstring0"; /** 設定ファイル用Recordフィルタリストキー */ public static final String PREF_RECORD = "pref_record0"; /** 設定ファイル用Flagフィルタリストキー */ public static final String PREF_FLAG = "pref_flag0"; /** 設定ファイル用Levelフィルタリストキー */ public static final String PREF_LEVEL = "pref_level0"; /** 設定ファイル用Stringsキー */ public static final String PREF_STRINGS = "pref_strings"; /** 設定ファイル用Filterキー */ public static final String PREF_FILTER = "pref_filter"; /** 設定ファイル用Statキー */ public static final String PREF_STAT = "pref_stat"; /** 設定ファイル用Manageキー */ public static final String PREF_MANAGE = "pref_manage"; /** 設定ファイル用Versionキー */ public static final String PREF_VERSION = "pref_version"; /** 設定ファイル用Systemキー */ public static final String PREF_SYSTEM = "pref_system"; /** 設定ファイル用項目数キー */ private static final String PREF_COUNT = "pref_count"; /** 設定ファイル用先頭キー */ private static final String PREF_BASE = "pref_base"; /** 設定ファイル用タイトルキー */ private static final String PREF_TITLE = "pref_title"; /** 設定ファイル用未解答項目数キー */ private static final String PREF_ZERO = "pref_zero"; /** 設定ファイル用正解数キー */ private static final String PREF_OK = "pref_ok"; /** 設定ファイル用不正解数キー */ private static final String PREF_NG = "pref_ng"; /** 設定ファイル用単語帳情報リスト順序キー */ private static final String PREF_ORDER = "pref_order"; /** * コンストラクタ(ユーティリティクラス) */ private Common() { } /** * 背景色設定(枠付) * * @param view * ビュー * @param flag * フラグ */ public static void setBackgroundBorder(View view, int flag) { switch (flag) { case 0: view.setBackgroundResource(R.drawable.border00); break; case 1: view.setBackgroundResource(R.drawable.border01); break; case 2: view.setBackgroundResource(R.drawable.border02); break; case 3: view.setBackgroundResource(R.drawable.border03); break; case 4: view.setBackgroundResource(R.drawable.border04); break; case 5: view.setBackgroundResource(R.drawable.border05); break; case 6: view.setBackgroundResource(R.drawable.border06); break; case 7: view.setBackgroundResource(R.drawable.border07); break; default: view.setBackgroundResource(R.drawable.border00); break; } } /** * 背景色設定(枠無) * * @param view * ビュー * @param flag * フラグ */ public static void setBackgroundColor(View view, int flag) { switch (flag) { case 0: view.setBackgroundResource(R.drawable.color00); break; case 1: view.setBackgroundResource(R.drawable.color01); break; case 2: view.setBackgroundResource(R.drawable.color02); break; case 3: view.setBackgroundResource(R.drawable.color03); break; case 4: view.setBackgroundResource(R.drawable.color04); break; case 5: view.setBackgroundResource(R.drawable.color05); break; case 6: view.setBackgroundResource(R.drawable.color06); break; case 7: view.setBackgroundResource(R.drawable.color07); break; default: view.setBackgroundResource(R.drawable.color00); break; } } /** * Type文字列更新 * * @param context * コンテキスト * @param type * Type文字列 * @return true:成功/false:失敗 */ public static boolean modTypeString(Context context, String type) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); Editor editor = pref.edit(); editor.putString(PREF_TYPE, type); return editor.commit(); } /** * Mode文字列更新 * * @param context * コンテキスト * @param mode * Mode文字列 * @return true:成功/false:失敗 */ public static boolean modModeString(Context context, String mode) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); Editor editor = pref.edit(); editor.putString(PREF_MODE, mode); return editor.commit(); } /** * Sort文字列更新 * * @param context * コンテキスト * @param sort * Sort文字列 * @return true:成功/false:失敗 */ public static boolean modSortString(Context context, String sort) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); Editor editor = pref.edit(); editor.putString(PREF_SORT, sort); return editor.commit(); } /** * Suite文字列更新 * * @param context * コンテキスト * @param suite * Suite文字列 * @return true:成功/false:失敗 */ public static boolean modSuiteString(Context context, String suite) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); Editor editor = pref.edit(); editor.putString(PREF_SUITE, suite); return editor.commit(); } /** * Comm文字列更新 * * @param context * コンテキスト * @param comm * Comm文字列 * @return true:成功/false:失敗 */ public static boolean modCommString(Context context, String comm) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); Editor editor = pref.edit(); editor.putString(PREF_COMM, comm); return editor.commit(); } /** * Icon文字列更新 * * @param context * コンテキスト * @param icon * Icon文字列 * @return true:成功/false:失敗 */ public static boolean modIconString(Context context, String icon) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); Editor editor = pref.edit(); editor.putString(PREF_ICON, icon); return editor.commit(); } /** * 単語帳情報リスト順序文字列更新 * * @param context * コンテキスト * @param order * 単語帳情報リスト順序文字列 * @return true:成功/false:失敗 */ public static boolean modPrefListOrderString(Context context, String order) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); Editor editor = pref.edit(); editor.putString(PREF_ORDER, order); return editor.commit(); } /** * バージョン情報取得 * * @param context * コンテキスト * @return バージョン情報 */ public static String getVersionInfo(Context context) { StringBuilder sb = new StringBuilder(); final char LS = '\n'; sb.append("VersionCode: " + getVersionCode(context)); sb.append(LS); sb.append("VersionName: " + getVersionName(context)); return sb.toString(); } /** * システム情報取得 * * @param context * コンテキスト * @return システム情報 */ public static String getSystemInfo(Context context) { StringBuilder sb = new StringBuilder(); final char LS = '\n'; sb.append("MANUFACTURER: " + Build.MANUFACTURER); sb.append(LS); sb.append("MODEL: " + Build.MODEL); sb.append(LS); sb.append("PRODUCT: " + Build.PRODUCT); sb.append(LS); sb.append("RELEASE(SDK_INT): "); sb.append(Build.VERSION.RELEASE + "(" + Build.VERSION.SDK_INT + ")"); return sb.toString(); } /** * 試験形式取得 * * @param context * コンテキスト * @return 0:詳細/1:四択/2:一覧 */ public static int getType(Context context) { // 試験形式の初期値は詳細 int type = 0; String s = getTypeString(context); String[] sa = context.getResources().getStringArray(R.array.type); // 試験形式変換 for (int i = 0; i < sa.length; i++) { if (s.equals(sa[i])) { type = i; break; } } return type; } /** * 出題方法取得 * * @param context * コンテキスト * @return 1:正質問/2:逆質問/3:同時表示 */ public static int getMode(Context context) { // 出題方法の初期値は正質問 int mode = 0; String s = getModeString(context); String[] sa = context.getResources().getStringArray(R.array.mode); // 出題方法変換 for (int i = 0; i < sa.length; i++) { if (s.equals(sa[i])) { mode = i; break; } } // 数値変換(0:正質問/1:逆質問/2:同時表示) mode++; return mode; } /** * 出題順序取得 * * @param context * コンテキスト * @return * 0:ランダム/1:正答率昇順/2:正答率降順/3:フラグ昇順/4:フラグ降順/5:レベル昇順/6:レベル降順/7:識別子昇順/8:識別子降順 */ public static int getSort(Context context) { // 出題順序の初期値はランダム int sort = 0; String s = getSortString(context); String[] sa = context.getResources().getStringArray(R.array.sort); // 出題順序変換 for (int i = 0; i < sa.length; i++) { if (s.equals(sa[i])) { sort = i; break; } } return sort; } /** * 試験単位取得 * * @param context * コンテキスト * @return 10:10問/20:20問/30:30問/40:40問 */ public static int getSuite(Context context) { // 試験単位の初期値は10問 int suite = 0; String s = getSuiteString(context); String[] sa = context.getResources().getStringArray(R.array.suite); // 試験単位変換 for (int i = 0; i < sa.length; i++) { if (s.equals(sa[i])) { suite = i; break; } } // 数値変換(0:10問/1:20問/2:30問/3:40問) suite = (suite + 1) * 10; return suite; } /** * 短縮コマンド取得 * * @param context * コンテキスト * @return 0:フラグ/1:メモ/2:発音/3:リンク */ public static int getComm(Context context) { // 短縮コマンドの初期値はフラグ int comm = 0; String s = getCommString(context); String[] sa = context.getResources().getStringArray(R.array.comm); // 短縮コマンド変換 for (int i = 0; i < sa.length; i++) { if (s.equals(sa[i])) { comm = i; break; } } return comm; } /** * アイコン位置取得 * * @param context * コンテキスト * @return 0:左/1:右 */ public static int getIcon(Context context) { // アイコン位置の初期値は左 int icon = 0; String s = getIconString(context); String[] sa = context.getResources().getStringArray(R.array.icon); // アイコン位置変換 for (int i = 0; i < sa.length; i++) { if (s.equals(sa[i])) { icon = i; break; } } return icon; } /** * バージョン番号取得 * * @param context * コンテキスト * @return バージョン番号 */ private static int getVersionCode(Context context) { int vc = 0; try { PackageInfo pi = context.getPackageManager().getPackageInfo( context.getPackageName(), PackageManager.GET_META_DATA); vc = pi.versionCode; } catch (NameNotFoundException e) { e.printStackTrace(); } return vc; } /** * バージョン名称取得 * * @param context * コンテキスト * @return バージョン名称 */ private static String getVersionName(Context context) { String vn = null; try { PackageInfo pi = context.getPackageManager().getPackageInfo( context.getPackageName(), PackageManager.GET_META_DATA); vn = pi.versionName; } catch (NameNotFoundException e) { e.printStackTrace(); } return vn; } /** * Type文字列取得 * * @param context * コンテキスト * @return Type文字列 */ private static String getTypeString(Context context) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); String type = pref.getString(PREF_TYPE, context.getString(R.string.def_type)); return type; } /** * Mode文字列取得 * * @param context * コンテキスト * @return Mode文字列 */ private static String getModeString(Context context) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); String mode = pref.getString(PREF_MODE, context.getString(R.string.def_mode)); return mode; } /** * Sort文字列取得 * * @param context * コンテキスト * @return Sort文字列 */ private static String getSortString(Context context) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); String sort = pref.getString(PREF_SORT, context.getString(R.string.def_sort)); return sort; } /** * Suite文字列取得 * * @param context * コンテキスト * @return Suite文字列 */ private static String getSuiteString(Context context) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); String suite = pref.getString(PREF_SUITE, context.getString(R.string.def_suite)); return suite; } /** * Comm文字列取得 * * @param context * コンテキスト * @return Comm文字列 */ private static String getCommString(Context context) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); String comm = pref.getString(PREF_COMM, context.getString(R.string.def_comm)); return comm; } /** * Icon文字列取得 * * @param context * コンテキスト * @return Icon文字列 */ private static String getIconString(Context context) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); String icon = pref.getString(PREF_ICON, context.getString(R.string.def_icon)); return icon; } /** * 単語帳情報リスト順序文字列取得 * * @param context * コンテキスト * @return 単語帳情報リスト順序文字列 */ private static String getPrefListOrderString(Context context) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); String order = pref.getString(PREF_ORDER, ""); return order; } /** * Flagアイテムリスト取得 * * @param context * コンテキスト * @return Flagアイテムリスト */ public static List> getFlagItemList(Context context) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); List> list = new ArrayList>(); String[] sa = context.getResources().getStringArray(R.array.def_flag); for (int i = 0; i < sa.length; i++) { Map item = new HashMap(); String key = PREF_FLAGITEM + i; item.put(FLAG_TEXT, pref.getString(key, sa[i])); item.put(FLAG_BACK, i); list.add(item); } return list; } /** * Recordフィルタ配列取得 * * @param context * コンテキスト * @return Recordフィルタ配列 */ public static boolean[] getRecordList(Context context) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); boolean[] value = new boolean[6]; for (int i = 0; i < value.length; i++) { String key = PREF_RECORD + i; value[i] = pref.getBoolean(key, true); } return value; } /** * Flagフィルタ配列取得 * * @param context * コンテキスト * @return Flagフィルタ配列 */ public static boolean[] getFlagList(Context context) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); boolean[] value = new boolean[8]; for (int i = 0; i < value.length; i++) { String key = PREF_FLAG + i; value[i] = pref.getBoolean(key, true); } return value; } /** * Levelフィルタ配列取得 * * @param context * コンテキスト * @return Levelフィルタ配列 */ public static boolean[] getLevelList(Context context) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); boolean[] value = new boolean[8]; for (int i = 0; i < value.length; i++) { String key = PREF_LEVEL + i; value[i] = pref.getBoolean(key, true); } return value; } /** * 単語帳情報リスト取得 * * @param context * コンテキスト * @return 単語帳情報リスト */ public static List> getWordbookPrefList(Context context) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); List> list = new ArrayList>(); // 項目数存在確認 for (int i = 1; i <= PREF_MAX_SIZE; i++) { String key_title = PREF_TITLE + i; String title = pref.getString(key_title, ""); String key_count = PREF_COUNT + i; int count = pref.getInt(key_count, 0); String key_base = PREF_BASE + i; int base = pref.getInt(key_base, 0); String key_zero = PREF_ZERO + i; int zero = pref.getInt(key_zero, 0); String key_ok = PREF_OK + i; int ok = pref.getInt(key_ok, 0); String key_ng = PREF_NG + i; int ng = pref.getInt(key_ng, 0); // 単語帳情報リスト追加 if (count > 0) { Map item = new HashMap(); item.put(WORDBOOK_INDEX, i); item.put(WORDBOOK_COUNT, count); item.put(WORDBOOK_BASE, base); item.put(WORDBOOK_TITLE, title); String value = makeWordbookPrefValue(context, new int[] { count, zero, ok, ng }); item.put(WORDBOOK_VALUE, value); item.put(WORDBOOK_ZERO, zero); item.put(WORDBOOK_OK, ok); item.put(WORDBOOK_NG, ng); list.add(item); } } return list; } /** * 単語帳情報リスト空き位置取得 * * @param context * コンテキスト * @return >0:空き位置/=0:失敗 */ public static int getWordbookPrefIndex(Context context) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); int index = 0; // 項目数空き検索 for (int i = 1; i <= PREF_MAX_SIZE; i++) { String key = PREF_COUNT + i; int count = pref.getInt(key, 0); if (count == 0) { index = i; break; } } return index; } /** * 単語帳情報更新 * * @param context * コンテキスト * @param pos * 更新位置 * @param title * タイトル * @param info * 0:項目数/1:未解答項目数/2:正解数/3:不正解数 * @param base * 試験データ先頭 * @return true:成功/false:失敗 */ public static boolean modWordbookPref(Context context, int pos, String title, int[] info, int base) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); Editor editor = pref.edit(); // 更新位置確認 if (pos <= 0 || pos > PREF_MAX_SIZE) { return false; } // 更新 if (title != null) { String key_title = PREF_TITLE + pos; editor.putString(key_title, title); } if (base >= 0) { String key_base = PREF_BASE + pos; editor.putInt(key_base, base); } if (info != null && info.length == 4) { if (info[0] >= 0) { String key_count = PREF_COUNT + pos; editor.putInt(key_count, info[0]); } if (info[1] >= 0) { String key_zero = PREF_ZERO + pos; editor.putInt(key_zero, info[1]); } if (info[2] >= 0) { String key_ok = PREF_OK + pos; editor.putInt(key_ok, info[2]); } if (info[3] >= 0) { String key_ng = PREF_NG + pos; editor.putInt(key_ng, info[3]); } } return editor.commit(); } /** * 単語帳情報削除 * * @param context * コンテキスト * @param pos * 削除位置 * @return true:成功/false:失敗 */ public static boolean delWordbookPref(Context context, int pos) { SharedPreferences pref = PreferenceManager .getDefaultSharedPreferences(context); Editor editor = pref.edit(); // 削除位置確認 if (pos <= 0 || pos > PREF_MAX_SIZE) { return false; } // 削除 String key_title = PREF_TITLE + pos; editor.remove(key_title); String key_base = PREF_BASE + pos; editor.remove(key_base); String key_count = PREF_COUNT + pos; editor.remove(key_count); String key_zero = PREF_ZERO + pos; editor.remove(key_zero); String key_ok = PREF_OK + pos; editor.remove(key_ok); String key_ng = PREF_NG + pos; editor.remove(key_ng); return editor.commit(); } /** * 単語帳情報タイトル重複排除 * * @param list * 単語帳情報リスト * @param check * タイトル候補 * @return タイトル */ public static String normWordbookPrefTitle(List> list, String check) { String title = check; if (list == null || list.size() == 0) { return title; } // 追加文字列初期値 int tail = 1; boolean same; do { same = false; for (Map item : list) { if (item.get(WORDBOOK_TITLE).toString().equals(title)) { title = check + "_" + tail++; same = true; break; } } } while (same); return title; } /** * 単語帳情報コメント生成 * * @param context * コンテキスト * @param info * 0:項目数/1:未解答項目数/2:正解数/3:不正解数 * @return コメント */ public static String makeWordbookPrefValue(Context context, int[] info) { if (info == null || info.length != 4) { return ""; } // 書式文字列 String format = context.getString(R.string.result_format); // 問題数/進捗率/正答率 int count = info[0], zero = info[1], ok = info[2], ng = info[3]; Object[] args = { count, (count == zero || count == 0) ? 0 : (count - zero) * 1.0 / count, (ok == 0 || ok + ng == 0) ? 0 : ok * 1.0 / (ok + ng) }; return MessageFormat.format(format, args); } /** * 単語帳情報リスト順序更新 * * @param context * コンテキスト * @param list * 単語帳情報リスト */ public static void modPrefListOrder(Context context, List> list) { // INDEX値をキーにして順序保存 StringBuffer sb = new StringBuffer(); for (int i = 0; i < list.size(); i++) { sb.append(list.get(i).get(WORDBOOK_INDEX) + ","); } modPrefListOrderString(context, sb.toString()); } /** * 単語帳情報リスト順序設定 * * @param context * コンテキスト * @param list * 単語帳情報リスト */ public static void setPrefListOrder(Context context, List> list) { // INDEX値をキーにして順序復帰 String[] order = getPrefListOrderString(context).split(",", 0); for (int i = order.length - 1; i >= 0; i--) { // INDEX=0は無効 Integer index; try { index = Integer.valueOf(order[i]); } catch (NumberFormatException e) { index = 0; } // 有効な値のみ初回優先で処理 for (int j = 0; j < list.size(); j++) { Map item = list.get(j); if (index.equals(item.get(WORDBOOK_INDEX))) { list.remove(item); list.add(0, item); break; } } } } }