From d4289fdc3ce2142c7f8b076140b6e4e864aedfb3 Mon Sep 17 00:00:00 2001 From: yuta Date: Thu, 17 Nov 2011 17:29:11 +0900 Subject: [PATCH] =?utf8?q?finalize=E3=81=AE=E5=AE=9F=E9=A8=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java_conf/ofnhwx/olib/utils/OPreference.java | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/jp/gr/java_conf/ofnhwx/olib/utils/OPreference.java b/src/jp/gr/java_conf/ofnhwx/olib/utils/OPreference.java index 3f8a7ea..947505c 100644 --- a/src/jp/gr/java_conf/ofnhwx/olib/utils/OPreference.java +++ b/src/jp/gr/java_conf/ofnhwx/olib/utils/OPreference.java @@ -1,8 +1,16 @@ package jp.gr.java_conf.ofnhwx.olib.utils; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.Map; +import java.util.Map.Entry; + import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; +import android.util.Log; /** * デフォルト設定の取得・設定を行うクラス. @@ -39,6 +47,18 @@ public final class OPreference { private OPreference(Context context) { this.context = context; this.preferences = PreferenceManager.getDefaultSharedPreferences(context); + if (isGalaxyS()) { + readFromFile(); + } + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + Log.v("", "finalize"); + if (isGalaxyS()) { + writeToFile(); + } } /** @@ -291,4 +311,58 @@ public final class OPreference { return context.getResources().getIdentifier(name, "string", context.getPackageName()); } + // 以下、`Galaxy S 2.2.1'への対策 + + private static final String FILENAME = "preferences.dat"; + + private boolean isGalaxyS() { + return true; +// return Build.MODEL.equals("SC-02B"); + } + + private void readFromFile() { + FileInputStream fis = null; + ObjectInputStream ois = null; + try { + fis = context.openFileInput(FILENAME); + ois = new ObjectInputStream(fis); + @SuppressWarnings("unchecked") + Map map = (Map)ois.readObject(); + for (Entry entry : map.entrySet()) { + Object value = entry.getValue(); + if (value instanceof Boolean) { + write(entry.getKey(), (Boolean)value); + } else if (value instanceof Float) { + write(entry.getKey(), (Float)value); + } else if (value instanceof Integer) { + write(entry.getKey(), (Integer)value); + } else if (value instanceof Long) { + write(entry.getKey(), (Long)value); + } else if (value instanceof String) { + write(entry.getKey(), (String)value); + } + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { ois.close(); } catch (Exception e) {} + try { fis.close(); } catch (Exception e) {} + } + } + + private void writeToFile() { + FileOutputStream fos = null; + ObjectOutputStream oos = null; + try { + fos = context.openFileOutput(FILENAME, Context.MODE_WORLD_READABLE); + oos = new ObjectOutputStream(fos); + oos.writeObject(preferences.getAll()); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { oos.close(); } catch (Exception e) {} + try { fos.close(); } catch (Exception e) {} + } + } + } -- 2.11.4.GIT