MovieActivity.java revision b21b8e58a604f6c701245d84b141b5b87663192b
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.actionbarsherlock.widget.ShareActionProvider; 43import com.android.gallery3d.R; 44import com.android.gallery3d.common.ApiHelper; 45import com.android.gallery3d.common.Utils; 46 47/** 48 * This activity plays a video from a specified URI. 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. 53 */ 54public class MovieActivity extends SherlockActivity { 55 @SuppressWarnings("unused") 56 private static final String TAG = "MovieActivity"; 57 public static final String KEY_LOGO_BITMAP = "logo-bitmap"; 58 public static final String KEY_TREAT_UP_AS_BACK = "treat-up-as-back"; 59 60 private MoviePlayer mPlayer; 61 private boolean mFinishOnCompletion; 62 private Uri mUri; 63 private boolean mTreatUpAsBack; 64 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 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); 82 View rootView = findViewById(R.id.movie_view_root); 83 84 setSystemUiVisibility(rootView); 85 86 Intent intent = getIntent(); 87 initializeActionBar(intent); 88 mFinishOnCompletion = intent.getBooleanExtra( 89 MediaStore.EXTRA_FINISH_ON_COMPLETION, true); 90 mTreatUpAsBack = intent.getBooleanExtra(KEY_TREAT_UP_AS_BACK, false); 91 mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState, 92 !mFinishOnCompletion) { 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 } 108 Window win = getWindow(); 109 WindowManager.LayoutParams winParams = win.getAttributes(); 110 winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF; 111 winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 112 win.setAttributes(winParams); 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); 117 } 118 119 private void setActionBarLogoFromIntent(Intent intent) { 120 Bitmap logo = intent.getParcelableExtra(KEY_LOGO_BITMAP); 121 if (logo != null) { 122 getSupportActionBar().setLogo( 123 new BitmapDrawable(getResources(), logo)); 124 } 125 } 126 127 private void initializeActionBar(Intent intent) { 128 mUri = intent.getData(); 129 final ActionBar actionBar = getSupportActionBar(); 130 setActionBarLogoFromIntent(intent); 131 actionBar.setDisplayOptions( 132 ActionBar.DISPLAY_HOME_AS_UP, 133 ActionBar.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 getSupportMenuInflater().inflate(R.menu.movie, menu); 170 171 // Document says EXTRA_STREAM should be a content: Uri 172 // So, we only share the video if it's "content:". 173 MenuItem shareItem = menu.findItem(R.id.action_share); 174 if (ContentResolver.SCHEME_CONTENT.equals(mUri.getScheme())) { 175 shareItem.setVisible(true); 176 ((ShareActionProvider) shareItem.getActionProvider()) 177 .setShareIntent(createShareIntent()); 178 } else { 179 shareItem.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