summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/widget/WidgetConversationListService.java
blob: 023c56d5315e276a44715da1d7d91e7f57f4f122 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.messaging.widget;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

import com.android.messaging.R;
import com.android.messaging.datamodel.MessagingContentProvider;
import com.android.messaging.datamodel.data.ConversationListData;
import com.android.messaging.datamodel.data.ConversationListItemData;
import com.android.messaging.sms.MmsUtils;
import com.android.messaging.ui.UIIntents;
import com.android.messaging.ui.conversationlist.ConversationListItemView;
import com.android.messaging.util.ContentType;
import com.android.messaging.util.Dates;
import com.android.messaging.util.LogUtil;
import com.android.messaging.util.OsUtil;
import com.android.messaging.util.PhoneUtils;

public class WidgetConversationListService extends RemoteViewsService {
    private static final String TAG = LogUtil.BUGLE_WIDGET_TAG;

    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
            LogUtil.v(TAG, "onGetViewFactory intent: " + intent);
        }
        return new WidgetConversationListFactory(getApplicationContext(), intent);
    }

    /**
     * Remote Views Factory for Bugle Widget.
     */
    private static class WidgetConversationListFactory extends BaseWidgetFactory {

        public WidgetConversationListFactory(Context context, Intent intent) {
            super(context, intent);
        }

        @Override
        protected Cursor doQuery() {
            return  mContext.getContentResolver().query(MessagingContentProvider.CONVERSATIONS_URI,
                    ConversationListItemData.PROJECTION,
                    ConversationListData.WHERE_NOT_ARCHIVED,
                    null,       // selection args
                    ConversationListData.SORT_ORDER);
        }

        /**
         * @return the {@link RemoteViews} for a specific position in the list.
         */
        @Override
        public RemoteViews getViewAt(int position) {
            if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
                LogUtil.v(TAG, "getViewAt position: " + position);
            }
            synchronized (sWidgetLock) {
                // "View more conversations" view.
                if (mCursor == null
                        || (mShouldShowViewMore && position >= getItemCount())) {
                    return getViewMoreItemsView();
                }

                if (!mCursor.moveToPosition(position)) {
                    // If we ever fail to move to a position, return the "View More conversations"
                    // view.
                    LogUtil.w(TAG, "Failed to move to position: " + position);
                    return getViewMoreItemsView();
                }

                final ConversationListItemData conv = new ConversationListItemData();
                conv.bind(mCursor);

                // Inflate and fill out the remote view
                final RemoteViews remoteViews = new RemoteViews(
                        mContext.getPackageName(), R.layout.widget_conversation_list_item);

                final boolean hasUnreadMessages = !conv.getIsRead();
                final Resources resources = mContext.getResources();
                final boolean isDefaultSmsApp = PhoneUtils.getDefault().isDefaultSmsApp();

                final String timeStamp = conv.getIsSendRequested() ?
                        resources.getString(R.string.message_status_sending) :
                            Dates.getWidgetTimeString(conv.getTimestamp(), true /*abbreviated*/)
                                .toString();
                // Date/Timestamp or Sending or Error state -- all shown in the date item
                remoteViews.setTextViewText(R.id.date,
                        boldifyIfUnread(timeStamp, hasUnreadMessages));

                // From
                remoteViews.setTextViewText(R.id.from,
                        boldifyIfUnread(conv.getName(), hasUnreadMessages));

                // On click intent.
                final Intent intent = UIIntents.get().getIntentForConversationActivity(mContext,
                        conv.getConversationId(), null /* draft */);

                remoteViews.setOnClickFillInIntent(R.id.widget_conversation_list_item, intent);

                // Avatar
                boolean includeAvatar;
                if (OsUtil.isAtLeastJB()) {
                    final Bundle options = mAppWidgetManager.getAppWidgetOptions(mAppWidgetId);
                    if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
                        LogUtil.v(TAG, "getViewAt BugleWidgetProvider.WIDGET_SIZE_KEY: " +
                                options.getInt(BugleWidgetProvider.WIDGET_SIZE_KEY));
                    }

                    includeAvatar = options.getInt(BugleWidgetProvider.WIDGET_SIZE_KEY) ==
                            BugleWidgetProvider.SIZE_LARGE;
                } else {
                    includeAvatar = true;;
                }

                // Show the avatar when grande size, otherwise hide it.
                remoteViews.setViewVisibility(R.id.avatarView, includeAvatar ?
                        View.VISIBLE : View.GONE);

                Uri iconUri = null;
                if (conv.getIcon() != null) {
                    iconUri = Uri.parse(conv.getIcon());
                }
                remoteViews.setImageViewBitmap(R.id.avatarView, includeAvatar ?
                        getAvatarBitmap(iconUri) : null);

                // Error
                // Only show the fail icon if it is not a group conversation.
                // And also require that we be the default sms app.
                final boolean showError =
                        conv.getIsFailedStatus() && !conv.getIsGroup() && isDefaultSmsApp;
                final boolean showDraft = conv.getShowDraft() && isDefaultSmsApp;
                remoteViews.setViewVisibility(R.id.conversation_failed_status_icon,
                        showError && includeAvatar ?
                        View.VISIBLE : View.GONE);

                if (showError || showDraft) {
                    remoteViews.setViewVisibility(R.id.snippet, View.GONE);
                    remoteViews.setViewVisibility(R.id.errorBlock, View.VISIBLE);
                    remoteViews.setTextViewText(R.id.errorSnippet, getSnippetText(conv));

                    if (showDraft) {
                        // Show italicized "Draft" on third line
                        final String text = resources.getString(
                                R.string.conversation_list_item_view_draft_message);
                        SpannableStringBuilder builder = new SpannableStringBuilder(text);
                        builder.setSpan(new StyleSpan(Typeface.ITALIC), 0, text.length(),
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        builder.setSpan(new ForegroundColorSpan(
                                    resources.getColor(R.color.widget_text_color)),
                                0, text.length(),
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        remoteViews.setTextViewText(R.id.errorText, builder);
                    } else {
                        // Show error message on third line
                       int failureMessageId = R.string.message_status_download_failed;
                        if (conv.getIsMessageTypeOutgoing()) {
                            failureMessageId = MmsUtils.mapRawStatusToErrorResourceId(
                                    conv.getMessageStatus(),
                                    conv.getMessageRawTelephonyStatus());
                        }
                        remoteViews.setTextViewText(R.id.errorText,
                                resources.getString(failureMessageId));
                    }
                } else {
                    remoteViews.setViewVisibility(R.id.errorBlock, View.GONE);
                    remoteViews.setViewVisibility(R.id.snippet, View.VISIBLE);
                    remoteViews.setTextViewText(R.id.snippet,
                            boldifyIfUnread(getSnippetText(conv), hasUnreadMessages));
                }

                // Set the accessibility TalkBack text
                remoteViews.setContentDescription(R.id.widget_conversation_list_item,
                        ConversationListItemView.buildContentDescription(mContext.getResources(),
                                conv, new TextPaint()));

                return remoteViews;
            }
        }

        private String getSnippetText(final ConversationListItemData conv) {
            String snippetText = conv.getShowDraft() ?
                    conv.getDraftSnippetText() : conv.getSnippetText();
            final String previewContentType = conv.getShowDraft() ?
                    conv.getDraftPreviewContentType() : conv.getPreviewContentType();
            if (TextUtils.isEmpty(snippetText)) {
                Resources resources = mContext.getResources();
                // Use the attachment type as a snippet so the preview doesn't look odd
                if (ContentType.isAudioType(previewContentType)) {
                    snippetText = resources.getString(
                            R.string.conversation_list_snippet_audio_clip);
                } else if (ContentType.isImageType(previewContentType)) {
                    snippetText = resources.getString(R.string.conversation_list_snippet_picture);
                } else if (ContentType.isVideoType(previewContentType)) {
                    snippetText = resources.getString(R.string.conversation_list_snippet_video);
                } else if (ContentType.isVCardType(previewContentType)) {
                    snippetText = resources.getString(R.string.conversation_list_snippet_vcard);
                }
            }
            return snippetText;
        }

        /**
         * @return the "View more conversations" view. When the user taps this item, they're
         * taken to the Bugle's conversation list.
         */
        @Override
        protected RemoteViews getViewMoreItemsView() {
            if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
                LogUtil.v(TAG, "getViewMoreItemsView");
            }
            final RemoteViews view = new RemoteViews(mContext.getPackageName(),
                    R.layout.widget_loading);
            view.setTextViewText(
                    R.id.loading_text, mContext.getText(R.string.view_more_conversations));

            // Tapping this "More conversations" item should take us to the ConversationList.
            // However, the list view is primed with an intent to go to the Conversation activity.
            // Each normal conversation list item sets the fill-in intent with the
            // ConversationId for that particular conversation. In other words, the only place
            // we can go is the ConversationActivity. We add an extra here to tell the
            // ConversationActivity to really take us to the ConversationListActivity.
            final Intent intent = new Intent();
            intent.putExtra(UIIntents.UI_INTENT_EXTRA_GOTO_CONVERSATION_LIST, true);
            view.setOnClickFillInIntent(R.id.widget_loading, intent);
            return view;
        }

        @Override
        public RemoteViews getLoadingView() {
            RemoteViews view = new RemoteViews(mContext.getPackageName(), R.layout.widget_loading);
            view.setTextViewText(
                    R.id.loading_text, mContext.getText(R.string.loading_conversations));
            return view;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        protected int getMainLayoutId() {
            return R.layout.widget_conversation_list;
        }
    }

}