The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* //device/libs/android_runtime/android_pim_EventRecurrence.cpp
|
| 2 | **
|
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame^] | 3 | ** Copyright 2006, The Android Open Source Project
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4 | **
|
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame^] | 5 | ** Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 | ** you may not use this file except in compliance with the License.
|
| 7 | ** You may obtain a copy of the License at
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | **
|
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame^] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | **
|
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame^] | 11 | ** Unless required by applicable law or agreed to in writing, software
|
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 | ** See the License for the specific language governing permissions and
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 15 | ** limitations under the License.
|
| 16 | */
|
| 17 |
|
| 18 | #include <pim/EventRecurrence.h>
|
| 19 | #include "jni.h"
|
| 20 | #include "nativehelper/JNIHelp.h"
|
| 21 | #include <utils/String8.h>
|
| 22 |
|
| 23 | namespace android {
|
| 24 |
|
| 25 | struct cached_array_fields_t
|
| 26 | {
|
| 27 | jfieldID array;
|
| 28 | jfieldID count;
|
| 29 | };
|
| 30 |
|
| 31 | static jclass clazz;
|
| 32 | static jfieldID freq_field;
|
| 33 | static jfieldID until_field;
|
| 34 | static jfieldID count_field;
|
| 35 | static jfieldID interval_field;
|
| 36 | static jfieldID wkst_field;
|
| 37 | static cached_array_fields_t bysecond_fields;
|
| 38 | static cached_array_fields_t byminute_fields;
|
| 39 | static cached_array_fields_t byhour_fields;
|
| 40 | static cached_array_fields_t byday_fields;
|
| 41 | static cached_array_fields_t bydayNum_fields;
|
| 42 | static cached_array_fields_t bymonthday_fields;
|
| 43 | static cached_array_fields_t byyearday_fields;
|
| 44 | static cached_array_fields_t byweekno_fields;
|
| 45 | static cached_array_fields_t bymonth_fields;
|
| 46 | static cached_array_fields_t bysetpos_fields;
|
| 47 |
|
| 48 | static status_t
|
| 49 | set_array(JNIEnv* env, int inCount, int* inArray,
|
| 50 | jobject This, const cached_array_fields_t& fields)
|
| 51 | {
|
| 52 | if (inCount > 0) {
|
| 53 | jintArray array = (jintArray) env->GetObjectField(This, fields.array);
|
| 54 | if (array == NULL || env->GetArrayLength(array) < inCount) {
|
| 55 | // +4 because it's cheap to allocate a little extra here, and
|
| 56 | // that reduces the chance that we'll come back here again
|
| 57 | array = env->NewIntArray(inCount+4);
|
| 58 | env->SetObjectField(This, fields.array, array);
|
| 59 | }
|
| 60 | if (array == NULL) {
|
| 61 | return NO_MEMORY;
|
| 62 | }
|
| 63 | env->SetIntArrayRegion(array, 0, inCount, inArray);
|
| 64 |
|
| 65 | }
|
| 66 | env->SetIntField(This, fields.count, inCount);
|
| 67 | return NO_ERROR;
|
| 68 | }
|
| 69 |
|
| 70 | /*
|
| 71 | * In class android.pim.EventRecurrence
|
| 72 | * public native int parse(String str);
|
| 73 | */
|
| 74 | #define SET_ARRAY_AND_CHECK(name) \
|
| 75 | /*printf("setting " #name " to %d elements\n", er.name##Count);*/ \
|
| 76 | if (set_array(env, er.name##Count, er.name, This, name##_fields) \
|
| 77 | != NO_ERROR) { \
|
| 78 | jniThrowException(env, "java/lang/RuntimeException", \
|
| 79 | "EventRecurrence.parse error setting field " #name " or " \
|
| 80 | #name "Count."); \
|
| 81 | return ; \
|
| 82 | }
|
| 83 | static void
|
| 84 | EventRecurrence_parse(JNIEnv* env, jobject This, jstring jstr)
|
| 85 | {
|
| 86 | if (jstr == NULL) {
|
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame^] | 87 | jniThrowNullPointerException(env, "EventRecurrence.parse str parameter null");
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | return ;
|
| 89 | }
|
| 90 | jboolean isCopy;
|
| 91 | const jchar* jchars = env->GetStringChars(jstr, &isCopy);
|
| 92 | jsize len = env->GetStringLength(jstr);
|
| 93 | String16 str(jchars, len);
|
| 94 | env->ReleaseStringChars(jstr, jchars);
|
| 95 |
|
| 96 | //printf("the string was '%s'\n", String8(str).string());
|
| 97 |
|
| 98 | EventRecurrence er;
|
| 99 | if (NO_ERROR != er.parse(str)) {
|
| 100 | String8 msg("Error parsing recurrence: '");
|
| 101 | msg.append(String8(str));
|
| 102 | msg.append("'");
|
| 103 |
|
| 104 | jniThrowException(env,
|
| 105 | "android/pim/EventRecurrence$InvalidFormatException",
|
| 106 | msg.string());
|
| 107 | return ;
|
| 108 | }
|
| 109 |
|
| 110 | jstring untilStr;
|
| 111 | if (er.until.size() > 0) {
|
| 112 | untilStr = env->NewString(er.until.string(), er.until.size());
|
| 113 | if (untilStr == NULL) {
|
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame^] | 114 | jniThrowException(env, "java/lang/RuntimeException",
|
| 115 | "EventRecurrence.parse error setting field 'until'");
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | return ;
|
| 117 | }
|
| 118 | } else {
|
| 119 | untilStr = NULL;
|
| 120 | }
|
| 121 | env->SetObjectField(This, until_field, untilStr);
|
| 122 |
|
| 123 | env->SetIntField(This, freq_field, er.freq);
|
| 124 | env->SetIntField(This, count_field, er.count);
|
| 125 | env->SetIntField(This, interval_field, er.interval);
|
| 126 | env->SetIntField(This, wkst_field, er.wkst);
|
| 127 |
|
| 128 | SET_ARRAY_AND_CHECK(bysecond)
|
| 129 | SET_ARRAY_AND_CHECK(byminute)
|
| 130 | SET_ARRAY_AND_CHECK(byhour)
|
| 131 | SET_ARRAY_AND_CHECK(byday)
|
| 132 | // we'll just set the bydayCount field twice, it'll be less code total
|
| 133 | if (set_array(env, er.bydayCount, er.bydayNum, This, bydayNum_fields)
|
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame^] | 134 | != NO_ERROR) {
|
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | jniThrowException(env, "java/lang/RuntimeException",
|
| 136 | "EventRecurrence.parse error setting field bydayNum or "
|
| 137 | "bydayCount.");
|
| 138 | return ;
|
| 139 | }
|
| 140 | SET_ARRAY_AND_CHECK(bymonthday)
|
| 141 | SET_ARRAY_AND_CHECK(byyearday)
|
| 142 | SET_ARRAY_AND_CHECK(byweekno)
|
| 143 | SET_ARRAY_AND_CHECK(bymonth)
|
| 144 | SET_ARRAY_AND_CHECK(bysetpos)
|
| 145 | }
|
| 146 |
|
| 147 | /*
|
| 148 | * JNI registration.
|
| 149 | */
|
| 150 | static JNINativeMethod METHODS[] = {
|
| 151 | /* name, signature, funcPtr */
|
| 152 | { "parse", "(Ljava/lang/String;)V", (void*)EventRecurrence_parse }
|
| 153 | };
|
| 154 |
|
| 155 | static const char*const CLASS_NAME = "android/pim/EventRecurrence";
|
| 156 |
|
| 157 | int register_android_pim_EventRecurrence(JNIEnv* env)
|
| 158 | {
|
| 159 | clazz = env->FindClass(CLASS_NAME);
|
| 160 | if (clazz == NULL) {
|
| 161 | LOGE("Field lookup unable to find class '%s'\n", CLASS_NAME);
|
| 162 | return -1;
|
| 163 | }
|
| 164 |
|
| 165 | freq_field = env->GetFieldID(clazz, "freq", "I");
|
| 166 | count_field = env->GetFieldID(clazz, "count", "I");
|
| 167 | interval_field = env->GetFieldID(clazz, "interval", "I");
|
| 168 | wkst_field = env->GetFieldID(clazz, "wkst", "I");
|
| 169 |
|
| 170 | until_field = env->GetFieldID(clazz, "until", "Ljava/lang/String;");
|
| 171 |
|
| 172 | bysecond_fields.array = env->GetFieldID(clazz, "bysecond", "[I");
|
| 173 | bysecond_fields.count = env->GetFieldID(clazz, "bysecondCount", "I");
|
| 174 | byminute_fields.array = env->GetFieldID(clazz, "byminute", "[I");
|
| 175 | byminute_fields.count = env->GetFieldID(clazz, "byminuteCount", "I");
|
| 176 | byhour_fields.array = env->GetFieldID(clazz, "byhour", "[I");
|
| 177 | byhour_fields.count = env->GetFieldID(clazz, "byhourCount", "I");
|
| 178 | byday_fields.array = env->GetFieldID(clazz, "byday", "[I");
|
| 179 | byday_fields.count = env->GetFieldID(clazz, "bydayCount", "I");
|
| 180 | bydayNum_fields.array = env->GetFieldID(clazz, "bydayNum", "[I");
|
| 181 | bydayNum_fields.count = byday_fields.count;
|
| 182 | bymonthday_fields.array = env->GetFieldID(clazz, "bymonthday", "[I");
|
| 183 | bymonthday_fields.count = env->GetFieldID(clazz, "bymonthdayCount", "I");
|
| 184 | byyearday_fields.array = env->GetFieldID(clazz, "byyearday", "[I");
|
| 185 | byyearday_fields.count = env->GetFieldID(clazz, "byyeardayCount", "I");
|
| 186 | byweekno_fields.array = env->GetFieldID(clazz, "byweekno", "[I");
|
| 187 | byweekno_fields.count = env->GetFieldID(clazz, "byweeknoCount", "I");
|
| 188 | bymonth_fields.array = env->GetFieldID(clazz, "bymonth", "[I");
|
| 189 | bymonth_fields.count = env->GetFieldID(clazz, "bymonthCount", "I");
|
| 190 | bysetpos_fields.array = env->GetFieldID(clazz, "bysetpos", "[I");
|
| 191 | bysetpos_fields.count = env->GetFieldID(clazz, "bysetposCount", "I");
|
| 192 |
|
| 193 | return jniRegisterNativeMethods(env, CLASS_NAME,
|
| 194 | METHODS, sizeof(METHODS)/sizeof(METHODS[0]));
|
| 195 | }
|
| 196 |
|
| 197 | }; // namespace android
|