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.emailcommon.service;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.os.IBinder;
     22 import android.os.RemoteException;
     23 import android.util.Log;
     24 
     25 import com.android.emailcommon.provider.Account;
     26 import com.android.emailcommon.provider.Policy;
     27 
     28 public class PolicyServiceProxy extends ServiceProxy implements IPolicyService {
     29     private static final boolean DEBUG_PROXY = false; // DO NOT CHECK THIS IN SET TO TRUE
     30     private static final String TAG = "PolicyServiceProxy";
     31 
     32     // The intent used by sync adapter services to connect to the PolicyService
     33     public static final String POLICY_INTENT = "com.android.email.POLICY_INTENT";
     34 
     35     private IPolicyService mService = null;
     36     private Object mReturn = null;
     37 
     38     public PolicyServiceProxy(Context _context) {
     39         super(_context, new Intent(POLICY_INTENT));
     40     }
     41 
     42     @Override
     43     public void onConnected(IBinder binder) {
     44         mService = IPolicyService.Stub.asInterface(binder);
     45     }
     46 
     47     public IBinder asBinder() {
     48         return null;
     49     }
     50 
     51     @Override
     52     public Policy clearUnsupportedPolicies(final Policy arg0) throws RemoteException {
     53         setTask(new ProxyTask() {
     54             public void run() throws RemoteException {
     55                 mReturn = mService.clearUnsupportedPolicies(arg0);
     56             }
     57         }, "clearUnsupportedPolicies");
     58         waitForCompletion();
     59         if (DEBUG_PROXY) {
     60             Log.v(TAG, "clearUnsupportedPolicies: " + ((mReturn == null) ? "null" : mReturn));
     61         }
     62         if (mReturn == null) {
     63             throw new ServiceUnavailableException("clearUnsupportedPolicies");
     64         } else {
     65             return (Policy)mReturn;
     66         }
     67     }
     68 
     69     @Override
     70     public boolean isActive(final Policy arg0) throws RemoteException {
     71         setTask(new ProxyTask() {
     72             public void run() throws RemoteException {
     73                 mReturn = mService.isActive(arg0);
     74             }
     75         }, "isActive");
     76         waitForCompletion();
     77         if (DEBUG_PROXY) {
     78             Log.v(TAG, "isActive: " + ((mReturn == null) ? "null" : mReturn));
     79         }
     80         if (mReturn == null) {
     81             throw new ServiceUnavailableException("isActive");
     82         } else {
     83             return (Boolean)mReturn;
     84         }
     85     }
     86 
     87     @Override
     88     public boolean isActiveAdmin() throws RemoteException {
     89         setTask(new ProxyTask() {
     90             public void run() throws RemoteException {
     91                 mReturn = mService.isActiveAdmin();
     92             }
     93         }, "isActiveAdmin");
     94         waitForCompletion();
     95         if (DEBUG_PROXY) {
     96             Log.v(TAG, "isActiveAdmin: " + ((mReturn == null) ? "null" : mReturn));
     97         }
     98         if (mReturn == null) {
     99             throw new ServiceUnavailableException("isActiveAdmin");
    100         } else {
    101             return (Boolean)mReturn;
    102         }
    103     }
    104 
    105     @Override
    106     public boolean isSupported(final Policy arg0) throws RemoteException {
    107         setTask(new ProxyTask() {
    108             public void run() throws RemoteException {
    109                 mReturn = mService.isSupported(arg0);
    110             }
    111         }, "isSupported");
    112         waitForCompletion();
    113         if (DEBUG_PROXY) {
    114             Log.v(TAG, "isSupported: " + ((mReturn == null) ? "null" : mReturn));
    115         }
    116         if (mReturn == null) {
    117             throw new ServiceUnavailableException("isSupported");
    118         } else {
    119             return (Boolean)mReturn;
    120         }
    121     }
    122 
    123     @Override
    124     public void policiesRequired(final long arg0) throws RemoteException {
    125         setTask(new ProxyTask() {
    126             public void run() throws RemoteException {
    127                 mService.policiesRequired(arg0);
    128             }
    129         }, "policiesRequired");
    130     }
    131 
    132     @Override
    133     public void remoteWipe() throws RemoteException {
    134         setTask(new ProxyTask() {
    135             public void run() throws RemoteException {
    136                 mService.remoteWipe();
    137             }
    138         }, "remoteWipe");
    139     }
    140 
    141     @Override
    142     public void setAccountHoldFlag(final long arg0, final boolean arg1) throws RemoteException {
    143         setTask(new ProxyTask() {
    144             public void run() throws RemoteException {
    145                 mService.setAccountHoldFlag(arg0, arg1);
    146             }
    147         }, "setAccountHoldFlag");
    148     }
    149 
    150     @Override
    151     public void policiesUpdated(final long arg0) throws RemoteException {
    152         setTask(new ProxyTask() {
    153             public void run() throws RemoteException {
    154                 mService.policiesUpdated(arg0);
    155             }
    156         }, "policiesUpdated");
    157     }
    158 
    159     // Static methods that encapsulate the proxy calls above
    160     public static boolean isActive(Context context, Policy policies) {
    161         try {
    162             return new PolicyServiceProxy(context).isActive(policies);
    163         } catch (RemoteException e) {
    164         }
    165         return false;
    166     }
    167 
    168     public static void policiesRequired(Context context, long accountId) {
    169         try {
    170             new PolicyServiceProxy(context).policiesRequired(accountId);
    171         } catch (RemoteException e) {
    172             throw new IllegalStateException("PolicyService transaction failed");
    173         }
    174     }
    175 
    176     public static void policiesUpdated(Context context, long accountId) {
    177         try {
    178             new PolicyServiceProxy(context).policiesUpdated(accountId);
    179         } catch (RemoteException e) {
    180             throw new IllegalStateException("PolicyService transaction failed");
    181         }
    182     }
    183 
    184     public static void setAccountHoldFlag(Context context, Account account, boolean newState) {
    185         try {
    186             new PolicyServiceProxy(context).setAccountHoldFlag(account.mId, newState);
    187         } catch (RemoteException e) {
    188             throw new IllegalStateException("PolicyService transaction failed");
    189         }
    190     }
    191 
    192     public static boolean isActiveAdmin(Context context) {
    193         try {
    194             return new PolicyServiceProxy(context).isActiveAdmin();
    195         } catch (RemoteException e) {
    196         }
    197         return false;
    198     }
    199 
    200     public static void remoteWipe(Context context) {
    201         try {
    202             new PolicyServiceProxy(context).remoteWipe();
    203         } catch (RemoteException e) {
    204             throw new IllegalStateException("PolicyService transaction failed");
    205         }
    206     }
    207 
    208     public static boolean isSupported(Context context, Policy policy) {
    209         try {
    210             return new PolicyServiceProxy(context).isSupported(policy);
    211         } catch (RemoteException e) {
    212         }
    213         return false;
    214      }
    215 
    216     public static Policy clearUnsupportedPolicies(Context context, Policy policy) {
    217         try {
    218             return new PolicyServiceProxy(context).clearUnsupportedPolicies(policy);
    219         } catch (RemoteException e) {
    220         }
    221         throw new IllegalStateException("PolicyService transaction failed");
    222     }
    223 }
    224 
    225