blob: 8dd69e42aac09644ce713a1e1a24c145a364effa [file] [log] [blame]
Owen Linf9a0a432011-08-17 22:07:43 +08001/*
2 * Copyright (C) 2007 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
17package com.android.gallery3d.app;
18
Ray Chendcb265c2011-08-30 12:06:17 +080019import android.app.ActionBar;
Owen Linf9a0a432011-08-17 22:07:43 +080020import android.app.Activity;
Pin Ting4ba06f02012-02-11 02:53:53 +080021import android.content.AsyncQueryHandler;
Owen Linbe055bc2012-05-15 16:56:59 -070022import android.content.ContentResolver;
Owen Linf9a0a432011-08-17 22:07:43 +080023import android.content.Intent;
24import android.content.pm.ActivityInfo;
25import android.database.Cursor;
Ray Chen7b9c72a2012-03-07 18:22:17 +080026import android.graphics.Bitmap;
27import android.graphics.drawable.BitmapDrawable;
Owen Linf9a0a432011-08-17 22:07:43 +080028import android.media.AudioManager;
Ray Chene6e4beb2011-10-11 11:37:18 +080029import android.net.Uri;
Owen Linf9a0a432011-08-17 22:07:43 +080030import android.os.Bundle;
31import android.provider.MediaStore;
Pin Ting4ba06f02012-02-11 02:53:53 +080032import android.provider.OpenableColumns;
Chih-Chung Changfc8c5032011-11-29 14:21:11 +080033import android.view.KeyEvent;
Ray Chene6e4beb2011-10-11 11:37:18 +080034import android.view.Menu;
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080035import android.view.MenuItem;
Owen Linf9a0a432011-08-17 22:07:43 +080036import android.view.View;
37import android.view.Window;
38import android.view.WindowManager;
Ray Chene6e4beb2011-10-11 11:37:18 +080039import android.widget.ShareActionProvider;
40
41import com.android.gallery3d.R;
Pin Ting4ba06f02012-02-11 02:53:53 +080042import com.android.gallery3d.common.Utils;
Owen Linf9a0a432011-08-17 22:07:43 +080043
44/**
45 * This activity plays a video from a specified URI.
Ray Chen7b9c72a2012-03-07 18:22:17 +080046 *
47 * The client of this activity can pass a logo bitmap in the intent (KEY_LOGO_BITMAP)
48 * to set the action bar logo so the playback process looks more seamlessly integrated with
49 * the original activity.
Owen Linf9a0a432011-08-17 22:07:43 +080050 */
51public class MovieActivity extends Activity {
52 @SuppressWarnings("unused")
53 private static final String TAG = "MovieActivity";
Ray Chena3871052012-05-17 12:15:08 +080054 public static final String KEY_LOGO_BITMAP = "logo-bitmap";
55 public static final String KEY_TREAT_UP_AS_BACK = "treat-up-as-back";
Owen Linf9a0a432011-08-17 22:07:43 +080056
57 private MoviePlayer mPlayer;
58 private boolean mFinishOnCompletion;
Ray Chene6e4beb2011-10-11 11:37:18 +080059 private Uri mUri;
Ray Chena3871052012-05-17 12:15:08 +080060 private boolean mTreatUpAsBack;
Owen Linf9a0a432011-08-17 22:07:43 +080061
62 @Override
63 public void onCreate(Bundle savedInstanceState) {
64 super.onCreate(savedInstanceState);
65
66 requestWindowFeature(Window.FEATURE_ACTION_BAR);
67 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
68
69 setContentView(R.layout.movie_view);
Chih-Chung Changfb559942012-03-12 12:46:25 +080070 View rootView = findViewById(R.id.movie_view_root);
Ray Chen43650c62012-05-11 21:50:28 +080071 rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Owen Linf9a0a432011-08-17 22:07:43 +080072 Intent intent = getIntent();
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080073 initializeActionBar(intent);
Chih-Chung Chang209a9162011-10-14 16:09:09 +080074 mFinishOnCompletion = intent.getBooleanExtra(
75 MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
Ray Chena3871052012-05-17 12:15:08 +080076 mTreatUpAsBack = intent.getBooleanExtra(KEY_TREAT_UP_AS_BACK, false);
Chih-Chung Chang209a9162011-10-14 16:09:09 +080077 mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState,
78 !mFinishOnCompletion) {
Owen Linf9a0a432011-08-17 22:07:43 +080079 @Override
80 public void onCompletion() {
81 if (mFinishOnCompletion) {
82 finish();
83 }
84 }
85 };
86 if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) {
87 int orientation = intent.getIntExtra(
88 MediaStore.EXTRA_SCREEN_ORIENTATION,
89 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
90 if (orientation != getRequestedOrientation()) {
91 setRequestedOrientation(orientation);
92 }
93 }
Owen Linf9a0a432011-08-17 22:07:43 +080094 Window win = getWindow();
95 WindowManager.LayoutParams winParams = win.getAttributes();
96 winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
Chih-Chung Chang81760122011-09-27 16:50:16 +080097 winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
Owen Linf9a0a432011-08-17 22:07:43 +080098 win.setAttributes(winParams);
Owen Linf9a0a432011-08-17 22:07:43 +080099 }
100
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800101 private void initializeActionBar(Intent intent) {
Pin Ting143594f2012-01-18 18:15:27 +0800102 mUri = intent.getData();
Pin Ting4ba06f02012-02-11 02:53:53 +0800103 final ActionBar actionBar = getActionBar();
Ray Chen7b9c72a2012-03-07 18:22:17 +0800104 Bitmap logo = intent.getParcelableExtra(KEY_LOGO_BITMAP);
105 if (logo != null) {
106 actionBar.setLogo(new BitmapDrawable(getResources(), logo));
107 }
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800108 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
109 ActionBar.DISPLAY_HOME_AS_UP);
Pin Ting143594f2012-01-18 18:15:27 +0800110
Pin Ting4ba06f02012-02-11 02:53:53 +0800111 String title = intent.getStringExtra(Intent.EXTRA_TITLE);
112 if (title != null) {
113 actionBar.setTitle(title);
114 } else {
115 // Displays the filename as title, reading the filename from the
116 // interface: {@link android.provider.OpenableColumns#DISPLAY_NAME}.
117 AsyncQueryHandler queryHandler =
118 new AsyncQueryHandler(getContentResolver()) {
119 @Override
120 protected void onQueryComplete(int token, Object cookie,
121 Cursor cursor) {
122 try {
123 if ((cursor != null) && cursor.moveToFirst()) {
124 String displayName = cursor.getString(0);
125
126 // Just show empty title if other apps don't set
127 // DISPLAY_NAME
128 actionBar.setTitle((displayName == null) ? "" :
129 displayName);
130 }
131 } finally {
132 Utils.closeSilently(cursor);
133 }
134 }
135 };
136 queryHandler.startQuery(0, null, mUri,
137 new String[] {OpenableColumns.DISPLAY_NAME}, null, null,
138 null);
139 }
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800140 }
141
142 @Override
Ray Chene6e4beb2011-10-11 11:37:18 +0800143 public boolean onCreateOptionsMenu(Menu menu) {
144 super.onCreateOptionsMenu(menu);
145
146 getMenuInflater().inflate(R.menu.movie, menu);
147 ShareActionProvider provider = GalleryActionBar.initializeShareActionProvider(menu);
148
Owen Linbe055bc2012-05-15 16:56:59 -0700149 // Document says EXTRA_STREAM should be a content: Uri
150 // So, we only share the video if it's "content:".
151 if (provider != null && ContentResolver.SCHEME_CONTENT
152 .equals(mUri.getScheme())) {
Ray Chene6e4beb2011-10-11 11:37:18 +0800153 Intent intent = new Intent(Intent.ACTION_SEND);
154 intent.setType("video/*");
155 intent.putExtra(Intent.EXTRA_STREAM, mUri);
156 provider.setShareIntent(intent);
157 }
158
159 return true;
160 }
161
162 @Override
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800163 public boolean onOptionsItemSelected(MenuItem item) {
164 if (item.getItemId() == android.R.id.home) {
Ray Chena3871052012-05-17 12:15:08 +0800165 if (mTreatUpAsBack) {
166 finish();
167 } else {
168 startActivity(new Intent(this, Gallery.class));
169 finish();
170 }
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800171 return true;
172 }
173 return false;
Owen Linf9a0a432011-08-17 22:07:43 +0800174 }
175
176 @Override
177 public void onStart() {
178 ((AudioManager) getSystemService(AUDIO_SERVICE))
179 .requestAudioFocus(null, AudioManager.STREAM_MUSIC,
180 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
181 super.onStart();
182 }
183
184 @Override
185 protected void onStop() {
186 ((AudioManager) getSystemService(AUDIO_SERVICE))
187 .abandonAudioFocus(null);
188 super.onStop();
189 }
190
191 @Override
192 public void onPause() {
193 mPlayer.onPause();
194 super.onPause();
195 }
196
197 @Override
198 public void onResume() {
199 mPlayer.onResume();
200 super.onResume();
201 }
202
203 @Override
Owen Lin540e4692011-09-14 19:49:03 +0800204 public void onSaveInstanceState(Bundle outState) {
205 super.onSaveInstanceState(outState);
206 mPlayer.onSaveInstanceState(outState);
207 }
208
209 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800210 public void onDestroy() {
211 mPlayer.onDestroy();
212 super.onDestroy();
213 }
Chih-Chung Changfc8c5032011-11-29 14:21:11 +0800214
215 @Override
216 public boolean onKeyDown(int keyCode, KeyEvent event) {
217 return mPlayer.onKeyDown(keyCode, event)
218 || super.onKeyDown(keyCode, event);
219 }
220
221 @Override
222 public boolean onKeyUp(int keyCode, KeyEvent event) {
223 return mPlayer.onKeyUp(keyCode, event)
224 || super.onKeyUp(keyCode, event);
225 }
Owen Linf9a0a432011-08-17 22:07:43 +0800226}