package com.cx.staffloss;
/**
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class FloatViewService extends Service
{
private static final String TAG = "FloatViewService";
//定义浮动窗口布局
private LinearLayout mFloatLayout;
private WindowManager.LayoutParams wmParams;
//创建浮动窗口设置布局参数的对象
private WindowManager mWindowManager;
private ImageButton mFloatView;
Context mContext;
String filename="";
String filename2="";
String SPName="StaffLossSP";
ArrayList todoList = new ArrayList<>();
ArrayList doneList = new ArrayList<>();
public int[] iconList = new int[]{R.drawable.pic0,R.drawable.pic1,R.drawable.pic2,
R.drawable.pic3,R.drawable.pic4,R.drawable.pic5,R.drawable.pic6,
R.drawable.pic7,R.drawable.pic8,R.drawable.pic9,R.drawable.pic10};
public int getIcon(){
int i =0;
i = todoList.size();
if(i>=iconList.length && i!=0){
i = iconList.length-1;
}
return iconList[i];
}
@Override
public void onCreate()
{
super.onCreate();
mContext = this;
Log.i(TAG, "onCreate");
createFloatView();
initTimer();
TakePhotoSP=mContext.getSharedPreferences(SPName,0);
screenShotContentObserver = new ScreenShotContentObserver(handler, this) {
@Override
protected void onScreenShot(String path, String fileName) {
if(!doneList.contains(fileName)&&!todoList.contains(fileName)){
todoList.add(fileName);
mFloatView.setBackgroundResource(getIcon());
}
}
};
}
private Timer mTimer;
private TimerTask mTimerTask ;
public void initTimer(){
mTimer = new Timer();
mTimerTask = new TimerTask() {
@Override
public void run() {
if(mTimer == null)
return;
bHandler.sendEmptyMessage(0);
}};
mTimer.schedule(mTimerTask, 500, 3000);
}
private Handler bHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
if(mTimer == null)
return;
try {
switch (msg.what) {
case 0:
if(todoList.isEmpty())
return;
setPicToView();
break;
default:
break;
}
} catch (Exception e) {
}
}
};
private void setPicToView() {
// if (uri != null) {
try {
Bitmap bitmapthumb = decodeBitmap(todoList.get(0));
// imageUri = uri;
// bitmapthumb = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)).copy(Bitmap.Config.ARGB_8888, true);;
bitmapthumb = bitmapthumb.copy(Bitmap.Config.ARGB_8888, true);;
setTextOnBitmap(bitmapthumb);
// img1.setImageBitmap(bitmapthumb);
Uri u = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),
FileUtil.saveBitmap(bitmapthumb, ""+stampTime), null, null));
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, u));
bitmapthumb.recycle();
Log.e("saveBitmap",todoList.get(0));
doneList.add(todoList.get(0));
todoList.remove(0);
mFloatView.setBackgroundResource(getIcon());
} catch (Exception e) {
e.printStackTrace();
}
// }
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long stampTime = 0;
public String timeStamp2Date() {
stampTime = System.currentTimeMillis();
return sdf.format(new Date(stampTime));
}
SharedPreferences TakePhotoSP;
public void setTextOnBitmap(Bitmap bitmap){
Canvas canvasTmp=new Canvas(bitmap);
canvasTmp.drawColor(Color.TRANSPARENT);
Paint pT=new Paint();
pT.setColor(Color.WHITE);
pT.setTextSize(40);
canvasTmp.drawBitmap(bitmap, 0, 0, pT);
Bitmap picc = BitmapFactory.decodeResource(getResources(), R.drawable.picc);
picc = scaleBitmap(picc,0.5f);
int widthL = picc.getWidth();
int heightL = picc.getHeight();
String time = timeStamp2Date();
Rect boundsT = new Rect();
pT.getTextBounds(time, 0, time.length(), boundsT);
int widthT = boundsT.width();
int heightT = boundsT.height();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int labelY = height-heightL-20;
// canvasTmp.drawText(“PICC”,width-widthL-widthT-50,labelY+heightT+10,pL);
canvasTmp.drawBitmap(picc, width-widthL-widthT-50, labelY-heightT, null);
int textX = width-widthT-30;
canvasTmp.drawText(time,textX,labelY,pT);
// int id=TakePhotoSP.getInt(“id”, -1);
String userId = TakePhotoSP.getString(“dingsunerName”, “”);
canvasTmp.drawText(userId,textX,height-heightT-20,pT);
// canvasTmp.drawText(longtitude,20,100,pT);
// canvasTmp.drawText(latitude,20,200,pT);
canvasTmp.save();
canvasTmp.restore();
}
private Bitmap scaleBitmap(Bitmap origin, float ratio) {
if (origin == null) {
return null;
}
int width = origin.getWidth();
int height = origin.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(ratio, ratio);
Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
if (newBM.equals(origin)) {
return newBM;
}
origin.recycle();
return newBM;
}
public BitmapFactory.Options op;
private Bitmap decodeBitmap(String path){
int degree = ImageLoader.getBitmapDegree(path);
int temp =0;
if(op==null)
op = new BitmapFactory.Options();
//inJustDecodeBounds
//If set to true, the decoder will return null (no bitmap), but the out…
op.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(path, op); //获取尺寸信息
//获取比例大小
int wRatio,hRatio;
if(degree == 90 || degree ==270){
wRatio = (int)Math.ceil(op.outWidth/1920);
hRatio = (int)Math.ceil(op.outHeight/1080);
}else {
wRatio = (int)Math.ceil(op.outWidth/1080);
hRatio = (int)Math.ceil(op.outHeight/1920);
}
//如果超出指定大小,则缩小相应的比例
if(wRatio > 1 || hRatio > 1){
if(wRatio > hRatio){
op.inSampleSize = wRatio;
}else{
op.inSampleSize = hRatio;
}
}
op.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(path, op);
if(degree !=0){
bmp = ImageLoader.rotateBitmapByDegree(bmp,degree);
}
return bmp;
}
@SuppressWarnings("static-access")
@SuppressLint("InflateParams") private void createFloatView()
{
wmParams = new WindowManager.LayoutParams();
//通过getApplication获取的是WindowManagerImpl.CompatModeWrapper
mWindowManager = (WindowManager)getApplication().getSystemService(getApplication().WINDOW_SERVICE);
//设置window type
wmParams.type = LayoutParams.TYPE_PHONE;
//设置图片格式,效果为背景透明
wmParams.format = PixelFormat.RGBA_8888;
//设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
//调整悬浮窗显示的停靠位置为左侧置顶
wmParams.gravity = Gravity.LEFT | Gravity.TOP;
// 以屏幕左上角为原点,设置x、y初始值,相对于gravity
wmParams.x = 0;
wmParams.y = 452;
//设置悬浮窗口长宽数据
wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
LayoutInflater inflater = LayoutInflater.from(getApplication());
//获取浮动窗口视图所在布局
mFloatLayout = (LinearLayout) inflater.inflate(R.layout.alert_window_menu, null);
//添加mFloatLayout
mWindowManager.addView(mFloatLayout, wmParams);
//浮动窗口按钮
mFloatView = (ImageButton) mFloatLayout.findViewById(R.id.alert_window_imagebtn);
mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//设置监听浮动窗口的触摸移动
mFloatView.setOnTouchListener(new OnTouchListener()
{
boolean isClick;
@SuppressLint("ClickableViewAccessibility") @Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mFloatView.setBackgroundResource(R.drawable.btn_shutter);
isClick = false;
break;
case MotionEvent.ACTION_MOVE:
isClick = true;
// getRawX是触摸位置相对于屏幕的坐标,getX是相对于按钮的坐标
wmParams.x = (int) event.getRawX()
- mFloatView.getMeasuredWidth() / 2;
// 减25为状态栏的高度
wmParams.y = (int) event.getRawY()
- mFloatView.getMeasuredHeight() / 2 - 75;
// 刷新
mWindowManager.updateViewLayout(mFloatLayout, wmParams);
return true;
case MotionEvent.ACTION_UP:
mFloatView.setBackgroundResource(getIcon());
Log.e("dada","121");
try {
doStartApplicationWithPackageName("com.huawei.camera");
}catch (Exception e){
Intent intent = new Intent(mContext, PicTakeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
return isClick;// 此处返回false则属于移动事件,返回true则释放事件,可以出发点击否。
default:
break;
}
return false;
}
});
mFloatView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
}
});
}
@TargetApi(Build.VERSION_CODES.DONUT)
private void doStartApplicationWithPackageName(final String packagename) {
getContentResolver().registerContentObserver(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
true,
screenShotContentObserver
);
// 通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等
PackageInfo packageinfo = null;
try {
packageinfo = getPackageManager().getPackageInfo(packagename, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (packageinfo == null) {
return;
}
// 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(packageinfo.packageName);
// 通过getPackageManager()的queryIntentActivities方法遍历
List resolveinfoList = getPackageManager()
.queryIntentActivities(resolveIntent, 0);
ResolveInfo resolveinfo = resolveinfoList.iterator().next();
if (resolveinfo != null) {
// packagename = 参数packname
String packageName = resolveinfo.activityInfo.packageName;
// 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式:packagename.mainActivityname]
String className = resolveinfo.activityInfo.name;
// LAUNCHER Intent
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// 设置ComponentName参数1:packagename参数2:MainActivity路径
ComponentName cn = new ComponentName(packageName, className);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(cn);
mContext.startActivity(intent);
}
}
private ScreenShotContentObserver screenShotContentObserver;
private Handler handler = new Handler();
@Override
public void onDestroy()
{
super.onDestroy();
if(mFloatLayout != null)
{
//移除悬浮窗口
mWindowManager.removeView(mFloatLayout);
}
if(mTimer !=null)
mTimer.cancel();
try {
if(screenShotContentObserver != null)
getContentResolver().unregisterContentObserver(screenShotContentObserver);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}