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.voicemail.impl;
18 
19 import android.annotation.TargetApi;
20 import android.content.Context;
21 import android.net.Network;
22 import android.os.Build.VERSION_CODES;
23 import android.support.annotation.WorkerThread;
24 import android.telecom.PhoneAccountHandle;
25 import com.android.dialer.common.Assert;
26 import com.android.voicemail.PinChanger;
27 import com.android.voicemail.impl.imap.ImapHelper;
28 import com.android.voicemail.impl.imap.ImapHelper.InitializingException;
29 import com.android.voicemail.impl.mail.MessagingException;
30 import com.android.voicemail.impl.sync.VvmNetworkRequest;
31 import com.android.voicemail.impl.sync.VvmNetworkRequest.NetworkWrapper;
32 import com.android.voicemail.impl.sync.VvmNetworkRequest.RequestFailedException;
33 
34 @TargetApi(VERSION_CODES.O)
35 class PinChangerImpl implements PinChanger {
36 
37   private final Context context;
38   private final PhoneAccountHandle phoneAccountHandle;
39 
40   private static final String KEY_SCRAMBLED_PIN = "default_old_pin"; // legacy name, DO NOT CHANGE
41 
PinChangerImpl(Context context, PhoneAccountHandle phoneAccountHandle)42   PinChangerImpl(Context context, PhoneAccountHandle phoneAccountHandle) {
43     this.context = context;
44     this.phoneAccountHandle = phoneAccountHandle;
45   }
46 
47   @WorkerThread
48   @Override
49   @ChangePinResult
changePin(String oldPin, String newPin)50   public int changePin(String oldPin, String newPin) {
51     Assert.isWorkerThread();
52     OmtpVvmCarrierConfigHelper config = new OmtpVvmCarrierConfigHelper(context, phoneAccountHandle);
53     VoicemailStatus.Editor status = VoicemailStatus.edit(context, phoneAccountHandle);
54     try (NetworkWrapper networkWrapper =
55         VvmNetworkRequest.getNetwork(config, phoneAccountHandle, status)) {
56       Network network = networkWrapper.get();
57       try (ImapHelper helper = new ImapHelper(context, phoneAccountHandle, network, status)) {
58         return helper.changePin(oldPin, newPin);
59       } catch (InitializingException | MessagingException e) {
60         VvmLog.e(
61             "VoicemailClientImpl.changePin", "ChangePinNetworkRequestCallback: onAvailable: " + e);
62         return PinChanger.CHANGE_PIN_SYSTEM_ERROR;
63       }
64 
65     } catch (RequestFailedException e) {
66       return PinChanger.CHANGE_PIN_SYSTEM_ERROR;
67     }
68   }
69 
70   @Override
setScrambledPin(String pin)71   public void setScrambledPin(String pin) {
72     new VisualVoicemailPreferences(context, phoneAccountHandle)
73         .edit()
74         .putString(KEY_SCRAMBLED_PIN, pin)
75         .apply();
76     if (pin == null) {
77       new OmtpVvmCarrierConfigHelper(context, phoneAccountHandle)
78           .handleEvent(
79               VoicemailStatus.edit(context, phoneAccountHandle), OmtpEvents.CONFIG_PIN_SET);
80     }
81   }
82 
83   @Override
getScrambledPin()84   public String getScrambledPin() {
85     return new VisualVoicemailPreferences(context, phoneAccountHandle).getString(KEY_SCRAMBLED_PIN);
86   }
87 
88   @Override
getPinSpecification()89   public PinSpecification getPinSpecification() {
90     PinSpecification result = new PinSpecification();
91     VisualVoicemailPreferences preferences =
92         new VisualVoicemailPreferences(context, phoneAccountHandle);
93     // The OMTP pin length format is {min}-{max}
94     String[] lengths = preferences.getString(OmtpConstants.TUI_PASSWORD_LENGTH, "").split("-");
95     if (lengths.length == 2) {
96       try {
97         result.minLength = Integer.parseInt(lengths[0]);
98         result.maxLength = Integer.parseInt(lengths[1]);
99       } catch (NumberFormatException e) {
100         // do nothing, return default value;
101       }
102     }
103     return result;
104   }
105 }
106