OC-2459: Ask for pincode, when click on an instant upload notification
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewVideoActivity.java
index 0b12920..59d9655 100644 (file)
@@ -2,9 +2,8 @@
  *   Copyright (C) 2012-2013 ownCloud Inc.
  *
  *   This program is free software: you can redistribute it and/or modify
  *   Copyright (C) 2012-2013 ownCloud Inc.
  *
  *   This program is free software: you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation, either version 2 of the License, or
- *   (at your option) any later version.
+ *   it under the terms of the GNU General Public License version 2,
+ *   as published by the Free Software Foundation.
  *
  *   This program is distributed in the hope that it will be useful,
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  *
  *   This program is distributed in the hope that it will be useful,
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 package com.owncloud.android.ui.preview;
 
 
 package com.owncloud.android.ui.preview;
 
+import com.owncloud.android.R;
+import com.owncloud.android.datamodel.FileDataStorageManager;
+import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.media.MediaService;
+import com.owncloud.android.ui.activity.FileActivity;
+import com.owncloud.android.utils.Log_OC;
+
 import android.accounts.Account;
 import android.accounts.Account;
-import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.content.Intent;
@@ -29,15 +34,11 @@ import android.media.MediaPlayer.OnErrorListener;
 import android.media.MediaPlayer.OnPreparedListener;
 import android.net.Uri;
 import android.os.Bundle;
 import android.media.MediaPlayer.OnPreparedListener;
 import android.net.Uri;
 import android.os.Bundle;
-import android.util.Log;
-import android.view.MotionEvent;
 import android.widget.MediaController;
 import android.widget.VideoView;
 
 import android.widget.MediaController;
 import android.widget.VideoView;
 
-import com.owncloud.android.AccountUtils;
-import com.owncloud.android.R;
-import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.media.MediaService;
+import com.owncloud.android.oc_framework.accounts.AccountUtils;
+import com.owncloud.android.oc_framework.accounts.AccountUtils.AccountNotFoundException;
 
 /**
  *  Activity implementing a basic video player.
 
 /**
  *  Activity implementing a basic video player.
@@ -49,14 +50,8 @@ import com.owncloud.android.media.MediaService;
  *  
  *  @author David A. Velasco
  */
  *  
  *  @author David A. Velasco
  */
