blob: 58b1b088c4f091dcb00e989a7827fbb02f669db8 [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;
Ray Chene6e4beb2011-10-11 11:37:18 +080029import android.view.Menu;
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;
Ray Chene6e4beb2011-10-11 11:37:18 +080034import android.widget.ShareActionProvider;
35
36import com.android.gallery3d.R;
Owen Linf9a0a432011-08-17 22:07:43 +080037
38/**
39 * This activity plays a video from a specified URI.
40 */
41public class MovieActivity extends Activity {
42 @SuppressWarnings("unused")
43 private static final String TAG = "MovieActivity";
44
45 private MoviePlayer mPlayer;
46 private boolean mFinishOnCompletion;
Ray Chene6e4beb2011-10-11 11:37:18 +080047 private Uri mUri;
Owen Linf9a0a432011-08-17 22:07:43 +080048
49 @Override
50 public void onCreate(Bundle savedInstanceState) {
51 super.onCreate(savedInstanceState);
52
53 requestWindowFeature(Window.FEATURE_ACTION_BAR);
54 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
55
56 setContentView(R.layout.movie_view);
57 View rootView = findViewById(R.id.root);
58 Intent intent = getIntent();
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080059 initializeActionBar(intent);
Owen Lin540e4692011-09-14 19:49:03 +080060 mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState) {
Owen Linf9a0a432011-08-17 22:07:43 +080061 @Override
62 public void onCompletion() {
63 if (mFinishOnCompletion) {
64 finish();
65 }
66 }
67 };
68 if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) {
69 int orientation = intent.getIntExtra(
70 MediaStore.EXTRA_SCREEN_ORIENTATION,
71 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
72 if (orientation != getRequestedOrientation()) {
73 setRequestedOrientation(orientation);
74 }
75 }
76 mFinishOnCompletion = intent.getBooleanExtra(MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
77 Window win = getWindow();
78 WindowManager.LayoutParams winParams = win.getAttributes();
79 winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
Chih-Chung Chang81760122011-09-27 16:50:16 +080080 winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
Owen Linf9a0a432011-08-17 22:07:43 +080081 win.setAttributes(winParams);
Owen Linf9a0a432011-08-17 22:07:43 +080082 }
83
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080084 private void initializeActionBar(Intent intent) {
85 ActionBar actionBar = getActionBar();
86 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
87 ActionBar.DISPLAY_HOME_AS_UP);
Owen Linf9a0a432011-08-17 22:07:43 +080088 String title = intent.getStringExtra(Intent.EXTRA_TITLE);
Ray Chene6e4beb2011-10-11 11:37:18 +080089 mUri = intent.getData();
Owen Linf9a0a432011-08-17 22:07:43 +080090 if (title == null) {
91 Cursor cursor = null;
92 try {
Ray Chene6e4beb2011-10-11 11:37:18 +080093 cursor = getContentResolver().query(mUri,
Owen Linf9a0a432011-08-17 22:07:43 +080094 new String[] {VideoColumns.TITLE}, null, null, null);
95 if (cursor != null && cursor.moveToNext()) {
96 title = cursor.getString(0);
97 }
98 } catch (Throwable t) {
99 Log.w(TAG, "cannot get title from: " + intent.getDataString(), t);
100 } finally {
101 if (cursor != null) cursor.close();
102 }
103 }
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800104 if (title != null) actionBar.setTitle(title);
105 }
106
107 @Override
Ray Chene6e4beb2011-10-11 11:37:18 +0800108 public boolean onCreateOptionsMenu(Menu menu) {
109 super.onCreateOptionsMenu(menu);
110
111 getMenuInflater().inflate(R.menu.movie, menu);
112 ShareActionProvider provider = GalleryActionBar.initializeShareActionProvider(menu);
113
114 if (provider != null) {
115 Intent intent = new Intent(Intent.ACTION_SEND);
116 intent.setType("video/*");
117 intent.putExtra(Intent.EXTRA_STREAM, mUri);
118 provider.setShareIntent(intent);
119 }
120
121 return true;
122 }
123
124 @Override
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800125 public boolean onOptionsItemSelected(MenuItem item) {
126 if (item.getItemId() == android.R.id.home) {
127 finish();
128 return true;
129 }
130 return false;
Owen Linf9a0a432011-08-17 22:07:43 +0800131 }
132
133 @Override
134 public void onStart() {
135 ((AudioManager) getSystemService(AUDIO_SERVICE))
136 .requestAudioFocus(null, AudioManager.STREAM_MUSIC,
137 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
138 super.onStart();
139 }
140
141 @Override
142 protected void onStop() {
143 ((AudioManager) getSystemService(AUDIO_SERVICE))
144 .abandonAudioFocus(null);
145 super.onStop();
146 }
147
148 @Override
149 public void onPause() {
150 mPlayer.onPause();
151 super.onPause();
152 }
153
154 @Override
155 public void onResume() {
156 mPlayer.onResume();
157 super.onResume();
158 }
159
160 @Override
Owen Lin540e4692011-09-14 19:49:03 +0800161 public void onSaveInstanceState(Bundle outState) {
162 super.onSaveInstanceState(outState);
163 mPlayer.onSaveInstanceState(outState);
164 }
165
166 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800167 public void onDestroy() {
168 mPlayer.onDestroy();
169 super.onDestroy();
170 }
171}