blob: b8f79025fd1ce26830716d211a3ba8941cd9b1a9 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 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.widget.GridView;
20import android.widget.AdapterView;
21import android.content.Context;
22import android.content.res.TypedArray;
23import android.util.AttributeSet;
24import android.view.View;
25import android.graphics.BitmapFactory;
26import android.graphics.Bitmap;
27import android.graphics.Paint;
28import android.graphics.Canvas;
29
30public class AllAppsGridView extends GridView implements AdapterView.OnItemClickListener,
31 AdapterView.OnItemLongClickListener, DragSource {
32
33 private DragController mDragger;
34 private Launcher mLauncher;
35 private Bitmap mTexture;
36 private Paint mPaint;
37 private int mTextureWidth;
38 private int mTextureHeight;
39
40 public AllAppsGridView(Context context) {
41 super(context);
42 }
43
44 public AllAppsGridView(Context context, AttributeSet attrs) {
45 this(context, attrs, com.android.internal.R.attr.gridViewStyle);
46 }
47
48 public AllAppsGridView(Context context, AttributeSet attrs, int defStyle) {
49 super(context, attrs, defStyle);
50
51 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AllAppsGridView, defStyle, 0);
52 final int textureId = a.getResourceId(R.styleable.AllAppsGridView_texture, 0);
53 if (textureId != 0) {
54 mTexture = BitmapFactory.decodeResource(getResources(), textureId);
55 mTextureWidth = mTexture.getWidth();
56 mTextureHeight = mTexture.getHeight();
57
58 mPaint = new Paint();
59 mPaint.setDither(false);
60 }
61 a.recycle();
62 }
63
64 @Override
65 protected void onFinishInflate() {
66 setOnItemClickListener(this);
67 setOnItemLongClickListener(this);
68 }
69
70 @Override
71 public void draw(Canvas canvas) {
72 final Bitmap texture = mTexture;
73 final Paint paint = mPaint;
74
75 final int width = getWidth();
76 final int height = getHeight();
77
78 final int textureWidth = mTextureWidth;
79 final int textureHeight = mTextureHeight;
80
81 int x = 0;
82 int y;
83
84 while (x < width) {
85 y = 0;
86 while (y < height) {
87 canvas.drawBitmap(texture, x, y, paint);
88 y += textureHeight;
89 }
90 x += textureWidth;
91 }
92
93 super.draw(canvas);
94 }
95
96 public void onItemClick(AdapterView parent, View v, int position, long id) {
97 ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position);
98 mLauncher.startActivitySafely(app.intent);
99 }
100
101 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
102 if (!view.isInTouchMode()) {
103 return false;
104 }
105
106 ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position);
107 app = new ApplicationInfo(app);
108
109 mDragger.startDrag(view, this, app, DragController.DRAG_ACTION_COPY);
110 mLauncher.closeAllApplications();
111
112 return true;
113 }
114
115 public void setDragger(DragController dragger) {
116 mDragger = dragger;
117 }
118
119 public void onDropCompleted(View target, boolean success) {
120 }
121
122 void setLauncher(Launcher launcher) {
123 mLauncher = launcher;
124 }
125}