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