MovieActivity.java revision f9a0a4306d589b4a4e20554fed512a603426bfa1
1/*
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
21import android.app.Activity;
22import android.content.Intent;
23import android.content.pm.ActivityInfo;
24import android.database.Cursor;
25import android.media.AudioManager;
26import android.os.Bundle;
27import android.provider.MediaStore;
28import android.provider.MediaStore.Video.VideoColumns;
29import android.view.View;
30import android.view.Window;
31import android.view.WindowManager;
32
33/**
34 * This activity plays a video from a specified URI.
35 */
36public class MovieActivity extends Activity {
37    @SuppressWarnings("unused")
38    private static final String TAG = "MovieActivity";
39
40    private MoviePlayer mPlayer;
41    private boolean mFinishOnCompletion;
42
43    @Override
44    public void onCreate(Bundle savedInstanceState) {
45        super.onCreate(savedInstanceState);
46
47        requestWindowFeature(Window.FEATURE_ACTION_BAR);
48        requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
49
50        setContentView(R.layout.movie_view);
51        View rootView = findViewById(R.id.root);
52        Intent intent = getIntent();
53        setVideoTitle(intent);
54        mPlayer = new MoviePlayer(rootView, this, intent.getData()) {
55            @Override
56            public void onCompletion() {
57                if (mFinishOnCompletion) {
58                    finish();
59                }
60            }
61        };
62        if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) {
63            int orientation = intent.getIntExtra(
64                    MediaStore.EXTRA_SCREEN_ORIENTATION,
65                    ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
66            if (orientation != getRequestedOrientation()) {
67                setRequestedOrientation(orientation);
68            }
69        }
70        mFinishOnCompletion = intent.getBooleanExtra(MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
71        Window win = getWindow();
72        WindowManager.LayoutParams winParams = win.getAttributes();
73        winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
74        win.setAttributes(winParams);
75
76    }
77
78    private void setVideoTitle(Intent intent) {
79        String title = intent.getStringExtra(Intent.EXTRA_TITLE);
80        if (title == null) {
81            Cursor cursor = null;
82            try {
83                cursor = getContentResolver().query(intent.getData(),
84                        new String[] {VideoColumns.TITLE}, null, null, null);
85                if (cursor != null && cursor.moveToNext()) {
86                    title = cursor.getString(0);
87                }
88            } catch (Throwable t) {
89                Log.w(TAG, "cannot get title from: " + intent.getDataString(), t);
90            } finally {
91                if (cursor != null) cursor.close();
92            }
93        }
94        if (title != null) getActionBar().setTitle(title);
95    }
96
97    @Override
98    public void onStart() {
99        ((AudioManager) getSystemService(AUDIO_SERVICE))
100                .requestAudioFocus(null, AudioManager.STREAM_MUSIC,
101                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
102        super.onStart();
103    }
104
105    @Override
106    protected void onStop() {
107        ((AudioManager) getSystemService(AUDIO_SERVICE))
108                .abandonAudioFocus(null);
109        super.onStop();
110    }
111
112    @Override
113    public void onPause() {
114        mPlayer.onPause();
115        super.onPause();
116    }
117
118    @Override
119    public void onResume() {
120        mPlayer.onResume();
121        super.onResume();
122    }
123
124    @Override
125    public void onDestroy() {
126        mPlayer.onDestroy();
127        super.onDestroy();
128    }
129}
130