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.app.Fragment;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.util.Log;
24 import android.view.View;
25 import android.widget.TextView;
26 
27 import com.android.contacts.common.R;
28 import com.android.contacts.common.list.AccountFilterActivity;
29 import com.android.contacts.common.list.ContactListFilter;
30 import com.android.contacts.common.list.ContactListFilterController;
31 
32 /**
33  * Utility class for account filter manipulation.
34  */
35 public class AccountFilterUtil {
36     private static final String TAG = AccountFilterUtil.class.getSimpleName();
37 
38     /**
39      * Find TextView with the id "account_filter_header" and set correct text for the account
40      * filter header.
41      *
42      * @param filterContainer View containing TextView with id "account_filter_header"
43      * @return true when header text is set in the call. You may use this for conditionally
44      * showing or hiding this entire view.
45      */
updateAccountFilterTitleForPeople(View filterContainer, ContactListFilter filter, boolean showTitleForAllAccounts)46     public static boolean updateAccountFilterTitleForPeople(View filterContainer,
47             ContactListFilter filter, boolean showTitleForAllAccounts) {
48         return updateAccountFilterTitle(filterContainer, filter, showTitleForAllAccounts, false);
49     }
50 
51     /**
52      * Similar to {@link #updateAccountFilterTitleForPeople(View, ContactListFilter, boolean,
53      * boolean)}, but for Phone UI.
54      */
updateAccountFilterTitleForPhone(View filterContainer, ContactListFilter filter, boolean showTitleForAllAccounts)55     public static boolean updateAccountFilterTitleForPhone(View filterContainer,
56             ContactListFilter filter, boolean showTitleForAllAccounts) {
57         return updateAccountFilterTitle(
58                 filterContainer, filter, showTitleForAllAccounts, true);
59     }
60 
updateAccountFilterTitle(View filterContainer, ContactListFilter filter, boolean showTitleForAllAccounts, boolean forPhone)61     private static boolean updateAccountFilterTitle(View filterContainer,
62             ContactListFilter filter, boolean showTitleForAllAccounts,
63             boolean forPhone) {
64         final Context context = filterContainer.getContext();
65         final TextView headerTextView = (TextView)
66                 filterContainer.findViewById(R.id.account_filter_header);
67 
68         boolean textWasSet = false;
69         if (filter != null) {
70             if (forPhone) {
71                 if (filter.filterType == ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS) {
72                     if (showTitleForAllAccounts) {
73                         headerTextView.setText(R.string.list_filter_phones);
74                         textWasSet = true;
75                     }
76                 } else if (filter.filterType == ContactListFilter.FILTER_TYPE_ACCOUNT) {
77                     headerTextView.setText(context.getString(
78                             R.string.listAllContactsInAccount, filter.accountName));
79                     textWasSet = true;
80                 } else if (filter.filterType == ContactListFilter.FILTER_TYPE_CUSTOM) {
81                     headerTextView.setText(R.string.listCustomView);
82                     textWasSet = true;
83                 } else {
84                     Log.w(TAG, "Filter type \"" + filter.filterType + "\" isn't expected.");
85                 }
86             } else {
87                 if (filter.filterType == ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS) {
88                     if (showTitleForAllAccounts) {
89                         headerTextView.setText(R.string.list_filter_all_accounts);
90                         textWasSet = true;
91                     }
92                 } else if (filter.filterType == ContactListFilter.FILTER_TYPE_ACCOUNT) {
93                     headerTextView.setText(context.getString(
94                             R.string.listAllContactsInAccount, filter.accountName));
95                     textWasSet = true;
96                 } else if (filter.filterType == ContactListFilter.FILTER_TYPE_CUSTOM) {
97                     headerTextView.setText(R.string.listCustomView);
98                     textWasSet = true;
99                 } else if (filter.filterType == ContactListFilter.FILTER_TYPE_SINGLE_CONTACT) {
100                     headerTextView.setText(R.string.listSingleContact);
101                     textWasSet = true;
102                 } else {
103                     Log.w(TAG, "Filter type \"" + filter.filterType + "\" isn't expected.");
104                 }
105             }
106         } else {
107             Log.w(TAG, "Filter is null.");
108         }
109         return textWasSet;
110     }
111 
112     /**
113      * Launches account filter setting Activity using
114      * {@link Activity#startActivityForResult(Intent, int)}.
115      *
116      * @param activity
117      * @param requestCode requestCode for {@link Activity#startActivityForResult(Intent, int)}
118      * @param currentFilter currently-selected filter, so that it can be displayed as activated.
119      */
startAccountFilterActivityForResult( Activity activity, int requestCode, ContactListFilter currentFilter)120     public static void startAccountFilterActivityForResult(
121             Activity activity, int requestCode, ContactListFilter currentFilter) {
122         final Intent intent = new Intent(activity, AccountFilterActivity.class);
123         intent.putExtra(AccountFilterActivity.KEY_EXTRA_CURRENT_FILTER, currentFilter);
124         activity.startActivityForResult(intent, requestCode);
125     }
126 
127     /**
128      * Very similar to
129      * {@link #startAccountFilterActivityForResult(Activity, int, ContactListFilter)}
130      * but uses Fragment instead.
131      */
startAccountFilterActivityForResult( Fragment fragment, int requestCode, ContactListFilter currentFilter)132     public static void startAccountFilterActivityForResult(
133             Fragment fragment, int requestCode, ContactListFilter currentFilter) {
134         final Activity activity = fragment.getActivity();
135         if (activity != null) {
136             final Intent intent = new Intent(activity, AccountFilterActivity.class);
137             intent.putExtra(AccountFilterActivity.KEY_EXTRA_CURRENT_FILTER, currentFilter);
138             fragment.startActivityForResult(intent, requestCode);
139         } else {
140             Log.w(TAG, "getActivity() returned null. Ignored");
141         }
142     }
143 
144     /**
145      * Useful method to handle onActivityResult() for
146      * {@link #startAccountFilterActivityForResult(Activity, int)} or
147      * {@link #startAccountFilterActivityForResult(Fragment, int)}.
148      *
149      * This will update filter via a given ContactListFilterController.
150      */
handleAccountFilterResult( ContactListFilterController filterController, int resultCode, Intent data)151     public static void handleAccountFilterResult(
152             ContactListFilterController filterController, int resultCode, Intent data) {
153         if (resultCode == Activity.RESULT_OK) {
154             final ContactListFilter filter = (ContactListFilter)
155                     data.getParcelableExtra(AccountFilterActivity.KEY_EXTRA_CONTACT_LIST_FILTER);
156             if (filter == null) {
157                 return;
158             }
159             if (filter.filterType == ContactListFilter.FILTER_TYPE_CUSTOM) {
160                 filterController.selectCustomFilter();
161             } else {
162                 filterController.setContactListFilter(filter, true);
163             }
164         }
165     }
166 }
167