Skip to content

Commit c22dbdc

Browse files
committed
Merge branch 'dev'
2 parents 4f8a196 + 05657b2 commit c22dbdc

29 files changed

Lines changed: 1019 additions & 277 deletions
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2016 - 2018 Rui Zhao <renyuneyun@gmail.com>
3+
*
4+
* This file is part of Easer.
5+
*
6+
* Easer is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* Easer is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with Easer. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
package ryey.easer.plugins.event.condition_event;
21+
22+
import android.os.Parcel;
23+
24+
import org.junit.Test;
25+
26+
import ryey.easer.plugins.TestHelper;
27+
28+
import static org.junit.Assert.assertEquals;
29+
30+
public class ConditionEventEventDataTest {
31+
32+
@Test
33+
public void testParcel() {
34+
ConditionEventEventData dummyData = new ConditionEventEventDataFactory().dummyData();
35+
Parcel parcel = TestHelper.writeToParcel(dummyData);
36+
ConditionEventEventData parceledData = ConditionEventEventData.CREATOR.createFromParcel(parcel);
37+
assertEquals(dummyData, parceledData);
38+
}
39+
40+
}

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,14 @@
132132
android:enabled="true"
133133
android:exported="false">
134134
<intent-filter>
135-
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
135+
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
136136
</intent-filter>
137137
</receiver>
138+
139+
<service
140+
android:name=".core.ConditionHolderService"
141+
android:enabled="true"
142+
android:exported="false"></service>
138143
</application>
139144

140145
</manifest>
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* Copyright (c) 2016 - 2018 Rui Zhao <renyuneyun@gmail.com>
3+
*
4+
* This file is part of Easer.
5+
*
6+
* Easer is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* Easer is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with Easer. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
package ryey.easer.core;
21+
22+
import android.app.PendingIntent;
23+
import android.app.Service;
24+
import android.content.BroadcastReceiver;
25+
import android.content.Context;
26+
import android.content.Intent;
27+
import android.content.IntentFilter;
28+
import android.net.Uri;
29+
import android.os.Binder;
30+
import android.os.IBinder;
31+
import android.os.PatternMatcher;
32+
import android.support.v4.util.ArraySet;
33+
34+
import com.orhanobut.logger.Logger;
35+
36+
import java.util.HashMap;
37+
import java.util.Locale;
38+
import java.util.Map;
39+
import java.util.Set;
40+
41+
import ryey.easer.commons.plugindef.conditionplugin.ConditionData;
42+
import ryey.easer.commons.plugindef.conditionplugin.Tracker;
43+
import ryey.easer.core.data.ConditionStructure;
44+
import ryey.easer.core.data.storage.ConditionDataStorage;
45+
import ryey.easer.plugins.PluginRegistry;
46+
47+
public class ConditionHolderService extends Service {
48+
49+
private static final String ACTION_TRACKER_SATISFIED = "ryey.easer.triggerlotus.action.TRACKER_SATISFIED";
50+
private static final String ACTION_TRACKER_UNSATISFIED = "ryey.easer.triggerlotus.action.TRACKER_UNSATISFIED";
51+
private static final String CATEGORY_NOTIFY_HOLDER = "ryey.easer.triggerlotus.category.NOTIFY_HOLDER";
52+
53+
//FIXME concurrent
54+
private Map<String, Tracker> trackerMap = new HashMap<>();
55+
private Map<String, Set<Lotus.NotifyPendingIntents>> associateMap = new HashMap<>();
56+
57+
private final Uri uri = Uri.parse(String.format(Locale.US, "conditionholder://%d/", hashCode()));
58+
59+
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
60+
@Override
61+
public void onReceive(Context context, Intent intent) {
62+
try {
63+
if (ACTION_TRACKER_SATISFIED.equals(intent.getAction()) || ACTION_TRACKER_UNSATISFIED.equals(intent.getAction())) {
64+
String name = intent.getData().getLastPathSegment();
65+
if (intent.getAction().equals(ACTION_TRACKER_SATISFIED)) {
66+
for (Lotus.NotifyPendingIntents pendingIntents : associateMap.get(name)) {
67+
try {
68+
pendingIntents.positive.send();
69+
} catch (PendingIntent.CanceledException e) {
70+
e.printStackTrace();
71+
}
72+
}
73+
} else if (intent.getAction().equals(ACTION_TRACKER_UNSATISFIED)) {
74+
for (Lotus.NotifyPendingIntents pendingIntents : associateMap.get(name)) {
75+
try {
76+
pendingIntents.negative.send();
77+
} catch (PendingIntent.CanceledException e) {
78+
e.printStackTrace();
79+
}
80+
}
81+
}
82+
}
83+
} catch (NullPointerException e) {
84+
Logger.e(e, "ConditionHolder's BroadcastListener shouldn't hear invalid Intent");
85+
}
86+
}
87+
};
88+
private final IntentFilter filter;
89+
90+
{
91+
filter = new IntentFilter();
92+
filter.addAction(ACTION_TRACKER_SATISFIED);
93+
filter.addAction(ACTION_TRACKER_UNSATISFIED);
94+
filter.addCategory(CATEGORY_NOTIFY_HOLDER);
95+
filter.addDataScheme(uri.getScheme());
96+
filter.addDataAuthority(uri.getAuthority(), null);
97+
filter.addDataPath(uri.getPath(), PatternMatcher.PATTERN_PREFIX);
98+
}
99+
100+
public ConditionHolderService() {
101+
}
102+
103+
@Override
104+
public void onCreate() {
105+
super.onCreate();
106+
registerReceiver(mReceiver, filter);
107+
108+
ConditionDataStorage conditionDataStorage = ConditionDataStorage.getInstance(this);
109+
for (String name : conditionDataStorage.list()) {
110+
Intent intent = new Intent(ACTION_TRACKER_SATISFIED);
111+
Uri turi = uri.buildUpon().appendPath(name).build();
112+
intent.addCategory(CATEGORY_NOTIFY_HOLDER);
113+
intent.setData(turi);
114+
PendingIntent positive = PendingIntent.getBroadcast(this, 0, intent, 0);
115+
intent.setAction(ACTION_TRACKER_UNSATISFIED);
116+
PendingIntent negative = PendingIntent.getBroadcast(this, 0, intent, 0);
117+
118+
ConditionStructure conditionStructure = conditionDataStorage.get(name);
119+
ConditionData conditionData = conditionStructure.getData();
120+
Tracker tracker = PluginRegistry.getInstance().condition().findPlugin(conditionData)
121+
.tracker(this, conditionData, positive, negative);
122+
tracker.start();
123+
trackerMap.put(name, tracker);
124+
associateMap.put(name, new ArraySet<Lotus.NotifyPendingIntents>());
125+
}
126+
}
127+
128+
@Override
129+
public void onDestroy() {
130+
super.onDestroy();
131+
unregisterReceiver(mReceiver);
132+
for (Tracker tracker : trackerMap.values()) {
133+
tracker.stop();
134+
}
135+
trackerMap.clear();
136+
associateMap.clear();
137+
}
138+
139+
@Override
140+
public IBinder onBind(Intent intent) {
141+
return new CHBinder();
142+
}
143+
144+
class CHBinder extends Binder {
145+
void registerAssociation(String conditionName, Lotus.NotifyPendingIntents pendingIntents) {
146+
associateMap.get(conditionName).add(pendingIntents);
147+
}
148+
void unregisterAssociation(String conditionName, Lotus.NotifyPendingIntents pendingIntents) {
149+
associateMap.get(conditionName).remove(pendingIntents);
150+
}
151+
void clearAssociation() {
152+
for (String name :associateMap.keySet()) {
153+
associateMap.get(name).clear();
154+
}
155+
}
156+
Boolean conditionState(String conditionName) {
157+
Tracker tracker = trackerMap.get(conditionName);
158+
return tracker.state();
159+
}
160+
}
161+
}

