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.contacts.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 
     26 import com.android.contacts.CallDetailActivity;
     27 import com.android.contacts.ContactsUtils;
     28 
     29 /**
     30  * Used to create an intent to attach to an action in the call log.
     31  * <p>
     32  * The intent is constructed lazily with the given information.
     33  */
     34 public abstract class IntentProvider {
     35     public abstract Intent getIntent(Context context);
     36 
     37     public static IntentProvider getReturnCallIntentProvider(final String number) {
     38         return new IntentProvider() {
     39             @Override
     40             public Intent getIntent(Context context) {
     41                 return ContactsUtils.getCallIntent(number);
     42             }
     43         };
     44     }
     45 
     46     public static IntentProvider getPlayVoicemailIntentProvider(final long rowId,
     47             final String voicemailUri) {
     48         return new IntentProvider() {
     49             @Override
     50             public Intent getIntent(Context context) {
     51                 Intent intent = new Intent(context, CallDetailActivity.class);
     52                 intent.setData(ContentUris.withAppendedId(
     53                         Calls.CONTENT_URI_WITH_VOICEMAIL, rowId));
     54                 if (voicemailUri != null) {
     55                     intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI,
     56                             Uri.parse(voicemailUri));
     57                 }
     58                 intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_START_PLAYBACK, true);
     59                 return intent;
     60             }
     61         };
     62     }
     63 
     64     public static IntentProvider getCallDetailIntentProvider(
     65             final CallLogAdapter adapter, final int position, final long id, final int groupSize) {
     66         return new IntentProvider() {
     67             @Override
     68             public Intent getIntent(Context context) {
     69                 Cursor cursor = adapter.getCursor();
     70                 cursor.moveToPosition(position);
     71                 if (CallLogQuery.isSectionHeader(cursor)) {
     72                     // Do nothing when a header is clicked.
     73                     return null;
     74                 }
     75                 Intent intent = new Intent(context, CallDetailActivity.class);
     76                 // Check if the first item is a voicemail.
     77                 String voicemailUri = cursor.getString(CallLogQuery.VOICEMAIL_URI);
     78                 if (voicemailUri != null) {
     79                     intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI,
     80                             Uri.parse(voicemailUri));
     81                 }
     82                 intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_START_PLAYBACK, false);
     83 
     84                 if (groupSize > 1) {
     85                     // We want to restore the position in the cursor at the end.
     86                     long[] ids = new long[groupSize];
     87                     // Copy the ids of the rows in the group.
     88                     for (int index = 0; index < groupSize; ++index) {
     89                         ids[index] = cursor.getLong(CallLogQuery.ID);
     90                         cursor.moveToNext();
     91                     }
     92                     intent.putExtra(CallDetailActivity.EXTRA_CALL_LOG_IDS, ids);
     93                 } else {
     94                     // If there is a single item, use the direct URI for it.
     95                     intent.setData(ContentUris.withAppendedId(
     96                             Calls.CONTENT_URI_WITH_VOICEMAIL, id));
     97                 }
     98                 return intent;
     99             }
    100         };
    101     }
    102 }
    103