summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/calllog/ui/NewCallLogViewHolder.java
blob: 3b21a60def02a3062e6a238728020bf6f9a31bde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
package com.android.dialer.calllog.ui;

import android.app.Activity;
import android.content.res.ColorStateList;
import android.database.Cursor;
import android.provider.CallLog.Calls;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.v7.widget.RecyclerView;
import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.AccessibilityDelegate;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.dialer.calllog.database.Coalescer;
import com.android.dialer.calllog.model.CoalescedRow;
import com.android.dialer.calllog.ui.NewCallLogAdapter.PopCounts;
import com.android.dialer.calllog.ui.menu.NewCallLogMenu;
import com.android.dialer.calllogutils.CallLogEntryDescriptions;
import com.android.dialer.calllogutils.CallLogEntryText;
import com.android.dialer.calllogutils.CallLogRowActions;
import com.android.dialer.calllogutils.PhoneAccountUtils;
import com.android.dialer.calllogutils.PhotoInfoBuilder;
import com.android.dialer.common.concurrent.DialerExecutorComponent;
import com.android.dialer.compat.telephony.TelephonyManagerCompat;
import com.android.dialer.oem.MotorolaUtils;
import com.android.dialer.phonenumberutil.PhoneNumberHelper;
import com.android.dialer.telecom.TelecomUtil;
import com.android.dialer.time.Clock;
import com.android.dialer.widget.ContactPhotoView;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import java.util.Locale;
import java.util.concurrent.ExecutorService;

/** {@link RecyclerView.ViewHolder} for the new call log. */
final class NewCallLogViewHolder extends RecyclerView.ViewHolder {

  private final Activity activity;
  private final ContactPhotoView contactPhotoView;
  private final TextView primaryTextView;
  private final TextView callCountTextView;
  private final TextView secondaryTextView;
  private final ImageView callTypeIcon;
  private final ImageView hdIcon;
  private final ImageView wifiIcon;
  private final ImageView assistedDialIcon;
  private final TextView phoneAccountView;
  private final ImageView menuButton;
  private final View callLogEntryRootView;

  private final Clock clock;
  private final RealtimeRowProcessor realtimeRowProcessor;
  private final ExecutorService uiExecutorService;
  private final PopCounts popCounts;

  private long currentRowId;

  NewCallLogViewHolder(
      Activity activity,
      View view,
      Clock clock,
      RealtimeRowProcessor realtimeRowProcessor,
      PopCounts popCounts) {
    super(view);
    this.activity = activity;
    callLogEntryRootView = view;
    contactPhotoView = view.findViewById(R.id.contact_photo_view);
    primaryTextView = view.findViewById(R.id.primary_text);
    callCountTextView = view.findViewById(R.id.call_count);
    secondaryTextView = view.findViewById(R.id.secondary_text);
    callTypeIcon = view.findViewById(R.id.call_type_icon);
    hdIcon = view.findViewById(R.id.hd_icon);
    wifiIcon = view.findViewById(R.id.wifi_icon);
    assistedDialIcon = view.findViewById(R.id.assisted_dial_icon);
    phoneAccountView = view.findViewById(R.id.phone_account);
    menuButton = view.findViewById(R.id.menu_button);

    this.clock = clock;
    this.realtimeRowProcessor = realtimeRowProcessor;
    this.popCounts = popCounts;
    uiExecutorService = DialerExecutorComponent.get(activity).uiExecutor();
  }

  /**
   * @param cursor a cursor for {@link
   *     com.android.dialer.calllog.database.contract.AnnotatedCallLogContract.CoalescedAnnotatedCallLog}.
   */
  void bind(Cursor cursor) {
    CoalescedRow row = Coalescer.toRow(cursor);
    currentRowId = row.getId(); // Used to make sure async updates are applied to the correct views

    // Even if there is additional real time processing necessary, we still want to immediately show
    // what information we have, rather than an empty card. For example, if CP2 information needs to
    // be queried on the fly, we can still show the phone number until the contact name loads.
    displayRow(row);
    configA11yForRow(row);

    // Note: This leaks the view holder via the callback (which is an inner class), but this is OK
    // because we only create ~10 of them (and they'll be collected assuming all jobs finish).
    Futures.addCallback(
        realtimeRowProcessor.applyRealtimeProcessing(row),
        new RealtimeRowFutureCallback(row),
        uiExecutorService);
  }

