1 /* 2 * Copyright (C) 2010 Google Inc. 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.loaderapp; 18 19 import com.android.loaderapp.fragments.ContactFragment; 20 import com.android.loaderapp.fragments.ContactsListFragment; 21 import com.android.loaderapp.fragments.GroupsListFragment; 22 23 import android.app.ActionBar; 24 import android.app.Activity; 25 import android.app.FragmentTransaction; 26 import android.content.Intent; 27 import android.content.res.Resources; 28 import android.net.Uri; 29 import android.os.Bundle; 30 import android.provider.ContactsContract; 31 import android.view.Menu; 32 import android.view.MenuItem; 33 import android.view.View; 34 import android.view.ViewGroup; 35 36 public class HomeGroupsXLarge extends Activity implements ActionBar.Callback, 37 ContactsListFragment.Controller, GroupsListFragment.Controller { 38 private static final int ACTION_ID_SEARCH = 0; 39 private static final int ACTION_ID_ADD = 1; 40 41 private static final int MODE_GROUPS = 0; 42 private static final int MODE_DETAILS = 1; 43 44 int mMode; 45 46 @Override onCreate(Bundle savedState)47 public void onCreate(Bundle savedState) { 48 super.onCreate(savedState); 49 50 setContentView(R.layout.two_pane); 51 52 GroupsListFragment groupsList = new GroupsListFragment(); 53 groupsList.setController(this); 54 55 ContactsListFragment contactsList = new ContactsListFragment( 56 ContactsListFragment.MODE_NULL); 57 contactsList.setController(this); 58 59 FragmentTransaction xact = openFragmentTransaction(); 60 xact.add(R.id.smallPane, groupsList); 61 xact.add(R.id.largePane, contactsList); 62 xact.commit(); 63 mMode = MODE_GROUPS; 64 65 getActionBar().setCallback(this); 66 } 67 getContactsList()68 private ContactsListFragment getContactsList() { 69 switch (mMode) { 70 case MODE_GROUPS: 71 return (ContactsListFragment) findFragmentById(R.id.largePane); 72 case MODE_DETAILS: 73 return (ContactsListFragment) findFragmentById(R.id.smallPane); 74 } 75 throw new IllegalStateException("unknown mode " + mMode); 76 } 77 onAllContactsSelected()78 public void onAllContactsSelected() { 79 getContactsList().setMode(ContactsListFragment.MODE_VISIBLE); 80 } 81 onFavoritesSelected()82 public void onFavoritesSelected() { 83 getContactsList().setMode(ContactsListFragment.MODE_STREQUENT); 84 } 85 onGroupSelected(String title)86 public void onGroupSelected(String title) { 87 getContactsList().setGroupMode(title); 88 } 89 onContactSelected(Uri contactUri)90 public void onContactSelected(Uri contactUri) { 91 if (contactUri == null) { 92 return; 93 } 94 95 ContactFragment details = new ContactFragment(contactUri, 96 new ContactFragment.DefaultController(this)); 97 FragmentTransaction xact = openFragmentTransaction(); 98 xact.addToBackStack(null); 99 if (mMode == MODE_GROUPS) { 100 mMode = MODE_DETAILS; 101 // commit is actually async, so add in details at largePane, which will be correct 102 // after swapPanes() does its thing. 103 xact.remove(findFragmentById(R.id.smallPane)); 104 xact.add(R.id.largePane, details); 105 xact.commit(); 106 swapPanes(); // swap the list to the small pane 107 } else { 108 xact.replace(R.id.largePane, details); 109 xact.commit(); 110 } 111 } 112 swapPanes()113 private void swapPanes() { 114 ViewGroup paneHost = (ViewGroup) findViewById(R.id.paneHost); 115 116 View largePane = findViewById(R.id.largePane); 117 ViewGroup.LayoutParams largeParams = largePane.getLayoutParams(); 118 int largeIndex = paneHost.indexOfChild(largePane); 119 120 View smallPane = findViewById(R.id.smallPane); 121 ViewGroup.LayoutParams smallParams = smallPane.getLayoutParams(); 122 int smallIndex = paneHost.indexOfChild(smallPane); 123 124 paneHost.removeAllViews(); 125 126 largePane.setId(R.id.smallPane); 127 largePane.setLayoutParams(smallParams); 128 129 smallPane.setId(R.id.largePane); 130 smallPane.setLayoutParams(largeParams); 131 132 if (smallIndex < largeIndex) { 133 // Small was before large so add them back in reverse order 134 paneHost.addView(largePane); 135 paneHost.addView(smallPane); 136 } else { 137 // Large was before small so add them back in reverse order 138 paneHost.addView(smallPane); 139 paneHost.addView(largePane); 140 } 141 } 142 143 @Override onBackPressed()144 public void onBackPressed() { 145 if (!popBackStack(null)) { 146 finish(); 147 } else { 148 if (mMode == MODE_DETAILS) { 149 swapPanes(); 150 mMode = MODE_GROUPS; 151 } 152 } 153 } 154 155 /** Implements ActionBar.Callback */ onCreateActionMenu(Menu menu)156 public boolean onCreateActionMenu(Menu menu) { 157 Resources resources = getResources(); 158 menu.add(0, ACTION_ID_SEARCH, 0, R.string.menu_search) 159 .setIcon(resources.getDrawable(android.R.drawable.ic_menu_search)); 160 menu.add(0, ACTION_ID_ADD, 1, R.string.menu_newContact) 161 .setIcon(resources.getDrawable(android.R.drawable.ic_menu_add)); 162 163 return true; 164 } 165 166 /** Implements ActionBar.Callback */ onUpdateActionMenu(Menu menu)167 public boolean onUpdateActionMenu(Menu menu) { 168 return false; 169 } 170 171 /** Implements ActionBar.Callback */ onActionItemClicked(MenuItem item)172 public boolean onActionItemClicked(MenuItem item) { 173 switch (item.getItemId()) { 174 case ACTION_ID_SEARCH: { 175 startSearch(null, false, null, true); 176 return true; 177 } 178 179 case ACTION_ID_ADD: { 180 startActivity(new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI)); 181 return true; 182 } 183 } 184 return false; 185 } 186 187 /** Implements ActionBar.Callback */ onNavigationItemSelected(int itemPosition, long itemId)188 public boolean onNavigationItemSelected(int itemPosition, long itemId) { 189 return false; 190 } 191 192 /** Implements ActionBar.Callback */ onCreateContextMode(int modeId, Menu menu)193 public boolean onCreateContextMode(int modeId, Menu menu) { 194 return false; 195 } 196 197 /** Implements ActionBar.Callback */ onPrepareContextMode(int modeId, Menu menu)198 public boolean onPrepareContextMode(int modeId, Menu menu) { 199 return false; 200 } 201 202 /** Implements ActionBar.Callback */ onContextItemClicked(int modeId, MenuItem item)203 public boolean onContextItemClicked(int modeId, MenuItem item) { 204 return false; 205 } 206 } 207