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