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.historyitemactions; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.support.annotation.DrawableRes; 22 import android.support.annotation.StringRes; 23 import com.android.dialer.callintent.CallIntentBuilder; 24 import com.android.dialer.logging.DialerImpression; 25 import com.android.dialer.logging.Logger; 26 import com.android.dialer.precall.PreCall; 27 import com.android.dialer.util.DialerUtils; 28 import com.android.dialer.util.IntentUtil; 29 import com.google.common.collect.ImmutableList; 30 31 /** 32 * {@link HistoryItemActionModule} useful for making easy to build modules based on starting an 33 * intent. 34 */ 35 public class IntentModule implements HistoryItemActionModule { 36 37 private final Context context; 38 private final Intent intent; 39 private final @StringRes int text; 40 private final @DrawableRes int image; 41 private final ImmutableList<DialerImpression.Type> impressions; 42 43 /** 44 * @deprecated use {@link IntentModule#IntentModule(Context, Intent, int, int, ImmutableList)} 45 * instead. 46 */ 47 @Deprecated IntentModule(Context context, Intent intent, @StringRes int text, @DrawableRes int image)48 public IntentModule(Context context, Intent intent, @StringRes int text, @DrawableRes int image) { 49 this(context, intent, text, image, /* impressions = */ ImmutableList.of()); 50 } 51 IntentModule( Context context, Intent intent, @StringRes int text, @DrawableRes int image, ImmutableList<DialerImpression.Type> impressions)52 IntentModule( 53 Context context, 54 Intent intent, 55 @StringRes int text, 56 @DrawableRes int image, 57 ImmutableList<DialerImpression.Type> impressions) { 58 this.context = context; 59 this.intent = intent; 60 this.text = text; 61 this.image = image; 62 this.impressions = impressions; 63 } 64 65 @Override getStringId()66 public int getStringId() { 67 return text; 68 } 69 70 @Override getDrawableId()71 public int getDrawableId() { 72 return image; 73 } 74 75 @Override onClick()76 public boolean onClick() { 77 DialerUtils.startActivityWithErrorToast(context, intent); 78 impressions.forEach(Logger.get(context)::logImpression); 79 return true; 80 } 81 82 /** @deprecated Use {@link #newCallModule(Context, CallIntentBuilder, ImmutableList)} instead. */ 83 @Deprecated newCallModule(Context context, CallIntentBuilder callIntentBuilder)84 public static IntentModule newCallModule(Context context, CallIntentBuilder callIntentBuilder) { 85 return newCallModule(context, callIntentBuilder, /* impressions = */ ImmutableList.of()); 86 } 87 88 /** Creates a module for starting an outgoing call with a {@link CallIntentBuilder}. */ newCallModule( Context context, CallIntentBuilder callIntentBuilder, ImmutableList<DialerImpression.Type> impressions)89 static IntentModule newCallModule( 90 Context context, 91 CallIntentBuilder callIntentBuilder, 92 ImmutableList<DialerImpression.Type> impressions) { 93 @StringRes int text; 94 @DrawableRes int image; 95 96 if (callIntentBuilder.isVideoCall()) { 97 text = R.string.video_call; 98 image = R.drawable.quantum_ic_videocam_vd_white_24; 99 } else { 100 text = R.string.voice_call; 101 image = R.drawable.quantum_ic_call_white_24; 102 } 103 104 return new IntentModule( 105 context, PreCall.getIntent(context, callIntentBuilder), text, image, impressions); 106 } 107 108 /** 109 * @deprecated Use {@link #newModuleForSendingTextMessage(Context, String, ImmutableList)} 110 * instead. 111 */ 112 @Deprecated newModuleForSendingTextMessage(Context context, String number)113 public static IntentModule newModuleForSendingTextMessage(Context context, String number) { 114 return newModuleForSendingTextMessage(context, number, /* impressions = */ ImmutableList.of()); 115 } 116 117 /** Creates a module for sending a text message to the given number. */ newModuleForSendingTextMessage( Context context, String number, ImmutableList<DialerImpression.Type> impressions)118 static IntentModule newModuleForSendingTextMessage( 119 Context context, String number, ImmutableList<DialerImpression.Type> impressions) { 120 return new IntentModule( 121 context, 122 IntentUtil.getSendSmsIntent(number), 123 R.string.send_a_message, 124 R.drawable.quantum_ic_message_vd_theme_24, 125 impressions); 126 } 127 } 128