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     public void update(WorkingMessage msg) {
     70         hideView();
     71         mView = null;
     72 
     73         // If there's no attachment, we have nothing to do.
     74         if (!msg.hasAttachment()) {
     75             return;
     76         }
     77 
     78         // Get the slideshow from the message.
     79         mSlideshow = msg.getSlideshow();
     80 
     81         mView = createView();
     82 
     83         if ((mPresenter == null) || !mSlideshow.equals(mPresenter.getModel())) {
     84             mPresenter = PresenterFactory.getPresenter(
     85                     "MmsThumbnailPresenter", mContext, mView, mSlideshow);
     86         } else {
     87             mPresenter.setView(mView);
     88         }
     89 
     90         mPresenter.present();
     91     }
     92 
     93     public void setHandler(Handler handler) {
     94         mHandler = handler;
     95     }
     96 
     97     public void setCanSend(boolean enable) {
     98         if (mCanSend != enable) {
     99             mCanSend = enable;
    100             updateSendButton();
    101         }
    102     }
    103 
    104     private void updateSendButton() {
    105         if (null != mSendButton) {
    106             mSendButton.setEnabled(mCanSend);
    107             mSendButton.setFocusable(mCanSend);
    108         }
    109     }
    110 
    111     public void hideView() {
    112         if (mView != null) {
    113             ((View)mView).setVisibility(View.GONE);
    114         }
    115     }
    116 
    117     private View getStubView(int stubId, int viewId) {
    118         View view = findViewById(viewId);
    119         if (view == null) {
    120             ViewStub stub = (ViewStub) findViewById(stubId);
    121             view = stub.inflate();
    122         }
    123 
    124         return view;
    125     }
    126 
    127     private class MessageOnClick implements OnClickListener {
    128         private int mWhat;
    129 
    130         public MessageOnClick(int what) {
    131             mWhat = what;
    132         }
    133 
    134         public void onClick(View v) {
    135             Message msg = Message.obtain(mHandler, mWhat);
    136             msg.sendToTarget();
    137         }
    138     }
    139 
    140     private SlideViewInterface createView() {
    141         boolean inPortrait = inPortraitMode();
    142         if (mSlideshow.size() > 1) {
    143             return createSlideshowView(inPortrait);
    144         }
    145 
    146         SlideModel slide = mSlideshow.get(0);
    147         if (slide.hasImage()) {
    148             return createMediaView(
    149                     inPortrait ? R.id.image_attachment_view_portrait_stub :
    150                         R.id.image_attachment_view_landscape_stub,
    151                     inPortrait ? R.id.image_attachment_view_portrait :
    152                         R.id.image_attachment_view_landscape,
    153                     R.id.view_image_button, R.id.replace_image_button, R.id.remove_image_button,
    154                     MSG_VIEW_IMAGE, MSG_REPLACE_IMAGE, MSG_REMOVE_ATTACHMENT);
    155         } else if (slide.hasVideo()) {
    156             return createMediaView(
    157                     inPortrait ? R.id.video_attachment_view_portrait_stub :
    158                         R.id.video_attachment_view_landscape_stub,
    159                     inPortrait ? R.id.video_attachment_view_portrait :
    160                         R.id.video_attachment_view_landscape,
    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                     inPortrait ? R.id.audio_attachment_view_portrait_stub :
    166                         R.id.audio_attachment_view_landscape_stub,
    167                     inPortrait ? R.id.audio_attachment_view_portrait :
    168                         R.id.audio_attachment_view_landscape,
    169                     R.id.play_audio_button, R.id.replace_audio_button, R.id.remove_audio_button,
    170                     MSG_PLAY_AUDIO, MSG_REPLACE_AUDIO, MSG_REMOVE_ATTACHMENT);
    171         } else {
    172             throw new IllegalArgumentException();
    173         }
    174     }
    175 
    176     /**
    177      * What is the current orientation?
    178      */
    179     private boolean inPortraitMode() {
    180         final Configuration configuration = mContext.getResources().getConfiguration();
    181         return configuration.orientation == Configuration.ORIENTATION_PORTRAIT;
    182     }
    183 
    184     private SlideViewInterface createMediaView(
    185             int stub_view_id, int real_view_id,
    186             int view_button_id, int replace_button_id, int remove_button_id,
    187             int view_message, int replace_message, int remove_message) {
    188         LinearLayout view = (LinearLayout)getStubView(stub_view_id, real_view_id);
    189         view.setVisibility(View.VISIBLE);
    190 
    191         Button viewButton = (Button) view.findViewById(view_button_id);
    192         Button replaceButton = (Button) view.findViewById(replace_button_id);
    193         Button removeButton = (Button) view.findViewById(remove_button_id);
    194 
    195         viewButton.setOnClickListener(new MessageOnClick(view_message));
    196         replaceButton.setOnClickListener(new MessageOnClick(replace_message));
    197         removeButton.setOnClickListener(new MessageOnClick(remove_message));
    198 
    199         return (SlideViewInterface) view;
    200     }
    201 
    202     private SlideViewInterface createSlideshowView(boolean inPortrait) {
    203         LinearLayout view =(LinearLayout) getStubView(inPortrait ?
    204                 R.id.slideshow_attachment_view_portrait_stub :
    205                 R.id.slideshow_attachment_view_landscape_stub,
    206                 inPortrait ? R.id.slideshow_attachment_view_portrait :
    207                     R.id.slideshow_attachment_view_landscape);
    208         view.setVisibility(View.VISIBLE);
    209 
    210         Button editBtn = (Button) view.findViewById(R.id.edit_slideshow_button);
    211         mSendButton = (Button) view.findViewById(R.id.send_slideshow_button);
    212         updateSendButton();
    213         final ImageButton playBtn = (ImageButton) view.findViewById(
    214                 R.id.play_slideshow_button);
    215 
    216         editBtn.setOnClickListener(new MessageOnClick(MSG_EDIT_SLIDESHOW));
    217         mSendButton.setOnClickListener(new MessageOnClick(MSG_SEND_SLIDESHOW));
    218         playBtn.setOnClickListener(new MessageOnClick(MSG_PLAY_SLIDESHOW));
    219 
    220         return (SlideViewInterface) view;
    221     }
    222 }
    223