OSDN Git Service

いろいろ修正
[train-delayed/source.git] / workspace / TrainDelayed / src / com / td / HistoryContentProvider.java
1 package com.td;\r
2 \r
3 import com.td.db.SQLiteOpenHelperImpl;\r
4 \r
5 import android.content.ContentProvider;\r
6 import android.content.ContentUris;\r
7 import android.content.ContentValues;\r
8 import android.content.UriMatcher;\r
9 import android.database.Cursor;\r
10 import android.database.sqlite.SQLiteDatabase;\r
11 import android.net.Uri;\r
12 \r
13 public class HistoryContentProvider extends ContentProvider {\r
14         public class Fields {\r
15                 public static final String ID = android.provider.BaseColumns._ID;\r
16                 public static final String ROUTE_ID = "ROUTE_ID";\r
17                 public static final String YMD = "YMD";\r
18                 public static final String HM = "HM";\r
19                 public static final String WEEK = "WEEK";\r
20                 public static final String PERIOD = "PERIOD";\r
21                 public static final String NUM = "NUM";\r
22                 public static final String STATUS = "STATUS";\r
23         }\r
24 \r
25         public static class Projection {\r
26                 public static final String[] projection = { Fields.ID, Fields.ROUTE_ID,\r
27                                 Fields.YMD, Fields.HM, Fields.WEEK, Fields.PERIOD, Fields.NUM,\r
28                                 Fields.STATUS };\r
29         }\r
30 \r
31         public static final String AUTHORITY = "com.td.history";\r
32         public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY\r
33                         + "/" + "numbers");\r
34 \r
35         private static final int CODE_NUMBERS = 1;\r
36         private static final int CODE_NUMBER = 2;\r
37         private static final String HISTORY_TABLE = "history";\r
38 \r
39         /** \83f\83B\83\8c\83N\83g\83\8a\82ÌMIME\83^\83C\83v */\r
40         private static final String CONTENT_TYPE = "vnd.android.cursor.dir/com.td.history";\r
41 \r
42         /** \92P\88ê\82ÌMIME\83^\83C\83v */\r
43         private static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/com.td.history";\r
44 \r
45         private UriMatcher uriMatcher;\r
46         private SQLiteOpenHelperImpl sqlSupport;\r
47 \r
48         @Override\r
49         public boolean onCreate() {\r
50                 uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);\r
51                 uriMatcher.addURI(AUTHORITY, "numbers", CODE_NUMBERS);\r
52                 uriMatcher.addURI(AUTHORITY, "numbers/#", CODE_NUMBER);\r
53                 sqlSupport = new SQLiteOpenHelperImpl(getContext());\r
54                 return true;\r
55         }\r
56 \r
57         @Override\r
58         public String getType(Uri uri) {\r
59                 switch (uriMatcher.match(uri)) {\r
60                 case CODE_NUMBERS:\r
61                         return CONTENT_TYPE;\r
62                 case CODE_NUMBER:\r
63                         return CONTENT_ITEM_TYPE;\r
64                 default:\r
65                         // TODO\r
66                         throw new IllegalArgumentException("Unknown URI " + uri);\r
67                 }\r
68         }\r
69 \r
70         @Override\r
71         public int delete(Uri uri, String selection, String[] selectionArgs) {\r
72                 final SQLiteDatabase db = sqlSupport.getWritableDatabase();\r
73                 final int deleteCount;\r
74                 switch (uriMatcher.match(uri)) {\r
75                 case CODE_NUMBERS:\r
76                         deleteCount = db.delete(HISTORY_TABLE, selection, selectionArgs);\r
77                         break;\r
78                 case CODE_NUMBER:\r
79                         final long id = Long.parseLong(uri.getPathSegments().get(1));\r
80                         final String idPlusSelection = android.provider.BaseColumns._ID\r
81                                         + "=" + Long.toString(id)\r
82                                         + (selection == null ? "" : "AND (" + selection + ")");\r
83                         deleteCount = db.delete(HISTORY_TABLE, idPlusSelection,\r
84                                         selectionArgs);\r
85                         break;\r
86                 default:\r
87                         // TODO\r
88                         throw new IllegalArgumentException("Unknown URI " + uri);\r
89                 }\r
90 \r
91                 return deleteCount;\r
92         }\r
93 \r
94         @Override\r
95         public Uri insert(Uri uri, ContentValues values) {\r
96                 final SQLiteDatabase db = sqlSupport.getWritableDatabase();\r
97                 if (uriMatcher.match(uri) != CODE_NUMBERS) {\r
98                         // TODO\r
99                         throw new IllegalArgumentException("Unknown URI " + uri);\r
100                 }\r
101 \r
102                 final long id = db.insertOrThrow(HISTORY_TABLE, null, values);\r
103 \r
104                 // \95Ï\8dX\82ð\92Ê\92m\82·\82é\r
105                 final Uri newUri = ContentUris.withAppendedId(CONTENT_URI, id);\r
106                 getContext().getContentResolver().notifyChange(newUri, null);\r
107 \r
108                 return newUri;\r
109         }\r
110 \r
111         @Override\r
112         public Cursor query(Uri uri, String[] projection, String selection,\r
113                         String[] selectionArgs, String sortOrder) {\r
114                 if (uriMatcher.match(uri) == CODE_NUMBER) {\r
115                         final long id = Long.parseLong(uri.getPathSegments().get(1));\r
116                         selection = android.provider.BaseColumns._ID + "="\r
117                                         + Long.toString(id)\r
118                                         + (selection == null ? "" : "AND (" + selection + ")");\r
119                 }\r
120                 final SQLiteDatabase db = sqlSupport.getWritableDatabase();\r
121                 Cursor cursor = db.query(HISTORY_TABLE, projection, selection,\r
122                                 selectionArgs, null, null, sortOrder);\r
123 \r
124                 cursor.setNotificationUri(getContext().getContentResolver(), uri);\r
125 \r
126                 return cursor;\r
127         }\r
128 \r
129         @Override\r
130         public int update(Uri uri, ContentValues values, String selection,\r
131                         String[] selectionArgs) {\r
132                 final SQLiteDatabase db = sqlSupport.getWritableDatabase();\r
133                 final int updateCount;\r
134                 switch (uriMatcher.match(uri)) {\r
135                 case CODE_NUMBERS:\r
136                         updateCount = db.update(HISTORY_TABLE, values, selection,\r
137                                         selectionArgs);\r
138                         break;\r
139                 case CODE_NUMBER:\r
140                         final long id = Long.parseLong(uri.getPathSegments().get(1));\r
141                         final String idPlusSelection = android.provider.BaseColumns._ID\r
142                                         + "=" + Long.toString(id)\r
143                                         + (selection == null ? "" : "AND (" + selection + ")");\r
144                         updateCount = db.update(HISTORY_TABLE, values, idPlusSelection,\r
145                                         selectionArgs);\r
146                         break;\r
147                 default:\r
148                         // TODO\r
149                         throw new IllegalArgumentException("Unknown URI " + uri);\r
150                 }\r
151 \r
152                 // \95Ï\8dX\82ð\92Ê\92m\82·\82é\r
153                 getContext().getContentResolver().notifyChange(uri, null);\r
154 \r
155                 return updateCount;\r
156         }\r
157 }\r