1 /*
2  * Copyright (C) 2016 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.content.Context;
20 import android.provider.VoicemailContract;
21 import android.provider.VoicemailContract.Status;
22 import com.android.voicemail.impl.OmtpEvents.Type;
23 
24 public class DefaultOmtpEventHandler {
25 
26   private static final String TAG = "DefErrorCodeHandler";
27 
handleEvent( Context context, OmtpVvmCarrierConfigHelper config, VoicemailStatus.Editor status, OmtpEvents event)28   public static void handleEvent(
29       Context context,
30       OmtpVvmCarrierConfigHelper config,
31       VoicemailStatus.Editor status,
32       OmtpEvents event) {
33     switch (event.getType()) {
34       case Type.CONFIGURATION:
35         handleConfigurationEvent(context, status, event);
36         break;
37       case Type.DATA_CHANNEL:
38         handleDataChannelEvent(context, status, event);
39         break;
40       case Type.NOTIFICATION_CHANNEL:
41         handleNotificationChannelEvent(context, config, status, event);
42         break;
43       case Type.OTHER:
44         handleOtherEvent(context, status, event);
45         break;
46       default:
47         VvmLog.wtf(TAG, "invalid event type " + event.getType() + " for " + event);
48     }
49   }
50 
handleConfigurationEvent( Context context, VoicemailStatus.Editor status, OmtpEvents event)51   private static void handleConfigurationEvent(
52       Context context, VoicemailStatus.Editor status, OmtpEvents event) {
53     switch (event) {
54       case CONFIG_DEFAULT_PIN_REPLACED:
55       case CONFIG_REQUEST_STATUS_SUCCESS:
56       case CONFIG_PIN_SET:
57         status
58             .setConfigurationState(VoicemailContract.Status.CONFIGURATION_STATE_OK)
59             .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_OK)
60             .apply();
61         break;
62       case CONFIG_ACTIVATING:
63         // Wipe all errors from the last activation. All errors shown should be new errors
64         // for this activation.
65         status
66             .setConfigurationState(Status.CONFIGURATION_STATE_CONFIGURING)
67             .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_OK)
68             .setDataChannelState(Status.DATA_CHANNEL_STATE_OK)
69             .apply();
70         break;
71       case CONFIG_ACTIVATING_SUBSEQUENT:
72         status
73             .setConfigurationState(Status.CONFIGURATION_STATE_OK)
74             .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_OK)
75             .setDataChannelState(Status.DATA_CHANNEL_STATE_OK)
76             .apply();
77         break;
78       case CONFIG_SERVICE_NOT_AVAILABLE:
79         status.setConfigurationState(Status.CONFIGURATION_STATE_FAILED).apply();
80         break;
81       case CONFIG_STATUS_SMS_TIME_OUT:
82         status.setConfigurationState(Status.CONFIGURATION_STATE_FAILED).apply();
83         break;
84       default:
85         VvmLog.wtf(TAG, "invalid configuration event " + event);
86     }
87   }
88 
handleDataChannelEvent( Context context, VoicemailStatus.Editor status, OmtpEvents event)89   private static void handleDataChannelEvent(
90       Context context, VoicemailStatus.Editor status, OmtpEvents event) {
91     switch (event) {
92       case DATA_IMAP_OPERATION_STARTED:
93       case DATA_IMAP_OPERATION_COMPLETED:
94         status.setDataChannelState(Status.DATA_CHANNEL_STATE_OK).apply();
95         break;
96 
97       case DATA_NO_CONNECTION:
98         status.setDataChannelState(Status.DATA_CHANNEL_STATE_NO_CONNECTION).apply();
99         break;
100 
101       case DATA_NO_CONNECTION_CELLULAR_REQUIRED:
102         status
103             .setDataChannelState(Status.DATA_CHANNEL_STATE_NO_CONNECTION_CELLULAR_REQUIRED)
104             .apply();
105         break;
106       case DATA_INVALID_PORT:
107         status
108             .setDataChannelState(VoicemailContract.Status.DATA_CHANNEL_STATE_BAD_CONFIGURATION)
109             .apply();
110         break;
111       case DATA_CANNOT_RESOLVE_HOST_ON_NETWORK:
112         status
113             .setDataChannelState(
114                 VoicemailContract.Status.DATA_CHANNEL_STATE_SERVER_CONNECTION_ERROR)
115             .apply();
116         break;
117       case DATA_SSL_INVALID_HOST_NAME:
118       case DATA_CANNOT_ESTABLISH_SSL_SESSION:
119       case DATA_IOE_ON_OPEN:
120       case DATA_GENERIC_IMAP_IOE:
121         status
122             .setDataChannelState(VoicemailContract.Status.DATA_CHANNEL_STATE_COMMUNICATION_ERROR)
123             .apply();
124         break;
125       case DATA_BAD_IMAP_CREDENTIAL:
126       case DATA_AUTH_UNKNOWN_USER:
127       case DATA_AUTH_UNKNOWN_DEVICE:
128       case DATA_AUTH_INVALID_PASSWORD:
129       case DATA_AUTH_MAILBOX_NOT_INITIALIZED:
130       case DATA_AUTH_SERVICE_NOT_PROVISIONED:
131       case DATA_AUTH_SERVICE_NOT_ACTIVATED:
132       case DATA_AUTH_USER_IS_BLOCKED:
133         status
134             .setDataChannelState(VoicemailContract.Status.DATA_CHANNEL_STATE_BAD_CONFIGURATION)
135             .apply();
136         break;
137 
138       case DATA_REJECTED_SERVER_RESPONSE:
139       case DATA_INVALID_INITIAL_SERVER_RESPONSE:
140       case DATA_MAILBOX_OPEN_FAILED:
141       case DATA_SSL_EXCEPTION:
142       case DATA_ALL_SOCKET_CONNECTION_FAILED:
143         status
144             .setDataChannelState(VoicemailContract.Status.DATA_CHANNEL_STATE_SERVER_ERROR)
145             .apply();
146         break;
147 
148       default:
149         VvmLog.wtf(TAG, "invalid data channel event " + event);
150     }
151   }
152 
handleNotificationChannelEvent( Context context, OmtpVvmCarrierConfigHelper config, VoicemailStatus.Editor status, OmtpEvents event)153   private static void handleNotificationChannelEvent(
154       Context context,
155       OmtpVvmCarrierConfigHelper config,
156       VoicemailStatus.Editor status,
157       OmtpEvents event) {
158     switch (event) {
159       case NOTIFICATION_IN_SERVICE:
160         status
161             .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_OK)
162             // Clear the error state. A sync should follow signal return so any error
163             // will be reposted.
164             .setDataChannelState(Status.DATA_CHANNEL_STATE_OK)
165             .apply();
166         break;
167       case NOTIFICATION_SERVICE_LOST:
168         status.setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION);
169         if (config.isCellularDataRequired()) {
170           status.setDataChannelState(Status.DATA_CHANNEL_STATE_NO_CONNECTION_CELLULAR_REQUIRED);
171         }
172         status.apply();
173         break;
174       default:
175         VvmLog.wtf(TAG, "invalid notification channel event " + event);
176     }
177   }
178 
handleOtherEvent( Context context, VoicemailStatus.Editor status, OmtpEvents event)179   private static void handleOtherEvent(
180       Context context, VoicemailStatus.Editor status, OmtpEvents event) {
181     switch (event) {
182       case OTHER_SOURCE_REMOVED:
183         status
184             .setConfigurationState(Status.CONFIGURATION_STATE_NOT_CONFIGURED)
185             .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_NO_CONNECTION)
186             .setDataChannelState(Status.DATA_CHANNEL_STATE_NO_CONNECTION)
187             .apply();
188         break;
189       default:
190         VvmLog.wtf(TAG, "invalid other event " + event);
191     }
192   }
193 }
194