Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
renyuneyun committed May 13, 2018
2 parents 4f8a196 + 05657b2 commit c22dbdc
Show file tree
Hide file tree
Showing 29 changed files with 1,019 additions and 277 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2016 - 2018 Rui Zhao <renyuneyun@gmail.com>
*
* This file is part of Easer.
*
* Easer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Easer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Easer. If not, see <http://www.gnu.org/licenses/>.
*/

package ryey.easer.plugins.event.condition_event;

import android.os.Parcel;

import org.junit.Test;

import ryey.easer.plugins.TestHelper;

import static org.junit.Assert.assertEquals;

public class ConditionEventEventDataTest {

@Test
public void testParcel() {
ConditionEventEventData dummyData = new ConditionEventEventDataFactory().dummyData();
Parcel parcel = TestHelper.writeToParcel(dummyData);
ConditionEventEventData parceledData = ConditionEventEventData.CREATOR.createFromParcel(parcel);
assertEquals(dummyData, parceledData);
}

}
7 changes: 6 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,14 @@
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>

<service
android:name=".core.ConditionHolderService"
android:enabled="true"
android:exported="false"></service>
</application>

</manifest>
161 changes: 161 additions & 0 deletions app/src/main/java/ryey/easer/core/ConditionHolderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* Copyright (c) 2016 - 2018 Rui Zhao <renyuneyun@gmail.com>
*
* This file is part of Easer.
*
* Easer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Easer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Easer. If not, see <http://www.gnu.org/licenses/>.
*/

package ryey.easer.core;

import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.os.PatternMatcher;
import android.support.v4.util.ArraySet;

import com.orhanobut.logger.Logger;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import ryey.easer.commons.plugindef.conditionplugin.ConditionData;
import ryey.easer.commons.plugindef.conditionplugin.Tracker;
import ryey.easer.core.data.ConditionStructure;
import ryey.easer.core.data.storage.ConditionDataStorage;
import ryey.easer.plugins.PluginRegistry;

public class ConditionHolderService extends Service {

private static final String ACTION_TRACKER_SATISFIED = "ryey.easer.triggerlotus.action.TRACKER_SATISFIED";
private static final String ACTION_TRACKER_UNSATISFIED = "ryey.easer.triggerlotus.action.TRACKER_UNSATISFIED";
private static final String CATEGORY_NOTIFY_HOLDER = "ryey.easer.triggerlotus.category.NOTIFY_HOLDER";

//FIXME concurrent
private Map<String, Tracker> trackerMap = new HashMap<>();
private Map<String, Set<Lotus.NotifyPendingIntents>> associateMap = new HashMap<>();

private final Uri uri = Uri.parse(String.format(Locale.US, "conditionholder://%d/", hashCode()));

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
try {
if (ACTION_TRACKER_SATISFIED.equals(intent.getAction()) || ACTION_TRACKER_UNSATISFIED.equals(intent.getAction())) {
String name = intent.getData().getLastPathSegment();
if (intent.getAction().equals(ACTION_TRACKER_SATISFIED)) {
for (Lotus.NotifyPendingIntents pendingIntents : associateMap.get(name)) {
try {
pendingIntents.positive.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
} else if (intent.getAction().equals(ACTION_TRACKER_UNSATISFIED)) {
for (Lotus.NotifyPendingIntents pendingIntents : associateMap.get(name)) {
try {
pendingIntents.negative.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
}
}
} catch (NullPointerException e) {
Logger.e(e, "ConditionHolder's BroadcastListener shouldn't hear invalid Intent");
}
}
};
private final IntentFilter filter;

{
filter = new IntentFilter();
filter.addAction(ACTION_TRACKER_SATISFIED);
filter.addAction(ACTION_TRACKER_UNSATISFIED);
filter.addCategory(CATEGORY_NOTIFY_HOLDER);
filter.addDataScheme(uri.getScheme());
filter.addDataAuthority(uri.getAuthority(), null);
filter.addDataPath(uri.getPath(), PatternMatcher.PATTERN_PREFIX);
}

public ConditionHolderService() {
}

@Override
public void onCreate() {
super.onCreate();
registerReceiver(mReceiver, filter);

ConditionDataStorage conditionDataStorage = ConditionDataStorage.getInstance(this);
for (String name : conditionDataStorage.list()) {
Intent intent = new Intent(ACTION_TRACKER_SATISFIED);
Uri turi = uri.buildUpon().appendPath(name).build();
intent.addCategory(CATEGORY_NOTIFY_HOLDER);
intent.setData(turi);
PendingIntent positive = PendingIntent.getBroadcast(this, 0, intent, 0);
intent.setAction(ACTION_TRACKER_UNSATISFIED);
PendingIntent negative = PendingIntent.getBroadcast(this, 0, intent, 0);

ConditionStructure conditionStructure = conditionDataStorage.get(name);
ConditionData conditionData = conditionStructure.getData();
Tracker tracker = PluginRegistry.getInstance().condition().findPlugin(conditionData)
.tracker(this, conditionData, positive, negative);
tracker.start();
trackerMap.put(name, tracker);
associateMap.put(name, new ArraySet<Lotus.NotifyPendingIntents>());
}
}

@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
for (Tracker tracker : trackerMap.values()) {
tracker.stop();
}
trackerMap.clear();
associateMap.clear();
}

@Override
public IBinder onBind(Intent intent) {
return new CHBinder();
}

class CHBinder extends Binder {
void registerAssociation(String conditionName, Lotus.NotifyPendingIntents pendingIntents) {
associateMap.get(conditionName).add(pendingIntents);
}
void unregisterAssociation(String conditionName, Lotus.NotifyPendingIntents pendingIntents) {
associateMap.get(conditionName).remove(pendingIntents);
}
void clearAssociation() {
for (String name :associateMap.keySet()) {
associateMap.get(name).clear();
}
}
Boolean conditionState(String conditionName) {
Tracker tracker = trackerMap.get(conditionName);
return tracker.state();
}
}
}
12 changes: 6 additions & 6 deletions app/src/main/java/ryey/easer/core/ConditionLotus.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@
class ConditionLotus extends Lotus {
private final ConditionStructure conditionStructure;

ConditionLotus(@NonNull Context context, @NonNull ScriptTree scriptTree, @NonNull ExecutorService executorService, @NonNull EHService.ConditionHolder conditionHolder) {
super(context, scriptTree, executorService, conditionHolder);
ConditionLotus(@NonNull Context context, @NonNull ScriptTree scriptTree, @NonNull ExecutorService executorService, @NonNull ConditionHolderService.CHBinder chBinder) {
super(context, scriptTree, executorService, chBinder);
conditionStructure = scriptTree.getCondition();
}

@Override
protected void onListen() {
conditionHolder.registerAssociation(conditionStructure.getName(), notifyPendingIntents);
Boolean state = conditionHolder.conditionState(conditionStructure.getName());
chBinder.registerAssociation(conditionStructure.getName(), notifyPendingIntents);
Boolean state = chBinder.conditionState(conditionStructure.getName());
if (state == null) {
} else {
if (state) {
if (state != scriptTree.isReversed()) {
onSatisfied();
} else {
onUnsatisfied();
Expand All @@ -51,6 +51,6 @@ protected void onListen() {

@Override
protected void onCancel() {
conditionHolder.unregisterAssociation(conditionStructure.getName(), notifyPendingIntents);
chBinder.unregisterAssociation(conditionStructure.getName(), notifyPendingIntents);
}
}
Loading

0 comments on commit c22dbdc

Please sign in to comment.