AccountService.java revision c6089bc01f2ae49fb11904a4b4f222811358254f
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) {
51            NotificationController.getInstance(mContext).showLoginFailedNotification(accountId);
52        }
53
54        @Override
55        public void notifyLoginSucceeded(long accountId) {
56            NotificationController.getInstance(mContext).cancelLoginFailedNotification(accountId);
57        }
58
59        private ArrayList<Account> getAccountList(String forProtocol) {
60            ArrayList<Account> providerAccounts = new ArrayList<Account>();
61            Cursor c = mContext.getContentResolver().query(Account.CONTENT_URI,
62                    Account.ID_PROJECTION, null, null, null);
63            try {
64                while (c.moveToNext()) {
65                    long accountId = c.getLong(Account.CONTENT_ID_COLUMN);
66                    String protocol = Account.getProtocol(mContext, accountId);
67                    if ((protocol != null) && forProtocol.equals(protocol)) {
68                        Account account = Account.restoreAccountWithId(mContext, accountId);
69                        if (account != null) {
70                            providerAccounts.add(account);
71                        }
72                    }
73                }
74            } finally {
75                c.close();
76            }
77            return providerAccounts;
78        }
79
80        @Override
81        public void reconcileAccounts(String protocol, String accountManagerType) {
82            ArrayList<Account> providerList = getAccountList(protocol);
83            android.accounts.Account[] accountMgrList =
84                AccountManager.get(mContext).getAccountsByType(accountManagerType);
85            AccountReconciler.reconcileAccounts(mContext, providerList, accountMgrList, mContext);
86        }
87
88        @Override
89        public int getAccountColor(long accountId) {
90            return ResourceHelper.getInstance(mContext).getAccountColor(accountId);
91        }
92
93        @Override
94        public Bundle getConfigurationData(String accountType) {
95            Bundle bundle = new Bundle();
96            bundle.putBoolean(Configuration.EXCHANGE_CONFIGURATION_USE_ALTERNATE_STRINGS,
97                    VendorPolicyLoader.getInstance(mContext).useAlternateExchangeStrings());
98            return bundle;
99        }
100
101        @Override
102        public String getDeviceId() {
103            try {
104                EmailAsyncTask.runAsyncSerial(new Runnable() {
105                    @Override
106                    public void run() {
107                        // Make sure remote services are running (re: lifecycle)
108                        EmailServiceUtils.startRemoteServices(mContext);
109                        // Send current logging flags
110                        MailActivityEmail.updateLoggingFlags(mContext);
111                    }});
112                return Device.getDeviceId(mContext);
113            } catch (IOException e) {
114                return null;
115            }
116        }
117    };
118
119    @Override
120    public IBinder onBind(Intent intent) {
121        if (mContext == null) {
122            mContext = this;
123        }
124        // Make sure we have a valid deviceId (just retrieves a static String except first time)
125        try {
126            Device.getDeviceId(this);
127        } catch (IOException e) {
128        }
129        return mBinder;
130    }
131}