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