Skip to content
This repository has been archived by the owner on Feb 3, 2020. It is now read-only.

Multiple Floating View Support #75

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
multiple view support added to FloatingViewListener, OnKeyListener su…
…pport added, remove method is now public to make easy to manage FloatingView instances
  • Loading branch information
brnogz committed Feb 6, 2018
commit b040147af392ad19f7f9807ed14bfa4fb51f39ba
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
* http:https://stackoverflow.com/questions/18503050/how-to-create-draggabble-system-alert-in-android
* FIXME:Nexus5+YouTubeアプリの場合にナビゲーションバーよりも前面に出てきてしまう
*/
class FloatingView extends FrameLayout implements ViewTreeObserver.OnPreDrawListener {
public class FloatingView extends FrameLayout implements ViewTreeObserver.OnPreDrawListener {

/**
* 移動に最低必要なしきい値(dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package jp.co.recruit_lifestyle.android.floatingview;


import android.view.View;

/**
* FloatingViewのリスナです。
*/
Expand All @@ -34,6 +36,6 @@ public interface FloatingViewListener {
* @param x x coordinate
* @param y y coordinate
*/
void onTouchFinished(boolean isFinishing, int x, int y);
void onTouchFinished(View v,boolean isFinishing, int x, int y);

}
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL)
if (mFloatingViewListener != null) {
final boolean isFinishing = mTargetFloatingView.getState() == FloatingView.STATE_FINISHING;
final WindowManager.LayoutParams params = mTargetFloatingView.getWindowLayoutParams();
mFloatingViewListener.onTouchFinished(isFinishing, params.x, params.y);
mFloatingViewListener.onTouchFinished(mTargetFloatingView, isFinishing, params.x, params.y);
}
}

