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