1 /*
2  * Copyright (C) 2012 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.contacts.common.util;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.util.Log;
23 import android.view.View;
24 import android.widget.TextView;
25 import com.android.contacts.common.R;
26 import com.android.contacts.common.list.ContactListFilter;
27 import com.android.contacts.common.list.ContactListFilterController;
28 
29 /** Utility class for account filter manipulation. */
30 public class AccountFilterUtil {
31 
32   public static final String EXTRA_CONTACT_LIST_FILTER = "contactListFilter";
33   private static final String TAG = AccountFilterUtil.class.getSimpleName();
34 
35   /**
36    * Find TextView with the id "account_filter_header" and set correct text for the account filter
37    * header.
38    *
39    * @param filterContainer View containing TextView with id "account_filter_header"
40    * @return true when header text is set in the call. You may use this for conditionally showing or
41    *     hiding this entire view.
42    */
updateAccountFilterTitleForPeople( View filterContainer, ContactListFilter filter, boolean showTitleForAllAccounts)43   public static boolean updateAccountFilterTitleForPeople(
44       View filterContainer, ContactListFilter filter, boolean showTitleForAllAccounts) {
45     return updateAccountFilterTitle(filterContainer, filter, showTitleForAllAccounts, false);
46   }
47 
48   /**
49    * Similar to {@link #updateAccountFilterTitleForPeople(View, ContactListFilter, boolean,
50    * boolean)}, but for Phone UI.
51    */
updateAccountFilterTitleForPhone( View filterContainer, ContactListFilter filter, boolean showTitleForAllAccounts)52   public static boolean updateAccountFilterTitleForPhone(
53       View filterContainer, ContactListFilter filter, boolean showTitleForAllAccounts) {
54     return updateAccountFilterTitle(filterContainer, filter, showTitleForAllAccounts, true);
55   }
56 
updateAccountFilterTitle( View filterContainer, ContactListFilter filter, boolean showTitleForAllAccounts, boolean forPhone)57   private static boolean updateAccountFilterTitle(
58       View filterContainer,
59       ContactListFilter filter,
60       boolean showTitleForAllAccounts,
61       boolean forPhone) {
62     final Context context = filterContainer.getContext();
63     final TextView headerTextView =
64         (TextView) filterContainer.findViewById(R.id.account_filter_header);
65 
66     boolean textWasSet = false;
67     if (filter != null) {
68       if (forPhone) {
69         if (filter.filterType == ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS) {
70           if (showTitleForAllAccounts) {
71             headerTextView.setText(R.string.list_filter_phones);
72             textWasSet = true;
73           }
74         } else if (filter.filterType == ContactListFilter.FILTER_TYPE_ACCOUNT) {
75           headerTextView.setText(
76               context.getString(R.string.listAllContactsInAccount, filter.accountName));
77           textWasSet = true;
78         } else if (filter.filterType == ContactListFilter.FILTER_TYPE_CUSTOM) {
79           headerTextView.setText(R.string.listCustomView);
80           textWasSet = true;
81         } else {
82           Log.w(TAG, "Filter type \"" + filter.filterType + "\" isn't expected.");
83         }
84       } else {
85         if (filter.filterType == ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS) {
86           if (showTitleForAllAccounts) {
87             headerTextView.setText(R.string.list_filter_all_accounts);
88             textWasSet = true;
89           }
90         } else if (filter.filterType == ContactListFilter.FILTER_TYPE_ACCOUNT) {
91           headerTextView.setText(
92               context.getString(R.string.listAllContactsInAccount, filter.accountName));
93           textWasSet = true;
94         } else if (filter.filterType == ContactListFilter.FILTER_TYPE_CUSTOM) {
95           headerTextView.setText(R.string.listCustomView);
96           textWasSet = true;
97         } else if (filter.filterType == ContactListFilter.FILTER_TYPE_SINGLE_CONTACT) {
98           headerTextView.setText(R.string.listSingleContact);
99           textWasSet = true;
100         } else {
101           Log.w(TAG, "Filter type \"" + filter.filterType + "\" isn't expected.");
102         }
103       }
104     } else {
105       Log.w(TAG, "Filter is null.");
106     }
107     return textWasSet;
108   }
109 
110   /** This will update filter via a given ContactListFilterController. */
handleAccountFilterResult( ContactListFilterController filterController, int resultCode, Intent data)111   public static void handleAccountFilterResult(
112       ContactListFilterController filterController, int resultCode, Intent data) {
113     if (resultCode == Activity.RESULT_OK) {
114       final ContactListFilter filter = data.getParcelableExtra(EXTRA_CONTACT_LIST_FILTER);
115       if (filter == null) {
116         return;
117       }
118       if (filter.filterType == ContactListFilter.FILTER_TYPE_CUSTOM) {
119         filterController.selectCustomFilter();
120       } else {
121         filterController.setContactListFilter(filter, true);
122       }
123     }
124   }
125 }
126