MovieActivity.java revision e6e4beb069778fafd053552287e36304df5626eb
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 android.app.ActionBar;
20import android.app.Activity;
21import android.content.Intent;
22import android.content.pm.ActivityInfo;
23import android.database.Cursor;
24import android.media.AudioManager;
25import android.net.Uri;
26import android.os.Bundle;
27import android.provider.MediaStore;
28import android.provider.MediaStore.Video.VideoColumns;
29import android.view.Menu;
30import android.view.MenuItem;
31import android.view.View;
32import android.view.Window;
33import android.view.WindowManager;
34import android.widget.ShareActionProvider;
35
36import com.android.gallery3d.R;
37
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;
47    private Uri mUri;
48
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();
59        initializeActionBar(intent);
60        mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState) {
61            @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;
80        winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
81        win.setAttributes(winParams);
82    }
83
84    private void initializeActionBar(Intent intent) {
85        ActionBar actionBar = getActionBar();
86        actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
87                ActionBar.DISPLAY_HOME_AS_UP);
88        String title = intent.getStringExtra(Intent.EXTRA_TITLE);
89        mUri = intent.getData();
90        if (title == null) {
91            Cursor cursor = null;
92            try {
93                cursor = getContentResolver().query(mUri,
94                        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        }
104        if (title != null) actionBar.setTitle(title);
105    }
106
107    @Override
108    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
125    public boolean onOptionsItemSelected(MenuItem item) {
126        if (item.getItemId() == android.R.id.home) {
127            finish();
128            return true;
129        }
130        return false;
131    }
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
161    public void onSaveInstanceState(Bundle outState) {
162        super.onSaveInstanceState(outState);
163        mPlayer.onSaveInstanceState(outState);
164    }
165
166    @Override
167    public void onDestroy() {
168        mPlayer.onDestroy();
169        super.onDestroy();
170    }
171}
172