Home | History | Annotate | Download | only in trigger
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.googlecode.android_scripting.trigger;
     18 
     19 import com.google.common.collect.Maps;
     20 import com.googlecode.android_scripting.facade.FacadeConfiguration;
     21 import com.googlecode.android_scripting.facade.FacadeManager;
     22 import com.googlecode.android_scripting.rpc.MethodDescriptor;
     23 import com.googlecode.android_scripting.trigger.TriggerRepository.TriggerRepositoryObserver;
     24 
     25 import java.util.Map;
     26 
     27 import org.json.JSONArray;
     28 
     29 /**
     30  * A {@link TriggerRepositoryObserver} that starts and stops the monitoring of events depending on
     31  * whether or not triggers for the event exist.
     32  *
     33  */
     34 public class EventGenerationControllingObserver implements TriggerRepositoryObserver {
     35   private final FacadeManager mFacadeManager;
     36   private final Map<String, MethodDescriptor> mStartEventGeneratingMethodDescriptors;
     37   private final Map<String, MethodDescriptor> mStopEventGeneratingMethodDescriptors;
     38   private final Map<String, Integer> mEventTriggerRefCounts = Maps.newHashMap();
     39 
     40   /**
     41    * Creates a new StartEventMonitoringObserver for the given trigger repository.
     42    *
     43    * @param facadeManager
     44    * @param triggerRepository
     45    */
     46   public EventGenerationControllingObserver(FacadeManager facadeManager) {
     47     mFacadeManager = facadeManager;
     48     mStartEventGeneratingMethodDescriptors =
     49         FacadeConfiguration.collectStartEventMethodDescriptors();
     50     mStopEventGeneratingMethodDescriptors = FacadeConfiguration.collectStopEventMethodDescriptors();
     51   }
     52 
     53   private synchronized int incrementAndGetRefCount(String eventName) {
     54     int refCount =
     55         (mEventTriggerRefCounts.containsKey(eventName)) ? mEventTriggerRefCounts.get(eventName) : 0;
     56     refCount++;
     57     mEventTriggerRefCounts.put(eventName, refCount);
     58     return refCount;
     59   }
     60 
     61   private synchronized int decrementAndGetRefCount(String eventName) {
     62     int refCount =
     63         (mEventTriggerRefCounts.containsKey(eventName)) ? mEventTriggerRefCounts.get(eventName) : 0;
     64     refCount--;
     65     mEventTriggerRefCounts.put(eventName, refCount);
     66     return refCount;
     67   }
     68 
     69   @Override
     70   public synchronized void onPut(Trigger trigger) {
     71     // If we're not already monitoring the events corresponding to this trigger, do so.
     72     if (incrementAndGetRefCount(trigger.getEventName()) == 1) {
     73       startMonitoring(trigger.getEventName());
     74     }
     75   }
     76 
     77   @Override
     78   public synchronized void onRemove(Trigger trigger) {
     79     // If there are no more triggers listening to this event, then we need to stop monitoring.
     80     if (decrementAndGetRefCount(trigger.getEventName()) == 1) {
     81       stopMonitoring(trigger.getEventName());
     82     }
     83   }
     84 
     85   private void startMonitoring(String eventName) {
     86     MethodDescriptor startEventGeneratingMethod =
     87         mStartEventGeneratingMethodDescriptors.get(eventName);
     88     try {
     89       startEventGeneratingMethod.invoke(mFacadeManager, new JSONArray());
     90     } catch (Throwable t) {
     91       throw new RuntimeException(t);
     92     }
     93   }
     94 
     95   private void stopMonitoring(String eventName) {
     96     MethodDescriptor stopEventGeneratingMethod =
     97         mStopEventGeneratingMethodDescriptors.get(eventName);
     98     try {
     99       stopEventGeneratingMethod.invoke(mFacadeManager, new JSONArray());
    100     } catch (Throwable t) {
    101       throw new RuntimeException(t);
    102     }
    103   }
    104 }
    105