-public class PreviewVideoActivity extends Activity implements OnCompletionListener, OnPreparedListener, OnErrorListener {
+public class PreviewVideoActivity extends FileActivity implements OnCompletionListener, OnPreparedListener, OnErrorListener {
 
 
-    /** Key to receive an {@link OCFile} to play as an extra value in an {@link Intent} */
-    public static final String EXTRA_FILE = "FILE";
-    
-    /** Key to receive the ownCloud {@link Account} where the file to play is saved as an extra value in an {@link Intent} */
-    public static final String EXTRA_ACCOUNT = "ACCOUNT";
-    
     /** Key to receive a flag signaling if the video should be started immediately */
     public static final String EXTRA_AUTOPLAY = "AUTOPLAY";
     
     /** Key to receive a flag signaling if the video should be started immediately */
     public static final String EXTRA_AUTOPLAY = "AUTOPLAY";
     
@@ -65,8 +60,8 @@ public class PreviewVideoActivity extends Activity implements OnCompletionListen
     
     private static final String TAG = PreviewVideoActivity.class.getSimpleName();
 
     
     private static final String TAG = PreviewVideoActivity.class.getSimpleName();
 
-    private OCFile mFile;                       // video file to play
-    private Account mAccount;                   // ownCloud account holding mFile
+    private FileDataStorageManager mStorageManager;
+    
     private int mSavedPlaybackPosition;         // in the unit time handled by MediaPlayer.getCurrentPosition()
     private boolean mAutoplay;                  // when 'true', the playback starts immediately with the activity
     private VideoView mVideoPlayer;             // view to play the file; both performs and show the playback
     private int mSavedPlaybackPosition;         // in the unit time handled by MediaPlayer.getCurrentPosition()
     private boolean mAutoplay;                  // when 'true', the playback starts immediately with the activity
     private VideoView mVideoPlayer;             // view to play the file; both performs and show the playback
@@ -85,14 +80,19 @@ public class PreviewVideoActivity extends Activity implements OnCompletionListen
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        Log_OC.e(TAG, "ACTIVITY\t\tonCreate");
         
         setContentView(R.layout.video_layout);
     
         
         setContentView(R.layout.video_layout);
     
-        Bundle extras = getIntent().getExtras();
-        mFile = extras.getParcelable(EXTRA_FILE);
-        mAccount = extras.getParcelable(EXTRA_ACCOUNT);
-        mSavedPlaybackPosition = extras.getInt(EXTRA_START_POSITION);
-        mAutoplay = extras.getBoolean(EXTRA_AUTOPLAY);
+        if (savedInstanceState == null) {
+            Bundle extras = getIntent().getExtras();
+            mSavedPlaybackPosition = extras.getInt(EXTRA_START_POSITION);
+            mAutoplay = extras.getBoolean(EXTRA_AUTOPLAY);
+            
+        } else {
+            mSavedPlaybackPosition = savedInstanceState.getInt(EXTRA_START_POSITION);
+            mAutoplay = savedInstanceState.getBoolean(EXTRA_AUTOPLAY);
+        }
           
         mVideoPlayer = (VideoView) findViewById(R.id.videoPlayer);
 
           
         mVideoPlayer = (VideoView) findViewById(R.id.videoPlayer);
 
@@ -103,34 +103,24 @@ public class PreviewVideoActivity extends Activity implements OnCompletionListen
           
         // keep the screen on while the playback is performed (prevents screen off by battery save)
         mVideoPlayer.setKeepScreenOn(true);
           
         // keep the screen on while the playback is performed (prevents screen off by battery save)
         mVideoPlayer.setKeepScreenOn(true);
-        
-        if (mFile != null) {
-            if (mFile.isDown()) {
-                mVideoPlayer.setVideoPath(mFile.getStoragePath());
-                
-            } else if (mAccount != null) {
-                // not working now
-                String url = AccountUtils.constructFullURLForAccount(this, mAccount) + mFile.getRemotePath();
-                mVideoPlayer.setVideoURI(Uri.parse(url));
-                
-            } else {
-                onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account);
-            }
-            
-            // create and prepare control panel for the user
-            mMediaController = new MediaController(this);
-            mMediaController.setMediaPlayer(mVideoPlayer);
-            mMediaController.setAnchorView(mVideoPlayer);
-            mVideoPlayer.setMediaController(mMediaController);
-            
-        } else {
-            onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_nothing_to_play);
-        }
     }    
     
     
     }    
     
     
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void onSaveInstanceState(Bundle outState) {
+        super.onSaveInstanceState(outState);
+        Log_OC.e(TAG, "ACTIVITY\t\tonSaveInstanceState");
+        outState.putInt(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
+        outState.putBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY , mVideoPlayer.isPlaying());
+    }
+
+    
     @Override
     public void onBackPressed() {
     @Override
     public void onBackPressed() {
+        Log_OC.e(TAG, "ACTIVTIY\t\tonBackPressed");
         Intent i = new Intent();
         i.putExtra(EXTRA_AUTOPLAY, mVideoPlayer.isPlaying());
         i.putExtra(EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
         Intent i = new Intent();
         i.putExtra(EXTRA_AUTOPLAY, mVideoPlayer.isPlaying());
         i.putExtra(EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
@@ -147,7 +137,8 @@ public class PreviewVideoActivity extends Activity implements OnCompletionListen
      * @param   mp    {@link MediaPlayer} instance performing the playback.
      */
     @Override
      * @param   mp    {@link MediaPlayer} instance performing the playback.
      */
     @Override
-    public void onPrepared(MediaPlayer vp) {
+    public void onPrepared(MediaPlayer mp) {
+        Log_OC.e(TAG, "ACTIVITY\t\tonPrepare");
         mVideoPlayer.seekTo(mSavedPlaybackPosition);
         if (mAutoplay) { 
             mVideoPlayer.start();
         mVideoPlayer.seekTo(mSavedPlaybackPosition);
         if (mAutoplay) { 
             mVideoPlayer.start();
@@ -178,7 +169,7 @@ public class PreviewVideoActivity extends Activity implements OnCompletionListen
      */
     @Override
     public boolean onError(MediaPlayer mp, int what, int extra) {
      */
     @Override
     public boolean onError(MediaPlayer mp, int what, int extra) {
-        Log.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra);
+        Log_OC.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra);
         
         if (mMediaController != null) {
             mMediaController.hide();
         
         if (mMediaController != null) {
             mMediaController.hide();
@@ -201,20 +192,48 @@ public class PreviewVideoActivity extends Activity implements OnCompletionListen
     }
     
     
     }
     
     
-    /**  
-     * Screen touches trigger the appearance of the control panel for a limited time.
-     *
-     * {@inheritDoc}
-     */
     @Override
     @Override
-    public boolean onTouchEvent (MotionEvent ev){ 
-        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
-            mMediaController.show(MediaService.MEDIA_CONTROL_SHORT_LIFE);
-            return true;        
+    protected void onAccountSet(boolean stateWasRecovered) {
+        if (getAccount() != null) {
+            OCFile file = getFile();
+            /// Validate handled file  (first image to preview)
+            if (file == null) {
+                throw new IllegalStateException("Instanced with a NULL OCFile");
+            }
+            if (!file.isVideo()) {
+                throw new IllegalArgumentException("Non-video file passed as argument");
+            }
+            mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
+            file = mStorageManager.getFileById(file.getFileId()); 
+            if (file != null) {
+                if (file.isDown()) {
+                    mVideoPlayer.setVideoPath(file.getStoragePath());
+                    
+                } else {
+                    // not working yet
+                    String url;
+                    try {
+                        url = AccountUtils.constructFullURLForAccount(this, getAccount()) + file.getRemotePath();
+                        mVideoPlayer.setVideoURI(Uri.parse(url));
+                    } catch (AccountNotFoundException e) {
+                        onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account);
+                    }
+                }
+                
+                // create and prepare control panel for the user
+                mMediaController = new MediaController(this);
+                mMediaController.setMediaPlayer(mVideoPlayer);
+                mMediaController.setAnchorView(mVideoPlayer);
+                mVideoPlayer.setMediaController(mMediaController);
+                
+            } else {
+                finish();
+            }
         } else {
         } else {
-            return false;
+            Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!");
+            finish();
         }
         }
-    }
+   }
 
 
 }
\ No newline at end of file
 
 
 }
\ No newline at end of file