1 /*
2  * Copyright (C) 2016 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 package com.android.contacts.list;
17 
18 import android.content.Intent;
19 import android.os.Bundle;
20 import android.text.TextUtils;
21 import android.view.LayoutInflater;
22 import android.view.Menu;
23 import android.view.MenuInflater;
24 import android.view.MenuItem;
25 import android.view.View;
26 import android.view.ViewGroup;
27 
28 import com.android.contacts.R;
29 import com.android.contacts.group.GroupUtil;
30 import com.android.contacts.logging.ListEvent;
31 
32 import java.util.List;
33 import java.util.TreeSet;
34 
35 /** Displays a list of emails with check boxes. */
36 public class MultiSelectEmailAddressesListFragment
37         extends MultiSelectContactsListFragment<MultiSelectEmailAddressesListAdapter>{
38 
MultiSelectEmailAddressesListFragment()39     public MultiSelectEmailAddressesListFragment() {
40         setPhotoLoaderEnabled(true);
41         setSectionHeaderDisplayEnabled(false);
42         setSearchMode(false);
43         setHasOptionsMenu(true);
44         setListType(ListEvent.ListType.PICK_EMAIL);
45     }
46 
47     @Override
createListAdapter()48     public MultiSelectEmailAddressesListAdapter createListAdapter() {
49         final MultiSelectEmailAddressesListAdapter adapter =
50                 new MultiSelectEmailAddressesListAdapter(getActivity());
51         adapter.setArguments(getArguments());
52         return adapter;
53     }
54 
55     @Override
onCreateOptionsMenu(Menu menu, final MenuInflater inflater)56     public void onCreateOptionsMenu(Menu menu, final MenuInflater inflater) {
57         super.onCreateOptionsMenu(menu, inflater);
58         inflater.inflate(R.menu.items_multi_select, menu);
59     }
60 
61     @Override
onPrepareOptionsMenu(Menu menu)62     public void onPrepareOptionsMenu(Menu menu) {
63         final MenuItem item = menu.findItem(R.id.menu_send);
64         item.setVisible(getAdapter().hasSelectedItems());
65         item.getActionView().setOnClickListener(new View.OnClickListener() {
66             @Override
67             public void onClick(View v) {
68                 onOptionsItemSelected(item);
69             }
70         });
71     }
72 
73     @Override
onActivityResult(int requestCode, int resultCode, Intent data)74     public void onActivityResult(int requestCode, int resultCode, Intent data) {
75         getActivity().finish();
76     }
77 
78     @Override
onOptionsItemSelected(MenuItem item)79     public boolean onOptionsItemSelected(MenuItem item) {
80         if (item.getItemId() == R.id.menu_send) {
81             final String scheme = getActivity().getIntent().getStringExtra(
82                     UiIntentActions.SELECTION_SEND_SCHEME);
83             final String title = getActivity().getIntent().getStringExtra(
84                     UiIntentActions.SELECTION_SEND_TITLE);
85             final List<String> items = GroupUtil.getSendToDataForIds(
86                     getActivity(), getAdapter().getSelectedContactIdsArray(), scheme);
87             final String list = TextUtils.join(",", items);
88             GroupUtil.startSendToSelectionActivity(this, list, scheme, title);
89             return true;
90         }
91         return super.onOptionsItemSelected(item);
92     }
93 
94     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)95     public View onCreateView(LayoutInflater inflater, ViewGroup container,
96             Bundle savedInstanceState) {
97         final long[] selectedIds = getActivity().getIntent().getLongArrayExtra(
98                 UiIntentActions.SELECTION_DEFAULT_SELECTION);
99         if (selectedIds != null && selectedIds.length != 0) {
100             final TreeSet<Long> selectedIdsTree = new TreeSet<>();
101             for (int i = 0; i < selectedIds.length; i++) {
102                 selectedIdsTree.add(selectedIds[i]);
103             }
104             getAdapter().setSelectedContactIds(selectedIdsTree);
105             onSelectedContactsChanged();
106         }
107         return super.onCreateView(inflater, container, savedInstanceState);
108     }
109 
110     @Override
onStart()111     public void onStart() {
112         super.onStart();
113         displayCheckBoxes(true);
114 
115         final long[] itemIds = getActivity().getIntent().getLongArrayExtra(
116                 UiIntentActions.SELECTION_ITEM_LIST);
117         final boolean[] selectedFlags = getActivity().getIntent().getBooleanArrayExtra(
118                 UiIntentActions.SELECTION_DEFAULT_SELECTION);
119         if (itemIds != null && selectedFlags != null && itemIds.length == selectedFlags.length) {
120             TreeSet<Long> selectedIds = new TreeSet<>();
121             for (int i = 0; i < itemIds.length; i++) {
122                 if (selectedFlags[i]) {
123                     selectedIds.add(itemIds[i]);
124                 }
125             }
126             getAdapter().setSelectedContactIds(selectedIds);
127             onSelectedContactsChanged();
128         }
129     }
130 
131     @Override
onItemLongClick(int position, long id)132     protected boolean onItemLongClick(int position, long id) {
133         return true;
134     }
135 
136     @Override
inflateView(LayoutInflater inflater, ViewGroup container)137     protected View inflateView(LayoutInflater inflater, ViewGroup container) {
138         return inflater.inflate(com.android.contacts.R.layout.contact_list_content, null);
139     }
140 }
141