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.dialer.voicemail.listui.error;
18 
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.provider.VoicemailContract.Status;
22 import android.support.annotation.Nullable;
23 import com.android.dialer.common.concurrent.DialerExecutor.Worker;
24 import com.android.dialer.telecom.TelecomUtil;
25 import com.android.dialer.voicemailstatus.VoicemailStatusQuery;
26 import com.android.voicemail.VoicemailComponent;
27 import java.util.ArrayList;
28 import java.util.List;
29 
30 /**
31  * Worker for {@link com.android.dialer.common.concurrent.DialerExecutors} to fetch voicemail status
32  */
33 public class VoicemailStatusWorker implements Worker<Context, List<VoicemailStatus>> {
34 
35   @Nullable
36   @Override
doInBackground(@ullable Context context)37   public List<VoicemailStatus> doInBackground(@Nullable Context context) throws Throwable {
38     List<VoicemailStatus> statuses = new ArrayList<>();
39     if (!TelecomUtil.hasReadWriteVoicemailPermissions(context)) {
40       return statuses;
41     }
42     StringBuilder where = new StringBuilder();
43     java.util.List<String> selectionArgs = new ArrayList<>();
44 
45     VoicemailComponent.get(context)
46         .getVoicemailClient()
47         .appendOmtpVoicemailStatusSelectionClause(context, where, selectionArgs);
48 
49     try (Cursor cursor =
50         context
51             .getContentResolver()
52             .query(
53                 Status.CONTENT_URI,
54                 VoicemailStatusQuery.getProjection(),
55                 where.toString(),
56                 selectionArgs.toArray(new String[selectionArgs.size()]),
57                 null)) {
58       if (cursor == null) {
59         return statuses;
60       }
61 
62       for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
63         statuses.add(new VoicemailStatus(context, cursor));
64       }
65     }
66 
67     return statuses;
68   }
69 }
70