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