1 /*
2  * Copyright (C) 2016 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 
17 package com.android.incallui.answer.impl.answermethod;
18 
19 import android.app.Activity;
20 import android.support.annotation.NonNull;
21 import android.support.annotation.Nullable;
22 import android.support.annotation.VisibleForTesting;
23 import android.support.v4.app.Fragment;
24 import com.android.dialer.common.LogUtil;
25 import com.android.incallui.util.AccessibilityUtil;
26 
27 /** Creates the appropriate {@link AnswerMethod} for the circumstances. */
28 public class AnswerMethodFactory {
29   private static boolean shouldUseTwoButtonMethodForTesting;
30 
31   @NonNull
createAnswerMethod(@onNull Activity activity)32   public static AnswerMethod createAnswerMethod(@NonNull Activity activity) {
33     if (needTwoButton(activity)) {
34       return new TwoButtonMethod();
35     } else {
36       return new FlingUpDownMethod();
37     }
38   }
39 
needsReplacement(@ullable Fragment answerMethod)40   public static boolean needsReplacement(@Nullable Fragment answerMethod) {
41     //noinspection SimplifiableIfStatement
42     if (answerMethod == null) {
43       return true;
44     }
45     // If we have already started showing TwoButtonMethod, we should keep showing TwoButtonMethod.
46     // Otherwise check if we need to change to TwoButtonMethod
47     return !(answerMethod instanceof TwoButtonMethod) && needTwoButton(answerMethod.getActivity());
48   }
49 
50   @VisibleForTesting
setShouldUseTwoButtonMethodForTesting(boolean shouldUse)51   public static void setShouldUseTwoButtonMethodForTesting(boolean shouldUse) {
52     shouldUseTwoButtonMethodForTesting = shouldUse;
53   }
54 
needTwoButton(@onNull Activity activity)55   private static boolean needTwoButton(@NonNull Activity activity) {
56     if (shouldUseTwoButtonMethodForTesting) {
57       LogUtil.i("AnswerMethodFactory.needTwoButton", "enabled for testing");
58       return true;
59     }
60 
61     return AccessibilityUtil.isTouchExplorationEnabled(activity) || activity.isInMultiWindowMode();
62   }
63 }
64