• 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.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)
setSystemUiVisibility(View rootView)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
onCreate(Bundle savedInstanceState)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 
setActionBarLogoFromIntent(Intent intent)119     private void setActionBarLogoFromIntent(Intent intent) {
120         Bitmap logo = intent.getParcelableExtra(KEY_LOGO_BITMAP);
121         if (logo != null) {
122             getActionBar().setLogo(
123                     new BitmapDrawable(getResources(), logo));
124         }
125     }
126 
initializeActionBar(Intent intent)127     private void initializeActionBar(Intent intent) {
128         mUri = intent.getData();
129         final ActionBar actionBar = getActionBar();
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
onCreateOptionsMenu(Menu menu)167     public boolean onCreateOptionsMenu(Menu menu) {
168         super.onCreateOptionsMenu(menu);
169         getMenuInflater().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 
createShareIntent()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
onOptionsItemSelected(MenuItem item)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
onStart()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
onStop()219     protected void onStop() {
220         ((AudioManager) getSystemService(AUDIO_SERVICE))
221                 .abandonAudioFocus(null);
222         super.onStop();
223     }
224 
225     @Override
onPause()226     public void onPause() {
227         mPlayer.onPause();
228         super.onPause();
229     }
230 
231     @Override
onResume()232     public void onResume() {
233         mPlayer.onResume();
234         super.onResume();
235     }
236 
237     @Override
onSaveInstanceState(Bundle outState)238     public void onSaveInstanceState(Bundle outState) {
239         super.onSaveInstanceState(outState);
240         mPlayer.onSaveInstanceState(outState);
241     }
242 
243     @Override
onDestroy()244     public void onDestroy() {
245         mPlayer.onDestroy();
246         super.onDestroy();
247     }
248 
249     @Override
onKeyDown(int keyCode, KeyEvent event)250     public boolean onKeyDown(int keyCode, KeyEvent event) {
251         return mPlayer.onKeyDown(keyCode, event)
252                 || super.onKeyDown(keyCode, event);
253     }
254 
255     @Override
onKeyUp(int keyCode, KeyEvent event)256     public boolean onKeyUp(int keyCode, KeyEvent event) {
257         return mPlayer.onKeyUp(keyCode, event)
258                 || super.onKeyUp(keyCode, event);
259     }
260 }
261