19ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank/*
29ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank * Copyright (C) 2011 The Android Open Source Project
39ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank *
49ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank * Licensed under the Apache License, Version 2.0 (the "License");
59ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank * you may not use this file except in compliance with the License.
69ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank * You may obtain a copy of the License at
79ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank *
89ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank *      http://www.apache.org/licenses/LICENSE-2.0
99ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank *
109ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank * Unless required by applicable law or agreed to in writing, software
119ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank * distributed under the License is distributed on an "AS IS" BASIS,
129ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank * See the License for the specific language governing permissions and
149ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank * limitations under the License.
159ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank */
169ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
179ba506c4dd498150555f6c59aa758f7467bf9236Marc Blankpackage com.android.emailcommon.service;
189ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
199ba506c4dd498150555f6c59aa758f7467bf9236Marc Blankimport android.content.Context;
209ba506c4dd498150555f6c59aa758f7467bf9236Marc Blankimport android.os.IBinder;
219ba506c4dd498150555f6c59aa758f7467bf9236Marc Blankimport android.os.RemoteException;
229ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
23c28fdfeaaddbd7d3f3b8f78895303f7a9ae5a521Marc Blankimport com.android.emailcommon.provider.Account;
24c28fdfeaaddbd7d3f3b8f78895303f7a9ae5a521Marc Blankimport com.android.emailcommon.provider.Policy;
25560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedyimport com.android.mail.utils.LogUtils;
26c28fdfeaaddbd7d3f3b8f78895303f7a9ae5a521Marc Blank
279ba506c4dd498150555f6c59aa758f7467bf9236Marc Blankpublic class PolicyServiceProxy extends ServiceProxy implements IPolicyService {
28da3c4b8261825063ddf081e9335823569b813bd3Marc Blank    private static final boolean DEBUG_PROXY = false; // DO NOT CHECK THIS IN SET TO TRUE
299ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    private static final String TAG = "PolicyServiceProxy";
309ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
319ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    private IPolicyService mService = null;
329ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    private Object mReturn = null;
339ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
349ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    public PolicyServiceProxy(Context _context) {
35e714bb9d153cfe13a7f0932e7d67ea08fa5a1d98Marc Blank        super(_context, getIntentForEmailPackage(_context, "POLICY_INTENT"));
369ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    }
379ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
389ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    @Override
399ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    public void onConnected(IBinder binder) {
409ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        mService = IPolicyService.Stub.asInterface(binder);
419ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    }
429ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
43560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy    @Override
449ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    public IBinder asBinder() {
459ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        return null;
469ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    }
479ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
489ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    @Override
49aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank    public boolean isActive(final Policy arg0) throws RemoteException {
509ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        setTask(new ProxyTask() {
51560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy            @Override
529ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank            public void run() throws RemoteException {
539ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank                mReturn = mService.isActive(arg0);
549ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank            }
559ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        }, "isActive");
569ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        waitForCompletion();
579ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        if (DEBUG_PROXY) {
58560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy            LogUtils.v(TAG, "isActive: " + ((mReturn == null) ? "null" : mReturn));
599ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        }
609ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        if (mReturn == null) {
61eb190a8d9d42ab9e4c063babc14d3b0094008bf7Yu Ping Hu            // This is not a great situation, but it's better to act like the policy isn't enforced
62eb190a8d9d42ab9e4c063babc14d3b0094008bf7Yu Ping Hu            // rather than crash.
63eb190a8d9d42ab9e4c063babc14d3b0094008bf7Yu Ping Hu            LogUtils.e(TAG, "PolicyService unavailable in isActive; assuming false");
64eb190a8d9d42ab9e4c063babc14d3b0094008bf7Yu Ping Hu            return false;
659ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        } else {
669ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank            return (Boolean)mReturn;
679ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        }
689ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    }
699ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
709ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    @Override
71f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank    public void setAccountPolicy(final long accountId, final Policy policy,
72f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank            final String securityKey) throws RemoteException {
7334662f11b2ff38f05219415f3672a52a7af2338fTony Mantler        setAccountPolicy2(accountId, policy, securityKey, true /* notify */);
7434662f11b2ff38f05219415f3672a52a7af2338fTony Mantler    }
7534662f11b2ff38f05219415f3672a52a7af2338fTony Mantler
7634662f11b2ff38f05219415f3672a52a7af2338fTony Mantler    @Override
7734662f11b2ff38f05219415f3672a52a7af2338fTony Mantler    public void setAccountPolicy2(final long accountId, final Policy policy,
7834662f11b2ff38f05219415f3672a52a7af2338fTony Mantler            final String securityKey, final boolean notify) throws RemoteException {
799ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        setTask(new ProxyTask() {
80560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy            @Override
819ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank            public void run() throws RemoteException {
8234662f11b2ff38f05219415f3672a52a7af2338fTony Mantler                mService.setAccountPolicy2(accountId, policy, securityKey, notify);
839ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank            }
8434662f11b2ff38f05219415f3672a52a7af2338fTony Mantler        }, "setAccountPolicy2");
859ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        waitForCompletion();
869ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    }
879ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
88ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon    public boolean canDisableCamera() throws RemoteException {
89ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon        setTask(new ProxyTask() {
90ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon            @Override
91ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon            public void run() throws RemoteException {
92ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon                mReturn = mService.canDisableCamera();
93ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon            }
94ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon        }, "canDisableCamera");
95ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon        waitForCompletion();
96ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon        if (mReturn == null) {
97ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon            // This is not a great situation, but it's better to act like the policy isn't enforced
98ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon            // rather than crash.
99ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon            LogUtils.e(TAG, "PolicyService unavailable in canDisableCamera; assuming false");
100ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon            return false;
101ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon        } else {
102ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon            return (Boolean)mReturn;
103ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon        }
104ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon    }
105ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon
1069ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    @Override
1079ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    public void remoteWipe() throws RemoteException {
1089ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        setTask(new ProxyTask() {
109560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy            @Override
1109ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank            public void run() throws RemoteException {
1119ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank                mService.remoteWipe();
1129ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank            }
1139ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        }, "remoteWipe");
1149ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    }
1159ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
1169ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    @Override
1179ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    public void setAccountHoldFlag(final long arg0, final boolean arg1) throws RemoteException {
1189ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        setTask(new ProxyTask() {
119560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy            @Override
1209ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank            public void run() throws RemoteException {
1219ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank                mService.setAccountHoldFlag(arg0, arg1);
1229ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank            }
1239ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        }, "setAccountHoldFlag");
1249ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    }
1259ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
1269ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    // Static methods that encapsulate the proxy calls above
127aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank    public static boolean isActive(Context context, Policy policies) {
1289ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        try {
1299ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank            return new PolicyServiceProxy(context).isActive(policies);
1309ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        } catch (RemoteException e) {
1319ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        }
1329ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        return false;
1339ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    }
1349ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
1359ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    public static void setAccountHoldFlag(Context context, Account account, boolean newState) {
1369ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        try {
1379ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank            new PolicyServiceProxy(context).setAccountHoldFlag(account.mId, newState);
1389ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        } catch (RemoteException e) {
1399ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank            throw new IllegalStateException("PolicyService transaction failed");
1409ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        }
1419ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    }
1429ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
1439ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    public static void remoteWipe(Context context) {
1449ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        try {
1459ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank            new PolicyServiceProxy(context).remoteWipe();
1469ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        } catch (RemoteException e) {
1479ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank            throw new IllegalStateException("PolicyService transaction failed");
1489ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        }
1499ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    }
1509ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
151f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank    public static void setAccountPolicy(Context context, long accountId, Policy policy,
152f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank            String securityKey) {
15334662f11b2ff38f05219415f3672a52a7af2338fTony Mantler        setAccountPolicy2(context, accountId, policy, securityKey, true /* notify */);
15434662f11b2ff38f05219415f3672a52a7af2338fTony Mantler    }
15534662f11b2ff38f05219415f3672a52a7af2338fTony Mantler
15634662f11b2ff38f05219415f3672a52a7af2338fTony Mantler    public static void setAccountPolicy2(Context context, long accountId, Policy policy,
15734662f11b2ff38f05219415f3672a52a7af2338fTony Mantler            String securityKey, boolean notify) {
1589ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        try {
15934662f11b2ff38f05219415f3672a52a7af2338fTony Mantler            new PolicyServiceProxy(context).setAccountPolicy2(accountId, policy, securityKey,
16034662f11b2ff38f05219415f3672a52a7af2338fTony Mantler                    notify);
161f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank            return;
1629ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        } catch (RemoteException e) {
1639ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        }
1649ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        throw new IllegalStateException("PolicyService transaction failed");
1659ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    }
166ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon
167ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon    public static boolean canDisableCamera(Context context) {
168ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon        try {
169ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon            return new PolicyServiceProxy(context).canDisableCamera();
170ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon        } catch (RemoteException e) {
171ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon        }
172ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon        return false;
173ce6916b32a98a568ceafb734d050801f4459a532Martin Hibdon    }
1749ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank}
1759ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
176