package com.tyd.floatball.service;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.Arrays;
import android.app.Instrumentation;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetManager;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.InputDevice;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.TouchDelegate;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.tyd.floatball.R;
import com.tyd.floatball.util.MyApplication;
import android.hardware.input.InputManager;
public class TopFloatService extends Service implements OnClickListener, OnKeyListener, OnTouchListener {
private static final String TAG = "cchen";
WindowManager wm = null;
WindowManager.LayoutParams ballWmParams = null;
private View ballView;
private View menuView;
private float mTouchStartX;
private float mTouchStartY;
private float x;
private float y;
private RelativeLayout menuLayout;
private Button floatImage;
private PopupWindow pop;
private RelativeLayout menuTop;
private boolean ismoving = false;
private TextView btn_favor;
private TextView btn_home_screen;
private TextView btn_lock_screen;
private TextView btn_setting;
private boolean debug = false;
private static int countClick = 0;
private BroadcastReceiver onOffReceiver;
private boolean showing = false;
/*
* private boolean showingMenu = false;
*/private boolean floatImageIsPrepared = false;
protected boolean mUped;
protected boolean mDowned;
protected boolean toDismiss;
private WindowManager.LayoutParams menuWmParams;
private int pop_h;
private int pop_w;
private DisplayMetrics dm;
protected float mLastX;
protected float mLastY;
protected int mMenuX;
protected int mMenuY;
protected int mLastMenuX;
protected int mLastMenuY;
protected int countMenuMove;
public static boolean RUNING = false;
@Override
public void onCreate() {
super.onCreate();
pop_w = (int) getApplicationContext().getResources().getDimension(R.dimen.size_dialog_w);
pop_h = (int) getApplicationContext().getResources().getDimension(R.dimen.size_dialog_h);
dm = getResources().getDisplayMetrics();
// 加载辅助球布局
if (ballView == null)
ballView = LayoutInflater.from(this).inflate(R.layout.floatball, null);
floatImage = (Button) ballView.findViewById(R.id.float_image);
setUpFloatMenuView();
createView();
RUNING = true;
onOffReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("com.cchen.onoff.toolball") && wm != null) {
boolean on = intent.getBooleanExtra("on", true);
if (showing && !on) {
mRemoveView(ballView);
showing = false;
} else if (!showing && on) {
createView();
}
}
}
};
this.registerReceiver(onOffReceiver, new IntentFilter("com.cchen.onoff.toolball"));
}
@Override
public void onDestroy() {
this.unregisterReceiver(onOffReceiver);
mRemoveView(menuView);
mRemoveView(ballView);
if (showing && wm != null) {
showing = false;
// showingMenu = false;
floatImageIsPrepared = false;
}
super.onDestroy();
}
/**
* 窗口菜单初始化
*/
private void setUpFloatMenuView() {
menuView = LayoutInflater.from(this).inflate(R.layout.floatmenu_veri, null);
menuLayout = (RelativeLayout) menuView.findViewById(R.id.menu);
menuTop = (RelativeLayout) menuView.findViewById(R.id.lay_main);
menuLayout.setOnClickListener(this);
menuLayout.setOnKeyListener(this);
menuTop.setOnClickListener(this);
btn_favor = (TextView) menuView.findViewById(R.id.btn_dial);
btn_home_screen = (TextView) menuView.findViewById(R.id.btn_home_screen);
btn_lock_screen = (TextView) menuView.findViewById(R.id.btn_back);
btn_setting = (TextView) menuView.findViewById(R.id.btn_contacts);
btn_favor.setOnClickListener(this);
btn_home_screen.setOnClickListener(this);
btn_lock_screen.setOnClickListener(this);
btn_setting.setOnClickListener(this);
menuView.setOnTouchListener(this);
btn_favor.setOnTouchListener(this);
btn_home_screen.setOnTouchListener(this);
btn_lock_screen.setOnTouchListener(this);
btn_setting.setOnTouchListener(this);
}
protected void updateMenuViewPosition() {
menuWmParams.x += (int) (mMenuX - mLastMenuX); // 偏移量
menuWmParams.y += (int) (mMenuY - mLastMenuY); // 偏移量
wm.updateViewLayout(menuView, menuWmParams);
}
/**
* 通过MyApplication创建view,并初始化显示参数
*/
private void createView() {
if (wm == null)
wm = (WindowManager) getApplicationContext().getSystemService("window");
if (ballWmParams == null) {
ballWmParams = new WindowManager.LayoutParams();// ((MyApplication)
// getApplication()).getMywmParams();
ballWmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
ballWmParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
ballWmParams.gravity = Gravity.LEFT | Gravity.TOP;
ballWmParams.x = 50;
ballWmParams.y = 50;
ballWmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
ballWmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
ballWmParams.format = PixelFormat.RGBA_8888;
// 添加显示层
}
wm.addView(ballView, ballWmParams);
showing = true;
if (floatImageIsPrepared)
return;
/*
* floatImage.setClickable(true); Rect r = new Rect();
* floatImage.getHitRect(r); float density =
* getResources().getDisplayMetrics().density; r.right += 30 * density;
* r.left += 30 * density; r.bottom += 30 * density; r.top += 30 *
* density; ((View) floatImage.getParent()).setTouchDelegate(new
* TouchDelegate(r, floatImage));
*/
// 注册触碰事件监听器
floatImage.setOnTouchListener(new OnTouchListener() {
private int countMove = 0;
public boolean onTouch(View v, MotionEvent event) {
评论9
最新资源