summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/launcher/cardprovider/DashClockExtensionCardProvider.java
blob: afcbdc842943ede35401af287b471f7cb72a727f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package org.cyanogenmod.launcher.cardprovider;

import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import org.cyanogenmod.launcher.cards.CmCard;
import org.cyanogenmod.launcher.cards.DashClockExtensionCard;
import org.cyanogenmod.launcher.dashclock.ExtensionHost;
import org.cyanogenmod.launcher.dashclock.ExtensionManager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import it.gmariotti.cardslib.library.internal.Card;

/**
 * Manages fetching data from all installed DashClock extensions
 * and generates cards to be displayed.
 */
public class DashClockExtensionCardProvider implements ICardProvider, ExtensionManager.OnChangeListener {
    public static final String TAG = "DashClockExtensionCardProvider";
    public static final String EXTENSION_TIMEOUT_SHARED_PREF_FILE = "DashClockExtensionTimeouts";
    public static final int CARD_REAPPEAR_TIME_IN_MINUTES = 180; // three hours

    private ExtensionManager mExtensionManager;
    private ExtensionHost mExtensionHost;
    private Context mContext;
    private Context mHostActivityContext;
    private List<CardProviderUpdateListener> mUpdateListeners = new ArrayList<CardProviderUpdateListener>();

    public DashClockExtensionCardProvider(Context context, Context hostActivityContext) {
        mContext = context;
        mHostActivityContext = hostActivityContext;
        mExtensionManager = ExtensionManager.getInstance(context, hostActivityContext);
        mExtensionManager.addOnChangeListener(this);
        mExtensionHost = new ExtensionHost(context, hostActivityContext);

        trackAllExtensions();
    }

    @Override
    public void onShow() {
        mExtensionHost.init();
        mExtensionManager.addOnChangeListener(this);
        trackAllExtensions();
    }

    @Override
    public void onDestroy(Context context) {
        mExtensionManager.removeOnChangeListener(this);
        mExtensionHost.destroy();
        mExtensionManager.setActiveExtensions(new ArrayList<ComponentName>());
    }

    @Override
    public void onHide(Context context) {
        // Tear down the extension connections when the app is hidden,
        // so that we don't block other readers (i.e. actual dashclock).
        mExtensionManager.removeOnChangeListener(this);
        mExtensionHost.destroy();
        mExtensionManager.setActiveExtensions(new ArrayList<ComponentName>());
    }

    @Override
    public List<CmCard> getCards() {
        List<CmCard> cards = new ArrayList<CmCard>();

        for(ExtensionManager.ExtensionWithData extensionWithData :
                mExtensionManager.getActiveExtensionsWithData()) {
            if(extensionWithData.latestData != null
               && extensionWithData.latestData.visible()
               && shouldReappear(extensionWithData.listing.componentName.flattenToString())
               && !TextUtils.isEmpty(extensionWithData.latestData.status())) {
                DashClockExtensionCard card = new DashClockExtensionCard(mContext,
                                                       extensionWithData,
                                                       mHostActivityContext);
                setCardSwipeAndUndoListeners(card);
                cards.add(card);
            }
        }

        return cards;
    }

    @Override
    public void requestRefresh() {
        trackAllExtensions();
        mExtensionHost.requestAllManualUpdate();
    }

    @Override
    public CardProviderUpdateResult updateAndAddCards(List<CmCard> cards) {
        List<ExtensionManager.ExtensionWithData> extensions
                = mExtensionManager.getActiveExtensionsWithData();

        // A List of cards to return that must be removed
        List<CmCard> cardsToRemove = new ArrayList<CmCard>();

        // Create a map from ComponentName String -> extensionWithData
        HashMap<String, ExtensionManager.ExtensionWithData> map
                = new HashMap<String, ExtensionManager.ExtensionWithData>();
        for(ExtensionManager.ExtensionWithData extension : extensions) {
            map.put(extension.listing.componentName.flattenToString(), extension);
        }

        for(CmCard card : cards) {
            if(card instanceof DashClockExtensionCard) {
                DashClockExtensionCard dashClockExtensionCard
                        = (DashClockExtensionCard) card;
                if(map.containsKey(dashClockExtensionCard
                        .getFlattenedComponentNameString())) {
                    ExtensionManager.ExtensionWithData extensionWithData
                            = map.get(dashClockExtensionCard
                                      .getFlattenedComponentNameString());
                    if (extensionWithData.latestData.visible()
                        && shouldReappear(extensionWithData.
                            listing.componentName.flattenToString())) {
                        dashClockExtensionCard
                                .updateFromExtensionWithData(extensionWithData);
                    } else {
                        cardsToRemove.add(dashClockExtensionCard);
                    }
                    map.remove(dashClockExtensionCard.getFlattenedComponentNameString());
                }
            }
        }

        // A List of cards to return that must be added
        List<CmCard> cardsToAdd = new ArrayList<CmCard>();

        // Create new cards for extensions that were not represented
        for(Map.Entry<String, ExtensionManager.ExtensionWithData> entry : map.entrySet()) {
            ExtensionManager.ExtensionWithData extension = entry.getValue();

            if(extension.latestData != null && !TextUtils.isEmpty(extension.latestData.status())) {
                DashClockExtensionCard card =
                        new DashClockExtensionCard(mContext, extension, mHostActivityContext);
                if (extension.latestData.visible()
                    && shouldReappear(extension.listing.componentName.flattenToString())) {
                    setCardSwipeAndUndoListeners(card);
                    cardsToAdd.add(card);
                }
            }
        }

        return new CardProviderUpdateResult(cardsToAdd, cardsToRemove);
    }

