Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 84e29723 authored by Andy McFadden's avatar Andy McFadden
Browse files

Remove native EventRecurrence parser

Switch over to the new parser.

Bug 4575374

Change-Id: If78d8042fb266182900398f7fc464a048c779966
parent 7654b20e
Loading
Loading
Loading
Loading
+1 −44
Original line number Diff line number Diff line
@@ -155,49 +155,6 @@ public class EventRecurrence {
        }
    }

    /**
     * Parse an iCalendar/RFC2445 recur type according to Section 4.3.10.  The string is
     * parsed twice, by the old and new parsers, and the results are compared.
     * <p>
     * TODO: this will go away, and what is now parse2() will simply become parse().
     */
    public void parse(String recur) {
        InvalidFormatException newExcep = null;
        try {
            parse2(recur);
        } catch (InvalidFormatException ife) {
            newExcep = ife;
        }

        boolean oldThrew = false;
        try {
            EventRecurrence check = new EventRecurrence();
            check.parseNative(recur);
            if (newExcep == null) {
                // Neither threw, check to see if results match.
                if (!equals(check)) {
                    throw new InvalidFormatException("Recurrence rule parse does not match [" +
                            recur + "]");
                }
            }
        } catch (InvalidFormatException ife) {
            oldThrew = true;
            if (newExcep == null) {
                // Old threw, but new didn't.  Log a warning, but don't throw.
                Log.d(TAG, "NOTE: old parser rejected [" + recur + "]: " + ife.getMessage());
            }
        }

        if (newExcep != null) {
            if (!oldThrew) {
                // New threw, but old didn't.  Log a warning and throw the exception.
                Log.d(TAG, "NOTE: new parser rejected [" + recur + "]: " + newExcep.getMessage());
            }
            throw newExcep;
        }
    }

    native void parseNative(String recur);

    public void setStartDate(Time date) {
        startDate = date;
@@ -566,7 +523,7 @@ public class EventRecurrence {
     *
     * @param recur The recurrence rule to parse (in un-folded form).
     */
    void parse2(String recur) {
    public void parse(String recur) {
        /*
         * From RFC 2445 section 4.3.10:
         *
+0 −1
Original line number Diff line number Diff line
@@ -75,7 +75,6 @@ LOCAL_SRC_FILES:= \
	android_nio_utils.cpp \
	android_nfc_NdefMessage.cpp \
	android_nfc_NdefRecord.cpp \
	android_pim_EventRecurrence.cpp \
	android_text_format_Time.cpp \
	android_util_AssetManager.cpp \
	android_util_Binder.cpp \
+0 −2
Original line number Diff line number Diff line
@@ -131,7 +131,6 @@ extern int register_android_debug_JNITest(JNIEnv* env);
extern int register_android_nio_utils(JNIEnv* env);
extern int register_android_nfc_NdefMessage(JNIEnv *env);
extern int register_android_nfc_NdefRecord(JNIEnv *env);
extern int register_android_pim_EventRecurrence(JNIEnv* env);
extern int register_android_text_format_Time(JNIEnv* env);
extern int register_android_os_Debug(JNIEnv* env);
extern int register_android_os_MessageQueue(JNIEnv* env);
@@ -1103,7 +1102,6 @@ static const RegJNIRec gRegJNI[] = {
    REG_JNI(register_android_util_Log),
    REG_JNI(register_android_util_FloatMath),
    REG_JNI(register_android_text_format_Time),
    REG_JNI(register_android_pim_EventRecurrence),
    REG_JNI(register_android_content_AssetManager),
    REG_JNI(register_android_content_StringBlock),
    REG_JNI(register_android_content_XmlBlock),
+0 −195
Original line number Diff line number Diff line
/* //device/libs/android_runtime/android_pim_EventRecurrence.cpp
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

#include <pim/EventRecurrence.h>
#include "jni.h"
#include "nativehelper/JNIHelp.h"
#include <utils/String8.h>

namespace android {

struct cached_array_fields_t
{
    jfieldID array;
    jfieldID count;
};

static jfieldID freq_field;
static jfieldID until_field;
static jfieldID count_field;
static jfieldID interval_field;
static jfieldID wkst_field;
static cached_array_fields_t bysecond_fields;
static cached_array_fields_t byminute_fields;
static cached_array_fields_t byhour_fields;
static cached_array_fields_t byday_fields;
static cached_array_fields_t bydayNum_fields;
static cached_array_fields_t bymonthday_fields;
static cached_array_fields_t byyearday_fields;
static cached_array_fields_t byweekno_fields;
static cached_array_fields_t bymonth_fields;
static cached_array_fields_t bysetpos_fields;

static status_t
set_array(JNIEnv* env, int inCount, int* inArray,
            jobject This, const cached_array_fields_t& fields)
{
    if (inCount > 0) {
        jintArray array = (jintArray) env->GetObjectField(This, fields.array);
        if (array == NULL || env->GetArrayLength(array) < inCount) {
            // +4 because it's cheap to allocate a little extra here, and
            // that reduces the chance that we'll come back here again
            array = env->NewIntArray(inCount+4);
            env->SetObjectField(This, fields.array, array);
        }
        if (array == NULL) {
            return NO_MEMORY;
        }
        env->SetIntArrayRegion(array, 0, inCount, inArray);

    }
    env->SetIntField(This, fields.count, inCount);
    return NO_ERROR;
}

/*
 * In class android.pim.EventRecurrence
 *  public native int parse(String str);
 */
#define SET_ARRAY_AND_CHECK(name) \
    /*printf("setting " #name " to %d elements\n", er.name##Count);*/ \
    if (set_array(env, er.name##Count, er.name, This, name##_fields) \
            != NO_ERROR) { \
        jniThrowException(env, "java/lang/RuntimeException", \
                "EventRecurrence.parse error setting field " #name " or " \
                #name "Count."); \
        return ; \
    }
static void
EventRecurrence_parse(JNIEnv* env, jobject This, jstring jstr)
{
    if (jstr == NULL) {
        jniThrowNullPointerException(env, "EventRecurrence.parse str parameter null");
        return ;
    }
    const jchar* jchars = env->GetStringChars(jstr, NULL);
    jsize len = env->GetStringLength(jstr);
    String16 str(jchars, len);
    env->ReleaseStringChars(jstr, jchars);

    //printf("the string was '%s'\n", String8(str).string());

    EventRecurrence er;
    if (NO_ERROR != er.parse(str)) {
        String8 msg("Error parsing recurrence: '");
        msg.append(String8(str));
        msg.append("'");

        jniThrowException(env,
                "android/pim/EventRecurrence$InvalidFormatException",
                msg.string());
        return ;
    }

    jstring untilStr;
    if (er.until.size() > 0) {
        untilStr = env->NewString(er.until.string(), er.until.size());
        if (untilStr == NULL) {
            jniThrowException(env, "java/lang/RuntimeException",
                    "EventRecurrence.parse error setting field 'until'");
            return ;
        }
    } else {
        untilStr = NULL;
    }
    env->SetObjectField(This, until_field, untilStr);

    env->SetIntField(This, freq_field, er.freq);
    env->SetIntField(This, count_field, er.count);
    env->SetIntField(This, interval_field, er.interval);
    env->SetIntField(This, wkst_field, er.wkst);

    SET_ARRAY_AND_CHECK(bysecond)
    SET_ARRAY_AND_CHECK(byminute)
    SET_ARRAY_AND_CHECK(byhour)
    SET_ARRAY_AND_CHECK(byday)
    // we'll just set the bydayCount field twice, it'll be less code total
    if (set_array(env, er.bydayCount, er.bydayNum, This, bydayNum_fields)
            != NO_ERROR) {
        jniThrowException(env, "java/lang/RuntimeException",
                "EventRecurrence.parse error setting field bydayNum or "
                "bydayCount.");
        return ;
    }
    SET_ARRAY_AND_CHECK(bymonthday)
    SET_ARRAY_AND_CHECK(byyearday)
    SET_ARRAY_AND_CHECK(byweekno)
    SET_ARRAY_AND_CHECK(bymonth)
    SET_ARRAY_AND_CHECK(bysetpos)
}

/*
 * JNI registration.
 */
static JNINativeMethod METHODS[] = {
    /* name, signature, funcPtr */
    { "parseNative", "(Ljava/lang/String;)V", (void*)EventRecurrence_parse }
};

static const char*const CLASS_NAME = "android/pim/EventRecurrence";

int register_android_pim_EventRecurrence(JNIEnv* env)
{
    jclass clazz = env->FindClass(CLASS_NAME);
    if (clazz == NULL) {
        LOGE("Field lookup unable to find class '%s'\n", CLASS_NAME);
        return -1;
    }

    freq_field = env->GetFieldID(clazz, "freq", "I");
    count_field = env->GetFieldID(clazz, "count", "I");
    interval_field = env->GetFieldID(clazz, "interval", "I");
    wkst_field = env->GetFieldID(clazz, "wkst", "I");

    until_field = env->GetFieldID(clazz, "until", "Ljava/lang/String;");

    bysecond_fields.array = env->GetFieldID(clazz, "bysecond", "[I");
    bysecond_fields.count = env->GetFieldID(clazz, "bysecondCount", "I");
    byminute_fields.array = env->GetFieldID(clazz, "byminute", "[I");
    byminute_fields.count = env->GetFieldID(clazz, "byminuteCount", "I");
    byhour_fields.array = env->GetFieldID(clazz, "byhour", "[I");
    byhour_fields.count = env->GetFieldID(clazz, "byhourCount", "I");
    byday_fields.array = env->GetFieldID(clazz, "byday", "[I");
    byday_fields.count = env->GetFieldID(clazz, "bydayCount", "I");
    bydayNum_fields.array = env->GetFieldID(clazz, "bydayNum", "[I");
    bydayNum_fields.count = byday_fields.count;
    bymonthday_fields.array = env->GetFieldID(clazz, "bymonthday", "[I");
    bymonthday_fields.count = env->GetFieldID(clazz, "bymonthdayCount", "I");
    byyearday_fields.array = env->GetFieldID(clazz, "byyearday", "[I");
    byyearday_fields.count = env->GetFieldID(clazz, "byyeardayCount", "I");
    byweekno_fields.array = env->GetFieldID(clazz, "byweekno", "[I");
    byweekno_fields.count = env->GetFieldID(clazz, "byweeknoCount", "I");
    bymonth_fields.array = env->GetFieldID(clazz, "bymonth", "[I");
    bymonth_fields.count = env->GetFieldID(clazz, "bymonthCount", "I");
    bysetpos_fields.array = env->GetFieldID(clazz, "bysetpos", "[I");
    bysetpos_fields.count = env->GetFieldID(clazz, "bysetposCount", "I");

    return jniRegisterNativeMethods(env, CLASS_NAME,
        METHODS, sizeof(METHODS)/sizeof(METHODS[0]));
}

}; // namespace android

include/pim/EventRecurrence.h

deleted100644 → 0
+0 −82
Original line number Diff line number Diff line
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//
#ifndef _PIM_EVENT_RECURRENCE_H
#define _PIM_EVENT_RECURRENCE_H

#include <utils/String16.h>

namespace android {

struct EventRecurrence
{
public:
                EventRecurrence();
                ~EventRecurrence();
    
    status_t    parse(const String16&);


    enum freq_t {
        SECONDLY = 1,
        MINUTELY = 2,
        HOURLY = 3,
        DAILY = 4,
        WEEKLY = 5,
        MONTHLY = 6,
        YEARLY = 7
    };

    enum {
        SU = 0x00010000,
        MO = 0x00020000,
        TU = 0x00040000,
        WE = 0x00080000,
        TH = 0x00100000,
        FR = 0x00200000,
        SA = 0x00400000
    };
    
    freq_t    freq;
    String16  until;
    int       count;
    int       interval;
    int*      bysecond;
    int       bysecondCount;
    int*      byminute;
    int       byminuteCount;
    int*      byhour;
    int       byhourCount;
    int*      byday;
    int*      bydayNum;
    int       bydayCount;   
    int*      bymonthday;
    int       bymonthdayCount;
    int*      byyearday;
    int       byyeardayCount;
    int*      byweekno;
    int       byweeknoCount;
    int*      bymonth;
    int       bymonthCount;
    int*      bysetpos;
    int       bysetposCount;
    int       wkst;
};

}; // namespace android

#endif // _PIM_EVENT_RECURRENCE_H
Loading