Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

update setting wallpaper routine

  • Loading branch information...
commit 1bd241223b07146f0457ac3a33ed66d5f27101de 1 parent f92feb5
Roman Birg authored
1  res/values/strings.xml
@@ -17,5 +17,6 @@
17 17
     <string name="wars">Wallpaper Wars</string>
18 18
     <string name="vote">Vote!</string>
19 19
     <string name="wallpaper">wallpaper</string>
  20
+    <string name="setting_wallpaper">Setting wallpaper…</string>
20 21
 
21 22
 </resources>
95  src/com/aokp/swagpapers/Preview.java
@@ -9,7 +9,10 @@
9 9
 import android.content.Intent;
10 10
 import android.content.res.Configuration;
11 11
 import android.graphics.Bitmap;
  12
+import android.graphics.Bitmap.Config;
12 13
 import android.graphics.BitmapFactory;
  14
+import android.graphics.Canvas;
  15
+import android.graphics.drawable.BitmapDrawable;
13 16
 import android.graphics.drawable.Drawable;
14 17
 import android.os.AsyncTask;
15 18
 import android.os.Bundle;
@@ -48,6 +51,7 @@
48 51
 
49 52
     String fileDest = null;
50 53
     String fileName = null;
  54
+    Drawable loadedWallpaper = null;
51 55
 
52 56
     @Override
53 57
     public void onCreate(Bundle savedInstanceState) {
@@ -75,6 +79,7 @@ public void onLoaded(ImageView imageView, Drawable loadedDrawable, String url,
75 79
                         boolean loadedFromCache) {
76 80
                     progress.setIndeterminate(false);
77 81
                     progress.setVisibility(View.GONE);
  82
+                    loadedWallpaper = loadedDrawable;
78 83
                 }
79 84
             });
80 85
         } else {
@@ -163,8 +168,9 @@ protected Dialog onCreateDialog(int id) {
163 168
         switch (id) {
164 169
             case DIALOG_DOWNLOAD_PROGRESS:
165 170
                 mProgressDialog = new ProgressDialog(this);
166  
-                mProgressDialog.setMessage(getResources().getString(R.string.downloading));
167  
-                mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  171
+                mProgressDialog.setMessage(getResources().getString(R.string.setting_wallpaper));
  172
+                mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  173
+                mProgressDialog.setIndeterminate(true);
168 174
                 mProgressDialog.setCancelable(true);
169 175
                 mProgressDialog.show();
170 176
                 return mProgressDialog;
@@ -186,72 +192,31 @@ protected void onPreExecute() {
186 192
 
187 193
         @Override
188 194
         protected String doInBackground(String... aurl) {
189  
-            int count;
190  
-
  195
+            WallpaperManager wallpaperManager = WallpaperManager
  196
+                    .getInstance(getApplicationContext());
191 197
             try {
192  
-
193  
-                mProgressDialog.setProgress(0);
194  
-
195  
-                URL url = new URL(aurl[0]);
196  
-                URLConnection conexion = url.openConnection();
197  
-                conexion.connect();
198  
-
199  
-                SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
200  
-                Date now = new Date();
201  
-                fileName = formatter.format(now) + ".jpg";
202  
-
203  
-                int lengthOfFile = conexion.getContentLength();
204  
-                Log.d("ANDRO_ASYNC", "Lenght of file: " + lengthOfFile);
205  
-
206  
-                InputStream input = new BufferedInputStream(url.openStream());
207  
-                OutputStream output = new FileOutputStream(getDlDir() + fileName);
208  
-                fileDest = getDlDir() + fileName;
209  
-
210  
-                byte data[] = new byte[1024];
211  
-
212  
-                long total = 0;
213  
-
214  
-                while ((count = input.read(data)) != -1) {
215  
-                    total += count;
216  
-                    publishProgress("" + (int) ((total * 100) / lengthOfFile));
217  
-                    output.write(data, 0, count);
218  
-                }
219  
-
220  
-                output.flush();
221  
-                output.close();
222  
-                input.close();
223  
-            } catch (Exception e) {
  198
+                wallpaperManager.setBitmap(drawableToBitmap(loadedWallpaper));
  199
+            } catch (IOException e) {
  200
+                e.printStackTrace();
  201
+            }
  202
+            File file = new File(getDlDir() + fileName);
  203
+            if (file.exists() == true) {
  204
+                file.delete();
224 205
             }
225  
-
226 206
             return null;
227 207
         }
228 208
 
  209
+        @Override
  210
+        protected void onPostExecute(String result) {
  211
+            mProgressDialog.cancel();
  212
+            Toast.makeText(getApplicationContext(), R.string.set, Toast.LENGTH_LONG).show();
  213
+        }
  214
+
229 215
         protected void onProgressUpdate(String... progress) {
230 216
             Log.d("ANDRO_ASYNC", progress[0]);
231 217
             mProgressDialog.setProgress(Integer.parseInt(progress[0]));
232 218
         }
233 219
 
234  
-        @SuppressWarnings("deprecation")
235  
-        @Override
236  
-        protected void onPostExecute(String unused) {
237  
-            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
238  
-            mProgressDialog.setProgress(0);
239  
-            WallpaperManager wallpaperManager = WallpaperManager.getInstance(Preview.this);
240  
-            getResources().getDrawable(R.drawable.ic_launcher);
241  
-            Bitmap wallpaper = BitmapFactory.decodeFile(fileDest);
242  
-            try {
243  
-                wallpaperManager.setBitmap(wallpaper);
244  
-            } catch (IOException e) {
245  
-                // TODO Auto-generated catch block
246  
-                e.printStackTrace();
247  
-            }
248  
-            Toast.makeText(getApplicationContext(), R.string.set, Toast.LENGTH_LONG).show();
249  
-            File file = new File(getDlDir() + fileName);
250  
-            if (file.exists() == true) {
251  
-                file.delete();
252  
-
253  
-            }
254  
-        }
255 220
     }
256 221
 
257 222
     // Downloads the wallpaper in Async with a dialog
@@ -329,4 +294,18 @@ String getDlDir() {
329 294
     String getSvDir() {
330 295
         return WallpaperActivity.getSvDir(getApplicationContext());
331 296
     }
  297
+
  298
+    public static Bitmap drawableToBitmap(Drawable drawable) {
  299
+        if (drawable instanceof BitmapDrawable) {
  300
+            return ((BitmapDrawable) drawable).getBitmap();
  301
+        }
  302
+
  303
+        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
  304
+                drawable.getIntrinsicHeight(), Config.ARGB_8888);
  305
+        Canvas canvas = new Canvas(bitmap);
  306
+        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
  307
+        drawable.draw(canvas);
  308
+
  309
+        return bitmap;
  310
+    }
332 311
 }

0 notes on commit 1bd2412

Please sign in to comment.
Something went wrong with that request. Please try again.