|
| 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 | +} |
0 commit comments