summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/Wallpaper.java
blob: 5c7d0e570ba2090c5043376402f5ed17033d1164 (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
/*
 * 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 android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * Wallpaper picker for the camera application. This just redirects to the standard pick action.
 */
public class Wallpaper extends Activity {
    private static final String LOG_TAG = "Camera";
    static final int PHOTO_PICKED = 1;
    static final int CROP_DONE = 2;

    static final int SHOW_PROGRESS = 0;
    static final int FINISH = 1;

    static final String sDoLaunchIcicle = "do_launch";
    static final String sTempFilePathIcicle = "temp_file_path";

    private ProgressDialog mProgressDialog = null;
    private boolean mDoLaunch = true;
    private String mTempFilePath;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SHOW_PROGRESS: {
                    CharSequence c = getText(R.string.wallpaper);
                    mProgressDialog = ProgressDialog.show(Wallpaper.this, "", c, true, false);
                    break;
                }

                case FINISH: {
                    closeProgressDialog();
                    setResult(RESULT_OK);
                    finish();
                    break;
                }
            }
        }
    };

    static class SetWallpaperThread extends Thread {
        private final Bitmap mBitmap;
        private final Handler mHandler;
        private final Context mContext;
        private final File mFile;

        public SetWallpaperThread(Bitmap bitmap, Handler handler, Context context, File file) {
            mBitmap = bitmap;
            mHandler = handler;
            mContext = context;
            mFile = file;
        }

        @Override
        public void run() {
            try {
                mContext.setWallpaper(mBitmap);
            } catch (IOException e) {
                Log.e(LOG_TAG, "Failed to set wallpaper.", e);
            } finally {
                mHandler.sendEmptyMessage(FINISH);
                mFile.delete();
            }
        }
    }

    private synchronized void closeProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        if (icicle != null) {
            mDoLaunch = icicle.getBoolean(sDoLaunchIcicle);
            mTempFilePath = icicle.getString(sTempFilePathIcicle);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle icicle) {
        icicle.putBoolean(sDoLaunchIcicle, mDoLaunch);
        icicle.putString(sTempFilePathIcicle, mTempFilePath);
    }

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

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

        if (!mDoLaunch) {
            return;
        }
        Uri imageToUse = getIntent().getData();
        if (imageToUse != null) {
            Intent intent = new Intent();
            intent.setClassName("com.android.camera", "com.android.camera.CropImage");
            intent.setData(imageToUse);
            formatIntent(intent);
            startActivityForResult(intent, CROP_DONE);
        } else {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
            intent.setType("image/*");
            intent.putExtra("crop", "true");
            formatIntent(intent);
            startActivityForResult(intent, PHOTO_PICKED);
        }
    }

    protected void formatIntent(Intent intent) {
        // TODO: A temporary file is NOT necessary
        // The CropImage intent should be able to set the wallpaper directly
        // without writing to a file, which we then need to read here to write
        // it again as the final wallpaper, this is silly
        File f = getFileStreamPath("temp-wallpaper");
        (new File(f.getParent())).mkdirs();
        mTempFilePath = f.toString();
        f.delete();

        int width = getWallpaperDesiredMinimumWidth();
        int height = getWallpaperDesiredMinimumHeight();
        intent.putExtra("outputX",         width);
        intent.putExtra("outputY",         height);
        intent.putExtra("aspectX",         width);
        intent.putExtra("aspectY",         height);
        intent.putExtra("scale",           true);
        intent.putExtra("noFaceDetection", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse("file:/" + mTempFilePath));
        intent.putExtra("outputFormat",    Bitmap.CompressFormat.PNG.name());
        // TODO: we should have an extra called "setWallpaper" to ask CropImage to
        // set the cropped image as a wallpaper directly
        // This means the SetWallpaperThread should be moved out of this class to CropImage
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if ((requestCode == PHOTO_PICKED || requestCode == CROP_DONE) && (resultCode == RESULT_OK)
                && (data != null)) {
            try {
                File tempFile = new File(mTempFilePath);
                InputStream s = new FileInputStream(tempFile);
                Bitmap bitmap = BitmapFactory.decodeStream(s);
                if (bitmap == null) {
                    Log.e(LOG_TAG, "Failed to set wallpaper.  Couldn't get bitmap for path " + mTempFilePath);
                } else {
                    if (android.util.Config.LOGV)
                        Log.v(LOG_TAG, "bitmap size is " + bitmap.getWidth() + " / " + bitmap.getHeight());
                    mHandler.sendEmptyMessage(SHOW_PROGRESS);
                    new SetWallpaperThread(bitmap, mHandler, this, tempFile).start();
                }
                mDoLaunch = false;
            } catch (FileNotFoundException ex) {

            } catch (IOException ex) {

            }
        } else {
            setResult(RESULT_CANCELED);
            finish();
        }
    }
}