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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
/*
* 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.speeddial;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.dialer.callintent.CallInitiationType;
import com.android.dialer.callintent.CallIntentBuilder;
import com.android.dialer.common.Assert;
import com.android.dialer.common.FragmentUtils;
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.constants.ActivityRequestCodes;
import com.android.dialer.historyitemactions.DividerModule;
import com.android.dialer.historyitemactions.HistoryItemActionBottomSheet;
import com.android.dialer.historyitemactions.HistoryItemActionModule;
import com.android.dialer.historyitemactions.HistoryItemBottomSheetHeaderInfo;
import com.android.dialer.historyitemactions.IntentModule;
import com.android.dialer.logging.DialerImpression;
import com.android.dialer.logging.Logger;
import com.android.dialer.precall.PreCall;
import com.android.dialer.speeddial.ContextMenu.ContextMenuItemListener;
import com.android.dialer.speeddial.FavoritesViewHolder.FavoriteContactsListener;
import com.android.dialer.speeddial.HeaderViewHolder.SpeedDialHeaderListener;
import com.android.dialer.speeddial.SuggestionViewHolder.SuggestedContactsListener;
import com.android.dialer.speeddial.database.SpeedDialEntry.Channel;
import com.android.dialer.speeddial.draghelper.SpeedDialItemTouchHelperCallback;
import com.android.dialer.speeddial.draghelper.SpeedDialLayoutManager;
import com.android.dialer.speeddial.loader.SpeedDialUiItem;
import com.android.dialer.speeddial.loader.UiItemLoaderComponent;
import com.android.dialer.util.IntentUtil;
import com.android.dialer.util.PermissionsUtil;
import com.android.dialer.widget.EmptyContentView;
import com.android.dialer.widget.EmptyContentView.OnEmptyViewActionButtonClickedListener;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.Futures;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Fragment for displaying:
*
* <ul>
* <li>Favorite/Starred contacts
* <li>Suggested contacts
* </ul>
*
* <p>Suggested contacts built from {@link android.provider.ContactsContract#STREQUENT_PHONE_ONLY}.
*/
public class SpeedDialFragment extends Fragment {
private static final int READ_CONTACTS_PERMISSION_REQUEST_CODE = 1;
/**
* Listen to broadcast events about permissions in order to be notified if the READ_CONTACTS
* permission is granted via the UI in another fragment.
*/
private final BroadcastReceiver readContactsPermissionGrantedReceiver =
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
loadContacts();
}
};
private final SpeedDialHeaderListener headerListener = new SpeedDialFragmentHeaderListener();
private final SpeedDialSuggestedListener suggestedListener = new SpeedDialSuggestedListener();
private SpeedDialAdapter adapter;
private SupportUiListener<ImmutableList<SpeedDialUiItem>> speedDialLoaderListener;
private SpeedDialFavoritesListener favoritesListener;
private EmptyContentView emptyContentView;
/**
* We update the UI every time the fragment is resumed. This boolean suppresses that functionality
* once per onResume call.
*/
private boolean updateSpeedDialItemsOnResume = true;
public static SpeedDialFragment newInstance() {
return new SpeedDialFragment();
}
@Nullable
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LogUtil.enterBlock("SpeedDialFragment.onCreateView");
View rootLayout = inflater.inflate(R.layout.fragment_speed_dial, container, false);
emptyContentView = rootLayout.findViewById(R.id.speed_dial_empty_content_view);
emptyContentView.setActionLabel(R.string.speed_dial_turn_on_contacts_permission);
emptyContentView.setDescription(R.string.speed_dial_contacts_permission_description);
emptyContentView.setImage(R.drawable.empty_speed_dial);
emptyContentView.setActionClickedListener(
new SpeedDialEmptyContentViewClickedListener(getContext(), this));
speedDialLoaderListener =
DialerExecutorComponent.get(getContext())
.createUiListener(getChildFragmentManager(), "speed_dial_loader_listener");
// Setup our RecyclerView
SpeedDialLayoutManager layoutManager =
new SpeedDialLayoutManager(getContext(), 3 /* spanCount */);
favoritesListener =
new SpeedDialFavoritesListener(
getActivity(),
getChildFragmentManager(),
new SpeedDialContextMenuItemListener(
getActivity(), new UpdateSpeedDialAdapterListener(), speedDialLoaderListener),
layoutManager);
adapter =
new SpeedDialAdapter(getContext(), favoritesListener, suggestedListener, headerListener);
layoutManager.setSpanSizeLookup(adapter.getSpanSizeLookup());
RecyclerView recyclerView = rootLayout.findViewById(R.id.speed_dial_recycler_view);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
// Setup drag and drop touch helper
ItemTouchHelper.Callback callback = new SpeedDialItemTouchHelperCallback(adapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(recyclerView);
adapter.setItemTouchHelper(touchHelper);
return rootLayout;
}
@Override
public void onResume() {
super.onResume();
loadContacts();
}
@Override
public void onPause() {
super.onPause();
favoritesListener.hideMenu();
suggestedListener.onPause();
}
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (hidden) {
onHidden();
} else {
loadContacts();
}
}
private void onHidden() {
if (!PermissionsUtil.hasContactsReadPermissions(getContext())) {
return;
}
Futures.addCallback(
DialerExecutorComponent.get(getContext())
.backgroundExecutor()
.submit(
() -> {
UiItemLoaderComponent.get(getContext())
.speedDialUiItemMutator()
.updatePinnedPosition(adapter.getSpeedDialUiItems());
return null;
}),
new DefaultFutureCallback<>(),
DialerExecutorComponent.get(getContext()).backgroundExecutor());
}
@Override
public void onRequestPermissionsResult(
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == READ_CONTACTS_PERMISSION_REQUEST_CODE
&& grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
PermissionsUtil.notifyPermissionGranted(getContext(), Manifest.permission.READ_CONTACTS);
loadContacts();
}
}
private void loadContacts() {
if (!updateSpeedDialItemsOnResume) {
updateSpeedDialItemsOnResume = true;
return;
}
if (!PermissionsUtil.hasContactsReadPermissions(getContext())) {
emptyContentView.setVisibility(View.VISIBLE);
return;
} else {
emptyContentView.setVisibility(View.GONE);
}
speedDialLoaderListener.listen(
getContext(),
UiItemLoaderComponent.get(getContext()).speedDialUiItemMutator().loadSpeedDialUiItems(),
this::onSpeedDialUiItemListLoaded,
throwable -> {
throw new RuntimeException(throwable);
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ActivityRequestCodes.SPEED_DIAL_ADD_FAVORITE) {
if (resultCode == AppCompatActivity.RESULT_OK && data.getData() != null) {
updateSpeedDialItemsOnResume = false;
speedDialLoaderListener.listen(
getContext(),
UiItemLoaderComponent.get(getContext())
.speedDialUiItemMutator()
.starContact(data.getData()),
this::onSpeedDialUiItemListLoaded,
throwable -> {
throw new RuntimeException(throwable);
});
}
}
}
private void onSpeedDialUiItemListLoaded(ImmutableList<SpeedDialUiItem> speedDialUiItems) {
LogUtil.enterBlock("SpeedDialFragment.onSpeedDialUiItemListLoaded");
// TODO(calderwoodra): Use DiffUtil to properly update and animate the change
adapter.setSpeedDialUiItems(
UiItemLoaderComponent.get(getContext())
.speedDialUiItemMutator()
.insertDuoChannels(getContext(), speedDialUiItems));
adapter.notifyDataSetChanged();
if (getActivity() != null) {
FragmentUtils.getParentUnsafe(this, HostInterface.class)
.setHasFrequents(adapter.hasFrequents());
}
}
@Override
public void onStart() {
super.onStart();
PermissionsUtil.registerPermissionReceiver(
getActivity(), readContactsPermissionGrantedReceiver, Manifest.permission.READ_CONTACTS);
}
@Override
public void onStop() {
super.onStop();
PermissionsUtil.unregisterPermissionReceiver(
getContext(), readContactsPermissionGrantedReceiver);
}
private class SpeedDialFragmentHeaderListener implements SpeedDialHeaderListener {
@Override
public void onAddFavoriteClicked() {
Intent intent = new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI);
startActivityForResult(intent, ActivityRequestCodes.SPEED_DIAL_ADD_FAVORITE);
}
}
private static final class SpeedDialFavoritesListener implements FavoriteContactsListener {
private final FragmentActivity activity;
private final FragmentManager childFragmentManager;
private final ContextMenuItemListener contextMenuListener;
private final SpeedDialLayoutManager layoutManager;
private ContextMenu contextMenu;
SpeedDialFavoritesListener(
FragmentActivity activity,
FragmentManager childFragmentManager,
ContextMenuItemListener contextMenuListener,
SpeedDialLayoutManager layoutManager) {
this.activity = activity;
this.childFragmentManager = childFragmentManager;
this.contextMenuListener = contextMenuListener;
this.layoutManager = layoutManager;
}
@Override
public void onAmbiguousContactClicked(SpeedDialUiItem speedDialUiItem) {
// If there is only one channel, skip the menu and place a call directly
if (speedDialUiItem.channels().size() == 1) {
onClick(speedDialUiItem.channels().get(0));
return;
}
DisambigDialog.show(speedDialUiItem, childFragmentManager);
}
@Override
public void onClick(Channel channel) {
if (channel.technology() == Channel.DUO) {
Logger.get(activity)
.logImpression(DialerImpression.Type.LIGHTBRINGER_VIDEO_REQUESTED_FOR_FAVORITE_CONTACT);
}
PreCall.start(
activity,
new CallIntentBuilder(channel.number(), CallInitiationType.Type.SPEED_DIAL)
.setIsVideoCall(channel.isVideoTechnology())
.setIsDuoCall(channel.technology() == Channel.DUO));
}
@Override
public void showContextMenu(View view, SpeedDialUiItem speedDialUiItem) {
layoutManager.setScrollEnabled(false);
contextMenu = ContextMenu.show(activity, view, contextMenuListener, speedDialUiItem);
}
@Override
public void onTouchFinished(boolean closeContextMenu) {
layoutManager.setScrollEnabled(true);
if (closeContextMenu) {
contextMenu.hide();
contextMenu = null;
}
}
public void hideMenu() {
if (contextMenu != null) {
contextMenu.hide();
contextMenu = null;
}
}
}
private final class SpeedDialSuggestedListener implements SuggestedContactsListener {
private HistoryItemActionBottomSheet bottomSheet;
@Override
public void onOverFlowMenuClicked(
SpeedDialUiItem speedDialUiItem, HistoryItemBottomSheetHeaderInfo headerInfo) {
List<HistoryItemActionModule> modules = new ArrayList<>();
Channel defaultChannel = speedDialUiItem.defaultChannel();
// Add voice call module
Channel voiceChannel = speedDialUiItem.getDefaultVoiceChannel();
if (voiceChannel != null) {
modules.add(
IntentModule.newCallModule(
getContext(),
new CallIntentBuilder(voiceChannel.number(), CallInitiationType.Type.SPEED_DIAL)
.setAllowAssistedDial(true)));
}
// Add video if we can determine the correct channel
Channel videoChannel = speedDialUiItem.getDefaultVideoChannel();
if (videoChannel != null) {
modules.add(
IntentModule.newCallModule(
getContext(),
new CallIntentBuilder(videoChannel.number(), CallInitiationType.Type.SPEED_DIAL)
.setIsVideoCall(true)
.setAllowAssistedDial(true)));
}
// Add sms module
if (!TextUtils.isEmpty(defaultChannel.number())) {
modules.add(
IntentModule.newModuleForSendingTextMessage(getContext(), defaultChannel.number()));
}
modules.add(new DividerModule());
modules.add(new StarContactModule(speedDialUiItem));
// TODO(calderwoodra): remove from strequent module
// Contact info module
modules.add(
new ContactInfoModule(
getContext(),
new Intent(
Intent.ACTION_VIEW,
Uri.withAppendedPath(
Contacts.CONTENT_URI, String.valueOf(speedDialUiItem.contactId()))),
R.string.contact_menu_contact_info,
R.drawable.context_menu_contact_icon));
bottomSheet = HistoryItemActionBottomSheet.show(getContext(), headerInfo, modules);
}
@Override
public void onRowClicked(Channel channel) {
if (channel.technology() == Channel.DUO) {
Logger.get(getContext())
.logImpression(
DialerImpression.Type.LIGHTBRINGER_VIDEO_REQUESTED_FOR_SUGGESTED_CONTACT);
}
PreCall.start(
getContext(),
new CallIntentBuilder(channel.number(), CallInitiationType.Type.SPEED_DIAL)
.setIsVideoCall(channel.isVideoTechnology())
.setIsDuoCall(channel.technology() == Channel.DUO));
}
private final class StarContactModule implements HistoryItemActionModule {
private final SpeedDialUiItem speedDialUiItem;
StarContactModule(SpeedDialUiItem speedDialUiItem) {
this.speedDialUiItem = speedDialUiItem;
}
@Override
public int getStringId() {
return R.string.suggested_contact_bottom_sheet_add_favorite_option;
}
@Override
public int getDrawableId() {
return R.drawable.quantum_ic_star_vd_theme_24;
}
@Override
public boolean onClick() {
speedDialLoaderListener.listen(
getContext(),
UiItemLoaderComponent.get(getContext())
.speedDialUiItemMutator()
.starContact(
Uri.withAppendedPath(
Phone.CONTENT_FILTER_URI, speedDialUiItem.defaultChannel().number())),
SpeedDialFragment.this::onSpeedDialUiItemListLoaded,
throwable -> {
throw new RuntimeException(throwable);
});
return true;
}
}
private final class ContactInfoModule extends IntentModule {
public ContactInfoModule(Context context, Intent intent, int text, int image) {
super(context, intent, text, image);
}
@Override
public boolean tintDrawable() {
return false;
}
}
public void onPause() {
if (bottomSheet != null && bottomSheet.isShowing()) {
bottomSheet.dismiss();
}
}
}
private static final class SpeedDialContextMenuItemListener implements ContextMenuItemListener {
private final FragmentActivity activity;
private final SupportUiListener<ImmutableList<SpeedDialUiItem>> speedDialLoaderListener;
private final UpdateSpeedDialAdapterListener updateAdapterListener;
SpeedDialContextMenuItemListener(
FragmentActivity activity,
UpdateSpeedDialAdapterListener updateAdapterListener,
SupportUiListener<ImmutableList<SpeedDialUiItem>> speedDialLoaderListener) {
this.activity = activity;
this.updateAdapterListener = updateAdapterListener;
this.speedDialLoaderListener = speedDialLoaderListener;
}
@Override
public void placeCall(Channel channel) {
if (channel.technology() == Channel.DUO) {
Logger.get(activity)
.logImpression(DialerImpression.Type.LIGHTBRINGER_VIDEO_REQUESTED_FOR_FAVORITE_CONTACT);
}
PreCall.start(
activity,
new CallIntentBuilder(channel.number(), CallInitiationType.Type.SPEED_DIAL)
.setIsVideoCall(channel.isVideoTechnology())
.setIsDuoCall(channel.technology() == Channel.DUO));
}
@Override
public void openSmsConversation(String number) {
activity.startActivity(IntentUtil.getSendSmsIntent(number));
}
@Override
public void removeFavoriteContact(SpeedDialUiItem speedDialUiItem) {
speedDialLoaderListener.listen(
activity,
UiItemLoaderComponent.get(activity)
.speedDialUiItemMutator()
.removeSpeedDialUiItem(speedDialUiItem),
updateAdapterListener::updateAdapter,
throwable -> {
throw new RuntimeException(throwable);
});
}
@Override
public void openContactInfo(SpeedDialUiItem speedDialUiItem) {
activity.startActivity(
new Intent(
Intent.ACTION_VIEW,
Uri.withAppendedPath(
Contacts.CONTENT_URI, String.valueOf(speedDialUiItem.contactId()))));
}
}
private static final class SpeedDialEmptyContentViewClickedListener
implements OnEmptyViewActionButtonClickedListener {
private final Context context;
private final Fragment fragment;
private SpeedDialEmptyContentViewClickedListener(Context context, Fragment fragment) {
this.context = context;
this.fragment = fragment;
}
@Override
public void onEmptyViewActionButtonClicked() {
String[] deniedPermissions =
PermissionsUtil.getPermissionsCurrentlyDenied(
context, PermissionsUtil.allContactsGroupPermissionsUsedInDialer);
Assert.checkArgument(deniedPermissions.length > 0);
LogUtil.i(
"OldSpeedDialFragment.onEmptyViewActionButtonClicked",
"Requesting permissions: " + Arrays.toString(deniedPermissions));
fragment.requestPermissions(deniedPermissions, READ_CONTACTS_PERMISSION_REQUEST_CODE);
}
}
/** Listener for when a SpeedDialUiItem is updated. */
private class UpdateSpeedDialAdapterListener {
void updateAdapter(ImmutableList<SpeedDialUiItem> speedDialUiItems) {
onSpeedDialUiItemListLoaded(speedDialUiItems);
}
}
/** Interface for {@link SpeedDialFragment} to communicate with its host/parent. */
public interface HostInterface {
void setHasFrequents(boolean hasFrequents);
}
}
|