blob: 1950549e31cf6688980e13e9848ff461afcb7b13 [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
19import com.android.gallery3d.R;
20
Ray Chendcb265c2011-08-30 12:06:17 +080021import android.app.ActionBar;
Owen Linf9a0a432011-08-17 22:07:43 +080022import android.app.Activity;
23import android.content.Intent;
24import android.content.pm.ActivityInfo;
25import android.database.Cursor;
26import android.media.AudioManager;
27import android.os.Bundle;
28import android.provider.MediaStore;
29import android.provider.MediaStore.Video.VideoColumns;
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080030import android.view.MenuItem;
Owen Linf9a0a432011-08-17 22:07:43 +080031import android.view.View;
32import android.view.Window;
33import android.view.WindowManager;
34
35/**
36 * This activity plays a video from a specified URI.
37 */
38public class MovieActivity extends Activity {
39 @SuppressWarnings("unused")
40 private static final String TAG = "MovieActivity";
41
42 private MoviePlayer mPlayer;
43 private boolean mFinishOnCompletion;
44
45 @Override
46 public void onCreate(Bundle savedInstanceState) {
47 super.onCreate(savedInstanceState);
48
49 requestWindowFeature(Window.FEATURE_ACTION_BAR);
50 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
51
52 setContentView(R.layout.movie_view);
53 View rootView = findViewById(R.id.root);
54 Intent intent = getIntent();
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080055 initializeActionBar(intent);
Chih-Chung Chang209a9162011-10-14 16:09:09 +080056 mFinishOnCompletion = intent.getBooleanExtra(
57 MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
58 mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState,
59 !mFinishOnCompletion) {
Owen Linf9a0a432011-08-17 22:07:43 +080060 @Override
61 public void onCompletion() {
62 if (mFinishOnCompletion) {
63 finish();
64 }
65 }
66 };
67 if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) {
68 int orientation = intent.getIntExtra(
69 MediaStore.EXTRA_SCREEN_ORIENTATION,
70 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
71 if (orientation != getRequestedOrientation()) {
72 setRequestedOrientation(orientation);
73 }
74 }
Owen Linf9a0a432011-08-17 22:07:43 +080075 Window win = getWindow();
76 WindowManager.LayoutParams winParams = win.getAttributes();
77 winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
Chih-Chung Chang81760122011-09-27 16:50:16 +080078 winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
Owen Linf9a0a432011-08-17 22:07:43 +080079 win.setAttributes(winParams);
Owen Linf9a0a432011-08-17 22:07:43 +080080 }
81
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080082 private void initializeActionBar(Intent intent) {
83 ActionBar actionBar = getActionBar();
84 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
85 ActionBar.DISPLAY_HOME_AS_UP);
Owen Linf9a0a432011-08-17 22:07:43 +080086 String title = intent.getStringExtra(Intent.EXTRA_TITLE);
87 if (title == null) {
88 Cursor cursor = null;
89 try {
90 cursor = getContentResolver().query(intent.getData(),
91 new String[] {VideoColumns.TITLE}, null, null, null);
92 if (cursor != null && cursor.moveToNext()) {
93 title = cursor.getString(0);
94 }
95 } catch (Throwable t) {
96 Log.w(TAG, "cannot get title from: " + intent.getDataString(), t);
97 } finally {
98 if (cursor != null) cursor.close();
99 }
100 }
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800101 if (title != null) actionBar.setTitle(title);
102 }
103
104 @Override
105 public boolean onOptionsItemSelected(MenuItem item) {
106 if (item.getItemId() == android.R.id.home) {
107 finish();
108 return true;
109 }
110 return false;
Owen Linf9a0a432011-08-17 22:07:43 +0800111 }
112
113 @Override
114 public void onStart() {
115 ((AudioManager) getSystemService(AUDIO_SERVICE))
116 .requestAudioFocus(null, AudioManager.STREAM_MUSIC,
117 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
118 super.onStart();
119 }
120
121 @Override
122 protected void onStop() {
123 ((AudioManager) getSystemService(AUDIO_SERVICE))
124 .abandonAudioFocus(null);
125 super.onStop();
126 }
127
128 @Override
129 public void onPause() {
130 mPlayer.onPause();
131 super.onPause();
132 }
133
134 @Override
135 public void onResume() {
136 mPlayer.onResume();
137 super.onResume();
138 }
139
140 @Override
Owen Lin540e4692011-09-14 19:49:03 +0800141 public void onSaveInstanceState(Bundle outState) {
142 super.onSaveInstanceState(outState);
143 mPlayer.onSaveInstanceState(outState);
144 }
145
146 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800147 public void onDestroy() {
148 mPlayer.onDestroy();
149 super.onDestroy();
150 }
151}