blob: aa55c9bdf4246f8d7ce3cd54169e634eeff1f433 [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
Owen Lin7a5e1e72012-06-20 16:54:24 +080019import android.annotation.TargetApi;
Ray Chendcb265c2011-08-30 12:06:17 +080020import android.app.ActionBar;
Owen Linf9a0a432011-08-17 22:07:43 +080021import android.app.Activity;
Pin Ting4ba06f02012-02-11 02:53:53 +080022import android.content.AsyncQueryHandler;
Owen Linbe055bc2012-05-15 16:56:59 -070023import android.content.ContentResolver;
Owen Linf9a0a432011-08-17 22:07:43 +080024import android.content.Intent;
25import android.content.pm.ActivityInfo;
26import android.database.Cursor;
Ray Chen7b9c72a2012-03-07 18:22:17 +080027import android.graphics.Bitmap;
28import android.graphics.drawable.BitmapDrawable;
Owen Linf9a0a432011-08-17 22:07:43 +080029import android.media.AudioManager;
Ray Chene6e4beb2011-10-11 11:37:18 +080030import android.net.Uri;
Owen Lin7a5e1e72012-06-20 16:54:24 +080031import android.os.Build;
Owen Linf9a0a432011-08-17 22:07:43 +080032import android.os.Bundle;
33import android.provider.MediaStore;
Pin Ting4ba06f02012-02-11 02:53:53 +080034import android.provider.OpenableColumns;
Chih-Chung Changfc8c5032011-11-29 14:21:11 +080035import android.view.KeyEvent;
Ray Chene6e4beb2011-10-11 11:37:18 +080036import android.view.Menu;
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080037import android.view.MenuItem;
Owen Linf9a0a432011-08-17 22:07:43 +080038import android.view.View;
39import android.view.Window;
40import android.view.WindowManager;
Ray Chene6e4beb2011-10-11 11:37:18 +080041import android.widget.ShareActionProvider;
42
43import com.android.gallery3d.R;
Owen Lin7a5e1e72012-06-20 16:54:24 +080044import com.android.gallery3d.common.ApiHelper;
Pin Ting4ba06f02012-02-11 02:53:53 +080045import com.android.gallery3d.common.Utils;
Owen Linf9a0a432011-08-17 22:07:43 +080046
47/**
48 * This activity plays a video from a specified URI.
Ray Chen7b9c72a2012-03-07 18:22:17 +080049 *
50 * The client of this activity can pass a logo bitmap in the intent (KEY_LOGO_BITMAP)
51 * to set the action bar logo so the playback process looks more seamlessly integrated with
52 * the original activity.
Owen Linf9a0a432011-08-17 22:07:43 +080053 */
54public class MovieActivity extends Activity {
55 @SuppressWarnings("unused")
56 private static final String TAG = "MovieActivity";
Ray Chena3871052012-05-17 12:15:08 +080057 public static final String KEY_LOGO_BITMAP = "logo-bitmap";
58 public static final String KEY_TREAT_UP_AS_BACK = "treat-up-as-back";
Owen Linf9a0a432011-08-17 22:07:43 +080059
60 private MoviePlayer mPlayer;
61 private boolean mFinishOnCompletion;
Ray Chene6e4beb2011-10-11 11:37:18 +080062 private Uri mUri;
Ray Chena3871052012-05-17 12:15:08 +080063 private boolean mTreatUpAsBack;
Owen Linf9a0a432011-08-17 22:07:43 +080064
Owen Lin7a5e1e72012-06-20 16:54:24 +080065 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
66 private void setSystemUiVisibility(View rootView) {
67 if (ApiHelper.HAS_VIEW_SYSTEM_UI_FLAG_LAYOUT_STABLE) {
68 rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
69 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
70 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
71 }
72 }
73
Owen Linf9a0a432011-08-17 22:07:43 +080074 @Override
75 public void onCreate(Bundle savedInstanceState) {
76 super.onCreate(savedInstanceState);
77
78 requestWindowFeature(Window.FEATURE_ACTION_BAR);
79 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
80
81 setContentView(R.layout.movie_view);
Chih-Chung Changfb559942012-03-12 12:46:25 +080082 View rootView = findViewById(R.id.movie_view_root);
Owen Lin7a5e1e72012-06-20 16:54:24 +080083
84 setSystemUiVisibility(rootView);
85
Owen Linf9a0a432011-08-17 22:07:43 +080086 Intent intent = getIntent();
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080087 initializeActionBar(intent);
Chih-Chung Chang209a9162011-10-14 16:09:09 +080088 mFinishOnCompletion = intent.getBooleanExtra(
89 MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
Ray Chena3871052012-05-17 12:15:08 +080090 mTreatUpAsBack = intent.getBooleanExtra(KEY_TREAT_UP_AS_BACK, false);
Chih-Chung Chang209a9162011-10-14 16:09:09 +080091 mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState,
92 !mFinishOnCompletion) {
Owen Linf9a0a432011-08-17 22:07:43 +080093 @Override
94 public void onCompletion() {
95 if (mFinishOnCompletion) {
96 finish();
97 }
98 }
99 };
100 if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) {
101 int orientation = intent.getIntExtra(
102 MediaStore.EXTRA_SCREEN_ORIENTATION,
103 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
104 if (orientation != getRequestedOrientation()) {
105 setRequestedOrientation(orientation);
106 }
107 }
Owen Linf9a0a432011-08-17 22:07:43 +0800108 Window win = getWindow();
109 WindowManager.LayoutParams winParams = win.getAttributes();
110 winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
Chih-Chung Chang81760122011-09-27 16:50:16 +0800111 winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
Owen Linf9a0a432011-08-17 22:07:43 +0800112 win.setAttributes(winParams);
Owen Lin5dd57a22012-05-22 14:19:31 -0700113
114 // We set the background in the theme to have the launching animation.
115 // But for the performance (and battery), we remove the background here.
116 win.setBackgroundDrawable(null);
Owen Linf9a0a432011-08-17 22:07:43 +0800117 }
118
Owen Lincc8e7ab2012-07-27 11:46:50 +0800119 @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
120 private void setActionBarLogoFromIntent(Intent intent) {
121 if (ApiHelper.HAS_ACTION_BAR_SET_LOGO) {
122 Bitmap logo = intent.getParcelableExtra(KEY_LOGO_BITMAP);
123 if (logo != null) {
124 getActionBar().setLogo(new BitmapDrawable(getResources(), logo));
125 }
126 }
127 }
128
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800129 private void initializeActionBar(Intent intent) {
Pin Ting143594f2012-01-18 18:15:27 +0800130 mUri = intent.getData();
Pin Ting4ba06f02012-02-11 02:53:53 +0800131 final ActionBar actionBar = getActionBar();
Owen Lincc8e7ab2012-07-27 11:46:50 +0800132 setActionBarLogoFromIntent(intent);
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800133 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
134 ActionBar.DISPLAY_HOME_AS_UP);
Pin Ting143594f2012-01-18 18:15:27 +0800135
Pin Ting4ba06f02012-02-11 02:53:53 +0800136 String title = intent.getStringExtra(Intent.EXTRA_TITLE);
137 if (title != null) {
138 actionBar.setTitle(title);
139 } else {
140 // Displays the filename as title, reading the filename from the
141 // interface: {@link android.provider.OpenableColumns#DISPLAY_NAME}.
142 AsyncQueryHandler queryHandler =
143 new AsyncQueryHandler(getContentResolver()) {
144 @Override
145 protected void onQueryComplete(int token, Object cookie,
146 Cursor cursor) {
147 try {
148 if ((cursor != null) && cursor.moveToFirst()) {
149 String displayName = cursor.getString(0);
150
151 // Just show empty title if other apps don't set
152 // DISPLAY_NAME
153 actionBar.setTitle((displayName == null) ? "" :
154 displayName);
155 }
156 } finally {
157 Utils.closeSilently(cursor);
158 }
159 }
160 };
161 queryHandler.startQuery(0, null, mUri,
162 new String[] {OpenableColumns.DISPLAY_NAME}, null, null,
163 null);
164 }
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800165 }
166
167 @Override
Ray Chene6e4beb2011-10-11 11:37:18 +0800168 public boolean onCreateOptionsMenu(Menu menu) {
169 super.onCreateOptionsMenu(menu);
170
171 getMenuInflater().inflate(R.menu.movie, menu);
172 ShareActionProvider provider = GalleryActionBar.initializeShareActionProvider(menu);
173
Owen Linbe055bc2012-05-15 16:56:59 -0700174 // Document says EXTRA_STREAM should be a content: Uri
175 // So, we only share the video if it's "content:".
176 if (provider != null && ContentResolver.SCHEME_CONTENT
177 .equals(mUri.getScheme())) {
Ray Chene6e4beb2011-10-11 11:37:18 +0800178 Intent intent = new Intent(Intent.ACTION_SEND);
179 intent.setType("video/*");
180 intent.putExtra(Intent.EXTRA_STREAM, mUri);
181 provider.setShareIntent(intent);
182 }
183
184 return true;
185 }
186
187 @Override
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800188 public boolean onOptionsItemSelected(MenuItem item) {
189 if (item.getItemId() == android.R.id.home) {
Ray Chena3871052012-05-17 12:15:08 +0800190 if (mTreatUpAsBack) {
191 finish();
192 } else {
193 startActivity(new Intent(this, Gallery.class));
194 finish();
195 }
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800196 return true;
197 }
198 return false;
Owen Linf9a0a432011-08-17 22:07:43 +0800199 }
200
201 @Override
202 public void onStart() {
203 ((AudioManager) getSystemService(AUDIO_SERVICE))
204 .requestAudioFocus(null, AudioManager.STREAM_MUSIC,
205 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
206 super.onStart();
207 }
208
209 @Override
210 protected void onStop() {
211 ((AudioManager) getSystemService(AUDIO_SERVICE))
212 .abandonAudioFocus(null);
213 super.onStop();
214 }
215
216 @Override
217 public void onPause() {
218 mPlayer.onPause();
219 super.onPause();
220 }
221
222 @Override
223 public void onResume() {
224 mPlayer.onResume();
225 super.onResume();
226 }
227
228 @Override
Owen Lin540e4692011-09-14 19:49:03 +0800229 public void onSaveInstanceState(Bundle outState) {
230 super.onSaveInstanceState(outState);
231 mPlayer.onSaveInstanceState(outState);
232 }
233
234 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800235 public void onDestroy() {
236 mPlayer.onDestroy();
237 super.onDestroy();
238 }
Chih-Chung Changfc8c5032011-11-29 14:21:11 +0800239
240 @Override
241 public boolean onKeyDown(int keyCode, KeyEvent event) {
242 return mPlayer.onKeyDown(keyCode, event)
243 || super.onKeyDown(keyCode, event);
244 }
245
246 @Override
247 public boolean onKeyUp(int keyCode, KeyEvent event) {
248 return mPlayer.onKeyUp(keyCode, event)
249 || super.onKeyUp(keyCode, event);
250 }
Owen Linf9a0a432011-08-17 22:07:43 +0800251}