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 17 package com.android.email.widget; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 import android.util.Log; 22 23 import com.android.email.Email; 24 import com.android.emailcommon.Logging; 25 import com.android.emailcommon.provider.Account; 26 import com.android.emailcommon.provider.Mailbox; 27 28 import java.io.FileDescriptor; 29 import java.io.PrintWriter; 30 import java.util.Map; 31 import java.util.concurrent.ConcurrentHashMap; 32 33 /** 34 * Class that maintains references to all widgets. 35 */ 36 public 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 void updateWidgets(Context context, int[] widgetIds) { 73 for (int widgetId : widgetIds) { 74 // Find the widget in the map 75 final EmailWidget widget = WidgetManager.getInstance().get(widgetId); 76 if (widget != null) { 77 widget.reset(); 78 } else { 79 getOrCreateWidget(context, widgetId); 80 } 81 } 82 } 83 84 public synchronized EmailWidget getOrCreateWidget(Context context, int widgetId) { 85 EmailWidget widget = WidgetManager.getInstance().get(widgetId); 86 if (widget == null) { 87 if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) { 88 Log.d(EmailWidget.TAG, "Create email widget; ID: " + widgetId); 89 } 90 widget = new EmailWidget(context, widgetId); 91 put(widgetId, widget); 92 widget.start(); 93 } 94 return widget; 95 } 96 97 private EmailWidget get(int widgetId) { 98 return mWidgets.get(widgetId); 99 } 100 101 private void put(int widgetId, EmailWidget widget) { 102 mWidgets.put(widgetId, widget); 103 } 104 105 private void remove(Context context, int widgetId) { 106 mWidgets.remove(widgetId); 107 WidgetManager.removeWidgetPrefs(context, widgetId); 108 } 109 110 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { 111 int n = 0; 112 for (EmailWidget widget : mWidgets.values()) { 113 writer.println("Widget #" + (++n)); 114 writer.println(" " + widget.toString()); 115 } 116 } 117 118 /** Saves shared preferences for the given widget */ 119 static void saveWidgetPrefs(Context context, int appWidgetId, long accountId, long mailboxId) { 120 SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0); 121 prefs.edit() 122 .putLong(ACCOUNT_ID_PREFIX + appWidgetId, accountId) 123 .putLong(MAILBOX_ID_PREFIX + appWidgetId, mailboxId) 124 .commit(); // preferences must be committed before we return 125 } 126 127 /** Removes shared preferences for the given widget */ 128 static void removeWidgetPrefs(Context context, int appWidgetId) { 129 SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0); 130 SharedPreferences.Editor editor = prefs.edit(); 131 for (String key : prefs.getAll().keySet()) { 132 if (key.endsWith("_" + appWidgetId)) { 133 editor.remove(key); 134 } 135 } 136 editor.apply(); // just want to clean up; don't care when preferences are actually removed 137 } 138 139 /** 140 * Returns the saved account ID for the given widget. Otherwise, {@link Account#NO_ACCOUNT} if 141 * the account ID was not previously saved. 142 */ 143 static long loadAccountIdPref(Context context, int appWidgetId) { 144 SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0); 145 long accountId = prefs.getLong(ACCOUNT_ID_PREFIX + appWidgetId, Account.NO_ACCOUNT); 146 return accountId; 147 } 148 149 /** 150 * Returns the saved mailbox ID for the given widget. Otherwise, {@link Mailbox#NO_MAILBOX} if 151 * the mailbox ID was not previously saved. 152 */ 153 static long loadMailboxIdPref(Context context, int appWidgetId) { 154 SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0); 155 long mailboxId = prefs.getLong(MAILBOX_ID_PREFIX + appWidgetId, Mailbox.NO_MAILBOX); 156 return mailboxId; 157 } 158 } 159