1ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian/*
2ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * Copyright (C) 2011 The Android Open Source Project
3ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian *
4ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * Licensed under the Apache License, Version 2.0 (the "License");
5ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * you may not use this file except in compliance with the License.
6ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * You may obtain a copy of the License at
7ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian *
8ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian *      http://www.apache.org/licenses/LICENSE-2.0
9ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian *
10ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * Unless required by applicable law or agreed to in writing, software
11ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * distributed under the License is distributed on an "AS IS" BASIS,
12ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * See the License for the specific language governing permissions and
14ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * limitations under the License
15ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian */
16ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
17ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianpackage com.android.dialer.voicemailstatus;
18ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
19ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianimport android.database.Cursor;
20ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanianimport android.provider.VoicemailContract.Status;
21fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanianimport com.android.dialer.database.VoicemailStatusQuery;
22ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
23ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian/**
2410ae593a59aa50963e1d3159747da2d65ca79bedEric Erfanian * Utility used by the call log UI to determine what user message, if any, related to voicemail
25ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian * source status needs to be shown. The messages are returned in the order of importance.
26ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian *
2710ae593a59aa50963e1d3159747da2d65ca79bedEric Erfanian * <p>This class interacts with the voicemail content provider to fetch statuses of all the
2810ae593a59aa50963e1d3159747da2d65ca79bedEric Erfanian * registered voicemail sources and determines if any status message needs to be shown. The user of
2910ae593a59aa50963e1d3159747da2d65ca79bedEric Erfanian * this class must observe/listen to provider changes and invoke this class to check if any message
3010ae593a59aa50963e1d3159747da2d65ca79bedEric Erfanian * needs to be shown.
31ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian */
3210ae593a59aa50963e1d3159747da2d65ca79bedEric Erfanianpublic final class VoicemailStatusHelper {
3310ae593a59aa50963e1d3159747da2d65ca79bedEric Erfanian
3410ae593a59aa50963e1d3159747da2d65ca79bedEric Erfanian  private VoicemailStatusHelper() {}
35ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian
36ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  /**
37ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian   * Returns the number of active voicemail sources installed.
38ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian   *
39ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian   * <p>The number of sources is counted by querying the voicemail status table.
40fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian   *
41fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian   * @param cursor The caller is responsible for the life cycle of the cursor and resetting the
42fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian   *     position
43ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian   */
4410ae593a59aa50963e1d3159747da2d65ca79bedEric Erfanian  public static int getNumberActivityVoicemailSources(Cursor cursor) {
45fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian    int count = 0;
46fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian    if (!cursor.moveToFirst()) {
47fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian      return 0;
4830ccc4f3aa6da94f0bb8a01a880a6353b883b263Eric Erfanian    }
49fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian    do {
50fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian      if (isVoicemailSourceActive(cursor)) {
51fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian        ++count;
52fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian      }
53fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian    } while (cursor.moveToNext());
54fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian    return count;
55fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian  }
5630ccc4f3aa6da94f0bb8a01a880a6353b883b263Eric Erfanian
57fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian  /**
58fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian   * Returns whether the source status in the cursor corresponds to an active source. A source is
59fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian   * active if its' configuration state is not NOT_CONFIGURED. For most voicemail sources, only OK
60fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian   * and NOT_CONFIGURED are used. The OMTP visual voicemail client has the same behavior pre-NMR1.
61fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian   * NMR1 visual voicemail will only set it to NOT_CONFIGURED when it is deactivated. As soon as
62fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian   * activation is attempted, it will transition into CONFIGURING then into OK or other error state,
63fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian   * NOT_CONFIGURED is never set through an error.
64fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian   */
6510ae593a59aa50963e1d3159747da2d65ca79bedEric Erfanian  private static boolean isVoicemailSourceActive(Cursor cursor) {
66fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian    return cursor.getString(VoicemailStatusQuery.SOURCE_PACKAGE_INDEX) != null
6710ae593a59aa50963e1d3159747da2d65ca79bedEric Erfanian        // getInt() returns 0 when null
6810ae593a59aa50963e1d3159747da2d65ca79bedEric Erfanian        && !cursor.isNull(VoicemailStatusQuery.CONFIGURATION_STATE_INDEX)
69fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian        && cursor.getInt(VoicemailStatusQuery.CONFIGURATION_STATE_INDEX)
70fc37b02f5d3381a7882770941e461b13b679b6efEric Erfanian            != Status.CONFIGURATION_STATE_NOT_CONFIGURED;
71ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian  }
72ccca31529c07970e89419fb85a9e8153a5396838Eric Erfanian}
73