Home | History | Annotate | Download | only in speeddial
      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.speeddial;
     18 
     19 import android.app.Fragment;
     20 import android.app.LoaderManager.LoaderCallbacks;
     21 import android.content.Intent;
     22 import android.content.Loader;
     23 import android.database.Cursor;
     24 import android.os.Bundle;
     25 import android.support.annotation.Nullable;
     26 import android.support.v7.widget.RecyclerView;
     27 import android.view.LayoutInflater;
     28 import android.view.View;
     29 import android.view.ViewGroup;
     30 import com.android.dialer.callintent.CallInitiationType;
     31 import com.android.dialer.callintent.CallIntentBuilder;
     32 import com.android.dialer.common.Assert;
     33 import com.android.dialer.precall.PreCall;
     34 import com.android.dialer.speeddial.FavoritesViewHolder.FavoriteContactsListener;
     35 import com.android.dialer.speeddial.HeaderViewHolder.SpeedDialHeaderListener;
     36 import com.android.dialer.speeddial.SuggestionViewHolder.SuggestedContactsListener;
     37 
     38 /**
     39  * Fragment for displaying:
     40  *
     41  * <ul>
     42  *   <li>Favorite/Starred contacts
     43  *   <li>Suggested contacts
     44  * </ul>
     45  *
     46  * <p>Suggested contacts built from {@link android.provider.ContactsContract#STREQUENT_PHONE_ONLY}.
     47  */
     48 public class SpeedDialFragment extends Fragment {
     49 
     50   private static final int STREQUENT_CONTACTS_LOADER_ID = 1;
     51 
     52   private final SpeedDialHeaderListener headerListener = new SpeedDialFragmentHeaderListener();
     53   private final FavoriteContactsListener favoritesListener = new SpeedDialFavoritesListener();
     54   private final SuggestedContactsListener suggestedListener = new SpeedDialSuggestedListener();
     55   private final SpeedDialFragmentLoaderCallback loaderCallback =
     56       new SpeedDialFragmentLoaderCallback();
     57 
     58   private SpeedDialAdapter adapter;
     59 
     60   public static SpeedDialFragment newInstance() {
     61     return new SpeedDialFragment();
     62   }
     63 
     64   @Nullable
     65   @Override
     66   public View onCreateView(
     67       LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     68     View view = inflater.inflate(R.layout.fragment_speed_dial, container, false);
     69     RecyclerView recyclerView = view.findViewById(R.id.speed_dial_recycler_view);
     70 
     71     adapter =
     72         new SpeedDialAdapter(getContext(), favoritesListener, suggestedListener, headerListener);
     73     recyclerView.setLayoutManager(adapter.getLayoutManager(getContext()));
     74     recyclerView.setAdapter(adapter);
     75     getLoaderManager().initLoader(STREQUENT_CONTACTS_LOADER_ID, null /* args */, loaderCallback);
     76     return view;
     77   }
     78 
     79   public boolean hasFrequents() {
     80     // TODO(calderwoodra)
     81     return false;
     82   }
     83 
     84   @Override
     85   public void onResume() {
     86     super.onResume();
     87     getLoaderManager().restartLoader(STREQUENT_CONTACTS_LOADER_ID, null, loaderCallback);
     88   }
     89 
     90   private class SpeedDialFragmentHeaderListener implements SpeedDialHeaderListener {
     91 
     92     @Override
     93     public void onAddFavoriteClicked() {
     94       startActivity(new Intent(getContext(), AddFavoriteActivity.class));
     95     }
     96   }
     97 
     98   private class SpeedDialFavoritesListener implements FavoriteContactsListener {
     99 
    100     @Override
    101     public void onAmbiguousContactClicked(String lookupKey) {
    102       DisambigDialog.show(lookupKey, getFragmentManager());
    103     }
    104 
    105     @Override
    106     public void onClick(String number, boolean isVideoCall) {
    107       // TODO(calderwoodra): add logic for duo video calls
    108       PreCall.start(
    109           getContext(),
    110           new CallIntentBuilder(number, CallInitiationType.Type.SPEED_DIAL)
    111               .setIsVideoCall(isVideoCall));
    112     }
    113 
    114     @Override
    115     public void onLongClick(String number) {
    116       // TODO(calderwoodra): show favorite contact floating context menu
    117     }
    118   }
    119 
    120   private class SpeedDialSuggestedListener implements SuggestedContactsListener {
    121 
    122     @Override
    123     public void onOverFlowMenuClicked(String number) {
    124       // TODO(calderwoodra) show overflow menu for suggested contacts
    125     }
    126 
    127     @Override
    128     public void onRowClicked(String number) {
    129       PreCall.start(
    130           getContext(), new CallIntentBuilder(number, CallInitiationType.Type.SPEED_DIAL));
    131     }
    132   }
    133 
    134   /**
    135    * Loader callback that registers a content observer. {@link #unregisterContentObserver()} needs
    136    * to be called during tear down of the fragment.
    137    */
    138   private class SpeedDialFragmentLoaderCallback implements LoaderCallbacks<Cursor> {
    139 
    140     @Override
    141     public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    142       if (id == STREQUENT_CONTACTS_LOADER_ID) {
    143         return new StrequentContactsCursorLoader(getContext());
    144       }
    145       throw Assert.createIllegalStateFailException("Invalid loader id: " + id);
    146     }
    147 
    148     @Override
    149     public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    150       adapter.setCursor((SpeedDialCursor) data);
    151     }
    152 
    153     @Override
    154     public void onLoaderReset(Loader<Cursor> loader) {
    155       adapter.setCursor(null);
    156     }
    157   }
    158 }
    159