Home | History | Annotate | Download | only in service
      1 /*
      2  * Copyright (C) 2010 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 import android.os.RemoteException;
     25 
     26 import com.android.emailcommon.Api;
     27 import com.android.emailcommon.provider.HostAuth;
     28 import com.android.emailcommon.service.EmailServiceProxy;
     29 import com.android.emailcommon.service.IEmailService;
     30 import com.android.emailcommon.service.IEmailServiceCallback;
     31 import com.android.emailcommon.service.SearchParams;
     32 
     33 /**
     34  * Utility functions for EmailService support.
     35  */
     36 public class EmailServiceUtils {
     37     /**
     38      * Starts an EmailService by name
     39      */
     40     public static void startService(Context context, String intentAction) {
     41         context.startService(new Intent(intentAction));
     42     }
     43 
     44     /**
     45      * Returns an {@link IEmailService} for the service; otherwise returns an empty
     46      * {@link IEmailService} implementation.
     47      *
     48      * @param context
     49      * @param callback Object to get callback, or can be null
     50      */
     51     public static IEmailService getService(Context context, String intentAction,
     52             IEmailServiceCallback callback) {
     53         return new EmailServiceProxy(context, intentAction, callback);
     54     }
     55 
     56     /**
     57      * Determine if the EmailService is available
     58      */
     59     public static boolean isServiceAvailable(Context context, String intentAction) {
     60         return new EmailServiceProxy(context, intentAction, null).test();
     61     }
     62 
     63     public static void startExchangeService(Context context) {
     64         startService(context, EmailServiceProxy.EXCHANGE_INTENT);
     65     }
     66 
     67     public static IEmailService getExchangeService(Context context,
     68             IEmailServiceCallback callback) {
     69         return getService(context, EmailServiceProxy.EXCHANGE_INTENT, callback);
     70     }
     71 
     72     public static boolean isExchangeAvailable(Context context) {
     73         return isServiceAvailable(context, EmailServiceProxy.EXCHANGE_INTENT);
     74     }
     75 
     76     /**
     77      * An empty {@link IEmailService} implementation which is used instead of
     78      * {@link com.android.exchange.ExchangeService} on the build with no exchange support.
     79      *
     80      * <p>In theory, the service in question isn't used on the no-exchange-support build,
     81      * because we won't have any exchange accounts in that case, so we wouldn't have to have this
     82      * class.  However, there are a few places we do use the service even if there's no exchange
     83      * accounts (e.g. setLogging), so this class is added for safety and simplicity.
     84      */
     85     public static class NullEmailService extends Service implements IEmailService {
     86         public static final NullEmailService INSTANCE = new NullEmailService();
     87 
     88         public int getApiLevel() {
     89             return Api.LEVEL;
     90         }
     91 
     92         public Bundle autoDiscover(String userName, String password) throws RemoteException {
     93             return Bundle.EMPTY;
     94         }
     95 
     96         public boolean createFolder(long accountId, String name) throws RemoteException {
     97             return false;
     98         }
     99 
    100         public boolean deleteFolder(long accountId, String name) throws RemoteException {
    101             return false;
    102         }
    103 
    104         public void hostChanged(long accountId) throws RemoteException {
    105         }
    106 
    107         public void loadAttachment(long attachmentId, boolean background) throws RemoteException {
    108         }
    109 
    110         public void loadMore(long messageId) throws RemoteException {
    111         }
    112 
    113         public boolean renameFolder(long accountId, String oldName, String newName)
    114                 throws RemoteException {
    115             return false;
    116         }
    117 
    118         public void sendMeetingResponse(long messageId, int response) throws RemoteException {
    119         }
    120 
    121         public void setCallback(IEmailServiceCallback cb) throws RemoteException {
    122         }
    123 
    124         public void setLogging(int flags) throws RemoteException {
    125         }
    126 
    127         public void startSync(long mailboxId, boolean userRequest) throws RemoteException {
    128         }
    129 
    130         public void stopSync(long mailboxId) throws RemoteException {
    131         }
    132 
    133         public void updateFolderList(long accountId) throws RemoteException {
    134         }
    135 
    136         public Bundle validate(HostAuth hostAuth) throws RemoteException {
    137             return null;
    138         }
    139 
    140         public void deleteAccountPIMData(long accountId) throws RemoteException {
    141         }
    142 
    143         public int searchMessages(long accountId, SearchParams searchParams, long destMailboxId) {
    144             return 0;
    145         }
    146 
    147         public IBinder asBinder() {
    148             return null;
    149         }
    150 
    151         @Override
    152         public IBinder onBind(Intent intent) {
    153             return null;
    154         }
    155     }
    156 }
    157