1 /*
2  * Copyright (C) 2012 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 package com.android.tradefed.result;
17 
18 import com.android.tradefed.config.OptionClass;
19 import com.android.tradefed.util.Email;
20 import com.android.tradefed.util.IEmail;
21 
22 /**
23  * An {@link EmailResultReporter} that can also restrict notifications to just invocation failures.
24  */
25 @OptionClass(alias = "invocation-failure-email")
26 public class InvocationFailureEmailResultReporter extends EmailResultReporter {
27 
28     private boolean mDisabled = false;
29 
30     /**
31      * Default constructor
32      */
InvocationFailureEmailResultReporter()33     public InvocationFailureEmailResultReporter() {
34         this(new Email());
35     }
36 
37     /**
38      * Create a {@link InvocationFailureEmailResultReporter} with a custom {@link IEmail} instance
39      * to use.
40      * <p/>
41      * Exposed for unit testing.
42      *
43      * @param mailer the {@link IEmail} instance to use.
44      */
InvocationFailureEmailResultReporter(IEmail mailer)45     protected InvocationFailureEmailResultReporter(IEmail mailer) {
46         super(mailer);
47     }
48 
49     /**
50      * Send a message if there was an invocation failure.
51      *
52      * @return {@code true} if the InvocationStatus is not {@link InvocationStatus#SUCCESS}, or
53      * {@code false} if it is.
54      */
55     @Override
shouldSendMessage()56     protected boolean shouldSendMessage() {
57         if (mDisabled) {
58             // Inhibit the failure from sending emails if we are shutting down TF.
59             return false;
60         }
61         return !getInvocationStatus().equals(InvocationStatus.SUCCESS);
62     }
63 
64     /**
65      * {@inheritDoc}
66      */
67     @Override
invocationInterrupted()68     public void invocationInterrupted() {
69         mDisabled = true;
70     }
71 }
72