summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/SlideShow.java
blob: 23c7d4aaee31a0b5701f00072a7d405e892f6a36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * 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.android.camera;

import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import android.widget.Gallery.LayoutParams;

import android.view.WindowManager;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import com.android.camera.ImageManager.IGetBitmap_cancelable;
import com.android.camera.ImageManager.IImage;
import com.android.camera.ImageManager.IImageList;

import android.view.MotionEvent;

public class SlideShow extends Activity implements ViewSwitcher.ViewFactory
{
    static final private String TAG = "SlideShow";
    static final int sLag = 2000;
    static final int sNextImageInterval = 3000;
    private ImageManager.IImageList mImageList;
    private int mCurrentPosition = 0;
    private ImageView mSwitcher;
    private boolean mPosted = false;

    @Override
    protected void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        Window wp = getWindow();
        wp.setFlags(FLAG_KEEP_SCREEN_ON, FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.slide_show);

        mSwitcher = (ImageView)findViewById(R.id.imageview);
        if (android.util.Config.LOGV)
            Log.v(TAG, "mSwitcher " + mSwitcher);
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        if (mImageList == null) {
            mImageList = new FileImageList();
            mCurrentPosition = 0;
        }
        loadImage();
    }

    @Override
    protected void onPause() {
        super.onPause();
        cancelPost();
    }

    static public class ImageViewTouch extends ImageView {
        class xy {
            public xy(float xIn, float yIn) {
                x = xIn;
                y = yIn;
                timeAdded = System.currentTimeMillis();
            }
            public xy(MotionEvent e) {
                x = e.getX();
                y = e.getY();
                timeAdded = System.currentTimeMillis();
            }
            float x,y;
            long timeAdded;
        }

        SlideShow mSlideShow;
        Paint mPaints[] = new Paint[1];
        ArrayList<xy> mPoints = new ArrayList<xy>();
        boolean mDown;

        public ImageViewTouch(Context context) {
            super(context);
            mSlideShow = (SlideShow) context;
            setScaleType(ImageView.ScaleType.CENTER);
            setupPaint();
        }

        public ImageViewTouch(Context context, AttributeSet attrs) {
            super(context, attrs);
            mSlideShow = (SlideShow) context;
            setScaleType(ImageView.ScaleType.CENTER);
            setupPaint();
        }

        private void setupPaint() {
            for (int i = 0; i < mPaints.length; i++) {
                Paint p = new Paint();
                p.setARGB(255, 255, 255, 0);
                p.setAntiAlias(true);
                p.setStyle(Paint.Style.FILL);
                p.setStrokeWidth(3F);
                mPaints[i] = p;
            }
        }

        private void addEvent(MotionEvent event) {
            long now = System.currentTimeMillis();
            mPoints.add(new xy(event));
            for (int i = 0; i < event.getHistorySize(); i++)
                mPoints.add(new xy(event.getHistoricalX(i), event.getHistoricalY(i)));
            while (mPoints.size() > 0) {
                xy ev = mPoints.get(0);
                if (now - ev.timeAdded < sLag)
                    break;
                mPoints.remove(0);
            }
        }

        public boolean onTouchEvent(MotionEvent event) {
            addEvent(event);
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mDown = true;
                    mSlideShow.cancelPost();
                    postInvalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    mDown = false;
                    postInvalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    mSlideShow.cancelPost();
                    postInvalidate();
                    break;
            }
            return true;
        }

        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            boolean didPaint = false;
            long now = System.currentTimeMillis();
            for (xy ev: mPoints) {
                Paint p = mPaints[0];
                long delta = now - ev.timeAdded;
                if (delta > sLag)
                    continue;

                int alpha2 = Math.max(0, 255 - (255 * (int)delta / sLag));
                if (alpha2 == 0)
                    continue;
                p.setAlpha(alpha2);
                canvas.drawCircle(ev.x, ev.y, 2, p);
                didPaint = true;
            }
            if (didPaint && !mDown)
                postInvalidate();
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                cancelPost();
                loadPreviousImage();
                return true;

            case KeyEvent.KEYCODE_DPAD_RIGHT:
                cancelPost();
                loadNextImage();
                return true;

            case KeyEvent.KEYCODE_DPAD_CENTER:
                if (mPosted)
                    cancelPost();
                else
                    loadNextImage();
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void cancelPost() {
        mHandler.removeCallbacks(mNextImageRunnable);
        mPosted = false;
    }

    private void post() {
        mHandler.postDelayed(mNextImageRunnable, sNextImageInterval);
        mPosted = true;
    }

    private void loadImage() {
        ImageManager.IImage image = mImageList.getImageAt(mCurrentPosition);
        if (image == null)
            return;

        Bitmap bitmap = image.thumbBitmap();
        if (bitmap == null)
            return;

        mSwitcher.setImageDrawable(new BitmapDrawable(bitmap));
        post();
    }

    private Runnable mNextImageRunnable = new Runnable() {
        public void run() {
            if (android.util.Config.LOGV)
                Log.v(TAG, "mNextImagerunnable called");
            loadNextImage();
        }
    };

    private void loadNextImage() {
        if (++mCurrentPosition >= mImageList.getCount())
            mCurrentPosition = 0;
        loadImage();
    }

    private void loadPreviousImage() {
        if (mCurrentPosition == 0)
            mCurrentPosition = mImageList.getCount() - 1;
        else
            mCurrentPosition -= 1;

        loadImage();
    }

    public View makeView() {
        ImageView i = new ImageView(this);
        i.setBackgroundColor(0xFF000000);
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        return i;
    }

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        }
    };

    class FileImageList implements IImageList {
        public HashMap<String, String> getBucketIds() {
            throw new UnsupportedOperationException();
        }

        public void checkThumbnails(ThumbCheckCallback cb, int totalThumbnails) {
            // TODO Auto-generated method stub

        }

        public void commitChanges() {
            // TODO Auto-generated method stub

        }

        public void removeOnChangeListener(OnChange changeCallback) {
            // TODO Auto-generated method stub

        }

        public void setOnChangeListener(OnChange changeCallback, Handler h) {
            // TODO Auto-generated method stub

        }

        private ArrayList<FileImage> mImages = new ArrayList<FileImage>();
        // image uri ==> Image object
        private HashMap<Long, IImage> mCache = new HashMap<Long, IImage>();

        class FileImage extends ImageManager.SimpleBaseImage {
            long mId;
            String mPath;

            FileImage(long id, String path) {
                mId = id;
                mPath = path;
            }

            public long imageId() {
                return mId;
            }

            public String getDataPath() {
                return mPath;
            }

            public Bitmap fullSizeBitmap(int targetWidthOrHeight) {
                return BitmapFactory.decodeFile(mPath);
            }

            public IGetBitmap_cancelable fullSizeBitmap_cancelable(int targetWidthOrHeight) {
                return null;
            }

            public Bitmap thumbBitmap() {
                Bitmap b = fullSizeBitmap(320);
                Matrix m = new Matrix();
                float scale = 320F / (float) b.getWidth();
                m.setScale(scale, scale);
                Bitmap scaledBitmap = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
                return scaledBitmap;
            }

            public Bitmap miniThumbBitmap() {
                return thumbBitmap();
            }

            public long fullSizeImageId() {
                return mId;
            }
        }

        private void enumerate(String path, ArrayList<String> list) {
            File f = new File(path);
            if (f.isDirectory()) {
                String [] children = f.list();
                if (children != null) {
                    for (int i = 0; i < children.length; i++) {
                        if (children[i].charAt(0) != '.')
                            enumerate(path + "/" + children[i], list);
                    }
                }
            } else {
                if (path.endsWith(".jpeg") || path.endsWith(".jpg") || path.endsWith(".png")) {
                    if (f.length() > 0) {
                        list.add(path);
                    }
                }
            }
        }

        public FileImageList() {
            ArrayList<String> list = new ArrayList<String>();
            enumerate(Environment.getExternalStorageDirectory().getPath(), list);
            enumerate("/data/images", list);

            for (int i = 0; i < list.size(); i++) {
                FileImage img = new FileImage(i, list.get(i));
                mCache.put((long)i, img);
                mImages.add(img);
            }
        }

        public IImage getImageAt(int i) {
            if (i >= mImages.size())
                return null;

            return mImages.get(i);
        }

        public IImage getImageForUri(Uri uri) {
            // TODO make this a hash lookup
            int count = getCount();
            for (int i = 0; i < count; i++) {
                IImage image = getImageAt(i);
                if (image.fullSizeImageUri().equals(uri)) {
                    return image;
                }
            }
            return null;
        }

        public IImage getImageWithId(long id) {
            throw new UnsupportedOperationException();
        }

        public void removeImageAt(int i) {
            throw new UnsupportedOperationException();
        }

        public boolean removeImage(IImage image) {
            throw new UnsupportedOperationException();
        }

        public int getCount() {
            return mImages.size();
        }

        public boolean isEmpty() {
            return mImages.isEmpty();
        }

        public void deactivate() {
            // nothing to do here
        }
    }

}