1 /* 2 * Copyright (C) 2017 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.hint; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.SharedPreferences; 23 import android.support.annotation.IntDef; 24 import android.support.annotation.VisibleForTesting; 25 import android.text.TextUtils; 26 import android.widget.Toast; 27 import com.android.dialer.common.LogUtil; 28 import com.android.dialer.configprovider.ConfigProviderComponent; 29 import com.android.dialer.logging.DialerImpression.Type; 30 import com.android.dialer.logging.Logger; 31 import com.android.dialer.storage.StorageComponent; 32 import java.util.Random; 33 34 /** 35 * Listen to the broadcast when the user dials "*#*#[number]#*#*" to toggle the event answer hint. 36 */ 37 public class PawSecretCodeListener extends BroadcastReceiver { 38 39 @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) 40 static final String CONFIG_PAW_SECRET_CODE = "paw_secret_code"; 41 42 public static final String PAW_ENABLED_WITH_SECRET_CODE_KEY = "paw_enabled_with_secret_code"; 43 44 /** Which paw to show, must be {@link PawType} */ 45 public static final String PAW_TYPE = "paw_type"; 46 47 /** Resource id is not stable across app versions. Use {@link #PAW_TYPE} instead. */ 48 @Deprecated public static final String PAW_DRAWABLE_ID_KEY = "paw_drawable_id"; 49 50 /** Enum for all paws. */ 51 @IntDef({PAW_TYPE_INVALID, PAW_TYPE_CAT, PAW_TYPE_DOG}) 52 @interface PawType {} 53 54 public static final int PAW_TYPE_INVALID = 0; 55 public static final int PAW_TYPE_CAT = 1; 56 public static final int PAW_TYPE_DOG = 2; 57 58 @Override onReceive(Context context, Intent intent)59 public void onReceive(Context context, Intent intent) { 60 String host = intent.getData().getHost(); 61 if (TextUtils.isEmpty(host)) { 62 return; 63 } 64 String secretCode = 65 ConfigProviderComponent.get(context) 66 .getConfigProvider() 67 .getString(CONFIG_PAW_SECRET_CODE, "729"); 68 if (secretCode == null) { 69 return; 70 } 71 if (!TextUtils.equals(secretCode, host)) { 72 return; 73 } 74 SharedPreferences preferences = StorageComponent.get(context).unencryptedSharedPrefs(); 75 boolean wasEnabled = preferences.getBoolean(PAW_ENABLED_WITH_SECRET_CODE_KEY, false); 76 if (wasEnabled) { 77 preferences.edit().putBoolean(PAW_ENABLED_WITH_SECRET_CODE_KEY, false).apply(); 78 Toast.makeText(context, R.string.event_deactivated, Toast.LENGTH_SHORT).show(); 79 Logger.get(context).logImpression(Type.EVENT_ANSWER_HINT_DEACTIVATED); 80 LogUtil.i("PawSecretCodeListener.onReceive", "PawAnswerHint disabled"); 81 } else { 82 selectPawType(preferences); 83 Toast.makeText(context, R.string.event_activated, Toast.LENGTH_SHORT).show(); 84 Logger.get(context).logImpression(Type.EVENT_ANSWER_HINT_ACTIVATED); 85 LogUtil.i("PawSecretCodeListener.onReceive", "PawAnswerHint enabled"); 86 } 87 } 88 selectPawType(SharedPreferences preferences)89 public static void selectPawType(SharedPreferences preferences) { 90 @PawType int pawType; 91 if (new Random().nextBoolean()) { 92 pawType = PAW_TYPE_CAT; 93 } else { 94 pawType = PAW_TYPE_DOG; 95 } 96 preferences 97 .edit() 98 .putBoolean(PAW_ENABLED_WITH_SECRET_CODE_KEY, true) 99 .putInt(PAW_TYPE, pawType) 100 .apply(); 101 } 102 } 103