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
17package com.android.dialer.speeddial;
18
19import android.content.ContentValues;
20import android.net.Uri;
21import android.os.Bundle;
22import android.provider.ContactsContract.Contacts;
23import android.support.annotation.Nullable;
24import android.support.annotation.WorkerThread;
25import android.support.v7.app.AppCompatActivity;
26import android.support.v7.widget.SearchView;
27import android.view.Menu;
28import android.view.MenuItem;
29import android.widget.ImageView;
30import com.android.dialer.common.Assert;
31import com.android.dialer.common.concurrent.DialerExecutorComponent;
32import com.android.dialer.contactsfragment.ContactsFragment;
33import com.android.dialer.contactsfragment.ContactsFragment.OnContactSelectedListener;
34
35/**
36 * Activity for selecting a single contact and adding it to favorites.
37 *
38 * <p>Contacts are displayed using {@link ContactsFragment}. Contacts are searchable via search bar
39 * in the toolbar. When a contact is selected, it's uri is passed back in the result data.
40 */
41public class AddFavoriteActivity extends AppCompatActivity implements OnContactSelectedListener {
42
43  private ContactsFragment contactsFragment;
44
45  @Override
46  protected void onCreate(@Nullable Bundle bundle) {
47    super.onCreate(bundle);
48    setContentView(R.layout.add_favorite_activity);
49    contactsFragment = ContactsFragment.newAddFavoritesInstance();
50    getFragmentManager()
51        .beginTransaction()
52        .add(R.id.add_favorite_container, contactsFragment, null)
53        .commit();
54  }
55
56  @Override
57  public boolean onCreateOptionsMenu(Menu menu) {
58    getMenuInflater().inflate(R.menu.add_favorite_menu, menu);
59
60    MenuItem searchItem = menu.findItem(R.id.action_search);
61    SearchView searchView = (SearchView) searchItem.getActionView();
62    searchView.setOnQueryTextListener(
63        new SearchView.OnQueryTextListener() {
64          @Override
65          public boolean onQueryTextSubmit(String query) {
66            if (!searchView.isIconified()) {
67              searchView.setIconified(true);
68            }
69            searchItem.collapseActionView();
70            return false;
71          }
72
73          @Override
74          public boolean onQueryTextChange(String s) {
75            contactsFragment.updateQuery(s);
76            return false;
77          }
78        });
79    return true;
80  }
81
82  @Override
83  public void onContactSelected(ImageView photo, Uri contactUri, long contactId) {
84    DialerExecutorComponent.get(this)
85        .dialerExecutorFactory()
86        .createUiTaskBuilder(
87            getFragmentManager(), "mark_contact_favorite", this::markContactStarred)
88        .onSuccess(output -> finish())
89        .onFailure(this::onContactStarredFailed)
90        .build()
91        .executeParallel(contactId);
92  }
93
94  @WorkerThread
95  private int markContactStarred(long contactId) {
96    // TODO(calderwoodra): For now, we will just mark contacts as starred. This means that contacts
97    // will only be able to exist once in favorites until we implement multiple contact avenues.
98    ContentValues contentValues = new ContentValues();
99    contentValues.put(Contacts.STARRED, 1);
100
101    String where = Contacts._ID + " = ?";
102    String[] selectionArgs = new String[] {Long.toString(contactId)};
103    return getContentResolver().update(Contacts.CONTENT_URI, contentValues, where, selectionArgs);
104  }
105
106  private void onContactStarredFailed(Throwable throwable) {
107    throw Assert.createAssertionFailException(throwable.getMessage());
108  }
109}
110