|
| 1 | +package com.example.genieapiservice; |
| 2 | + |
| 3 | +import android.animation.ValueAnimator; |
| 4 | +import android.app.Service; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.Intent; |
| 7 | +import android.content.SharedPreferences; |
| 8 | +import android.content.res.Configuration; |
| 9 | +import android.os.Handler; |
| 10 | +import android.os.IBinder; |
| 11 | +import android.graphics.PixelFormat; |
| 12 | +import android.os.Message; |
| 13 | +import android.view.Gravity; |
| 14 | +import android.view.LayoutInflater; |
| 15 | +import android.view.MotionEvent; |
| 16 | +import android.view.View; |
| 17 | +import android.view.WindowManager; |
| 18 | +import android.view.animation.Animation; |
| 19 | +import android.view.animation.AnimationUtils; |
| 20 | +import android.widget.ImageView; |
| 21 | + |
| 22 | +import androidx.annotation.NonNull; |
| 23 | + |
| 24 | + |
| 25 | +public class FloatingService extends Service { |
| 26 | + |
| 27 | + private final String TAG = "FloatingService"; |
| 28 | + private WindowManager windowManager; |
| 29 | + private ImageView floatingView; |
| 30 | + private final long MaxClickTime = 200; |
| 31 | + private long startTouchTime; |
| 32 | + private int screenWidth; |
| 33 | + private WindowManager.LayoutParams layoutParams; |
| 34 | + private boolean isHidden = false; |
| 35 | + private boolean isMoving; |
| 36 | + private boolean isAligning; |
| 37 | + private Handler mHandler = new Handler() { |
| 38 | + @Override |
| 39 | + public void handleMessage(@NonNull Message msg) { |
| 40 | + super.handleMessage(msg); |
| 41 | + switch (msg.what) { |
| 42 | + case 0: |
| 43 | + isHidden = true; |
| 44 | + floatingView.animate().translationX(-floatingView.getWidth() * 4f / 5f).setDuration(300).start(); |
| 45 | + break; |
| 46 | + case 1: |
| 47 | + isHidden = true; |
| 48 | + floatingView.animate().translationX(floatingView.getWidth() * 4f / 5f).setDuration(300).start(); |
| 49 | + break; |
| 50 | + case 3: |
| 51 | + isHidden = false; |
| 52 | + floatingView.animate().translationX(0).setDuration(300).start(); |
| 53 | + break; |
| 54 | + default: |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + @Override |
| 61 | + public void onConfigurationChanged(Configuration newConfig) { |
| 62 | + super.onConfigurationChanged(newConfig); |
| 63 | + screenWidth = this.getResources().getDisplayMetrics().widthPixels; |
| 64 | + autoAlignAndHide(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void onCreate() { |
| 69 | + super.onCreate(); |
| 70 | + SharedPreferences sharedPref = this.getSharedPreferences("position",Context.MODE_PRIVATE); |
| 71 | + screenWidth = this.getResources().getDisplayMetrics().widthPixels; |
| 72 | + windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); |
| 73 | + floatingView = (ImageView) LayoutInflater.from(this).inflate(R.layout.layout_floating, null); |
| 74 | + layoutParams = new WindowManager.LayoutParams( |
| 75 | + 120, |
| 76 | + 120, |
| 77 | + WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, |
| 78 | + WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, |
| 79 | + PixelFormat.TRANSLUCENT |
| 80 | + ); |
| 81 | + layoutParams.gravity = Gravity.TOP | Gravity.START; |
| 82 | + layoutParams.x = sharedPref.getInt("x",0); |
| 83 | + layoutParams.y = sharedPref.getInt("y",100); |
| 84 | + windowManager.addView(floatingView, layoutParams); |
| 85 | + if (layoutParams.x == 0) { |
| 86 | + mHandler.sendEmptyMessageDelayed(0,2000); |
| 87 | + } else { |
| 88 | + mHandler.sendEmptyMessageDelayed(1,2000); |
| 89 | + } |
| 90 | + floatingView.setOnTouchListener(new View.OnTouchListener() { |
| 91 | + private float initialTouchX,initialTouchY; |
| 92 | + @Override |
| 93 | + public boolean onTouch(View v, MotionEvent event) { |
| 94 | + switch (event.getAction()){ |
| 95 | + case MotionEvent.ACTION_DOWN: |
| 96 | + isMoving = false; |
| 97 | + startTouchTime = System.currentTimeMillis(); |
| 98 | + initialTouchX = event.getRawX(); |
| 99 | + initialTouchY = event.getRawY(); |
| 100 | + return true; |
| 101 | + case MotionEvent.ACTION_MOVE: |
| 102 | + isMoving = true; |
| 103 | + if (mHandler.hasMessages(0)) { |
| 104 | + mHandler.removeMessages(0); |
| 105 | + } |
| 106 | + if (mHandler.hasMessages(1)) { |
| 107 | + mHandler.removeMessages(1); |
| 108 | + } |
| 109 | + if (isHidden) { |
| 110 | + restoreIcon(); |
| 111 | + } |
| 112 | + int detailX = (int) (event.getRawX()-initialTouchX); |
| 113 | + int detailY = (int) (event.getRawY()-initialTouchY); |
| 114 | + layoutParams.x +=detailX; |
| 115 | + layoutParams.y +=detailY; |
| 116 | + windowManager.updateViewLayout(floatingView,layoutParams); |
| 117 | + initialTouchX = event.getRawX(); |
| 118 | + initialTouchY = event.getRawY(); |
| 119 | + return true; |
| 120 | + case MotionEvent.ACTION_UP: |
| 121 | + isMoving = false; |
| 122 | + long touchTime = System.currentTimeMillis()-startTouchTime; |
| 123 | + if(touchTime < MaxClickTime){ |
| 124 | + v.performClick(); |
| 125 | + } else { |
| 126 | + autoAlignAndHide(); |
| 127 | + } |
| 128 | + } |
| 129 | + return false; |
| 130 | + } |
| 131 | + }); |
| 132 | + floatingView.setOnClickListener(v -> { |
| 133 | + if (isHidden) { |
| 134 | + restore(); |
| 135 | + } else { |
| 136 | + if (!isMoving && !isAligning) { |
| 137 | + Intent intent = new Intent(FloatingService.this, MainActivity.class); |
| 138 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 139 | + startActivity(intent); |
| 140 | + } |
| 141 | + } |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + private void autoAlignAndHide() { |
| 146 | + int targetX; |
| 147 | + if (layoutParams.x < screenWidth / 2) { |
| 148 | + // align left → move left |
| 149 | + targetX = 0; |
| 150 | + animateWindow(layoutParams.x, targetX); |
| 151 | + } else { |
| 152 | + // align right → move right |
| 153 | + targetX = screenWidth - layoutParams.width; |
| 154 | + animateWindow(layoutParams.x, targetX); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private void restore() { |
| 159 | + int targetX; |
| 160 | + if (layoutParams.x < screenWidth / 2) { |
| 161 | + targetX = 0; |
| 162 | + } else { |
| 163 | + targetX = screenWidth - layoutParams.width; |
| 164 | + } |
| 165 | + animateWindow(layoutParams.x, targetX); |
| 166 | + mHandler.sendEmptyMessage(3); |
| 167 | + } |
| 168 | + |
| 169 | + private void restoreIcon() { |
| 170 | + //floatingView.animate().translationX(0).setDuration(1).start(); |
| 171 | + restore(); |
| 172 | + } |
| 173 | + |
| 174 | + private void animateWindow(int fromX, int toX) { |
| 175 | + ValueAnimator animator = ValueAnimator.ofInt(fromX, toX); |
| 176 | + animator.setDuration(300); |
| 177 | + animator.addUpdateListener(animation -> { |
| 178 | + layoutParams.x = (int) animation.getAnimatedValue(); |
| 179 | + if (floatingView.isAttachedToWindow()) { |
| 180 | + windowManager.updateViewLayout(floatingView, layoutParams); |
| 181 | + } |
| 182 | + if (layoutParams.x == toX) { |
| 183 | + if (layoutParams.x == 0) { |
| 184 | + mHandler.sendEmptyMessageDelayed(0, 2000); |
| 185 | + } else { |
| 186 | + mHandler.sendEmptyMessageDelayed(1, 2000); |
| 187 | + } |
| 188 | + isAligning = false; |
| 189 | + } |
| 190 | + }); |
| 191 | + isAligning = true; |
| 192 | + animator.start(); |
| 193 | + } |
| 194 | + |
| 195 | + |
| 196 | + @Override |
| 197 | + public IBinder onBind(Intent intent) { |
| 198 | + return null; |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public void onDestroy() { |
| 203 | + super.onDestroy(); |
| 204 | + try { |
| 205 | + SharedPreferences sharedPref = this.getSharedPreferences("position",Context.MODE_PRIVATE); |
| 206 | + SharedPreferences.Editor editor = sharedPref.edit(); |
| 207 | + editor.putInt("x", layoutParams.x); |
| 208 | + editor.putInt("y", layoutParams.y); |
| 209 | + editor.apply(); |
| 210 | + windowManager.removeView(floatingView); |
| 211 | + stopSelf(); |
| 212 | + } catch (Exception exception) { |
| 213 | + exception.printStackTrace(); |
| 214 | + } |
| 215 | + LogUtils.logDebug(TAG,"call onDestroy",LogUtils.LOG_DEBUG); |
| 216 | + } |
| 217 | +} |
0 commit comments