Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.theme.app;
     18 
     19 import android.graphics.Bitmap;
     20 import android.graphics.Bitmap.CompressFormat;
     21 import android.graphics.Canvas;
     22 import android.os.AsyncTask;
     23 import android.os.Environment;
     24 import android.util.Log;
     25 import android.view.View;
     26 
     27 import java.io.File;
     28 import java.io.FileOutputStream;
     29 
     30 /**
     31  * A task which gets the UI element to render to a bitmap and then saves that
     32  * as a PNG asynchronously.
     33  */
     34 class GenerateBitmapTask extends AsyncTask<Void, Void, Boolean> {
     35     private static final String TAG = "GenerateBitmapTask";
     36 
     37     private final View mView;
     38     private final File mOutDir;
     39 
     40     private Bitmap mBitmap;
     41 
     42     protected final String mName;
     43 
     44     public GenerateBitmapTask(View view, File outDir, String name) {
     45         mView = view;
     46         mOutDir = outDir;
     47         mName = name;
     48     }
     49 
     50     @Override
     51     protected void onPreExecute() {
     52         if (mView.getWidth() == 0 || mView.getHeight() == 0) {
     53             Log.e(TAG, "Unable to draw view due to incorrect size: " + mName);
     54             return;
     55         }
     56 
     57         mBitmap = Bitmap.createBitmap(mView.getWidth(), mView.getHeight(),
     58                 Bitmap.Config.ARGB_8888);
     59 
     60         final Canvas canvas = new Canvas(mBitmap);
     61         mView.draw(canvas);
     62     }
     63 
     64     @Override
     65     protected Boolean doInBackground(Void... ignored) {
     66         final Bitmap bitmap = mBitmap;
     67         if (bitmap == null) {
     68             return false;
     69         }
     70 
     71         final File file = new File(mOutDir, mName + ".png");
     72         if (file.exists() && !file.canWrite()) {
     73             Log.e(TAG, "Unable to write file: " + file.getAbsolutePath());
     74             return false;
     75         }
     76 
     77         boolean success = false;
     78         try {
     79             FileOutputStream stream = null;
     80             try {
     81                 stream = new FileOutputStream(file);
     82                 success = bitmap.compress(CompressFormat.PNG, 100, stream);
     83             } finally {
     84                 if (stream != null) {
     85                     stream.flush();
     86                     stream.close();
     87                 }
     88             }
     89         } catch (Exception e) {
     90             Log.e(TAG, e.getMessage());
     91         } finally {
     92             bitmap.recycle();
     93         }
     94 
     95         return success;
     96     }
     97 }
     98