blob: 6bc6fdc4472fd096c4ae0367903831aa5e078204 [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);
Owen Lin540e4692011-09-14 19:49:03 +080056 mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState) {
Owen Linf9a0a432011-08-17 22:07:43 +080057 @Override
58 public void onCompletion() {
59 if (mFinishOnCompletion) {
60 finish();
61 }
62 }
63 };
64 if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) {
65 int orientation = intent.getIntExtra(
66 MediaStore.EXTRA_SCREEN_ORIENTATION,
67 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
68 if (orientation != getRequestedOrientation()) {
69 setRequestedOrientation(orientation);
70 }
71 }
72 mFinishOnCompletion = intent.getBooleanExtra(MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
73 Window win = getWindow();
74 WindowManager.LayoutParams winParams = win.getAttributes();
75 winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
76 win.setAttributes(winParams);
Owen Linf9a0a432011-08-17 22:07:43 +080077 }
78
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080079 private void initializeActionBar(Intent intent) {
80 ActionBar actionBar = getActionBar();
81 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
82 ActionBar.DISPLAY_HOME_AS_UP);
Owen Linf9a0a432011-08-17 22:07:43 +080083 String title = intent.getStringExtra(Intent.EXTRA_TITLE);
84 if (title == null) {
85 Cursor cursor = null;
86 try {
87 cursor = getContentResolver().query(intent.getData(),
88 new String[] {VideoColumns.TITLE}, null, null, null);
89 if (cursor != null && cursor.moveToNext()) {
90 title = cursor.getString(0);
91 }
92 } catch (Throwable t) {
93 Log.w(TAG, "cannot get title from: " + intent.getDataString(), t);
94 } finally {
95 if (cursor != null) cursor.close();
96 }
97 }
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080098 if (title != null) actionBar.setTitle(title);
99 }
100
101 @Override
102 public boolean onOptionsItemSelected(MenuItem item) {
103 if (item.getItemId() == android.R.id.home) {
104 finish();
105 return true;
106 }
107 return false;
Owen Linf9a0a432011-08-17 22:07:43 +0800108 }
109
110 @Override
111 public void onStart() {
112 ((AudioManager) getSystemService(AUDIO_SERVICE))
113 .requestAudioFocus(null, AudioManager.STREAM_MUSIC,
114 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
115 super.onStart();
116 }
117
118 @Override
119 protected void onStop() {
120 ((AudioManager) getSystemService(AUDIO_SERVICE))
121 .abandonAudioFocus(null);
122 super.onStop();
123 }
124
125 @Override
126 public void onPause() {
127 mPlayer.onPause();
128 super.onPause();
129 }
130
131 @Override
132 public void onResume() {
133 mPlayer.onResume();
134 super.onResume();
135 }
136
137 @Override
Owen Lin540e4692011-09-14 19:49:03 +0800138 public void onSaveInstanceState(Bundle outState) {
139 super.onSaveInstanceState(outState);
140 mPlayer.onSaveInstanceState(outState);
141 }
142
143 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800144 public void onDestroy() {
145 mPlayer.onDestroy();
146 super.onDestroy();
147 }
148}