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.net.Uri;
23 import android.provider.CallLog.Calls;
24 import android.telecom.PhoneAccountHandle;
25 
26 import com.android.contacts.common.CallUtil;
27 import com.android.dialer.CallDetailActivity;
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 
36     private static final String TAG = IntentProvider.class.getSimpleName();
37 
getIntent(Context context)38     public abstract Intent getIntent(Context context);
39 
getReturnCallIntentProvider(final String number)40     public static IntentProvider getReturnCallIntentProvider(final String number) {
41         return getReturnCallIntentProvider(number, null);
42     }
43 
getReturnCallIntentProvider(final String number, final PhoneAccountHandle accountHandle)44     public static IntentProvider getReturnCallIntentProvider(final String number,
45             final PhoneAccountHandle accountHandle) {
46         return new IntentProvider() {
47             @Override
48             public Intent getIntent(Context context) {
49                 return CallUtil.getCallIntent(number, accountHandle);
50             }
51         };
52     }
53 
54     public static IntentProvider getReturnVideoCallIntentProvider(final String number) {
55         return getReturnVideoCallIntentProvider(number, null);
56     }
57 
58     public static IntentProvider getReturnVideoCallIntentProvider(final String number,
59             final PhoneAccountHandle accountHandle) {
60         return new IntentProvider() {
61             @Override
62             public Intent getIntent(Context context) {
63                 return CallUtil.getVideoCallIntent(number, accountHandle);
64             }
65         };
66     }
67 
68     public static IntentProvider getReturnVoicemailCallIntentProvider() {
69         return new IntentProvider() {
70             @Override
71             public Intent getIntent(Context context) {
72                 return CallUtil.getVoicemailIntent();
73             }
74         };
75     }
76 
77     public static IntentProvider getPlayVoicemailIntentProvider(final long rowId,
78             final String voicemailUri) {
79         return new IntentProvider() {
80             @Override
81             public Intent getIntent(Context context) {
82                 Intent intent = new Intent(context, CallDetailActivity.class);
83                 intent.setData(ContentUris.withAppendedId(
84                         Calls.CONTENT_URI_WITH_VOICEMAIL, rowId));
85                 if (voicemailUri != null) {
86                     intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI,
87                             Uri.parse(voicemailUri));
88                 }
89                 intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_START_PLAYBACK, true);
90                 return intent;
91             }
92         };
93     }
94 
95     /**
96      * Retrieves the call details intent provider for an entry in the call log.
97      *
98      * @param id The call ID of the first call in the call group.
99      * @param extraIds The call ID of the other calls grouped together with the call.
100      * @param voicemailUri If call log entry is for a voicemail, the voicemail URI.
101      * @return The call details intent provider.
102      */
103     public static IntentProvider getCallDetailIntentProvider(
104             final long id, final long[] extraIds, final String voicemailUri) {
105         return new IntentProvider() {
106             @Override
107             public Intent getIntent(Context context) {
108                 Intent intent = new Intent(context, CallDetailActivity.class);
109                 // Check if the first item is a voicemail.
110                 if (voicemailUri != null) {
111                     intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI,
112                             Uri.parse(voicemailUri));
113                 }
114                 intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_START_PLAYBACK, false);
115 
116                 if (extraIds != null && extraIds.length > 0) {
117                     intent.putExtra(CallDetailActivity.EXTRA_CALL_LOG_IDS, extraIds);
118                 } else {
119                     // If there is a single item, use the direct URI for it.
120                     intent.setData(ContentUris.withAppendedId(
121                             Calls.CONTENT_URI_WITH_VOICEMAIL, id));
122                 }
123                 return intent;
124             }
125         };
126     }
127 }
128