MovieActivity.java revision fb5571987f2555b04731b6f3e1c479efc16d542a
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.annotation.TargetApi;
20import android.app.ActionBar;
21import android.app.Activity;
22import android.content.AsyncQueryHandler;
23import android.content.ContentResolver;
24import android.content.Intent;
25import android.content.pm.ActivityInfo;
26import android.database.Cursor;
27import android.graphics.Bitmap;
28import android.graphics.drawable.BitmapDrawable;
29import android.media.AudioManager;
30import android.net.Uri;
31import android.os.Build;
32import android.os.Bundle;
33import android.provider.MediaStore;
34import android.provider.OpenableColumns;
35import android.view.KeyEvent;
36import android.view.Menu;
37import android.view.MenuItem;
38import android.view.View;
39import android.view.Window;
40import android.view.WindowManager;
41import android.widget.ShareActionProvider;
42
43import com.android.gallery3d.R;
44import com.android.gallery3d.common.ApiHelper;
45import com.android.gallery3d.common.Utils;
46
47/**
48 * This activity plays a video from a specified URI.
49 *
50 * The client of this activity can pass a logo bitmap in the intent (KEY_LOGO_BITMAP)
51 * to set the action bar logo so the playback process looks more seamlessly integrated with
52 * the original activity.
53 */
54public class MovieActivity extends Activity {
55    @SuppressWarnings("unused")
56    private static final String TAG = "MovieActivity";
57    public static final String KEY_LOGO_BITMAP = "logo-bitmap";
58    public static final String KEY_TREAT_UP_AS_BACK = "treat-up-as-back";
59
60    private MoviePlayer mPlayer;
61    private boolean mFinishOnCompletion;
62    private Uri mUri;
63    private boolean mTreatUpAsBack;
64
65    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
66    private void setSystemUiVisibility(View rootView) {
67        if (ApiHelper.HAS_VIEW_SYSTEM_UI_FLAG_LAYOUT_STABLE) {
68            rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
69                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
70                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
71        }
72    }
73
74    @Override
75    public void onCreate(Bundle savedInstanceState) {
76        super.onCreate(savedInstanceState);
77
78        requestWindowFeature(Window.FEATURE_ACTION_BAR);
79        requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
80
81        setContentView(R.layout.movie_view);
82        View rootView = findViewById(R.id.movie_view_root);
83
84        setSystemUiVisibility(rootView);
85
86        Intent intent = getIntent();
87        initializeActionBar(intent);
88        mFinishOnCompletion = intent.getBooleanExtra(
89                MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
90        mTreatUpAsBack = intent.getBooleanExtra(KEY_TREAT_UP_AS_BACK, false);
91        mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState,
92                !mFinishOnCompletion) {
93            @Override
94            public void onCompletion() {
95                if (mFinishOnCompletion) {
96                    finish();
97                }
98            }
99        };
100        if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) {
101            int orientation = intent.getIntExtra(
102                    MediaStore.EXTRA_SCREEN_ORIENTATION,
103                    ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
104            if (orientation != getRequestedOrientation()) {
105                setRequestedOrientation(orientation);
106            }
107        }
108        Window win = getWindow();
109        WindowManager.LayoutParams winParams = win.getAttributes();
110        winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
111        winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
112        win.setAttributes(winParams);
113
114        // We set the background in the theme to have the launching animation.
115        // But for the performance (and battery), we remove the background here.
116        win.setBackgroundDrawable(null);
117    }
118
119    @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
120    private void setActionBarLogoFromIntent(Intent intent) {
121        if (ApiHelper.HAS_ACTION_BAR_SET_LOGO) {
122            Bitmap logo = intent.getParcelableExtra(KEY_LOGO_BITMAP);
123            if (logo != null) {
124                getActionBar().setLogo(new BitmapDrawable(getResources(), logo));
125            }
126        }
127    }
128
129    private void initializeActionBar(Intent intent) {
130        mUri = intent.getData();
131        final ActionBar actionBar = getActionBar();
132        setActionBarLogoFromIntent(intent);
133        actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
134                ActionBar.DISPLAY_HOME_AS_UP);
135
136        String title = intent.getStringExtra(Intent.EXTRA_TITLE);
137        if (title != null) {
138            actionBar.setTitle(title);
139        } else {
140            // Displays the filename as title, reading the filename from the
141            // interface: {@link android.provider.OpenableColumns#DISPLAY_NAME}.
142            AsyncQueryHandler queryHandler =
143                    new AsyncQueryHandler(getContentResolver()) {
144                @Override
145                protected void onQueryComplete(int token, Object cookie,
146                        Cursor cursor) {
147                    try {
148                        if ((cursor != null) && cursor.moveToFirst()) {
149                            String displayName = cursor.getString(0);
150
151                            // Just show empty title if other apps don't set
152                            // DISPLAY_NAME
153                            actionBar.setTitle((displayName == null) ? "" :
154                                    displayName);
155                        }
156                    } finally {
157                        Utils.closeSilently(cursor);
158                    }
159                }
160            };
161            queryHandler.startQuery(0, null, mUri,
162                    new String[] {OpenableColumns.DISPLAY_NAME}, null, null,
163                    null);
164        }
165    }
166
167    @Override
168    public boolean onCreateOptionsMenu(Menu menu) {
169        super.onCreateOptionsMenu(menu);
170        getMenuInflater().inflate(R.menu.movie, menu);
171
172        // Document says EXTRA_STREAM should be a content: Uri
173        // So, we only share the video if it's "content:".
174        if (ContentResolver.SCHEME_CONTENT.equals(mUri.getScheme())) {
175            initializeShareActionProvider(menu);
176        } else {
177            menu.findItem(R.id.action_share).setVisible(false);
178        }
179        return true;
180    }
181
182    @TargetApi(ApiHelper.VERSION_CODES.JELLY_BEAN)
183    private void initializeShareActionProvider(Menu menu) {
184        if (!ApiHelper.HAS_SHARE_ACTION_PROVIDER) return;
185
186        ShareActionProvider provider = GalleryActionBar.initializeShareActionProvider(
187                menu, this);
188        provider.setShareIntent(createShareIntent());
189    }
190
191    private Intent createShareIntent() {
192        Intent intent = new Intent(Intent.ACTION_SEND);
193        intent.setType("video/*");
194        intent.putExtra(Intent.EXTRA_STREAM, mUri);
195        return intent;
196    }
197
198    @Override
199    public boolean onOptionsItemSelected(MenuItem item) {
200        int id = item.getItemId();
201        if (id == android.R.id.home) {
202            if (mTreatUpAsBack) {
203                finish();
204            } else {
205                startActivity(new Intent(this, Gallery.class));
206                finish();
207            }
208            return true;
209        } else if (id == R.id.action_share) {
210            startActivity(Intent.createChooser(createShareIntent(),
211                    getString(R.string.share)));
212            return true;
213        }
214        return false;
215    }
216
217    @Override
218    public void onStart() {
219        ((AudioManager) getSystemService(AUDIO_SERVICE))
220                .requestAudioFocus(null, AudioManager.STREAM_MUSIC,
221                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
222        super.onStart();
223    }
224
225    @Override
226    protected void onStop() {
227        ((AudioManager) getSystemService(AUDIO_SERVICE))
228                .abandonAudioFocus(null);
229        super.onStop();
230    }
231
232    @Override
233    public void onPause() {
234        mPlayer.onPause();
235        super.onPause();
236    }
237
238    @Override
239    public void onResume() {
240        mPlayer.onResume();
241        super.onResume();
242    }
243
244    @Override
245    public void onSaveInstanceState(Bundle outState) {
246        super.onSaveInstanceState(outState);
247        mPlayer.onSaveInstanceState(outState);
248    }
249
250    @Override
251    public void onDestroy() {
252        mPlayer.onDestroy();
253        super.onDestroy();
254    }
255
256    @Override
257    public boolean onKeyDown(int keyCode, KeyEvent event) {
258        return mPlayer.onKeyDown(keyCode, event)
259                || super.onKeyDown(keyCode, event);
260    }
261
262    @Override
263    public boolean onKeyUp(int keyCode, KeyEvent event) {
264        return mPlayer.onKeyUp(keyCode, event)
265                || super.onKeyUp(keyCode, event);
266    }
267}
268