2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.app;
19 import android.annotation.TargetApi;
20 import android.app.ActionBar;
21 import android.app.Activity;
22 import android.content.AsyncQueryHandler;
23 import android.content.ContentResolver;
24 import android.content.Intent;
25 import android.content.pm.ActivityInfo;
26 import android.database.Cursor;
27 import android.graphics.Bitmap;
28 import android.graphics.drawable.BitmapDrawable;
29 import android.media.AudioManager;
30 import android.net.Uri;
31 import android.os.Build;
32 import android.os.Bundle;
33 import android.provider.MediaStore;
34 import android.provider.OpenableColumns;
35 import android.view.KeyEvent;
36 import android.view.Menu;
37 import android.view.MenuItem;
38 import android.view.View;
39 import android.view.Window;
40 import android.view.WindowManager;
41 import android.widget.ShareActionProvider;
43 import com.android.gallery3d.R;
44 import com.android.gallery3d.common.ApiHelper;
45 import com.android.gallery3d.common.Utils;
48 * This activity plays a video from a specified URI.
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.
54 public class MovieActivity extends Activity {
55 @SuppressWarnings("unused")
56 private static final String TAG = "MovieActivity";
57 public static final String KEY_LOGO_BITMAP = "logo-bitmap";
58 public static final String KEY_TREAT_UP_AS_BACK = "treat-up-as-back";
60 private MoviePlayer mPlayer;
61 private boolean mFinishOnCompletion;
63 private boolean mTreatUpAsBack;
65 @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);
75 public void onCreate(Bundle savedInstanceState) {
76 super.onCreate(savedInstanceState);
78 requestWindowFeature(Window.FEATURE_ACTION_BAR);
79 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
81 setContentView(R.layout.movie_view);
82 View rootView = findViewById(R.id.movie_view_root);
84 setSystemUiVisibility(rootView);
86 Intent intent = getIntent();
87 initializeActionBar(intent);
88 mFinishOnCompletion = intent.getBooleanExtra(
89 MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
90 mTreatUpAsBack = intent.getBooleanExtra(KEY_TREAT_UP_AS_BACK, false);
91 mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState,
92 !mFinishOnCompletion) {
94 public void onCompletion() {
95 if (mFinishOnCompletion) {
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);
108 Window win = getWindow();
109 WindowManager.LayoutParams winParams = win.getAttributes();
110 winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
111 winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
112 winParams.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
113 win.setAttributes(winParams);
115 // We set the background in the theme to have the launching animation.
116 // But for the performance (and battery), we remove the background here.
117 win.setBackgroundDrawable(null);
120 private void setActionBarLogoFromIntent(Intent intent) {
121 Bitmap logo = intent.getParcelableExtra(KEY_LOGO_BITMAP);
123 getActionBar().setLogo(
124 new BitmapDrawable(getResources(), logo));
128 private void initializeActionBar(Intent intent) {
129 mUri = intent.getData();
130 final ActionBar actionBar = getActionBar();
131 if (actionBar == null) {
134 setActionBarLogoFromIntent(intent);
135 actionBar.setDisplayOptions(
136 ActionBar.DISPLAY_HOME_AS_UP,
137 ActionBar.DISPLAY_HOME_AS_UP);
139 String title = intent.getStringExtra(Intent.EXTRA_TITLE);
141 actionBar.setTitle(title);
143 // Displays the filename as title, reading the filename from the
144 // interface: {@link android.provider.OpenableColumns#DISPLAY_NAME}.
145 AsyncQueryHandler queryHandler =
146 new AsyncQueryHandler(getContentResolver()) {
148 protected void onQueryComplete(int token, Object cookie,
151 if ((cursor != null) && cursor.moveToFirst()) {
152 String displayName = cursor.getString(0);
154 // Just show empty title if other apps don't set
156 actionBar.setTitle((displayName == null) ? "" :
160 Utils.closeSilently(cursor);
164 queryHandler.startQuery(0, null, mUri,
165 new String[] {OpenableColumns.DISPLAY_NAME}, null, null,
171 public boolean onCreateOptionsMenu(Menu menu) {
172 super.onCreateOptionsMenu(menu);
173 getMenuInflater().inflate(R.menu.movie, menu);
175 // Document says EXTRA_STREAM should be a content: Uri
176 // So, we only share the video if it's "content:".
177 MenuItem shareItem = menu.findItem(R.id.action_share);
178 if (ContentResolver.SCHEME_CONTENT.equals(mUri.getScheme())) {
179 shareItem.setVisible(true);
180 ((ShareActionProvider) shareItem.getActionProvider())
181 .setShareIntent(createShareIntent());
183 shareItem.setVisible(false);
188 private Intent createShareIntent() {
189 Intent intent = new Intent(Intent.ACTION_SEND);
190 intent.setType("video/*");
191 intent.putExtra(Intent.EXTRA_STREAM, mUri);
196 public boolean onOptionsItemSelected(MenuItem item) {
197 int id = item.getItemId();
198 if (id == android.R.id.home) {
199 if (mTreatUpAsBack) {
202 startActivity(new Intent(this, Gallery.class));
206 } else if (id == R.id.action_share) {
207 startActivity(Intent.createChooser(createShareIntent(),
208 getString(R.string.share)));
215 public void onStart() {
216 ((AudioManager) getSystemService(AUDIO_SERVICE))
217 .requestAudioFocus(null, AudioManager.STREAM_MUSIC,
218 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
223 protected void onStop() {
224 ((AudioManager) getSystemService(AUDIO_SERVICE))
225 .abandonAudioFocus(null);
230 public void onPause() {
236 public void onResume() {
242 public void onSaveInstanceState(Bundle outState) {
243 super.onSaveInstanceState(outState);
244 mPlayer.onSaveInstanceState(outState);
248 public void onDestroy() {
254 public boolean onKeyDown(int keyCode, KeyEvent event) {
255 return mPlayer.onKeyDown(keyCode, event)
256 || super.onKeyDown(keyCode, event);
260 public boolean onKeyUp(int keyCode, KeyEvent event) {
261 return mPlayer.onKeyUp(keyCode, event)
262 || super.onKeyUp(keyCode, event);