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