blob: 3879232841b83a408a010cacc6daf99ca2d877b9 [file] [log] [blame]
Owen Linf9a0a432011-08-17 22:07:43 +08001/*
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
Owen Lin7a5e1e72012-06-20 16:54:24 +080019import android.annotation.TargetApi;
John Reck2abaaf72012-12-12 13:43:43 -080020import android.app.ActionBar;
21import android.app.Activity;
Pin Ting4ba06f02012-02-11 02:53:53 +080022import android.content.AsyncQueryHandler;
Owen Linbe055bc2012-05-15 16:56:59 -070023import android.content.ContentResolver;
xulei71e9a7b2015-09-16 10:28:17 +080024import android.content.BroadcastReceiver;
Owen Linf9a0a432011-08-17 22:07:43 +080025import android.content.Intent;
xulei71e9a7b2015-09-16 10:28:17 +080026import android.content.IntentFilter;
Owen Linf9a0a432011-08-17 22:07:43 +080027import android.content.pm.ActivityInfo;
xulei71e9a7b2015-09-16 10:28:17 +080028import android.content.Context;
Owen Linf9a0a432011-08-17 22:07:43 +080029import android.database.Cursor;
Ray Chen7b9c72a2012-03-07 18:22:17 +080030import android.graphics.Bitmap;
31import android.graphics.drawable.BitmapDrawable;
Owen Linf9a0a432011-08-17 22:07:43 +080032import android.media.AudioManager;
Ray Chene6e4beb2011-10-11 11:37:18 +080033import android.net.Uri;
Owen Lin7a5e1e72012-06-20 16:54:24 +080034import android.os.Build;
Owen Linf9a0a432011-08-17 22:07:43 +080035import android.os.Bundle;
36import android.provider.MediaStore;
Pin Ting4ba06f02012-02-11 02:53:53 +080037import android.provider.OpenableColumns;
xulei71e9a7b2015-09-16 10:28:17 +080038import android.telephony.PhoneStateListener;
39import android.telephony.TelephonyManager;
Chih-Chung Changfc8c5032011-11-29 14:21:11 +080040import android.view.KeyEvent;
John Reck2abaaf72012-12-12 13:43:43 -080041import android.view.Menu;
42import android.view.MenuItem;
Owen Linf9a0a432011-08-17 22:07:43 +080043import android.view.View;
44import android.view.Window;
45import android.view.WindowManager;
John Reck2abaaf72012-12-12 13:43:43 -080046import android.widget.ShareActionProvider;
Ray Chene6e4beb2011-10-11 11:37:18 +080047import com.android.gallery3d.R;
Owen Lin7a5e1e72012-06-20 16:54:24 +080048import com.android.gallery3d.common.ApiHelper;
Pin Ting4ba06f02012-02-11 02:53:53 +080049import com.android.gallery3d.common.Utils;
Owen Linf9a0a432011-08-17 22:07:43 +080050
51/**
52 * This activity plays a video from a specified URI.
Ray Chen7b9c72a2012-03-07 18:22:17 +080053 *
54 * The client of this activity can pass a logo bitmap in the intent (KEY_LOGO_BITMAP)
55 * to set the action bar logo so the playback process looks more seamlessly integrated with
56 * the original activity.
Owen Linf9a0a432011-08-17 22:07:43 +080057 */
John Reck2abaaf72012-12-12 13:43:43 -080058public class MovieActivity extends Activity {
Owen Linf9a0a432011-08-17 22:07:43 +080059 @SuppressWarnings("unused")
60 private static final String TAG = "MovieActivity";
Ray Chena3871052012-05-17 12:15:08 +080061 public static final String KEY_LOGO_BITMAP = "logo-bitmap";
62 public static final String KEY_TREAT_UP_AS_BACK = "treat-up-as-back";
Owen Linf9a0a432011-08-17 22:07:43 +080063
64 private MoviePlayer mPlayer;
65 private boolean mFinishOnCompletion;
Ray Chene6e4beb2011-10-11 11:37:18 +080066 private Uri mUri;
Ray Chena3871052012-05-17 12:15:08 +080067 private boolean mTreatUpAsBack;
xulei71e9a7b2015-09-16 10:28:17 +080068 private AudioManager mAudioManager;
69 private TelephonyManager mTelephonyManager;
Owen Linf9a0a432011-08-17 22:07:43 +080070
Owen Lin7a5e1e72012-06-20 16:54:24 +080071 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
72 private void setSystemUiVisibility(View rootView) {
73 if (ApiHelper.HAS_VIEW_SYSTEM_UI_FLAG_LAYOUT_STABLE) {
74 rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
75 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
76 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
77 }
78 }
79
Owen Linf9a0a432011-08-17 22:07:43 +080080 @Override
81 public void onCreate(Bundle savedInstanceState) {
82 super.onCreate(savedInstanceState);
83
John Reck2abaaf72012-12-12 13:43:43 -080084 requestWindowFeature(Window.FEATURE_ACTION_BAR);
85 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
xulei71e9a7b2015-09-16 10:28:17 +080086 mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
87 mTelephonyManager.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
88 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Owen Linf9a0a432011-08-17 22:07:43 +080089
90 setContentView(R.layout.movie_view);
Chih-Chung Changfb559942012-03-12 12:46:25 +080091 View rootView = findViewById(R.id.movie_view_root);
Owen Lin7a5e1e72012-06-20 16:54:24 +080092
93 setSystemUiVisibility(rootView);
94
Owen Linf9a0a432011-08-17 22:07:43 +080095 Intent intent = getIntent();
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +080096 initializeActionBar(intent);
Chih-Chung Chang209a9162011-10-14 16:09:09 +080097 mFinishOnCompletion = intent.getBooleanExtra(
98 MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
Ray Chena3871052012-05-17 12:15:08 +080099 mTreatUpAsBack = intent.getBooleanExtra(KEY_TREAT_UP_AS_BACK, false);
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800100 mPlayer = new MoviePlayer(rootView, this, intent.getData(), savedInstanceState,
101 !mFinishOnCompletion) {
Owen Linf9a0a432011-08-17 22:07:43 +0800102 @Override
103 public void onCompletion() {
104 if (mFinishOnCompletion) {
105 finish();
106 }
107 }
108 };
109 if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) {
110 int orientation = intent.getIntExtra(
111 MediaStore.EXTRA_SCREEN_ORIENTATION,
112 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
113 if (orientation != getRequestedOrientation()) {
114 setRequestedOrientation(orientation);
115 }
116 }
Owen Linf9a0a432011-08-17 22:07:43 +0800117 Window win = getWindow();
118 WindowManager.LayoutParams winParams = win.getAttributes();
119 winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
Chih-Chung Chang81760122011-09-27 16:50:16 +0800120 winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
Owen Linf9a0a432011-08-17 22:07:43 +0800121 win.setAttributes(winParams);
Owen Lin5dd57a22012-05-22 14:19:31 -0700122
123 // We set the background in the theme to have the launching animation.
124 // But for the performance (and battery), we remove the background here.
125 win.setBackgroundDrawable(null);
Owen Linf9a0a432011-08-17 22:07:43 +0800126 }
127
Owen Lincc8e7ab2012-07-27 11:46:50 +0800128 private void setActionBarLogoFromIntent(Intent intent) {
Owen Lin8a861a02012-08-24 15:09:42 +0800129 Bitmap logo = intent.getParcelableExtra(KEY_LOGO_BITMAP);
130 if (logo != null) {
John Reck2abaaf72012-12-12 13:43:43 -0800131 getActionBar().setLogo(
Owen Lin8a861a02012-08-24 15:09:42 +0800132 new BitmapDrawable(getResources(), logo));
Owen Lincc8e7ab2012-07-27 11:46:50 +0800133 }
134 }
135
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800136 private void initializeActionBar(Intent intent) {
Pin Ting143594f2012-01-18 18:15:27 +0800137 mUri = intent.getData();
John Reck2abaaf72012-12-12 13:43:43 -0800138 final ActionBar actionBar = getActionBar();
John Reck4ea5c272013-01-18 16:00:45 -0800139 if (actionBar == null) {
140 return;
141 }
Owen Lincc8e7ab2012-07-27 11:46:50 +0800142 setActionBarLogoFromIntent(intent);
Owen Linb21b8e52012-08-24 12:25:57 +0800143 actionBar.setDisplayOptions(
144 ActionBar.DISPLAY_HOME_AS_UP,
145 ActionBar.DISPLAY_HOME_AS_UP);
Pin Ting143594f2012-01-18 18:15:27 +0800146
Pin Ting4ba06f02012-02-11 02:53:53 +0800147 String title = intent.getStringExtra(Intent.EXTRA_TITLE);
148 if (title != null) {
149 actionBar.setTitle(title);
150 } else {
151 // Displays the filename as title, reading the filename from the
152 // interface: {@link android.provider.OpenableColumns#DISPLAY_NAME}.
153 AsyncQueryHandler queryHandler =
154 new AsyncQueryHandler(getContentResolver()) {
155 @Override
156 protected void onQueryComplete(int token, Object cookie,
157 Cursor cursor) {
158 try {
159 if ((cursor != null) && cursor.moveToFirst()) {
160 String displayName = cursor.getString(0);
161
162 // Just show empty title if other apps don't set
163 // DISPLAY_NAME
164 actionBar.setTitle((displayName == null) ? "" :
165 displayName);
166 }
167 } finally {
168 Utils.closeSilently(cursor);
169 }
170 }
171 };
172 queryHandler.startQuery(0, null, mUri,
173 new String[] {OpenableColumns.DISPLAY_NAME}, null, null,
174 null);
175 }
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800176 }
177
178 @Override
Ray Chene6e4beb2011-10-11 11:37:18 +0800179 public boolean onCreateOptionsMenu(Menu menu) {
180 super.onCreateOptionsMenu(menu);
John Reck2abaaf72012-12-12 13:43:43 -0800181 getMenuInflater().inflate(R.menu.movie, menu);
Owen Lin8a861a02012-08-24 15:09:42 +0800182
Owen Linbe055bc2012-05-15 16:56:59 -0700183 // Document says EXTRA_STREAM should be a content: Uri
184 // So, we only share the video if it's "content:".
Owen Linb21b8e52012-08-24 12:25:57 +0800185 MenuItem shareItem = menu.findItem(R.id.action_share);
Wu-cheng Lifb557192012-07-19 17:11:06 +0800186 if (ContentResolver.SCHEME_CONTENT.equals(mUri.getScheme())) {
Owen Linb21b8e52012-08-24 12:25:57 +0800187 shareItem.setVisible(true);
188 ((ShareActionProvider) shareItem.getActionProvider())
189 .setShareIntent(createShareIntent());
Wu-cheng Lifb557192012-07-19 17:11:06 +0800190 } else {
Owen Linb21b8e52012-08-24 12:25:57 +0800191 shareItem.setVisible(false);
Ray Chene6e4beb2011-10-11 11:37:18 +0800192 }
Owen Lin8a861a02012-08-24 15:09:42 +0800193 return true;
Wu-cheng Lifb557192012-07-19 17:11:06 +0800194 }
195
196 private Intent createShareIntent() {
197 Intent intent = new Intent(Intent.ACTION_SEND);
198 intent.setType("video/*");
199 intent.putExtra(Intent.EXTRA_STREAM, mUri);
200 return intent;
201 }
202
Ray Chene6e4beb2011-10-11 11:37:18 +0800203 @Override
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800204 public boolean onOptionsItemSelected(MenuItem item) {
Wu-cheng Lifb557192012-07-19 17:11:06 +0800205 int id = item.getItemId();
206 if (id == android.R.id.home) {
Ray Chena3871052012-05-17 12:15:08 +0800207 if (mTreatUpAsBack) {
208 finish();
209 } else {
Mangesh Ghiware06c02762013-09-11 15:59:59 -0700210 startActivity(new Intent(this, GalleryActivity.class));
Ray Chena3871052012-05-17 12:15:08 +0800211 finish();
212 }
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800213 return true;
Wu-cheng Lifb557192012-07-19 17:11:06 +0800214 } else if (id == R.id.action_share) {
215 startActivity(Intent.createChooser(createShareIntent(),
216 getString(R.string.share)));
217 return true;
Chih-Chung Changbb7d7b92011-09-21 15:38:35 +0800218 }
219 return false;
Owen Linf9a0a432011-08-17 22:07:43 +0800220 }
221
222 @Override
223 public void onStart() {
224 ((AudioManager) getSystemService(AUDIO_SERVICE))
225 .requestAudioFocus(null, AudioManager.STREAM_MUSIC,
226 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
227 super.onStart();
228 }
229
230 @Override
231 protected void onStop() {
232 ((AudioManager) getSystemService(AUDIO_SERVICE))
233 .abandonAudioFocus(null);
234 super.onStop();
235 }
236
237 @Override
238 public void onPause() {
xulei71e9a7b2015-09-16 10:28:17 +0800239 unregisterReceiver(mReceiver);
Owen Linf9a0a432011-08-17 22:07:43 +0800240 mPlayer.onPause();
241 super.onPause();
242 }
243
244 @Override
245 public void onResume() {
xulei71e9a7b2015-09-16 10:28:17 +0800246 IntentFilter filter = new IntentFilter();
247 filter.addAction("com.android.deskclock.ALARM_ALERT");
248 filter.addAction("com.android.deskclock.ALARM_DONE");
249 registerReceiver(mReceiver, filter);
Owen Linf9a0a432011-08-17 22:07:43 +0800250 mPlayer.onResume();
251 super.onResume();
252 }
253
254 @Override
Owen Lin540e4692011-09-14 19:49:03 +0800255 public void onSaveInstanceState(Bundle outState) {
256 super.onSaveInstanceState(outState);
257 mPlayer.onSaveInstanceState(outState);
258 }
259
260 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800261 public void onDestroy() {
262 mPlayer.onDestroy();
263 super.onDestroy();
264 }
Chih-Chung Changfc8c5032011-11-29 14:21:11 +0800265
266 @Override
267 public boolean onKeyDown(int keyCode, KeyEvent event) {
268 return mPlayer.onKeyDown(keyCode, event)
269 || super.onKeyDown(keyCode, event);
270 }
271
272 @Override
273 public boolean onKeyUp(int keyCode, KeyEvent event) {
274 return mPlayer.onKeyUp(keyCode, event)
275 || super.onKeyUp(keyCode, event);
276 }
xulei71e9a7b2015-09-16 10:28:17 +0800277
278 PhoneStateListener mPhoneListener = new PhoneStateListener() {
279 private boolean mControlPaused = false;
280
281 public void onCallStateChanged(int state, String incomingNumber) {
282 super.onCallStateChanged(state, incomingNumber);
283 switch (state) {
284 case TelephonyManager.CALL_STATE_RINGING:
285 if (mPlayer != null && mPlayer.isPlaying()) {
286 mControlPaused = true;
287 mPlayer.onMoviePlayPause();
288 }
289 break;
290 case TelephonyManager.CALL_STATE_IDLE:
291 if (mPlayer != null && mControlPaused) {
292 mPlayer.onReplay();
293 mControlPaused = false;
294 }
295 break;
296 default:
297 break;
298 }
299 };
300 };
301
302 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
303 private boolean mControlPaused = false;
304
305 @Override
306 public void onReceive(Context context, Intent intent) {
307 String action = intent.getAction();
308 if (intent.getAction().equals("com.android.deskclock.ALARM_ALERT")) {
309 Log.d(TAG, "ALARM_ALERT");
310 if (mPlayer != null && mPlayer.isPlaying()) {
311 mPlayer.onMoviePlayPause();
312 mControlPaused = true;
313 }
314 } else if (intent.getAction().equals("com.android.deskclock.ALARM_DONE")) {
315 Log.d(TAG, "ALARM_DONE");
316 if (mPlayer != null && mControlPaused) {
317 mPlayer.onReplay();
318 mControlPaused = false;
319 }
320 }
321 }
322 };
323
Owen Linf9a0a432011-08-17 22:07:43 +0800324}