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