session-android/src/org/thoughtcrime/securesms/ContactSelectionActivity.java

204 lines
5.5 KiB
Java
Raw Normal View History

/**
2011-12-20 18:20:44 +00:00
* Copyright (C) 2011 Whisper Systems
*
2011-12-20 18:20:44 +00:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2011-12-20 18:20:44 +00:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
2013-12-16 20:17:52 +00:00
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
2013-12-16 20:17:52 +00:00
import android.support.v4.view.ViewPager;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.DynamicTheme;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
2011-12-20 18:20:44 +00:00
/**
* Activity container for selecting a list of contacts. Provides a tab frame for
* contact, group, and "recent contact" activity tabs. Used by ComposeMessageActivity
* when selecting a list of contacts to address a message to.
*
2011-12-20 18:20:44 +00:00
* @author Moxie Marlinspike
*
*/
public class ContactSelectionActivity extends PassphraseRequiredSherlockFragmentActivity {
2011-12-20 18:20:44 +00:00
private final DynamicTheme dynamicTheme = new DynamicTheme();
2013-12-16 20:17:52 +00:00
private ViewPager viewPager;
private ContactSelectionListFragment contactsFragment;
private ContactSelectionGroupsFragment groupsFragment;
private ContactSelectionRecentFragment recentFragment;
2011-12-20 18:20:44 +00:00
private Recipients recipients;
2011-12-20 18:20:44 +00:00
@Override
protected void onCreate(Bundle icicle) {
dynamicTheme.onCreate(this);
2011-12-20 18:20:44 +00:00
super.onCreate(icicle);
ActionBar actionBar = this.getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayHomeAsUpEnabled(true);
2011-12-20 18:20:44 +00:00
setContentView(R.layout.contact_selection_activity);
2011-12-20 18:20:44 +00:00
2013-12-16 20:17:52 +00:00
setupFragments();
setupViewPager();
setupTabs();
2011-12-20 18:20:44 +00:00
}
@Override
public void onResume() {
super.onResume();
dynamicTheme.onResume(this);
}
2011-12-20 18:20:44 +00:00
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = this.getSupportMenuInflater();
inflater.inflate(R.menu.contact_selection, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_selection_finished:
case android.R.id.home:
handleSelectionFinished(); return true;
}
2011-12-20 18:20:44 +00:00
return false;
}
private void handleSelectionFinished() {
recipients = contactsFragment.getSelectedContacts();
recipients.append(recentFragment.getSelectedContacts());
recipients.append(groupsFragment.getSelectedContacts(this));
Intent resultIntent = getIntent();
resultIntent.putExtra("recipients", this.recipients);
setResult(RESULT_OK, resultIntent);
finish();
}
2013-12-16 20:17:52 +00:00
private void setupViewPager() {
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new SelectionPagerAdapter());
viewPager.setOnPageChangeListener(new TabSwitchingPageListener());
}
2013-12-16 20:17:52 +00:00
private void setupTabs() {
int[] icons = new int[] { R.drawable.ic_tab_contacts, R.drawable.ic_tab_groups, R.drawable.ic_tab_recent };
2013-12-16 20:17:52 +00:00
for (int i = 0; i < icons.length; i++) {
ActionBar.Tab tab = getSupportActionBar().newTab();
tab.setIcon(icons[i]);
tab.setTabListener(new ViewPagerTabListener(i));
getSupportActionBar().addTab(tab);
}
}
2013-12-16 20:17:52 +00:00
private void setupFragments() {
contactsFragment = new ContactSelectionListFragment();
groupsFragment = new ContactSelectionGroupsFragment();
recentFragment = new ContactSelectionRecentFragment();
}
private class SelectionPagerAdapter extends FragmentPagerAdapter {
public SelectionPagerAdapter() {
super(getSupportFragmentManager());
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return contactsFragment;
case 1:
return groupsFragment;
case 2:
default:
return recentFragment;
}
2013-12-16 20:17:52 +00:00
}
2013-12-16 20:17:52 +00:00
@Override
public int getCount() {
return 3;
}
}
2013-12-16 20:17:52 +00:00
private class ViewPagerTabListener implements TabListener {
private int tabIndex;
public ViewPagerTabListener(int index) {
tabIndex = index;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tabIndex);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
}
}
2013-12-16 20:17:52 +00:00
private class TabSwitchingPageListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrolled(int i, float v, int i2) {
}
@Override
public void onPageSelected(int i) {
getSupportActionBar().setSelectedNavigationItem(i);
}
@Override
public void onPageScrollStateChanged(int i) {
}
}
2013-12-16 20:17:52 +00:00
2011-12-20 18:20:44 +00:00
}