Easy to use event detection based on time. Time Sense provide default set of rules that can trigger an event listeners. By default we have four rules; morning, afternoon, evening, and night but developer can add their own rules to trigger event listeners.
Add jitpack repository
repositories {
// another repositories
maven { url 'https://jitpack.io' }
}
Add dependencies in build.gradle file
dependencies {
// another dependencies
compile 'com.github.archansel:TimeSense:0.1'
}
See example project.
Rule object used in TimeSense detection.
By default there are four rules constants that already defined in the library:
TS_RULE_NAME_MORNING
, 04:00 <= T < 11:00TS_RULE_NAME_AFTERNOON
, 11:00 <= T < 17:00TS_RULE_NAME_EVENING
, 17:00 <= T < 21:00TS_RULE_NAME_NIGHT
, 21:00 <= T < 04:00
Quite easy to create TSRule object, just use its object constructor that accept three parameters: name (String), start time (Date), and end time (Date).
TSRule rule = new TSRule(ruleName, startTime, endTime);
Aside from its default constructor, we provide a fast way to generate each default rules.
TSRule.morning();
TSRule.afternoon();
TSRule.evening();
TSRule.night();
name
, used as rule identifierstartTime
, start time of the rule (included in calculation)endTime
, end time of the rule (excluded in calculation)
TSRule.equals(TSRule)
, check whether current rule match another rule. ReturnBoolean
TSRule.getName()
, getter for name property. ReturnString
TSRule.setName(String)
, setter for name propertyTSRule.getStartTime()
, getter for startTime property. ReturnDate
TSRule.setStartTime(Date)
, setter for startTime propertyTSRule.getEndTime()
, getter for endTime property. ReturnDate
TSRule.setEndTime(Date)
, setter for endTime property
Configuration object for TimeSense class. TSConfiguration might contains some rules that will be used by TimeSense detection.
There are three ways how you can create TSConfiguration object:
TSConfiguration.defaultConfiguration()
The easiest way to generate TSConfiguration object. It will return new TSConfiguration object with four rules defined in it (TS_RULE_NAME_MORNING
, TS_RULE_NAME_AFTERNOON
, TS_RULE_NAME_EVENING
, TS_RULE_NAME_NIGHT
).
new TSConfiguration()
Empty configuration object. No rules are defined in it, you have to add it manualy.
new TSConfiguration(configuration)
Create TSConfiguration object based on another TSConfiguration object. The new configuration object will copy every rules defined in the source configuration object.
We also provide a builder class TSConfiguration.Builder
that can be used to build configuration object.
TSConfiguration.create().addRule(TSRule).addRule(TSRule).done()
TSConfiguration.getRules()
, get all rules in configuration. ReturnArrayList
of TSRuleTSConfiguration.setRules(ArrayList<TSRule>)
, set rules in configuration replacing the old oneTSConfiguration.addRules(ArrayList<TSRule>)
, add rules to existing rules in configurationTSConfiguration.addRule(TSRule)
, add rule object to the configurationTSConfiguration.addRule(ruleName, startTime, endTime)
, add rule object as separated rule propertyTSConfiguration.updateRule(ruleName, startTime, endTime)
, update rule based on its rule name with new start and end time. ReturnBoolean
whether rule updated or notTSConfiguration.removeRule(ruleName)
, remove rule name from configurationTSConfiguration.findRule(ruleName)
, find rule with name from configuration
Interface class that can listen to TimeSense event.
TSListener.timeSenseTriggered()
, triggered only if listen to all rules (not using specific rule name)TSListener.timeSenseTriggered(ruleName)
, triggered if certain rule match
Singleton class that handle all the detection logic and handling. Can be accessed using TimeSense.getInstance()
TimeSense will use empty configuration (no rules defined) if no TSConfiguration provided by developer. To change configuration use
TimeSense.setConfiguration(TSConfiguration)
Detection methods:
TimeSense.detect()
, find matching rule for current time. ReturnArrayList<TSRule>
TimeSense.detect(time)
, find matching rule for time. ReturnArrayList<TSRule>
TimeSense.isMorning()
, check whether current time is morning (based on TS_RULE_NAME_MORNING) or not. ReturnBoolean
TimeSense.isMorning(time)
, check whether time is morning (based on TS_RULE_NAME_MORNING) or not. ReturnBoolean
TimeSense.isAfternoon()
, check whether current time is afternoon (based on TS_RULE_NAME_AFTERNOON) or not. ReturnBoolean
TimeSense.isAfternoon(time)
, check whether time is afternoon (based on TS_RULE_NAME_AFTERNOON) or not. ReturnBoolean
TimeSense.isEvening()
, check whether current time is evening (based on TS_RULE_NAME_EVENING) or not. ReturnBoolean
TimeSense.isEvening(time)
, check whether time is evening (based on TS_RULE_NAME_EVENING) or not. ReturnBoolean
TimeSense.isNight()
, check whether current time is night (based on TS_RULE_NAME_NIGHT) or not. ReturnBoolean
TimeSense.isNight(time)
, check whether time is night (based on TS_RULE_NAME_NIGHT) or not. ReturnBoolean
TimeSense.isMatch(TSRule)
, check whether current time is inside rule time interval or not. ReturnBoolean
TimeSense.iSMatch(TSRule, time)
, check whether time is inside rule time interval or not. ReturnBoolean
TimeSense.trigger()
, trigger all listeners for current time that match the rule detected inTimeSense.detect()
TimeSense.trigger(time)
, trigger all listeners for time that match the rule detected inTimeSense.detect(time)
TimeSense.triggerMatch(TSRule)
, trigger all listeners for rule in current time if matchTimeSense.triggerMatch(TSRule, time)
, trigger all listeners for rule in time if match
Rule methods:
TimeSense.addRules(ArrayList<TSRule>)
, add rules for detectionTimeSense.addRule(TSRule)
, add rule for detectionTimeSense.addRule(ruleName, startTime, endTime)
, add rule using name, start time, and end timeTimeSense.updateRule(ruleName, startTime, endTime)
, update existing rule with start time and end time. ReturnBoolean
whether rule updated or notTimeSense.removeRule(ruleName)
, remove rule by rule nameTimeSense.getTimeRange(ruleName)
, get start time and end time for certain rule. ReturnArrayList<Date>
, start time and end time
Listener methods:
TimeSense.addListener(listener)
, add listener for all rules defined in TimeSenseTimeSense.addListener(ruleName, listener)
, add listener for rule nameTimeSense.removeListeners()
, remove all listeners in TimeSenseTimeSense.removeListener(listener)
, remove listener from TimeSenseTimeSense.removeListener(ruleName)
, remove all listeners for rule nameTimeSense.removeListener(ruleName, listener)
, remove specific listener for rule name
MIT License
Copyright (c) 2017 Anselmus KA Kurniawan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.