|
|
|
|
@ -28,14 +28,18 @@ import net.micode.notes.R;
|
|
|
|
|
import net.micode.notes.model.Note;
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
|
|
|
|
|
import android.view.GestureDetector;
|
|
|
|
|
|
|
|
|
|
public class CapsuleService extends Service {
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "CapsuleService";
|
|
|
|
|
private WindowManager mWindowManager;
|
|
|
|
|
private View mCollapsedView;
|
|
|
|
|
private View mExpandedView;
|
|
|
|
|
private EditText mEtContent;
|
|
|
|
|
private WindowManager.LayoutParams mCollapsedParams;
|
|
|
|
|
private WindowManager.LayoutParams mExpandedParams;
|
|
|
|
|
private GestureDetector mGestureDetector;
|
|
|
|
|
|
|
|
|
|
private Handler mHandler = new Handler();
|
|
|
|
|
public static String currentSourcePackage = "";
|
|
|
|
|
@ -62,6 +66,11 @@ public class CapsuleService extends Service {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
|
return START_STICKY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreate() {
|
|
|
|
|
super.onCreate();
|
|
|
|
|
@ -98,14 +107,18 @@ public class CapsuleService extends Service {
|
|
|
|
|
mExpandedView = LayoutInflater.from(this).inflate(R.layout.layout_capsule_expanded, null);
|
|
|
|
|
|
|
|
|
|
mExpandedParams = new WindowManager.LayoutParams(
|
|
|
|
|
dp2px(300),
|
|
|
|
|
dp2px(320),
|
|
|
|
|
dp2px(400),
|
|
|
|
|
layoutFlag,
|
|
|
|
|
WindowManager.LayoutParams.FLAG_DIM_BEHIND, // Allow focus for EditText
|
|
|
|
|
WindowManager.LayoutParams.FLAG_DIM_BEHIND | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
|
|
|
|
|
PixelFormat.TRANSLUCENT);
|
|
|
|
|
mExpandedParams.dimAmount = 0.5f;
|
|
|
|
|
mExpandedParams.gravity = Gravity.CENTER;
|
|
|
|
|
|
|
|
|
|
// Setup Fields
|
|
|
|
|
mCollapsedView.setClickable(true);
|
|
|
|
|
mEtContent = mExpandedView.findViewById(R.id.et_content);
|
|
|
|
|
|
|
|
|
|
// Setup Listeners
|
|
|
|
|
setupCollapsedListener();
|
|
|
|
|
setupExpandedListener();
|
|
|
|
|
@ -120,6 +133,15 @@ public class CapsuleService extends Service {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setupCollapsedListener() {
|
|
|
|
|
mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onSingleTapConfirmed(MotionEvent e) {
|
|
|
|
|
Log.d(TAG, "onSingleTapConfirmed: Triggering showExpandedView");
|
|
|
|
|
showExpandedView();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mCollapsedView.setOnTouchListener(new View.OnTouchListener() {
|
|
|
|
|
private int initialX;
|
|
|
|
|
private int initialY;
|
|
|
|
|
@ -128,28 +150,33 @@ public class CapsuleService extends Service {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onTouch(View v, MotionEvent event) {
|
|
|
|
|
// Let GestureDetector handle taps
|
|
|
|
|
if (mGestureDetector.onTouchEvent(event)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (event.getAction()) {
|
|
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
|
|
v.setPressed(true);
|
|
|
|
|
initialX = mCollapsedParams.x;
|
|
|
|
|
initialY = mCollapsedParams.y;
|
|
|
|
|
initialTouchX = event.getRawX();
|
|
|
|
|
initialTouchY = event.getRawY();
|
|
|
|
|
Log.d(TAG, "onTouch: ACTION_DOWN at " + initialTouchX + ", " + initialTouchY);
|
|
|
|
|
return true;
|
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
|
case MotionEvent.ACTION_MOVE:
|
|
|
|
|
int Xdiff = (int) (event.getRawX() - initialTouchX);
|
|
|
|
|
int Ydiff = (int) (event.getRawY() - initialTouchY);
|
|
|
|
|
Log.d(TAG, "onTouch: ACTION_UP, diff: " + Xdiff + ", " + Ydiff);
|
|
|
|
|
// If click (small movement)
|
|
|
|
|
if (Math.abs(Xdiff) < 10 && Math.abs(Ydiff) < 10) {
|
|
|
|
|
Log.d(TAG, "onTouch: Click detected, showing expanded view");
|
|
|
|
|
showExpandedView();
|
|
|
|
|
|
|
|
|
|
// Move if dragged
|
|
|
|
|
if (Math.abs(Xdiff) > 10 || Math.abs(Ydiff) > 10) {
|
|
|
|
|
mCollapsedParams.x = initialX + Xdiff;
|
|
|
|
|
mCollapsedParams.y = initialY + Ydiff;
|
|
|
|
|
mWindowManager.updateViewLayout(mCollapsedView, mCollapsedParams);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
case MotionEvent.ACTION_MOVE:
|
|
|
|
|
mCollapsedParams.x = initialX + (int) (event.getRawX() - initialTouchX);
|
|
|
|
|
mCollapsedParams.y = initialY + (int) (event.getRawY() - initialTouchY);
|
|
|
|
|
mWindowManager.updateViewLayout(mCollapsedView, mCollapsedParams);
|
|
|
|
|
case MotionEvent.ACTION_CANCEL:
|
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
|
v.setPressed(false);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
@ -195,44 +222,73 @@ public class CapsuleService extends Service {
|
|
|
|
|
private void setupExpandedListener() {
|
|
|
|
|
Button btnCancel = mExpandedView.findViewById(R.id.btn_cancel);
|
|
|
|
|
Button btnSave = mExpandedView.findViewById(R.id.btn_save);
|
|
|
|
|
EditText etContent = mExpandedView.findViewById(R.id.et_content);
|
|
|
|
|
|
|
|
|
|
btnCancel.setOnClickListener(v -> showCollapsedView());
|
|
|
|
|
|
|
|
|
|
btnSave.setOnClickListener(v -> {
|
|
|
|
|
String content = etContent.getText().toString();
|
|
|
|
|
String content = mEtContent.getText().toString();
|
|
|
|
|
if (!content.isEmpty()) {
|
|
|
|
|
saveNote(content);
|
|
|
|
|
etContent.setText("");
|
|
|
|
|
mEtContent.setText("");
|
|
|
|
|
showCollapsedView();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showExpandedView() {
|
|
|
|
|
if (mCollapsedView.getParent() != null) {
|
|
|
|
|
mWindowManager.removeView(mCollapsedView);
|
|
|
|
|
}
|
|
|
|
|
if (mExpandedView.getParent() == null) {
|
|
|
|
|
mWindowManager.addView(mExpandedView, mExpandedParams);
|
|
|
|
|
|
|
|
|
|
TextView tvSource = mExpandedView.findViewById(R.id.tv_source);
|
|
|
|
|
if (currentSourcePackage != null && !currentSourcePackage.isEmpty()) {
|
|
|
|
|
tvSource.setText("Source: " + currentSourcePackage);
|
|
|
|
|
tvSource.setVisibility(View.VISIBLE);
|
|
|
|
|
} else {
|
|
|
|
|
tvSource.setVisibility(View.GONE);
|
|
|
|
|
mHandler.post(() -> {
|
|
|
|
|
try {
|
|
|
|
|
Log.d(TAG, "showExpandedView: Attempting to show expanded view");
|
|
|
|
|
if (mCollapsedView != null && mCollapsedView.getParent() != null) {
|
|
|
|
|
Log.d(TAG, "showExpandedView: Removing collapsed view");
|
|
|
|
|
mWindowManager.removeViewImmediate(mCollapsedView);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mExpandedView != null && mExpandedView.getParent() == null) {
|
|
|
|
|
Log.d(TAG, "showExpandedView: Adding expanded view");
|
|
|
|
|
mWindowManager.addView(mExpandedView, mExpandedParams);
|
|
|
|
|
|
|
|
|
|
TextView tvSource = mExpandedView.findViewById(R.id.tv_source);
|
|
|
|
|
if (currentSourcePackage != null && !currentSourcePackage.isEmpty()) {
|
|
|
|
|
tvSource.setText("Source: " + currentSourcePackage);
|
|
|
|
|
tvSource.setVisibility(View.VISIBLE);
|
|
|
|
|
} else {
|
|
|
|
|
tvSource.setVisibility(View.GONE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mEtContent != null) {
|
|
|
|
|
mEtContent.requestFocus();
|
|
|
|
|
}
|
|
|
|
|
Log.d(TAG, "showExpandedView: Expanded view added successfully");
|
|
|
|
|
} else {
|
|
|
|
|
Log.w(TAG, "showExpandedView: Expanded view already has a parent or is null");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "showExpandedView: Error", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showCollapsedView() {
|
|
|
|
|
if (mExpandedView.getParent() != null) {
|
|
|
|
|
mWindowManager.removeView(mExpandedView);
|
|
|
|
|
}
|
|
|
|
|
if (mCollapsedView.getParent() == null) {
|
|
|
|
|
mWindowManager.addView(mCollapsedView, mCollapsedParams);
|
|
|
|
|
}
|
|
|
|
|
mHandler.post(() -> {
|
|
|
|
|
try {
|
|
|
|
|
Log.d(TAG, "showCollapsedView: Attempting to show collapsed view");
|
|
|
|
|
if (mExpandedView != null && mExpandedView.getParent() != null) {
|
|
|
|
|
Log.d(TAG, "showCollapsedView: Removing expanded view");
|
|
|
|
|
mWindowManager.removeViewImmediate(mExpandedView);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mCollapsedView != null && mCollapsedView.getParent() == null) {
|
|
|
|
|
Log.d(TAG, "showCollapsedView: Adding collapsed view");
|
|
|
|
|
mWindowManager.addView(mCollapsedView, mCollapsedParams);
|
|
|
|
|
Log.d(TAG, "showCollapsedView: Collapsed view added successfully");
|
|
|
|
|
} else {
|
|
|
|
|
Log.w(TAG, "showCollapsedView: Collapsed view already has a parent or is null");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "showCollapsedView: Error", e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void saveNote(String content) {
|
|
|
|
|
@ -243,7 +299,6 @@ public class CapsuleService extends Service {
|
|
|
|
|
|
|
|
|
|
// 2. Create Note object
|
|
|
|
|
Note note = new Note();
|
|
|
|
|
note.setNoteValue(Notes.NoteColumns.ID, String.valueOf(noteId));
|
|
|
|
|
note.setTextData(Notes.DataColumns.CONTENT, content);
|
|
|
|
|
|
|
|
|
|
// Generate Summary (First 20 chars or first line)
|
|
|
|
|
|