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