Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed the problem where updating event colors would cause exceptions

Bug: 6742712
Change-Id: Ie097e05c74995513b5b6fc04efd8e0d35876b698
  • Loading branch information...
commit f98ed2e5c5e08cac159ce4cf22ab86a718f0bb0b 1 parent 35225e0
Michael Chan authored
20 src/com/android/providers/calendar/CalendarProvider2.java
@@ -203,8 +203,9 @@
203 203 private static final String SQL_WHERE_CALENDAR_COLOR = Calendars.ACCOUNT_NAME + "=? AND "
204 204 + Calendars.ACCOUNT_TYPE + "=? AND " + Calendars.CALENDAR_COLOR_KEY + "=?";
205 205
206   - private static final String SQL_WHERE_EVENT_COLOR = Events.ACCOUNT_NAME + "=? AND "
207   - + Events.ACCOUNT_TYPE + "=? AND " + Events.EVENT_COLOR_KEY + "=?";
  206 + private static final String SQL_WHERE_EVENT_COLOR = Calendars._ID + " in (SELECT _id from "
  207 + + Tables.CALENDARS + " WHERE " + Events.ACCOUNT_NAME + "=? AND " + Events.ACCOUNT_TYPE
  208 + + "=?) AND " + Events.EVENT_COLOR_KEY + "=?";
