summaryrefslogtreecommitdiffstats
path: root/src/com/android/email/provider/WidgetProvider.java
blob: 6909e8902cbd7bf7f9680a5344bc9e52d0f66d5f (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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.email.provider;

import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;

import com.android.emailcommon.provider.Account;
import com.android.emailcommon.provider.Mailbox;
import com.android.mail.providers.Folder;
import com.android.mail.providers.UIProvider;
import com.android.mail.utils.LogTag;
import com.android.mail.utils.LogUtils;
import com.android.mail.widget.BaseWidgetProvider;
import com.android.mail.widget.WidgetService;

public class WidgetProvider extends BaseWidgetProvider {
    private static final String LEGACY_PREFS_NAME = "com.android.email.widget.WidgetManager";
    private static final String LEGACY_ACCOUNT_ID_PREFIX = "accountId_";
    private static final String LEGACY_MAILBOX_ID_PREFIX = "mailboxId_";

    private static final String LOG_TAG = LogTag.getLogTag();

    /**
     * Remove preferences when deleting widget
     */
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);

        // Remove any legacy Email widget information
        final SharedPreferences prefs = context.getSharedPreferences(LEGACY_PREFS_NAME, 0);
        final SharedPreferences.Editor editor = prefs.edit();
        for (int widgetId : appWidgetIds) {
            // Remove the account in the preference
            editor.remove(LEGACY_ACCOUNT_ID_PREFIX + widgetId);
            editor.remove(LEGACY_MAILBOX_ID_PREFIX + widgetId);
        }
        editor.apply();
    }

    @Override
    protected com.android.mail.providers.Account getAccountObject(
            Context context, String accountUri) {
        final ContentResolver resolver = context.getContentResolver();
        final Cursor accountCursor = resolver.query(Uri.parse(accountUri),
                UIProvider.ACCOUNTS_PROJECTION_NO_CAPABILITIES, null, null, null);

        return getPopulatedAccountObject(accountCursor);
    }


    @Override
    protected boolean isAccountValid(Context context, com.android.mail.providers.Account account) {
        if (account != null) {
            final ContentResolver resolver = context.getContentResolver();
            final Cursor accountCursor = resolver.query(account.uri,
                    UIProvider.ACCOUNTS_PROJECTION_NO_CAPABILITIES, null, null, null);
            if (accountCursor != null) {
                try {
                    return accountCursor.getCount() > 0;
                } finally {
                    accountCursor.close();
                }
            }
        }
        return false;
    }

    @Override
    protected void migrateLegacyWidgetInformation(Context context, int widgetId) {
        final SharedPreferences prefs = context.getSharedPreferences(LEGACY_PREFS_NAME, 0);
        final SharedPreferences.Editor editor = prefs.edit();

        long accountId = loadAccountIdPref(context, widgetId);
        long mailboxId = loadMailboxIdPref(context, widgetId);
        // Legacy support; if preferences haven't been saved for this widget, load something
        if (accountId == Account.NO_ACCOUNT || mailboxId == Mailbox.NO_MAILBOX) {
            LogUtils.d(LOG_TAG, "Couldn't load account or mailbox.  accountId: %d" +
                    " mailboxId: %d widgetId %d", accountId, mailboxId, widgetId);
            return;
        }

        accountId = migrateLegacyWidgetAccountId(accountId);
        mailboxId = migrateLegacyWidgetMailboxId(mailboxId, accountId);

        // Get Account and folder objects for the account id and mailbox id
        final com.android.mail.providers.Account uiAccount = getAccount(context, accountId);
        final Folder uiFolder = EmailProvider.getFolder(context, mailboxId);

        if (uiAccount != null && uiFolder != null) {
            WidgetService.saveWidgetInformation(context, widgetId, uiAccount,
                    uiFolder.folderUri.fullUri.toString());

            updateWidgetInternal(context, widgetId, uiAccount, uiFolder.type, uiFolder.capabilities,
                    uiFolder.folderUri.fullUri, uiFolder.conversationListUri, uiFolder.name);

            // Now remove the old legacy preference value
            editor.remove(LEGACY_ACCOUNT_ID_PREFIX + widgetId);
            editor.remove(LEGACY_MAILBOX_ID_PREFIX + widgetId);
        }
        editor.apply();
    }

    private static long migrateLegacyWidgetAccountId(long accountId) {
        if (accountId == Account.ACCOUNT_ID_COMBINED_VIEW) {
            return EmailProvider.COMBINED_ACCOUNT_ID;
        }
        return accountId;
    }

    /**
     * @param accountId The migrated accountId
     * @return
     */
    private static long migrateLegacyWidgetMailboxId(long mailboxId, long accountId) {
        if (mailboxId == Mailbox.QUERY_ALL_INBOXES) {
            return EmailProvider.getVirtualMailboxId(accountId, Mailbox.TYPE_INBOX);
        } else if (mailboxId == Mailbox.QUERY_ALL_UNREAD) {
            return EmailProvider.getVirtualMailboxId(accountId, Mailbox.TYPE_UNREAD);
        }
        return mailboxId;
    }

    private static com.android.mail.providers.Account getAccount(Context context, long accountId) {
        final ContentResolver resolver = context.getContentResolver();
        final Cursor ac = resolver.query(EmailProvider.uiUri("uiaccount", accountId),
                UIProvider.ACCOUNTS_PROJECTION_NO_CAPABILITIES, null, null, null);

        com.android.mail.providers.Account uiAccount = getPopulatedAccountObject(ac);

        return uiAccount;
    }

    private static com.android.mail.providers.Account getPopulatedAccountObject(
            final Cursor accountCursor) {
        if (accountCursor == null) {
            LogUtils.e(LOG_TAG, "Null account cursor");
            return null;
        }

        com.android.mail.providers.Account uiAccount = null;
        try {
            if (accountCursor.moveToFirst()) {
                 uiAccount = com.android.mail.providers.Account.builder().buildFrom(accountCursor);
            }
        } finally {
            accountCursor.close();
        }
        return uiAccount;
    }

    /**
     * Returns the saved account ID for the given widget. Otherwise,
     * {@link com.android.emailcommon.provider.Account#NO_ACCOUNT} if
     * the account ID was not previously saved.
     */
    static long loadAccountIdPref(Context context, int appWidgetId) {
        final SharedPreferences prefs = context.getSharedPreferences(LEGACY_PREFS_NAME, 0);
        long accountId = prefs.getLong(LEGACY_ACCOUNT_ID_PREFIX + appWidgetId, Account.NO_ACCOUNT);
        return accountId;
    }

    /**
     * Returns the saved mailbox ID for the given widget. Otherwise,
     * {@link com.android.emailcommon.provider.Mailbox#NO_MAILBOX} if
     * the mailbox ID was not previously saved.
     */
    static long loadMailboxIdPref(Context context, int appWidgetId) {
        final SharedPreferences prefs = context.getSharedPreferences(LEGACY_PREFS_NAME, 0);
        long mailboxId = prefs.getLong(LEGACY_MAILBOX_ID_PREFIX + appWidgetId, Mailbox.NO_MAILBOX);
        return mailboxId;
    }
}