1/*
2 * Copyright (C) 2010 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 */
16package com.android.contacts.interactions;
17
18import android.app.Activity;
19import android.app.FragmentManager;
20import android.content.Intent;
21import android.os.Bundle;
22import android.widget.EditText;
23
24import com.android.contacts.ContactSaveService;
25import com.android.contacts.R;
26import com.android.contacts.common.model.account.AccountWithDataSet;
27
28/**
29 * A dialog for creating a new group.
30 */
31public class GroupCreationDialogFragment extends GroupNameDialogFragment {
32    private static final String ARG_ACCOUNT_TYPE = "accountType";
33    private static final String ARG_ACCOUNT_NAME = "accountName";
34    private static final String ARG_DATA_SET = "dataSet";
35
36    public static final String FRAGMENT_TAG = "createGroupDialog";
37
38    private final OnGroupCreatedListener mListener;
39
40    public interface OnGroupCreatedListener {
41        public void onGroupCreated();
42    }
43
44    public static void show(
45            FragmentManager fragmentManager, String accountType, String accountName,
46            String dataSet, OnGroupCreatedListener listener) {
47        GroupCreationDialogFragment dialog = new GroupCreationDialogFragment(listener);
48        Bundle args = new Bundle();
49        args.putString(ARG_ACCOUNT_TYPE, accountType);
50        args.putString(ARG_ACCOUNT_NAME, accountName);
51        args.putString(ARG_DATA_SET, dataSet);
52        dialog.setArguments(args);
53        dialog.show(fragmentManager, FRAGMENT_TAG);
54    }
55
56    public GroupCreationDialogFragment() {
57        super();
58        mListener = null;
59    }
60
61    private GroupCreationDialogFragment(OnGroupCreatedListener listener) {
62        super();
63        mListener = listener;
64    }
65
66    public OnGroupCreatedListener getOnGroupCreatedListener() {
67        return mListener;
68    }
69
70    @Override
71    protected void initializeGroupLabelEditText(EditText editText) {
72    }
73
74    @Override
75    protected int getTitleResourceId() {
76        return R.string.create_group_dialog_title;
77    }
78
79    @Override
80    protected void onCompleted(String groupLabel) {
81        Bundle arguments = getArguments();
82        String accountType = arguments.getString(ARG_ACCOUNT_TYPE);
83        String accountName = arguments.getString(ARG_ACCOUNT_NAME);
84        String dataSet = arguments.getString(ARG_DATA_SET);
85
86        // Indicate to the listener that a new group will be created.
87        // If the device is rotated, mListener will become null, so that the
88        // popup from GroupMembershipView will not be shown.
89        if (mListener != null) {
90            mListener.onGroupCreated();
91        }
92
93        Activity activity = getActivity();
94        activity.startService(ContactSaveService.createNewGroupIntent(activity,
95                new AccountWithDataSet(accountName, accountType, dataSet), groupLabel,
96                null /* no new members to add */,
97                activity.getClass(), Intent.ACTION_EDIT));
98    }
99}
100