summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/photoeditor/SaveCopyTask.java
blob: e43959e9468f94a39daae46397462dc34376fae2 (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
/*
 * Copyright (C) 2010 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.gallery3d.photoeditor;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.ImageColumns;
import android.view.Gravity;
import android.widget.Toast;

import com.android.gallery3d.R;

import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;

/**
 * Asynchronous task for saving edited photo as a new copy.
 */
public class SaveCopyTask extends AsyncTask<Bitmap, Void, Uri> {

    /**
     * Callback for the completed asynchronous task.
     */
    public interface Callback {

        void onComplete(Uri uri);
    }

    private static final String TIME_STAMP_NAME = "'IMG'_yyyyMMdd_HHmmss";
    private static final int INDEX_DATE_TAKEN = 0;
    private static final int INDEX_LATITUDE = 1;
    private static final int INDEX_LONGITUDE = 2;

    private static final String[] IMAGE_PROJECTION = new String[] {
        ImageColumns.DATE_TAKEN,
        ImageColumns.LATITUDE,
        ImageColumns.LONGITUDE,
    };

    private final Context context;
    private final Uri sourceUri;
    private final Callback callback;
    private final String albumName;
    private final String saveFileName;

    public SaveCopyTask(Context context, Uri sourceUri, Callback callback) {
        this.context = context;
        this.sourceUri = sourceUri;
        this.callback = callback;

        albumName = context.getString(R.string.edited_photo_bucket_name);
        saveFileName = new SimpleDateFormat(TIME_STAMP_NAME).format(
                new Date(System.currentTimeMillis()));
    }

    /**
     * The task should be executed with one given bitmap to be saved.
     */
    @Override
    protected Uri doInBackground(Bitmap... params) {
        // TODO: Support larger dimensions for photo saving.
        if (params[0] == null) {
            return null;
        }
        Bitmap bitmap = params[0];
        File file = save(bitmap);
        Uri uri = (file != null) ? insertContent(file) : null;
        bitmap.recycle();
        return uri;
    }

    @Override
    protected void onPostExecute(Uri result) {
        String message = (result == null) ? context.getString(R.string.saving_failure)
                : context.getString(R.string.photo_saved, albumName);
        Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();

        callback.onComplete(result);
    }

    private File save(Bitmap bitmap) {
        String directory = Environment.getExternalStorageDirectory().toString() + "/" + albumName;
        return new BitmapUtils(context).saveBitmap(
                bitmap, directory, saveFileName, Bitmap.CompressFormat.JPEG);
    }

    /**
     * Insert the content (saved file) with proper source photo properties.
     */
    private Uri insertContent(File file) {
        long now = System.currentTimeMillis() / 1000;
        long dateTaken = now;
        double latitude = 0f;
        double longitude = 0f;

        ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = contentResolver.query(
                sourceUri, IMAGE_PROJECTION, null, null, null);
        try {
            if ((cursor != null) && cursor.moveToNext()) {
                dateTaken = cursor.getLong(INDEX_DATE_TAKEN);
                latitude = cursor.getDouble(INDEX_LATITUDE);
                longitude = cursor.getDouble(INDEX_LONGITUDE);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        ContentValues values = new ContentValues();
        values.put(Images.Media.TITLE, saveFileName);
        values.put(Images.Media.DISPLAY_NAME, saveFileName);
        values.put(Images.Media.MIME_TYPE, "image/jpeg");
        values.put(Images.Media.DATE_TAKEN, dateTaken);
        values.put(Images.Media.DATE_MODIFIED, now);
        values.put(Images.Media.DATE_ADDED, now);
        values.put(Images.Media.ORIENTATION, 0);
        values.put(Images.Media.DATA, file.getAbsolutePath());
        values.put(Images.Media.SIZE, file.length());

        // TODO: Change || to && after the default location issue is fixed.
        if ((latitude != 0f) || (longitude != 0f)) {
            values.put(Images.Media.LATITUDE, latitude);
            values.put(Images.Media.LONGITUDE, longitude);
        }
        return contentResolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values);
    }
}