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 com.android.ex.carousel.CarouselView;
20import com.android.ex.carousel.CarouselViewHelper;
21
22import android.app.Activity;
23import android.app.AlertDialog;
24import android.content.Context;
25import android.content.res.Resources;
26import android.graphics.Bitmap;
27import android.graphics.BitmapFactory;
28import android.graphics.Canvas;
29import android.graphics.Paint;
30import android.graphics.PixelFormat;
31import android.graphics.Rect;
32import android.os.Bundle;
33import android.util.Log;
34
35public class CarouselTestActivity extends Activity {
36    private static final String TAG = "CarouselTestActivity";
37    private static final int CARD_SLOTS = 56;
38    private static final int TOTAL_CARDS = 100;
39    private static final int TEXTURE_HEIGHT = 256;
40    private static final int TEXTURE_WIDTH = 256;
41    private static final int SLOTS_VISIBLE = 7;
42
43    protected static final boolean DBG = false;
44    private static final int DETAIL_TEXTURE_WIDTH = 200;
45    private static final int DETAIL_TEXTURE_HEIGHT = 80;
46    private static final int VISIBLE_DETAIL_COUNT = 3;
47    private static boolean INCREMENTAL_ADD = false; // To debug incrementally adding cards
48    private CarouselView mView;
49    private Paint mPaint = new Paint();
50    private CarouselViewHelper mHelper;
51    private Bitmap mGlossyOverlay;
52    private Bitmap mBorder;
53
54    class LocalCarouselViewHelper extends CarouselViewHelper {
55        private static final int PIXEL_BORDER = 3;
56        private DetailTextureParameters mDetailTextureParameters
57                = new DetailTextureParameters(5.0f, 5.0f, 3.0f, 10.0f);
58
59        LocalCarouselViewHelper(Context context) {
60            super(context);
61        }
62
63        @Override
64        public void onCardSelected(final int id) {
65            postMessage("Selection", "Card " + id + " was selected");
66        }
67
68        @Override
69        public void onDetailSelected(final int id, int x, int y) {
70            postMessage("Selection", "Detail for card " + id + " was selected");
71        }
72
73        @Override
74        public void onCardLongPress(int n, int touchPosition[], Rect detailCoordinates) {
75            postMessage("Selection", "Long press on card " + n);
76        }
77
78        @Override
79        public DetailTextureParameters getDetailTextureParameters(int id) {
80            return mDetailTextureParameters;
81        }
82
83        @Override
84        public Bitmap getTexture(int n) {
85            Bitmap bitmap = Bitmap.createBitmap(TEXTURE_WIDTH, TEXTURE_HEIGHT,
86                    Bitmap.Config.ARGB_8888);
87            Canvas canvas = new Canvas(bitmap);
88            canvas.drawARGB(0, 0, 0, 0);
89            mPaint.setColor(0x40808080);
90            canvas.drawRect(2, 2, TEXTURE_WIDTH-2, TEXTURE_HEIGHT-2, mPaint);
91            mPaint.setTextSize(100.0f);
92            mPaint.setAntiAlias(true);
93            mPaint.setColor(0xffffffff);
94            canvas.drawText("" + n, 2, TEXTURE_HEIGHT-10, mPaint);
95            canvas.drawBitmap(mGlossyOverlay, null,
96                    new Rect(PIXEL_BORDER, PIXEL_BORDER,
97                            TEXTURE_WIDTH - PIXEL_BORDER, TEXTURE_HEIGHT - PIXEL_BORDER), mPaint);
98            return bitmap;
99        }
100
101        @Override
102        public Bitmap getDetailTexture(int n) {
103            Bitmap bitmap = Bitmap.createBitmap(DETAIL_TEXTURE_WIDTH, DETAIL_TEXTURE_HEIGHT,
104                    Bitmap.Config.ARGB_8888);
105            Canvas canvas = new Canvas(bitmap);
106            canvas.drawARGB(32, 10, 10, 10);
107            mPaint.setTextSize(15.0f);
108            mPaint.setAntiAlias(true);
109            canvas.drawText("Detail text for card " + n, 0, DETAIL_TEXTURE_HEIGHT/2, mPaint);
110            return bitmap;
111        }
112    };
113
114    @Override
115    public CharSequence onCreateDescription() {
116        return getText(R.string.carousel_test_activity_description);
117    }
118
119    private Runnable mAddCardRunnable = new Runnable() {
120        public void run() {
121            if (mView.getCardCount() < TOTAL_CARDS) {
122                mView.createCards(mView.getCardCount() + 1);
123                mView.postDelayed(mAddCardRunnable, 2000);
124            }
125        }
126    };
127
128    void postMessage(final CharSequence title, final CharSequence msg) {
129        runOnUiThread(new Runnable() {
130            public void run() {
131                new AlertDialog.Builder(CarouselTestActivity.this)
132                    .setTitle(title)
133                    .setMessage(msg)
134                    .setPositiveButton("OK", null)
135                    .create()
136                    .show();
137            }
138        });
139    }
140
141    @Override
142    protected void onCreate(Bundle savedInstanceState) {
143        super.onCreate(savedInstanceState);
144
145        setContentView(R.layout.carousel_test);
146        mView = (CarouselView) findViewById(R.id.carousel);
147        mView.getHolder().setFormat(PixelFormat.RGBA_8888);
148        mPaint.setColor(0xffffffff);
149        final Resources res = getResources();
150
151        mHelper = new LocalCarouselViewHelper(this);
152        mHelper.setCarouselView(mView);
153        mView.setSlotCount(CARD_SLOTS);
154        mView.createCards(INCREMENTAL_ADD ? 1: TOTAL_CARDS);
155        mView.setVisibleSlots(SLOTS_VISIBLE);
156        mView.setStartAngle((float) -(2.0f*Math.PI * 5 / CARD_SLOTS));
157        mBorder = BitmapFactory.decodeResource(res, R.drawable.border);
158        mView.setDefaultBitmap(mBorder);
159        mView.setLoadingBitmap(mBorder);
160        mView.setBackgroundColor(0.25f, 0.25f, 0.5f, 0.5f);
161        mView.setRezInCardCount(3.0f);
162        mView.setFadeInDuration(250);
163        mView.setVisibleDetails(VISIBLE_DETAIL_COUNT);
164        mView.setDragModel(CarouselView.DRAG_MODEL_PLANE);
165        if (INCREMENTAL_ADD) {
166            mView.postDelayed(mAddCardRunnable, 2000);
167        }
168
169        mGlossyOverlay = BitmapFactory.decodeResource(res, R.drawable.glossy_overlay);
170    }
171
172    @Override
173    protected void onResume() {
174        super.onResume();
175        mHelper.onResume();
176    }
177
178    @Override
179    protected void onPause() {
180        super.onPause();
181        mHelper.onPause();
182    }
183
184}
185