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