summaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/trebuchet/LiveFoldersReceiver.java
blob: 056341346d785acf1c4b907ff2abbc71f5c11089 (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
package com.cyanogenmod.trebuchet;

import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.UserHandle;
import android.util.Log;

import org.cyanogenmod.support.ui.LiveFolder;
import static org.cyanogenmod.support.ui.LiveFolder.Constants.*;

public class LiveFoldersReceiver extends BroadcastReceiver {

    private static final String TAG = LiveFoldersReceiver.class.getName();

    static void alertFolderModified(Context ctx, LiveFolderInfo info, boolean deleted) {
        Intent i = new Intent(LIVE_FOLDER_UPDATES);
        i.putExtra(FOLDER_UPDATE_TYPE_EXTRA, deleted ? FOLDER_DELETED : NEW_FOLDER_CREATED);
        i.putExtra(FOLDER_ID_EXTRA, info.id);
        i.setComponent(info.receiver);
        ctx.sendBroadcastAsUser(i, UserHandle.CURRENT_OR_SELF);
    }

    static void alertItemOpened(Context ctx, LiveFolderInfo info, LiveFolderItemInfo item) {
        Intent i = new Intent(LIVE_FOLDER_UPDATES);
        i.putExtra(FOLDER_UPDATE_TYPE_EXTRA, FOLDER_ITEM_SELECTED);
        i.putExtra(FOLDER_ID_EXTRA, info.id);
        i.putExtra(FOLDER_ITEM_TITLE_EXTRA, item.title);
        i.putExtra(FOLDER_ITEM_ID_EXTRA, item.item_id);
        i.setComponent(info.receiver);
        ctx.sendBroadcastAsUser(i, UserHandle.CURRENT_OR_SELF);
    }

    static void alertItemRemoved(Context ctx, LiveFolderInfo info, LiveFolderItemInfo item) {
        Intent i = new Intent(LIVE_FOLDER_UPDATES);
        i.putExtra(FOLDER_UPDATE_TYPE_EXTRA, FOLDER_ITEM_REMOVED);
        i.putExtra(FOLDER_ID_EXTRA, info.id);
        i.putExtra(FOLDER_ITEM_TITLE_EXTRA, item.title);
        i.putExtra(FOLDER_ITEM_ID_EXTRA, item.item_id);
        i.setComponent(info.receiver);
        ctx.sendBroadcastAsUser(i, UserHandle.CURRENT_OR_SELF);
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        // Verify item list
        ArrayList<LiveFolder.Item> items = intent.getParcelableArrayListExtra(
                FOLDER_ENTRIES_EXTRA);
        if (items == null) {
            return;
        }

        if (intent.hasExtra(FOLDER_ID_EXTRA)) {

            long id = intent.getLongExtra(FOLDER_ID_EXTRA, 0);
            if (id != 0) {
                synchronized (LauncherModel.sBgLock) {
                    FolderInfo folder = LauncherModel.sBgFolders.get(id);

                    if (folder == null || !(folder instanceof LiveFolderInfo)) {
                        Log.e(TAG, "No live folder found with id " + id);
                        return;
                    }

                    LiveFolderInfo fInfo = (LiveFolderInfo) folder;

                    if (!fInfo.isOwner(context, getSendingPackage(intent))) {
                        Log.e(TAG, "Cannot modify a folder that belongs to another package");
                        return;
                    }

                    fInfo.populateWithItems(context, items);

                    // Update folder title provided
                    if (intent.hasExtra(LiveFolder.Constants.FOLDER_TITLE_EXTRA)) {
                        String title = intent.getStringExtra(
                                LiveFolder.Constants.FOLDER_TITLE_EXTRA);
                        fInfo.title = title;
                    }
                }
            }

        } else if (intent.getBooleanExtra(FOLDER_UPDATE_ALL, false)) {

            synchronized (LauncherModel.sBgLock) {
                for (FolderInfo info : LauncherModel.sBgFolders.values()) {
                    if (info instanceof LiveFolderInfo) {
                        LiveFolderInfo fInfo = (LiveFolderInfo) info;

                        if (fInfo.isOwner(context, getSendingPackage(intent))) {
                            fInfo.populateWithItems(context, items);
                        }
                    }
                }
            }

        } else {
            Log.d(TAG, "No folder id specified");
        }
    }
}