1/*
2 * Copyright (C) 2011 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
17package com.android.contacts.activities;
18
19import android.app.ActionBar;
20import android.content.ContentUris;
21import android.content.Intent;
22import android.net.Uri;
23import android.os.Bundle;
24import android.provider.ContactsContract.Groups;
25import android.text.TextUtils;
26import android.view.Menu;
27import android.view.MenuInflater;
28import android.view.MenuItem;
29import android.view.View;
30import android.view.View.OnClickListener;
31
32import com.android.contacts.ContactsActivity;
33import com.android.contacts.R;
34import com.android.contacts.group.GroupDetailDisplayUtils;
35import com.android.contacts.group.GroupDetailFragment;
36import com.android.contacts.model.AccountTypeManager;
37import com.android.contacts.model.account.AccountType;
38
39public class GroupDetailActivity extends ContactsActivity {
40
41    private static final String TAG = "GroupDetailActivity";
42
43    private boolean mShowGroupSourceInActionBar;
44
45    private String mAccountTypeString;
46    private String mDataSet;
47
48    private GroupDetailFragment mFragment;
49
50    @Override
51    public void onCreate(Bundle savedState) {
52        super.onCreate(savedState);
53
54        // TODO: Create Intent Resolver to handle the different ways users can get to this list.
55        // TODO: Handle search or key down
56
57        setContentView(R.layout.group_detail_activity);
58
59        mShowGroupSourceInActionBar = getResources().getBoolean(
60                R.bool.config_show_group_action_in_action_bar);
61
62        mFragment = (GroupDetailFragment) getFragmentManager().findFragmentById(
63                R.id.group_detail_fragment);
64        mFragment.setListener(mFragmentListener);
65        mFragment.setShowGroupSourceInActionBar(mShowGroupSourceInActionBar);
66        mFragment.loadGroup(getIntent().getData());
67        mFragment.closeActivityAfterDelete(true);
68
69        // We want the UP affordance but no app icon.
70        ActionBar actionBar = getActionBar();
71        if (actionBar != null) {
72            actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE,
73                    ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE
74                    | ActionBar.DISPLAY_SHOW_HOME);
75        }
76    }
77
78    private final GroupDetailFragment.Listener mFragmentListener =
79            new GroupDetailFragment.Listener() {
80
81        @Override
82        public void onGroupSizeUpdated(String size) {
83            getActionBar().setSubtitle(size);
84        }
85
86        @Override
87        public void onGroupTitleUpdated(String title) {
88            getActionBar().setTitle(title);
89        }
90
91        @Override
92        public void onAccountTypeUpdated(String accountTypeString, String dataSet) {
93            mAccountTypeString = accountTypeString;
94            mDataSet = dataSet;
95            invalidateOptionsMenu();
96        }
97
98        @Override
99        public void onEditRequested(Uri groupUri) {
100            final Intent intent = new Intent(GroupDetailActivity.this, GroupEditorActivity.class);
101            intent.setData(groupUri);
102            intent.setAction(Intent.ACTION_EDIT);
103            startActivity(intent);
104        }
105
106        @Override
107        public void onContactSelected(Uri contactUri) {
108            Intent intent = new Intent(Intent.ACTION_VIEW, contactUri);
109            startActivity(intent);
110        }
111
112    };
113
114    @Override
115    public boolean onCreateOptionsMenu(Menu menu) {
116        super.onCreateOptionsMenu(menu);
117        if (mShowGroupSourceInActionBar) {
118            MenuInflater inflater = getMenuInflater();
119            inflater.inflate(R.menu.group_source, menu);
120        }
121        return true;
122    }
123
124    @Override
125    public boolean onPrepareOptionsMenu(Menu menu) {
126        if (!mShowGroupSourceInActionBar) {
127            return false;
128        }
129        MenuItem groupSourceMenuItem = menu.findItem(R.id.menu_group_source);
130        if (groupSourceMenuItem == null) {
131            return false;
132        }
133        final AccountTypeManager manager = AccountTypeManager.getInstance(this);
134        final AccountType accountType =
135                manager.getAccountType(mAccountTypeString, mDataSet);
136        if (TextUtils.isEmpty(mAccountTypeString)
137                || TextUtils.isEmpty(accountType.getViewGroupActivity())) {
138            groupSourceMenuItem.setVisible(false);
139            return false;
140        }
141        View groupSourceView = GroupDetailDisplayUtils.getNewGroupSourceView(this);
142        GroupDetailDisplayUtils.bindGroupSourceView(this, groupSourceView,
143                mAccountTypeString, mDataSet);
144        groupSourceView.setOnClickListener(new OnClickListener() {
145            @Override
146            public void onClick(View v) {
147                final Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI,
148                        mFragment.getGroupId());
149                final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
150                intent.setClassName(accountType.syncAdapterPackageName,
151                        accountType.getViewGroupActivity());
152                startActivity(intent);
153            }
154        });
155        groupSourceMenuItem.setActionView(groupSourceView);
156        groupSourceMenuItem.setVisible(true);
157        return true;
158    }
159
160    @Override
161    public boolean onOptionsItemSelected(MenuItem item) {
162        switch (item.getItemId()) {
163            case android.R.id.home:
164                Intent intent = new Intent(this, PeopleActivity.class);
165                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
166                startActivity(intent);
167                finish();
168                return true;
169            default:
170                break;
171        }
172        return super.onOptionsItemSelected(item);
173    }
174}
175