208 209
209 210 protected static final String SQL_WHERE_ID = GENERIC_ID + "=?";
210 211 private static final String SQL_WHERE_EVENT_ID = GENERIC_EVENT_ID + "=?";
@@ -3875,10 +3876,17 @@ protected int updateInTransaction(Uri uri, ContentValues values, String selectio
3875 3876 }
3876 3877
3877 3878 case COLORS:
3878   - Integer color = values.getAsInteger(Colors.COLOR);
3879   - if (values.size() > 1 || (values.size() == 1 && color == null)) {
3880   - throw new UnsupportedOperationException("You may only change the COLOR "
3881   - + "for an existing Colors entry.");
  3879 + int validValues = 0;
  3880 + if (values.getAsInteger(Colors.COLOR) != null) {
  3881 + validValues++;
  3882 + }
  3883 + if (values.getAsString(Colors.DATA) != null) {
  3884 + validValues++;
  3885 + }
  3886 +
  3887 + if (values.size() != validValues) {
  3888 + throw new UnsupportedOperationException("You may only change the COLOR and"
  3889 + + " DATA columns for an existing Colors entry.");
3882 3890 }
3883 3891 return handleUpdateColors(values, appendAccountToSelection(uri, selection,
3884 3892 Calendars.ACCOUNT_NAME, Calendars.ACCOUNT_TYPE),
75 tests/src/com/android/providers/calendar/CalendarProvider2Test.java
@@ -1072,7 +1072,7 @@ private String obsToString(Object... objs) {
1072 1072 return bob.toString();
1073 1073 }
1074 1074
1075   - private long insertColor(long colorType, String colorKey, long color) {
  1075 + private Uri insertColor(long colorType, String colorKey, long color) {
1076 1076 ContentValues m = new ContentValues();
1077 1077 m.put(Colors.ACCOUNT_NAME, DEFAULT_ACCOUNT);
1078 1078 m.put(Colors.ACCOUNT_TYPE, DEFAULT_ACCOUNT_TYPE);
@@ -1083,9 +1083,31 @@ private long insertColor(long colorType, String colorKey, long color) {
1083 1083
1084 1084 Uri uri = CalendarContract.Colors.CONTENT_URI;
1085 1085
1086   - uri = mResolver.insert(addSyncQueryParams(uri, DEFAULT_ACCOUNT, DEFAULT_ACCOUNT_TYPE), m);
1087   - String id = uri.getLastPathSegment();
1088   - return Long.parseLong(id);
  1086 + return mResolver.insert(addSyncQueryParams(uri, DEFAULT_ACCOUNT, DEFAULT_ACCOUNT_TYPE), m);
  1087 + }
  1088 +
  1089 + private void updateAndCheckColor(long colorId, long colorType, String colorKey, long color) {
  1090 +
  1091 + Uri uri = CalendarContract.Colors.CONTENT_URI;
  1092 +
  1093 + final String where = Colors.ACCOUNT_NAME + "=? AND " + Colors.ACCOUNT_TYPE + "=? AND "
  1094 + + Colors.COLOR_TYPE + "=? AND " + Colors.COLOR_KEY + "=?";
  1095 +
  1096 + String[] selectionArgs = new String[] {
  1097 + DEFAULT_ACCOUNT, DEFAULT_ACCOUNT_TYPE, Long.toString(colorType), colorKey
  1098 + };
  1099 +
  1100 + ContentValues cv = new ContentValues();
  1101 + cv.put(Colors.COLOR, color);
  1102 + cv.put(Colors.DATA, obsToString(colorType, colorKey, color));
  1103 +
  1104 + int count = mResolver.update(
  1105 + addSyncQueryParams(uri, DEFAULT_ACCOUNT, DEFAULT_ACCOUNT_TYPE), cv, where,
  1106 + selectionArgs);
  1107 +
  1108 + checkColor(colorId, colorType, colorKey, color);
  1109 +
  1110 + assertEquals(1, count);
1089 1111 }
1090 1112
1091 1113 /**
@@ -1293,19 +1315,42 @@ private void deleteAllEvents() {
1293 1315 mMetaData.clearInstanceRange();
1294 1316 }
1295 1317
1296   - public void testInsertColor() throws Exception {
1297   - checkColor(Colors.TYPE_EVENT, "1" /* color key */, 11 /* color */);
  1318 + public void testInsertAndUpdateColor() throws Exception {
  1319 + // Calendar Color
  1320 + long colorType = Colors.TYPE_CALENDAR;
  1321 + String colorKey = "123";
  1322 + long colorValue = 11;
  1323 + long colorId = insertAndCheckColor(colorType, colorKey, colorValue);
  1324 +
1298 1325 try {
1299   - checkColor(Colors.TYPE_EVENT, "1" /* color key */, 11 /* color */);
  1326 + insertAndCheckColor(colorType, colorKey, colorValue);
1300 1327 fail("Expected to fail with duplicate insertion");
1301 1328 } catch (IllegalArgumentException iae) {
1302 1329 // good
1303 1330 }
1304 1331
1305   - checkColor(Colors.TYPE_CALENDAR, "1" /* color key */, 22 /* color */);
  1332 + // Test Update
  1333 + colorValue += 11;
  1334 + updateAndCheckColor(colorId, colorType, colorKey, colorValue);
  1335 +
  1336 + // Event Color
  1337 + colorType = Colors.TYPE_EVENT;
  1338 + colorValue += 11;
  1339 +
  1340 + colorId = insertAndCheckColor(colorType, colorKey, colorValue);
  1341 + try {
  1342 + insertAndCheckColor(colorType, colorKey, colorValue);
  1343 + fail("Expected to fail with duplicate insertion");
  1344 + } catch (IllegalArgumentException iae) {
  1345 + // good
  1346 + }
  1347 +
  1348 + // Test Update
  1349 + colorValue += 11;
  1350 + updateAndCheckColor(colorId, colorType, colorKey, colorValue);
1306 1351 }
1307 1352
1308   - private void checkColor(long colorType, String colorKey, long color) {
  1353 + private void checkColor(long colorId, long colorType, String colorKey, long color) {
1309 1354 String[] projection = new String[] {
1310 1355 Colors.ACCOUNT_NAME, // 0
1311 1356 Colors.ACCOUNT_TYPE, // 1
@@ -1316,8 +1361,6 @@ private void checkColor(long colorType, String colorKey, long color) {
1316 1361 Colors.DATA, // 6
1317 1362 };
1318 1363
1319   - long color1Id = insertColor(colorType, colorKey, color);
1320   -
1321 1364 Cursor cursor = mResolver.query(Colors.CONTENT_URI, projection, Colors.COLOR_KEY
1322 1365 + "=? AND " + Colors.COLOR_TYPE + "=?", new String[] {
1323 1366 colorKey, Long.toString(colorType)
@@ -1331,11 +1374,19 @@ private void checkColor(long colorType, String colorKey, long color) {
1331 1374 assertEquals(colorType, cursor.getLong(2));
1332 1375 assertEquals(colorKey, cursor.getString(3));
1333 1376 assertEquals(color, cursor.getLong(4));
1334   - assertEquals(color1Id, cursor.getLong(5));
  1377 + assertEquals(colorId, cursor.getLong(5));
1335 1378 assertEquals(obsToString(colorType, colorKey, color), cursor.getString(6));
1336 1379 cursor.close();
1337 1380 }
1338 1381
  1382 + private long insertAndCheckColor(long colorType, String colorKey, long color) {
  1383 + Uri uri = insertColor(colorType, colorKey, color);
  1384 + long id = Long.parseLong(uri.getLastPathSegment());
  1385 +
  1386 + checkColor(id, colorType, colorKey, color);
  1387 + return id;
  1388 + }
  1389 +
1339 1390 public void testInsertNormalEvents() throws Exception {
1340 1391 final int calId = insertCal("Calendar0", DEFAULT_TIMEZONE);
1341 1392 Cursor cursor = mResolver.query(mEventsUri, null, null, null, null);

0 comments on commit f98ed2e

Please sign in to comment.
Something went wrong with that request. Please try again.