Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2016 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.google.android.leanbackjank.ui;
     18 
     19 import android.app.Activity;
     20 import android.content.Intent;
     21 import android.media.MediaPlayer;
     22 import android.media.MediaPlayer.OnPreparedListener;
     23 import android.net.Uri;
     24 import android.os.Build;
     25 import android.os.Bundle;
     26 import android.view.Window;
     27 import android.view.WindowManager;
     28 import android.widget.VideoView;
     29 
     30 public class VideoActivity extends Activity {
     31 
     32     private VideoView mVideoView;
     33 
     34     @Override
     35     protected void onCreate(Bundle savedInstanceState) {
     36         super.onCreate(savedInstanceState);
     37 
     38         requestWindowFeature(Window.FEATURE_NO_TITLE);
     39         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
     40                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
     41 
     42         mVideoView = new VideoView(this);
     43         mVideoView.setOnPreparedListener(new OnPreparedListener() {
     44             @Override
     45             public void onPrepared(MediaPlayer mp) {
     46                 mp.setLooping(true);
     47             }
     48         });
     49         setContentView(mVideoView);
     50 
     51         if (checkIntent(getIntent())) {
     52             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
     53                 enterPictureInPictureMode();
     54             }
     55         }
     56     }
     57 
     58     private void playVideo(Uri uri) {
     59         mVideoView.setVideoURI(uri);
     60         mVideoView.start();
     61     }
     62 
     63     private boolean checkIntent(Intent intent) {
     64         if (Intent.ACTION_VIEW.equals(intent.getAction())) {
     65             Uri uri = intent.getData();
     66             playVideo(uri);
     67             return true;
     68         } else {
     69             finish();
     70             return false;
     71         }
     72     }
     73 
     74     @Override
     75     protected void onNewIntent(Intent intent) {
     76         super.onNewIntent(intent);
     77         checkIntent(intent);
     78     }
     79 
     80     @Override
     81     protected void onStop() {
     82         if (mVideoView != null) {
     83             mVideoView.stopPlayback();
     84         }
     85         super.onStop();
     86         finish();
     87     }
     88 }
     89