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 Dodd
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.datamodel.data;
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.app.LoaderManager;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Context;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Loader;
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.database.Cursor;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.net.Uri;
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.os.Bundle;
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.BoundCursorLoader;
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.MessagingContentProvider;
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.action.BugleActionToasts;
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.action.UpdateConversationOptionsAction;
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.action.UpdateDestinationBlockedAction;
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.binding.BindableData;
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.binding.BindingBase;
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.Assert;
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.LogUtil;
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.util.List;
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/**
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Services data needs for PeopleAndOptionsFragment.
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class PeopleAndOptionsData extends BindableData implements
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        LoaderManager.LoaderCallbacks<Cursor> {
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public interface PeopleAndOptionsDataListener {
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        void onOptionsCursorUpdated(PeopleAndOptionsData data, Cursor cursor);
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        void onParticipantsListLoaded(PeopleAndOptionsData data,
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                List<ParticipantData> participants);
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final String BINDING_ID = "bindingId";
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final Context mContext;
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final String mConversationId;
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final ConversationParticipantsData mParticipantData;
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private LoaderManager mLoaderManager;
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private PeopleAndOptionsDataListener mListener;
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public PeopleAndOptionsData(final String conversationId, final Context context,
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final PeopleAndOptionsDataListener listener) {
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mListener = listener;
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mContext = context;
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mConversationId = conversationId;
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mParticipantData = new ConversationParticipantsData();
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int CONVERSATION_OPTIONS_LOADER = 1;
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int PARTICIPANT_LOADER = 2;
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public Loader<Cursor> onCreateLoader(final int id, final Bundle args) {
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String bindingId = args.getString(BINDING_ID);
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Check if data still bound to the requesting ui element
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (isBound(bindingId)) {
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            switch (id) {
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                case CONVERSATION_OPTIONS_LOADER: {
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    final Uri uri =
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                            MessagingContentProvider.buildConversationMetadataUri(mConversationId);
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    return new BoundCursorLoader(bindingId, mContext, uri,
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                            PeopleOptionsItemData.PROJECTION, null, null, null);
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                }
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                case PARTICIPANT_LOADER: {
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    final Uri uri =
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                            MessagingContentProvider
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                                    .buildConversationParticipantsUri(mConversationId);
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    return new BoundCursorLoader(bindingId, mContext, uri,
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                            ParticipantData.ParticipantsQuery.PROJECTION, null, null, null);
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                }
87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                default:
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    Assert.fail("Unknown loader id for PeopleAndOptionsFragment!");
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    break;
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else {
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            LogUtil.w(LogUtil.BUGLE_TAG, "Loader created after unbinding PeopleAndOptionsFragment");
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return null;
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * {@inheritDoc}
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void onLoadFinished(final Loader<Cursor> loader, final Cursor data) {
103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final BoundCursorLoader cursorLoader = (BoundCursorLoader) loader;
104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (isBound(cursorLoader.getBindingId())) {
105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            switch (loader.getId()) {
106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                case CONVERSATION_OPTIONS_LOADER:
107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    mListener.onOptionsCursorUpdated(this, data);
108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    break;
109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                case PARTICIPANT_LOADER:
111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    mParticipantData.bind(data);
112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    mListener.onParticipantsListLoaded(this,
113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                            mParticipantData.getParticipantListExcludingSelf());
114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    break;
115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
116d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                default:
117d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    Assert.fail("Unknown loader id for PeopleAndOptionsFragment!");
118d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    break;
119d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
120d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else {
121d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            LogUtil.w(LogUtil.BUGLE_TAG,
122d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    "Loader finished after unbinding PeopleAndOptionsFragment");
123d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
126d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
127d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * {@inheritDoc}
128d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
129d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
130d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void onLoaderReset(final Loader<Cursor> loader) {
131d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final BoundCursorLoader cursorLoader = (BoundCursorLoader) loader;
132d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (isBound(cursorLoader.getBindingId())) {
133d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            switch (loader.getId()) {
134d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                case CONVERSATION_OPTIONS_LOADER:
135d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    mListener.onOptionsCursorUpdated(this, null);
136d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    break;
137d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
138d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                case PARTICIPANT_LOADER:
139d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    mParticipantData.bind(null);
140d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    break;
141d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
142d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                default:
143d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    Assert.fail("Unknown loader id for PeopleAndOptionsFragment!");
144d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    break;
145d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
146d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else {
147d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            LogUtil.w(LogUtil.BUGLE_TAG, "Loader reset after unbinding PeopleAndOptionsFragment");
148d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
149d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
150d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
151d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void init(final LoaderManager loaderManager,
152d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final BindingBase<PeopleAndOptionsData> binding) {
153d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Bundle args = new Bundle();
154d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        args.putString(BINDING_ID, binding.getBindingId());
155d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mLoaderManager = loaderManager;
156d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mLoaderManager.initLoader(CONVERSATION_OPTIONS_LOADER, args, this);
157d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mLoaderManager.initLoader(PARTICIPANT_LOADER, args, this);
158d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
159d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
160d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
161d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    protected void unregisterListeners() {
162d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mListener = null;
163d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
164d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // This could be null if we bind but the caller doesn't init the BindableData
165d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (mLoaderManager != null) {
166d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mLoaderManager.destroyLoader(CONVERSATION_OPTIONS_LOADER);
167d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mLoaderManager.destroyLoader(PARTICIPANT_LOADER);
168d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mLoaderManager = null;
169d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
170d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
171d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
172d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void enableConversationNotifications(final BindingBase<PeopleAndOptionsData> binding,
173d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final boolean enable) {
174d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String bindingId = binding.getBindingId();
175d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (isBound(bindingId)) {
176d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            UpdateConversationOptionsAction.enableConversationNotifications(
177d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    mConversationId, enable);
178d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
179d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
180d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
181d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void setConversationNotificationSound(final BindingBase<PeopleAndOptionsData> binding,
182d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final String ringtoneUri) {
183d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String bindingId = binding.getBindingId();
184d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (isBound(bindingId)) {
185d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            UpdateConversationOptionsAction.setConversationNotificationSound(mConversationId,
186d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    ringtoneUri);
187d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
188d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
189d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
190d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void enableConversationNotificationVibration(
191d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final BindingBase<PeopleAndOptionsData> binding, final boolean enable) {
192d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String bindingId = binding.getBindingId();
193d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (isBound(bindingId)) {
194d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            UpdateConversationOptionsAction.enableVibrationForConversationNotification(
195d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    mConversationId, enable);
196d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
197d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
198d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
199d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void setDestinationBlocked(final BindingBase<PeopleAndOptionsData> binding,
200d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final boolean blocked) {
201d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String bindingId = binding.getBindingId();
202d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final ParticipantData participantData = mParticipantData.getOtherParticipant();
203d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (isBound(bindingId) && participantData != null) {
204d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            UpdateDestinationBlockedAction.updateDestinationBlocked(
205d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    participantData.getNormalizedDestination(),
206d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    blocked, mConversationId,
207d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    BugleActionToasts.makeUpdateDestinationBlockedActionListener(mContext));
208d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
209d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
210d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
211