• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.videoeditor;
18 
19 import android.content.Intent;
20 import android.graphics.Bitmap;
21 import android.graphics.BitmapFactory;
22 import android.os.Bundle;
23 import android.text.Editable;
24 import android.text.TextWatcher;
25 import android.util.Log;
26 import android.view.View;
27 import android.widget.Button;
28 import android.widget.ImageView;
29 import android.widget.TextView;
30 
31 import com.android.videoeditor.service.MovieOverlay;
32 import com.android.videoeditor.util.ImageUtils;
33 
34 /**
35  * Activity that lets user add or edit title overlay of a media item.
36  */
37 public class OverlayTitleEditor extends NoSearchActivity {
38     // Parameter names
39     public static final String PARAM_OVERLAY_ATTRIBUTES = "attributes";
40     public static final String PARAM_OVERLAY_ID = "overlay_id";
41     public static final String PARAM_MEDIA_ITEM_ID = "media_item_id";
42 
43     private static final String LOG_TAG = "OverlayTitleEditor";
44     private static final int REQUEST_CODE_PICK_TITLE_TEMPLATE = 1;
45 
46     private int mOverlayType;
47     private ImageView mOverlayImageView;
48     private Button mOverlayChangeTitleTemplateButton;
49     private TextView mTitleView, mSubtitleView;
50     private Bitmap mOverlayBitmap;
51     private int mPreviewWidth, mPreviewHeight;
52 
53     private final TextWatcher mTextWatcher = new TextWatcher() {
54         @Override
55         public void onTextChanged(CharSequence s, int start, int before, int count) {
56             // no-op
57         }
58 
59         @Override
60         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
61             // no-op
62         }
63 
64         @Override
65         public void afterTextChanged(Editable s) {
66             // Update preview image as user types in the title or sub-title fields.
67             updatePreviewImage();
68             invalidateOptionsMenu();
69         }
70     };
71 
72     @Override
onCreate(Bundle savedInstanceState)73     public void onCreate(Bundle savedInstanceState) {
74         super.onCreate(savedInstanceState);
75         setContentView(R.layout.overlay_title_editor);
76         setFinishOnTouchOutside(true);
77 
78         mOverlayImageView = (ImageView) findViewById(R.id.overlay_preview);
79 
80         mOverlayChangeTitleTemplateButton = (Button) findViewById(
81                 R.id.overlay_change_title_template);
82         mOverlayChangeTitleTemplateButton.setOnClickListener(new View.OnClickListener() {
83                 @Override
84                 public void onClick(View v) {
85                     launchOverlayTitleTemplatePicker();
86                 }
87         });
88 
89         mTitleView = (TextView) findViewById(R.id.overlay_title);
90         mTitleView.addTextChangedListener(mTextWatcher);
91 
92         mSubtitleView = (TextView) findViewById(R.id.overlay_subtitle);
93         mSubtitleView.addTextChangedListener(mTextWatcher);
94 
95         // Determine bitmap dimensions.
96         final BitmapFactory.Options dbo = new BitmapFactory.Options();
97         dbo.inJustDecodeBounds = true;
98         BitmapFactory.decodeResource(getResources(), R.drawable.effects_generic, dbo);
99         mPreviewWidth = dbo.outWidth;
100         mPreviewHeight = dbo.outHeight;
101 
102         final Bundle attributes = getIntent().getBundleExtra(PARAM_OVERLAY_ATTRIBUTES);
103         if (attributes != null) {
104             // The media item already has a title overlay. Fill in the contents in the input fields
105             // and let user edit them.
106             mOverlayType = MovieOverlay.getType(attributes);
107             mTitleView.setText(MovieOverlay.getTitle(attributes));
108             mSubtitleView.setText(MovieOverlay.getSubtitle(attributes));
109         } else {
110             // Default overlay type that puts title at the bottom of the media item.
111             mOverlayType = MovieOverlay.OVERLAY_TYPE_BOTTOM_1;
112         }
113         updatePreviewImage();
114     }
115 
launchOverlayTitleTemplatePicker()116     private void launchOverlayTitleTemplatePicker() {
117         final Intent intent = new Intent(this, OverlayTitleTemplatePicker.class);
118         startActivityForResult(intent, REQUEST_CODE_PICK_TITLE_TEMPLATE);
119     }
120 
updatePreviewImage()121     private void updatePreviewImage() {
122         mOverlayBitmap = ImageUtils.buildOverlayBitmap(this, mOverlayBitmap, mOverlayType,
123                 mTitleView.getText().toString(), mSubtitleView.getText().toString(),
124                 mPreviewWidth, mPreviewHeight);
125         mOverlayImageView.setImageBitmap(mOverlayBitmap);
126     }
127 
128     @Override
onActivityResult(int requestCode, int resultCode, Intent extras)129     protected void onActivityResult(int requestCode, int resultCode, Intent extras) {
130         if (resultCode != RESULT_OK)
131             return;
132 
133         switch (requestCode) {
134             case REQUEST_CODE_PICK_TITLE_TEMPLATE:
135                 // Get chosen overlay type from extras and then update preview image.
136                 final Bundle attributes = extras.getBundleExtra(
137                         OverlayTitleTemplatePicker.PARAM_OVERLAY_ATTRIBUTES);
138                 mOverlayType = MovieOverlay.getType(attributes);
139                 updatePreviewImage();
140                 break;
141             default:
142                 Log.w(LOG_TAG, "Invalid request code received: " + requestCode);
143                 break;
144         }
145     }
146 
147     /**
148      * Handler used to responds to "OK" and "Cancel" buttons.
149      * @param target "OK" or "Cancel" button
150      */
onClickHandler(View target)151     public void onClickHandler(View target) {
152         switch (target.getId()) {
153             case R.id.overlay_ok: {
154                 // Extras to be returned to the caller of this activity.
155                 final Intent extras = new Intent();
156                 extras.putExtra(PARAM_MEDIA_ITEM_ID,
157                         getIntent().getStringExtra(PARAM_MEDIA_ITEM_ID));
158 
159                 String overlayId = getIntent().getStringExtra(PARAM_OVERLAY_ID);
160                 if (overlayId != null) {
161                     extras.putExtra(PARAM_OVERLAY_ID, overlayId);
162                 }
163 
164                 final TextView titleView = (TextView) findViewById(R.id.overlay_title);
165                 final TextView subTitleView = (TextView) findViewById(R.id.overlay_subtitle);
166                 final Bundle attributes = MovieOverlay.buildUserAttributes(mOverlayType,
167                         titleView.getText().toString(), subTitleView.getText().toString());
168                 extras.putExtra(PARAM_OVERLAY_ATTRIBUTES, attributes);
169 
170                 setResult(RESULT_OK, extras);
171                 finish();
172                 break;
173             }
174 
175             case R.id.overlay_cancel: {
176                 finish();
177                 break;
178             }
179         }
180     }
181 }
182