OSDN Git Service

8bf9b55df7226f0125d873b8cbeb0fe728d7f334
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / app / MovieActivity.java
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.annotation.TargetApi;
20 import android.app.ActionBar;
21 import android.app.Activity;
22 import android.content.AsyncQueryHandler;
23 import android.content.ContentResolver;
24 import android.content.Intent;
25 import android.content.pm.ActivityInfo;
26 import android.database.Cursor;
27 import android.graphics.Bitmap;
28 import android.graphics.drawable.BitmapDrawable;
29 import android.media.AudioManager;
30 import android.net.Uri;
31 import android.os.Build;
32 import android.os.Bundle;
33 import android.provider.MediaStore;
34 import android.provider.OpenableColumns;
35 import android.view.KeyEvent;
36 import android.view.Menu;
37 import android.view.MenuItem;
38 import android.view.View;
39 import android.view.Window;
40 import android.view.WindowManager;
41 import android.widget.ShareActionProvider;
42
43 import com.android.gallery3d.R;
44 import com.android.gallery3d.common.ApiHelper;
45 import 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  */
54 public class MovieActivity extends Activity {
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         winParams.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
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             getActionBar().setLogo(
124                     new BitmapDrawable(getResources(), logo));
125         }
126     }
127
128     private void initializeActionBar(Intent intent) {
129         mUri = intent.getData();
130         final ActionBar actionBar = getActionBar();
131         if (actionBar == null) {
132             return;
133         }
134         setActionBarLogoFromIntent(intent);
135         actionBar.setDisplayOptions(
136                 ActionBar.DISPLAY_HOME_AS_UP,
137                 ActionBar.DISPLAY_HOME_AS_UP);
138
139         String title = intent.getStringExtra(Intent.EXTRA_TITLE);
140         if (title != null) {
141             actionBar.setTitle(title);
142         } else {
143             // Displays the filename as title, reading the filename from the
144             // interface: {@link android.provider.OpenableColumns#DISPLAY_NAME}.
145             AsyncQueryHandler queryHandler =
146                     new AsyncQueryHandler(getContentResolver()) {
147                 @Override
148                 protected void onQueryComplete(int token, Object cookie,
149                         Cursor cursor) {
150                     try {
151                         if ((cursor != null) && cursor.moveToFirst()) {
152                             String displayName = cursor.getString(0);
153
154                             // Just show empty title if other apps don't set
155                             // DISPLAY_NAME
156                             actionBar.setTitle((displayName == null) ? "" :
157                                     displayName);
158                         }
159                     } finally {
160                         Utils.closeSilently(cursor);
161                     }
162                 }
163             };
164             queryHandler.startQuery(0, null, mUri,
165                     new String[] {OpenableColumns.DISPLAY_NAME}, null, null,
166                     null);
167         }
168     }
169
170     @Override
171     public boolean onCreateOptionsMenu(Menu menu) {
172         super.onCreateOptionsMenu(menu);
173         getMenuInflater().inflate(R.menu.movie, menu);
174
175         // Document says EXTRA_STREAM should be a content: Uri
176         // So, we only share the video if it's "content:".
177         MenuItem shareItem = menu.findItem(R.id.action_share);
178         if (ContentResolver.SCHEME_CONTENT.equals(mUri.getScheme())) {
179             shareItem.setVisible(true);
180             ((ShareActionProvider) shareItem.getActionProvider())
181                     .setShareIntent(createShareIntent());
182         } else {
183             shareItem.setVisible(false);
184         }
185         return true;
186     }
187
188     private Intent createShareIntent() {
189         Intent intent = new Intent(Intent.ACTION_SEND);
190         intent.setType("video/*");
191         intent.putExtra(Intent.EXTRA_STREAM, mUri);
192         return intent;
193     }
194
195     @Override
196     public boolean onOptionsItemSelected(MenuItem item) {
197         int id = item.getItemId();
198         if (id == android.R.id.home) {
199             if (mTreatUpAsBack) {
200                 finish();
201             } else {
202                 startActivity(new Intent(this, Gallery.class));
203                 finish();
204             }
205             return true;
206         } else if (id == R.id.action_share) {
207             startActivity(Intent.createChooser(createShareIntent(),
208                     getString(R.string.share)));
209             return true;
210         }
211         return false;
212     }
213
214     @Override
215     public void onStart() {
216         ((AudioManager) getSystemService(AUDIO_SERVICE))
217                 .requestAudioFocus(null, AudioManager.STREAM_MUSIC,
218                 AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
219         super.onStart();
220     }
221
222     @Override
223     protected void onStop() {
224         ((AudioManager) getSystemService(AUDIO_SERVICE))
225                 .abandonAudioFocus(null);
226         super.onStop();
227     }
228
229     @Override
230     public void onPause() {
231         mPlayer.onPause();
232         super.onPause();
233     }
234
235     @Override
236     public void onResume() {
237         mPlayer.onResume();
238         super.onResume();
239     }
240
241     @Override
242     public void onSaveInstanceState(Bundle outState) {
243         super.onSaveInstanceState(outState);
244         mPlayer.onSaveInstanceState(outState);
245     }
246
247     @Override
248     public void onDestroy() {
249         mPlayer.onDestroy();
250         super.onDestroy();
251     }
252
253     @Override
254     public boolean onKeyDown(int keyCode, KeyEvent event) {
255         return mPlayer.onKeyDown(keyCode, event)
256                 || super.onKeyDown(keyCode, event);
257     }
258
259     @Override
260     public boolean onKeyUp(int keyCode, KeyEvent event) {
261         return mPlayer.onKeyUp(keyCode, event)
262                 || super.onKeyUp(keyCode, event);
263     }
264 }