summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/calllog/ui/NewCallLogAdapter.java
blob: 501cf16572f8373e90ff2d4d0cfb6f7f018ae58f (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * 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.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.android.dialer.calllog.database.Coalescer;
import com.android.dialer.calllogutils.CallLogDates;
import com.android.dialer.common.Assert;
import com.android.dialer.duo.Duo;
import com.android.dialer.duo.DuoComponent;
import com.android.dialer.logging.Logger;
import com.android.dialer.promotion.RttPromotion;
import com.android.dialer.storage.StorageComponent;
import com.android.dialer.time.Clock;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.concurrent.TimeUnit;

/** {@link RecyclerView.Adapter} for the new call log fragment. */
final class NewCallLogAdapter extends RecyclerView.Adapter<ViewHolder> {

  @VisibleForTesting
  static final String SHARED_PREF_KEY_DUO_DISCLOSURE_DISMISSED = "duo_disclosure_dismissed";

  private static final String SHARED_PREF_KEY_DUO_DISCLOSURE_FIRST_VIEW_TIME_MILLIS =
      "duo_disclosure_first_viewed_time_ms";

  /** IntDef for the different types of rows that can be shown in the call log. */
  @Retention(RetentionPolicy.SOURCE)
  @IntDef({
    RowType.DUO_DISCLOSURE_CARD,
    RowType.HEADER_TODAY,
    RowType.HEADER_YESTERDAY,
    RowType.HEADER_OLDER,
    RowType.CALL_LOG_ENTRY
  })
  @interface RowType {
    /** The Duo disclosure card. */
    int DUO_DISCLOSURE_CARD = 1;

    /** Header that displays "Today". */
    int HEADER_TODAY = 2;

    /** Header that displays "Yesterday". */
    int HEADER_YESTERDAY = 3;

    /** Header that displays "Older". */
    int HEADER_OLDER = 4;

    /** A row representing a call log entry (which could represent one or more calls). */
    int CALL_LOG_ENTRY = 5;
  }

  private final Clock clock;
  private final Activity activity;
  private final RealtimeRowProcessor realtimeRowProcessor;
  private final PopCounts popCounts = new PopCounts();
  private final SharedPreferences sharedPref;
  private final OnScrollListenerForRecordingDuoDisclosureFirstViewTime
      onScrollListenerForRecordingDuoDisclosureFirstViewTime;

  private Cursor cursor;

  /** Position of the Duo disclosure card. Null when it should not be displayed. */
  @Nullable private Integer duoDisclosureCardPosition;

  /** Position of the "Today" header. Null when it should not be displayed. */
  @Nullable private Integer todayHeaderPosition;

  /** Position of the "Yesterday" header. Null when it should not be displayed. */
  @Nullable private Integer yesterdayHeaderPosition;

  /** Position of the "Older" header. Null when it should not be displayed. */
  @Nullable private Integer olderHeaderPosition;

  NewCallLogAdapter(Activity activity, Cursor cursor, Clock clock) {
    this.activity = activity;
    this.cursor = cursor;
    this.clock = clock;
    this.realtimeRowProcessor = CallLogUiComponent.get(activity).realtimeRowProcessor();
    this.sharedPref = StorageComponent.get(activity).unencryptedSharedPrefs();
    this.onScrollListenerForRecordingDuoDisclosureFirstViewTime =
        new OnScrollListenerForRecordingDuoDisclosureFirstViewTime(sharedPref, clock);

    setCardAndHeaderPositions();
  }

  void updateCursor(Cursor updatedCursor) {
    this.cursor = updatedCursor;
    this.realtimeRowProcessor.clearCache();
    this.popCounts.reset();

    setCardAndHeaderPositions();
    notifyDataSetChanged();
  }

  void clearCache() {
    this.realtimeRowProcessor.clearCache();
  }

  void logMetrics(Context context) {
    Logger.get(context).logAnnotatedCallLogMetrics(popCounts.popped, popCounts.didNotPop);
  }

  private void setCardAndHeaderPositions() {
    // Set the position for the Duo disclosure card if it should be shown.
    duoDisclosureCardPosition = null;
    int numCards = 0;
    if (shouldShowDuoDisclosureCard()) {
      duoDisclosureCardPosition = 0;
      numCards++;
    }

    // If there are no rows to display, set all header positions to null.
    if (!cursor.moveToFirst()) {
      todayHeaderPosition = null;
      yesterdayHeaderPosition = null;
      olderHeaderPosition = null;
      return;
    }

    // Calculate positions for headers.
    long currentTimeMillis = clock.currentTimeMillis();

    int numItemsInToday = 0;
    int numItemsInYesterday = 0;
    do {
      long timestamp = Coalescer.getTimestamp(cursor);
      long dayDifference = CallLogDates.getDayDifference(currentTimeMillis, timestamp);
      if (dayDifference == 0) {
        numItemsInToday++;
      } else if (dayDifference == 1) {
        numItemsInYesterday++;
      } else {
        break;
      }
    } while (cursor.moveToNext());

    if (numItemsInToday > 0) {
      numItemsInToday++; // including the "Today" header;
    }
    if (numItemsInYesterday > 0) {
      numItemsInYesterday++; // including the "Yesterday" header;
    }

    // Set all header positions.
    // A header position will be null if there is no item to be displayed under that header.
    todayHeaderPosition = numItemsInToday > 0 ? numCards : null;
    yesterdayHeaderPosition = numItemsInYesterday > 0 ? numItemsInToday + numCards : null;
    olderHeaderPosition =
        !cursor.isAfterLast() ? numItemsInToday + numItemsInYesterday + numCards : null;
  }

  private boolean shouldShowDuoDisclosureCard() {
    if (new RttPromotion(activity).shouldShow()) {
      return false;
    }
    // Don't show the Duo disclosure card if
    // (1) Duo integration is not enabled on the device, or
    // (2) Duo is not activated.
    Duo duo = DuoComponent.get(activity).getDuo();
    if (!duo.isEnabled(activity) || !duo.isActivated(activity)) {
      return false;
    }

    // Don't show the Duo disclosure card if it has been dismissed.
    if (sharedPref.getBoolean(SHARED_PREF_KEY_DUO_DISCLOSURE_DISMISSED, false)) {
      return false;
    }

    // At this point, Duo is activated and the disclosure card hasn't been dismissed.
    // We should show the card if it has never been viewed by the user.
    if (!sharedPref.contains(SHARED_PREF_KEY_DUO_DISCLOSURE_FIRST_VIEW_TIME_MILLIS)) {
      return true;
    }

    // At this point, the card has been viewed but not dismissed.
    // We should not show the card if it has been viewed for more than 1 day.
    long duoDisclosureFirstViewTimeMillis =
        sharedPref.getLong(SHARED_PREF_KEY_DUO_DISCLOSURE_FIRST_VIEW_TIME_MILLIS, 0);
    return clock.currentTimeMillis() - duoDisclosureFirstViewTimeMillis
        <= TimeUnit.DAYS.toMillis(1);
  }

  @Override
  public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);

    // Register a OnScrollListener that records the timestamp at which the Duo disclosure is first
    // viewed if
    // (1) the Duo disclosure card should be shown, and
    // (2) it hasn't been viewed yet.
    if (shouldShowDuoDisclosureCard()
        && !sharedPref.contains(SHARED_PREF_KEY_DUO_DISCLOSURE_FIRST_VIEW_TIME_MILLIS)) {
      recyclerView.addOnScrollListener(onScrollListenerForRecordingDuoDisclosureFirstViewTime);
    }
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup viewGroup, @RowType int viewType) {
    switch (viewType) {
      case RowType.DUO_DISCLOSURE_CARD:
        return new DuoDisclosureCardViewHolder(
            LayoutInflater.from(activity)
                .inflate(
                    R.layout.new_call_log_duo_disclosure_card,
                    viewGroup,
                    /* attachToRoot = */ false));
      case RowType.HEADER_TODAY:
      case RowType.HEADER_YESTERDAY:
      case RowType.HEADER_OLDER:
        return new HeaderViewHolder(
            LayoutInflater.from(activity)
                .inflate(R.layout.new_call_log_header, viewGroup, /* attachToRoot = */ false));
      case RowType.CALL_LOG_ENTRY:
        return new NewCallLogViewHolder(
            activity,
            LayoutInflater.from(activity)
                .inflate(R.layout.new_call_log_entry, viewGroup, /* attachToRoot = */ false),
            clock,
            realtimeRowProcessor,
            popCounts);
      default:
        throw Assert.createUnsupportedOperationFailException("Unsupported view type: " + viewType);
    }
  }

  @Override
  public void onBindViewHolder(ViewHolder viewHolder, int position) {
    @RowType int viewType = getItemViewType(position);
    switch (viewType) {
      case RowType.DUO_DISCLOSURE_CARD:
        ((DuoDisclosureCardViewHolder) viewHolder)
            .setDismissListener(
                unused -> {
                  StorageComponent.get(activity)
                      .unencryptedSharedPrefs()
                      .edit()
                      .putBoolean(SHARED_PREF_KEY_DUO_DISCLOSURE_DISMISSED, true)
                      .apply();
                  notifyItemRemoved(duoDisclosureCardPosition);
                  setCardAndHeaderPositions();
                });
        break;
      case RowType.HEADER_TODAY:
        ((HeaderViewHolder) viewHolder).setHeader(R.string.new_call_log_header_today);
        break;
      case RowType.HEADER_YESTERDAY:
        ((HeaderViewHolder) viewHolder).setHeader(R.string.new_call_log_header_yesterday);
        break;
      case RowType.HEADER_OLDER:
        ((HeaderViewHolder) viewHolder).setHeader(R.string.new_call_log_header_older);
        break;
      case RowType.CALL_LOG_ENTRY:
        NewCallLogViewHolder newCallLogViewHolder = (NewCallLogViewHolder) viewHolder;
        int previousCardAndHeaders = 0;
        if (duoDisclosureCardPosition != null && position > duoDisclosureCardPosition) {
          previousCardAndHeaders++;
        }
        if (todayHeaderPosition != null && position > todayHeaderPosition) {
          previousCardAndHeaders++;
        }
        if (yesterdayHeaderPosition != null && position > yesterdayHeaderPosition) {
          previousCardAndHeaders++;
        }
        if (olderHeaderPosition != null && position > olderHeaderPosition) {
          previousCardAndHeaders++;
        }
        cursor.moveToPosition(position - previousCardAndHeaders);
        newCallLogViewHolder.bind(cursor);
        break;
      default:
        throw Assert.createIllegalStateFailException(
            "Unexpected view type " + viewType + " at position: " + position);
    }
  }

  @Override
  @RowType
  public int getItemViewType(int position) {
    if (duoDisclosureCardPosition != null && position == duoDisclosureCardPosition) {
      return RowType.DUO_DISCLOSURE_CARD;
    }
    if (todayHeaderPosition != null && position == todayHeaderPosition) {
      return RowType.HEADER_TODAY;
    }
    if (yesterdayHeaderPosition != null && position == yesterdayHeaderPosition) {
      return RowType.HEADER_YESTERDAY;
    }
    if (olderHeaderPosition != null && position == olderHeaderPosition) {
      return RowType.HEADER_OLDER;
    }
    return RowType.CALL_LOG_ENTRY;
  }

  @Override
  public int getItemCount() {
    int numberOfCards = 0;
    int numberOfHeaders = 0;

    if (duoDisclosureCardPosition != null) {
      numberOfCards++;
    }
    if (todayHeaderPosition != null) {
      numberOfHeaders++;
    }
    if (yesterdayHeaderPosition != null) {
      numberOfHeaders++;
    }
    if (olderHeaderPosition != null) {
      numberOfHeaders++;
    }
    return cursor.getCount() + numberOfHeaders + numberOfCards;
  }

  /**
   * A {@link RecyclerView.OnScrollListener} that records the timestamp at which the Duo disclosure
   * card is first viewed.
   *
   * <p>We consider the card as viewed if the user scrolls the containing RecyclerView since such
   * action is a strong proof.
   */
  private static final class OnScrollListenerForRecordingDuoDisclosureFirstViewTime
      extends RecyclerView.OnScrollListener {

    private final SharedPreferences sharedPref;
    private final Clock clock;

    OnScrollListenerForRecordingDuoDisclosureFirstViewTime(
        SharedPreferences sharedPref, Clock clock) {
      this.sharedPref = sharedPref;
      this.clock = clock;
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
      if (!sharedPref.contains(SHARED_PREF_KEY_DUO_DISCLOSURE_FIRST_VIEW_TIME_MILLIS)
          && newState == RecyclerView.SCROLL_STATE_SETTLING) {
        sharedPref
            .edit()
            .putLong(
                SHARED_PREF_KEY_DUO_DISCLOSURE_FIRST_VIEW_TIME_MILLIS, clock.currentTimeMillis())
            .apply();

        // Recording the timestamp is this listener's sole responsibility.
        // We can remove it from the containing RecyclerView after the job is done.
        recyclerView.removeOnScrollListener(this);
      }

      super.onScrollStateChanged(recyclerView, newState);
    }
  }

  static class PopCounts {
    int popped;
    int didNotPop;

    private void reset() {
      popped = 0;
      didNotPop = 0;
    }
  }
}