OSDN Git Service

Delete old icon cache on update
authoreagletmt <eagletmt@gmail.com>
Wed, 7 Nov 2012 03:16:37 +0000 (12:16 +0900)
committereagletmt <eagletmt@gmail.com>
Wed, 7 Nov 2012 03:16:37 +0000 (12:16 +0900)
CUTEn/src/jp/ac/titech/sharp4k/cuten/DownloadActivity.java
CUTEn/src/jp/ac/titech/sharp4k/cuten/LectureFolderActivity.java
CUTEn/src/jp/ac/titech/sharp4k/cuten/Teacher.java
CUTEnTest/src/jp/ac/titech/sharp4k/cuten/test/LectureActivityTest.java
CUTEnTest/src/jp/ac/titech/sharp4k/cuten/test/LectureFolderActivityTest.java
CUTEnTest/src/jp/ac/titech/sharp4k/cuten/test/NormalDownloadActivityTest.java

index ad3c180..3f7c536 100644 (file)
@@ -192,7 +192,7 @@ public class DownloadActivity extends BaseMenuActivity {
                                                                .getString("icon_path"));
                                Lecture l = new Lecture(lecture.getInt("id"),
                                                lecture.getString("name"), t);
-                               t.save(db);
+                               t.save(db, this);
                                l.save(db);
                        }
                } catch (JSONException e) {
index 73d81c4..d4f2aa6 100644 (file)
@@ -686,7 +686,7 @@ public class LectureFolderActivity extends BaseMenuActivity {
                                                teacher.getString("name"),
                                                teacher.isNull("icon_path") ? null : teacher
                                                                .getString("icon_path"));
-                               t1.save(database);
+                               t1.save(database, this);
                                Lecture l = new Lecture(lecture.getInt("id"),
                                                lecture.getString("name"), t1);
                                JSONArray taskList = lecture.getJSONArray("tasks");
index a27b38c..dd0cebb 100644 (file)
@@ -4,9 +4,14 @@ import java.io.File;
 
 import android.content.ContentValues;
 import android.content.Context;
+import android.database.Cursor;
+import android.database.SQLException;
 import android.database.sqlite.SQLiteDatabase;
+import android.util.Log;
 
 public class Teacher {
+       private static final String TAG = Teacher.class.getSimpleName();
+
        private int id;
        private String name;
        private String iconPath;
@@ -29,7 +34,36 @@ public class Teacher {
                return iconPath;
        }
 
-       public long save(SQLiteDatabase db) {
+       public static Teacher find(SQLiteDatabase db, int id) {
+               try {
+                       Cursor c = db.rawQuery("SELECT teachers.name, teachers.icon_path"
+                                       + " FROM teachers WHERE teachers.id = ? LIMIT 1",
+                                       new String[] { String.valueOf(id) });
+                       if (c.moveToNext()) {
+                               Teacher t = new Teacher(id, c.getString(0), c.getString(1));
+                               c.close();
+                               return t;
+                       } else {
+                               return null;
+                       }
+               } catch (SQLException e) {
+                       e.printStackTrace();
+                       return null;
+               }
+       }
+
+       public long save(SQLiteDatabase db, Context context) {
+               Teacher old = Teacher.find(db, getId());
+               if (old != null && old.getIconPath() != null
+                               && !old.getIconPath().equals(getIconPath())) {
+                       Log.d(TAG,
+                                       "[" + getId() + "] Delete old cache " + old.getIconPath());
+                       old.getIconCacheFile(context).delete();
+               }
+               return replace(db);
+       }
+
+       public long replace(SQLiteDatabase db) {
                ContentValues row = new ContentValues();
                row.put("id", getId());
                row.put("name", getName());
index b3183ee..4b50c40 100644 (file)
@@ -76,7 +76,7 @@ public class LectureActivityTest extends
 
                SQLiteOpenHelper h = new SQLHelper(ctx);
                SQLiteDatabase db = h.getWritableDatabase();
-               t.save(db);
+               t.save(db, ctx);
                dummyLecture.save(db);
                for (Task task : dummyTasks) {
                        task.save(db);
index ddbbdea..05217ef 100644 (file)
@@ -139,7 +139,7 @@ public class LectureFolderActivityTest extends
                SQLiteDatabase db = new SQLHelper(ctx).getWritableDatabase();
                for (Lecture l : NormalHttpAPIClient.dummyLectures) {
                        l.save(db);
-                       l.getTeacher().save(db);
+                       l.getTeacher().save(db, ctx);
                }
                NormalHttpAPIClient.dummyLectures.get(0).saveToSelected(db);
                NormalHttpAPIClient.dummyLectures.get(2).saveToSelected(db);
index 70d78e3..023309a 100644 (file)
@@ -59,7 +59,7 @@ public class NormalDownloadActivityTest extends
                Lecture l = new Lecture(1000, "lec0", t);
                Context ctx = getInstrumentation().getTargetContext();
                SQLiteDatabase db = new SQLHelper(ctx).getWritableDatabase();
-               t.save(db);
+               t.save(db, ctx);
                l.save(db);
                l.saveToSelected(db);
                db.close();