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