1 /*
2  * Copyright (C) 2018 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.dialer.oem;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import android.support.annotation.VisibleForTesting;
22 import com.android.dialer.common.Assert;
23 import com.android.dialer.compat.telephony.TelephonyManagerCompat;
24 import com.google.common.collect.ImmutableSet;
25 
26 /** Utilities for Transsion devices. */
27 public final class TranssionUtils {
28 
29   @VisibleForTesting
30   public static final ImmutableSet<String> TRANSSION_DEVICE_MANUFACTURERS =
31       ImmutableSet.of("INFINIX MOBILITY LIMITED", "itel", "TECNO");
32 
33   @VisibleForTesting
34   public static final ImmutableSet<String> TRANSSION_SECRET_CODES =
35       ImmutableSet.of("*#07#", "*#87#", "*#43#", "*#2727#", "*#88#");
36 
TranssionUtils()37   private TranssionUtils() {}
38 
39   /**
40    * Returns true if
41    *
42    * <ul>
43    *   <li>the device is a Transsion device, AND
44    *   <li>the input is a secret code for Transsion devices.
45    * </ul>
46    */
isTranssionSecretCode(String input)47   public static boolean isTranssionSecretCode(String input) {
48     return TRANSSION_DEVICE_MANUFACTURERS.contains(Build.MANUFACTURER)
49         && TRANSSION_SECRET_CODES.contains(input);
50   }
51 
52   /**
53    * Handle a Transsion secret code by passing it to {@link
54    * TelephonyManagerCompat#handleSecretCode(Context, String)}.
55    *
56    * <p>Before calling this method, we must use {@link #isTranssionSecretCode(String)} to ensure the
57    * device is a Transsion device and the input is a valid Transsion secret code.
58    *
59    * <p>An exception will be thrown if either of the conditions above is not met.
60    */
handleTranssionSecretCode(Context context, String input)61   public static void handleTranssionSecretCode(Context context, String input) {
62     Assert.checkState(isTranssionSecretCode(input));
63 
64     TelephonyManagerCompat.handleSecretCode(context, getDigitsFromSecretCode(input));
65   }
66 
getDigitsFromSecretCode(String input)67   private static String getDigitsFromSecretCode(String input) {
68     // We assume a valid secret code is of format "*#{[0-9]+}#".
69     Assert.checkArgument(input.length() > 3 && input.startsWith("*#") && input.endsWith("#"));
70 
71     return input.substring(2, input.length() - 1);
72   }
73 }
74