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.app.Service;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.os.Bundle;
     23 import android.os.IBinder;
     24 
     25 import com.android.email.NotificationController;
     26 import com.android.email.ResourceHelper;
     27 import com.android.email2.ui.MailActivityEmail;
     28 import com.android.emailcommon.Configuration;
     29 import com.android.emailcommon.Device;
     30 import com.android.emailcommon.VendorPolicyLoader;
     31 import com.android.emailcommon.service.IAccountService;
     32 import com.android.emailcommon.utility.EmailAsyncTask;
     33 
     34 import java.io.IOException;
     35 
     36 public class AccountService extends Service {
     37 
     38     // Save context
     39     private Context mContext;
     40 
     41     private final IAccountService.Stub mBinder = new IAccountService.Stub() {
     42 
     43         @Override
     44         public void notifyLoginFailed(long accountId, String reason) {
     45             NotificationController nc = NotificationController.getInstance(mContext);
     46             nc.showLoginFailedNotification(accountId, reason);
     47         }
     48 
     49         @Override
     50         public void notifyLoginSucceeded(long accountId) {
     51             NotificationController.getInstance(mContext).cancelLoginFailedNotification(accountId);
     52         }
     53 
     54         @Override
     55         public void reconcileAccounts(String protocol, String accountManagerType) {
     56             // TODO: No longer used, delete this.
     57         }
     58 
     59         @Override
     60         public int getAccountColor(long accountId) {
     61             return ResourceHelper.getInstance(mContext).getAccountColor(accountId);
     62         }
     63 
     64         @Override
     65         public Bundle getConfigurationData(String accountType) {
     66             Bundle bundle = new Bundle();
     67             bundle.putBoolean(Configuration.EXCHANGE_CONFIGURATION_USE_ALTERNATE_STRINGS,
     68                     VendorPolicyLoader.getInstance(mContext).useAlternateExchangeStrings());
     69             return bundle;
     70         }
     71 
     72         @Override
     73         public String getDeviceId() {
     74             try {
     75                 EmailAsyncTask.runAsyncSerial(new Runnable() {
     76                     @Override
     77                     public void run() {
     78                         // Make sure remote services are running (re: lifecycle)
     79                         EmailServiceUtils.startRemoteServices(mContext);
     80                         // Send current logging flags
     81                         MailActivityEmail.updateLoggingFlags(mContext);
     82                     }});
     83                 return Device.getDeviceId(mContext);
     84             } catch (IOException e) {
     85                 return null;
     86             }
     87         }
     88     };
     89 
     90     @Override
     91     public IBinder onBind(Intent intent) {
     92         if (mContext == null) {
     93             mContext = this;
     94         }
     95         // Make sure we have a valid deviceId (just retrieves a static String except first time)
     96         try {
     97             Device.getDeviceId(this);
     98         } catch (IOException e) {
     99         }
    100         return mBinder;
    101     }
    102 }
    103