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