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; |
| 21 | import android.content.Intent; |
| 22 | import android.content.pm.ActivityInfo; |
| 23 | import android.database.Cursor; |
| 24 | import android.media.AudioManager; |
Ray Chen | e6e4beb | 2011-10-11 11:37:18 +0800 | [diff] [blame^] | 25 | import android.net.Uri; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 26 | import android.os.Bundle; |
| 27 | import android.provider.MediaStore; |
| 28 | import android.provider.MediaStore.Video.VideoColumns; |
Ray Chen | e6e4beb | 2011-10-11 11:37:18 +0800 | [diff] [blame^] | 29 | import android.view.Menu; |
Chih-Chung Chang | bb7d7b9 | 2011-09-21 15:38:35 +0800 | [diff] [blame] | 30 | import android.view.MenuItem; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 31 | import android.view.View; |
| 32 | import android.view.Window; |
| 33 | import android.view.WindowManager; |
Ray Chen | e6e4beb | 2011-10-11 11:37:18 +0800 | [diff] [blame^] | 34 | import android.widget.ShareActionProvider; |
| 35 | |
| 36 | import com.android.gallery3d.R; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * This activity plays a video from a specified URI. |
| 40 | */ |
| 41 | public class MovieActivity extends Activity { |
| 42 | @SuppressWarnings("unused") |
| 43 | private static final String TAG = "MovieActivity"; |
| 44 | |
| 45 | private MoviePlayer mPlayer; |
| 46 | private boolean mFinishOnCompletion; |
Ray Chen | e6e4beb | 2011-10-11 11:37:18 +0800 | [diff] [blame^] | 47 | private Uri mUri; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 48 | |
| 49 | @Override |
| 50 | public void onCreate(Bundle savedInstanceState) { |
| 51 | super.onCreate(savedInstanceState); |
| 52 | |
| 53 | requestWindowFeature(Window.FEATURE_ACTION_BAR); |
| 54 | requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); |
| 55 | |
| 56 | setContentView(R.layout.movie_view); |
| 57 | View rootView = findViewById(R.id.root); |
| 58 | Intent intent = getIntent(); |
Chih-Chung Chang | bb7d7b9 | 2011-09-21 15:38:35 +0800 | [diff] [blame] | 59 | initializeActionBar(intent); |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 60 | mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState) { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 61 | @Override |
| 62 | public void onCompletion() { |
| 63 | if (mFinishOnCompletion) { |
| 64 | finish(); |
| 65 | } |
| 66 | } |
| 67 | }; |
| 68 | if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) { |
| 69 | int orientation = intent.getIntExtra( |
| 70 | MediaStore.EXTRA_SCREEN_ORIENTATION, |
| 71 | ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); |
| 72 | if (orientation != getRequestedOrientation()) { |
| 73 | setRequestedOrientation(orientation); |
| 74 | } |
| 75 | } |
| 76 | mFinishOnCompletion = intent.getBooleanExtra(MediaStore.EXTRA_FINISH_ON_COMPLETION, true); |
| 77 | Window win = getWindow(); |
| 78 | WindowManager.LayoutParams winParams = win.getAttributes(); |
| 79 | winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF; |
Chih-Chung Chang | 8176012 | 2011-09-27 16:50:16 +0800 | [diff] [blame] | 80 | winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 81 | win.setAttributes(winParams); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 82 | } |
| 83 | |
Chih-Chung Chang | bb7d7b9 | 2011-09-21 15:38:35 +0800 | [diff] [blame] | 84 | private void initializeActionBar(Intent intent) { |
| 85 | ActionBar actionBar = getActionBar(); |
| 86 | actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, |
| 87 | ActionBar.DISPLAY_HOME_AS_UP); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 88 | String title = intent.getStringExtra(Intent.EXTRA_TITLE); |
Ray Chen | e6e4beb | 2011-10-11 11:37:18 +0800 | [diff] [blame^] | 89 | mUri = intent.getData(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 90 | if (title == null) { |
| 91 | Cursor cursor = null; |
| 92 | try { |
Ray Chen | e6e4beb | 2011-10-11 11:37:18 +0800 | [diff] [blame^] | 93 | cursor = getContentResolver().query(mUri, |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 94 | new String[] {VideoColumns.TITLE}, null, null, null); |
| 95 | if (cursor != null && cursor.moveToNext()) { |
| 96 | title = cursor.getString(0); |
| 97 | } |
| 98 | } catch (Throwable t) { |
| 99 | Log.w(TAG, "cannot get title from: " + intent.getDataString(), t); |
| 100 | } finally { |
| 101 | if (cursor != null) cursor.close(); |
| 102 | } |
| 103 | } |
Chih-Chung Chang | bb7d7b9 | 2011-09-21 15:38:35 +0800 | [diff] [blame] | 104 | if (title != null) actionBar.setTitle(title); |
| 105 | } |
| 106 | |
| 107 | @Override |
Ray Chen | e6e4beb | 2011-10-11 11:37:18 +0800 | [diff] [blame^] | 108 | public boolean onCreateOptionsMenu(Menu menu) { |
| 109 | super.onCreateOptionsMenu(menu); |
| 110 | |
| 111 | getMenuInflater().inflate(R.menu.movie, menu); |
| 112 | ShareActionProvider provider = GalleryActionBar.initializeShareActionProvider(menu); |
| 113 | |
| 114 | if (provider != null) { |
| 115 | Intent intent = new Intent(Intent.ACTION_SEND); |
| 116 | intent.setType("video/*"); |
| 117 | intent.putExtra(Intent.EXTRA_STREAM, mUri); |
| 118 | provider.setShareIntent(intent); |
| 119 | } |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | @Override |
Chih-Chung Chang | bb7d7b9 | 2011-09-21 15:38:35 +0800 | [diff] [blame] | 125 | public boolean onOptionsItemSelected(MenuItem item) { |
| 126 | if (item.getItemId() == android.R.id.home) { |
| 127 | finish(); |
| 128 | return true; |
| 129 | } |
| 130 | return false; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void onStart() { |
| 135 | ((AudioManager) getSystemService(AUDIO_SERVICE)) |
| 136 | .requestAudioFocus(null, AudioManager.STREAM_MUSIC, |
| 137 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); |
| 138 | super.onStart(); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | protected void onStop() { |
| 143 | ((AudioManager) getSystemService(AUDIO_SERVICE)) |
| 144 | .abandonAudioFocus(null); |
| 145 | super.onStop(); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void onPause() { |
| 150 | mPlayer.onPause(); |
| 151 | super.onPause(); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void onResume() { |
| 156 | mPlayer.onResume(); |
| 157 | super.onResume(); |
| 158 | } |
| 159 | |
| 160 | @Override |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 161 | public void onSaveInstanceState(Bundle outState) { |
| 162 | super.onSaveInstanceState(outState); |
| 163 | mPlayer.onSaveInstanceState(outState); |
| 164 | } |
| 165 | |
| 166 | @Override |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 167 | public void onDestroy() { |
| 168 | mPlayer.onDestroy(); |
| 169 | super.onDestroy(); |
| 170 | } |
| 171 | } |