Home | History | Annotate | Download | only in calllog
      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.calllog;
     18 
     19 import android.content.ContentUris;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.database.Cursor;
     23 import android.net.Uri;
     24 import android.provider.CallLog.Calls;
     25 import android.util.Log;
     26 
     27 import com.android.contacts.common.CallUtil;
     28 import com.android.dialer.CallDetailActivity;
     29 
     30 /**
     31  * Used to create an intent to attach to an action in the call log.
     32  * <p>
     33  * The intent is constructed lazily with the given information.
     34  */
     35 public abstract class IntentProvider {
     36 
     37     private static final String TAG = IntentProvider.class.getSimpleName();
     38 
     39     public abstract Intent getIntent(Context context);
     40 
     41     public static IntentProvider getReturnCallIntentProvider(final String number) {
     42         return new IntentProvider() {
     43             @Override
     44             public Intent getIntent(Context context) {
     45                 return CallUtil.getCallIntent(number);
     46             }
     47         };
     48     }
     49 
     50     public static IntentProvider getPlayVoicemailIntentProvider(final long rowId,
     51             final String voicemailUri) {
     52         return new IntentProvider() {
     53             @Override
     54             public Intent getIntent(Context context) {
     55                 Intent intent = new Intent(context, CallDetailActivity.class);
     56                 intent.setData(ContentUris.withAppendedId(
     57                         Calls.CONTENT_URI_WITH_VOICEMAIL, rowId));
     58                 if (voicemailUri != null) {
     59                     intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI,
     60                             Uri.parse(voicemailUri));
     61                 }
     62                 intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_START_PLAYBACK, true);
     63                 return intent;
     64             }
     65         };
     66     }
     67 
     68     public static IntentProvider getCallDetailIntentProvider(
     69             final Cursor cursor, final int position, final long id, final int groupSize) {
     70         return new IntentProvider() {
     71             @Override
     72             public Intent getIntent(Context context) {
     73                 if (cursor.isClosed()) {
     74                     // There are reported instances where the cursor is already closed.
     75                     // b/10937133
     76                     // When causes a crash when it's accessed here.
     77                     Log.e(TAG, "getCallDetailIntentProvider() cursor is already closed.");
     78                     return null;
     79                 }
     80 
     81                 cursor.moveToPosition(position);
     82 
     83                 Intent intent = new Intent(context, CallDetailActivity.class);
     84                 // Check if the first item is a voicemail.
     85                 String voicemailUri = cursor.getString(CallLogQuery.VOICEMAIL_URI);
     86                 if (voicemailUri != null) {
     87                     intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI,
     88                             Uri.parse(voicemailUri));
     89                 }
     90                 intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_START_PLAYBACK, false);
     91 
     92                 if (groupSize > 1) {
     93                     // We want to restore the position in the cursor at the end.
     94                     long[] ids = new long[groupSize];
     95                     // Copy the ids of the rows in the group.
     96                     for (int index = 0; index < groupSize; ++index) {
     97                         ids[index] = cursor.getLong(CallLogQuery.ID);
     98                         cursor.moveToNext();
     99                     }
    100                     intent.putExtra(CallDetailActivity.EXTRA_CALL_LOG_IDS, ids);
    101                 } else {
    102                     // If there is a single item, use the direct URI for it.
    103                     intent.setData(ContentUris.withAppendedId(
    104                             Calls.CONTENT_URI_WITH_VOICEMAIL, id));
    105                 }
    106                 return intent;
    107             }
    108         };
    109     }
    110 }
    111