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.os.IBinder;
21import android.os.RemoteException;
22
23import com.android.emailcommon.provider.Account;
24import com.android.emailcommon.provider.Policy;
25import com.android.mail.utils.LogUtils;
26
27public class PolicyServiceProxy extends ServiceProxy implements IPolicyService {
28    private static final boolean DEBUG_PROXY = false; // DO NOT CHECK THIS IN SET TO TRUE
29    private static final String TAG = "PolicyServiceProxy";
30
31    private IPolicyService mService = null;
32    private Object mReturn = null;
33
34    public PolicyServiceProxy(Context _context) {
35        super(_context, getIntentForEmailPackage(_context, "POLICY_INTENT"));
36    }
37
38    @Override
39    public void onConnected(IBinder binder) {
40        mService = IPolicyService.Stub.asInterface(binder);
41    }
42
43    @Override
44    public IBinder asBinder() {
45        return null;
46    }
47
48    @Override
49    public boolean isActive(final Policy arg0) throws RemoteException {
50        setTask(new ProxyTask() {
51            @Override
52            public void run() throws RemoteException {
53                mReturn = mService.isActive(arg0);
54            }
55        }, "isActive");
56        waitForCompletion();
57        if (DEBUG_PROXY) {
58            LogUtils.v(TAG, "isActive: " + ((mReturn == null) ? "null" : mReturn));
59        }
60        if (mReturn == null) {
61            // This is not a great situation, but it's better to act like the policy isn't enforced
62            // rather than crash.
63            LogUtils.e(TAG, "PolicyService unavailable in isActive; assuming false");
64            return false;
65        } else {
66            return (Boolean)mReturn;
67        }
68    }
69
70    @Override
71    public void setAccountPolicy(final long accountId, final Policy policy,
72            final String securityKey) throws RemoteException {
73        setTask(new ProxyTask() {
74            @Override
75            public void run() throws RemoteException {
76                mService.setAccountPolicy(accountId, policy, securityKey);
77            }
78        }, "setAccountPolicy");
79        waitForCompletion();
80    }
81
82    @Override
83    public void remoteWipe() throws RemoteException {
84        setTask(new ProxyTask() {
85            @Override
86            public void run() throws RemoteException {
87                mService.remoteWipe();
88            }
89        }, "remoteWipe");
90    }
91
92    @Override
93    public void setAccountHoldFlag(final long arg0, final boolean arg1) throws RemoteException {
94        setTask(new ProxyTask() {
95            @Override
96            public void run() throws RemoteException {
97                mService.setAccountHoldFlag(arg0, arg1);
98            }
99        }, "setAccountHoldFlag");
100    }
101
102    // Static methods that encapsulate the proxy calls above
103    public static boolean isActive(Context context, Policy policies) {
104        try {
105            return new PolicyServiceProxy(context).isActive(policies);
106        } catch (RemoteException e) {
107        }
108        return false;
109    }
110
111    public static void setAccountHoldFlag(Context context, Account account, boolean newState) {
112        try {
113            new PolicyServiceProxy(context).setAccountHoldFlag(account.mId, newState);
114        } catch (RemoteException e) {
115            throw new IllegalStateException("PolicyService transaction failed");
116        }
117    }
118
119    public static void remoteWipe(Context context) {
120        try {
121            new PolicyServiceProxy(context).remoteWipe();
122        } catch (RemoteException e) {
123            throw new IllegalStateException("PolicyService transaction failed");
124        }
125    }
126
127    public static void setAccountPolicy(Context context, long accountId, Policy policy,
128            String securityKey) {
129        try {
130            new PolicyServiceProxy(context).setAccountPolicy(accountId, policy, securityKey);
131            return;
132        } catch (RemoteException e) {
133        }
134        throw new IllegalStateException("PolicyService transaction failed");
135    }
136}
137
138