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 | |
Ray Chen | dcb265c | 2011-08-30 12:06:17 +0800 | [diff] [blame] | 19 | import android.app.ActionBar; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 20 | import android.app.Activity; |
| 21 | import android.content.Intent; |
| 22 | import android.content.pm.ActivityInfo; |
| 23 | import android.database.Cursor; |
| 24 | import android.media.AudioManager; |
Ray Chen | e6e4beb | 2011-10-11 11:37:18 +0800 | [diff] [blame] | 25 | import android.net.Uri; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 26 | import android.os.Bundle; |
| 27 | import android.provider.MediaStore; |
| 28 | import android.provider.MediaStore.Video.VideoColumns; |
Chih-Chung Chang | fc8c503 | 2011-11-29 14:21:11 +0800 | [diff] [blame^] | 29 | import android.view.KeyEvent; |
Ray Chen | e6e4beb | 2011-10-11 11:37:18 +0800 | [diff] [blame] | 30 | import android.view.Menu; |
Chih-Chung Chang | bb7d7b9 | 2011-09-21 15:38:35 +0800 | [diff] [blame] | 31 | import android.view.MenuItem; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 32 | import android.view.View; |
| 33 | import android.view.Window; |
| 34 | import android.view.WindowManager; |
Ray Chen | e6e4beb | 2011-10-11 11:37:18 +0800 | [diff] [blame] | 35 | import android.widget.ShareActionProvider; |
| 36 | |
| 37 | import com.android.gallery3d.R; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * This activity plays a video from a specified URI. |
| 41 | */ |
| 42 | public class MovieActivity extends Activity { |
| 43 | @SuppressWarnings("unused") |
| 44 | private static final String TAG = "MovieActivity"; |
| 45 | |
| 46 | private MoviePlayer mPlayer; |
| 47 | private boolean mFinishOnCompletion; |
Ray Chen | e6e4beb | 2011-10-11 11:37:18 +0800 | [diff] [blame] | 48 | private Uri mUri; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 49 | |
| 50 | @Override |
| 51 | public void onCreate(Bundle savedInstanceState) { |
| 52 | super.onCreate(savedInstanceState); |
| 53 | |
| 54 | requestWindowFeature(Window.FEATURE_ACTION_BAR); |
| 55 | requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); |
| 56 | |
| 57 | setContentView(R.layout.movie_view); |
| 58 | View rootView = findViewById(R.id.root); |
| 59 | Intent intent = getIntent(); |
Chih-Chung Chang | bb7d7b9 | 2011-09-21 15:38:35 +0800 | [diff] [blame] | 60 | initializeActionBar(intent); |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 61 | mFinishOnCompletion = intent.getBooleanExtra( |
| 62 | MediaStore.EXTRA_FINISH_ON_COMPLETION, true); |
| 63 | mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState, |
| 64 | !mFinishOnCompletion) { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 65 | @Override |
| 66 | public void onCompletion() { |
| 67 | if (mFinishOnCompletion) { |
| 68 | finish(); |
| 69 | } |
| 70 | } |
| 71 | }; |
| 72 | if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) { |
| 73 | int orientation = intent.getIntExtra( |
| 74 | MediaStore.EXTRA_SCREEN_ORIENTATION, |
| 75 | ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); |
| 76 | if (orientation != getRequestedOrientation()) { |
| 77 | setRequestedOrientation(orientation); |
| 78 | } |
| 79 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 80 | Window win = getWindow(); |
| 81 | WindowManager.LayoutParams winParams = win.getAttributes(); |
| 82 | winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF; |
Chih-Chung Chang | 8176012 | 2011-09-27 16:50:16 +0800 | [diff] [blame] | 83 | winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 84 | win.setAttributes(winParams); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 85 | } |
| 86 | |
Chih-Chung Chang | bb7d7b9 | 2011-09-21 15:38:35 +0800 | [diff] [blame] | 87 | private void initializeActionBar(Intent intent) { |
| 88 | ActionBar actionBar = getActionBar(); |
| 89 | actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, |
| 90 | ActionBar.DISPLAY_HOME_AS_UP); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 91 | String title = intent.getStringExtra(Intent.EXTRA_TITLE); |
Ray Chen | e6e4beb | 2011-10-11 11:37:18 +0800 | [diff] [blame] | 92 | mUri = intent.getData(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 93 | if (title == null) { |
| 94 | Cursor cursor = null; |
| 95 | try { |
Ray Chen | e6e4beb | 2011-10-11 11:37:18 +0800 | [diff] [blame] | 96 | cursor = getContentResolver().query(mUri, |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 97 | new String[] {VideoColumns.TITLE}, null, null, null); |
| 98 | if (cursor != null && cursor.moveToNext()) { |
| 99 | title = cursor.getString(0); |
| 100 | } |
| 101 | } catch (Throwable t) { |
| 102 | Log.w(TAG, "cannot get title from: " + intent.getDataString(), t); |
| 103 | } finally { |
| 104 | if (cursor != null) cursor.close(); |
| 105 | } |
| 106 | } |
Chih-Chung Chang | bb7d7b9 | 2011-09-21 15:38:35 +0800 | [diff] [blame] | 107 | if (title != null) actionBar.setTitle(title); |
| 108 | } |
| 109 | |
| 110 | @Override |
Ray Chen | e6e4beb | 2011-10-11 11:37:18 +0800 | [diff] [blame] | 111 | public boolean onCreateOptionsMenu(Menu menu) { |
| 112 | super.onCreateOptionsMenu(menu); |
| 113 | |
| 114 | getMenuInflater().inflate(R.menu.movie, menu); |
| 115 | ShareActionProvider provider = GalleryActionBar.initializeShareActionProvider(menu); |
| 116 | |
| 117 | if (provider != null) { |
| 118 | Intent intent = new Intent(Intent.ACTION_SEND); |
| 119 | intent.setType("video/*"); |
| 120 | intent.putExtra(Intent.EXTRA_STREAM, mUri); |
| 121 | provider.setShareIntent(intent); |
| 122 | } |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | @Override |
Chih-Chung Chang | bb7d7b9 | 2011-09-21 15:38:35 +0800 | [diff] [blame] | 128 | public boolean onOptionsItemSelected(MenuItem item) { |
| 129 | if (item.getItemId() == android.R.id.home) { |
| 130 | finish(); |
| 131 | return true; |
| 132 | } |
| 133 | return false; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void onStart() { |
| 138 | ((AudioManager) getSystemService(AUDIO_SERVICE)) |
| 139 | .requestAudioFocus(null, AudioManager.STREAM_MUSIC, |
| 140 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); |
| 141 | super.onStart(); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | protected void onStop() { |
| 146 | ((AudioManager) getSystemService(AUDIO_SERVICE)) |
| 147 | .abandonAudioFocus(null); |
| 148 | super.onStop(); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void onPause() { |
| 153 | mPlayer.onPause(); |
| 154 | super.onPause(); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void onResume() { |
| 159 | mPlayer.onResume(); |
| 160 | super.onResume(); |
| 161 | } |
| 162 | |
| 163 | @Override |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 164 | public void onSaveInstanceState(Bundle outState) { |
| 165 | super.onSaveInstanceState(outState); |
| 166 | mPlayer.onSaveInstanceState(outState); |
| 167 | } |
| 168 | |
| 169 | @Override |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 170 | public void onDestroy() { |
| 171 | mPlayer.onDestroy(); |
| 172 | super.onDestroy(); |
| 173 | } |
Chih-Chung Chang | fc8c503 | 2011-11-29 14:21:11 +0800 | [diff] [blame^] | 174 | |
| 175 | @Override |
| 176 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 177 | return mPlayer.onKeyDown(keyCode, event) |
| 178 | || super.onKeyDown(keyCode, event); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 183 | return mPlayer.onKeyUp(keyCode, event) |
| 184 | || super.onKeyUp(keyCode, event); |
| 185 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 186 | } |