Home | History | Annotate | Download | only in service
      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.service;
     18 
     19 import android.accounts.AccountManager;
     20 import android.app.Service;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.database.Cursor;
     24 import android.os.Bundle;
     25 import android.os.IBinder;
     26 
     27 import com.android.email.Email;
     28 import com.android.email.NotificationController;
     29 import com.android.email.ResourceHelper;
     30 import com.android.email.VendorPolicyLoader;
     31 import com.android.email.provider.AccountReconciler;
     32 import com.android.emailcommon.Configuration;
     33 import com.android.emailcommon.Device;
     34 import com.android.emailcommon.provider.Account;
     35 import com.android.emailcommon.service.IAccountService;
     36 import com.android.emailcommon.utility.EmailAsyncTask;
     37 
     38 import java.io.IOException;
     39 import java.util.ArrayList;
     40 
     41 public class AccountService extends Service {
     42 
     43     // Save context
     44     private Context mContext;
     45 
     46     private final IAccountService.Stub mBinder = new IAccountService.Stub() {
     47 
     48         @Override
     49         public void notifyLoginFailed(long accountId) {
     50             NotificationController.getInstance(mContext).showLoginFailedNotification(accountId);
     51         }
     52 
     53         @Override
     54         public void notifyLoginSucceeded(long accountId) {
     55             NotificationController.getInstance(mContext).cancelLoginFailedNotification(accountId);
     56         }
     57 
     58         private ArrayList<Account> getAccountList(String forProtocol) {
     59             ArrayList<Account> providerAccounts = new ArrayList<Account>();
     60             Cursor c = mContext.getContentResolver().query(Account.CONTENT_URI,
     61                     Account.ID_PROJECTION, null, null, null);
     62             try {
     63                 while (c.moveToNext()) {
     64                     long accountId = c.getLong(Account.CONTENT_ID_COLUMN);
     65                     String protocol = Account.getProtocol(mContext, accountId);
     66                     if ((protocol != null) && forProtocol.equals(protocol)) {
     67                         Account account = Account.restoreAccountWithId(mContext, accountId);
     68                         if (account != null) {
     69                             providerAccounts.add(account);
     70                         }
     71                     }
     72                 }
     73             } finally {
     74                 c.close();
     75             }
     76             return providerAccounts;
     77         }
     78 
     79         @Override
     80         public void reconcileAccounts(String protocol, String accountManagerType) {
     81             ArrayList<Account> providerList = getAccountList(protocol);
     82             android.accounts.Account[] accountMgrList =
     83                 AccountManager.get(mContext).getAccountsByType(accountManagerType);
     84             AccountReconciler.reconcileAccounts(mContext, providerList, accountMgrList, mContext);
     85         }
     86 
     87         @Override
     88         public int getAccountColor(long accountId) {
     89             return ResourceHelper.getInstance(mContext).getAccountColor(accountId);
     90         }
     91 
     92         @Override
     93         public Bundle getConfigurationData(String accountType) {
     94             Bundle bundle = new Bundle();
     95             bundle.putBoolean(Configuration.EXCHANGE_CONFIGURATION_USE_ALTERNATE_STRINGS,
     96                     VendorPolicyLoader.getInstance(mContext).useAlternateExchangeStrings());
     97             return bundle;
     98         }
     99 
    100         @Override
    101         public String getDeviceId() {
    102             try {
    103                 EmailAsyncTask.runAsyncSerial(new Runnable() {
    104                     @Override
    105                     public void run() {
    106                         // Make sure the service is properly running (re: lifecycle)
    107                         EmailServiceUtils.startExchangeService(mContext);
    108                         // Send current logging flags
    109                         Email.updateLoggingFlags(mContext);
    110                     }});
    111                 return Device.getDeviceId(mContext);
    112             } catch (IOException e) {
    113                 return null;
    114             }
    115         }
    116     };
    117 
    118     @Override
    119     public IBinder onBind(Intent intent) {
    120         if (mContext == null) {
    121             mContext = this;
    122         }
    123         // Make sure we have a valid deviceId (just retrieves a static String except first time)
    124         try {
    125             Device.getDeviceId(this);
    126         } catch (IOException e) {
    127         }
    128         return mBinder;
    129     }
    130 }