Home | History | Annotate | Download | only in service
      1 /*
      2  * Copyright (C) 2010 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.service;
     18 
     19 import android.accounts.Account;
     20 import android.accounts.OperationCanceledException;
     21 import android.app.Service;
     22 import android.content.AbstractThreadedSyncAdapter;
     23 import android.content.ContentProviderClient;
     24 import android.content.ContentResolver;
     25 import android.content.Context;
     26 import android.content.Intent;
     27 import android.content.SyncResult;
     28 import android.database.Cursor;
     29 import android.os.Bundle;
     30 import android.os.IBinder;
     31 import android.util.Log;
     32 
     33 import com.android.email.Controller;
     34 import com.android.emailcommon.provider.EmailContent;
     35 import com.android.emailcommon.provider.EmailContent.AccountColumns;
     36 import com.android.emailcommon.provider.Mailbox;
     37 
     38 public class PopImapSyncAdapterService extends Service {
     39     private static final String TAG = "PopImapSyncAdapterService";
     40     private static SyncAdapterImpl sSyncAdapter = null;
     41     private static final Object sSyncAdapterLock = new Object();
     42 
     43     public PopImapSyncAdapterService() {
     44         super();
     45     }
     46 
     47     private static class SyncAdapterImpl extends AbstractThreadedSyncAdapter {
     48         private Context mContext;
     49 
     50         public SyncAdapterImpl(Context context) {
     51             super(context, true /* autoInitialize */);
     52             mContext = context;
     53         }
     54 
     55         @Override
     56         public void onPerformSync(Account account, Bundle extras,
     57                 String authority, ContentProviderClient provider, SyncResult syncResult) {
     58             try {
     59                 PopImapSyncAdapterService.performSync(mContext, account, extras,
     60                         authority, provider, syncResult);
     61             } catch (OperationCanceledException e) {
     62             }
     63         }
     64     }
     65 
     66     @Override
     67     public void onCreate() {
     68         super.onCreate();
     69         synchronized (sSyncAdapterLock) {
     70             if (sSyncAdapter == null) {
     71                 sSyncAdapter = new SyncAdapterImpl(getApplicationContext());
     72             }
     73         }
     74     }
     75 
     76     @Override
     77     public IBinder onBind(Intent intent) {
     78         return sSyncAdapter.getSyncAdapterBinder();
     79     }
     80 
     81     /**
     82      * Partial integration with system SyncManager; we initiate manual syncs upon request
     83      */
     84     private static void performSync(Context context, Account account, Bundle extras,
     85             String authority, ContentProviderClient provider, SyncResult syncResult)
     86             throws OperationCanceledException {
     87         if (extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false)) {
     88             String emailAddress = account.name;
     89             // Find an EmailProvider account with the Account's email address
     90             Cursor c = context.getContentResolver().query(
     91                     com.android.emailcommon.provider.Account.CONTENT_URI,
     92                     EmailContent.ID_PROJECTION, AccountColumns.EMAIL_ADDRESS + "=?",
     93                     new String[] {emailAddress}, null);
     94             if (c.moveToNext()) {
     95                 // If we have one, find the inbox and start it syncing
     96                 long accountId = c.getLong(EmailContent.ID_PROJECTION_COLUMN);
     97                 long mailboxId = Mailbox.findMailboxOfType(context, accountId,
     98                         Mailbox.TYPE_INBOX);
     99                 if (mailboxId > 0) {
    100                     Log.d(TAG, "Starting manual sync for account " + emailAddress);
    101                     Controller.getInstance(context).updateMailbox(accountId, mailboxId, false);
    102                 }
    103             }
    104         }
    105     }
    106 }