WidgetManager.java revision f5418f1f93b02e7fab9f15eb201800b65510998e
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.email.widget;
18
19import com.android.email.Email;
20import com.android.emailcommon.Logging;
21import com.android.emailcommon.provider.Account;
22import com.android.emailcommon.provider.Mailbox;
23
24import android.content.Context;
25import android.content.SharedPreferences;
26import android.util.Log;
27
28import java.io.FileDescriptor;
29import java.io.PrintWriter;
30import java.util.Map;
31import java.util.concurrent.ConcurrentHashMap;
32
33/**
34 * Class that maintains references to all widgets.
35 */
36public class WidgetManager {
37    private static final String PREFS_NAME = "com.android.email.widget.WidgetManager";
38    private static final String ACCOUNT_ID_PREFIX = "accountId_";
39    private static final String MAILBOX_ID_PREFIX = "mailboxId_";
40
41    private final static WidgetManager sInstance = new WidgetManager();
42
43    // Widget ID -> Widget
44    private final static Map<Integer, EmailWidget> mWidgets =
45            new ConcurrentHashMap<Integer, EmailWidget>();
46
47    private WidgetManager() {
48    }
49
50    public static WidgetManager getInstance() {
51        return sInstance;
52    }
53
54    public synchronized void createWidgets(Context context, int[] widgetIds) {
55        for (int widgetId : widgetIds) {
56            getOrCreateWidget(context, widgetId);
57        }
58    }
59
60    public synchronized void deleteWidgets(Context context, int[] widgetIds) {
61        for (int widgetId : widgetIds) {
62            // Find the widget in the map
63            final EmailWidget widget = WidgetManager.getInstance().get(widgetId);
64            if (widget != null) {
65                // Stop loading and remove the widget from the map
66                widget.onDeleted();
67            }
68            remove(context, widgetId);
69        }
70    }
71
72    public synchronized EmailWidget getOrCreateWidget(Context context, int widgetId) {
73        EmailWidget widget = WidgetManager.getInstance().get(widgetId);
74        if (widget == null) {
75            if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
76                Log.d(EmailWidget.TAG, "Create email widget; ID: " + widgetId);
77            }
78            widget = new EmailWidget(context, widgetId);
79            WidgetManager.getInstance().put(widgetId, widget);
80            widget.start();
81        }
82        return widget;
83    }
84
85    private EmailWidget get(int widgetId) {
86        return mWidgets.get(widgetId);
87    }
88
89    private void put(int widgetId, EmailWidget widget) {
90        mWidgets.put(widgetId, widget);
91    }
92
93    private void remove(Context context, int widgetId) {
94        mWidgets.remove(widgetId);
95        WidgetManager.removeWidgetPrefs(context, widgetId);
96    }
97
98    public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
99        int n = 0;
100        for (EmailWidget widget : mWidgets.values()) {
101            writer.println("Widget #" + (++n));
102            writer.println("    " + widget.toString());
103        }
104    }
105
106    /** Saves shared preferences for the given widget */
107    static void saveWidgetPrefs(Context context, int appWidgetId, long accountId, long mailboxId) {
108        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
109        prefs.edit()
110            .putLong(ACCOUNT_ID_PREFIX + appWidgetId, accountId)
111            .putLong(MAILBOX_ID_PREFIX + appWidgetId, mailboxId)
112            .commit();    // preferences must be committed before we return
113    }
114
115    /** Removes shared preferences for the given widget */
116    static void removeWidgetPrefs(Context context, int appWidgetId) {
117        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
118        SharedPreferences.Editor editor = prefs.edit();
119        for (String key : prefs.getAll().keySet()) {
120            if (key.endsWith("_" + appWidgetId)) {
121                editor.remove(key);
122            }
123        }
124        editor.apply();   // just want to clean up; don't care when preferences are actually removed
125    }
126
127    /**
128     * Returns the saved account ID for the given widget. Otherwise, {@link Account#NO_ACCOUNT} if
129     * the account ID was not previously saved.
130     */
131    static long loadAccountIdPref(Context context, int appWidgetId) {
132        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
133        long accountId = prefs.getLong(ACCOUNT_ID_PREFIX + appWidgetId, Account.NO_ACCOUNT);
134        return accountId;
135    }
136
137    /**
138     * Returns the saved mailbox ID for the given widget. Otherwise, {@link Mailbox#NO_MAILBOX} if
139     * the mailbox ID was not previously saved.
140     */
141    static long loadMailboxIdPref(Context context, int appWidgetId) {
142        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
143        long mailboxId = prefs.getLong(MAILBOX_ID_PREFIX + appWidgetId, Mailbox.NO_MAILBOX);
144        return mailboxId;
145    }
146}
147