blob: 099e9f59e91a40b9d48de49e378bb2294e627a96 [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;
21import android.content.Intent;
22import android.content.pm.ActivityInfo;
23import android.database.Cursor;
24import android.media.AudioManager;
Ray Chene6e4beb2011-10-11 11:37:18 +080025import android.net.Uri;
Owen Linf9a0a432011-08-17 22:07:43 +080026import android.os.Bundle;
27import android.provider.MediaStore;
28import android.provider.MediaStore.Video.VideoColumns;
Chih-Chung Changfc8c5032011-11-29 14:21:11 +080029import android.view.KeyEvent;
Ray Chene6e4beb2011-10-11 11:37:18 +080030import android.view.Menu;
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080031import android.view.MenuItem;
Owen Linf9a0a432011-08-17 22:07:43 +080032import android.view.View;
33import android.view.Window;
34import android.view.WindowManager;
Ray Chene6e4beb2011-10-11 11:37:18 +080035import android.widget.ShareActionProvider;
36
37import com.android.gallery3d.R;
Owen Linf9a0a432011-08-17 22:07:43 +080038
39/**
40 * This activity plays a video from a specified URI.
41 */
42public class MovieActivity extends Activity {
43 @SuppressWarnings("unused")
44 private static final String TAG = "MovieActivity";
45
46 private MoviePlayer mPlayer;
47 private boolean mFinishOnCompletion;
Ray Chene6e4beb2011-10-11 11:37:18 +080048 private Uri mUri;
Owen Linf9a0a432011-08-17 22:07:43 +080049
50 @Override
51 public void onCreate(Bundle savedInstanceState) {
52 super.onCreate(savedInstanceState);
53
54 requestWindowFeature(Window.FEATURE_ACTION_BAR);
55 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
56
57 setContentView(R.layout.movie_view);
58 View rootView = findViewById(R.id.root);
59 Intent intent = getIntent();
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080060 initializeActionBar(intent);
Chih-Chung Chang209a9162011-10-14 16:09:09 +080061 mFinishOnCompletion = intent.getBooleanExtra(
62 MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
63 mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState,
64 !mFinishOnCompletion) {
Owen Linf9a0a432011-08-17 22:07:43 +080065 @Override
66 public void onCompletion() {
67 if (mFinishOnCompletion) {
68 finish();
69 }
70 }
71 };
72 if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) {
73 int orientation = intent.getIntExtra(
74 MediaStore.EXTRA_SCREEN_ORIENTATION,
75 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
76 if (orientation != getRequestedOrientation()) {
77 setRequestedOrientation(orientation);
78 }
79 }
Owen Linf9a0a432011-08-17 22:07:43 +080080 Window win = getWindow();
81 WindowManager.LayoutParams winParams = win.getAttributes();
82 winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
Chih-Chung Chang81760122011-09-27 16:50:16 +080083 winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
Owen Linf9a0a432011-08-17 22:07:43 +080084 win.setAttributes(winParams);
Owen Linf9a0a432011-08-17 22:07:43 +080085 }
86
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080087 private void initializeActionBar(Intent intent) {
88 ActionBar actionBar = getActionBar();
89 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
90 ActionBar.DISPLAY_HOME_AS_UP);
Owen Linf9a0a432011-08-17 22:07:43 +080091 String title = intent.getStringExtra(Intent.EXTRA_TITLE);
Ray Chene6e4beb2011-10-11 11:37:18 +080092 mUri = intent.getData();
Owen Linf9a0a432011-08-17 22:07:43 +080093 if (title == null) {
94 Cursor cursor = null;
95 try {
Ray Chene6e4beb2011-10-11 11:37:18 +080096 cursor = getContentResolver().query(mUri,
Owen Linf9a0a432011-08-17 22:07:43 +080097 new String[] {VideoColumns.TITLE}, null, null, null);
98 if (cursor != null && cursor.moveToNext()) {
99 title = cursor.getString(0);
100 }
101 } catch (Throwable t) {
102 Log.w(TAG, "cannot get title from: " + intent.getDataString(), t);
103 } finally {
104 if (cursor != null) cursor.close();
105 }
106 }
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800107 if (title != null) actionBar.setTitle(title);
108 }
109
110 @Override
Ray Chene6e4beb2011-10-11 11:37:18 +0800111 public boolean onCreateOptionsMenu(Menu menu) {
112 super.onCreateOptionsMenu(menu);
113
114 getMenuInflater().inflate(R.menu.movie, menu);
115 ShareActionProvider provider = GalleryActionBar.initializeShareActionProvider(menu);
116
117 if (provider != null) {
118 Intent intent = new Intent(Intent.ACTION_SEND);
119 intent.setType("video/*");
120 intent.putExtra(Intent.EXTRA_STREAM, mUri);
121 provider.setShareIntent(intent);
122 }
123
124 return true;
125 }
126
127 @Override
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800128 public boolean onOptionsItemSelected(MenuItem item) {
129 if (item.getItemId() == android.R.id.home) {
130 finish();
131 return true;
132 }
133 return false;
Owen Linf9a0a432011-08-17 22:07:43 +0800134 }
135
136 @Override
137 public void onStart() {
138 ((AudioManager) getSystemService(AUDIO_SERVICE))
139 .requestAudioFocus(null, AudioManager.STREAM_MUSIC,
140 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
141 super.onStart();
142 }
143
144 @Override
145 protected void onStop() {
146 ((AudioManager) getSystemService(AUDIO_SERVICE))
147 .abandonAudioFocus(null);
148 super.onStop();
149 }
150
151 @Override
152 public void onPause() {
153 mPlayer.onPause();
154 super.onPause();
155 }
156
157 @Override
158 public void onResume() {
159 mPlayer.onResume();
160 super.onResume();
161 }
162
163 @Override
Owen Lin540e4692011-09-14 19:49:03 +0800164 public void onSaveInstanceState(Bundle outState) {
165 super.onSaveInstanceState(outState);
166 mPlayer.onSaveInstanceState(outState);
167 }
168
169 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800170 public void onDestroy() {
171 mPlayer.onDestroy();
172 super.onDestroy();
173 }
Chih-Chung Changfc8c5032011-11-29 14:21:11 +0800174
175 @Override
176 public boolean onKeyDown(int keyCode, KeyEvent event) {
177 return mPlayer.onKeyDown(keyCode, event)
178 || super.onKeyDown(keyCode, event);
179 }
180
181 @Override
182 public boolean onKeyUp(int keyCode, KeyEvent event) {
183 return mPlayer.onKeyUp(keyCode, event)
184 || super.onKeyUp(keyCode, event);
185 }
Owen Linf9a0a432011-08-17 22:07:43 +0800186}