Home | History | Annotate | Download | only in settings
      1 /**
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
      5  * except in compliance with the License. You may obtain a copy of the License at
      6  *
      7  * <p>http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * <p>Unless required by applicable law or agreed to in writing, software distributed under the
     10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     11  * express or implied. See the License for the specific language governing permissions and
     12  * limitations under the License
     13  */
     14 package com.android.voicemail.impl.settings;
     15 
     16 import android.annotation.TargetApi;
     17 import android.content.Intent;
     18 import android.os.Build.VERSION_CODES;
     19 import android.os.Bundle;
     20 import android.preference.Preference;
     21 import android.preference.Preference.OnPreferenceClickListener;
     22 import android.preference.PreferenceFragment;
     23 import android.preference.PreferenceScreen;
     24 import android.preference.SwitchPreference;
     25 import android.provider.Settings;
     26 import android.support.annotation.Nullable;
     27 import android.telecom.PhoneAccountHandle;
     28 import android.telephony.TelephonyManager;
     29 import com.android.dialer.common.Assert;
     30 import com.android.dialer.logging.DialerImpression;
     31 import com.android.dialer.logging.Logger;
     32 import com.android.dialer.notification.NotificationChannelManager;
     33 import com.android.voicemail.VoicemailClient;
     34 import com.android.voicemail.VoicemailComponent;
     35 import com.android.voicemail.impl.OmtpVvmCarrierConfigHelper;
     36 import com.android.voicemail.impl.R;
     37 import com.android.voicemail.impl.VvmLog;
     38 import com.android.voicemail.impl.sync.VvmAccountManager;
     39 
     40 /**
     41  * Fragment for voicemail settings. Requires {@link VoicemailClient#PARAM_PHONE_ACCOUNT_HANDLE} set
     42  * in arguments.
     43  */
     44 @TargetApi(VERSION_CODES.O)
     45 public class VoicemailSettingsFragment extends PreferenceFragment
     46     implements Preference.OnPreferenceChangeListener, VvmAccountManager.Listener {
     47 
     48   private static final String TAG = "VmSettingsActivity";
     49 
     50   @Nullable private PhoneAccountHandle phoneAccountHandle;
     51   private OmtpVvmCarrierConfigHelper omtpVvmCarrierConfigHelper;
     52 
     53   private Preference voicemailNotificationPreference;
     54   private SwitchPreference voicemailVisualVoicemail;
     55   private SwitchPreference autoArchiveSwitchPreference;
     56   private Preference voicemailChangePinPreference;
     57   private PreferenceScreen advancedSettings;
     58 
     59   @Override
     60   public void onCreate(Bundle icicle) {
     61     super.onCreate(icicle);
     62 
     63     phoneAccountHandle =
     64         Assert.isNotNull(getArguments().getParcelable(VoicemailClient.PARAM_PHONE_ACCOUNT_HANDLE));
     65 
     66     omtpVvmCarrierConfigHelper = new OmtpVvmCarrierConfigHelper(getContext(), phoneAccountHandle);
     67   }
     68 
     69   @Override
     70   public void onResume() {
     71     super.onResume();
     72     Logger.get(getContext()).logImpression(DialerImpression.Type.VVM_SETTINGS_VIEWED);
     73     VvmAccountManager.addListener(this);
     74     PreferenceScreen preferenceScreen = getPreferenceScreen();
     75     if (preferenceScreen != null) {
     76       preferenceScreen.removeAll();
     77     }
     78 
     79     addPreferencesFromResource(R.xml.voicemail_settings);
     80 
     81     PreferenceScreen prefSet = getPreferenceScreen();
     82 
     83     voicemailNotificationPreference =
     84         findPreference(getString(R.string.voicemail_notifications_key));
     85     voicemailNotificationPreference.setIntent(getNotificationSettingsIntent());
     86 
     87     voicemailNotificationPreference.setOnPreferenceClickListener(
     88         new OnPreferenceClickListener() {
     89           @Override
     90           public boolean onPreferenceClick(Preference preference) {
     91             Logger.get(getContext())
     92                 .logImpression(DialerImpression.Type.VVM_CHANGE_RINGTONE_CLICKED);
     93             // Let the preference handle the click.
     94             return false;
     95           }
     96         });
     97 
     98     voicemailVisualVoicemail =
     99         (SwitchPreference) findPreference(getString(R.string.voicemail_visual_voicemail_key));
    100 
    101     autoArchiveSwitchPreference =
    102         (SwitchPreference)
    103             findPreference(getString(R.string.voicemail_visual_voicemail_archive_key));
    104 
    105     if (!VoicemailComponent.get(getContext())
    106         .getVoicemailClient()
    107         .isVoicemailArchiveAvailable(getContext())) {
    108       getPreferenceScreen().removePreference(autoArchiveSwitchPreference);
    109     }
    110 
    111     voicemailChangePinPreference = findPreference(getString(R.string.voicemail_change_pin_key));
    112 
    113     if (omtpVvmCarrierConfigHelper.isValid()) {
    114       Assert.isNotNull(phoneAccountHandle);
    115       Intent changePinIntent =
    116           new Intent(new Intent(getContext(), VoicemailChangePinActivity.class));
    117       changePinIntent.putExtra(
    118           VoicemailChangePinActivity.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
    119 
    120       voicemailChangePinPreference.setIntent(changePinIntent);
    121       voicemailChangePinPreference.setOnPreferenceClickListener(
    122           new OnPreferenceClickListener() {
    123             @Override
    124             public boolean onPreferenceClick(Preference preference) {
    125               Logger.get(getContext()).logImpression(DialerImpression.Type.VVM_CHANGE_PIN_CLICKED);
    126               // Let the preference handle the click.
    127               return false;
    128             }
    129           });
    130       if (VoicemailChangePinActivity.isDefaultOldPinSet(getContext(), phoneAccountHandle)) {
    131         voicemailChangePinPreference.setTitle(R.string.voicemail_set_pin_preference_title);
    132       } else {
    133         voicemailChangePinPreference.setTitle(R.string.voicemail_change_pin_preference_title);
    134       }
    135       updateChangePin();
    136 
    137       voicemailVisualVoicemail.setOnPreferenceChangeListener(this);
    138       voicemailVisualVoicemail.setChecked(
    139           VisualVoicemailSettingsUtil.isEnabled(getContext(), phoneAccountHandle));
    140 
    141       autoArchiveSwitchPreference.setOnPreferenceChangeListener(this);
    142       autoArchiveSwitchPreference.setChecked(
    143           VisualVoicemailSettingsUtil.isArchiveEnabled(getContext(), phoneAccountHandle));
    144     } else {
    145       prefSet.removePreference(voicemailVisualVoicemail);
    146       prefSet.removePreference(autoArchiveSwitchPreference);
    147       prefSet.removePreference(voicemailChangePinPreference);
    148     }
    149 
    150     advancedSettings =
    151         (PreferenceScreen) findPreference(getString(R.string.voicemail_advanced_settings_key));
    152     Intent advancedSettingsIntent = new Intent(TelephonyManager.ACTION_CONFIGURE_VOICEMAIL);
    153     advancedSettingsIntent.putExtra(TelephonyManager.EXTRA_HIDE_PUBLIC_SETTINGS, true);
    154     advancedSettings.setIntent(advancedSettingsIntent);
    155     voicemailChangePinPreference.setOnPreferenceClickListener(
    156         new OnPreferenceClickListener() {
    157           @Override
    158           public boolean onPreferenceClick(Preference preference) {
    159             Logger.get(getContext())
    160                 .logImpression(DialerImpression.Type.VVM_ADVANCED_SETINGS_CLICKED);
    161             // Let the preference handle the click.
    162             return false;
    163           }
    164         });
    165   }
    166 
    167   @Override
    168   public void onPause() {
    169     VvmAccountManager.removeListener(this);
    170     super.onPause();
    171   }
    172 
    173   /**
    174    * Implemented to support onPreferenceChangeListener to look for preference changes.
    175    *
    176    * @param preference is the preference to be changed
    177    * @param objValue should be the value of the selection, NOT its localized display value.
    178    */
    179   @Override
    180   public boolean onPreferenceChange(Preference preference, Object objValue) {
    181     VvmLog.d(TAG, "onPreferenceChange: \"" + preference + "\" changed to \"" + objValue + "\"");
    182     if (preference.getKey().equals(voicemailVisualVoicemail.getKey())) {
    183       boolean isEnabled = (boolean) objValue;
    184       VisualVoicemailSettingsUtil.setEnabled(getContext(), phoneAccountHandle, isEnabled);
    185 
    186       if (isEnabled) {
    187         Logger.get(getContext()).logImpression(DialerImpression.Type.VVM_USER_ENABLED_IN_SETTINGS);
    188       } else {
    189         Logger.get(getContext()).logImpression(DialerImpression.Type.VVM_USER_DISABLED_IN_SETTINGS);
    190       }
    191 
    192       updateChangePin();
    193     } else if (preference.getKey().equals(autoArchiveSwitchPreference.getKey())) {
    194       logArchiveToggle((boolean) objValue);
    195       VisualVoicemailSettingsUtil.setArchiveEnabled(
    196           getContext(), phoneAccountHandle, (boolean) objValue);
    197     }
    198 
    199     // Always let the preference setting proceed.
    200     return true;
    201   }
    202 
    203   private void updateChangePin() {
    204     if (!VisualVoicemailSettingsUtil.isEnabled(getContext(), phoneAccountHandle)) {
    205       voicemailChangePinPreference.setSummary(
    206           R.string.voicemail_change_pin_preference_summary_disable);
    207       voicemailChangePinPreference.setEnabled(false);
    208     } else if (!VvmAccountManager.isAccountActivated(getContext(), phoneAccountHandle)) {
    209       voicemailChangePinPreference.setSummary(
    210           R.string.voicemail_change_pin_preference_summary_not_activated);
    211       voicemailChangePinPreference.setEnabled(false);
    212     } else {
    213       voicemailChangePinPreference.setSummary(null);
    214       voicemailChangePinPreference.setEnabled(true);
    215     }
    216   }
    217 
    218   private void logArchiveToggle(boolean userTurnedOn) {
    219     if (userTurnedOn) {
    220       Logger.get(getContext())
    221           .logImpression(DialerImpression.Type.VVM_USER_TURNED_ARCHIVE_ON_FROM_SETTINGS);
    222     } else {
    223       Logger.get(getContext())
    224           .logImpression(DialerImpression.Type.VVM_USER_TURNED_ARCHIVE_OFF_FROM_SETTINGS);
    225     }
    226   }
    227 
    228   @Override
    229   public void onActivationStateChanged(PhoneAccountHandle phoneAccountHandle, boolean isActivated) {
    230     if (this.phoneAccountHandle.equals(phoneAccountHandle)) {
    231       updateChangePin();
    232     }
    233   }
    234 
    235   private Intent getNotificationSettingsIntent() {
    236     String channelId =
    237         NotificationChannelManager.getVoicemailChannelId(getContext(), phoneAccountHandle);
    238     return new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
    239         .putExtra(Settings.EXTRA_CHANNEL_ID, channelId)
    240         .putExtra(Settings.EXTRA_APP_PACKAGE, getContext().getPackageName());
    241   }
    242 }
    243