PolicyServiceProxy.java revision 2736c1a11ce3ecdcd9d19aa9c324fb9ce0910c7b
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.emailcommon.service;
18
19import android.content.Context;
20import android.content.Intent;
21import android.os.IBinder;
22import android.os.RemoteException;
23import android.util.Log;
24
25import com.android.emailcommon.provider.Account;
26import com.android.emailcommon.provider.Policy;
27
28public 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 boolean isActive(final Policy arg0) throws RemoteException {
53        setTask(new ProxyTask() {
54            public void run() throws RemoteException {
55                mReturn = mService.isActive(arg0);
56            }
57        }, "isActive");
58        waitForCompletion();
59        if (DEBUG_PROXY) {
60            Log.v(TAG, "isActive: " + ((mReturn == null) ? "null" : mReturn));
61        }
62        if (mReturn == null) {
63            throw new ServiceUnavailableException("isActive");
64        } else {
65            return (Boolean)mReturn;
66        }
67    }
68
69    @Override
70    public void setAccountPolicy(final long accountId, final Policy policy,
71            final String securityKey) throws RemoteException {
72        setTask(new ProxyTask() {
73            public void run() throws RemoteException {
74                mService.setAccountPolicy(accountId, policy, securityKey);
75            }
76        }, "setAccountPolicy");
77        waitForCompletion();
78    }
79
80    @Override
81    public void remoteWipe() throws RemoteException {
82        setTask(new ProxyTask() {
83            public void run() throws RemoteException {
84                mService.remoteWipe();
85            }
86        }, "remoteWipe");
87    }
88
89    @Override
90    public void setAccountHoldFlag(final long arg0, final boolean arg1) throws RemoteException {
91        setTask(new ProxyTask() {
92            public void run() throws RemoteException {
93                mService.setAccountHoldFlag(arg0, arg1);
94            }
95        }, "setAccountHoldFlag");
96    }
97
98    // Static methods that encapsulate the proxy calls above
99    public static boolean isActive(Context context, Policy policies) {
100        try {
101            return new PolicyServiceProxy(context).isActive(policies);
102        } catch (RemoteException e) {
103        }
104        return false;
105    }
106
107    public static void setAccountHoldFlag(Context context, Account account, boolean newState) {
108        try {
109            new PolicyServiceProxy(context).setAccountHoldFlag(account.mId, newState);
110        } catch (RemoteException e) {
111            throw new IllegalStateException("PolicyService transaction failed");
112        }
113    }
114
115    public static void remoteWipe(Context context) {
116        try {
117            new PolicyServiceProxy(context).remoteWipe();
118        } catch (RemoteException e) {
119            throw new IllegalStateException("PolicyService transaction failed");
120        }
121    }
122
123    public static void setAccountPolicy(Context context, long accountId, Policy policy,
124            String securityKey) {
125        try {
126            new PolicyServiceProxy(context).setAccountPolicy(accountId, policy, securityKey);
127            return;
128        } catch (RemoteException e) {
129        }
130        throw new IllegalStateException("PolicyService transaction failed");
131    }
132}
133
134