    private void setCardSwipeAndUndoListeners(DashClockExtensionCard card) {
        card.setOnSwipeListener(new Card.OnSwipeListener() {
            @Override
            public void onSwipe(Card card) {
                storeReappearTime(card.getId(),
                                  getReappearTimeFromNow());
            }
        });

        card.setOnUndoSwipeListListener(new Card.OnUndoSwipeListListener() {
            @Override
            public void onUndoSwipe(Card card, boolean timedOut) {
                if (!timedOut) {
                    clearReappearTime(card.getId());
                }
            }
        });
    }

    @Override
    public void updateCard(CmCard card) {
        if (!(card instanceof DashClockExtensionCard)) {
            return;
        }

        List<ExtensionManager.ExtensionWithData> extensions
                = mExtensionManager.getActiveExtensionsWithData();

        for(ExtensionManager.ExtensionWithData extension : extensions) {
            if (extension.listing.componentName.flattenToString()
                    .equals(card.getId())) {
                ((DashClockExtensionCard) card)
                        .updateFromExtensionWithData(extension);
            }
        }
    }

    public CmCard createCardForId(String id) {
        List<ExtensionManager.ExtensionWithData> extensions
                = mExtensionManager.getActiveExtensionsWithData();

        for(ExtensionManager.ExtensionWithData extension : extensions) {
            if (extension.listing.componentName.flattenToString()
                    .equals(id)
                && extension.latestData.visible()
                && shouldReappear(extension.listing.componentName.flattenToString())) {
                DashClockExtensionCard card =
                        new DashClockExtensionCard(mContext, extension,
                                                   mHostActivityContext);
                setCardSwipeAndUndoListeners(card);
                return card;
            }
        }

        return null;
    }

    @Override
    public void onExtensionsChanged(ComponentName sourceExtension) {
        if (sourceExtension != null) {
            for (CardProviderUpdateListener listener : mUpdateListeners) {
                listener.onCardProviderUpdate(sourceExtension.flattenToString(), false);
            }
        }
    }

    /**
     * Retrieves a list of all available extensions installed on the device
     * and sets mExtensionManager to track them for updates.
     */
    private void trackAllExtensions() {
        List<ComponentName> availableComponents = new ArrayList<ComponentName>();
        for(ExtensionManager.ExtensionListing listing : mExtensionManager.getAvailableExtensions()) {
           availableComponents.add(listing.componentName);
        }
        mExtensionManager.setActiveExtensions(availableComponents);
    }

    /**
     * Adds a listener for any extension updates.
     * @param listener The listener to update
     */
    @Override
    public void addOnUpdateListener(CardProviderUpdateListener listener) {
        mUpdateListeners.add(listener);
    }

    /**
     * Gets the time in the future when a card
     * should reappear, if it has been dismissed now.
     * @return The time in millis when the card should be allowed to reappear
     */
    private long getReappearTimeFromNow() {
        Calendar now = Calendar.getInstance();
        now.add(Calendar.MINUTE, CARD_REAPPEAR_TIME_IN_MINUTES);
        return now.getTimeInMillis();
    }

    /**
     * Gets the stored time at which the card should be allowed to reappear.
     * @param extensionKey The DashClock extension ComponentName String that will be the key
     * @return The time in millis at which the card can reappear OR zero if no time is stored.
     */
    private long getStoredReappearTime(String extensionKey) {
        SharedPreferences preferences =
                mHostActivityContext.getSharedPreferences(EXTENSION_TIMEOUT_SHARED_PREF_FILE,
                                                          Context.MODE_PRIVATE);
        return preferences.getLong(extensionKey, 0);
    }

    private void storeReappearTime(String extensionKey, long returnTime) {
        SharedPreferences preferences =
                mHostActivityContext.getSharedPreferences(EXTENSION_TIMEOUT_SHARED_PREF_FILE,
                                                          Context.MODE_PRIVATE);
        preferences.edit().putLong(extensionKey, returnTime).apply();
    }

    private void clearReappearTime(String extensionKey) {
        SharedPreferences preferences =
                mHostActivityContext.getSharedPreferences(EXTENSION_TIMEOUT_SHARED_PREF_FILE,
                                                          Context.MODE_PRIVATE);
        preferences.edit().remove(extensionKey).apply();
    }

    /**
     * Checks if the card representing the extensionKey parameter should be allowed to appear.
     * @param extensionKey The flattened ComponentName String representing an extension.
     * @return True if the current time is after the stored reappearance time OR
     *         if there is no stored time for this extension. False if the stored time
     *         is still in the future.
     */
    private boolean shouldReappear(String extensionKey) {
        Calendar now = Calendar.getInstance();
        long reappearTime = getStoredReappearTime(extensionKey);
        boolean shouldReappear = true;
        if (reappearTime != 0) {
            Calendar reappear = Calendar.getInstance();
            reappear.setTimeInMillis(reappearTime);
            shouldReappear = now.after(reappear);
        }
        return shouldReappear;
    }
}