  private void displayRow(CoalescedRow row) {
    // TODO(zachh): Handle RTL properly.
    primaryTextView.setText(CallLogEntryText.buildPrimaryText(activity, row));
    secondaryTextView.setText(CallLogEntryText.buildSecondaryTextForEntries(activity, clock, row));

    if (isUnreadMissedCall(row)) {
      primaryTextView.setTextAppearance(R.style.primary_textview_unread_call);
      callCountTextView.setTextAppearance(R.style.primary_textview_unread_call);
      secondaryTextView.setTextAppearance(R.style.secondary_textview_unread_call);
      phoneAccountView.setTextAppearance(R.style.phoneaccount_textview_unread_call);
    } else {
      primaryTextView.setTextAppearance(R.style.primary_textview);
      callCountTextView.setTextAppearance(R.style.primary_textview);
      secondaryTextView.setTextAppearance(R.style.secondary_textview);
      phoneAccountView.setTextAppearance(R.style.phoneaccount_textview);
    }

    setNumberCalls(row);
    setPhoto(row);
    setFeatureIcons(row);
    setCallTypeIcon(row);
    setPhoneAccounts(row);
    setOnClickListenerForRow(row);
    setOnClickListenerForMenuButon(row);
  }

  private void configA11yForRow(CoalescedRow row) {
    callLogEntryRootView.setContentDescription(
        CallLogEntryDescriptions.buildDescriptionForEntry(activity, clock, row));

    // Inform a11y users that double tapping an entry now makes a call.
    // This will instruct TalkBack to say "double tap to call" instead of
    // "double tap to activate".
    callLogEntryRootView.setAccessibilityDelegate(
        new AccessibilityDelegate() {
          @Override
          public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
            super.onInitializeAccessibilityNodeInfo(host, info);
            info.addAction(
                new AccessibilityAction(
                    AccessibilityNodeInfo.ACTION_CLICK,
                    activity
                        .getResources()
                        .getString(R.string.a11y_new_call_log_entry_tap_action)));
          }
        });
  }

  private void setNumberCalls(CoalescedRow row) {
    int numberCalls = row.getCoalescedIds().getCoalescedIdCount();
    if (numberCalls > 1) {
      callCountTextView.setText(String.format(Locale.getDefault(), "(%d)", numberCalls));
      callCountTextView.setVisibility(View.VISIBLE);
    } else {
      callCountTextView.setVisibility(View.GONE);
    }
  }

  private boolean isUnreadMissedCall(CoalescedRow row) {
    // Show missed call styling if the most recent call in the group was missed and it is still
    // marked as not read. The "NEW" column is presumably used for notifications and voicemails
    // only.
    return row.getCallType() == Calls.MISSED_TYPE && !row.getIsRead();
  }

  private void setPhoto(CoalescedRow row) {
    contactPhotoView.setPhoto(PhotoInfoBuilder.fromCoalescedRow(row).build());
  }

  private void setFeatureIcons(CoalescedRow row) {
    ColorStateList colorStateList =
        ColorStateList.valueOf(
            activity.getColor(
                isUnreadMissedCall(row)
                    ? R.color.feature_icon_unread_color
                    : R.color.feature_icon_read_color));

    // Handle HD Icon
    if ((row.getFeatures() & Calls.FEATURES_HD_CALL) == Calls.FEATURES_HD_CALL) {
      hdIcon.setVisibility(View.VISIBLE);
      hdIcon.setImageTintList(colorStateList);
    } else {
      hdIcon.setVisibility(View.GONE);
    }

    // Handle Wifi Icon
    if (MotorolaUtils.shouldShowWifiIconInCallLog(activity, row.getFeatures())) {
      wifiIcon.setVisibility(View.VISIBLE);
      wifiIcon.setImageTintList(colorStateList);
    } else {
      wifiIcon.setVisibility(View.GONE);
    }

    // Handle Assisted Dialing Icon
    if ((row.getFeatures() & TelephonyManagerCompat.FEATURES_ASSISTED_DIALING)
        == TelephonyManagerCompat.FEATURES_ASSISTED_DIALING) {
      assistedDialIcon.setVisibility(View.VISIBLE);
      assistedDialIcon.setImageTintList(colorStateList);
    } else {
      assistedDialIcon.setVisibility(View.GONE);
    }
  }

  private void setCallTypeIcon(CoalescedRow row) {
    @DrawableRes int resId;
    switch (row.getCallType()) {
      case Calls.INCOMING_TYPE:
      case Calls.ANSWERED_EXTERNALLY_TYPE:
        resId = R.drawable.quantum_ic_call_received_vd_theme_24;
        break;
      case Calls.OUTGOING_TYPE:
        resId = R.drawable.quantum_ic_call_made_vd_theme_24;
        break;
      case Calls.MISSED_TYPE:
        resId = R.drawable.quantum_ic_call_missed_vd_theme_24;
        break;
      case Calls.VOICEMAIL_TYPE:
        throw new IllegalStateException("Voicemails not expected in call log");
      case Calls.BLOCKED_TYPE:
        resId = R.drawable.quantum_ic_block_vd_theme_24;
        break;
      default:
        // It is possible for users to end up with calls with unknown call types in their
        // call history, possibly due to 3rd party call log implementations (e.g. to
        // distinguish between rejected and missed calls). Instead of crashing, just
        // assume that all unknown call types are missed calls.
        resId = R.drawable.quantum_ic_call_missed_vd_theme_24;
        break;
    }
    callTypeIcon.setImageResource(resId);

    if (isUnreadMissedCall(row)) {
      callTypeIcon.setImageTintList(
          ColorStateList.valueOf(activity.getColor(R.color.call_type_icon_unread_color)));
    } else {
      callTypeIcon.setImageTintList(
          ColorStateList.valueOf(activity.getColor(R.color.call_type_icon_read_color)));
    }
  }

  private void setPhoneAccounts(CoalescedRow row) {
    PhoneAccountHandle phoneAccountHandle =
        TelecomUtil.composePhoneAccountHandle(
            row.getPhoneAccountComponentName(), row.getPhoneAccountId());
    if (phoneAccountHandle == null) {
      phoneAccountView.setVisibility(View.GONE);
      return;
    }

    String phoneAccountLabel = PhoneAccountUtils.getAccountLabel(activity, phoneAccountHandle);
    if (TextUtils.isEmpty(phoneAccountLabel)) {
      phoneAccountView.setVisibility(View.GONE);
      return;
    }

    @ColorInt
    int phoneAccountColor = PhoneAccountUtils.getAccountColor(activity, phoneAccountHandle);
    if (phoneAccountColor == PhoneAccount.NO_HIGHLIGHT_COLOR) {
      phoneAccountColor =
          activity
              .getResources()
              .getColor(R.color.dialer_secondary_text_color, activity.getTheme());
    }

    phoneAccountView.setText(phoneAccountLabel);
    phoneAccountView.setTextColor(phoneAccountColor);
    phoneAccountView.setVisibility(View.VISIBLE);
  }

  private void setOnClickListenerForRow(CoalescedRow row) {
    if (!PhoneNumberHelper.canPlaceCallsTo(
        row.getNumber().getNormalizedNumber(), row.getNumberPresentation())) {
      itemView.setOnClickListener(null);
      return;
    }
    itemView.setOnClickListener(view -> CallLogRowActions.startCallForRow(activity, row));
  }

  private void setOnClickListenerForMenuButon(CoalescedRow row) {
    menuButton.setOnClickListener(NewCallLogMenu.createOnClickListener(activity, row));
    menuButton.setContentDescription(
        activity
            .getResources()
            .getString(
                R.string.a11y_new_call_log_entry_expand_menu,
                CallLogEntryText.buildPrimaryText(activity, row)));
  }

  private class RealtimeRowFutureCallback implements FutureCallback<CoalescedRow> {
    private final CoalescedRow originalRow;

    RealtimeRowFutureCallback(CoalescedRow originalRow) {
      this.originalRow = originalRow;
    }

    @Override
    public void onSuccess(CoalescedRow updatedRow) {
      // If the user scrolled then this ViewHolder may not correspond to the completed task and
      // there's nothing to do.
      if (originalRow.getId() != currentRowId) {
        popCounts.didNotPop++;
        return;
      }
      // Only update the UI if the updated row differs from the original row (which has already
      // been displayed).
      if (!updatedRow.equals(originalRow)) {
        displayRow(updatedRow);
        popCounts.popped++;
        return;
      }
      popCounts.didNotPop++;
    }

    @Override
    public void onFailure(Throwable throwable) {
      throw new RuntimeException("realtime processing failed", throwable);
    }
  }
}