1 /*
2  * Copyright (C) 2011 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.dialer.voicemailstatus;
18 
19 import android.database.Cursor;
20 import android.provider.VoicemailContract.Status;
21 
22 /**
23  * Utility used by the call log UI to determine what user message, if any, related to voicemail
24  * source status needs to be shown. The messages are returned in the order of importance.
25  *
26  * <p>This class interacts with the voicemail content provider to fetch statuses of all the
27  * registered voicemail sources and determines if any status message needs to be shown. The user of
28  * this class must observe/listen to provider changes and invoke this class to check if any message
29  * needs to be shown.
30  */
31 public final class VoicemailStatusHelper {
32 
VoicemailStatusHelper()33   private VoicemailStatusHelper() {}
34 
35   /**
36    * Returns the number of active voicemail sources installed.
37    *
38    * <p>The number of sources is counted by querying the voicemail status table.
39    *
40    * @param cursor The caller is responsible for the life cycle of the cursor and resetting the
41    *     position
42    */
getNumberActivityVoicemailSources(Cursor cursor)43   public static int getNumberActivityVoicemailSources(Cursor cursor) {
44     int count = 0;
45     if (!cursor.moveToFirst()) {
46       return 0;
47     }
48     do {
49       if (isVoicemailSourceActive(cursor)) {
50         ++count;
51       }
52     } while (cursor.moveToNext());
53     return count;
54   }
55 
56   /**
57    * Returns whether the source status in the cursor corresponds to an active source. A source is
58    * active if its' configuration state is not NOT_CONFIGURED. For most voicemail sources, only OK
59    * and NOT_CONFIGURED are used. The OMTP visual voicemail client has the same behavior pre-NMR1.
60    * NMR1 visual voicemail will only set it to NOT_CONFIGURED when it is deactivated. As soon as
61    * activation is attempted, it will transition into CONFIGURING then into OK or other error state,
62    * NOT_CONFIGURED is never set through an error.
63    */
isVoicemailSourceActive(Cursor cursor)64   private static boolean isVoicemailSourceActive(Cursor cursor) {
65     return cursor.getString(VoicemailStatusQuery.SOURCE_PACKAGE_INDEX) != null
66         // getInt() returns 0 when null
67         && !cursor.isNull(VoicemailStatusQuery.CONFIGURATION_STATE_INDEX)
68         && cursor.getInt(VoicemailStatusQuery.CONFIGURATION_STATE_INDEX)
69             != Status.CONFIGURATION_STATE_NOT_CONFIGURED;
70   }
71 }
72