Skip to content

Commit

Permalink
Merge pull request #100 from ksvc/floatingPlaying
Browse files Browse the repository at this point in the history
Floating playing
  • Loading branch information
FirmianaRain authored Mar 22, 2017
2 parents 49da019 + 36ebb3a commit 4d31484
Show file tree
Hide file tree
Showing 31 changed files with 1,224 additions and 17 deletions.
2 changes: 1 addition & 1 deletion KsyunPlayerDemo/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
buildToolsVersion '25.0.0'

defaultConfig {
applicationId "com.ksyun.player.demo"
Expand Down
12 changes: 12 additions & 0 deletions KsyunPlayerDemo/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<application
android:allowBackup="true"
Expand All @@ -40,6 +41,17 @@
android:launchMode="singleTop">
</activity>

<activity android:name=".activity.FloatingVideoActivity"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="orientation|keyboardHidden|screenSize"
/>
<activity android:name=".activity.FloatingPlayingActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="orientation|keyboardHidden|screenSize"
/>

<activity android:name=".activity.SettingActivity"></activity>
<activity
android:configChanges="orientation|keyboardHidden|screenSize"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package com.ksyun.player.demo.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.ksyun.player.demo.R;
import com.ksyun.player.demo.view.KSYFloatingWindowView;

/**
* Created by xbc on 2017/3/14.
*/

public class FloatingPlayingActivity extends Activity implements Handler.Callback {

public static final int LEAVE_FLOATING_WINDOW_PLAYING = 6789;
public static final int REMOVE_FLOATING_WINDOW = LEAVE_FLOATING_WINDOW_PLAYING + 1;

private ImageView mFloatingLeave;

private KSYFloatingWindowView mFloatingView;
private WindowManager.LayoutParams mFloatingViewParams;

private WindowManager mWindowManager;

private Handler mHandler;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_floating_playing);

mFloatingLeave = (ImageView) findViewById(R.id.floating_playing_back);
mFloatingLeave.setOnClickListener(mOnClickListener);

mHandler = new Handler(getMainLooper(), this);
createFloatingWindow(getApplicationContext());
}

@Override
protected void onDestroy() {
super.onDestroy();

removeFloatingWindow(getApplicationContext());
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (event.getAction()) {
case KeyEvent.KEYCODE_BACK:
FloatingPlayingActivity.this.finish();
break;
}
return super.onKeyDown(keyCode, event);
}

private View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
FloatingPlayingActivity.this.finish();
}
};

private void createFloatingWindow(Context context) {
if (context == null)
return;

WindowManager windowManager = getWindowManager(context);
int screenWidth = windowManager.getDefaultDisplay().getWidth();
int screenHeight = windowManager.getDefaultDisplay().getHeight();
if (mFloatingView == null) {
mFloatingView = new KSYFloatingWindowView(context);
mFloatingView.setHandler(mHandler);
if (mFloatingViewParams == null) {
mFloatingViewParams = new WindowManager.LayoutParams();
mFloatingViewParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
mFloatingViewParams.format = PixelFormat.RGBA_8888;
mFloatingViewParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
mFloatingViewParams.gravity = Gravity.LEFT | Gravity.TOP;
mFloatingViewParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mFloatingViewParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
mFloatingViewParams.x = screenWidth;
mFloatingViewParams.y = screenHeight;
}

mFloatingView.updateViewLayoutParams(mFloatingViewParams);
windowManager.addView(mFloatingView, mFloatingViewParams);
}
}

public void removeFloatingWindow(Context context) {
if (mFloatingView != null) {
WindowManager windowManager = getWindowManager(context);
windowManager.removeView(mFloatingView);
mFloatingView = null;
}
}

private WindowManager getWindowManager(Context context) {
if (mWindowManager == null) {
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
}
return mWindowManager;
}

@Override
public boolean handleMessage(Message message) {
switch (message.what) {
case LEAVE_FLOATING_WINDOW_PLAYING:
FloatingPlayingActivity.this.finish();
break;
case REMOVE_FLOATING_WINDOW:
removeFloatingWindow(getApplicationContext());
break;
}
return false;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Loading

0 comments on commit 4d31484

Please sign in to comment.