Home | History | Annotate | Download | only in hint
      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.preference.PreferenceManager;
     24 import android.support.annotation.VisibleForTesting;
     25 import android.text.TextUtils;
     26 import android.widget.Toast;
     27 import com.android.dialer.common.Assert;
     28 import com.android.dialer.common.ConfigProviderBindings;
     29 import com.android.dialer.common.LogUtil;
     30 import com.android.dialer.logging.DialerImpression.Type;
     31 import com.android.dialer.logging.Logger;
     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   public static final String PAW_DRAWABLE_ID_KEY = "paw_drawable_id";
     44 
     45   @Override
     46   public void onReceive(Context context, Intent intent) {
     47     String host = intent.getData().getHost();
     48     Assert.checkState(!TextUtils.isEmpty(host));
     49     String secretCode =
     50         ConfigProviderBindings.get(context).getString(CONFIG_PAW_SECRET_CODE, "729");
     51     if (secretCode == null) {
     52       return;
     53     }
     54     if (!TextUtils.equals(secretCode, host)) {
     55       return;
     56     }
     57     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
     58     boolean wasEnabled = preferences.getBoolean(PAW_ENABLED_WITH_SECRET_CODE_KEY, false);
     59     if (wasEnabled) {
     60       preferences.edit().putBoolean(PAW_ENABLED_WITH_SECRET_CODE_KEY, false).apply();
     61       Toast.makeText(context, R.string.event_deactivated, Toast.LENGTH_SHORT).show();
     62       Logger.get(context).logImpression(Type.EVENT_ANSWER_HINT_DEACTIVATED);
     63       LogUtil.i("PawSecretCodeListener.onReceive", "PawAnswerHint disabled");
     64     } else {
     65       int drawableId;
     66       if (new Random().nextBoolean()) {
     67         drawableId = R.drawable.cat_paw;
     68       } else {
     69         drawableId = R.drawable.dog_paw;
     70       }
     71       preferences
     72           .edit()
     73           .putBoolean(PAW_ENABLED_WITH_SECRET_CODE_KEY, true)
     74           .putInt(PAW_DRAWABLE_ID_KEY, drawableId)
     75           .apply();
     76       Toast.makeText(context, R.string.event_activated, Toast.LENGTH_SHORT).show();
     77       Logger.get(context).logImpression(Type.EVENT_ANSWER_HINT_ACTIVATED);
     78       LogUtil.i("PawSecretCodeListener.onReceive", "PawAnswerHint enabled");
     79     }
     80   }
     81 }
     82