android视频播放画中画内置应用gallery实现

frameworks/base/core/java/android/provider/Settings.java

+        //add by rex KT_VIDEO_SUSPEND --->start
+        public static final String VIDEOSUSPEND_CLICKED ="video_suspend_click";
+        public static final String VIDEO_POSITION ="video_suspend_position";
+        //add by rex KT_VIDEO_SUSPEND --->end

frameworks/base/core/java/android/widget/VideoView.java

+    //add by rex KT_VIDEO_SUSPEND start
+    public  int   mSizeWidth;
+    public  int   mSizeHeight;
+    public int GetSizeWidth() {
+      return mSizeWidth;
+    }
+    public int GetSizeHeight() {
+      return mSizeHeight;  
+    }
+    //add by rex KT_VIDEO_SUSPEND end
+    
     public VideoView(Context context) {
         super(context);
         initVideoView();
@@ -280,6 +291,11 @@ public class VideoView extends SurfaceView
         } else {
             // no size yet, just adopt the given spec sizes
         }
+        //add by rex KT_VIDEO_SUSPEND start
+        mSizeWidth=width;
+        mSizeHeight=height;
+        //add by rex KT_VIDEO_SUSPEND end
+
         setMeasuredDimension(width, height);
    }

+    <!--add by douxiang KT_VIDEO_SUSPEND start-->
+    <integer name="def_video_position">0</integer>
+    <bool name="def_video_suspend">false</bool>
+    <!--add by douxiang KT_VIDEO_SUSPEND end-->
+    

frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java

+            //add by rex KT_VIDEO_SUSPEND --->start
+	    loadBooleanSetting(stmt, Settings.Global.VIDEOSUSPEND_CLICKED,  
+                    R.bool.def_video_suspend);
+            loadIntegerSetting(stmt, Settings.Global.VIDEO_POSITION,
+                    R.integer.def_video_position);
+	    //add by rex KT_VIDEO_SUSPEND --->end
+

packages/apps/Gallery2/AndroidManifest.xml

+    <!--add by rex KT_VIDEO_SUSPEND start-->
+    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
+    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
+    <uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
+    <!--add by rex KT_VIDEO_SUSPEND end-->
+
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
@@ -95,6 +101,14 @@
         </activity>
         <!--@}-->
 
+        <!--add by rex KT_VIDEO_SUSPEND start-->
+	<service android:name="com.android.gallery3d.app.SuspendVideoWin">
+			<intent-filter>
+				<action android:name="com.android.gallery3d.app.ACTION_SUSPEND_VIDEO" />
+			</intent-filter>	
+	</service>
+        <!--add by rex KT_VIDEO_SUSPEND end-->
+

packages/apps/Gallery2/res/drawable/ic_media_fullscreen.png

packages/apps/Gallery2/res/drawable/ic_menu_close_clear_cancel.png

+alps_n0/packages/apps/Gallery2/res/layout/suspend_view.xml

+<?xml version="1.0" encoding="utf-8"?>
+
+<!--add by rex KT_VIDEO_SUSPEND start-->
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="fill_parent"
+    android:layout_height="wrap_content"
+    android:id="@+id/suspendLayout" 
+    android:gravity="bottom|center"
+    >
+
+      <!--com.mediatek.gallery3d.video.MTKVideoView -->
+     <VideoView 
+        android:id="@+id/suspend"
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+      />
+ 
+    <LinearLayout
+        android:layout_width="fill_parent"
+        android:layout_height="fill_parent"
+        android:gravity="bottom|center"
+        >
+
+        <ImageButton android:id="@+id/close"   
+         style="@android:style/MediaButton.Play" 
+        />
+
+        <ImageButton android:id="@+id/pause" 
+        style="@android:style/MediaButton.Play" 
+        />
+
+        <ImageButton android:id="@+id/fullscreen" 
+          style="@android:style/MediaButton.Play" 
+        />
+
+    </LinearLayout>
+
+    <!--LinearLayout
+        android:layout_width="fill_parent"
+        android:layout_height="fill_parent"
+        android:paddingTop="160dip"
+        android:gravity="top|center"
+        android:layout_marginLeft="-8dp"
+        >
+        <ImageButton android:id="@+id/pause" 
+        style="@android:style/MediaButton.Play" 
+        />
+
+    </LinearLayout-->
+
+</RelativeLayout>
+<!--add by rex KT_VIDEO_SUSPEND end-->

