package de.cketti.dashclock.k9;

import java.util.List;

import com.google.android.apps.dashclock.api.DashClockExtension;
import com.google.android.apps.dashclock.api.ExtensionData;

import de.cketti.dashclock.k9.K9Helper.Account;


public class K9Extension extends DashClockExtension {

    @Override
    protected void onUpdateData(int reason) {

        int unreadCount = 0;
        StringBuilder body = new StringBuilder();

        List<Account> accounts = K9Helper.getAccounts(this);
        if (accounts != null) {
            for (Account account : accounts) {
                int accountUnread = K9Helper.getUnreadCount(this, account);
                unreadCount += accountUnread;

                if (accountUnread > 0) {
                    if (body.length() > 0) {
                        body.append('\n');
                    }

                    body.append(account.name).append(" (").append(accountUnread).append(')');
                }
            }
        }

        ExtensionData data = new ExtensionData()
                .visible(unreadCount > 0)
                .icon(R.drawable.ic_envelope)
                .status(Integer.toString(unreadCount))
                .expandedTitle(getString(R.string.unread_title, unreadCount))
                .expandedBody(body.toString());

        publishUpdate(data);
    }

}

@@ -50,16 +50,159 @@
 .List;%0A%0A
+import android.content.BroadcastReceiver;%0Aimport android.content.Context;%0Aimport android.content.Intent;%0Aimport android.content.IntentFilter;%0A%0A
 import c
@@ -420,24 +420,765 @@
 xtension %7B%0A%0A
+    private BroadcastReceiver mReceiver = new BroadcastReceiver() %7B%0A%0A        @Override%0A        public void onReceive(Context context, Intent intent) %7B%0A            onUpdateData(UPDATE_REASON_CONTENT_CHANGED);%0A        %7D%0A    %7D;%0A%0A    @Override%0A    protected void onInitialize(boolean isReconnect) %7B%0A        IntentFilter filter = new IntentFilter();%0A        filter.addAction(K9Helper.BroadcastIntents.ACTION_EMAIL_RECEIVED);%0A        filter.addAction(K9Helper.BroadcastIntents.ACTION_EMAIL_DELETED);%0A        filter.addAction(K9Helper.BroadcastIntents.ACTION_REFRESH_OBSERVER);%0A%0A        registerReceiver(mReceiver, filter);%0A    %7D%0A%0A    @Override%0A    public void onDestroy() %7B%0A        unregisterReceiver(mReceiver);%0A        super.onDestroy();%0A    %7D%0A%0A
     @Overrid

Add broadcast receiver to be notified of changes in K-9 Mail