1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/*
2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project
3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License");
5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License.
6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at
7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *      http://www.apache.org/licenses/LICENSE-2.0
9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software
11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS,
12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and
14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License.
15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.ui.conversationlist;
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.app.Activity;
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.app.AlertDialog;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.app.AlertDialog.Builder;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.app.Dialog;
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.app.DialogFragment;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.DialogInterface;
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.DialogInterface.OnClickListener;
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.database.Cursor;
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.os.Bundle;
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.support.v7.widget.LinearLayoutManager;
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.support.v7.widget.RecyclerView;
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.LayoutInflater;
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.View;
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.ViewGroup;
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.R;
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.binding.Binding;
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.binding.BindingBase;
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.ConversationListData;
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.ConversationListItemData;
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.ConversationListData.ConversationListDataListener;
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.ListEmptyView;
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.DataModel;
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/**
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Allow user to pick conversation to which an incoming attachment will be shared.
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class ShareIntentFragment extends DialogFragment implements ConversationListDataListener,
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        ShareIntentAdapter.HostInterface {
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final String HIDE_NEW_CONVERSATION_BUTTON_KEY = "hide_conv_button_key";
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public interface HostInterface {
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        public void onConversationClick(final ConversationListItemData conversationListItemData);
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        public void onCreateConversationClick();
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final Binding<ConversationListData> mListBinding = BindingBase.createBinding(this);
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private RecyclerView mRecyclerView;
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private ListEmptyView mEmptyListMessageView;
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private ShareIntentAdapter mAdapter;
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private HostInterface mHost;
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private boolean mDismissed;
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * {@inheritDoc} from Fragment
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public Dialog onCreateDialog(final Bundle bundle) {
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Activity activity = getActivity();
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final LayoutInflater inflater = activity.getLayoutInflater();
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        View view = inflater.inflate(R.layout.share_intent_conversation_list_view, null);
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mEmptyListMessageView = (ListEmptyView) view.findViewById(R.id.no_conversations_view);
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mEmptyListMessageView.setImageHint(R.drawable.ic_oobe_conv_list);
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // The default behavior for default layout param generation by LinearLayoutManager is to
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // provide width and height of WRAP_CONTENT, but this is not desirable for
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // ShareIntentFragment; the view in each row should be a width of MATCH_PARENT so that
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // the entire row is tappable.
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final LinearLayoutManager manager = new LinearLayoutManager(activity) {
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            @Override
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        ViewGroup.LayoutParams.WRAP_CONTENT);
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        };
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mListBinding.getData().init(getLoaderManager(), mListBinding);
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mAdapter = new ShareIntentAdapter(activity, null, this);
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mRecyclerView.setLayoutManager(manager);
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mRecyclerView.setHasFixedSize(true);
87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mRecyclerView.setAdapter(mAdapter);
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Builder dialogBuilder = new AlertDialog.Builder(activity)
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                .setView(view)
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                .setTitle(R.string.share_intent_activity_label);
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Bundle arguments = getArguments();
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (arguments == null || !arguments.getBoolean(HIDE_NEW_CONVERSATION_BUTTON_KEY)) {
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            dialogBuilder.setPositiveButton(R.string.share_new_message, new OnClickListener() {
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        @Override
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    public void onClick(DialogInterface dialog, int which) {
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                            mDismissed = true;
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                            mHost.onCreateConversationClick();
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    }
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                });
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return dialogBuilder.setNegativeButton(R.string.share_cancel, null)
103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                .create();
104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void onDismiss(DialogInterface dialog) {
108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (!mDismissed) {
109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final Activity activity = getActivity();
110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (activity != null) {
111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                activity.finish();
112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
116d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
117d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * {@inheritDoc} from Fragment
118d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
119d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
120d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void onDestroy() {
121d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        super.onDestroy();
122d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mListBinding.unbind();
123d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
126d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void onAttach(final Activity activity) {
127d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        super.onAttach(activity);
128d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (activity instanceof HostInterface) {
129d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mHost = (HostInterface) activity;
130d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
131d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mListBinding.bind(DataModel.get().createConversationListData(activity, this, false));
132d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
133d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
134d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
135d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void onConversationListCursorUpdated(final ConversationListData data,
136d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final Cursor cursor) {
137d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mListBinding.ensureBound(data);
138d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mAdapter.swapCursor(cursor);
139d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        updateEmptyListUi(cursor == null || cursor.getCount() == 0);
140d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
141d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
142d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
143d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * {@inheritDoc} from SharIntentItemView.HostInterface
144d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
145d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
146d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void onConversationClicked(final ConversationListItemData conversationListItemData) {
147d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mHost.onConversationClick(conversationListItemData);
148d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
149d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
150d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // Show and hide empty list UI as needed with appropriate text based on view specifics
151d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private void updateEmptyListUi(final boolean isEmpty) {
152d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (isEmpty) {
153d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mEmptyListMessageView.setTextHint(R.string.conversation_list_empty_text);
154d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mEmptyListMessageView.setVisibility(View.VISIBLE);
155d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else {
156d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mEmptyListMessageView.setVisibility(View.GONE);
157d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
158d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
159d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
160d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
161d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void setBlockedParticipantsAvailable(boolean blockedAvailable) {
162d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
163d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
164