app/src/main/java/ryey/easer/core/ConditionLotus.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@
3030
class ConditionLotus extends Lotus {
3131
private final ConditionStructure conditionStructure;
3232

33-
ConditionLotus(@NonNull Context context, @NonNull ScriptTree scriptTree, @NonNull ExecutorService executorService, @NonNull EHService.ConditionHolder conditionHolder) {
34-
super(context, scriptTree, executorService, conditionHolder);
33+
ConditionLotus(@NonNull Context context, @NonNull ScriptTree scriptTree, @NonNull ExecutorService executorService, @NonNull ConditionHolderService.CHBinder chBinder) {
34+
super(context, scriptTree, executorService, chBinder);
3535
conditionStructure = scriptTree.getCondition();
3636
}
3737

3838
@Override
3939
protected void onListen() {
40-
conditionHolder.registerAssociation(conditionStructure.getName(), notifyPendingIntents);
41-
Boolean state = conditionHolder.conditionState(conditionStructure.getName());
40+
chBinder.registerAssociation(conditionStructure.getName(), notifyPendingIntents);
41+
Boolean state = chBinder.conditionState(conditionStructure.getName());
4242
if (state == null) {
4343
} else {
44-
if (state) {
44+
if (state != scriptTree.isReversed()) {
4545
onSatisfied();
4646
} else {
4747
onUnsatisfied();
@@ -51,6 +51,6 @@ protected void onListen() {
5151

5252
@Override
5353
protected void onCancel() {
54-
conditionHolder.unregisterAssociation(conditionStructure.getName(), notifyPendingIntents);
54+
chBinder.unregisterAssociation(conditionStructure.getName(), notifyPendingIntents);
5555
}
5656
}

0 commit comments

Comments
 (0)