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.email.service;
189ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
199ba506c4dd498150555f6c59aa758f7467bf9236Marc Blankimport android.app.Service;
209ba506c4dd498150555f6c59aa758f7467bf9236Marc Blankimport android.content.Context;
219ba506c4dd498150555f6c59aa758f7467bf9236Marc Blankimport android.content.Intent;
229ba506c4dd498150555f6c59aa758f7467bf9236Marc Blankimport android.os.IBinder;
239ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
24f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blankimport com.android.email.SecurityPolicy;
25f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blankimport com.android.emailcommon.provider.Policy;
26f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blankimport com.android.emailcommon.service.IPolicyService;
2795bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrookimport com.android.mail.utils.LogTag;
2895bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrookimport com.android.mail.utils.LogUtils;
29f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank
309ba506c4dd498150555f6c59aa758f7467bf9236Marc Blankpublic class PolicyService extends Service {
3195bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook    private static final String LOG_TAG = LogTag.getLogTag();
329ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
339ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    private SecurityPolicy mSecurityPolicy;
349ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    private Context mContext;
359ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
369ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    private final IPolicyService.Stub mBinder = new IPolicyService.Stub() {
379e521deb6bb525b33365cc2926cb2d0faa7095e2Scott Kennedy        @Override
38aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank        public boolean isActive(Policy policy) {
3995bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook            try {
4095bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                return mSecurityPolicy.isActive(policy);
4195bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook            } catch (RuntimeException e) {
4295bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                // Catch, log and rethrow the exception, as otherwise when the exception is
4395bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                // ultimately handled, the complete stack trace is losk
4495bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                LogUtils.e(LOG_TAG, e, "Exception thrown during call to SecurityPolicy#isActive");
4595bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                throw e;
4695bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook            }
479ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        }
489ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
499e521deb6bb525b33365cc2926cb2d0faa7095e2Scott Kennedy        @Override
509ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        public void setAccountHoldFlag(long accountId, boolean newState) {
519ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank            SecurityPolicy.setAccountHoldFlag(mContext, accountId, newState);
529ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        }
539ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
549e521deb6bb525b33365cc2926cb2d0faa7095e2Scott Kennedy        @Override
559ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        public void remoteWipe() {
5695bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook            try {
5795bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                mSecurityPolicy.remoteWipe();
5895bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook            } catch (RuntimeException e) {
5995bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                // Catch, log and rethrow the exception, as otherwise when the exception is
6095bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                // ultimately handled, the complete stack trace is losk
6195bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                LogUtils.e(LOG_TAG, e, "Exception thrown during call to SecurityPolicy#remoteWipe");
6295bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                throw e;
6395bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook            }
649ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        }
659ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
669e521deb6bb525b33365cc2926cb2d0faa7095e2Scott Kennedy        @Override
67f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank        public void setAccountPolicy(long accountId, Policy policy, String securityKey) {
6895bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook            try {
6995bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                mSecurityPolicy.setAccountPolicy(accountId, policy, securityKey);
7095bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook            } catch (RuntimeException e) {
7195bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                // Catch, log and rethrow the exception, as otherwise when the exception is
7295bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                // ultimately handled, the complete stack trace is losk
7395bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                LogUtils.e(LOG_TAG, e,
7495bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                        "Exception thrown from call to SecurityPolicy#setAccountPolicy");
7595bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook                throw e;
7695bb350f38d08beeabee68323b8514f3cbfd5db5Paul Westbrook            }
779ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        }
789ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    };
799ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank
809ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    @Override
819ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    public IBinder onBind(Intent intent) {
829ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        // When we bind this service, save the context and SecurityPolicy singleton
839ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        mContext = this;
849ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        mSecurityPolicy = SecurityPolicy.getInstance(this);
859ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank        return mBinder;
869ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank    }
879ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank}