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