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