blob: 4f7e8f2f2c265de9262b53665c6e8636df05cbd6 [file] [log] [blame]
The Android Open Source Project15a88802009-02-10 15:44:05 -08001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher;
18
19import android.content.Context;
20import android.gadget.GadgetHost;
21import android.gadget.GadgetHostView;
22import android.gadget.GadgetInfo;
23import android.graphics.Color;
24import android.view.LayoutInflater;
25import android.view.MotionEvent;
26import android.view.View;
27import android.view.ViewConfiguration;
28import android.widget.TextView;
29
30/**
31 * Specific {@link GadgetHost} that creates our {@link LauncherGadgetHostView} which correctly
32 * captures all long-press events. This ensures that users can always pick up and move gadgets.
33 */
34public class LauncherGadgetHost extends GadgetHost {
35 public LauncherGadgetHost(Context context, int hostId) {
36 super(context, hostId);
37 }
38
39 protected GadgetHostView onCreateView(Context context, int gadgetId, GadgetInfo gadget) {
40 return new LauncherGadgetHostView(context);
41 }
42
43 /**
44 * {@inheritDoc}
45 */
46 public class LauncherGadgetHostView extends GadgetHostView {
47 static final String TAG = "LauncherGadgetHostView";
48
49 private boolean mHasPerformedLongPress;
50
51 private CheckForLongPress mPendingCheckForLongPress;
52
53 private LayoutInflater mInflater;
54
55 public LauncherGadgetHostView(Context context) {
56 super(context);
57
58 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
59
60 // Prepare our default transition animations
61 setAnimateFirstView(true);
62 setInAnimation(context, android.R.anim.fade_in);
63 setOutAnimation(context, android.R.anim.fade_out);
64 }
65
66 @Override
67 protected View getErrorView() {
68 return mInflater.inflate(R.layout.gadget_error, this, false);
69 }
70
71 public boolean onInterceptTouchEvent(MotionEvent ev) {
72
73 // Consume any touch events for ourselves after longpress is triggered
74 if (mHasPerformedLongPress) {
75 mHasPerformedLongPress = false;
76 return true;
77 }
78
79 // Watch for longpress events at this level to make sure
80 // users can always pick up this Gadget
81 switch (ev.getAction()) {
82 case MotionEvent.ACTION_DOWN: {
83 postCheckForLongClick();
84 break;
85 }
86
87 case MotionEvent.ACTION_UP: {
88 mHasPerformedLongPress = false;
89 if (mPendingCheckForLongPress != null) {
90 removeCallbacks(mPendingCheckForLongPress);
91 }
92 break;
93 }
94 }
95
96 // Otherwise continue letting touch events fall through to children
97 return false;
98 }
99
100 class CheckForLongPress implements Runnable {
101 private int mOriginalWindowAttachCount;
102
103 public void run() {
104 if ((mParent != null) && hasWindowFocus()
105 && mOriginalWindowAttachCount == getWindowAttachCount()
106 && !mHasPerformedLongPress) {
107 if (performLongClick()) {
108 mHasPerformedLongPress = true;
109 }
110 }
111 }
112
113 public void rememberWindowAttachCount() {
114 mOriginalWindowAttachCount = getWindowAttachCount();
115 }
116 }
117
118 private void postCheckForLongClick() {
119 mHasPerformedLongPress = false;
120
121 if (mPendingCheckForLongPress == null) {
122 mPendingCheckForLongPress = new CheckForLongPress();
123 }
124 mPendingCheckForLongPress.rememberWindowAttachCount();
125 postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());
126 }
127
128 }
129
130}
131