blob: 9ad63dbe65ba75af6ef6b42ef753f77a3897334a [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;
25import android.media.AudioManager;
Ray Chene6e4beb2011-10-11 11:37:18 +080026import android.net.Uri;
Owen Linf9a0a432011-08-17 22:07:43 +080027import android.os.Bundle;
28import android.provider.MediaStore;
Pin Ting4ba06f02012-02-11 02:53:53 +080029import android.provider.OpenableColumns;
Chih-Chung Changfc8c5032011-11-29 14:21:11 +080030import android.view.KeyEvent;
Ray Chene6e4beb2011-10-11 11:37:18 +080031import android.view.Menu;
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080032import android.view.MenuItem;
Owen Linf9a0a432011-08-17 22:07:43 +080033import android.view.View;
34import android.view.Window;
35import android.view.WindowManager;
Ray Chene6e4beb2011-10-11 11:37:18 +080036import android.widget.ShareActionProvider;
37
38import com.android.gallery3d.R;
Pin Ting4ba06f02012-02-11 02:53:53 +080039import com.android.gallery3d.common.Utils;
Owen Linf9a0a432011-08-17 22:07:43 +080040
41/**
42 * This activity plays a video from a specified URI.
43 */
44public class MovieActivity extends Activity {
45 @SuppressWarnings("unused")
46 private static final String TAG = "MovieActivity";
47
48 private MoviePlayer mPlayer;
49 private boolean mFinishOnCompletion;
Ray Chene6e4beb2011-10-11 11:37:18 +080050 private Uri mUri;
Owen Linf9a0a432011-08-17 22:07:43 +080051
52 @Override
53 public void onCreate(Bundle savedInstanceState) {
54 super.onCreate(savedInstanceState);
55
56 requestWindowFeature(Window.FEATURE_ACTION_BAR);
57 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
58
59 setContentView(R.layout.movie_view);
60 View rootView = findViewById(R.id.root);
61 Intent intent = getIntent();
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080062 initializeActionBar(intent);
Chih-Chung Chang209a9162011-10-14 16:09:09 +080063 mFinishOnCompletion = intent.getBooleanExtra(
64 MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
65 mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState,
66 !mFinishOnCompletion) {
Owen Linf9a0a432011-08-17 22:07:43 +080067 @Override
68 public void onCompletion() {
69 if (mFinishOnCompletion) {
70 finish();
71 }
72 }
73 };
74 if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) {
75 int orientation = intent.getIntExtra(
76 MediaStore.EXTRA_SCREEN_ORIENTATION,
77 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
78 if (orientation != getRequestedOrientation()) {
79 setRequestedOrientation(orientation);
80 }
81 }
Owen Linf9a0a432011-08-17 22:07:43 +080082 Window win = getWindow();
83 WindowManager.LayoutParams winParams = win.getAttributes();
84 winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
Chih-Chung Chang81760122011-09-27 16:50:16 +080085 winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
Owen Linf9a0a432011-08-17 22:07:43 +080086 win.setAttributes(winParams);
Owen Linf9a0a432011-08-17 22:07:43 +080087 }
88
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080089 private void initializeActionBar(Intent intent) {
Pin Ting143594f2012-01-18 18:15:27 +080090 mUri = intent.getData();
Pin Ting4ba06f02012-02-11 02:53:53 +080091 final ActionBar actionBar = getActionBar();
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080092 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
93 ActionBar.DISPLAY_HOME_AS_UP);
Pin Ting143594f2012-01-18 18:15:27 +080094
Pin Ting4ba06f02012-02-11 02:53:53 +080095 String title = intent.getStringExtra(Intent.EXTRA_TITLE);
96 if (title != null) {
97 actionBar.setTitle(title);
98 } else {
99 // Displays the filename as title, reading the filename from the
100 // interface: {@link android.provider.OpenableColumns#DISPLAY_NAME}.
101 AsyncQueryHandler queryHandler =
102 new AsyncQueryHandler(getContentResolver()) {
103 @Override
104 protected void onQueryComplete(int token, Object cookie,
105 Cursor cursor) {
106 try {
107 if ((cursor != null) && cursor.moveToFirst()) {
108 String displayName = cursor.getString(0);
109
110 // Just show empty title if other apps don't set
111 // DISPLAY_NAME
112 actionBar.setTitle((displayName == null) ? "" :
113 displayName);
114 }
115 } finally {
116 Utils.closeSilently(cursor);
117 }
118 }
119 };
120 queryHandler.startQuery(0, null, mUri,
121 new String[] {OpenableColumns.DISPLAY_NAME}, null, null,
122 null);
123 }
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800124 }
125
126 @Override
Ray Chene6e4beb2011-10-11 11:37:18 +0800127 public boolean onCreateOptionsMenu(Menu menu) {
128 super.onCreateOptionsMenu(menu);
129
130 getMenuInflater().inflate(R.menu.movie, menu);
131 ShareActionProvider provider = GalleryActionBar.initializeShareActionProvider(menu);
132
133 if (provider != null) {
134 Intent intent = new Intent(Intent.ACTION_SEND);
135 intent.setType("video/*");
136 intent.putExtra(Intent.EXTRA_STREAM, mUri);
137 provider.setShareIntent(intent);
138 }
139
140 return true;
141 }
142
143 @Override
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800144 public boolean onOptionsItemSelected(MenuItem item) {
145 if (item.getItemId() == android.R.id.home) {
146 finish();
147 return true;
148 }
149 return false;
Owen Linf9a0a432011-08-17 22:07:43 +0800150 }
151
152 @Override
153 public void onStart() {
154 ((AudioManager) getSystemService(AUDIO_SERVICE))
155 .requestAudioFocus(null, AudioManager.STREAM_MUSIC,
156 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
157 super.onStart();
158 }
159
160 @Override
161 protected void onStop() {
162 ((AudioManager) getSystemService(AUDIO_SERVICE))
163 .abandonAudioFocus(null);
164 super.onStop();
165 }
166
167 @Override
168 public void onPause() {
169 mPlayer.onPause();
170 super.onPause();
171 }
172
173 @Override
174 public void onResume() {
175 mPlayer.onResume();
176 super.onResume();
177 }
178
179 @Override
Owen Lin540e4692011-09-14 19:49:03 +0800180 public void onSaveInstanceState(Bundle outState) {
181 super.onSaveInstanceState(outState);
182 mPlayer.onSaveInstanceState(outState);
183 }
184
185 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800186 public void onDestroy() {
187 mPlayer.onDestroy();
188 super.onDestroy();
189 }
Chih-Chung Changfc8c5032011-11-29 14:21:11 +0800190
191 @Override
192 public boolean onKeyDown(int keyCode, KeyEvent event) {
193 return mPlayer.onKeyDown(keyCode, event)
194 || super.onKeyDown(keyCode, event);
195 }
196
197 @Override
198 public boolean onKeyUp(int keyCode, KeyEvent event) {
199 return mPlayer.onKeyUp(keyCode, event)
200 || super.onKeyUp(keyCode, event);
201 }
Owen Linf9a0a432011-08-17 22:07:43 +0800202}