OSDN Git Service

Fix dashboard long-press
authorBraden Farmer <farmerbb@gmail.com>
Mon, 19 Dec 2016 22:13:05 +0000 (15:13 -0700)
committerBraden Farmer <farmerbb@gmail.com>
Mon, 19 Dec 2016 22:13:05 +0000 (15:13 -0700)
app/src/main/java/com/farmerbb/taskbar/service/DashboardService.java
app/src/main/java/com/farmerbb/taskbar/widget/DashboardCell.java [new file with mode: 0644]
app/src/main/res/layout/dashboard.xml

index 6841c53..77a65a5 100644 (file)
@@ -52,6 +52,7 @@ import android.widget.TextView;
 import com.farmerbb.taskbar.R;
 import com.farmerbb.taskbar.activity.DashboardActivity;
 import com.farmerbb.taskbar.activity.DashboardActivityDark;
+import com.farmerbb.taskbar.widget.DashboardCell;
 import com.farmerbb.taskbar.util.FreeformHackHelper;
 import com.farmerbb.taskbar.util.LauncherHelper;
 import com.farmerbb.taskbar.util.U;
@@ -64,7 +65,7 @@ public class DashboardService extends Service {
     private WindowManager windowManager;
     private LinearLayout layout;
 
-    private SparseArray<FrameLayout> cells = new SparseArray<>();
+    private SparseArray<DashboardCell> cells = new SparseArray<>();
 
     private final int APPWIDGET_HOST_ID = 123;
 
@@ -120,6 +121,13 @@ public class DashboardService extends Service {
         }
     };
 
+    private DashboardCell.OnInterceptedLongPressListener listener = new DashboardCell.OnInterceptedLongPressListener() {
+        @Override
+        public void onInterceptedLongPress(DashboardCell cell) {
+            cellLongClick(cell);
+        }
+    };
+
     private BroadcastReceiver toggleReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
@@ -138,7 +146,7 @@ public class DashboardService extends Service {
 
                 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
 
-                FrameLayout cellLayout = cells.get(cellId);
+                DashboardCell cellLayout = cells.get(cellId);
                 AppWidgetHostView hostView = mAppWidgetHost.createView(DashboardService.this, appWidgetId, appWidgetInfo);
                 hostView.setAppWidget(appWidgetId, appWidgetInfo);
                 hostView.updateAppWidgetSize(null, cellLayout.getWidth(), cellLayout.getHeight(), cellLayout.getWidth(), cellLayout.getHeight());
@@ -150,6 +158,7 @@ public class DashboardService extends Service {
                 cellLayout.findViewById(R.id.empty).setVisibility(View.GONE);
                 cellLayout.setOnLongClickListener(olcl);
                 cellLayout.setOnGenericMotionListener(ogml);
+                cellLayout.setOnInterceptedLongPressListener(listener);
 
                 LinearLayout linearLayout = (LinearLayout) cellLayout.findViewById(R.id.dashboard);
                 linearLayout.addView(hostView);
@@ -169,7 +178,7 @@ public class DashboardService extends Service {
             if(intent.hasExtra("cellId")) {
                 int cellId = intent.getExtras().getInt("cellId", -1);
 
-                FrameLayout cellLayout = cells.get(cellId);
+                DashboardCell cellLayout = cells.get(cellId);
                 Bundle bundle = (Bundle) cellLayout.getTag();
 
                 mAppWidgetHost.deleteAppWidgetId(bundle.getInt("appWidgetId"));
@@ -183,6 +192,7 @@ public class DashboardService extends Service {
                 cellLayout.setOnHoverListener(cellOhl);
                 cellLayout.setOnLongClickListener(null);
                 cellLayout.setOnGenericMotionListener(null);
+                cellLayout.setOnInterceptedLongPressListener(null);
             }
         }
     };
@@ -291,7 +301,7 @@ public class DashboardService extends Service {
             layout2.setOrientation(LinearLayout.VERTICAL);
 
             for(int j = 0; j < rows; j++) {
-                FrameLayout cellLayout = (FrameLayout) View.inflate(this, R.layout.dashboard, null);
+                DashboardCell cellLayout = (DashboardCell) View.inflate(this, R.layout.dashboard, null);
                 cellLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1));
                 cellLayout.setBackgroundColor(backgroundTint);
                 cellLayout.setOnClickListener(cellOcl);
diff --git a/app/src/main/java/com/farmerbb/taskbar/widget/DashboardCell.java b/app/src/main/java/com/farmerbb/taskbar/widget/DashboardCell.java
new file mode 100644 (file)
index 0000000..e48b347
--- /dev/null
@@ -0,0 +1,77 @@
+/* Copyright 2016 Braden Farmer
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.farmerbb.taskbar.widget;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.view.MotionEvent;
+import android.view.View;
+import android.view.ViewConfiguration;
+import android.widget.FrameLayout;
+
+public class DashboardCell extends FrameLayout {
+
+    public DashboardCell(Context context) {
+        super(context);
+    }
+
+    public DashboardCell(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public DashboardCell(Context context, AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+    }
+
+    public interface OnInterceptedLongPressListener {
+        void onInterceptedLongPress(DashboardCell cell);
+    }
+
+    // Use this instance of the interface to deliver action events
+    private OnInterceptedLongPressListener listener;
+
+    long beginTime;
+
+    @Override
+    public boolean onInterceptTouchEvent(MotionEvent ev) {
+        switch(ev.getAction()) {
+            case MotionEvent.ACTION_DOWN: {
+                beginTime = ev.getEventTime();
+                break;
+            }
+
+            case MotionEvent.ACTION_UP:
+            case MotionEvent.ACTION_CANCEL:
+                long eventLength = ev.getEventTime() - beginTime;
+
+                if(eventLength > ViewConfiguration.getLongPressTimeout()) {
+                    if(listener != null)
+                        listener.onInterceptedLongPress(this);
+
+                    cancelLongPress();
+                    return true;
+                }
+
+                break;
+        }
+
+        return false;
+    }
+
+    public void setOnInterceptedLongPressListener(OnInterceptedLongPressListener listener) {
+        this.listener = listener;
+    }
+}
\ No newline at end of file
index 62e27e6..af5fa2f 100644 (file)
@@ -14,7 +14,8 @@
      limitations under the License.
 -->
 
-<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<com.farmerbb.taskbar.widget.DashboardCell
+    xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >
 
@@ -38,4 +39,4 @@
         android:text="@string/click_to_add_widget"
         android:textSize="24sp" />
 
-</FrameLayout>
+</com.farmerbb.taskbar.widget.DashboardCell>