Expand Down Expand Up @@ -467,8 +467,9 @@ public boolean isTrashViewEnabled() {
*
* @param view フローティングさせるView
* @param options Options
* @return floatingView
*/
public void addViewToWindow(View view, Options options) {
public FloatingView addViewToWindow(View view, Options options) {
final boolean isFirstAttach = mFloatingViewList.isEmpty();
// FloatingView
final FloatingView floatingView = new FloatingView(mContext);
Expand All @@ -482,6 +483,7 @@ public void addViewToWindow(View view, Options options) {
// set FloatingView size
final FrameLayout.LayoutParams targetParams = new FrameLayout.LayoutParams(options.floatingViewWidth, options.floatingViewHeight);
view.setLayoutParams(targetParams);
floatingView.setTag(view.getTag());
floatingView.addView(view);

// 非表示モードの場合
Expand All @@ -503,14 +505,16 @@ public void addViewToWindow(View view, Options options) {
}
// 必ずトップに来て欲しいので毎回貼り付け
mWindowManager.addView(mTrashView, mTrashView.getWindowLayoutParams());

return floatingView;
}

/**
* ViewをWindowから取り外します。
*
* @param floatingView FloatingView
*/
private void removeViewToWindow(FloatingView floatingView) {
public void removeViewToWindow(FloatingView floatingView) {
final int matchIndex = mFloatingViewList.indexOf(floatingView);
// 見つかった場合は表示とリストから削除
if (matchIndex != -1) {
Expand Down Expand Up @@ -542,6 +546,10 @@ public void removeAllViewToWindow() {
mFloatingViewList.clear();
}

public void setOnKeyListener(View.OnKeyListener listener){
mFullscreenObserverView.setOnKeyListener(listener);
}

/**
* FloatingViewを貼り付ける際のオプションを表すクラスです。
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.Build;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
Expand Down Expand Up @@ -62,6 +63,8 @@ class FullscreenObserverView extends View implements ViewTreeObserver.OnGlobalLa
*/
private final Rect mWindowRect;

private OnKeyListener mKeyListener;

static {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1) {
OVERLAY_TYPE = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
Expand All @@ -84,8 +87,7 @@ class FullscreenObserverView extends View implements ViewTreeObserver.OnGlobalLa
mParams.width = 1;
mParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
mParams.type = OVERLAY_TYPE;
mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
mParams.format = PixelFormat.TRANSLUCENT;

Expand Down Expand Up @@ -142,6 +144,17 @@ public void onSystemUiVisibilityChange(int visibility) {
}
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
return (mKeyListener != null && mKeyListener.onKey(this, event.getKeyCode(), event)) || super.dispatchKeyEvent(event);
}

@Override
public void setOnKeyListener(OnKeyListener l) {
super.setOnKeyListener(l);
mKeyListener = l;
}

/**
* WindowManager.LayoutParams
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import jp.co.recruit.floatingview.R;
import jp.co.recruit_lifestyle.android.floatingview.FloatingView;
import jp.co.recruit_lifestyle.android.floatingview.FloatingViewListener;
import jp.co.recruit_lifestyle.android.floatingview.FloatingViewManager;

Expand Down Expand Up @@ -52,27 +58,77 @@ public int onStartCommand(Intent intent, int flags, int startId) {
final WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
final LayoutInflater inflater = LayoutInflater.from(this);

final ImageView iconView = (ImageView) inflater.inflate(R.layout.widget_chathead, null, false);
iconView.setTag("one");

final ImageView iconView2 = (ImageView) inflater.inflate(R.layout.widget_chathead, null, false);
iconView2.setTag("two");

mFloatingViewManager = new FloatingViewManager(this, this);
mFloatingViewManager.setFixedTrashIconImage(R.drawable.ic_trash_fixed);
mFloatingViewManager.setActionTrashIconImage(R.drawable.ic_trash_action);

final FloatingView firstReference = mFloatingViewManager.addViewToWindow(iconView, getOptions(metrics, iconView));
final FloatingView secondReference = mFloatingViewManager.addViewToWindow(iconView2, getOptions(metrics, iconView2));


iconView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, getString(R.string.chathead_click_message));
mFloatingViewManager.removeViewToWindow(secondReference);
}
});
iconView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, getString(R.string.chathead_click_message) + " 2");
mFloatingViewManager.removeViewToWindow(firstReference);
}
});

mFloatingViewManager = new FloatingViewManager(this, this);
mFloatingViewManager.setFixedTrashIconImage(R.drawable.ic_trash_fixed);
mFloatingViewManager.setActionTrashIconImage(R.drawable.ic_trash_action);
final FloatingViewManager.Options options = new FloatingViewManager.Options();
options.overMargin = (int) (16 * metrics.density);
mFloatingViewManager.addViewToWindow(iconView, options);
mFloatingViewManager.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d(TAG, event.getKeyCode() + " " + event.getCharacters());

return true;
}
});

// 常駐起動
startForeground(NOTIFICATION_ID, createNotification(this));

return START_REDELIVER_INTENT;
}

@NonNull
private FloatingViewManager.Options getOptions(DisplayMetrics metrics, View iconView) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
FloatingViewManager.Options options = new FloatingViewManager.Options();
options.overMargin = (int) (16 * metrics.density);

// Last position
boolean isUseLastPosition = sharedPref.getBoolean("settings_save_last_position", false);
if (isUseLastPosition) {
final int defaultX = options.floatingViewX;
final int defaultY = options.floatingViewY;
options.floatingViewX = sharedPref.getInt(iconView.getTag()+"_x", defaultX);
options.floatingViewY = sharedPref.getInt(iconView.getTag()+"_y", defaultY);
} else {
// Init X/Y
final String initXSettings = sharedPref.getString("settings_init_x", "");
final String initYSettings = sharedPref.getString("settings_init_y", "");
if (!TextUtils.isEmpty(initXSettings) && !TextUtils.isEmpty(initYSettings)) {
final int offset = (int) (48 + 8 * metrics.density);
options.floatingViewX = (int) (metrics.widthPixels * Float.parseFloat(initXSettings) - offset);
options.floatingViewY = (int) (metrics.heightPixels * Float.parseFloat(initYSettings) - offset);
}
}
return options;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -103,11 +159,14 @@ public void onFinishFloatingView() {
* {@inheritDoc}
*/
@Override
public void onTouchFinished(boolean isFinishing, int x, int y) {
if (isFinishing) {
Log.d(TAG, getString(R.string.deleted_soon));
} else {
Log.d(TAG, getString(R.string.touch_finished_position, x, y));
public void onTouchFinished(View view, boolean isFinishing, int x, int y) {
if (!isFinishing) {
// Save the last position
final SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putInt(view.getTag() + "_x", x);
editor.putInt(view.getTag() + "_y", y);
editor.apply();

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void onFinishFloatingView() {
* {@inheritDoc}
*/
@Override
public void onTouchFinished(boolean isFinishing, int x, int y) {
public void onTouchFinished(View view, boolean isFinishing, int x, int y) {
if (!isFinishing) {
// Save the last position
final SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
Expand Down