summaryrefslogtreecommitdiffstats
path: root/java/com/android/incallui/sessiondata/MultimediaFragment.java
blob: 85a60b6e3ae2e949f00e69dba1aecb0782ea14d7 (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
/*
 * Copyright (C) 2016 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.incallui.sessiondata;

import android.graphics.drawable.Drawable;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.dialer.common.FragmentUtils;
import com.android.dialer.common.LogUtil;
import com.android.dialer.multimedia.MultimediaData;
import com.android.incallui.maps.MapsComponent;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

/**
 * Displays info from {@link MultimediaData MultimediaData}.
 *
 * <p>Currently displays image, location (as a map), and message that come bundled with
 * MultimediaData when calling {@link #newInstance(MultimediaData, boolean, boolean, boolean)}.
 */
public class MultimediaFragment extends Fragment implements AvatarPresenter {

  private static final String ARG_SUBJECT = "subject";
  private static final String ARG_IMAGE = "image";
  private static final String ARG_LOCATION = "location";
  private static final String ARG_INTERACTIVE = "interactive";
  private static final String ARG_SHOW_AVATAR = "show_avatar";
  private static final String ARG_IS_SPAM = "is_spam";
  private ImageView avatarImageView;

  private boolean showAvatar;
  private boolean isSpam;

  public static MultimediaFragment newInstance(
      @NonNull MultimediaData multimediaData,
      boolean isInteractive,
      boolean showAvatar,
      boolean isSpam) {
    return newInstance(
        multimediaData.getText(),
        multimediaData.getImageUri(),
        multimediaData.getLocation(),
        isInteractive,
        showAvatar,
        isSpam);
  }

  @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
  public static MultimediaFragment newInstance(
      @Nullable String subject,
      @Nullable Uri imageUri,
      @Nullable Location location,
      boolean isInteractive,
      boolean showAvatar,
      boolean isSpam) {
    Bundle args = new Bundle();
    args.putString(ARG_SUBJECT, subject);
    args.putParcelable(ARG_IMAGE, imageUri);
    args.putParcelable(ARG_LOCATION, location);
    args.putBoolean(ARG_INTERACTIVE, isInteractive);
    args.putBoolean(ARG_SHOW_AVATAR, showAvatar);
    args.putBoolean(ARG_IS_SPAM, isSpam);
    MultimediaFragment fragment = new MultimediaFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override
  public void onCreate(@Nullable Bundle bundle) {
    super.onCreate(bundle);
    showAvatar = getArguments().getBoolean(ARG_SHOW_AVATAR);
    isSpam = getArguments().getBoolean(ARG_IS_SPAM);
  }

  @Nullable
  @Override
  public View onCreateView(
      LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
    if (isSpam) {
      return layoutInflater.inflate(R.layout.fragment_spam, viewGroup, false);
    }

    boolean hasImage = getImageUri() != null;
    boolean hasSubject = !TextUtils.isEmpty(getSubject());
    boolean hasMap = getLocation() != null;
    if (hasMap && MapsComponent.get(getContext()).getMaps().isAvailable()) {
      if (hasImage) {
        if (hasSubject) {
          return layoutInflater.inflate(
              R.layout.fragment_composer_text_image_frag, viewGroup, false);
        } else {
          return layoutInflater.inflate(R.layout.fragment_composer_image_frag, viewGroup, false);
        }
      } else if (hasSubject) {
        return layoutInflater.inflate(R.layout.fragment_composer_text_frag, viewGroup, false);
      } else {
        return layoutInflater.inflate(R.layout.fragment_composer_frag, viewGroup, false);
      }
    } else if (hasImage) {
      if (hasSubject) {
        return layoutInflater.inflate(R.layout.fragment_composer_text_image, viewGroup, false);
      } else {
        return layoutInflater.inflate(R.layout.fragment_composer_image, viewGroup, false);
      }
    } else {
      return layoutInflater.inflate(R.layout.fragment_composer_text, viewGroup, false);
    }
  }

  @Override
  public void onViewCreated(View view, @Nullable Bundle bundle) {
    super.onViewCreated(view, bundle);
    View container = view.findViewById(R.id.answer_message_container);
    if (container != null) {
      container.setClipToOutline(true);
    }

    // If the call is spam and only has a subject, update the view to reflect that.
    if (isSpam
        && getLocation() == null
        && getImageUri() == null
        && !TextUtils.isEmpty(getSubject())) {
      ((ImageView) view.findViewById(R.id.spam_image))
          .setImageResource(R.drawable.quantum_ic_message_white_24);
      ((TextView) view.findViewById(R.id.spam_text)).setText(R.string.spam_message_text);
    }

    TextView messageText = (TextView) view.findViewById(R.id.answer_message_text);
    if (messageText != null) {
      messageText.setText(getSubject());
    }
    ImageView mainImage = (ImageView) view.findViewById(R.id.answer_message_image);
    if (mainImage != null) {
      Glide.with(this)
          .load(getImageUri())
          .transition(DrawableTransitionOptions.withCrossFade())
          .listener(
              new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(
                    @Nullable GlideException e,
                    Object model,
                    Target<Drawable> target,
                    boolean isFirstResource) {
                  view.findViewById(R.id.loading_spinner).setVisibility(View.GONE);
                  LogUtil.e("MultimediaFragment.onLoadFailed", null, e);
                  // TODO(b/34720074) handle error cases nicely
                  return false; // Let Glide handle the rest
                }

                @Override
                public boolean onResourceReady(
                    Drawable drawable,
                    Object model,
                    Target<Drawable> target,
                    DataSource dataSource,
                    boolean isFirstResource) {
                  view.findViewById(R.id.loading_spinner).setVisibility(View.GONE);
                  return false;
                }
              })
          .into(mainImage);
      mainImage.setClipToOutline(true);
    }
    FrameLayout fragmentHolder = (FrameLayout) view.findViewById(R.id.answer_message_frag);
    if (fragmentHolder != null) {
      fragmentHolder.setClipToOutline(true);
      Fragment mapFragment =
          MapsComponent.get(getContext()).getMaps().createStaticMapFragment(getLocation());
      getChildFragmentManager()
          .beginTransaction()
          .replace(R.id.answer_message_frag, mapFragment)
          .commitNow();
    }
    avatarImageView = ((ImageView) view.findViewById(R.id.answer_message_avatar));
    if (avatarImageView != null) {
      avatarImageView.setVisibility(showAvatar ? View.VISIBLE : View.GONE);
    }

    Holder parent = FragmentUtils.getParent(this, Holder.class);
    if (parent != null) {
      parent.updateAvatar(this);
    }
  }

  @Nullable
  @Override
  public ImageView getAvatarImageView() {
    return avatarImageView;
  }

  @Override
  public int getAvatarSize() {
    return getResources().getDimensionPixelSize(R.dimen.answer_message_avatar_size);
  }

  @Override
  public boolean shouldShowAnonymousAvatar() {
    return showAvatar;
  }

  @Nullable
  public String getSubject() {
    return getArguments().getString(ARG_SUBJECT);
  }

  @Nullable
  public Uri getImageUri() {
    return getArguments().getParcelable(ARG_IMAGE);
  }

  @Nullable
  public Location getLocation() {
    return getArguments().getParcelable(ARG_LOCATION);
  }

  /** Interface for notifying the fragment parent of changes. */
  public interface Holder {
    void updateAvatar(AvatarPresenter sessionDataScreen);
  }
}