PolicyServiceProxy.java revision 560bfadc3151f7a06f3b06e9a6c92cfa534c63ec
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 throw new ServiceUnavailableException("isActive"); 62 } else { 63 return (Boolean)mReturn; 64 } 65 } 66 67 @Override 68 public void setAccountPolicy(final long accountId, final Policy policy, 69 final String securityKey) throws RemoteException { 70 setTask(new ProxyTask() { 71 @Override 72 public void run() throws RemoteException { 73 mService.setAccountPolicy(accountId, policy, securityKey); 74 } 75 }, "setAccountPolicy"); 76 waitForCompletion(); 77 } 78 79 @Override 80 public void remoteWipe() throws RemoteException { 81 setTask(new ProxyTask() { 82 @Override 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 @Override 93 public void run() throws RemoteException { 94 mService.setAccountHoldFlag(arg0, arg1); 95 } 96 }, "setAccountHoldFlag"); 97 } 98 99 // Static methods that encapsulate the proxy calls above 100 public static boolean isActive(Context context, Policy policies) { 101 try { 102 return new PolicyServiceProxy(context).isActive(policies); 103 } catch (RemoteException e) { 104 } 105 return false; 106 } 107 108 public static void setAccountHoldFlag(Context context, Account account, boolean newState) { 109 try { 110 new PolicyServiceProxy(context).setAccountHoldFlag(account.mId, newState); 111 } catch (RemoteException e) { 112 throw new IllegalStateException("PolicyService transaction failed"); 113 } 114 } 115 116 public static void remoteWipe(Context context) { 117 try { 118 new PolicyServiceProxy(context).remoteWipe(); 119 } catch (RemoteException e) { 120 throw new IllegalStateException("PolicyService transaction failed"); 121 } 122 } 123 124 public static void setAccountPolicy(Context context, long accountId, Policy policy, 125 String securityKey) { 126 try { 127 new PolicyServiceProxy(context).setAccountPolicy(accountId, policy, securityKey); 128 return; 129 } catch (RemoteException e) { 130 } 131 throw new IllegalStateException("PolicyService transaction failed"); 132 } 133} 134 135