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.attachmentchooser;
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.app.Fragment;
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Context;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.Rect;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.net.Uri;
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.os.Bundle;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.support.v7.app.ActionBar;
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.LayoutInflater;
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.Menu;
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.MenuInflater;
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.MenuItem;
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.View;
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.ViewGroup;
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.widget.ArrayAdapter;
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.R;
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.DataModel;
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.MessagingContentProvider;
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.binding.Binding;
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.binding.BindingBase;
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.DraftMessageData;
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.DraftMessageData.DraftMessageDataListener;
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.MessagePartData;
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.BugleActionBarActivity;
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.UIIntents;
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.attachmentchooser.AttachmentGridView.AttachmentGridHost;
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.google.common.annotations.VisibleForTesting;
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.util.ArrayList;
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.util.List;
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class AttachmentChooserFragment extends Fragment implements DraftMessageDataListener,
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        AttachmentGridHost {
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public interface AttachmentChooserFragmentHost {
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        void onConfirmSelection();
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private AttachmentGridView mAttachmentGridView;
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private AttachmentGridAdapter mAdapter;
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private AttachmentChooserFragmentHost mHost;
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @VisibleForTesting
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    Binding<DraftMessageData> mBinding = BindingBase.createBinding(this);
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final Bundle savedInstanceState) {
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final View view = inflater.inflate(R.layout.attachment_chooser_fragment, container, false);
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mAttachmentGridView = (AttachmentGridView) view.findViewById(R.id.grid);
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mAdapter = new AttachmentGridAdapter(getActivity());
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mAttachmentGridView.setAdapter(mAdapter);
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mAttachmentGridView.setHost(this);
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        setHasOptionsMenu(true);
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return view;
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void onDestroy() {
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        super.onDestroy();
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mBinding.unbind();
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        super.onCreateOptionsMenu(menu, inflater);
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        inflater.inflate(R.menu.attachment_chooser_menu, menu);
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public boolean onOptionsItemSelected(MenuItem item) {
87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        switch (item.getItemId()) {
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case R.id.action_confirm_selection:
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                confirmSelection();
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return true;
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            default:
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return super.onOptionsItemSelected(item);
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @VisibleForTesting
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    void confirmSelection() {
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (mBinding.isBound()) {
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mBinding.getData().removeExistingAttachments(
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    mAttachmentGridView.getUnselectedAttachments());
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mBinding.getData().saveToStorage(mBinding);
103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mHost.onConfirmSelection();
104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void setConversationId(final String conversationId) {
108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mBinding.bind(DataModel.get().createDraftMessageData(conversationId));
109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mBinding.getData().addListener(this);
110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mBinding.getData().loadFromStorage(mBinding, null, false);
111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void setHost(final AttachmentChooserFragmentHost host) {
114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mHost = host;
115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
116d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
117d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
118d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void onDraftChanged(final DraftMessageData data, final int changeFlags) {
119d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mBinding.ensureBound(data);
120d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if ((changeFlags & DraftMessageData.ATTACHMENTS_CHANGED) ==
121d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                DraftMessageData.ATTACHMENTS_CHANGED) {
122d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mAdapter.onAttachmentsLoaded(data.getReadOnlyAttachments());
123d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
126d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
127d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void onDraftAttachmentLimitReached(final DraftMessageData data) {
128d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Do nothing since the user is in the process of unselecting attachments.
129d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
130d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
131d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
132d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void onDraftAttachmentLoadFailed() {
133d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Do nothing since the user is in the process of unselecting attachments.
134d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
135d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
136d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
137d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void displayPhoto(final Rect viewRect, final Uri photoUri) {
138d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Uri imagesUri = MessagingContentProvider.buildDraftImagesUri(
139d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mBinding.getData().getConversationId());
140d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        UIIntents.get().launchFullScreenPhotoViewer(
141d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                getActivity(), photoUri, viewRect, imagesUri);
142d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
143d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
144d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
145d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void updateSelectionCount(int count) {
146d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (getActivity() instanceof BugleActionBarActivity) {
147d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final ActionBar actionBar = ((BugleActionBarActivity) getActivity())
148d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    .getSupportActionBar();
149d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (actionBar != null) {
150d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                actionBar.setTitle(getResources().getString(
151d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        R.string.attachment_chooser_selection, count));
152d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
153d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
154d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
155d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
156d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    class AttachmentGridAdapter extends ArrayAdapter<MessagePartData> {
157d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        public AttachmentGridAdapter(final Context context) {
158d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            super(context, R.layout.attachment_grid_item_view, new ArrayList<MessagePartData>());
159d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
160d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
161d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        public void onAttachmentsLoaded(final List<MessagePartData> attachments) {
162d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            clear();
163d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            addAll(attachments);
164d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            notifyDataSetChanged();
165d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
166d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
167d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        @Override
168d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        public View getView(final int position, final View convertView, final ViewGroup parent) {
169d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            AttachmentGridItemView itemView;
170d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final MessagePartData item = getItem(position);
171d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (convertView != null && convertView instanceof AttachmentGridItemView) {
172d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                itemView = (AttachmentGridItemView) convertView;
173d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            } else {
174d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final LayoutInflater inflater = (LayoutInflater) getContext()
175d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
176d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                itemView = (AttachmentGridItemView) inflater.inflate(
177d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        R.layout.attachment_grid_item_view, parent, false);
178d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
179d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            itemView.bind(item, mAttachmentGridView);
180d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return itemView;
181d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
182d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
183d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
184