Home | History | Annotate | Download | only in settings
      1 /**
      2  * Copyright (C) 2014 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.phone.settings;
     18 
     19 import android.telephony.PhoneNumberUtils;
     20 
     21 import com.android.internal.telephony.CallForwardInfo;
     22 import com.android.internal.telephony.CommandsInterface;
     23 
     24 /**
     25  * Settings for a voicemail provider, including any conditional forwarding information.
     26  */
     27 public class VoicemailProviderSettings {
     28 
     29     // If no forwarding is set, leave the forwarding number unchanged from its current value.
     30     public static final CallForwardInfo[] NO_FORWARDING = null;
     31 
     32     /**
     33      * Reasons for the forwarding settings we are going to save.
     34      */
     35     public static final int [] FORWARDING_SETTINGS_REASONS = new int[] {
     36         CommandsInterface.CF_REASON_UNCONDITIONAL,
     37         CommandsInterface.CF_REASON_BUSY,
     38         CommandsInterface.CF_REASON_NO_REPLY,
     39         CommandsInterface.CF_REASON_NOT_REACHABLE
     40     };
     41 
     42     private String mVoicemailNumber;
     43     private CallForwardInfo[] mForwardingSettings;
     44 
     45     /**
     46      * Constructs settings object, setting all conditional forwarding to the specified number
     47      */
     48     public VoicemailProviderSettings(
     49             String voicemailNumber, String forwardingNumber, int timeSeconds) {
     50         mVoicemailNumber = voicemailNumber;
     51         if (forwardingNumber == null || forwardingNumber.length() == 0) {
     52             mForwardingSettings = NO_FORWARDING;
     53         } else {
     54             mForwardingSettings = new CallForwardInfo[FORWARDING_SETTINGS_REASONS.length];
     55             for (int i = 0; i < mForwardingSettings.length; i++) {
     56                 CallForwardInfo fi = new CallForwardInfo();
     57                 mForwardingSettings[i] = fi;
     58                 fi.reason = FORWARDING_SETTINGS_REASONS[i];
     59                 fi.status = (fi.reason == CommandsInterface.CF_REASON_UNCONDITIONAL) ? 0 : 1;
     60                 fi.serviceClass = CommandsInterface.SERVICE_CLASS_VOICE;
     61                 fi.toa = PhoneNumberUtils.TOA_International;
     62                 fi.number = forwardingNumber;
     63                 fi.timeSeconds = timeSeconds;
     64             }
     65         }
     66     }
     67 
     68     public VoicemailProviderSettings(String voicemailNumber, CallForwardInfo[] infos) {
     69         mVoicemailNumber = voicemailNumber;
     70         mForwardingSettings = infos;
     71     }
     72 
     73     @Override
     74     public boolean equals(Object o) {
     75         if (o == null) {
     76             return false;
     77         }
     78         if (!(o instanceof VoicemailProviderSettings)) {
     79             return false;
     80         }
     81 
     82         final VoicemailProviderSettings v = (VoicemailProviderSettings) o;
     83         return ((mVoicemailNumber == null && v.getVoicemailNumber() == null)
     84                 || (mVoicemailNumber != null && mVoicemailNumber.equals(v.getVoicemailNumber()))
     85                         && forwardingSettingsEqual(mForwardingSettings, v.getForwardingSettings()));
     86     }
     87 
     88     @Override
     89     public String toString() {
     90         return mVoicemailNumber + ((mForwardingSettings == null) ? ""
     91                 : ", " + mForwardingSettings.toString());
     92     }
     93 
     94     public String getVoicemailNumber() {
     95         return mVoicemailNumber;
     96     }
     97 
     98     public CallForwardInfo[] getForwardingSettings() {
     99         return mForwardingSettings;
    100     }
    101 
    102     private boolean forwardingSettingsEqual(CallForwardInfo[] infos1, CallForwardInfo[] infos2) {
    103         if (infos1 == infos2) {
    104             return true;
    105         }
    106         if (infos1 == null || infos2 == null) {
    107             return false;
    108         }
    109         if (infos1.length != infos2.length) {
    110             return false;
    111         }
    112 
    113         for (int i = 0; i < infos1.length; i++) {
    114             CallForwardInfo i1 = infos1[i];
    115             CallForwardInfo i2 = infos2[i];
    116             if (i1.status != i2.status
    117                     || i1.reason != i2.reason
    118                     || i1.serviceClass != i2.serviceClass
    119                     || i1.toa != i2.toa
    120                     || i1.number != i2.number
    121                     || i1.timeSeconds != i2.timeSeconds) {
    122                 return false;
    123             }
    124         }
    125         return true;
    126     }
    127 }
    128