1/*
2 * Copyright (C) 2007 Esmertec AG.
3 * Copyright (C) 2007 The Android Open Source Project
4 *
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
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
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
15 * limitations under the License.
16 */
17
18package com.android.mms.dom.events;
19
20import com.android.mms.LogTag;
21
22import java.util.ArrayList;
23
24import org.w3c.dom.events.Event;
25import org.w3c.dom.events.EventException;
26import org.w3c.dom.events.EventListener;
27import org.w3c.dom.events.EventTarget;
28
29import android.util.Log;
30
31public class EventTargetImpl implements EventTarget {
32    private static final String TAG = LogTag.TAG;
33    private ArrayList<EventListenerEntry> mListenerEntries;
34    private EventTarget mNodeTarget;
35
36    static class EventListenerEntry
37    {
38        final String mType;
39        final EventListener mListener;
40        final boolean mUseCapture;
41
42        EventListenerEntry(String type, EventListener listener, boolean useCapture)
43        {
44            mType = type;
45            mListener = listener;
46            mUseCapture = useCapture;
47        }
48    }
49
50    public EventTargetImpl(EventTarget target) {
51        mNodeTarget = target;
52    }
53
54    public void addEventListener(String type, EventListener listener, boolean useCapture) {
55        if ((type == null) || type.equals("") || (listener == null)) {
56            return;
57        }
58
59        // Make sure we have only one entry
60        removeEventListener(type, listener, useCapture);
61
62        if (mListenerEntries == null) {
63            mListenerEntries = new ArrayList<EventListenerEntry>();
64        }
65        mListenerEntries.add(new EventListenerEntry(type, listener, useCapture));
66    }
67
68    public boolean dispatchEvent(Event evt) throws EventException {
69        // We need to use the internal APIs to modify and access the event status
70        EventImpl eventImpl = (EventImpl)evt;
71
72        if (!eventImpl.isInitialized()) {
73            throw new EventException(EventException.UNSPECIFIED_EVENT_TYPE_ERR,
74                    "Event not initialized");
75        } else if ((eventImpl.getType() == null) || eventImpl.getType().equals("")) {
76            throw new EventException(EventException.UNSPECIFIED_EVENT_TYPE_ERR,
77                    "Unspecified even type");
78        }
79
80        // Initialize event status
81        eventImpl.setTarget(mNodeTarget);
82
83        // TODO: At this point, to support event capturing and bubbling, we should
84        // establish the chain of EventTargets from the top of the tree to this
85        // event's target.
86
87        // TODO: CAPTURING_PHASE skipped
88
89        // Handle AT_TARGET
90        // Invoke handleEvent of non-capturing listeners on this EventTarget.
91        eventImpl.setEventPhase(Event.AT_TARGET);
92        eventImpl.setCurrentTarget(mNodeTarget);
93        if (!eventImpl.isPropogationStopped() && (mListenerEntries != null)) {
94            for (int i = 0; i < mListenerEntries.size(); i++) {
95                EventListenerEntry listenerEntry = mListenerEntries.get(i);
96                if (!listenerEntry.mUseCapture
97                        && listenerEntry.mType.equals(eventImpl.getType())) {
98                    try {
99                        listenerEntry.mListener.handleEvent(eventImpl);
100                    }
101                    catch (Exception e) {
102                        // Any exceptions thrown inside an EventListener will
103                        // not stop propagation of the event
104                        Log.w(TAG, "Catched EventListener exception", e);
105                    }
106                }
107            }
108        }
109
110        if (eventImpl.getBubbles()) {
111            // TODO: BUBBLING_PHASE skipped
112        }
113
114        return eventImpl.isPreventDefault();
115    }
116
117    public void removeEventListener(String type, EventListener listener,
118            boolean useCapture) {
119        if (null == mListenerEntries) {
120            return;
121        }
122        for (int i = 0; i < mListenerEntries.size(); i ++) {
123            EventListenerEntry listenerEntry = mListenerEntries.get(i);
124            if ((listenerEntry.mUseCapture == useCapture)
125                    && (listenerEntry.mListener == listener)
126                    && listenerEntry.mType.equals(type)) {
127                mListenerEntries.remove(i);
128                break;
129            }
130        }
131    }
132
133}
134