Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2008 Esmertec AG.
      3  * Copyright (C) 2008 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.mms.ui;
     19 
     20 import com.android.mms.R;
     21 import com.android.mms.data.WorkingMessage;
     22 import com.android.mms.model.SlideModel;
     23 import com.android.mms.model.SlideshowModel;
     24 import com.android.mms.util.ItemLoadedCallback;
     25 
     26 import android.content.Context;
     27 import android.content.res.Configuration;
     28 import android.os.Handler;
     29 import android.os.Message;
     30 import android.util.AttributeSet;
     31 import android.util.Log;
     32 import android.view.View;
     33 import android.view.ViewStub;
     34 import android.widget.Button;
     35 import android.widget.ImageButton;
     36 import android.widget.LinearLayout;
     37 
     38 /**
     39  * This is an embedded editor/view to add photos and sound/video clips
     40  * into a multimedia message.
     41  */
     42 public class AttachmentEditor extends LinearLayout {
     43     private static final String TAG = "AttachmentEditor";
     44 
     45     static final int MSG_EDIT_SLIDESHOW   = 1;
     46     static final int MSG_SEND_SLIDESHOW   = 2;
     47     static final int MSG_PLAY_SLIDESHOW   = 3;
     48     static final int MSG_REPLACE_IMAGE    = 4;
     49     static final int MSG_REPLACE_VIDEO    = 5;
     50     static final int MSG_REPLACE_AUDIO    = 6;
     51     static final int MSG_PLAY_VIDEO       = 7;
     52     static final int MSG_PLAY_AUDIO       = 8;
     53     static final int MSG_VIEW_IMAGE       = 9;
     54     static final int MSG_REMOVE_ATTACHMENT = 10;
     55 
     56     private final Context mContext;
     57     private Handler mHandler;
     58 
     59     private SlideViewInterface mView;
     60     private SlideshowModel mSlideshow;
     61     private Presenter mPresenter;
     62     private boolean mCanSend;
     63     private Button mSendButton;
     64 
     65     public AttachmentEditor(Context context, AttributeSet attr) {
     66         super(context, attr);
     67         mContext = context;
     68     }
     69 
     70     /**
     71      * Returns true if the attachment editor has an attachment to show.
     72      */
     73     public boolean update(WorkingMessage msg) {
     74         hideView();
     75         mView = null;
     76 
     77         // If there's no attachment, we have nothing to do.
     78         if (!msg.hasAttachment()) {
     79             return false;
     80         }
     81 
     82         // Get the slideshow from the message.
     83         mSlideshow = msg.getSlideshow();
     84 
     85         mView = createView();
     86 
     87         if ((mPresenter == null) || !mSlideshow.equals(mPresenter.getModel())) {
     88             mPresenter = PresenterFactory.getPresenter(
     89                     "MmsThumbnailPresenter", mContext, mView, mSlideshow);
     90         } else {
     91             mPresenter.setView(mView);
     92         }
     93 
     94         mPresenter.present(null);
     95         return true;
     96     }
     97 
     98     public void setHandler(Handler handler) {
     99         mHandler = handler;
    100     }
    101 
    102     public void setCanSend(boolean enable) {
    103         if (mCanSend != enable) {
    104             mCanSend = enable;
    105             updateSendButton();
    106         }
    107     }
    108 
    109     private void updateSendButton() {
    110         if (null != mSendButton) {
    111             mSendButton.setEnabled(mCanSend);
    112             mSendButton.setFocusable(mCanSend);
    113         }
    114     }
    115 
    116     public void hideView() {
    117         if (mView != null) {
    118             ((View)mView).setVisibility(View.GONE);
    119         }
    120     }
    121 
    122     private View getStubView(int stubId, int viewId) {
    123         View view = findViewById(viewId);
    124         if (view == null) {
    125             ViewStub stub = (ViewStub) findViewById(stubId);
    126             view = stub.inflate();
    127         }
    128 
    129         return view;
    130     }
    131 
    132     private class MessageOnClick implements OnClickListener {
    133         private int mWhat;
    134 
    135         public MessageOnClick(int what) {
    136             mWhat = what;
    137         }
    138 
    139         public void onClick(View v) {
    140             Message msg = Message.obtain(mHandler, mWhat);
    141             msg.sendToTarget();
    142         }
    143     }
    144 
    145     private SlideViewInterface createView() {
    146         boolean inPortrait = inPortraitMode();
    147         if (mSlideshow.size() > 1) {
    148             return createSlideshowView(inPortrait);
    149         }
    150 
    151         SlideModel slide = mSlideshow.get(0);
    152         if (slide.hasImage()) {
    153             return createMediaView(
    154                     R.id.image_attachment_view_stub,
    155                     R.id.image_attachment_view,
    156                     R.id.view_image_button, R.id.replace_image_button, R.id.remove_image_button,
    157                     MSG_VIEW_IMAGE, MSG_REPLACE_IMAGE, MSG_REMOVE_ATTACHMENT);
    158         } else if (slide.hasVideo()) {
    159             return createMediaView(
    160                     R.id.video_attachment_view_stub,
    161                     R.id.video_attachment_view,
    162                     R.id.view_video_button, R.id.replace_video_button, R.id.remove_video_button,
    163                     MSG_PLAY_VIDEO, MSG_REPLACE_VIDEO, MSG_REMOVE_ATTACHMENT);
    164         } else if (slide.hasAudio()) {
    165             return createMediaView(
    166                     R.id.audio_attachment_view_stub,
    167                     R.id.audio_attachment_view,
    168                     R.id.play_audio_button, R.id.replace_audio_button, R.id.remove_audio_button,
    169                     MSG_PLAY_AUDIO, MSG_REPLACE_AUDIO, MSG_REMOVE_ATTACHMENT);
    170         } else {
    171             throw new IllegalArgumentException();
    172         }
    173     }
    174 
    175     /**
    176      * What is the current orientation?
    177      */
    178     private boolean inPortraitMode() {
    179         final Configuration configuration = mContext.getResources().getConfiguration();
    180         return configuration.orientation == Configuration.ORIENTATION_PORTRAIT;
    181     }
    182 
    183     private SlideViewInterface createMediaView(
    184             int stub_view_id, int real_view_id,
    185             int view_button_id, int replace_button_id, int remove_button_id,
    186             int view_message, int replace_message, int remove_message) {
    187         LinearLayout view = (LinearLayout)getStubView(stub_view_id, real_view_id);
    188         view.setVisibility(View.VISIBLE);
    189 
    190         Button viewButton = (Button) view.findViewById(view_button_id);
    191         Button replaceButton = (Button) view.findViewById(replace_button_id);
    192         Button removeButton = (Button) view.findViewById(remove_button_id);
    193 
    194         viewButton.setOnClickListener(new MessageOnClick(view_message));
    195         replaceButton.setOnClickListener(new MessageOnClick(replace_message));
    196         removeButton.setOnClickListener(new MessageOnClick(remove_message));
    197 
    198         return (SlideViewInterface) view;
    199     }
    200 
    201     private SlideViewInterface createSlideshowView(boolean inPortrait) {
    202         LinearLayout view =(LinearLayout) getStubView(
    203                 R.id.slideshow_attachment_view_stub,
    204                 R.id.slideshow_attachment_view);
    205         view.setVisibility(View.VISIBLE);
    206 
    207         Button editBtn = (Button) view.findViewById(R.id.edit_slideshow_button);
    208         mSendButton = (Button) view.findViewById(R.id.send_slideshow_button);
    209         updateSendButton();
    210         final ImageButton playBtn = (ImageButton) view.findViewById(
    211                 R.id.play_slideshow_button);
    212 
    213         editBtn.setOnClickListener(new MessageOnClick(MSG_EDIT_SLIDESHOW));
    214         mSendButton.setOnClickListener(new MessageOnClick(MSG_SEND_SLIDESHOW));
    215         playBtn.setOnClickListener(new MessageOnClick(MSG_PLAY_SLIDESHOW));
    216 
    217         Button removeButton = (Button) view.findViewById(R.id.remove_slideshow_button);
    218         removeButton.setOnClickListener(new MessageOnClick(MSG_REMOVE_ATTACHMENT));
    219 
    220         return (SlideViewInterface) view;
    221     }
    222 }
    223