Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | |
| 17 | package com.android.gallery3d.app; |
| 18 | |
| 19 | import com.android.gallery3d.R; |
| 20 | |
Ray Chen | dcb265c | 2011-08-30 12:06:17 +0800 | [diff] [blame] | 21 | import android.app.ActionBar; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 22 | import android.app.Activity; |
| 23 | import android.content.Intent; |
| 24 | import android.content.pm.ActivityInfo; |
| 25 | import android.database.Cursor; |
| 26 | import android.media.AudioManager; |
| 27 | import android.os.Bundle; |
| 28 | import android.provider.MediaStore; |
| 29 | import android.provider.MediaStore.Video.VideoColumns; |
Chih-Chung Chang | bb7d7b9 | 2011-09-21 15:38:35 +0800 | [diff] [blame] | 30 | import android.view.MenuItem; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 31 | import android.view.View; |
| 32 | import android.view.Window; |
| 33 | import android.view.WindowManager; |
| 34 | |
| 35 | /** |
| 36 | * This activity plays a video from a specified URI. |
| 37 | */ |
| 38 | public 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 Chang | bb7d7b9 | 2011-09-21 15:38:35 +0800 | [diff] [blame] | 55 | initializeActionBar(intent); |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 56 | mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState) { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 57 | @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; |
Chih-Chung Chang | 8176012 | 2011-09-27 16:50:16 +0800 | [diff] [blame^] | 76 | winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 77 | win.setAttributes(winParams); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 78 | } |
| 79 | |
Chih-Chung Chang | bb7d7b9 | 2011-09-21 15:38:35 +0800 | [diff] [blame] | 80 | private void initializeActionBar(Intent intent) { |
| 81 | ActionBar actionBar = getActionBar(); |
| 82 | actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, |
| 83 | ActionBar.DISPLAY_HOME_AS_UP); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 84 | String title = intent.getStringExtra(Intent.EXTRA_TITLE); |
| 85 | if (title == null) { |
| 86 | Cursor cursor = null; |
| 87 | try { |
| 88 | cursor = getContentResolver().query(intent.getData(), |
| 89 | new String[] {VideoColumns.TITLE}, null, null, null); |
| 90 | if (cursor != null && cursor.moveToNext()) { |
| 91 | title = cursor.getString(0); |
| 92 | } |
| 93 | } catch (Throwable t) { |
| 94 | Log.w(TAG, "cannot get title from: " + intent.getDataString(), t); |
| 95 | } finally { |
| 96 | if (cursor != null) cursor.close(); |
| 97 | } |
| 98 | } |
Chih-Chung Chang | bb7d7b9 | 2011-09-21 15:38:35 +0800 | [diff] [blame] | 99 | if (title != null) actionBar.setTitle(title); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public boolean onOptionsItemSelected(MenuItem item) { |
| 104 | if (item.getItemId() == android.R.id.home) { |
| 105 | finish(); |
| 106 | return true; |
| 107 | } |
| 108 | return false; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void onStart() { |
| 113 | ((AudioManager) getSystemService(AUDIO_SERVICE)) |
| 114 | .requestAudioFocus(null, AudioManager.STREAM_MUSIC, |
| 115 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); |
| 116 | super.onStart(); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | protected void onStop() { |
| 121 | ((AudioManager) getSystemService(AUDIO_SERVICE)) |
| 122 | .abandonAudioFocus(null); |
| 123 | super.onStop(); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void onPause() { |
| 128 | mPlayer.onPause(); |
| 129 | super.onPause(); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void onResume() { |
| 134 | mPlayer.onResume(); |
| 135 | super.onResume(); |
| 136 | } |
| 137 | |
| 138 | @Override |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 139 | public void onSaveInstanceState(Bundle outState) { |
| 140 | super.onSaveInstanceState(outState); |
| 141 | mPlayer.onSaveInstanceState(outState); |
| 142 | } |
| 143 | |
| 144 | @Override |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 145 | public void onDestroy() { |
| 146 | mPlayer.onDestroy(); |
| 147 | super.onDestroy(); |
| 148 | } |
| 149 | } |