CarouselTestActivity.java revision 3df59346f395434454d310b070fff195089fbaf1
1/*
2 * Copyright (C) 2010 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.carouseltest;
18
19import java.io.File;
20import java.io.FileOutputStream;
21import java.io.IOException;
22import java.io.OutputStream;
23
24import com.android.carouseltest.MyCarouselView;
25import com.android.ex.carousel.CarouselView;
26import com.android.ex.carousel.CarouselRS.CarouselCallback;
27
28import android.app.Activity;
29import android.content.res.Resources;
30import android.graphics.Bitmap;
31import android.graphics.BitmapFactory;
32import android.graphics.Canvas;
33import android.graphics.Paint;
34import android.media.MediaScannerConnection;
35import android.net.Uri;
36import android.os.Bundle;
37import android.os.Environment;
38import android.os.Handler;
39import android.os.HandlerThread;
40import android.os.Message;
41import android.util.Log;
42
43public class CarouselTestActivity extends Activity {
44    private static final int HOLDOFF_DELAY = 0;
45    private static final int CARD_SLOTS = 56;
46    private static final int TOTAL_CARDS = 1000;
47    private static final int TEXTURE_HEIGHT = 511;
48    private static final int TEXTURE_WIDTH = 511;
49    private static final String TAG = "CarouselTestActivity";
50
51    private static final int SLOTS_VISIBLE = 7;
52    protected static final boolean DBG = false;
53    protected static final int SET_TEXTURE_N = 1000;
54    protected static final int SET_DETAIL_TEXTURE_N = 1000;
55    private static final int DETAIL_TEXTURE_WIDTH = 200;
56    private static final int DETAIL_TEXTURE_HEIGHT = 80;
57    private CarouselView mView;
58    private Paint mPaint = new Paint();
59
60    private Handler mTextureHandler;
61    private Handler mDetailTextureHandler;
62    private Handler mGeometryHandler;
63    private Handler mSetTextureHandler;
64    private Handler mSetDetailTextureHandler;
65    private HandlerThread mHandlerThread;
66
67    @Override
68    protected void onCreate(Bundle savedInstanceState) {
69        super.onCreate(savedInstanceState);
70
71        mView = new MyCarouselView(this);
72        mPaint.setColor(0xffffffff);
73        final Resources res = getResources();
74
75        mHandlerThread = new HandlerThread(TAG + ".handler");
76        mHandlerThread.start();
77
78        mTextureHandler = new Handler(mHandlerThread.getLooper()) {
79            @Override
80            public void handleMessage(Message msg) {
81                if (msg.what < TOTAL_CARDS) {
82                    final int id = (Integer) msg.arg1;
83                    final int n = msg.what;
84                    final Bitmap bitmap = createBitmap(id);
85                    mSetTextureHandler.obtainMessage(SET_TEXTURE_N + n, bitmap).sendToTarget();
86                }
87            }
88        };
89
90        mDetailTextureHandler = new Handler(mHandlerThread.getLooper()) {
91            @Override
92            public void handleMessage(Message msg) {
93                if (msg.what < TOTAL_CARDS) {
94                    final int id = (Integer) msg.arg1;
95                    final int n = msg.what;
96                    final Bitmap bitmap = createDetailBitmap(id);
97                    // writeBitmapToFile(bitmap, "detailBitmap" + n + ".png");
98                    mSetDetailTextureHandler.obtainMessage(SET_DETAIL_TEXTURE_N + n, bitmap)
99                            .sendToTarget();
100                }
101            }
102        };
103
104        mGeometryHandler = new Handler(mHandlerThread.getLooper()) {
105            @Override
106            public void handleMessage(Message msg) {
107                // TODO
108            }
109        };
110
111        mSetTextureHandler = new Handler() {
112            @Override
113            public void handleMessage(Message msg) {
114                if (msg.what >= SET_TEXTURE_N) {
115                    final Bitmap bitmap = (Bitmap) msg.obj;
116                    mView.setTextureForItem(msg.what - SET_TEXTURE_N, bitmap);
117                }
118            }
119        };
120
121        mSetDetailTextureHandler = new Handler() {
122            @Override
123            public void handleMessage(Message msg) {
124                if (msg.what >= SET_DETAIL_TEXTURE_N) {
125                    final Bitmap bitmap = (Bitmap) msg.obj;
126                    mView.setDetailTextureForItem(msg.what - SET_TEXTURE_N, 5.0f, 5.0f, bitmap);
127                }
128            }
129        };
130
131        mView.setCallback(mCarouselCallback);
132        mView.setSlotCount(CARD_SLOTS);
133        mView.createCards(TOTAL_CARDS);
134        mView.setVisibleSlots(SLOTS_VISIBLE);
135        mView.setStartAngle((float) -(2.0f*Math.PI * 5 / CARD_SLOTS));
136        mView.setDefaultBitmap(BitmapFactory.decodeResource(res, R.drawable.unknown));
137        mView.setLoadingBitmap(BitmapFactory.decodeResource(res, R.drawable.wait));
138        mView.setBackgroundColor(0.25f, 0.25f, 0.5f, 1.0f);
139
140        /*
141        int flags = WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
142        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
143        mView.getWidth(), mView.getHeight(), WindowManager.LayoutParams.TYPE_APPLICATION,
144            flags, PixelFormat.TRANSLUCENT);
145        mView.setBackgroundColor(0x80000000);
146
147        getWindow().setAttributes(lp);
148        */
149        setContentView(mView);
150    }
151
152    @Override
153    protected void onResume() {
154        super.onResume();
155        mView.onResume();
156    }
157
158    @Override
159    protected void onPause() {
160        super.onPause();
161        mView.onPause();
162    }
163
164    Bitmap createBitmap(int n) {
165        Bitmap bitmap = Bitmap.createBitmap(TEXTURE_WIDTH, TEXTURE_HEIGHT,
166                Bitmap.Config.RGB_565);
167        Canvas canvas = new Canvas(bitmap);
168        canvas.drawARGB(255, 128, 128, 128);
169        mPaint.setTextSize(100.0f);
170        mPaint.setAntiAlias(true);
171        canvas.drawText(""+n, 0, TEXTURE_HEIGHT-10, mPaint);
172        return bitmap;
173    }
174
175    Bitmap createDetailBitmap(int n) {
176        Bitmap bitmap = Bitmap.createBitmap(DETAIL_TEXTURE_WIDTH, DETAIL_TEXTURE_HEIGHT,
177                Bitmap.Config.ARGB_8888);
178        Canvas canvas = new Canvas(bitmap);
179        canvas.drawARGB(200, 200, 200, 255);
180        mPaint.setTextSize(15.0f);
181        mPaint.setAntiAlias(true);
182        canvas.drawText("Detail text for card " + n, 0, DETAIL_TEXTURE_HEIGHT/2, mPaint);
183        return bitmap;
184    }
185
186    void writeBitmapToFile(Bitmap bitmap, String fname) {
187        File path = Environment.getExternalStoragePublicDirectory(
188                Environment.DIRECTORY_PICTURES);
189        File file = new File(path, fname);
190
191        try {
192            path.mkdirs();
193            OutputStream os = new FileOutputStream(file);
194            MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null,
195                    new MediaScannerConnection.OnScanCompletedListener() {
196
197                public void onScanCompleted(String path, Uri uri) {
198
199                }
200            });
201            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
202        } catch (IOException e) {
203            Log.w("ExternalStorage", "Error writing " + file, e);
204        }
205    }
206
207    private CarouselCallback mCarouselCallback = new CarouselCallback() {
208
209        public void onRequestTexture(int n) {
210            if (DBG) Log.v(TAG, "onRequestTexture(" + n + ")" );
211            mTextureHandler.removeMessages(n);
212            Message message = mTextureHandler.obtainMessage(n, n, 0);
213            mTextureHandler.sendMessageDelayed(message, HOLDOFF_DELAY);
214        }
215
216        public void onInvalidateTexture(final int n) {
217            if (DBG) Log.v(TAG, "onInvalidateTexture(" + n + ")");
218            mTextureHandler.removeMessages(n);
219        }
220
221        public void onRequestGeometry(int n) {
222            if (DBG) Log.v(TAG, "onRequestGeometry(" + n + ")");
223            mGeometryHandler.removeMessages(n);
224            mGeometryHandler.sendMessage(mGeometryHandler.obtainMessage(n));
225        }
226
227        public void onRequestDetailTexture(int n) {
228            if (DBG) Log.v(TAG, "onRequestDetailTexture(" + n + ")" );
229            mDetailTextureHandler.removeMessages(n);
230            Message message = mDetailTextureHandler.obtainMessage(n, n, 0);
231            mDetailTextureHandler.sendMessageDelayed(message, HOLDOFF_DELAY);
232        }
233
234        public void onInvalidateDetailTexture(int n) {
235            if (DBG) Log.v(TAG, "onInvalidateDetailTexture(" + n + ")");
236            mDetailTextureHandler.removeMessages(n);
237        }
238
239        public void onInvalidateGeometry(int n) {
240            if (DBG) Log.v(TAG, "onInvalidateGeometry(" + n + ")");
241            mGeometryHandler.removeMessages(n);
242        }
243
244        public void onCardSelected(int n) {
245            if (DBG) Log.v(TAG, "onCardSelected(" + n + ")");
246        }
247
248        public void onAnimationStarted() {
249
250        }
251
252        public void onAnimationFinished() {
253
254        }
255
256        public void onReportFirstCardPosition(int n) {
257
258        }
259    };
260
261}
262