blob: 288eeb05f8da3babe5fc74c184e48d19c58bc4de [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 Linf9a0a432011-08-17 22:07:43 +080022import android.content.Intent;
23import android.content.pm.ActivityInfo;
24import android.database.Cursor;
Ray Chen7b9c72a2012-03-07 18:22:17 +080025import android.graphics.Bitmap;
26import android.graphics.drawable.BitmapDrawable;
Owen Linf9a0a432011-08-17 22:07:43 +080027import android.media.AudioManager;
Ray Chene6e4beb2011-10-11 11:37:18 +080028import android.net.Uri;
Owen Linf9a0a432011-08-17 22:07:43 +080029import android.os.Bundle;
30import android.provider.MediaStore;
Pin Ting4ba06f02012-02-11 02:53:53 +080031import android.provider.OpenableColumns;
Chih-Chung Changfc8c5032011-11-29 14:21:11 +080032import android.view.KeyEvent;
Ray Chene6e4beb2011-10-11 11:37:18 +080033import android.view.Menu;
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080034import android.view.MenuItem;
Owen Linf9a0a432011-08-17 22:07:43 +080035import android.view.View;
36import android.view.Window;
37import android.view.WindowManager;
Ray Chene6e4beb2011-10-11 11:37:18 +080038import android.widget.ShareActionProvider;
39
40import com.android.gallery3d.R;
Pin Ting4ba06f02012-02-11 02:53:53 +080041import com.android.gallery3d.common.Utils;
Owen Linf9a0a432011-08-17 22:07:43 +080042
43/**
44 * This activity plays a video from a specified URI.
Ray Chen7b9c72a2012-03-07 18:22:17 +080045 *
46 * The client of this activity can pass a logo bitmap in the intent (KEY_LOGO_BITMAP)
47 * to set the action bar logo so the playback process looks more seamlessly integrated with
48 * the original activity.
Owen Linf9a0a432011-08-17 22:07:43 +080049 */
50public class MovieActivity extends Activity {
51 @SuppressWarnings("unused")
52 private static final String TAG = "MovieActivity";
Ray Chen7b9c72a2012-03-07 18:22:17 +080053 private static final String KEY_LOGO_BITMAP = "logo-bitmap";
Owen Linf9a0a432011-08-17 22:07:43 +080054
55 private MoviePlayer mPlayer;
56 private boolean mFinishOnCompletion;
Ray Chene6e4beb2011-10-11 11:37:18 +080057 private Uri mUri;
Owen Linf9a0a432011-08-17 22:07:43 +080058
59 @Override
60 public void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62
63 requestWindowFeature(Window.FEATURE_ACTION_BAR);
64 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
65
66 setContentView(R.layout.movie_view);
Chih-Chung Changfb559942012-03-12 12:46:25 +080067 View rootView = findViewById(R.id.movie_view_root);
Owen Linf9a0a432011-08-17 22:07:43 +080068 Intent intent = getIntent();
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080069 initializeActionBar(intent);
Chih-Chung Chang209a9162011-10-14 16:09:09 +080070 mFinishOnCompletion = intent.getBooleanExtra(
71 MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
72 mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState,
73 !mFinishOnCompletion) {
Owen Linf9a0a432011-08-17 22:07:43 +080074 @Override
75 public void onCompletion() {
76 if (mFinishOnCompletion) {
77 finish();
78 }
79 }
80 };
81 if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) {
82 int orientation = intent.getIntExtra(
83 MediaStore.EXTRA_SCREEN_ORIENTATION,
84 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
85 if (orientation != getRequestedOrientation()) {
86 setRequestedOrientation(orientation);
87 }
88 }
Owen Linf9a0a432011-08-17 22:07:43 +080089 Window win = getWindow();
90 WindowManager.LayoutParams winParams = win.getAttributes();
91 winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
Chih-Chung Chang81760122011-09-27 16:50:16 +080092 winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
Owen Linf9a0a432011-08-17 22:07:43 +080093 win.setAttributes(winParams);
Owen Linf9a0a432011-08-17 22:07:43 +080094 }
95
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080096 private void initializeActionBar(Intent intent) {
Pin Ting143594f2012-01-18 18:15:27 +080097 mUri = intent.getData();
Pin Ting4ba06f02012-02-11 02:53:53 +080098 final ActionBar actionBar = getActionBar();
Ray Chen7b9c72a2012-03-07 18:22:17 +080099 Bitmap logo = intent.getParcelableExtra(KEY_LOGO_BITMAP);
100 if (logo != null) {
101 actionBar.setLogo(new BitmapDrawable(getResources(), logo));
102 }
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800103 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
104 ActionBar.DISPLAY_HOME_AS_UP);
Pin Ting143594f2012-01-18 18:15:27 +0800105
Pin Ting4ba06f02012-02-11 02:53:53 +0800106 String title = intent.getStringExtra(Intent.EXTRA_TITLE);
107 if (title != null) {
108 actionBar.setTitle(title);
109 } else {
110 // Displays the filename as title, reading the filename from the
111 // interface: {@link android.provider.OpenableColumns#DISPLAY_NAME}.
112 AsyncQueryHandler queryHandler =
113 new AsyncQueryHandler(getContentResolver()) {
114 @Override
115 protected void onQueryComplete(int token, Object cookie,
116 Cursor cursor) {
117 try {
118 if ((cursor != null) && cursor.moveToFirst()) {
119 String displayName = cursor.getString(0);
120
121 // Just show empty title if other apps don't set
122 // DISPLAY_NAME
123 actionBar.setTitle((displayName == null) ? "" :
124 displayName);
125 }
126 } finally {
127 Utils.closeSilently(cursor);
128 }
129 }
130 };
131 queryHandler.startQuery(0, null, mUri,
132 new String[] {OpenableColumns.DISPLAY_NAME}, null, null,
133 null);
134 }
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800135 }
136
137 @Override
Ray Chene6e4beb2011-10-11 11:37:18 +0800138 public boolean onCreateOptionsMenu(Menu menu) {
139 super.onCreateOptionsMenu(menu);
140
141 getMenuInflater().inflate(R.menu.movie, menu);
142 ShareActionProvider provider = GalleryActionBar.initializeShareActionProvider(menu);
143
144 if (provider != null) {
145 Intent intent = new Intent(Intent.ACTION_SEND);
146 intent.setType("video/*");
147 intent.putExtra(Intent.EXTRA_STREAM, mUri);
148 provider.setShareIntent(intent);
149 }
150
151 return true;
152 }
153
154 @Override
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800155 public boolean onOptionsItemSelected(MenuItem item) {
156 if (item.getItemId() == android.R.id.home) {
157 finish();
158 return true;
159 }
160 return false;
Owen Linf9a0a432011-08-17 22:07:43 +0800161 }
162
163 @Override
164 public void onStart() {
165 ((AudioManager) getSystemService(AUDIO_SERVICE))
166 .requestAudioFocus(null, AudioManager.STREAM_MUSIC,
167 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
168 super.onStart();
169 }
170
171 @Override
172 protected void onStop() {
173 ((AudioManager) getSystemService(AUDIO_SERVICE))
174 .abandonAudioFocus(null);
175 super.onStop();
176 }
177
178 @Override
179 public void onPause() {
180 mPlayer.onPause();
181 super.onPause();
182 }
183
184 @Override
185 public void onResume() {
186 mPlayer.onResume();
187 super.onResume();
188 }
189
190 @Override
Owen Lin540e4692011-09-14 19:49:03 +0800191 public void onSaveInstanceState(Bundle outState) {
192 super.onSaveInstanceState(outState);
193 mPlayer.onSaveInstanceState(outState);
194 }
195
196 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800197 public void onDestroy() {
198 mPlayer.onDestroy();
199 super.onDestroy();
200 }
Chih-Chung Changfc8c5032011-11-29 14:21:11 +0800201
202 @Override
203 public boolean onKeyDown(int keyCode, KeyEvent event) {
204 return mPlayer.onKeyDown(keyCode, event)
205 || super.onKeyDown(keyCode, event);
206 }
207
208 @Override
209 public boolean onKeyUp(int keyCode, KeyEvent event) {
210 return mPlayer.onKeyUp(keyCode, event)
211 || super.onKeyUp(keyCode, event);
212 }
Owen Linf9a0a432011-08-17 22:07:43 +0800213}