summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/BestpictureActivity.java
blob: bb519e74f9167e9484ccf0a65f3b0223ed05aa88 (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
/*
 * Copyright (c) 2016, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of The Linux Foundation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.android.camera;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Point;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.android.camera.exif.ExifInterface;
import com.android.camera.ui.DotsView;
import com.android.camera.ui.DotsViewItem;
import com.android.camera.ui.RotateTextToast;
import com.android.camera.util.CameraUtil;

import org.codeaurora.snapcam.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

public class BestpictureActivity extends FragmentActivity{
    private static final String TAG = "BestpictureActivity";
    public static final String[] NAMES = {
        "00", "01", "02", "03", "04", "05", "06", "07", "08", "09"
    };

    public static final int NUM_IMAGES = 10;

    private ViewPager mImagePager;
    private PagerAdapter mImagePagerAdapter;
    private int mWidth;
    private int mHeight;
    private String mFilesPath;
    private ProgressDialog mProgressDialog;
    private BestpictureActivity mActivity;
    private DotsView mDotsView;
    private ImageItems mImageItems;
    private ImageLoadingThread mLoadingThread;
    private PhotoModule.NamedImages mNamedImages;
    private Uri mPlaceHolderUri;
    public static int BESTPICTURE_ACTIVITY_CODE = 11;

    static class ImageItems implements Parcelable, DotsViewItem {
        private Bitmap[] mBitmap;
        private boolean[] mChosen;
        private BestpictureActivity mActivity;

        public ImageItems(BestpictureActivity activity) {
            mBitmap = new Bitmap[NUM_IMAGES];
            mChosen = new boolean[NUM_IMAGES];
            for (int i = 0; i < mChosen.length; i++) {
                if (i == 0) {
                    mChosen[i] = true;
                } else {
                    mChosen[i] = false;
                }
            }
            mActivity = activity;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        public Bitmap getBitmap(int index) {
            return mBitmap[index];
        }

        public void setBitmap(int index, Bitmap bitmap) {
            mBitmap[index] = bitmap;
        }

        @Override
        public int getTotalItemNums() {
            return NUM_IMAGES;
        }

        public boolean isChosen(int index) {
            return mChosen[index];
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
        }

        public void toggleImageSelection(int num) {
            mChosen[num] = !mChosen[num];
            boolean isChosen = false;
            for(int i=0; i < mChosen.length; i++) {
                isChosen |= mChosen[i];
            }
            if(!isChosen) {
                mChosen[num] = true;
                RotateTextToast.makeText(mActivity,
                        mActivity.getResources().getString(R.string.bestpicture_at_least_one_picture),
                        Toast.LENGTH_SHORT).show();
            }
            mActivity.mDotsView.update();
        }
    }

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        mActivity = this;
        mFilesPath = getFilesDir()+"/Bestpicture";
        setContentView(R.layout.bestpicture_editor);
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        mWidth = size.x/2;
        mHeight = size.y/2;
        mNamedImages = new PhotoModule.NamedImages();

        mImageItems = new ImageItems(mActivity);
        mDotsView = (DotsView) findViewById(R.id.dots_view);
        mDotsView.setItems(mImageItems);
        mPlaceHolderUri = getIntent().getData();

        mImagePager = (ViewPager) findViewById(R.id.bestpicture_pager);
        mImagePagerAdapter = new ImagePagerAdapter(getFragmentManager());
        mImagePager.setAdapter(mImagePagerAdapter);
        mImagePager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                mDotsView.update(position, positionOffset);
            }

            @Override
            public void onPageSelected(int position) {
            }
        });
        findViewById(R.id.bestpicture_done).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                int index = -1;
                for(int i=0; i < mImageItems.mChosen.length; i++) {
                    if (mImageItems.mChosen[i]) {
                        if(index != -1) {
                            new SaveImageTask().execute(mFilesPath + "/" + NAMES[i] + ".jpg");
                        } else {
                            index = i;
                            saveForground(mFilesPath + "/" + NAMES[i] + ".jpg");
                        }
                    }
                }
                setResult(RESULT_OK, new Intent());
                finish();
            }
        });
    }

    private class ImageLoadingThread extends Thread {
        public void run() {
            showProgressDialog();
            for(int i=0; i < NUM_IMAGES; i++) {
                String path = mFilesPath + "/" + BestpictureActivity.NAMES[i] + ".jpg";
                final BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(path, o);
                ExifInterface exif = new ExifInterface();
                int orientation = 0;
                try {
                    exif.readExif(path);
                    orientation = Exif.getOrientation(exif);
                } catch (IOException e) {
                }
                int h = o.outHeight;
                int w = o.outWidth;
                int sample = 1;
                if (h > mHeight || w > mWidth) {
                    while (h / sample / 2 > mHeight && w / sample / 2 > mWidth) {
                        sample *= 2;
                    }
                }

                o.inJustDecodeBounds = false;
                o.inSampleSize = sample;
                Bitmap bitmap = BitmapFactory.decodeFile(path, o);
                if (orientation != 0) {
                    Matrix matrix = new Matrix();
                    matrix.setRotate(orientation);
                    bitmap = Bitmap.createBitmap(bitmap, 0, 0,
                            bitmap.getWidth(), bitmap.getHeight(), matrix, false);
                }
                mImageItems.setBitmap(i, bitmap);
            }
            dismissProgressDialog();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mLoadingThread == null) {
            mLoadingThread = new ImageLoadingThread();
            mLoadingThread.start();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private void showProgressDialog() {
        mActivity.runOnUiThread(new Runnable() {
            public void run() {
                mProgressDialog = ProgressDialog.show(mActivity, "", "Processing...", true, false);
                mProgressDialog.show();
            }
        });
    }

    private void dismissProgressDialog() {
        mActivity.runOnUiThread(new Runnable() {
            public void run() {
                if (mProgressDialog != null && mProgressDialog.isShowing()) {
                    mProgressDialog.dismiss();
                    mProgressDialog = null;
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return true;
    }

    private class ImagePagerAdapter extends FragmentStatePagerAdapter {
        public ImagePagerAdapter(android.app.FragmentManager manager) {
            super(manager);
        }

        @Override
        public android.app.Fragment getItem(int imageNum) {
           while(mImageItems.getBitmap(imageNum) == null) {
                try {
                    Thread.sleep(5);
                } catch (Exception e) {
                }
            }
            return BestpictureFragment.create(imageNum, mImageItems);
        }

        @Override
        public int getCount() {
            return NUM_IMAGES;
        }
    }

    private void saveForground(String path) {
        long captureStartTime = System.currentTimeMillis();
        mNamedImages.nameNewImage(captureStartTime);
        PhotoModule.NamedImages.NamedEntity name = mNamedImages.getNextNameEntity();
        String title = (name == null) ? null : name.title;
        String outPath = mPlaceHolderUri.getPath();
        try {
            FileOutputStream out = new FileOutputStream(outPath);
            FileInputStream in = new FileInputStream(path);
            byte[] buf = new byte[4096];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        } catch (Exception e) {
        }
    }

    private class SaveImageTask extends AsyncTask<String, Void, Void> {
        protected Void doInBackground(String... path) {
            long captureStartTime = System.currentTimeMillis();
            mNamedImages.nameNewImage(captureStartTime);
            PhotoModule.NamedImages.NamedEntity name = mNamedImages.getNextNameEntity();
            String title = (name == null) ? null : name.title;
            String outPath = Storage.generateFilepath(title, "jpeg");
            try {
                FileOutputStream out = new FileOutputStream(outPath);
                FileInputStream in = new FileInputStream(path[0]);
                byte[] buf = new byte[4096];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
            } catch (Exception e) {
            }
            Uri uri = Uri.fromFile(new File(outPath));
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
            sendBroadcast(intent);
            return null;
        }

        protected void onPostExecute(Void v) {
        }
    }
}