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        setAccountPolicy2(accountId, policy, securityKey, true /* notify */);
74    }
75
76    @Override
77    public void setAccountPolicy2(final long accountId, final Policy policy,
78            final String securityKey, final boolean notify) throws RemoteException {
79        setTask(new ProxyTask() {
80            @Override
81            public void run() throws RemoteException {
82                mService.setAccountPolicy2(accountId, policy, securityKey, notify);
83            }
84        }, "setAccountPolicy2");
85        waitForCompletion();
86    }
87
88    public boolean canDisableCamera() throws RemoteException {
89        setTask(new ProxyTask() {
90            @Override
91            public void run() throws RemoteException {
92                mReturn = mService.canDisableCamera();
93            }
94        }, "canDisableCamera");
95        waitForCompletion();
96        if (mReturn == null) {
97            // This is not a great situation, but it's better to act like the policy isn't enforced
98            // rather than crash.
99            LogUtils.e(TAG, "PolicyService unavailable in canDisableCamera; assuming false");
100            return false;
101        } else {
102            return (Boolean)mReturn;
103        }
104    }
105
106    @Override
107    public void remoteWipe() throws RemoteException {
108        setTask(new ProxyTask() {
109            @Override
110            public void run() throws RemoteException {
111                mService.remoteWipe();
112            }
113        }, "remoteWipe");
114    }
115
116    @Override
117    public void setAccountHoldFlag(final long arg0, final boolean arg1) throws RemoteException {
118        setTask(new ProxyTask() {
119            @Override
120            public void run() throws RemoteException {
121                mService.setAccountHoldFlag(arg0, arg1);
122            }
123        }, "setAccountHoldFlag");
124    }
125
126    // Static methods that encapsulate the proxy calls above
127    public static boolean isActive(Context context, Policy policies) {
128        try {
129            return new PolicyServiceProxy(context).isActive(policies);
130        } catch (RemoteException e) {
131        }
132        return false;
133    }
134
135    public static void setAccountHoldFlag(Context context, Account account, boolean newState) {
136        try {
137            new PolicyServiceProxy(context).setAccountHoldFlag(account.mId, newState);
138        } catch (RemoteException e) {
139            throw new IllegalStateException("PolicyService transaction failed");
140        }
141    }
142
143    public static void remoteWipe(Context context) {
144        try {
145            new PolicyServiceProxy(context).remoteWipe();
146        } catch (RemoteException e) {
147            throw new IllegalStateException("PolicyService transaction failed");
148        }
149    }
150
151    public static void setAccountPolicy(Context context, long accountId, Policy policy,
152            String securityKey) {
153        setAccountPolicy2(context, accountId, policy, securityKey, true /* notify */);
154    }
155
156    public static void setAccountPolicy2(Context context, long accountId, Policy policy,
157            String securityKey, boolean notify) {
158        try {
159            new PolicyServiceProxy(context).setAccountPolicy2(accountId, policy, securityKey,
160                    notify);
161            return;
162        } catch (RemoteException e) {
163        }
164        throw new IllegalStateException("PolicyService transaction failed");
165    }
166
167    public static boolean canDisableCamera(Context context) {
168        try {
169            return new PolicyServiceProxy(context).canDisableCamera();
170        } catch (RemoteException e) {
171        }
172        return false;
173    }
174}
175
176