The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | package com.android.providers.subscribedfeeds; |
| 2 | |
| 3 | import android.content.Intent; |
| 4 | import android.content.Context; |
| 5 | import android.content.ContentResolver; |
| 6 | import android.content.ContentValues; |
| 7 | import android.content.SharedPreferences; |
| 8 | import android.util.Log; |
| 9 | import android.util.Config; |
| 10 | import android.util.EventLog; |
| 11 | import android.app.IntentService; |
| 12 | import android.provider.Sync; |
| 13 | import android.provider.SubscribedFeeds; |
| 14 | import android.provider.SyncConstValue; |
| 15 | import android.database.Cursor; |
| 16 | import android.database.sqlite.SQLiteFullException; |
| 17 | import android.app.AlarmManager; |
| 18 | import android.app.PendingIntent; |
| 19 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.text.TextUtils; |
| 21 | import android.net.Uri; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 22 | import android.accounts.Account; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Calendar; |
| 26 | |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 27 | import com.google.android.collect.Lists; |
| 28 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | /** |
| 30 | * A service to handle various intents asynchronously. |
| 31 | */ |
| 32 | public class SubscribedFeedsIntentService extends IntentService { |
| 33 | private static final String TAG = "Sync"; |
| 34 | |
| 35 | private static final String[] sAccountProjection = |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 36 | new String[] {SubscribedFeeds.Accounts._SYNC_ACCOUNT, |
| 37 | SubscribedFeeds.Accounts._SYNC_ACCOUNT_TYPE}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
| 39 | /** How often to refresh the subscriptions, in milliseconds */ |
| 40 | private static final long SUBSCRIPTION_REFRESH_INTERVAL = 1000L * 60 * 60 * 24; // one day |
| 41 | |
| 42 | private static final String sRefreshTime = "refreshTime"; |
| 43 | |
| 44 | private static final String sSubscribedFeedsPrefs = "subscribedFeeds"; |
| 45 | |
| 46 | private static final String GTALK_DATA_MESSAGE_RECEIVED = |
| 47 | "android.intent.action.GTALK_DATA_MESSAGE_RECEIVED"; |
| 48 | |
| 49 | private static final String SUBSCRIBED_FEEDS_REFRESH_ACTION = |
| 50 | "com.android.subscribedfeeds.action.REFRESH"; |
| 51 | |
| 52 | private static final int LOG_TICKLE = 2742; |
| 53 | |
| 54 | public SubscribedFeedsIntentService() { |
| 55 | super("SubscribedFeedsIntentService"); |
| 56 | } |
| 57 | |
| 58 | protected void onHandleIntent(Intent intent) { |
| 59 | if (GTALK_DATA_MESSAGE_RECEIVED.equals(intent.getAction())) { |
| 60 | boolean fromTrustedServer = intent.getBooleanExtra("from_trusted_server", false); |
| 61 | if (fromTrustedServer) { |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 62 | String accountName = intent.getStringExtra("account"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | String token = intent.getStringExtra("message_token"); |
| 64 | |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 65 | if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(token)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | if (Config.LOGD) { |
| 67 | Log.d(TAG, "Ignoring malformed tickle -- missing account or token."); |
| 68 | } |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | if (Config.LOGD) { |
| 73 | Log.d(TAG, "Received network tickle for " |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 74 | + accountName + " - " + token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 77 | handleTickle(this, accountName, token); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | } else { |
| 79 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 80 | Log.v(TAG, "Ignoring tickle -- not from trusted server."); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | } else if (Intent.ACTION_BOOT_COMPLETED.equals( |
| 85 | intent.getAction())) { |
| 86 | if (Config.LOGD) { |
| 87 | Log.d(TAG, "Received boot completed action"); |
| 88 | } |
| 89 | // load the time from the shared preferences and schedule an alarm |
| 90 | long refreshTime = getSharedPreferences( |
| 91 | sSubscribedFeedsPrefs, |
| 92 | Context.MODE_WORLD_READABLE).getLong(sRefreshTime, 0); |
| 93 | scheduleRefresh(this, refreshTime); |
| 94 | } else if (SUBSCRIBED_FEEDS_REFRESH_ACTION.equals(intent.getAction())) { |
| 95 | if (Config.LOGD) { |
| 96 | Log.d(TAG, "Received sSubscribedFeedsRefreshIntent"); |
| 97 | } |
| 98 | handleRefreshAlarm(this); |
| 99 | } |
| 100 | } |
| 101 | private void scheduleRefresh(Context context, long when) { |
| 102 | AlarmManager alarmManager = (AlarmManager) context.getSystemService( |
| 103 | Context.ALARM_SERVICE); |
| 104 | PendingIntent pendingIntent = PendingIntent.getBroadcast(context, |
| 105 | 0, new Intent(SUBSCRIBED_FEEDS_REFRESH_ACTION), 0); |
| 106 | alarmManager.set(AlarmManager.RTC, when, pendingIntent); |
| 107 | } |
| 108 | |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 109 | private void handleTickle(Context context, String accountName, String feed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | Cursor c = null; |
| 111 | Sync.Settings.QueryMap syncSettings = |
| 112 | new Sync.Settings.QueryMap(context.getContentResolver(), |
| 113 | false /* don't keep updated */, |
| 114 | null /* not needed since keep updated is false */); |
| 115 | final String where = SubscribedFeeds.Feeds._SYNC_ACCOUNT + "= ? " |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 116 | + "and " + SubscribedFeeds.Feeds._SYNC_ACCOUNT_TYPE + "= ? " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | + "and " + SubscribedFeeds.Feeds.FEED + "= ?"; |
| 118 | try { |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 119 | // TODO(fredq) fix the hardcoded type |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | c = context.getContentResolver().query(SubscribedFeeds.Feeds.CONTENT_URI, |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 121 | null, where, new String[]{accountName, "com.google.GAIA", feed}, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | if (c.getCount() == 0) { |
| 123 | Log.w(TAG, "received tickle for non-existent feed: " |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 124 | + "account " + accountName + ", feed " + feed); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | EventLog.writeEvent(LOG_TICKLE, "unknown"); |
| 126 | } |
| 127 | while (c.moveToNext()) { |
| 128 | // initiate a sync |
| 129 | String authority = c.getString(c.getColumnIndexOrThrow( |
| 130 | SubscribedFeeds.Feeds.AUTHORITY)); |
| 131 | EventLog.writeEvent(LOG_TICKLE, authority); |
| 132 | if (!syncSettings.getSyncProviderAutomatically(authority)) { |
| 133 | Log.d(TAG, "supressing tickle since provider " + authority |
| 134 | + " is configured to not sync automatically"); |
| 135 | continue; |
| 136 | } |
| 137 | Uri uri = Uri.parse("content://" + authority); |
| 138 | Bundle extras = new Bundle(); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 139 | extras.putParcelable(ContentResolver.SYNC_EXTRAS_ACCOUNT, |
| 140 | new Account(accountName, "com.google.GAIA")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | extras.putString("feed", feed); |
| 142 | context.getContentResolver().startSync(uri, extras); |
| 143 | } |
| 144 | } finally { |
| 145 | if (c != null) c.deactivate(); |
| 146 | syncSettings.close(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Cause all the subscribed feeds to be marked dirty and their |
| 152 | * authtokens to be refreshed, which will result in new authtokens |
| 153 | * being sent to the subscription server. Then reschedules this |
| 154 | * event for one week in the future. |
| 155 | * |
| 156 | * @param context Context we are running within |
| 157 | */ |
| 158 | private void handleRefreshAlarm(Context context) { |
| 159 | // retrieve the list of accounts from the subscribed feeds |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 160 | ArrayList<Account> accounts = Lists.newArrayList(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | ContentResolver contentResolver = context.getContentResolver(); |
| 162 | Cursor c = contentResolver.query(SubscribedFeeds.Accounts.CONTENT_URI, |
| 163 | sAccountProjection, null, null, null); |
Fred Quintana | 22f7114 | 2009-03-24 20:10:17 -0700 | [diff] [blame] | 164 | try { |
| 165 | while (c.moveToNext()) { |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 166 | String accountName = c.getString(0); |
| 167 | String accountType = c.getString(1); |
| 168 | accounts.add(new Account(accountName, accountType)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | } |
Fred Quintana | 22f7114 | 2009-03-24 20:10:17 -0700 | [diff] [blame] | 170 | } finally { |
| 171 | c.close(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | |
| 174 | // Clear the auth tokens for all these accounts so that we are sure |
| 175 | // they will still be valid until the next time we refresh them. |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 176 | // TODO(fredq): add this when the google login service is done |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | |
| 178 | // mark the feeds dirty, by setting the accounts to the same value, |
| 179 | // which will trigger a sync. |
| 180 | try { |
| 181 | ContentValues values = new ContentValues(); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 182 | for (Account account : accounts) { |
| 183 | values.put(SyncConstValue._SYNC_ACCOUNT, account.mName); |
| 184 | values.put(SyncConstValue._SYNC_ACCOUNT_TYPE, account.mType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | contentResolver.update(SubscribedFeeds.Feeds.CONTENT_URI, values, |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 186 | SubscribedFeeds.Feeds._SYNC_ACCOUNT + "=? AND " |
| 187 | + SubscribedFeeds.Feeds._SYNC_ACCOUNT_TYPE + "=?", |
| 188 | new String[] {account.mName, account.mType}); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | } |
| 190 | } catch (SQLiteFullException e) { |
| 191 | Log.w(TAG, "disk full while trying to mark the feeds as dirty, skipping"); |
| 192 | } |
| 193 | |
| 194 | // Schedule a refresh. |
| 195 | long refreshTime = Calendar.getInstance().getTimeInMillis() + SUBSCRIPTION_REFRESH_INTERVAL; |
| 196 | scheduleRefresh(context, refreshTime); |
| 197 | SharedPreferences.Editor editor = context.getSharedPreferences(sSubscribedFeedsPrefs, |
| 198 | Context.MODE_WORLD_READABLE).edit(); |
| 199 | editor.putLong(sRefreshTime, refreshTime); |
| 200 | editor.commit(); |
| 201 | } |
| 202 | } |