packages/apps/Gallery2/res/values/strings.xml

+    <!--add by rex KT_VIDEO_SUSPEND start-->
+    <string name="video_suspend">Small window</string>
+    <!--add by rex KT_VIDEO_SUSPEND end-->

packages/apps/Gallery2/src/com/android/gallery3d/app/MoviePlayer.java

+//add by rex KT_VIDEO_SUSPEND start
+import com.android.gallery3d.app.SuspendVideoWin;
+//add by rex KT_VIDEO_SUSPEND end
 public class MoviePlayer implements MediaPlayerWrapper.Listener,
         ControllerOverlay.Listener, LoaderListener,
         AudioManager.OnAudioFocusChangeListener,
@@ -882,6 +885,31 @@ public class MoviePlayer implements MediaPlayerWrapper.Listener,
     }
 
     public void onDestroy() {
+
+        //add by rex KT_VIDEO_SUSPEND start
+        /*
+        int tmp_pos=mMediaPlayerWrapper.getCurrentPosition();
+           try {
+                Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.VIDEO_POSITION,tmp_pos);
+               } catch (NumberFormatException e) {
+               }
+         */
+         if(Settings.Global.getInt(mContext.getContentResolver(),Settings.Global.VIDEOSUSPEND_CLICKED, 0) == 1){
+             int tmp_pos=mVideoPosition;//mMediaPlayerWrapper.getCurrentPosition();
+             Log.e(TAG, "mVideoPosition1" + String.valueOf(mVideoPosition));
+             Uri original = mMovieItem.getUri();
+             Intent intent = new Intent(mContext, SuspendVideoWin.class);
+             intent.setPackage("com.android.gallery3d.app");
+             intent.setData(original);
+
+             Bundle bundle1 = new Bundle();
+             bundle1.putInt("mPosition", tmp_pos);
+             intent.putExtras(bundle1);
+             //intent.putExtra(  "mPosition", tmp_pos.toString()  );
+             mContext.startService(intent);
+             Settings.Global.putInt(mContext.getContentResolver(),Settings.Global.VIDEOSUSPEND_CLICKED, 0);
+         }
+        //add by rex KT_VIDEO_SUSPEND end
         Log.v(TAG, "onDestroy");
         if (mVirtualizer != null) {
             mVirtualizer.release();
@@ -960,6 +988,16 @@ public class MoviePlayer implements MediaPlayerWrapper.Listener,
         if (position > 0 && (mVideoCanSeek || mMediaPlayerWrapper.canSeekForward())) {
             mMediaPlayerWrapper.seekTo(position);
         }
+
+        //add by rex KT_VIDEO_SUSPEND start
+        if(Settings.Global.getInt(mContext.getContentResolver(),Settings.Global.VIDEOSUSPEND_CLICKED, 0) == 1){
+                //mVideoPosition =Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.VIDEO_POSITION);
+                mVideoPosition =Settings.Global.getInt(mContext.getContentResolver(),Settings.Global.VIDEO_POSITION,0);
+                Log.e(TAG, "mVideoPosition3" + String.valueOf(mVideoPosition));
+                mMediaPlayerWrapper.seekTo(mVideoPosition);
+                Settings.Global.putInt(mContext.getContentResolver(),Settings.Global.VIDEOSUSPEND_CLICKED, 0);   
+         }
+        //add by rex KT_VIDEO_SUSPEND end
         if (enableFasten) {
             mMediaPlayerWrapper.setDuration(duration);
        }

+alps_n0/packages/apps/Gallery2/src/com/android/gallery3d/app/SuspendVideoWin.java

+package com.android.gallery3d.app;
+
+import java.io.File;
+import java.io.IOException;
+
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Binder;
+import android.os.IBinder;
+
+//import android.widget.ImageView;
+import android.widget.RelativeLayout;
+
+import android.app.Service;
+import android.app.ActivityManager;
+import android.util.Log;
+	
+import android.content.Context;
+import android.content.Intent;
+import android.view.MotionEvent;
+import android.view.WindowManager;
+import android.view.View.OnClickListener;
+import android.view.WindowManager.LayoutParams;
+import android.view.View.OnKeyListener;
+import android.view.View.OnTouchListener;
+import android.view.Gravity; 
+import android.view.LayoutInflater;
+import android.graphics.PixelFormat;
+import android.provider.Settings;
+import android.provider.Settings.SettingNotFoundException;
+
+import android.app.ActionBar;
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.media.AudioManager;
+import android.media.MediaPlayer;
+import android.net.Uri;
+import android.provider.MediaStore;
+import android.provider.MediaStore.Video;
+import android.provider.MediaStore.Video.Media;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.Window;
+import android.widget.TextView;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteException;
+import android.os.StatFs;
+import android.view.KeyEvent;
+
+import com.android.gallery3d.R;
+import com.android.gallery3d.util.SaveVideoFileInfo;
+import com.mediatek.gallery3d.video.SlowMotionItem;
+import com.android.gallery3d.util.SaveVideoFileUtils;
+import com.android.gallery3d.common.ApiHelper;
+
+import java.io.File;
+import java.io.IOException;
+import android.view.Gravity; 
+
+import android.media.MediaFormat;
+import android.media.MediaPlayer;
+import android.media.MediaPlayer.OnCompletionListener;
+import android.media.MediaPlayer.OnErrorListener;
+import android.media.MediaPlayer.OnInfoListener;
+
+import android.widget.MediaController;
+import android.widget.VideoView;
+
+import android.app.ActionBar;
+import android.app.Activity;
+
+import android.os.Message;
+
+import android.widget.ImageButton; 
+
+public class SuspendVideoWin extends Service
+{
+    private static final String TAG = "SuspendMoviePlayer";
+    public boolean quit;
+    private Context mContext;
+
+    //控件
+    private ImageButton mPauseButton;
+    private ImageButton mBackButton;
+    private ImageButton mFinishButton;
+    private static final int sDefaultTimeout = 3000;
+
+    private static WindowManager mWindowManager;
+    private static WindowManager.LayoutParams mLayout;
+    private static View mWindowsView= null;
+    private RelativeLayout rootView;
+    private VideoView mVideoView;
+
+    private Uri mUri;
+
+     private MediaController mController;  
+    //窗口宽高值
+    float x, y;
+    //状态栏高
+    private  int result = 0; 
+    //屏幕寬高
+    int WIDTH;
+    int HEIGHT;
+
+    private String mSrcVideoPath = null;
+    private int mVideoPosition = 0;
+    //private MockMediaPlayerControl mediaPlayerControl;
+
+       public IBinder onBind (Intent intent)
+	{
+	    Log.d(TAG, "Service is binded");
+	    return null;
+	}
+
+        public void onCreate()
+	{
+            Log.d(TAG, "Service is oncreate");	
+	}
+
+       public int onStartCommand(Intent intent, int flags, int startId)
+	{
+             Log.d(TAG, "Service is onStartCommand");
+             //Intent intent = getIntent();
+             mUri = intent.getData();
+         
+             Bundle bundle = new Bundle();
+             bundle = intent.getExtras();
+
+             mVideoPosition= bundle.getInt("mPosition");
+             Log.e(TAG, "mVideoPosition2" + String.valueOf(mVideoPosition));
+             //mSrcVideoPath = intent.getStringExtra(PhotoPage.KEY_MEDIA_ITEM_PATH);
+	     showFlowWindow();
+             quit = false;
+	    return START_STICKY;
+	}
+		
+	public boolean onUnbind(Intent intent)
+	{
+		Log.d(TAG, "Service is Unbinded");
+		return true;
+	}
+	
+	public void onDestroy()
+	{
+		super.onDestroy();
+		
+                mVideoView.stopPlayback();
+                if(!quit)
+                mWindowManager.removeView(mWindowsView);
+                quit = true;
+		Log.d(TAG, "Service is Destroyed");
+	}
+
+
+      public void showFlowWindow()
+      {
+        mWindowManager = (WindowManager) getApplicationContext().getSystemService("window");
+        mLayout = new WindowManager.LayoutParams();
+        mLayout.type = LayoutParams.TYPE_SYSTEM_ALERT;  
+        mLayout.format = PixelFormat.TRANSPARENT;
+        mLayout.flags = WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN
+                        | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
+                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
+                         |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
+
+        mLayout.format = PixelFormat.RGBA_8888;
+        mLayout.gravity = Gravity.LEFT | Gravity.TOP;
+        mLayout.x = 0;  
+        mLayout.y = 0;   
+
+        WIDTH = mWindowManager.getDefaultDisplay().getWidth();
+        HEIGHT = mWindowManager.getDefaultDisplay().getHeight();
+        //mLayout.width = WIDTH*1/2;
+        //mLayout.height = HEIGHT*1/2;
+
+        mLayout.width = 480;
+        mLayout.height = 640;
+
+        mWindowsView = View.inflate(this, R.layout.suspend_view, null);
+        rootView = (RelativeLayout)mWindowsView.findViewById(R.id.suspendLayout);
+        mVideoView = (VideoView)mWindowsView.findViewById(R.id.suspend);
+        mWindowManager.addView(mWindowsView, mLayout);
+
+        int resourceId = getResources().getIdentifier("status_bar_height","dimen", "android"); 
+        if (resourceId > 0) 
+        {  
+        result = getResources().getDimensionPixelSize(resourceId);  
+        } 
+       
+        mWindowsView.setOnTouchListener(new View.OnTouchListener() {
+            float mTouchStartX;
+            float mTouchStartY;
+            @Override
+            public boolean onTouch(View view, MotionEvent event) {
+                if (event.getPointerCount() > 1) 
+                 {
+                     return true;
+                 }
+                x = event.getRawX();
+                y = event.getRawY();
+                Log.e(TAG , "onclick1"+x);
+                switch (event.getAction()) {
+                    case MotionEvent.ACTION_DOWN:
+                        mTouchStartX = event.getX();
+                        mTouchStartY = event.getY()+result;  
+                        Log.e(TAG , "onclick2"+mTouchStartX);
+                        show(sDefaultTimeout);
+                        break;
+
+                    case MotionEvent.ACTION_MOVE:
+                        mLayout.x = (int) (x - mTouchStartX);
+                        mLayout.y = (int) (y - mTouchStartY);
+                        Log.e(TAG , "onclick3"+mLayout.x);
+                        if(   80<x&&(x<WIDTH-80)&&80<y&&(y<HEIGHT-80)  )
+                            updateWinViewLayout();
+                        break;
+
+                   case MotionEvent.ACTION_UP:
+                        break;
+                       }
+                   return true;
+                 }
+             });
+
+        /*
+        //采用自带的控制控件添加在视频之上,有问题,需要重写控制类
+        //mediaPlayerControl = new MediaController.MediaPlayerControl();
+        //mController = new MediaController(getApplicationContext()); 
+        //mController = new MediaController(getActivity()); 
+        //MockMediaPlayerControl mediaPlayerControl = new MockMediaPlayerControl();
+        //mController = (MediaController)mWindowsView.findViewById(R.id.mController);
+        mController = new MediaController(getApplicationContext()); 
+
+        //((ViewGroup) rootView).addView(mController.getView());
+        //((ViewGroup) rootView).addView(mController.getParent());
+        //mWindowManager.addView(mController, mLayout);
+
+        //mController.setMediaPlayer(mVideoView);
+        mVideoView.setMediaController(mController);
+        mController.setAnchorView(mVideoView);  
+        mController.setKeepScreenOn(true); 
+        */
+
+        mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
+            @Override
+            public boolean onError(MediaPlayer mp, int what, int extra)
+            {
+                mWindowManager.removeView(mWindowsView);
+                quit = true;
+                return false;
+            }
+        });
+
+        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
+            @Override
+            public void onCompletion(MediaPlayer mp) {
+                  //播放完直接关闭
+                  mVideoView.stopPlayback();
+                  mWindowManager.removeView(mWindowsView);
+                  quit = true;
+                  /*
+                  //播放完还原
+                  int tmp_pos=mVideoView.getCurrentPosition();
+                  try {   
+                      Settings.Global.putInt(getApplicationContext().getContentResolver(), Settings.Global.VIDEO_POSITION,tmp_pos);
+                      Settings.Global.putInt(getApplicationContext().getContentResolver(),Settings.Global.VIDEOSUSPEND_CLICKED, 1);
+                      } catch (NumberFormatException e) {
+                      }
+                  Intent intent = new Intent(getApplicationContext(), MovieActivity.class)
+                                 .setDataAndType(mUri, "video/*");
+                  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                  getApplicationContext().startActivity(intent);
+                  Log.e(TAG, "douyxiangdouyxiang10" + String.valueOf(tmp_pos));
+                  */
+
+            }
+        });
+
+        mVideoView.setVideoURI(mUri, null);
+
+        mHandlerSize.postDelayed(mRunnableSize, 100);
+        
+        mVideoView.start();
+        mVideoView.requestFocus();
+        mVideoView.seekTo(mVideoPosition);
+
+        mPauseButton = (ImageButton) mWindowsView.findViewById(R.id.pause);
+        if (mPauseButton != null) {
+            mPauseButton.requestFocus();
+            mPauseButton.setOnClickListener(mPauseListener);
+        }
+
+        mBackButton = (ImageButton) mWindowsView.findViewById(R.id.fullscreen);
+        if (mBackButton != null) {
+            mBackButton.requestFocus();
+            mBackButton.setOnClickListener(mBackListener);
+        }
+        mBackButton.setImageResource(R.drawable.ic_media_fullscreen);
+        //mBackButton.setImageResource(com.android.internal.R.drawable.ic_media_fullscreen_stretch);
+
+        mFinishButton = (ImageButton) mWindowsView.findViewById(R.id.close);
+        if (mFinishButton != null) {
+            mFinishButton.requestFocus();
+            mFinishButton.setOnClickListener(mCloseListener);
+        }
+        mFinishButton.setImageResource(R.drawable.ic_menu_close_clear_cancel);
+
+         show(sDefaultTimeout);
+        
+     }
+
+    private Runnable mRunnableSize = new Runnable() {
+         @Override
+         public void run() {
+             mHandlerSize.sendEmptyMessage(1);
+         }
+    };
+ 
+    Handler mHandlerSize = new Handler() {
+         @Override
+         public void handleMessage(Message msg) {
+             super.handleMessage(msg);
+             updateWinViewSize();
+         }
+    };
+
+    private Runnable mRunnable = new Runnable() {
+         @Override
+         public void run() {
+             mHandler.sendEmptyMessage(1);
+         }
+    };
+ 
+    Handler mHandler = new Handler() {
+         @Override
+         public void handleMessage(Message msg) {
+             super.handleMessage(msg);
+             hide();
+         }
+    };
+    
+    public void updateWinViewLayout() {
+        mWindowManager.updateViewLayout(mWindowsView, mLayout); 
+    }
+
+    public void updateWinViewSize() {
+        if( mVideoView.GetSizeWidth()< 480  ||   mVideoView.GetSizeHeight()<640)
+        {
+            mLayout.width = mVideoView.GetSizeWidth();
+            mLayout.height = mVideoView.GetSizeHeight();
+            updateWinViewLayout();
+        }
+        else if(mVideoView.GetSizeWidth()/mVideoView.GetSizeHeight()>1)
+        {
+            mLayout.width = mVideoView.GetSizeWidth();
+            mLayout.height = mVideoView.GetSizeHeight();
+            updateWinViewLayout();
+        }
+
+        updateWinViewLayout();
+    }
+
+    /*
+    private void updatePausePlay() {
+        if (mWindowsView == null || mPauseButton == null)
+            return;
+        if (mVideoView.isPlaying()) {
+            mPauseButton.setImageResource(com.android.internal.R.drawable.ic_media_pause);
+        } else {
+            mPauseButton.setImageResource(com.android.internal.R.drawable.ic_media_play);
+        }
+    }
+    */
+
+    private void doPauseResume() {
+        if (mVideoView.isPlaying()) {
+            mVideoView.pause();
+            mPauseButton.setImageResource(com.android.internal.R.drawable.ic_media_pause);
+        } else {
+            mVideoView.start();
+            mPauseButton.setImageResource(com.android.internal.R.drawable.ic_media_play);
+        }
+        //updatePausePlay();
+    }
+
+    private final View.OnClickListener mPauseListener = new View.OnClickListener() {
+        @Override
+        public void onClick(View v) {
+            doPauseResume();
+            mHandler.removeCallbacks(mRunnable);
+                mHandler.postDelayed(mRunnable, 3000);
+            
+        }
+    };
+
+    private final View.OnClickListener mBackListener = new View.OnClickListener() {
+        @Override
+        public void onClick(View v) {
+        int tmp_pos=mVideoView.getCurrentPosition();                   
+        Settings.Global.putInt(getApplicationContext().getContentResolver(), Settings.Global.VIDEO_POSITION,tmp_pos);
+        Settings.Global.putInt(getApplicationContext().getContentResolver(),Settings.Global.VIDEOSUSPEND_CLICKED, 1);
+
+        Intent intent = new Intent(getApplicationContext(), MovieActivity.class)
+                        .setDataAndType(mUri, "video/*");
+        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                                
+        mVideoView.stopPlayback();
+        mWindowManager.removeView(mWindowsView);
+        quit = true;
+        getApplicationContext().startActivity(intent);
+        }
+    };
+
+    private final View.OnClickListener mCloseListener = new View.OnClickListener() {
+        @Override
+        public void onClick(View v) {
+        mVideoView.stopPlayback();
+        mWindowManager.removeView(mWindowsView);
+        quit = true;
+        }
+    };
+
+    public void show(int timeout) {
+        mPauseButton.setVisibility(View.VISIBLE);
+        mBackButton.setVisibility(View.VISIBLE);
+        mFinishButton.setVisibility(View.VISIBLE);
+        mHandler.removeCallbacks(mRunnable);
+        if (timeout != 0 ) {
+             mHandler.postDelayed(mRunnable, timeout);
+        }
+
+    }
+ 
+    public void hide() {
+        mPauseButton.setVisibility(View.GONE);
+        mBackButton.setVisibility(View.GONE);
+        mFinishButton.setVisibility(View.GONE);
+    }
+
+}

