18a574694606f0e5d781334d0d426fc379c51f3edMarc Blank/* 28a574694606f0e5d781334d0d426fc379c51f3edMarc Blank * Copyright (C) 2011 The Android Open Source Project 38a574694606f0e5d781334d0d426fc379c51f3edMarc Blank * 48a574694606f0e5d781334d0d426fc379c51f3edMarc Blank * Licensed under the Apache License, Version 2.0 (the "License"); 58a574694606f0e5d781334d0d426fc379c51f3edMarc Blank * you may not use this file except in compliance with the License. 68a574694606f0e5d781334d0d426fc379c51f3edMarc Blank * You may obtain a copy of the License at 78a574694606f0e5d781334d0d426fc379c51f3edMarc Blank * 88a574694606f0e5d781334d0d426fc379c51f3edMarc Blank * http://www.apache.org/licenses/LICENSE-2.0 98a574694606f0e5d781334d0d426fc379c51f3edMarc Blank * 108a574694606f0e5d781334d0d426fc379c51f3edMarc Blank * Unless required by applicable law or agreed to in writing, software 118a574694606f0e5d781334d0d426fc379c51f3edMarc Blank * distributed under the License is distributed on an "AS IS" BASIS, 128a574694606f0e5d781334d0d426fc379c51f3edMarc Blank * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138a574694606f0e5d781334d0d426fc379c51f3edMarc Blank * See the License for the specific language governing permissions and 148a574694606f0e5d781334d0d426fc379c51f3edMarc Blank * limitations under the License. 158a574694606f0e5d781334d0d426fc379c51f3edMarc Blank */ 168a574694606f0e5d781334d0d426fc379c51f3edMarc Blank 178a574694606f0e5d781334d0d426fc379c51f3edMarc Blankpackage com.android.email.service; 188a574694606f0e5d781334d0d426fc379c51f3edMarc Blank 194e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blankimport android.accounts.AccountManager; 204e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blankimport android.app.Service; 214e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blankimport android.content.Context; 224e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blankimport android.content.Intent; 234e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blankimport android.database.Cursor; 244e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blankimport android.os.Bundle; 254e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blankimport android.os.IBinder; 264e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank 27bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport com.android.email.Email; 288a574694606f0e5d781334d0d426fc379c51f3edMarc Blankimport com.android.email.NotificationController; 298a574694606f0e5d781334d0d426fc379c51f3edMarc Blankimport com.android.email.ResourceHelper; 30bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrookimport com.android.email.VendorPolicyLoader; 314e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blankimport com.android.email.provider.AccountReconciler; 3237500777f1ca2dc7a1f831b9f3af63cbe7b9d487Marc Blankimport com.android.emailcommon.Configuration; 33dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blankimport com.android.emailcommon.Device; 344e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blankimport com.android.emailcommon.provider.Account; 358a574694606f0e5d781334d0d426fc379c51f3edMarc Blankimport com.android.emailcommon.service.IAccountService; 36c4cdb11d24c19428dd39f986b00c1a29e75e1505Todd Kennedyimport com.android.emailcommon.utility.EmailAsyncTask; 378a574694606f0e5d781334d0d426fc379c51f3edMarc Blank 38dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blankimport java.io.IOException; 394e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blankimport java.util.ArrayList; 40dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blank 418a574694606f0e5d781334d0d426fc379c51f3edMarc Blankpublic class AccountService extends Service { 428a574694606f0e5d781334d0d426fc379c51f3edMarc Blank 43dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blank // Save context 448a574694606f0e5d781334d0d426fc379c51f3edMarc Blank private Context mContext; 458a574694606f0e5d781334d0d426fc379c51f3edMarc Blank 468a574694606f0e5d781334d0d426fc379c51f3edMarc Blank private final IAccountService.Stub mBinder = new IAccountService.Stub() { 478a574694606f0e5d781334d0d426fc379c51f3edMarc Blank 488a574694606f0e5d781334d0d426fc379c51f3edMarc Blank @Override 49bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook public void notifyLoginFailed(long accountId) { 50bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook NotificationController.getInstance(mContext).showLoginFailedNotification(accountId); 518a574694606f0e5d781334d0d426fc379c51f3edMarc Blank } 528a574694606f0e5d781334d0d426fc379c51f3edMarc Blank 538a574694606f0e5d781334d0d426fc379c51f3edMarc Blank @Override 54c4cdb11d24c19428dd39f986b00c1a29e75e1505Todd Kennedy public void notifyLoginSucceeded(long accountId) { 558a574694606f0e5d781334d0d426fc379c51f3edMarc Blank NotificationController.getInstance(mContext).cancelLoginFailedNotification(accountId); 568a574694606f0e5d781334d0d426fc379c51f3edMarc Blank } 578a574694606f0e5d781334d0d426fc379c51f3edMarc Blank 584e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank private ArrayList<Account> getAccountList(String forProtocol) { 594e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank ArrayList<Account> providerAccounts = new ArrayList<Account>(); 604e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank Cursor c = mContext.getContentResolver().query(Account.CONTENT_URI, 614e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank Account.ID_PROJECTION, null, null, null); 624e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank try { 634e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank while (c.moveToNext()) { 644e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank long accountId = c.getLong(Account.CONTENT_ID_COLUMN); 654e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank String protocol = Account.getProtocol(mContext, accountId); 664e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank if ((protocol != null) && forProtocol.equals(protocol)) { 674e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank Account account = Account.restoreAccountWithId(mContext, accountId); 684e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank if (account != null) { 694e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank providerAccounts.add(account); 704e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank } 714e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank } 724e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank } 734e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank } finally { 744e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank c.close(); 754e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank } 764e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank return providerAccounts; 778a574694606f0e5d781334d0d426fc379c51f3edMarc Blank } 788a574694606f0e5d781334d0d426fc379c51f3edMarc Blank 798a574694606f0e5d781334d0d426fc379c51f3edMarc Blank @Override 804e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank public void reconcileAccounts(String protocol, String accountManagerType) { 814e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank ArrayList<Account> providerList = getAccountList(protocol); 824e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank android.accounts.Account[] accountMgrList = 834e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank AccountManager.get(mContext).getAccountsByType(accountManagerType); 844e4aba9ebc43c6a83190f3a883fa05bb7d5100b3Marc Blank AccountReconciler.reconcileAccounts(mContext, providerList, accountMgrList, mContext); 858a574694606f0e5d781334d0d426fc379c51f3edMarc Blank } 868a574694606f0e5d781334d0d426fc379c51f3edMarc Blank 878a574694606f0e5d781334d0d426fc379c51f3edMarc Blank @Override 88c4cdb11d24c19428dd39f986b00c1a29e75e1505Todd Kennedy public int getAccountColor(long accountId) { 898a574694606f0e5d781334d0d426fc379c51f3edMarc Blank return ResourceHelper.getInstance(mContext).getAccountColor(accountId); 908a574694606f0e5d781334d0d426fc379c51f3edMarc Blank } 9137500777f1ca2dc7a1f831b9f3af63cbe7b9d487Marc Blank 9237500777f1ca2dc7a1f831b9f3af63cbe7b9d487Marc Blank @Override 93c4cdb11d24c19428dd39f986b00c1a29e75e1505Todd Kennedy public Bundle getConfigurationData(String accountType) { 9437500777f1ca2dc7a1f831b9f3af63cbe7b9d487Marc Blank Bundle bundle = new Bundle(); 9537500777f1ca2dc7a1f831b9f3af63cbe7b9d487Marc Blank bundle.putBoolean(Configuration.EXCHANGE_CONFIGURATION_USE_ALTERNATE_STRINGS, 9637500777f1ca2dc7a1f831b9f3af63cbe7b9d487Marc Blank VendorPolicyLoader.getInstance(mContext).useAlternateExchangeStrings()); 9737500777f1ca2dc7a1f831b9f3af63cbe7b9d487Marc Blank return bundle; 9837500777f1ca2dc7a1f831b9f3af63cbe7b9d487Marc Blank } 99dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blank 100dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blank @Override 101c4cdb11d24c19428dd39f986b00c1a29e75e1505Todd Kennedy public String getDeviceId() { 102dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blank try { 103c4cdb11d24c19428dd39f986b00c1a29e75e1505Todd Kennedy EmailAsyncTask.runAsyncSerial(new Runnable() { 104fdec974c93817988a00248b7d223ec9f7f20ecb8Marc Blank @Override 105fdec974c93817988a00248b7d223ec9f7f20ecb8Marc Blank public void run() { 106bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook // Make sure the service is properly running (re: lifecycle) 107bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook EmailServiceUtils.startExchangeService(mContext); 108fdec974c93817988a00248b7d223ec9f7f20ecb8Marc Blank // Send current logging flags 109bc47398187c6ffd132435e51d8d61e6ec79a79dbPaul Westbrook Email.updateLoggingFlags(mContext); 110fdec974c93817988a00248b7d223ec9f7f20ecb8Marc Blank }}); 111dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blank return Device.getDeviceId(mContext); 112dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blank } catch (IOException e) { 113dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blank return null; 114dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blank } 115dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blank } 1168a574694606f0e5d781334d0d426fc379c51f3edMarc Blank }; 1178a574694606f0e5d781334d0d426fc379c51f3edMarc Blank 1188a574694606f0e5d781334d0d426fc379c51f3edMarc Blank @Override 1198a574694606f0e5d781334d0d426fc379c51f3edMarc Blank public IBinder onBind(Intent intent) { 1208a574694606f0e5d781334d0d426fc379c51f3edMarc Blank if (mContext == null) { 1218a574694606f0e5d781334d0d426fc379c51f3edMarc Blank mContext = this; 1228a574694606f0e5d781334d0d426fc379c51f3edMarc Blank } 123dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blank // Make sure we have a valid deviceId (just retrieves a static String except first time) 124dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blank try { 125dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blank Device.getDeviceId(this); 126dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blank } catch (IOException e) { 127dc78a769fce18d259eccc602c4623fa74cdf5319Marc Blank } 1288a574694606f0e5d781334d0d426fc379c51f3edMarc Blank return mBinder; 1298a574694606f0e5d781334d0d426fc379c51f3edMarc Blank } 1308a574694606f0e5d781334d0d426fc379c51f3edMarc Blank}