Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2015 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 package com.android.voicemail.impl.settings;
     17 
     18 import android.content.Context;
     19 import android.support.annotation.VisibleForTesting;
     20 import android.telecom.PhoneAccountHandle;
     21 import com.android.dialer.common.Assert;
     22 import com.android.dialer.common.concurrent.DialerExecutor.Worker;
     23 import com.android.dialer.common.concurrent.DialerExecutorComponent;
     24 import com.android.voicemail.VoicemailComponent;
     25 import com.android.voicemail.impl.OmtpVvmCarrierConfigHelper;
     26 import com.android.voicemail.impl.VisualVoicemailPreferences;
     27 import com.android.voicemail.impl.VvmLog;
     28 import com.android.voicemail.impl.sync.VvmAccountManager;
     29 import com.android.voicemail.impl.utils.VoicemailDatabaseUtil;
     30 
     31 /** Save whether or not a particular account is enabled in shared to be retrieved later. */
     32 public class VisualVoicemailSettingsUtil {
     33 
     34   @VisibleForTesting public static final String IS_ENABLED_KEY = "is_enabled";
     35   private static final String ARCHIVE_ENABLED_KEY = "archive_is_enabled";
     36   private static final String DONATE_VOICEMAILS_KEY = "donate_voicemails";
     37 
     38   public static void setEnabled(
     39       Context context, PhoneAccountHandle phoneAccount, boolean isEnabled) {
     40     VvmLog.i("VisualVoicemailSettingsUtil.setEnable", phoneAccount + " enabled:" + isEnabled);
     41     new VisualVoicemailPreferences(context, phoneAccount)
     42         .edit()
     43         .putBoolean(IS_ENABLED_KEY, isEnabled)
     44         .apply();
     45     OmtpVvmCarrierConfigHelper config = new OmtpVvmCarrierConfigHelper(context, phoneAccount);
     46     if (isEnabled) {
     47       config.startActivation();
     48     } else {
     49       VvmAccountManager.removeAccount(context, phoneAccount);
     50       config.startDeactivation();
     51       // Remove all voicemails from the database
     52       DialerExecutorComponent.get(context)
     53           .dialerExecutorFactory()
     54           .createNonUiTaskBuilder(new VoicemailDeleteWorker(context))
     55           .onSuccess(VisualVoicemailSettingsUtil::onSuccess)
     56           .onFailure(VisualVoicemailSettingsUtil::onFailure)
     57           .build()
     58           .executeParallel(null);
     59     }
     60   }
     61 
     62   private static class VoicemailDeleteWorker implements Worker<Void, Void> {
     63     private final Context context;
     64 
     65     VoicemailDeleteWorker(Context context) {
     66       this.context = context;
     67     }
     68 
     69     @Override
     70     public Void doInBackground(Void unused) {
     71       int deleted = VoicemailDatabaseUtil.deleteAll(context);
     72       VvmLog.i("VisualVoicemailSettingsUtil.doInBackground", "deleted " + deleted + " voicemails");
     73       return null;
     74     }
     75   }
     76 
     77   private static void onSuccess(Void unused) {
     78     VvmLog.i("VisualVoicemailSettingsUtil.onSuccess", "delete voicemails");
     79   }
     80 
     81   private static void onFailure(Throwable t) {
     82     VvmLog.e("VisualVoicemailSettingsUtil.onFailure", "delete voicemails", t);
     83   }
     84 
     85   public static void setArchiveEnabled(
     86       Context context, PhoneAccountHandle phoneAccount, boolean isEnabled) {
     87     Assert.checkArgument(
     88         VoicemailComponent.get(context).getVoicemailClient().isVoicemailArchiveAvailable(context));
     89     new VisualVoicemailPreferences(context, phoneAccount)
     90         .edit()
     91         .putBoolean(ARCHIVE_ENABLED_KEY, isEnabled)
     92         .apply();
     93   }
     94 
     95   public static void setVoicemailDonationEnabled(
     96       Context context, PhoneAccountHandle phoneAccount, boolean isEnabled) {
     97     Assert.checkArgument(
     98         VoicemailComponent.get(context)
     99             .getVoicemailClient()
    100             .isVoicemailTranscriptionAvailable(context));
    101     new VisualVoicemailPreferences(context, phoneAccount)
    102         .edit()
    103         .putBoolean(DONATE_VOICEMAILS_KEY, isEnabled)
    104         .apply();
    105   }
    106 
    107   public static boolean isEnabled(Context context, PhoneAccountHandle phoneAccount) {
    108     if (phoneAccount == null) {
    109       return false;
    110     }
    111 
    112     VisualVoicemailPreferences prefs = new VisualVoicemailPreferences(context, phoneAccount);
    113     if (prefs.contains(IS_ENABLED_KEY)) {
    114       // isEnableByDefault is a bit expensive, so don't use it as default value of
    115       // getBoolean(). The "false" here should never be actually used.
    116       return prefs.getBoolean(IS_ENABLED_KEY, false);
    117     }
    118     return new OmtpVvmCarrierConfigHelper(context, phoneAccount).isEnabledByDefault();
    119   }
    120 
    121   public static boolean isArchiveEnabled(Context context, PhoneAccountHandle phoneAccount) {
    122     Assert.isNotNull(phoneAccount);
    123 
    124     VisualVoicemailPreferences prefs = new VisualVoicemailPreferences(context, phoneAccount);
    125     return prefs.getBoolean(ARCHIVE_ENABLED_KEY, false);
    126   }
    127 
    128   public static boolean isVoicemailDonationEnabled(
    129       Context context, PhoneAccountHandle phoneAccount) {
    130     Assert.isNotNull(phoneAccount);
    131 
    132     VisualVoicemailPreferences prefs = new VisualVoicemailPreferences(context, phoneAccount);
    133     return prefs.getBoolean(DONATE_VOICEMAILS_KEY, false);
    134   }
    135 
    136   /**
    137    * Whether the client enabled status is explicitly set by user or by default(Whether carrier VVM
    138    * app is installed). This is used to determine whether to disable the client when the carrier VVM
    139    * app is installed. If the carrier VVM app is installed the client should give priority to it if
    140    * the settings are not touched.
    141    */
    142   public static boolean isEnabledUserSet(Context context, PhoneAccountHandle phoneAccount) {
    143     if (phoneAccount == null) {
    144       return false;
    145     }
    146     VisualVoicemailPreferences prefs = new VisualVoicemailPreferences(context, phoneAccount);
    147     return prefs.contains(IS_ENABLED_KEY);
    148   }
    149 }
    150