packages/apps/Gallery2/src/com/mediatek/gallery3d/video/ExtensionHelper.java

+//add by rex KT_VIDEO_SUSPEND start
+import android.os.SystemProperties;
+//add by rex KT_VIDEO_SUSPEND end
 public class ExtensionHelper {
     private static final String TAG = "Gallery2/VideoPlayer/ExtensionHelper";
     private static final boolean LOG = true;
@@ -62,6 +64,10 @@ public class ExtensionHelper {
         group.addHooker(new TrimVideoHooker());
         group.addHooker(new NfcHooker());
         group.addHooker(new LetvHooker());
+        //add by rex KT_VIDEO_SUSPEND start
+        if (SystemProperties.getInt("ro.kt_small_video", 0) == 1) 
+              group.addHooker(new SuspendVideoHooker()); 
+        //add by rex KT_VIDEO_SUSPEND end
         if (MtkVideoFeature.isSlowMotionSupport() && sHasRewindAndForward) {
             group.addHooker(new SlowMotionHooker());
        }

+alps_n0/packages/apps/Gallery2/src/com/mediatek/gallery3d/video/SuspendVideoHooker.java

+package com.mediatek.gallery3d.video;
+
+import android.view.Menu;
+import android.view.MenuItem;
+import com.android.gallery3d.R;
+import android.content.Context;
+import android.content.Intent;
+
+import android.database.Cursor;
+import android.database.sqlite.SQLiteException;
+import android.net.Uri;
+import android.provider.MediaStore;
+import com.android.gallery3d.app.SuspendVideoWin;
+import com.android.gallery3d.app.PhotoPage;
+import com.mediatek.galleryframework.util.MtkLog;
+import android.provider.Settings;
+import java.lang.Exception;
+import android.os.SystemProperties;
+import android.content.BroadcastReceiver;
+import android.app.Activity;
+
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Binder;
+import android.os.IBinder;
+import android.os.Message;
+
+public class SuspendVideoHooker extends MovieHooker 
+{
+    private static final int MENU_SUSPEND = 1;
+    private MenuItem mMenuSuspend;
+    private static final String TAG = "SuspendVideo";
+
+    private IMovieItem mMovieItem;
+    private static final String VIDEO_CONTENT_MEDIA = "content://media/external/video/media";
+
+    @Override
+    public void setParameter(final String key, final Object value) {
+        super.setParameter(key, value);
+        MtkLog.v(TAG, "setParameter(" + key + ", " + value + ")");
+        if (value instanceof IMovieItem) {
+            mMovieItem = (IMovieItem) value;
+        }
+    }
+
+    @Override
+    public boolean onCreateOptionsMenu(final Menu menu) {
+        super.onCreateOptionsMenu(menu);
+        mMenuSuspend = menu.add(MENU_HOOKER_GROUP_ID, getMenuActivityId(MENU_SUSPEND), 0, R.string.video_suspend);
+        return true;
+    }
+    @Override
+    public void setVisibility(boolean visible) {
+        if (mMenuSuspend != null) {
+            mMenuSuspend.setVisible(visible);
+        }
+    }
+    @Override
+    public boolean onPrepareOptionsMenu(final Menu menu) {
+        super.onPrepareOptionsMenu(menu);
+        if (MovieUtils.isLocalFile(mMovieItem.getUri(), mMovieItem.getMimeType()) &&
+            //MovieTitleHelper.isUriValid(getContext(), mMovieItem.getUri()) &&
+            isUriSupportSuspend(mMovieItem.getUri()) &&
+            !isDrmFile(getContext(), mMovieItem.getUri())) {
+            mMenuSuspend.setVisible(true);
+        } else {
+            mMenuSuspend.setVisible(false);
+        }   
+        return true;
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(final MenuItem item) {
+        super.onOptionsItemSelected(item);
+        switch(getMenuOriginalId(item.getItemId())) {
+        case MENU_SUSPEND:
+            //已经启动一个悬浮视频则杀死
+           Intent intent = new Intent(getContext(), SuspendVideoWin.class);
+           intent.setPackage("com.android.gallery3d.app");
+           getContext().stopService(intent);
+
+            Settings.Global.putInt(getContext().getContentResolver(),Settings.Global.VIDEOSUSPEND_CLICKED, 1);
+            /*
+            //以下为直接启动方式,但不保证小窗口启动时原画面activity已经结束
+            Uri original = mMovieItem.getUri();
+            Intent intent = new Intent(getContext(), SuspendVideoWin.class);
+            //intent.setAction("com.android.gallery3d.app.ACTION_SUSPEND_VIDEO"); 
+            //Intent intent = new Intent(getContext(), SuspendVideo.class);
+            //Intent intent = new Intent(getContext(), SuspendVideoWin.class);
+            intent.setPackage("com.android.gallery3d.app");
+            intent.setData(original);
+            //intent.putExtra(PhotoPage.KEY_MEDIA_ITEM_PATH,
+                    //getVideoPath(getContext(), original));
+            //getContext().startActivity(intent);
+            //getContext().sendBroadcast(intent);
+            getContext().startService(intent);
+           
+            Intent intent_ext = new Intent();
+            intent_ext.setAction("Suspend_Action"); 
+            getContext().sendBroadcast(intent_ext);
+           */
+
+            //getContext().finish();
+            //getContext().onDestory();
+
+            mHandlerStart.postDelayed(mRunnableStart, 500);
+
+            return true;
+        default:
+            return false;
+        }
+    }
+/*
+private  boolean isServiceRunning() {  
+        ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);  
+        List<ActivityManager.RunningServiceInfo> serviceList = activityManager  
+            .getRunningServices(100);
+        if (serviceList == null || serviceList.size() == 0)  
+            return false;  
+        for (RunningServiceInfo info : serviceList) {  
+            Log.e(TAG, info.service.getClassName());
+            if (info.service.getClassName().equals("com.android.gallery3d.MicVol"))  
+               return true;  
+        }  
+        return false;  
+    } 
+*/
+
+    private Runnable mRunnableStart = new Runnable() {
+         @Override
+         public void run() {
+             mHandlerStart.sendEmptyMessage(1);
+         }
+    };
+ 
+    Handler mHandlerStart = new Handler() {
+         @Override
+         public void handleMessage(Message msg) {
+             super.handleMessage(msg);
+             getContext().finish();
+         }
+    };
+
+private String getVideoPath(final Context context, Uri uri) {
+        String videoPath = null;
+        Cursor cursor = null;
+        MtkLog.v(TAG, "getVideoPath(" + uri + ")");
+        try {
+            //query from "content://....."
+            cursor = context.getContentResolver().query(uri,
+                    new String[] { MediaStore.Video.Media.DATA }, null, null,
+                    null);
+            //query from "file:///......"
+            if (cursor == null) {
+                String data = Uri.decode(uri.toString());
+                if (data == null) {
+                    return null;
+                }
+                data = data.replaceAll("'", "''");
+                final String where = "_data LIKE '%" + data.replaceFirst("file:///", "") + "'";
+                cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
+                    new String[]{MediaStore.Video.Media.DATA}, where, null, null);
+            }
+            if (cursor != null && cursor.moveToFirst()) {
+                videoPath = cursor.getString(0);
+            }
+        } catch (final SQLiteException ex) {
+            ex.printStackTrace();
+        } catch (IllegalArgumentException e) {
+            // if this exception happen, return false.
+            MtkLog.v(TAG, "ContentResolver query IllegalArgumentException");
+        } finally {
+            if (cursor != null) {
+                cursor.close();
+            }
+        }
+        return videoPath;
+    }
+private boolean isDrmFile(final Context context, Uri uri) {
+       Cursor cursor = null;
+       int result = 0;
+       MtkLog.v(TAG, "isDrmFile(" + uri + ")");
+       try {
+              cursor = context.getContentResolver().query(uri,
+                    new String[]{MediaStore.Video.Media.IS_DRM}, null, null, null);
+
+              if (cursor == null) {
+                  String data = Uri.decode(uri.toString());
+                  if (data == null) {
+                      return false;
+                  }
+                  data = data.replaceAll("'", "''");
+                  final String where = "_data LIKE '%" + data.replaceFirst("file:///", "") + "'";
+
+                  cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
+                      new String[]{MediaStore.Video.Media.IS_DRM}, where, null, null);
+                              }
+
+              if (cursor != null && cursor.moveToFirst()) {
+                    result = cursor.getInt(0);
+            }
+            } catch (final SQLiteException ex) {
+                ex.printStackTrace();
+            } catch (IllegalArgumentException e) {
+                //if this exception happen, return false.
+                MtkLog.v(TAG, "isDrmFile ContentResolver query IllegalArgumentException");
+            } finally {
+                if (cursor != null) {
+                    cursor.close();
+                }
+            }
+       MtkLog.v(TAG, "result = " + result);
+       // "1" means query from db, this file is drm file.
+       return (1 == result);
+
+    }
+
+    private boolean isUriSupportSuspend(Uri uri) {
+        return String.valueOf(uri).toLowerCase().startsWith(VIDEO_CONTENT_MEDIA) ||
+            String.valueOf(uri).toLowerCase().startsWith("file://");
+    }
+
+
+}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值