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
|
/*
* 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.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.dialer.calllog.CallLogComponent;
import com.android.dialer.calllog.RefreshAnnotatedCallLogReceiver;
import com.android.dialer.calllog.database.CallLogDatabaseComponent;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
import com.android.dialer.common.concurrent.DefaultFutureCallback;
import com.android.dialer.common.concurrent.DialerExecutorComponent;
import com.android.dialer.common.concurrent.SupportUiListener;
import com.android.dialer.common.concurrent.ThreadUtil;
import com.android.dialer.metrics.Metrics;
import com.android.dialer.metrics.MetricsComponent;
import com.android.dialer.metrics.jank.RecyclerViewJankLogger;
import com.android.dialer.util.PermissionsUtil;
import com.android.dialer.widget.EmptyContentView;
import com.android.dialer.widget.EmptyContentView.OnEmptyViewActionButtonClickedListener;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
/** The "new" call log fragment implementation, which is built on top of the annotated call log. */
public final class NewCallLogFragment extends Fragment implements LoaderCallbacks<Cursor> {
private static final int PHONE_PERMISSIONS_REQUEST_CODE = 1;
private static final int LOADER_ID = 0;
@VisibleForTesting
static final long MARK_ALL_CALLS_READ_WAIT_MILLIS = TimeUnit.SECONDS.toMillis(3);
private RecyclerView recyclerView;
private EmptyContentView emptyContentView;
private RefreshAnnotatedCallLogReceiver refreshAnnotatedCallLogReceiver;
private SupportUiListener<Cursor> coalesingAnnotatedCallLogListener;
private boolean shouldMarkCallsRead = false;
private final Runnable setShouldMarkCallsReadTrue = () -> shouldMarkCallsRead = true;
public NewCallLogFragment() {
LogUtil.enterBlock("NewCallLogFragment.NewCallLogFragment");
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LogUtil.enterBlock("NewCallLogFragment.onActivityCreated");
refreshAnnotatedCallLogReceiver = new RefreshAnnotatedCallLogReceiver(getContext());
}
@Override
public void onStart() {
super.onStart();
LogUtil.enterBlock("NewCallLogFragment.onStart");
}
@Override
public void onResume() {
super.onResume();
boolean isHidden = isHidden();
LogUtil.i("NewCallLogFragment.onResume", "isHidden = %s", isHidden);
// As a fragment's onResume() is tied to the containing Activity's onResume(), being resumed is
// not equivalent to becoming visible.
// For example, when an activity with a hidden fragment is resumed, the fragment's onResume()
// will be called but it is not visible.
if (!isHidden) {
onFragmentShown();
}
}
@Override
public void onStop() {
super.onStop();
if (recyclerView.getAdapter() != null) {
((NewCallLogAdapter) recyclerView.getAdapter()).logMetrics(getContext());
}
}
@Override
public void onPause() {
super.onPause();
LogUtil.enterBlock("NewCallLogFragment.onPause");
onFragmentHidden();
}
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
LogUtil.i("NewCallLogFragment.onHiddenChanged", "hidden = %s", hidden);
if (hidden) {
onFragmentHidden();
} else {
onFragmentShown();
}
}
/**
* To be called when the fragment becomes visible.
*
* <p>Note that for a fragment, being resumed is not equivalent to becoming visible.
*
* <p>For example, when an activity with a hidden fragment is resumed, the fragment's onResume()
* will be called but it is not visible.
*/
private void onFragmentShown() {
LoaderManager loaderManager = getLoaderManager();
if (!PermissionsUtil.hasCallLogReadPermissions(getContext())) {
recyclerView.setVisibility(View.GONE);
emptyContentView.setVisibility(View.VISIBLE);
loaderManager.destroyLoader(LOADER_ID);
return;
}
recyclerView.setVisibility(View.VISIBLE);
emptyContentView.setVisibility(View.GONE);
// This can happen if permissions were not enabled when the fragment was created.
if (loaderManager.getLoader(LOADER_ID) == null) {
loaderManager.restartLoader(LOADER_ID, null, this);
}
registerRefreshAnnotatedCallLogReceiver();
CallLogComponent.get(getContext())
.getRefreshAnnotatedCallLogNotifier()
.notify(/* checkDirty = */ true);
// There are some types of data that we show in the call log that are not represented in the
// AnnotatedCallLog. For example, CP2 information for invalid numbers can sometimes only be
// fetched at display time. Because of this, we need to clear the adapter's cache and update it
// whenever the user arrives at the call log (rather than relying on changes to the CursorLoader
// alone).
if (recyclerView.getAdapter() != null) {
((NewCallLogAdapter) recyclerView.getAdapter()).clearCache();
recyclerView.getAdapter().notifyDataSetChanged();
}
// We shouldn't mark the calls as read immediately when the 3 second timer expires because we
// don't want to disrupt the UI; instead we set a bit indicating to mark them read when the user
// leaves the fragment (in onPause).
shouldMarkCallsRead = false;
ThreadUtil.getUiThreadHandler()
.postDelayed(setShouldMarkCallsReadTrue, MARK_ALL_CALLS_READ_WAIT_MILLIS);
}
/**
* To be called when the fragment becomes hidden.
*
* <p>This can happen in the following two cases:
*
* <ul>
* <li>hide the fragment but keep the parent activity visible (e.g., calling {@link
* android.support.v4.app.FragmentTransaction#hide(Fragment)} in an activity, or
* <li>the parent activity is paused.
* </ul>
*/
private void onFragmentHidden() {
// This is pending work that we don't actually need to follow through with.
ThreadUtil.getUiThreadHandler().removeCallbacks(setShouldMarkCallsReadTrue);
unregisterRefreshAnnotatedCallLogReceiver();
if (shouldMarkCallsRead) {
Futures.addCallback(
CallLogComponent.get(getContext()).getClearMissedCalls().clearAll(),
new DefaultFutureCallback<>(),
MoreExecutors.directExecutor());
}
}
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LogUtil.enterBlock("NewCallLogFragment.onCreateView");
View view = inflater.inflate(R.layout.new_call_log_fragment, container, false);
recyclerView = view.findViewById(R.id.new_call_log_recycler_view);
recyclerView.addOnScrollListener(
new RecyclerViewJankLogger(
MetricsComponent.get(getContext()).metrics(), Metrics.NEW_CALL_LOG_JANK_EVENT_NAME));
emptyContentView = view.findViewById(R.id.new_call_log_empty_content_view);
configureEmptyContentView();
coalesingAnnotatedCallLogListener =
DialerExecutorComponent.get(getContext())
.createUiListener(
getChildFragmentManager(),
/* taskId = */ "NewCallLogFragment.coalescingAnnotatedCallLog");
if (PermissionsUtil.hasCallLogReadPermissions(getContext())) {
getLoaderManager().restartLoader(LOADER_ID, null, this);
}
return view;
}
private void configureEmptyContentView() {
emptyContentView.setImage(R.drawable.quantum_ic_query_builder_vd_theme_24);
emptyContentView.setImageTint(R.color.empty_call_log_icon_tint_color, null);
emptyContentView.setDescription(R.string.new_call_log_permission_no_calllog);
emptyContentView.setActionLabel(com.android.dialer.widget.R.string.permission_single_turn_on);
emptyContentView.setActionClickedListener(new TurnOnPhonePermissions());
}
private class TurnOnPhonePermissions implements OnEmptyViewActionButtonClickedListener {
@Override
public void onEmptyViewActionButtonClicked() {
if (getContext() == null) {
LogUtil.w("TurnOnPhonePermissions.onEmptyViewActionButtonClicked", "no context");
return;
}
String[] deniedPermissions =
PermissionsUtil.getPermissionsCurrentlyDenied(
getContext(), PermissionsUtil.allPhoneGroupPermissionsUsedInDialer);
if (deniedPermissions.length > 0) {
LogUtil.i(
"TurnOnPhonePermissions.onEmptyViewActionButtonClicked",
"requesting permissions: %s",
Arrays.toString(deniedPermissions));
// Don't implement onRequestPermissionsResult; instead rely on views being updated in
// #onFragmentShown.
requestPermissions(deniedPermissions, PHONE_PERMISSIONS_REQUEST_CODE);
}
}
}
private void registerRefreshAnnotatedCallLogReceiver() {
LogUtil.enterBlock("NewCallLogFragment.registerRefreshAnnotatedCallLogReceiver");
LocalBroadcastManager.getInstance(getContext())
.registerReceiver(
refreshAnnotatedCallLogReceiver, RefreshAnnotatedCallLogReceiver.getIntentFilter());
}
private void unregisterRefreshAnnotatedCallLogReceiver() {
LogUtil.enterBlock("NewCallLogFragment.unregisterRefreshAnnotatedCallLogReceiver");
// Cancel pending work as we don't need it any more.
CallLogComponent.get(getContext()).getRefreshAnnotatedCallLogNotifier().cancel();
LocalBroadcastManager.getInstance(getContext())
.unregisterReceiver(refreshAnnotatedCallLogReceiver);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
LogUtil.enterBlock("NewCallLogFragment.onCreateLoader");
return new AnnotatedCallLogCursorLoader(Assert.isNotNull(getContext()));
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor newCursor) {
LogUtil.enterBlock("NewCallLogFragment.onLoadFinished");
if (newCursor == null) {
// This might be possible when the annotated call log hasn't been created but we're trying
// to show the call log.
LogUtil.w("NewCallLogFragment.onLoadFinished", "null cursor");
return;
}
// Start combining adjacent rows which should be collapsed for display purposes.
// This is a time-consuming process so we will do it in the background.
ListenableFuture<Cursor> coalescedCursorFuture =
CallLogDatabaseComponent.get(getContext()).coalescer().coalesce(newCursor);
coalesingAnnotatedCallLogListener.listen(
getContext(),
coalescedCursorFuture,
coalescedCursor -> {
LogUtil.i("NewCallLogFragment.onLoadFinished", "coalescing succeeded");
// TODO(zachh): Handle empty cursor by showing empty view.
if (recyclerView.getAdapter() == null) {
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
// Note: It's not clear if this callback can be invoked when there's no associated
// activity, but if crashes are observed here it may be possible to use getContext()
// instead.
Activity activity = Assert.isNotNull(getActivity());
recyclerView.setAdapter(
new NewCallLogAdapter(activity, coalescedCursor, System::currentTimeMillis));
} else {
((NewCallLogAdapter) recyclerView.getAdapter()).updateCursor(coalescedCursor);
}
},
throwable -> {
// Coalescing can fail if the cursor passed to Coalescer is closed by the loader while
// the work is still in progress.
// This can happen when the loader restarts and finishes loading data before the
// coalescing work is completed.
// TODO(linyuh): throw an exception here if the failure above can be avoided.
LogUtil.e("NewCallLogFragment.onLoadFinished", "coalescing failed", throwable);
});
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
LogUtil.enterBlock("NewCallLogFragment.onLoaderReset");
recyclerView.setAdapter(null);
}
}
|