|
16 | 16 | */ |
17 | 17 | package io.github.ratul.topactivity.utils; |
18 | 18 |
|
| 19 | +import android.annotation.SuppressLint; |
19 | 20 | import android.content.Context; |
| 21 | +import android.content.Intent; |
| 22 | +import android.content.pm.PackageManager; |
20 | 23 | import android.graphics.PixelFormat; |
21 | 24 | import android.os.Build; |
22 | | -import android.view.MotionEvent; |
23 | | -import android.view.View; |
24 | 25 | import android.view.Gravity; |
25 | 26 | import android.view.LayoutInflater; |
| 27 | +import android.view.MotionEvent; |
| 28 | +import android.view.View; |
26 | 29 | import android.view.WindowManager; |
27 | | -import android.content.ClipboardManager; |
28 | | -import android.content.ClipData; |
29 | | -import android.widget.Toast; |
30 | | -import android.widget.LinearLayout; |
31 | | -import android.graphics.Typeface; |
32 | | -import android.content.Intent; |
33 | | -import com.google.android.material.imageview.ShapeableImageView; |
| 30 | +import android.widget.ImageView; |
| 31 | +import android.widget.TextView; |
| 32 | + |
| 33 | +import androidx.annotation.NonNull; |
| 34 | + |
| 35 | +import io.github.ratul.topactivity.App; |
34 | 36 | import io.github.ratul.topactivity.R; |
35 | | -import io.github.ratul.topactivity.model.NotificationMonitor; |
36 | | -import com.google.android.material.textview.MaterialTextView; |
| 37 | +import io.github.ratul.topactivity.receivers.NotificationReceiver; |
| 38 | +import io.github.ratul.topactivity.services.QuickSettingsTileService; |
37 | 39 | import io.github.ratul.topactivity.ui.MainActivity; |
38 | | -import io.github.ratul.topactivity.ui.BackgroundActivity; |
39 | | -import io.github.ratul.topactivity.service.QuickSettingsTileService; |
40 | | -import io.github.ratul.topactivity.service.MonitoringService; |
41 | | -import io.github.ratul.topactivity.service.AccessibilityMonitoringService; |
42 | | -import android.content.pm.PackageInfo; |
43 | | -import android.content.pm.PackageManager; |
44 | | -import io.github.ratul.topactivity.App; |
45 | 40 |
|
46 | 41 | /** |
47 | 42 | * Created by Ratul on 04/05/2022. |
48 | 43 | */ |
49 | 44 | public class WindowUtil { |
50 | | - private static WindowManager.LayoutParams sWindowParams; |
51 | | - public static WindowManager sWindowManager; |
52 | | - private static View sView; |
53 | | - private static int xInitCord = 0; |
54 | | - private static int yInitCord = 0; |
55 | | - private static int xInitMargin = 0; |
56 | | - private static int yInitMargin = 0; |
57 | | - private static String text, text1; |
58 | | - private static MaterialTextView appName, packageName, className; |
59 | | - private static ClipboardManager clipboard; |
60 | | - public static boolean viewAdded = false; |
61 | | - |
62 | | - public static void init(final Context context) { |
63 | | - sWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
64 | | - |
65 | | - sWindowParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, |
66 | | - WindowManager.LayoutParams.WRAP_CONTENT, |
67 | | - Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY |
68 | | - : WindowManager.LayoutParams.TYPE_PHONE, |
69 | | - WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); |
70 | | - |
71 | | - sWindowParams.gravity = Gravity.CENTER; |
72 | | - sWindowParams.width = (DatabaseUtil.getDisplayWidth() / 2) + 300; |
73 | | - sWindowParams.windowAnimations = android.R.style.Animation_Toast; |
74 | | - |
75 | | - sView = LayoutInflater.from(context).inflate(R.layout.window_tasks, null); |
76 | | - clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); |
77 | | - appName = sView.findViewById(R.id.text); |
78 | | - packageName = sView.findViewById(R.id.text1); |
79 | | - className = sView.findViewById(R.id.text2); |
80 | | - ShapeableImageView closeBtn = sView.findViewById(R.id.closeBtn); |
81 | | - |
82 | | - closeBtn.setOnClickListener(new View.OnClickListener() { |
83 | | - public void onClick(View v) { |
84 | | - dismiss(context); |
85 | | - DatabaseUtil.setIsShowWindow(false); |
86 | | - NotificationMonitor.cancelNotification(context); |
87 | | - context.sendBroadcast(new Intent(MainActivity.ACTION_STATE_CHANGED)); |
88 | | - } |
89 | | - }); |
90 | | - |
91 | | - appName.setOnLongClickListener(new View.OnLongClickListener() { |
92 | | - public boolean onLongClick(View v) { |
93 | | - copyString(context, text, "App name copied"); |
94 | | - return true; |
95 | | - } |
96 | | - }); |
97 | | - |
98 | | - packageName.setOnLongClickListener(new View.OnLongClickListener() { |
99 | | - public boolean onLongClick(View v) { |
100 | | - copyString(context, text, "Package name copied"); |
101 | | - return true; |
102 | | - } |
103 | | - }); |
104 | | - |
105 | | - className.setOnLongClickListener(new View.OnLongClickListener() { |
106 | | - public boolean onLongClick(View v) { |
107 | | - copyString(context, text1, "Class name copied"); |
108 | | - return true; |
109 | | - } |
110 | | - }); |
111 | | - |
112 | | - sView.setOnTouchListener(new View.OnTouchListener() { |
113 | | - public boolean onTouch(View view, MotionEvent event) { |
114 | | - WindowManager.LayoutParams layoutParams = sWindowParams; |
115 | | - |
116 | | - int xCord = (int) event.getRawX(); |
117 | | - int yCord = (int) event.getRawY(); |
118 | | - int xCordDestination; |
119 | | - int yCordDestination; |
120 | | - int action = event.getAction(); |
121 | | - |
122 | | - if (action == MotionEvent.ACTION_DOWN) { |
123 | | - xInitCord = xCord; |
124 | | - yInitCord = yCord; |
125 | | - xInitMargin = layoutParams.x; |
126 | | - yInitMargin = layoutParams.y; |
127 | | - } |
128 | | - else if (action == MotionEvent.ACTION_MOVE) { |
129 | | - int xDiffMove = xCord - xInitCord; |
130 | | - int yDiffMove = yCord - yInitCord; |
131 | | - xCordDestination = xInitMargin + xDiffMove; |
132 | | - yCordDestination = yInitMargin + yDiffMove; |
133 | | - |
134 | | - layoutParams.x = xCordDestination; |
135 | | - layoutParams.y = yCordDestination; |
136 | | - sWindowManager.updateViewLayout(view, layoutParams); |
137 | | - } |
138 | | - return true; |
139 | | - } |
140 | | - }); |
141 | | - } |
142 | | - |
143 | | - private static void copyString(Context context, String str, String msg) { |
144 | | - if (Build.VERSION.SDK_INT < 29) { |
145 | | - ClipData clip = ClipData.newPlainText("Current Activity", str); |
146 | | - clipboard.setPrimaryClip(clip); |
147 | | - } else { |
148 | | - context.startActivity( |
149 | | - new Intent(context, BackgroundActivity.class).putExtra(BackgroundActivity.STRING_COPY, str) |
150 | | - .putExtra(BackgroundActivity.COPY_MSG, msg).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); |
151 | | - } |
152 | | - App.showToast(msg, 0); |
153 | | - } |
154 | | - |
155 | | - public static String getAppName(Context context, String pkg) { |
156 | | - try { |
157 | | - PackageManager pm = context.getPackageManager(); |
158 | | - return pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString(); |
159 | | - } catch (Exception e) { |
160 | | - return "Unknown"; |
161 | | - } |
162 | | - } |
163 | | - |
164 | | - public static void show(Context context, String pkg, String clas) { |
165 | | - if (sWindowManager == null) { |
166 | | - init(context); |
167 | | - } |
168 | | - appName.setText(getAppName(context, pkg)); |
169 | | - packageName.setText(pkg); |
170 | | - className.setText(clas); |
171 | | - |
172 | | - if (!viewAdded) { |
173 | | - viewAdded = true; |
174 | | - if (DatabaseUtil.isShowWindow()) { |
175 | | - sWindowManager.addView(sView, sWindowParams); |
176 | | - } |
177 | | - } |
178 | | - |
179 | | - if (NotificationMonitor.builder != null) { |
180 | | - NotificationMonitor.builder.setContentTitle(pkg); |
181 | | - NotificationMonitor.builder.setContentText(clas); |
182 | | - NotificationMonitor.notifManager.notify(NotificationMonitor.NOTIFICATION_ID, |
183 | | - NotificationMonitor.builder.build()); |
184 | | - } |
185 | | - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
186 | | - QuickSettingsTileService.updateTile(context); |
187 | | - } |
188 | | - } |
189 | | - |
190 | | - public static void dismiss(Context context) { |
191 | | - viewAdded = false; |
192 | | - try { |
193 | | - sWindowManager.removeView(sView); |
194 | | - } catch (Exception e) { |
195 | | - e.printStackTrace(); |
196 | | - } |
197 | | - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
198 | | - QuickSettingsTileService.updateTile(context); |
199 | | - } |
200 | | - } |
| 45 | + private static WindowManager.LayoutParams layoutParams; |
| 46 | + private static WindowManager windowManager; |
| 47 | + private static PackageManager packageManager; |
| 48 | + private static View baseView; |
| 49 | + private static int xInitCord = 0; |
| 50 | + private static int yInitCord = 0; |
| 51 | + private static int xInitMargin = 0; |
| 52 | + private static int yInitMargin = 0; |
| 53 | + private static TextView appName, packageName, className; |
| 54 | + |
| 55 | + public static void show( |
| 56 | + @NonNull Context context, @NonNull String pkg, @NonNull String cls) { |
| 57 | + if (windowManager == null || baseView == null) { |
| 58 | + init(context.getApplicationContext()); |
| 59 | + } |
| 60 | + |
| 61 | + if (!isViewVisible()) { |
| 62 | + int userWidth = DatabaseUtil.getUserWidth(); |
| 63 | + if (userWidth != -1) { |
| 64 | + layoutParams.width = userWidth; |
| 65 | + } else { |
| 66 | + double displaySize = Math.min(1100, DatabaseUtil.getDisplayWidth()); |
| 67 | + layoutParams.width = (int) (displaySize * 0.65); |
| 68 | + } |
| 69 | + windowManager.addView(baseView, layoutParams); |
| 70 | + QuickSettingsTileService.updateTile(context); |
| 71 | + } |
| 72 | + |
| 73 | + boolean isPackageChanged = !packageName.getText().toString().equals(pkg); |
| 74 | + boolean isClassChanged = !className.getText().toString().equals(cls); |
| 75 | + |
| 76 | + if (isPackageChanged) { |
| 77 | + appName.setText(getAppName(pkg)); |
| 78 | + packageName.setText(pkg); |
| 79 | + } |
| 80 | + |
| 81 | + if (isClassChanged) { |
| 82 | + className.setText(cls); |
| 83 | + } |
| 84 | + |
| 85 | + if (isPackageChanged || isClassChanged) { |
| 86 | + NotificationReceiver.showNotification(context, pkg, cls); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public static void dismiss(@NonNull Context context) { |
| 91 | + if (windowManager != null) { |
| 92 | + windowManager.removeView(baseView); |
| 93 | + } |
| 94 | + QuickSettingsTileService.updateTile(context); |
| 95 | + } |
| 96 | + |
| 97 | + public static boolean isViewVisible() { |
| 98 | + return baseView != null && baseView.isAttachedToWindow(); |
| 99 | + } |
| 100 | + |
| 101 | + @SuppressLint("ClickableViewAccessibility") |
| 102 | + private static void init(@NonNull Context context) { |
| 103 | + windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
| 104 | + packageManager = context.getPackageManager(); |
| 105 | + |
| 106 | + layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, |
| 107 | + WindowManager.LayoutParams.WRAP_CONTENT, |
| 108 | + Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY |
| 109 | + : WindowManager.LayoutParams.TYPE_PHONE, |
| 110 | + WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); |
| 111 | + |
| 112 | + layoutParams.gravity = Gravity.CENTER; |
| 113 | + layoutParams.windowAnimations = android.R.style.Animation_Toast; |
| 114 | + |
| 115 | + baseView = LayoutInflater.from(context).inflate(R.layout.content_activity_info, null); |
| 116 | + appName = baseView.findViewById(R.id.app_name); |
| 117 | + packageName = baseView.findViewById(R.id.package_name); |
| 118 | + className = baseView.findViewById(R.id.class_name); |
| 119 | + ImageView closeBtn = baseView.findViewById(R.id.closeBtn); |
| 120 | + |
| 121 | + View.OnLongClickListener copyListener = v -> { |
| 122 | + TextView textView = (TextView) v; |
| 123 | + String label = ""; |
| 124 | + |
| 125 | + if (v.getId() == R.id.app_name) label = "App name"; |
| 126 | + else if (v.getId() == R.id.package_name) label = "Package"; |
| 127 | + else if (v.getId() == R.id.class_name) label = "Class"; |
| 128 | + |
| 129 | + App.copyString(context, textView.getText().toString(), label + " copied"); |
| 130 | + return true; |
| 131 | + }; |
| 132 | + |
| 133 | + closeBtn.setOnClickListener(v -> { |
| 134 | + DatabaseUtil.setShowingWindow(false); |
| 135 | + NotificationReceiver.cancelNotification(); |
| 136 | + dismiss(context); |
| 137 | + context.sendBroadcast(new Intent(MainActivity.ACTION_STATE_CHANGED)); |
| 138 | + }); |
| 139 | + |
| 140 | + packageName.setOnLongClickListener(copyListener); |
| 141 | + className.setOnLongClickListener(copyListener); |
| 142 | + |
| 143 | + baseView.setOnTouchListener((view, event) -> { |
| 144 | + int xCord = (int) event.getRawX(); |
| 145 | + int yCord = (int) event.getRawY(); |
| 146 | + |
| 147 | + switch (event.getAction()) { |
| 148 | + case MotionEvent.ACTION_DOWN: |
| 149 | + xInitCord = xCord; |
| 150 | + yInitCord = yCord; |
| 151 | + xInitMargin = layoutParams.x; |
| 152 | + yInitMargin = layoutParams.y; |
| 153 | + return true; |
| 154 | + case MotionEvent.ACTION_MOVE: |
| 155 | + int xDiffMove = xCord - xInitCord; |
| 156 | + int yDiffMove = yCord - yInitCord; |
| 157 | + layoutParams.x = xInitMargin + xDiffMove; |
| 158 | + layoutParams.y = yInitMargin + yDiffMove; |
| 159 | + windowManager.updateViewLayout(view, layoutParams); |
| 160 | + return true; |
| 161 | + } |
| 162 | + return false; |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + private static String getAppName(@NonNull String pkg) { |
| 167 | + try { |
| 168 | + return packageManager.getApplicationLabel( |
| 169 | + packageManager.getApplicationInfo(pkg, 0)).toString(); |
| 170 | + } catch (PackageManager.NameNotFoundException ignored) { |
| 171 | + return "Unknown"; |
| 172 | + } |
| 173 | + } |
201 | 174 | } |
0 commit comments