1 /* 2 * Copyright (C) 2013 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.example.android.contactslist.ui; 18 19 import android.app.SearchManager; 20 import android.content.Intent; 21 import android.net.Uri; 22 import android.os.Bundle; 23 import android.support.v4.app.FragmentActivity; 24 25 import com.example.android.contactslist.BuildConfig; 26 import com.example.android.contactslist.R; 27 import com.example.android.contactslist.util.Utils; 28 29 /** 30 * FragmentActivity to hold the main {@link ContactsListFragment}. On larger screen devices which 31 * can fit two panes also load {@link ContactDetailFragment}. 32 */ 33 public class ContactsListActivity extends FragmentActivity implements 34 ContactsListFragment.OnContactsInteractionListener { 35 36 // Defines a tag for identifying log entries 37 private static final String TAG = "ContactsListActivity"; 38 39 private ContactDetailFragment mContactDetailFragment; 40 41 // If true, this is a larger screen device which fits two panes 42 private boolean isTwoPaneLayout; 43 44 // True if this activity instance is a search result view (used on pre-HC devices that load 45 // search results in a separate instance of the activity rather than loading results in-line 46 // as the query is typed. 47 private boolean isSearchResultView = false; 48 49 @Override 50 protected void onCreate(Bundle savedInstanceState) { 51 if (BuildConfig.DEBUG) { 52 Utils.enableStrictMode(); 53 } 54 super.onCreate(savedInstanceState); 55 56 // Set main content view. On smaller screen devices this is a single pane view with one 57 // fragment. One larger screen devices this is a two pane view with two fragments. 58 setContentView(R.layout.activity_main); 59 60 // Check if two pane bool is set based on resource directories 61 isTwoPaneLayout = getResources().getBoolean(R.bool.has_two_panes); 62 63 // Check if this activity instance has been triggered as a result of a search query. This 64 // will only happen on pre-HC OS versions as from HC onward search is carried out using 65 // an ActionBar SearchView which carries out the search in-line without loading a new 66 // Activity. 67 if (Intent.ACTION_SEARCH.equals(getIntent().getAction())) { 68 69 // Fetch query from intent and notify the fragment that it should display search 70 // results instead of all contacts. 71 String searchQuery = getIntent().getStringExtra(SearchManager.QUERY); 72 ContactsListFragment mContactsListFragment = (ContactsListFragment) 73 getSupportFragmentManager().findFragmentById(R.id.contact_list); 74 75 // This flag notes that the Activity is doing a search, and so the result will be 76 // search results rather than all contacts. This prevents the Activity and Fragment 77 // from trying to a search on search results. 78 isSearchResultView = true; 79 mContactsListFragment.setSearchQuery(searchQuery); 80 81 // Set special title for search results 82 String title = getString(R.string.contacts_list_search_results_title, searchQuery); 83 setTitle(title); 84 } 85 86 if (isTwoPaneLayout) { 87 // If two pane layout, locate the contact detail fragment 88 mContactDetailFragment = (ContactDetailFragment) 89 getSupportFragmentManager().findFragmentById(R.id.contact_detail); 90 } 91 } 92 93 /** 94 * This interface callback lets the main contacts list fragment notify 95 * this activity that a contact has been selected. 96 * 97 * @param contactUri The contact Uri to the selected contact. 98 */ 99 @Override 100 public void onContactSelected(Uri contactUri) { 101 if (isTwoPaneLayout && mContactDetailFragment != null) { 102 // If two pane layout then update the detail fragment to show the selected contact 103 mContactDetailFragment.setContact(contactUri); 104 } else { 105 // Otherwise single pane layout, start a new ContactDetailActivity with 106 // the contact Uri 107 Intent intent = new Intent(this, ContactDetailActivity.class); 108 intent.setData(contactUri); 109 startActivity(intent); 110 } 111 } 112 113 /** 114 * This interface callback lets the main contacts list fragment notify 115 * this activity that a contact is no longer selected. 116 */ 117 @Override 118 public void onSelectionCleared() { 119 if (isTwoPaneLayout && mContactDetailFragment != null) { 120 mContactDetailFragment.setContact(null); 121 } 122 } 123 124 @Override 125 public boolean onSearchRequested() { 126 // Don't allow another search if this activity instance is already showing 127 // search results. Only used pre-HC. 128 return !isSearchResultView && super.onSearchRequested(); 129 } 130 } 131