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.searchfragment.list; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.support.annotation.IntDef; 22 import android.support.annotation.StringRes; 23 import android.support.annotation.VisibleForTesting; 24 import android.support.v7.widget.RecyclerView; 25 import android.view.View; 26 import android.view.View.OnClickListener; 27 import android.widget.ImageView; 28 import android.widget.TextView; 29 import com.android.dialer.common.Assert; 30 import com.android.dialer.logging.DialerImpression; 31 import com.android.dialer.logging.Logger; 32 import com.android.dialer.searchfragment.common.RowClickListener; 33 import com.android.dialer.util.DialerUtils; 34 import com.android.dialer.util.IntentUtil; 35 import java.lang.annotation.Retention; 36 import java.lang.annotation.RetentionPolicy; 37 38 /** 39 * {@link RecyclerView.ViewHolder} for showing an {@link SearchActionViewHolder.Action} in a list. 40 */ 41 final class SearchActionViewHolder extends RecyclerView.ViewHolder implements OnClickListener { 42 43 /** IntDef for the different types of actions that can be used. */ 44 @Retention(RetentionPolicy.SOURCE) 45 @IntDef({ 46 Action.INVALID, 47 Action.CREATE_NEW_CONTACT, 48 Action.ADD_TO_CONTACT, 49 Action.SEND_SMS, 50 Action.MAKE_VILTE_CALL, 51 Action.MAKE_VOICE_CALL 52 }) 53 @interface Action { 54 int INVALID = 0; 55 /** Opens the prompt to create a new contact. */ 56 int CREATE_NEW_CONTACT = 1; 57 /** Opens a prompt to add to an existing contact. */ 58 int ADD_TO_CONTACT = 2; 59 /** Opens the SMS conversation with the default SMS app. */ 60 int SEND_SMS = 3; 61 /** Attempts to make a VILTE call to the number. */ 62 int MAKE_VILTE_CALL = 4; 63 /** Places a voice call to the number. */ 64 int MAKE_VOICE_CALL = 5; 65 } 66 67 private final Context context; 68 private final ImageView actionImage; 69 private final TextView actionText; 70 private final RowClickListener listener; 71 72 private @Action int action; 73 private int position; 74 private String query; 75 SearchActionViewHolder(View view, RowClickListener listener)76 SearchActionViewHolder(View view, RowClickListener listener) { 77 super(view); 78 context = view.getContext(); 79 actionImage = view.findViewById(R.id.search_action_image); 80 actionText = view.findViewById(R.id.search_action_text); 81 this.listener = listener; 82 view.setOnClickListener(this); 83 } 84 setAction(@ction int action, int position, String query)85 void setAction(@Action int action, int position, String query) { 86 this.action = action; 87 this.position = position; 88 this.query = query; 89 switch (action) { 90 case Action.ADD_TO_CONTACT: 91 actionText.setText(R.string.search_shortcut_add_to_contact); 92 actionImage.setImageResource(R.drawable.quantum_ic_person_add_vd_theme_24); 93 break; 94 case Action.CREATE_NEW_CONTACT: 95 actionText.setText(R.string.search_shortcut_create_new_contact); 96 actionImage.setImageResource(R.drawable.quantum_ic_person_add_vd_theme_24); 97 break; 98 case Action.MAKE_VILTE_CALL: 99 actionText.setText(R.string.search_shortcut_make_video_call); 100 actionImage.setImageResource(R.drawable.quantum_ic_videocam_vd_theme_24); 101 break; 102 case Action.SEND_SMS: 103 actionText.setText(R.string.search_shortcut_send_sms_message); 104 actionImage.setImageResource(R.drawable.quantum_ic_message_vd_theme_24); 105 break; 106 case Action.MAKE_VOICE_CALL: 107 actionText.setText(context.getString(R.string.search_shortcut_make_voice_call, query)); 108 actionImage.setImageResource(R.drawable.quantum_ic_phone_vd_theme_24); 109 break; 110 case Action.INVALID: 111 default: 112 throw Assert.createIllegalStateFailException("Invalid action: " + action); 113 } 114 } 115 116 @VisibleForTesting 117 @Action getAction()118 int getAction() { 119 return action; 120 } 121 122 @Override onClick(View v)123 public void onClick(View v) { 124 switch (action) { 125 case Action.ADD_TO_CONTACT: 126 Logger.get(context).logImpression(DialerImpression.Type.ADD_TO_A_CONTACT_FROM_DIALPAD); 127 Intent intent = IntentUtil.getAddToExistingContactIntent(query); 128 @StringRes int errorString = R.string.add_contact_not_available; 129 DialerUtils.startActivityWithErrorToast(context, intent, errorString); 130 break; 131 132 case Action.CREATE_NEW_CONTACT: 133 Logger.get(context).logImpression(DialerImpression.Type.CREATE_NEW_CONTACT_FROM_DIALPAD); 134 intent = IntentUtil.getNewContactIntent(query); 135 DialerUtils.startActivityWithErrorToast(context, intent); 136 break; 137 138 case Action.MAKE_VILTE_CALL: 139 listener.placeVideoCall(query, position); 140 break; 141 142 case Action.SEND_SMS: 143 intent = IntentUtil.getSendSmsIntent(query); 144 DialerUtils.startActivityWithErrorToast(context, intent); 145 break; 146 147 case Action.MAKE_VOICE_CALL: 148 listener.placeVoiceCall(query, position); 149 break; 150 151 case Action.INVALID: 152 default: 153 throw Assert.createIllegalStateFailException("Invalid action: " + action); 154 } 155 } 156 } 157