• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.dialer.compat.ActivityCompat;
26 import com.android.incallui.util.AccessibilityUtil;
27 
28 /** Creates the appropriate {@link AnswerMethod} for the circumstances. */
29 public class AnswerMethodFactory {
30   private static boolean shouldUseTwoButtonMethodForTesting;
31 
32   @NonNull
createAnswerMethod(@onNull Activity activity)33   public static AnswerMethod createAnswerMethod(@NonNull Activity activity) {
34     if (needTwoButton(activity)) {
35       return new TwoButtonMethod();
36     } else {
37       return new FlingUpDownMethod();
38     }
39   }
40 
needsReplacement(@ullable Fragment answerMethod)41   public static boolean needsReplacement(@Nullable Fragment answerMethod) {
42     //noinspection SimplifiableIfStatement
43     if (answerMethod == null) {
44       return true;
45     }
46     // If we have already started showing TwoButtonMethod, we should keep showing TwoButtonMethod.
47     // Otherwise check if we need to change to TwoButtonMethod
48     return !(answerMethod instanceof TwoButtonMethod) && needTwoButton(answerMethod.getActivity());
49   }
50 
51   @VisibleForTesting
setShouldUseTwoButtonMethodForTesting(boolean shouldUse)52   public static void setShouldUseTwoButtonMethodForTesting(boolean shouldUse) {
53     shouldUseTwoButtonMethodForTesting = shouldUse;
54   }
55 
needTwoButton(@onNull Activity activity)56   private static boolean needTwoButton(@NonNull Activity activity) {
57     if (shouldUseTwoButtonMethodForTesting) {
58       LogUtil.i("AnswerMethodFactory.needTwoButton", "enabled for testing");
59       return true;
60     }
61 
62     return AccessibilityUtil.isTouchExplorationEnabled(activity)
63         || ActivityCompat.isInMultiWindowMode(activity);
64   }
65 }
66